Improve archive detection algorithm for node (#249)

This commit is contained in:
Vyacheslav
2023-07-14 16:17:21 +03:00
committed by GitHub
parent fb91effd26
commit 2031d18199
2 changed files with 17 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ import io.emeraldpay.dshackle.Global.Companion.objectMapper
import io.emeraldpay.dshackle.reader.JsonRpcReader
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse
import io.emeraldpay.etherjar.hex.HexQuantity
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
@@ -39,7 +40,17 @@ class EthereumLabelsDetector(
private fun detectArchiveNode(): Mono<Pair<String, String>> {
return reader
.read(JsonRpcRequest("eth_getBalance", listOf("0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0", "0x1")))
.read(JsonRpcRequest("eth_blockNumber", listOf()))
.flatMap(JsonRpcResponse::requireResult)
.flatMap {
val blockNum = HexQuantity.from(String(it).substring(3, it.size - 1).toLong(radix = 16) - 10_000) // this is definitely archive
reader.read(
JsonRpcRequest(
"eth_getBalance",
listOf("0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0", blockNum.toHex())
)
)
}
.flatMap(JsonRpcResponse::requireResult)
.map { "archive" to "true" }
.onErrorResume { Mono.empty() }

View File

@@ -21,7 +21,8 @@ class EthereumLabelsDetectorSpec extends Specification {
def up = TestingCommons.upstream(
new ApiReaderMock().tap {
answer("web3_clientVersion", [], response)
answer("eth_getBalance", ["0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0", "0x1"], "")
answer("eth_blockNumber", [], "0x10df3e5")
answer("eth_getBalance", ["0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0", "0x10dccd5"], "")
}
)
def detector = new EthereumLabelsDetector(up.getIngressReader())
@@ -50,7 +51,9 @@ class EthereumLabelsDetectorSpec extends Specification {
1 * getIngressReader() >> Mock(Reader) {
1 * read(new JsonRpcRequest("web3_clientVersion", [])) >>
Mono.just(new JsonRpcResponse('no/v1.19.3+e8ac1da4/linux-x64/dotnet7.0.8'.getBytes(), null))
1 * read(new JsonRpcRequest("eth_getBalance", ["0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0", "0x1"])) >>
1 * read(new JsonRpcRequest("eth_blockNumber", [])) >>
Mono.just(new JsonRpcResponse("\"0x10df3e5\"".getBytes(), null))
1 * read(new JsonRpcRequest("eth_getBalance", ["0x756F45E3FA69347A9A973A725E3C98bC4db0b5a0", "0x10dccd5"])) >>
Mono.error(new RuntimeException())
}
}