problem: hardcoded methods are not called

This commit is contained in:
Igor Artamonov
2020-05-14 22:37:46 -04:00
parent bbc21684ae
commit af4aba69e7
4 changed files with 65 additions and 14 deletions

View File

@@ -40,7 +40,7 @@ class AggregatedCallMethods(
*/ */
override fun getQuorumFor(method: String): CallQuorum { override fun getQuorumFor(method: String): CallQuorum {
return delegates.find { return delegates.find {
it.isAllowed(method) it.isAllowed(method) || it.isHardcoded(method)
}?.getQuorumFor(method) ?: throw IllegalStateException("No quorum for $method") }?.getQuorumFor(method) ?: throw IllegalStateException("No quorum for $method")
} }
@@ -62,7 +62,7 @@ class AggregatedCallMethods(
* @return true if there is at least one delegate that allows the method and it's hardcoded on that delegate * @return true if there is at least one delegate that allows the method and it's hardcoded on that delegate
*/ */
override fun isHardcoded(method: String): Boolean { override fun isHardcoded(method: String): Boolean {
return delegates.any { it.isAllowed(method) && it.isHardcoded(method) } return delegates.any { it.isHardcoded(method) }
} }
/** /**
@@ -70,7 +70,7 @@ class AggregatedCallMethods(
*/ */
override fun executeHardcoded(method: String): ByteArray { override fun executeHardcoded(method: String): ByteArray {
return delegates.find { return delegates.find {
it.isAllowed(method) && it.isHardcoded(method) it.isHardcoded(method)
}?.executeHardcoded(method) ?: throw IllegalStateException("No hardcoded for $method") }?.executeHardcoded(method) ?: throw IllegalStateException("No hardcoded for $method")
} }
} }

View File

@@ -47,18 +47,18 @@ class NativeCallRouter(
) )
override fun read(key: JsonRpcRequest): Mono<JsonRpcResponse> { override fun read(key: JsonRpcRequest): Mono<JsonRpcResponse> {
if (!methods.isAllowed(key.method)) {
return Mono.error(RpcException(RpcResponseError.CODE_METHOD_NOT_EXIST, "Unsupported method"))
}
if (methods.isHardcoded(key.method)) { if (methods.isHardcoded(key.method)) {
return Mono.just(methods.executeHardcoded(key.method)) return Mono.just(methods.executeHardcoded(key.method))
.map { JsonRpcResponse(it, null) } .map { JsonRpcResponse(it, null) }
} }
if (!methods.isAllowed(key.method)) {
return Mono.error(RpcException(RpcResponseError.CODE_METHOD_NOT_EXIST, "Unsupported method"))
}
val common = commonRequests(key) val common = commonRequests(key)
if (common != null) { if (common != null) {
return common.map { JsonRpcResponse(it, null) } return common.map { JsonRpcResponse(it, null) }
} }
return directApi.read(key) return Mono.empty()
} }
/** /**

View File

@@ -84,16 +84,34 @@ class AggregatedCallMethodsSpec extends Specification {
setup: setup:
def delegate1 = Mock(CallMethods) { def delegate1 = Mock(CallMethods) {
_ * getSupportedMethods() >> ["eth_no_test", "foo_bar"] _ * getSupportedMethods() >> ["eth_no_test", "foo_bar"]
1 * isAllowed("eth_test") >> false
1 * isAllowed("eth_no_test") >> true
1 * isHardcoded("eth_no_test") >> false 1 * isHardcoded("eth_no_test") >> false
} }
def delegate2 = Mock(CallMethods) { def delegate2 = Mock(CallMethods) {
_ * getSupportedMethods() >> ["eth_test", "foo_bar"] _ * getSupportedMethods() >> ["eth_test", "foo_bar"]
1 * isAllowed("eth_test") >> true 1 * isHardcoded("eth_test") >> true
1 * isAllowed("eth_no_test") >> false }
def aggregate = new AggregatedCallMethods([delegate1, delegate2])
when:
def act = aggregate.isHardcoded("eth_test")
then:
act
when:
act = aggregate.isHardcoded("eth_no_test")
then:
!act
}
def "Can be hardcoded if not allowed"() {
setup:
def delegate1 = Mock(CallMethods) {
_ * getSupportedMethods() >> ["eth_no_test", "foo_bar"]
_ * isAllowed(_) >> false
1 * isHardcoded("eth_no_test") >> false
}
def delegate2 = Mock(CallMethods) {
_ * getSupportedMethods() >> ["eth_test", "foo_bar"]
_ * isAllowed(_) >> false
1 * isHardcoded("eth_test") >> true 1 * isHardcoded("eth_test") >> true
} }
def aggregate = new AggregatedCallMethods([delegate1, delegate2]) def aggregate = new AggregatedCallMethods([delegate1, delegate2])
@@ -112,11 +130,10 @@ class AggregatedCallMethodsSpec extends Specification {
setup: setup:
def delegate1 = Mock(CallMethods) { def delegate1 = Mock(CallMethods) {
_ * getSupportedMethods() >> ["eth_no_test", "foo_bar"] _ * getSupportedMethods() >> ["eth_no_test", "foo_bar"]
1 * isAllowed("eth_test") >> false 1 * isHardcoded("eth_test") >> false
} }
def delegate2 = Mock(CallMethods) { def delegate2 = Mock(CallMethods) {
_ * getSupportedMethods() >> ["eth_test", "foo_bar"] _ * getSupportedMethods() >> ["eth_test", "foo_bar"]
1 * isAllowed("eth_test") >> true
1 * isHardcoded("eth_test") >> true 1 * isHardcoded("eth_test") >> true
1 * executeHardcoded("eth_test") >> "hello" 1 * executeHardcoded("eth_test") >> "hello"
} }

View File

@@ -0,0 +1,34 @@
package io.emeraldpay.dshackle.upstream.ethereum
import io.emeraldpay.dshackle.cache.Caches
import io.emeraldpay.dshackle.reader.EmptyReader
import io.emeraldpay.dshackle.test.TestingCommons
import io.emeraldpay.dshackle.upstream.calls.DefaultEthereumMethods
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse
import io.emeraldpay.grpc.Chain
import spock.lang.Specification
import java.time.Duration
class NativeCallRouterSpec extends Specification {
def "Calls hardcoded"() {
setup:
def methods = new DefaultEthereumMethods(TestingCommons.objectMapper(), Chain.ETHEREUM)
def router = new NativeCallRouter(
TestingCommons.objectMapper(),
new EthereumReader(
TestingCommons.aggregatedUpstream(TestingCommons.api()),
Caches.default(TestingCommons.objectMapper()),
TestingCommons.objectMapper()
),
new EmptyReader<JsonRpcRequest, JsonRpcResponse>(),
methods
)
when:
def act = router.read(new JsonRpcRequest("eth_coinbase", [])).block(Duration.ofSeconds(1))
then:
act.resultAsProcessedString == "0x0000000000000000000000000000000000000000"
}
}