From 25e2e3268583e8a43d7e817e25306e26b94064be Mon Sep 17 00:00:00 2001 From: a10zn8 Date: Mon, 12 Dec 2022 17:57:59 +0400 Subject: [PATCH] optimise default getBlockByNumber - skip processing requests of tx bodies --- .../upstream/ethereum/LocalCallRouter.kt | 19 ++++++++----------- .../ethereum/LocalCallRouterSpec.groovy | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/LocalCallRouter.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/LocalCallRouter.kt index 045ff299..af273045 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/LocalCallRouter.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/LocalCallRouter.kt @@ -136,6 +136,11 @@ class LocalCallRouter( throw RpcException(RpcResponseError.CODE_INVALID_METHOD_PARAMS, "Must provide 2 parameters") } val number: Long + val withTx = params[1].toString().toBoolean() + if (withTx) { + // with Tx request much more efficient in remote call + return null + } try { val blockRef = params[0].toString() when { @@ -165,16 +170,8 @@ class LocalCallRouter( } catch (e: IllegalArgumentException) { throw RpcException(RpcResponseError.CODE_INVALID_METHOD_PARAMS, "[0] must be a block number") } - val withTx = params[1].toString().toBoolean() - var block = reader.blocksByHeightAsCont() - .read(number) - block = if (withTx) { - block.flatMap { - fullBlocksReader.read(it.hash) - } - } else { - block - } - return block.map { it.json!! } + + return reader.blocksByHeightAsCont() + .read(number).map { it.json!! } } } diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/LocalCallRouterSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/LocalCallRouterSpec.groovy index 9072508e..85489977 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/LocalCallRouterSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/LocalCallRouterSpec.groovy @@ -138,4 +138,22 @@ class LocalCallRouterSpec extends Specification { } } } + + def "getBlockByNumber skips requests with tx bodies"() { + setup: + def head = Mock(Head) + def reader = Mock(EthereumCachingReader) { + _ * blocksByIdAsCont() >> new EmptyReader<>() + _ * txByHashAsCont() >> new EmptyReader<>() + _ * blocksByHeightAsCont() >> new EmptyReader<>() + } + def methods = new DefaultEthereumMethods(Chain.ETHEREUM) + def router = new LocalCallRouter(reader, methods, head, true) + + when: + def act = router.getBlockByNumber(["0x0", true]) + + then: + act == null + } }