-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Autodetect methods * add polkadot magic method detection * fix lint * change config.methods writing * fix log * change Flux to Mono.zip * change block to subscribe * split chain method detectors * eth method detector use rpc_modules * rm UpstreamRpcModulesDetector.kt * add tests * getAllMethods for eth * prefer enable/disable methods from config * update tests * whiter list & full is not available error check * prefer config method group * mv notAvailableRegexps to class variable
- Loading branch information
Showing
13 changed files
with
468 additions
and
97 deletions.
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
src/main/kotlin/io/emeraldpay/dshackle/upstream/UpstreamRpcMethodsDetector.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.emeraldpay.dshackle.upstream | ||
|
||
import io.emeraldpay.dshackle.config.UpstreamsConfig | ||
import io.emeraldpay.dshackle.upstream.rpcclient.CallParams | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import reactor.core.publisher.Mono | ||
|
||
typealias UpstreamRpcMethodsDetectorBuilder = (Upstream, UpstreamsConfig.Upstream<*>?) -> UpstreamRpcMethodsDetector? | ||
|
||
abstract class UpstreamRpcMethodsDetector( | ||
private val upstream: Upstream, | ||
private val config: UpstreamsConfig.Upstream<*>? = null, | ||
) { | ||
protected val log: Logger = LoggerFactory.getLogger(this::class.java) | ||
|
||
private val notAvailableRegexps = | ||
listOf( | ||
"method ([A-Za-z0-9_]+) does not exist/is not available", | ||
"([A-Za-z0-9_]+) found but the containing module is disabled", | ||
"Method not found", | ||
"The method ([A-Za-z0-9_]+) is not available", | ||
).map { s -> s.toRegex() } | ||
|
||
open fun detectRpcMethods(): Mono<Map<String, Boolean>> = detectByMagicMethod().switchIfEmpty(detectByMethod()) | ||
|
||
protected fun detectByMethod(): Mono<Map<String, Boolean>> = | ||
Mono.zip( | ||
rpcMethods().map { | ||
Mono | ||
.just(it) | ||
.flatMap { (method, param) -> | ||
upstream | ||
.getIngressReader() | ||
.read(ChainRequest(method, param)) | ||
.flatMap(ChainResponse::requireResult) | ||
.map { method to true } | ||
.onErrorResume { err -> | ||
val notAvailableError = | ||
notAvailableRegexps.any { s -> s.containsMatchIn(err.message ?: "") } | ||
if (notAvailableError) { | ||
Mono.just(method to false) | ||
} else { | ||
Mono.empty() | ||
} | ||
} | ||
} | ||
}, | ||
) { | ||
it | ||
.map { p -> p as Pair<String, Boolean> } | ||
.associate { (method, enabled) -> method to enabled } | ||
} | ||
|
||
protected abstract fun detectByMagicMethod(): Mono<Map<String, Boolean>> | ||
|
||
protected abstract fun rpcMethods(): Set<Pair<String, CallParams>> | ||
} |
41 changes: 0 additions & 41 deletions
41
src/main/kotlin/io/emeraldpay/dshackle/upstream/UpstreamRpcModulesDetector.kt
This file was deleted.
Oops, something went wrong.
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
52 changes: 52 additions & 0 deletions
52
...ain/kotlin/io/emeraldpay/dshackle/upstream/ethereum/BasicEthUpstreamRpcMethodsDetector.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.emeraldpay.dshackle.upstream.ethereum | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference | ||
import io.emeraldpay.dshackle.Global | ||
import io.emeraldpay.dshackle.config.UpstreamsConfig | ||
import io.emeraldpay.dshackle.upstream.ChainRequest | ||
import io.emeraldpay.dshackle.upstream.ChainResponse | ||
import io.emeraldpay.dshackle.upstream.Upstream | ||
import io.emeraldpay.dshackle.upstream.UpstreamRpcMethodsDetector | ||
import io.emeraldpay.dshackle.upstream.calls.DefaultEthereumMethods | ||
import io.emeraldpay.dshackle.upstream.rpcclient.CallParams | ||
import io.emeraldpay.dshackle.upstream.rpcclient.ListParams | ||
import reactor.core.publisher.Mono | ||
|
||
class BasicEthUpstreamRpcMethodsDetector( | ||
private val upstream: Upstream, | ||
private val config: UpstreamsConfig.Upstream<*>, | ||
) : UpstreamRpcMethodsDetector(upstream) { | ||
override fun detectByMagicMethod(): Mono<Map<String, Boolean>> = | ||
upstream | ||
.getIngressReader() | ||
.read(ChainRequest("rpc_modules", ListParams())) | ||
.flatMap(ChainResponse::requireResult) | ||
.map(::parseRpcModules) | ||
// force check all methods from rpcMethods | ||
.zipWith(detectByMethod()) { a, b -> | ||
a.plus(b) | ||
}.onErrorResume { | ||
log.warn("Can't detect rpc_modules of upstream ${upstream.getId()}, reason - {}", it.message) | ||
Mono.empty() | ||
} | ||
|
||
override fun rpcMethods(): Set<Pair<String, CallParams>> = | ||
setOf( | ||
"eth_getBlockReceipts" to ListParams("latest"), | ||
) | ||
|
||
private fun parseRpcModules(data: ByteArray): Map<String, Boolean> { | ||
val modules = Global.objectMapper.readValue(data, object : TypeReference<HashMap<String, String>>() {}) | ||
return DefaultEthereumMethods(upstream.getChain()) | ||
.getAllMethods() | ||
.associateWith { method -> | ||
if (config.methodGroups?.enabled?.any { group -> method.startsWith(group) } == true) { | ||
return@associateWith true | ||
} | ||
if (config.methodGroups?.disabled?.any { group -> method.startsWith(group) } == true) { | ||
return@associateWith false | ||
} | ||
modules.any { (module, _) -> method.startsWith(module) } | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.