problem: doesn't use hardcoded methods on Bitcoin

This commit is contained in:
Igor Artamonov
2021-11-17 21:39:17 -05:00
parent df805ee4a9
commit acdb450fdf
5 changed files with 63 additions and 10 deletions

View File

@@ -26,6 +26,7 @@ import io.emeraldpay.dshackle.upstream.Multistream
import io.emeraldpay.dshackle.upstream.RequestPostprocessor
import io.emeraldpay.dshackle.upstream.Selector
import io.emeraldpay.dshackle.upstream.Upstream
import io.emeraldpay.dshackle.upstream.ethereum.LocalCallRouter
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse
import io.emeraldpay.grpc.Chain
@@ -86,8 +87,7 @@ open class BitcoinMultistream(
}
override fun getRoutedApi(matcher: Selector.Matcher): Mono<Reader<JsonRpcRequest, JsonRpcResponse>> {
// TODO
return Mono.just(EmptyReader())
return Mono.just(LocalCallRouter(getMethods()))
}
open fun getReader(): BitcoinReader {

View File

@@ -0,0 +1,53 @@
/**
* Copyright (c) 2021 EmeraldPay, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.emeraldpay.dshackle.upstream.bitcoin
import io.emeraldpay.dshackle.reader.Reader
import io.emeraldpay.dshackle.upstream.calls.CallMethods
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcRequest
import io.emeraldpay.dshackle.upstream.rpcclient.JsonRpcResponse
import io.emeraldpay.etherjar.rpc.RpcException
import io.emeraldpay.etherjar.rpc.RpcResponseError
import org.slf4j.LoggerFactory
import reactor.core.publisher.Mono
/**
* Reader for JSON RPC requests. Verifies if the method is allowed, transforms if necessary, and calls EthereumReader for data.
* It provides data only if it's available through the router (cached, head, etc).
* If data is not available locally then it returns `empty`; at this case the caller should call the remote node for actual data.
*
* @see BitcoinReader
*/
class LocalCallRouter(
private val methods: CallMethods,
) : Reader<JsonRpcRequest, JsonRpcResponse> {
companion object {
private val log = LoggerFactory.getLogger(LocalCallRouter::class.java)
}
override fun read(key: JsonRpcRequest): Mono<JsonRpcResponse> {
if (methods.isHardcoded(key.method)) {
return Mono.just(methods.executeHardcoded(key.method))
.map { JsonRpcResponse(it, null) }
}
if (!methods.isAllowed(key.method)) {
return Mono.error(RpcException(RpcResponseError.CODE_METHOD_NOT_EXIST, "Unsupported method"))
}
return Mono.empty()
}
}

View File

@@ -124,7 +124,7 @@ open class EthereumMultistream(
}
override fun getRoutedApi(matcher: Selector.Matcher): Mono<Reader<JsonRpcRequest, JsonRpcResponse>> {
return Mono.just(NativeCallRouter(reader, getMethods(), getHead()))
return Mono.just(LocalCallRouter(reader, getMethods(), getHead()))
}
open fun getSubscribe(): EthereumSubscribe {

View File

@@ -36,14 +36,14 @@ import java.math.BigInteger
*
* @see EthereumReader
*/
class NativeCallRouter(
class LocalCallRouter(
private val reader: EthereumReader,
private val methods: CallMethods,
private val head: Head
) : Reader<JsonRpcRequest, JsonRpcResponse> {
companion object {
private val log = LoggerFactory.getLogger(NativeCallRouter::class.java)
private val log = LoggerFactory.getLogger(LocalCallRouter::class.java)
}
private val fullBlocksReader = EthereumFullBlocksReader(

View File

@@ -17,12 +17,12 @@ import spock.lang.Specification
import java.time.Duration
class NativeCallRouterSpec extends Specification {
class LocalCallRouterSpec extends Specification {
def "Calls hardcoded"() {
setup:
def methods = new DefaultEthereumMethods(Chain.ETHEREUM)
def router = new NativeCallRouter(
def router = new LocalCallRouter(
new EthereumReader(
TestingCommons.multistream(TestingCommons.api()),
Caches.default(),
@@ -50,7 +50,7 @@ class NativeCallRouterSpec extends Specification {
}
}
def methods = new DefaultEthereumMethods(Chain.ETHEREUM)
def router = new NativeCallRouter(reader, methods, head)
def router = new LocalCallRouter(reader, methods, head)
when:
def act = router.getBlockByNumber(["latest", false])
@@ -76,7 +76,7 @@ class NativeCallRouterSpec extends Specification {
}
}
def methods = new DefaultEthereumMethods(Chain.ETHEREUM)
def router = new NativeCallRouter(reader, methods, head)
def router = new LocalCallRouter(reader, methods, head)
when:
def act = router.getBlockByNumber(["earliest", false])
@@ -102,7 +102,7 @@ class NativeCallRouterSpec extends Specification {
}
}
def methods = new DefaultEthereumMethods(Chain.ETHEREUM)
def router = new NativeCallRouter(reader, methods, head)
def router = new LocalCallRouter(reader, methods, head)
when:
def act = router.getBlockByNumber(["0x123ef", false])