Add client version to labels (#346)

This commit is contained in:
KirillPamPam
2023-11-21 12:38:07 +04:00
committed by GitHub
parent 4f9b3e7fe4
commit 4b0ccf9bb5
2 changed files with 38 additions and 18 deletions

View File

@@ -9,6 +9,7 @@ import io.emeraldpay.dshackle.upstream.LabelsDetector
import io.emeraldpay.dshackle.upstream.ethereum.EthereumArchiveBlockNumberReader import io.emeraldpay.dshackle.upstream.ethereum.EthereumArchiveBlockNumberReader
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse
import org.slf4j.LoggerFactory
import reactor.core.publisher.Flux import reactor.core.publisher.Flux
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
@@ -18,6 +19,10 @@ class EthereumLabelsDetector(
) : LabelsDetector { ) : LabelsDetector {
private val blockNumberReader = EthereumArchiveBlockNumberReader(reader) private val blockNumberReader = EthereumArchiveBlockNumberReader(reader)
companion object {
private val log = LoggerFactory.getLogger(EthereumLabelsDetector::class.java)
}
override fun detectLabels(): Flux<Pair<String, String>> { override fun detectLabels(): Flux<Pair<String, String>> {
return Flux.merge( return Flux.merge(
detectNodeType(), detectNodeType(),
@@ -25,21 +30,25 @@ class EthereumLabelsDetector(
) )
} }
private fun detectNodeType(): Mono<Pair<String, String>?> { private fun detectNodeType(): Flux<Pair<String, String>?> {
return reader return reader
.read(JsonRpcRequest("web3_clientVersion", listOf())) .read(JsonRpcRequest("web3_clientVersion", listOf()))
.flatMap(JsonRpcResponse::requireResult) .flatMap(JsonRpcResponse::requireResult)
.mapNotNull { .map { objectMapper.readValue<JsonNode>(it) }
val node = objectMapper.readValue<JsonNode>(it) .flatMapMany { node ->
val labels = mutableListOf<Pair<String, String>>()
if (node.isTextual) { if (node.isTextual) {
nodeType(node.textValue())?.run { clientType(node.textValue())?.let {
"client_type" to this labels.add("client_type" to it)
}
clientVersion(node.textValue())?.let {
labels.add("client_version" to it)
} }
} else {
null
} }
Flux.fromIterable(labels)
} }
.onErrorResume { Mono.empty() } .onErrorResume { Flux.empty() }
} }
private fun detectArchiveNode(): Mono<Pair<String, String>> { private fun detectArchiveNode(): Mono<Pair<String, String>> {
@@ -60,16 +69,26 @@ class EthereumLabelsDetector(
).flatMap(JsonRpcResponse::requireResult) ).flatMap(JsonRpcResponse::requireResult)
} }
private fun nodeType(nodeType: String): String? { private fun clientVersion(client: String): String? {
return if (nodeType.contains("erigon", true)) { val firstSlash = client.indexOf("/")
val secondSlash = client.indexOf("/", firstSlash + 1)
if (firstSlash == -1 || secondSlash == -1 || secondSlash < firstSlash) {
return null
}
return client.substring(firstSlash + 1, secondSlash)
}
private fun clientType(client: String): String? {
return if (client.contains("erigon", true)) {
"erigon" "erigon"
} else if (nodeType.contains("geth", true)) { } else if (client.contains("geth", true)) {
"geth" "geth"
} else if (nodeType.contains("bor", true)) { } else if (client.contains("bor", true)) {
"bor" "bor"
} else if (nodeType.contains("nethermind", true)) { } else if (client.contains("nethermind", true)) {
"nethermind" "nethermind"
} else { } else {
log.debug("Unknown client type: {}", client)
null null
} }
} }

View File

@@ -35,16 +35,17 @@ class EthereumLabelsDetectorSpec extends Specification {
StepVerifier.create(act) StepVerifier.create(act)
.expectNext( .expectNext(
new Pair<String, String>("client_type", clientType), new Pair<String, String>("client_type", clientType),
new Pair<String, String>("client_version", version),
new Pair<String, String>("archive", "true") new Pair<String, String>("archive", "true")
) )
.expectComplete() .expectComplete()
.verify(Duration.ofSeconds(1)) .verify(Duration.ofSeconds(1))
where: where:
response | clientType response | clientType | version
"Nethermind/v1.19.3+e8ac1da4/linux-x64/dotnet7.0.8" | "nethermind" "Nethermind/v1.19.3+e8ac1da4/linux-x64/dotnet7.0.8" | "nethermind" | "v1.19.3+e8ac1da4"
"Geth/v1.12.0-stable-e501b3b0/linux-amd64/go1.20.3" | "geth" "Geth/v1.12.0-stable-e501b3b0/linux-amd64/go1.20.3" | "geth" | "v1.12.0-stable-e501b3b0"
"Erigon/v1.12.0-stable-e501b3b0/linux-amd64/go1.20.3" | "erigon" "Erigon/v1.12.0-stable-e501b3b0/linux-amd64/go1.20.3" | "erigon" | "v1.12.0-stable-e501b3b0"
"Bor/v0.4.0/linux-amd64/go1.19.10" | "bor" "Bor/v0.4.0/linux-amd64/go1.19.10" | "bor" | "v0.4.0"
} }
def "No any label"() { def "No any label"() {