Skip to content

Commit

Permalink
fix lookup block
Browse files Browse the repository at this point in the history
  • Loading branch information
andreypfau committed Jan 21, 2023
1 parent 667570d commit c45e5cc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import kotlinx.serialization.Serializable
import org.ton.api.tonnode.TonNodeBlockId
import org.ton.lite.api.liteserver.LiteServerBlockHeader
import org.ton.tl.*
import kotlin.jvm.JvmStatic

@Serializable
@SerialName("liteServer.lookupBlock")
Expand All @@ -15,42 +14,13 @@ public data class LiteServerLookupBlock(
val lt: Long?,
val utime: Int?
) : TLFunction<LiteServerLookupBlock, LiteServerBlockHeader> {
public constructor(id: TonNodeBlockId, lt: Long?, utime: Int?) : this(
mode(
lt != null,
utime != null
), id, lt, utime
)

public constructor(id: TonNodeBlockId, lt: Long) : this(id, lt, null)
public constructor(id: TonNodeBlockId, utime: Int) : this(id, null, utime)

override fun tlCodec(): TlCodec<LiteServerLookupBlock> = LiteServerLookupBlockTlConstructor
override fun resultTlCodec(): TlCodec<LiteServerBlockHeader> = LiteServerBlockHeader

public companion object : TlCodec<LiteServerLookupBlock> by LiteServerLookupBlockTlConstructor {
@JvmStatic
public fun mode(
hasLt: Boolean,
hasUtime: Boolean
): Int {
var mode = 0
if (hasLt) mode = mode or 1
if (hasUtime) mode = mode or 2
return mode
}

@JvmStatic
public fun byTime(
id: TonNodeBlockId,
utime: Int
): LiteServerLookupBlock = LiteServerLookupBlock(id, utime)

@JvmStatic
public fun byLt(
id: TonNodeBlockId,
lt: Long
): LiteServerLookupBlock = LiteServerLookupBlock(id, lt)
public const val ID_MASK: Int = 1
public const val LT_MASK: Int = 2
public const val UTIME_MASK: Int = 4
}
}

Expand Down
50 changes: 50 additions & 0 deletions ton-kotlin-liteclient/jvm/test/BlockSubscriberExample.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import io.ktor.util.*
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import org.ton.api.liteserver.LiteServerDesc
import org.ton.api.pub.PublicKeyEd25519
import org.ton.api.tonnode.TonNodeBlockId
import org.ton.api.tonnode.TonNodeBlockIdExt
import org.ton.lite.client.LiteClient

suspend fun main() {
val liteClient = LiteClient(
CoroutineName("liteClient"),
LiteServerDesc(
ip = 1091931625,
port = 30131,
id = PublicKeyEd25519("wrQaeIFispPfHndEBc0s0fx7GSp8UFFvebnytQQfc6A=".decodeBase64Bytes())
)
)

val mutableFlow = MutableStateFlow(
liteClient.getLastBlockId()
)
GlobalScope.launch {
while (true) {
mutableFlow.update {
var block: TonNodeBlockIdExt? = null
while (block == null) {
block = liteClient.lookupBlock(
TonNodeBlockId(
it.workchain,
it.shard,
it.seqno + 1,
)
)
}
block
}
}
}

val flow = mutableFlow.asStateFlow()
flow.collectLatest {
println(it)
}
}
33 changes: 0 additions & 33 deletions ton-kotlin-liteclient/jvm/test/test.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import org.ton.cell.CellType
import org.ton.crypto.crc16
import org.ton.crypto.sha256
import org.ton.lite.api.LiteApiClient
import org.ton.lite.api.exception.LiteServerNotReadyException
import org.ton.lite.api.exception.LiteServerUnknownException
import org.ton.lite.api.liteserver.*
import org.ton.lite.api.liteserver.functions.*
import org.ton.logger.Logger
Expand Down Expand Up @@ -241,15 +243,30 @@ public class LiteClient(
if (knownBlockId != null) {
return knownBlockId
}
val mode = when {
time != null -> LiteServerLookupBlock.UTIME_MASK
lt != null -> LiteServerLookupBlock.LT_MASK
else -> LiteServerLookupBlock.ID_MASK
}
val blockHeader = try {
liteApi(LiteServerLookupBlock(blockId, lt, time?.epochSeconds?.toInt()))
} catch (e: TonNotReadyException) {
liteApi(LiteServerLookupBlock(mode, blockId, lt, time?.epochSeconds?.toInt()))
} catch (e: LiteServerNotReadyException) {
return null
} catch (e: LiteServerUnknownException) {
if (e.message == "block is not applied") {
return null
} else {
throw e
}
} catch (e: Exception) {
throw RuntimeException("Can't lookup block header for $blockId from server", e)
}
val actualBlockId = blockHeader.id
check((!blockId.isValid()) || blockId == actualBlockId) {
check(
blockId.workchain == actualBlockId.workchain &&
blockId.shard == actualBlockId.shard &&
blockId.seqno == actualBlockId.seqno
) {
"block id mismatch, expected: $blockId actual: $actualBlockId"
}
val blockProofCell = try {
Expand Down

0 comments on commit c45e5cc

Please sign in to comment.