Merge pull request #79 from p2p-org/configure_method_groups

configure method groups
This commit is contained in:
MaxFomenkov
2022-12-12 17:00:57 +03:00
committed by GitHub
18 changed files with 275 additions and 32 deletions

View File

@@ -76,6 +76,7 @@ open class UpstreamsConfig {
var connection: T? = null
val labels = Labels()
var methods: Methods? = null
var methodGroups: MethodGroups? = null
var role: UpstreamRole = UpstreamRole.PRIMARY
@Suppress("UNCHECKED_CAST")
@@ -182,7 +183,12 @@ open class UpstreamsConfig {
class Methods(
val enabled: Set<Method>,
val disabled: Set<Method>
val disabled: Set<Method>,
)
class MethodGroups(
val enabled: Set<String>,
val disabled: Set<String>,
)
class Method(

View File

@@ -255,6 +255,7 @@ class UpstreamsConfigReader(
upstream.nodeId = getValueAsInt(upNode, "node-id")
upstream.options = tryReadOptions(upNode)
upstream.methods = tryReadMethods(upNode)
upstream.methodGroups = tryReadMethodGroups(upNode)
getValueAsBool(upNode, "enabled")?.let {
upstream.isEnabled = it
}
@@ -330,6 +331,15 @@ class UpstreamsConfigReader(
}
}
internal fun tryReadMethodGroups(upNode: MappingNode): UpstreamsConfig.MethodGroups? {
return getMapping(upNode, "method-groups")?.let {
UpstreamsConfig.MethodGroups(
enabled = getListOfString(it, "enabled")?.toSet() ?: emptySet(),
disabled = getListOfString(it, "disabled")?.toSet() ?: emptySet()
)
}
}
internal fun readOptions(values: MappingNode): UpstreamsConfig.Options {
val options = UpstreamsConfig.Options()
getValueAsInt(values, "min-peers")?.let {

View File

@@ -139,13 +139,15 @@ open class ConfiguredUpstreams(
}
fun buildMethods(config: UpstreamsConfig.Upstream<*>, chain: Chain): CallMethods {
return if (config.methods != null) {
return if (config.methods != null || config.methodGroups != null) {
ManagedCallMethods(
callTargets.getDefaultMethods(chain),
config.methods!!.enabled.map { it.name }.toSet(),
config.methods!!.disabled.map { it.name }.toSet()
delegate = callTargets.getDefaultMethods(chain),
enabled = config.methods?.enabled?.map { it.name }?.toSet() ?: emptySet(),
disabled = config.methods?.disabled?.map { it.name }?.toSet() ?: emptySet(),
groupsEnabled = config.methodGroups?.enabled ?: emptySet(),
groupsDisabled = config.methodGroups?.disabled ?: emptySet()
).also {
config.methods!!.enabled.forEach { m ->
config.methods?.enabled?.forEach { m ->
if (m.quorum != null) {
it.setQuorum(m.name, m.quorum)
}

View File

@@ -72,4 +72,7 @@ class AggregatedCallMethods(
it.isHardcoded(method)
}?.executeHardcoded(method) ?: throw IllegalStateException("No hardcoded for $method")
}
override fun getGroupMethods(groupName: String): Set<String> =
delegates.map { it.getGroupMethods(groupName) }.firstOrNull() ?: emptySet()
}

View File

@@ -60,4 +60,9 @@ interface CallMethods {
fun isAvailable(method: String): Boolean {
return isCallable(method) || isHardcoded(method)
}
/**
* Returns list of methods conforming the methods group
*/
fun getGroupMethods(groupName: String): Set<String>
}

View File

@@ -89,4 +89,6 @@ class DefaultBitcoinMethods : CallMethods {
else -> throw RpcException(-32601, "Method not found")
}
}
override fun getGroupMethods(groupName: String): Set<String> = emptySet()
}

View File

@@ -47,6 +47,18 @@ class DefaultEthereumMethods(
"eth_newBlockFilter",
"eth_newPendingTransactionFilter",
)
val traceMethods = listOf(
"trace_call",
"trace_callMany",
"trace_rawTransaction",
"trace_replayBlockTransactions",
"trace_replayTransaction",
"trace_block",
"trace_filter",
"trace_get",
"trace_transaction",
)
}
private val anyResponseMethods = listOf(
@@ -106,8 +118,7 @@ class DefaultEthereumMethods(
allowedMethods = anyResponseMethods +
firstValueMethods +
specialMethods +
headVerifiedMethods +
filterMethods -
headVerifiedMethods -
chainUnsupportedMethods(chain) +
getChainSpecificMethods(chain)
}
@@ -297,6 +308,13 @@ class DefaultEthereumMethods(
return json.toByteArray()
}
override fun getGroupMethods(groupName: String): Set<String> =
when (groupName) {
"filter" -> filterMethods
"trace" -> traceMethods
else -> emptyList()
}.toSet()
override fun getSupportedMethods(): Set<String> {
return allowedMethods.plus(hardcodedMethods).toSortedSet()
}

View File

@@ -47,4 +47,6 @@ open class DirectCallMethods(private val methods: Set<String>) : CallMethods {
override fun executeHardcoded(method: String): ByteArray {
return "unsupported".toByteArray()
}
override fun getGroupMethods(groupName: String): Set<String> = emptySet()
}

View File

@@ -34,7 +34,9 @@ import java.util.Collections
class ManagedCallMethods(
private val delegate: CallMethods,
private val enabled: Set<String>,
private val disabled: Set<String>
disabled: Set<String>,
groupsEnabled: Set<String>,
groupsDisabled: Set<String>
) : CallMethods {
companion object {
@@ -44,13 +46,15 @@ class ManagedCallMethods(
}
}
private val delegated = delegate.getSupportedMethods().sorted()
private val delegated = delegate.getSupportedMethods().associateWith { true }
private val allGroupEnabled = groupsEnabled.flatMap { delegate.getGroupMethods(it) }
private val allGroupDisabled = groupsDisabled.flatMap { delegate.getGroupMethods(it) }
private val allAllowed: Set<String> = Collections.unmodifiableSet(
enabled + delegated - disabled
delegated.keys + allGroupEnabled - allGroupDisabled.toSet() + enabled - disabled
)
private val quorum: MutableMap<String, Factory<CallQuorum>> = HashMap()
private val staticResponse: MutableMap<String, String> = HashMap()
private val redefined = delegated.filter(enabled::contains).sorted()
private val redefined = delegated.keys.filter(enabled::contains).associateWith { true }
init {
enabled.forEach { m ->
@@ -87,11 +91,11 @@ class ManagedCallMethods(
}
private fun isDelegated(method: String): Boolean {
return Collections.binarySearch(delegated, method) >= 0
return delegated[method] ?: false
}
private fun isRedefined(method: String): Boolean {
return Collections.binarySearch(redefined, method) >= 0
return redefined[method] ?: false
}
override fun isCallable(method: String): Boolean {
@@ -121,4 +125,7 @@ class ManagedCallMethods(
}
return delegate.executeHardcoded(method)
}
override fun getGroupMethods(groupName: String): Set<String> =
delegate.getGroupMethods(groupName)
}