Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.4.0 #124

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

0.4.0 #124

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 4 additions & 2 deletions TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
- [BitString concatenation with shifting](https://github.com/andreypfau/ton-kotlin/blob/363504ec96e821d4178dc09a2234377fd02808e9/ton-bitstring/src/commonTest/kotlin/org/ton/bitstring/BitStringTest.kt#L57)
- [BitString concatenation with double-shifting](https://github.com/andreypfau/ton-kotlin/blob/363504ec96e821d4178dc09a2234377fd02808e9/ton-bitstring/src/commonTest/kotlin/org/ton/bitstring/BitStringTest.kt#L68)
- [BitString.toString() on a zero number](https://github.com/andreypfau/ton-kotlin/blob/363504ec96e821d4178dc09a2234377fd02808e9/ton-bitstring/src/commonTest/kotlin/org/ton/bitstring/BitStringTest.kt#L107)
- [check BitString.size in `0..1023`](https://github.com/andreypfau/ton-kotlin/blob/3b02ad6c729e14fff8c023801062f9505cc6ed4a/ton-bitstring/src/commonMain/kotlin/org/ton/bitstring/ByteBackedBitString.kt#L167)
- [check BitString.size in
`0..1023`](https://github.com/andreypfau/ton-kotlin/blob/3b02ad6c729e14fff8c023801062f9505cc6ed4a/ton-bitstring/src/commonMain/kotlin/org/ton/bitstring/ByteBackedBitString.kt#L167)
- [BitString assertions for all tests](https://github.com/andreypfau/ton-kotlin/blob/363504ec96e821d4178dc09a2234377fd02808e9/ton-bitstring/src/commonTest/kotlin/org/ton/bitstring/BitStringTest.kt#L114)
- [from binary == from hex](https://github.com/andreypfau/ton-kotlin/blob/363504ec96e821d4178dc09a2234377fd02808e9/ton-bitstring/src/commonTest/kotlin/org/ton/bitstring/BitStringTest.kt#L119)
- [from binary toString() == from hex toString()](https://github.com/andreypfau/ton-kotlin/blob/363504ec96e821d4178dc09a2234377fd02808e9/ton-bitstring/src/commonTest/kotlin/org/ton/bitstring/BitStringTest.kt#L121)
Expand Down Expand Up @@ -99,7 +100,8 @@
- [check Bag-of-Cells (de)serialization equals](https://github.com/andreypfau/ton-kotlin/blob/addf79aeed62e87da74049aa1e720f96a791edde/ton-boc/src/jvmTest/kotlin/BagOfCellsTest.kt#L12)
- check Bag-of-Cells crc32c checksum calculation
- [check that cell offset indexes greater than previous](https://github.com/andreypfau/ton-kotlin/blob/addf79aeed62e87da74049aa1e720f96a791edde/ton-boc/src/commonMain/kotlin/org/ton/boc/BagOfCellsUtils.kt#L71)
- [check cell references count in `0..4`](https://github.com/andreypfau/ton-kotlin/blob/addf79aeed62e87da74049aa1e720f96a791edde/ton-boc/src/commonMain/kotlin/org/ton/boc/BagOfCellsUtils.kt#L95)
- [check cell references count in
`0..4`](https://github.com/andreypfau/ton-kotlin/blob/addf79aeed62e87da74049aa1e720f96a791edde/ton-boc/src/commonMain/kotlin/org/ton/boc/BagOfCellsUtils.kt#L95)
- [check invalid cell reference indexes](https://github.com/andreypfau/ton-kotlin/blob/addf79aeed62e87da74049aa1e720f96a791edde/ton-boc/src/commonMain/kotlin/org/ton/boc/BagOfCellsUtils.kt#L121)
- [check cell depth less than 1024](https://github.com/andreypfau/ton-kotlin/blob/6e2f83fc80f19466c84289c40e6de396b7320752/ton-boc/src/commonMain/kotlin/org/ton/boc/CachedBagOfCells.kt#L62)
- [check cell order algorithm. TODO: make unit-tests for illegal cell order](https://github.com/andreypfau/ton-kotlin/blob/6e2f83fc80f19466c84289c40e6de396b7320752/ton-boc/src/commonMain/kotlin/org/ton/boc/CachedBagOfCells.kt#L89)
Expand Down
84 changes: 49 additions & 35 deletions adnl/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,39 +1,53 @@
plugins {
id("multiplatform")
id("publish")
id("buildsrc.convention.multiplatform")
id("buildsrc.convention.layout")
id("buildsrc.convention.publish")
}

kotlin {
sourceSets {
commonMain {
dependencies {
api(projects.tonKotlinTonapiTl)
implementation(libs.serialization.json)
implementation(libs.atomicfu)
implementation(libs.datetime)
implementation(libs.coroutines.core)
implementation(libs.ktor.utils)
}
}
nativeMain {
dependencies {
implementation(libs.bignum)
}
}
appleMain {
dependencies {
implementation(libs.ktor.network)
}
}
jvmMain {
dependencies {
implementation(libs.ktor.network)
}
}
linuxMain {
dependencies {
implementation(libs.ktor.network)
}
}
}
dependencies {
commonMainApi(projects.tonKotlinTonapiTl)
commonMainApi(libs.coroutines.core)
commonMainApi(libs.datetime)
commonMainApi(libs.ktor.network)
commonMainApi(libs.atomicfu)
}

//plugins {
// id("multiplatform")
// id("publish")
//}
//
//kotlin {
// sourceSets {
// commonMain {
// dependencies {
// api(projects.tonKotlinTonapiTl)
// implementation(libs.serialization.json)
// implementation(libs.atomicfu)
// implementation(libs.datetime)
// implementation(libs.coroutines.core)
// implementation(libs.ktor.utils)
// }
// }
// nativeMain {
// dependencies {
// implementation(libs.bignum)
// }
// }
// appleMain {
// dependencies {
// implementation(libs.ktor.network)
// }
// }
// jvmMain {
// dependencies {
// implementation(libs.ktor.network)
// }
// }
// linuxMain {
// dependencies {
// implementation(libs.ktor.network)
// }
// }
// }
//}
33 changes: 19 additions & 14 deletions adnl/src/connection/AdnlConnection.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.ton.adnl.connection

import io.github.andreypfau.kotlinx.crypto.aes.AES
import io.github.andreypfau.kotlinx.crypto.cipher.CTRBlockCipher
import io.github.andreypfau.kotlinx.crypto.sha2.SHA256
import io.github.andreypfau.kotlinx.crypto.AES
import io.github.andreypfau.kotlinx.crypto.CTRBlockCipher
import io.github.andreypfau.kotlinx.crypto.Sha256
import io.github.andreypfau.kotlinx.crypto.sha256
import io.ktor.utils.io.*
import io.ktor.utils.io.core.*
import io.ktor.utils.io.errors.*
Expand All @@ -11,6 +12,10 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.io.readByteArray
import kotlinx.io.readIntLe
import kotlinx.io.write
import kotlinx.io.writeIntLe
import org.ton.adnl.network.TcpClient
import org.ton.api.liteserver.LiteServerDesc
import org.ton.crypto.SecureRandom
Expand Down Expand Up @@ -65,10 +70,10 @@ public class AdnlConnection(
}
}

connection.output.writePacket {
writeFully(liteServerDesc.id.toAdnlIdShort().id.toByteArray())
writeFully(liteServerDesc.id.encrypt(nonce))
}
val buffer = kotlinx.io.Buffer()
buffer.write(liteServerDesc.id.toAdnlIdShort().id)
buffer.write(liteServerDesc.id.encrypt(nonce))
connection.output.writePacket(buffer)
connection.output.flush()

val cipher = ChannelCipher(nonce)
Expand Down Expand Up @@ -126,7 +131,7 @@ public class AdnlConnection(
} finally {
output.flush()
if (closeChannel) {
output.close()
output.flushAndClose()
}
}
}
Expand Down Expand Up @@ -154,7 +159,7 @@ public class AdnlConnection(
val plainLength = ByteArray(4)
cipher.processBytes(encryptedLength, plainLength)

val length = ByteReadPacket(plainLength).readIntLittleEndian()
val length = ByteReadPacket(plainLength).readIntLe()
check(length in 32..(1 shl 24)) { "Invalid length" }
val encryptedData = input.readPacket(length).readBytes()
val plainData = ByteArray(length)
Expand All @@ -164,7 +169,7 @@ public class AdnlConnection(
val payload = data.readBytes((data.remaining - 32).toInt())
val hash = data.readBytes(32)

require(io.github.andreypfau.kotlinx.crypto.sha2.sha256(payload).contentEquals(hash)) {
require(sha256(payload).contentEquals(hash)) {
"sha256 mismatch"
}

Expand All @@ -183,20 +188,20 @@ public class AdnlConnection(
val nonce = SecureRandom.nextBytes(32)
val payload = packet.readBytes()

val hash = SHA256().apply {
val hash = Sha256().apply {
update(nonce)
update(payload)
}.digest()

val data = buildPacket {
writeIntLittleEndian(dataSize)
val data = Buffer().apply {
writeIntLe(dataSize)
writeFully(nonce)
writeFully(payload)
writeFully(hash)
}

val encryptedData = ByteArray(data.remaining.toInt())
cipher.processBytes(data.readBytes(), encryptedData)
cipher.processBytes(data.readByteArray(), encryptedData)
output.writeFully(encryptedData)
}

Expand Down
4 changes: 2 additions & 2 deletions adnl/src/connection/AdnlRequestData.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.ton.adnl.connection

import io.ktor.utils.io.core.*
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Job
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.io.Source
import org.ton.api.http.functions.HttpRequest
import kotlin.coroutines.CoroutineContext

Expand All @@ -15,7 +15,7 @@ public class AdnlRequestData(

public class AdnlResponseData(
public val requestTime: Instant,
public val body: ByteReadPacket,
public val body: Source,
public val callContext: CoroutineContext
) {
public val responseTime: Instant = Clock.System.now()
Expand Down
10 changes: 9 additions & 1 deletion adnl/src/network/TcpClientImpl.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
package org.ton.adnl.network

public expect class TcpClientImpl() : TcpClient
import io.ktor.utils.io.*

public expect class TcpClientImpl() : TcpClient {
override val input: ByteReadChannel
override val output: ByteWriteChannel
override suspend fun connect(host: String, port: Int)
override fun close(cause: Throwable?)
override fun close()
}
6 changes: 3 additions & 3 deletions adnl/src/network/UdpServer.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.ton.adnl.network

import io.ktor.utils.io.core.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.io.Source
import kotlin.coroutines.CoroutineContext

public interface UdpServer : CoroutineScope {
public suspend fun send(address: IPAddress, data: ByteReadPacket)
public suspend fun send(address: IPAddress, data: Source)

public fun interface Callback {
public fun receive(address: IPAddress, data: ByteReadPacket)
public fun receive(address: IPAddress, data: Source)
}

public companion object {
Expand Down
3 changes: 3 additions & 0 deletions adnl/src/network/UdpServerImpl.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ton.adnl.network

import kotlinx.io.Source
import kotlin.coroutines.CoroutineContext

internal expect class UdpServerImpl(
Expand All @@ -8,4 +9,6 @@ internal expect class UdpServerImpl(
callback: UdpServer.Callback
) : UdpServer {
val port: Int
override suspend fun send(address: IPAddress, data: Source)
override val coroutineContext: CoroutineContext
}
34 changes: 0 additions & 34 deletions adnl/src@apple/network/UdpServerImpl.kt

This file was deleted.

5 changes: 4 additions & 1 deletion adnl/src@jvm/engine/CIOAdnlNetworkEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.ktor.network.sockets.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.newFixedThreadPoolContext
import kotlinx.coroutines.runBlocking
import org.ton.adnl.ipv4
import org.ton.adnl.utils.toAdnlUdpAddress
import org.ton.adnl.utils.toSocketAddress
Expand All @@ -16,7 +17,9 @@ public class CIOAdnlNetworkEngine(
public constructor(port: Int) : this(AdnlAddressUdp(ipv4("0.0.0.0"), port))

public val socket: BoundDatagramSocket =
aSocket(ActorSelectorManager(DISPATCHER)).udp().bind(localAddress.toSocketAddress())
runBlocking {
aSocket(ActorSelectorManager(DISPATCHER)).udp().bind(localAddress.toSocketAddress())
}

override suspend fun sendDatagram(adnlAddress: AdnlAddressUdp, payload: ByteReadPacket) {
val datagram = Datagram(payload, adnlAddress.toSocketAddress())
Expand Down
15 changes: 7 additions & 8 deletions adnl/src@jvm/network/TcpClientImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,30 @@ package org.ton.adnl.network
import io.ktor.network.selector.*
import io.ktor.network.sockets.*
import io.ktor.utils.io.*
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.newFixedThreadPoolContext

public actual class TcpClientImpl actual constructor(
) : TcpClient {

private var isClosed by atomic(false)
private var isClosed = false
private lateinit var socket: Socket
private lateinit var connection: Connection
override val input: ByteReadChannel
actual override val input: ByteReadChannel
get() = connection.input
override val output: ByteWriteChannel
actual override val output: ByteWriteChannel
get() = connection.output

override suspend fun connect(host: String, port: Int) {
socket = aSocket(selectorManager).tcpNoDelay().tcp().connect(host, port)
actual override suspend fun connect(host: String, port: Int) {
socket = aSocket(selectorManager).tcp().connect(host, port)
connection = socket.connection()
isClosed = false
}

override fun close() {
actual override fun close() {
close(null)
}

override fun close(cause: Throwable?) {
actual override fun close(cause: Throwable?) {
if (isClosed) return
connection.input.cancel(cause)
connection.output.close(cause)
Expand Down
Loading