Created
December 15, 2022 13:46
-
-
Save 90K2/b75160e289f201f1282ee1d20a2a3bc2 to your computer and use it in GitHub Desktop.
LiteClient bean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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