From cfd73119f65c04d1b7f8abda424b6df437ca8db0 Mon Sep 17 00:00:00 2001 From: Igor Artamonov Date: Mon, 30 Nov 2020 19:40:56 -0500 Subject: [PATCH] problem: returns processed json for block with tx, instead of original json rel: #45 --- .../dshackle/rpc/TrackEthereumTx.kt | 2 +- .../upstream/ethereum/EthereumReader.kt | 55 +++++++------------ .../ethereum/EthereumReaderSpec.groovy | 10 ++-- .../src/main/resources/block-0x100001.json | 3 +- .../trial/proxy/StandardCallsSpec.groovy | 22 ++++++++ 5 files changed, 49 insertions(+), 43 deletions(-) diff --git a/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackEthereumTx.kt b/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackEthereumTx.kt index 9ef50393..e6d45165 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackEthereumTx.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackEthereumTx.kt @@ -205,7 +205,7 @@ class TrackEthereumTx( return Mono.empty() } return upstream.getReader() - .blocksByHash().read(tx.status.blockHash) + .blocksByHashParsed().read(tx.status.blockHash) .map { block -> setBlockDetails(tx, block) }.doOnError { t -> diff --git a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumReader.kt b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumReader.kt index 6f203a67..bb927f87 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumReader.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/upstream/ethereum/EthereumReader.kt @@ -71,54 +71,37 @@ open class EthereumReader( tx.json ?: ByteArray(0) } - val jsonToRaw = Function { json -> - objectMapper.writeValueAsBytes(json) - } - - val blockAsContainer = Function, BlockContainer> { block -> - BlockContainer.from(block.withoutTransactionDetails()) - } - val txAsContainer = Function { tx -> - TxContainer.from(tx) - } - private val idToBlockHash = Function { id -> BlockHash.from(id.value) } private val blockHashToId = Function { hash -> BlockId.from(hash) } private val txHashToId = Function { hash -> TxId.from(hash) } private val idToTxHash = Function { id -> TransactionId.from(id.value) } - fun blocksByHash(): Reader> { - return TransformingReader( - CompoundReader( - RekeyingReader(blockHashToId, caches.getBlocksByHash()), - directReader.blockReader - ), - extractBlock - ) - } - - fun blocksById(): Reader> { - return TransformingReader( - CompoundReader( - caches.getBlocksByHash(), - RekeyingReader(idToBlockHash, directReader.blockReader) - ), - extractBlock - ) - } - fun blocksByHashAsCont(): Reader { + return CompoundReader( + RekeyingReader(blockHashToId, caches.getBlocksByHash()), + directReader.blockReader + ) + } + + fun blocksByHashParsed(): Reader> { return TransformingReader( - blocksByHash(), - blockAsContainer + blocksByHashAsCont(), + extractBlock + ) + } + + fun blocksByIdParsed(): Reader> { + return TransformingReader( + blocksByIdAsCont(), + extractBlock ) } open fun blocksByIdAsCont(): Reader { - return TransformingReader( - blocksById(), - blockAsContainer + return CompoundReader( + caches.getBlocksByHash(), + RekeyingReader(idToBlockHash, directReader.blockReader) ) } diff --git a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumReaderSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumReaderSpec.groovy index c00c72f4..6806dc39 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumReaderSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/upstream/ethereum/EthereumReaderSpec.groovy @@ -71,7 +71,7 @@ class EthereumReaderSpec extends Specification { def reader = new EthereumReader(Stub(Multistream), caches, calls) when: - def act = reader.blocksById().read(blockId).block() + def act = reader.blocksByIdParsed().read(blockId).block() then: act == blockJson @@ -92,7 +92,7 @@ class EthereumReaderSpec extends Specification { def reader = new EthereumReader(upstream, caches, calls) when: - def act = reader.blocksById().read(blockId).block() + def act = reader.blocksByIdParsed().read(blockId).block() then: act == blockJson @@ -113,7 +113,7 @@ class EthereumReaderSpec extends Specification { def reader = new EthereumReader(upstream, caches, calls) when: - def act = reader.blocksById().read(blockId).block() + def act = reader.blocksByIdParsed().read(blockId).block() then: act == blockJson @@ -130,7 +130,7 @@ class EthereumReaderSpec extends Specification { def reader = new EthereumReader(Stub(Multistream), caches, calls) when: - def act = reader.blocksByHash().read(blockJson.hash).block() + def act = reader.blocksByHashParsed().read(blockJson.hash).block() then: act == blockJson @@ -150,7 +150,7 @@ class EthereumReaderSpec extends Specification { def reader = new EthereumReader(upstream, caches, calls) when: - def act = reader.blocksByHash().read(blockJson.hash).block() + def act = reader.blocksByHashParsed().read(blockJson.hash).block() then: act == blockJson diff --git a/testing/simple-upstream/src/main/resources/block-0x100001.json b/testing/simple-upstream/src/main/resources/block-0x100001.json index f8377291..0fca9d14 100644 --- a/testing/simple-upstream/src/main/resources/block-0x100001.json +++ b/testing/simple-upstream/src/main/resources/block-0x100001.json @@ -21,5 +21,6 @@ "0xe589a39acea3091b584b650158d08b159aa07e97b8e8cddb8f81cb606e13382e" ], "transactionsRoot": "0xc90078e2af52aef81815cb2a71c22ebd781dd658dd953d9df57f7769a0b2fe51", - "uncles": [] + "uncles": [], + "testFoo": "bar" } \ No newline at end of file diff --git a/testing/trial/src/test/groovy/io/emeraldpay/dshackle/testing/trial/proxy/StandardCallsSpec.groovy b/testing/trial/src/test/groovy/io/emeraldpay/dshackle/testing/trial/proxy/StandardCallsSpec.groovy index 76c0d528..65818107 100644 --- a/testing/trial/src/test/groovy/io/emeraldpay/dshackle/testing/trial/proxy/StandardCallsSpec.groovy +++ b/testing/trial/src/test/groovy/io/emeraldpay/dshackle/testing/trial/proxy/StandardCallsSpec.groovy @@ -48,4 +48,26 @@ class StandardCallsSpec extends Specification { } act.error == null } + + def "returns original block json"() { + when: + def act = client.execute("eth_getBlockByNumber", ["0x100001", false]) + then: + act.result != null + with(act.result) { + testFoo == "bar" + } + act.error == null + } + + def "returns original block json with tx"() { + when: + def act = client.execute("eth_getBlockByNumber", ["0x100001", true]) + then: + act.result != null + with(act.result) { + testFoo == "bar" + } + act.error == null + } }