Skip to content

Instantly share code, notes, and snippets.

@90K2
Created December 15, 2022 13:46
Show Gist options
  • Save 90K2/b75160e289f201f1282ee1d20a2a3bc2 to your computer and use it in GitHub Desktop.
Save 90K2/b75160e289f201f1282ee1d20a2a3bc2 to your computer and use it in GitHub Desktop.
LiteClient bean
@Bean("liteClientExtended")
fun liteClient(tonChainConfigReader: TonChainConfigReader): LiteClient {
val configList = tonChainConfigReader.load().liteservers
val nearestNodesList = mutableListOf<TonChainConfigReader.TonNetConfig.LiteServerParams>()
configList.forEach {
val liteClient = LiteClient(
LiteClientConfigGlobal(
liteservers = listOf(
LiteServerDesc(id = PublicKeyEd25519(base64(it.id.key)), ip = it.ip, port = it.port)
),
validator = ValidatorConfigGlobal()
)
)
runBlocking {
try {
val lastBlockId = liteClient.getLastBlockId()
val start = utcTsNow()
liteClient.getBlock(lastBlockId)
val delay = utcTsNow().time - start.time
println("NODECHECK ${ipv4IntToStr(it.ip)} getLastBlock took $delay ms")
if (delay < 350)
nearestNodesList.add(it)
} catch (ex: Exception) {
println("NODECHECK ${ipv4IntToStr(it.ip)} FAIL")
}
}
}
println("NODECHECK: suggested to use ${nearestNodesList.size}/${configList.size} liteservers")
return LiteClient(
LiteClientConfigGlobal(
liteservers = (nearestNodesList.takeIf { it.isNotEmpty() } ?: configList ).map {
LiteServerDesc(id = PublicKeyEd25519(base64(it.id.key)), ip = it.ip, port = it.port)
},
validator = ValidatorConfigGlobal()
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment