diff --git a/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackEthereumTx.kt b/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackEthereumTx.kt index 4c0cda3b..8b709535 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackEthereumTx.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/rpc/TrackEthereumTx.kt @@ -209,11 +209,11 @@ class TrackEthereumTx( } } - fun updateFromBlock(upstream: Upstream, tx: TxDetails, it: TransactionJson): Mono { - return if (it.blockNumber != null && it.blockHash != null && it.blockHash != ZERO_BLOCK) { + fun updateFromBlock(upstream: Upstream, tx: TxDetails, blockTx: TransactionJson): Mono { + return if (blockTx.blockNumber != null && blockTx.blockHash != null && blockTx.blockHash != ZERO_BLOCK) { val updated = tx.withStatus( - blockHash = it.blockHash, - height = it.blockNumber, + blockHash = blockTx.blockHash, + height = blockTx.blockNumber, found = true, mined = true, confirmations = 1 diff --git a/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackEthereumTxSpec.groovy b/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackEthereumTxSpec.groovy index 78e3c1d5..e8da61a6 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackEthereumTxSpec.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/rpc/TrackEthereumTxSpec.groovy @@ -303,30 +303,23 @@ class TrackEthereumTxSpec extends Specification { apiMock.answer("eth_getBlockByHash", [block.hash.toHex(), false], block) } - def nextBlock = { int i -> - return { - println("block $i"); - upstreamMock.nextBlock(BlockContainer.from(blocks[i], TestingCommons.objectMapper())) - } as Runnable - } + upstreamMock.blocks = Flux.fromIterable(blocks) + .map { block -> + BlockContainer.from(block, TestingCommons.objectMapper()) + } when: def flux = trackTx.subscribe(req) then: StepVerifier.create(flux) .expectNext(exp1.build()).as("Just empty") - .then(nextBlock(1)) .expectNext(exp1.setBroadcasted(true).build()).as("Found in mempool") - .then(nextBlock(2)) .expectNext(exp2.setConfirmations(1).build()).as("Mined") - .then(nextBlock(3)) - .expectNext(exp2.setConfirmations(2).build()) - .then(nextBlock(4)) - .expectNext(exp2.setConfirmations(3).build()) - .then(nextBlock(5)) - .expectNext(exp2.setConfirmations(4).build()) + .expectNext(exp2.setConfirmations(2).build()).as("Confirmed 2") + .expectNext(exp2.setConfirmations(3).build()).as("Confirmed 3") + .expectNext(exp2.setConfirmations(4).build()).as("Confirmed 4") .expectComplete() - .verify(Duration.ofSeconds(4)) + .verify(Duration.ofSeconds(1)) } } diff --git a/src/test/groovy/io/emeraldpay/dshackle/test/EthereumHeadMock.groovy b/src/test/groovy/io/emeraldpay/dshackle/test/EthereumHeadMock.groovy index d5d0ab21..46810e8d 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/test/EthereumHeadMock.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/test/EthereumHeadMock.groovy @@ -18,6 +18,7 @@ package io.emeraldpay.dshackle.test import io.emeraldpay.dshackle.data.BlockContainer import io.emeraldpay.dshackle.upstream.Head +import org.reactivestreams.Publisher import reactor.core.publisher.Flux import reactor.core.publisher.Mono import reactor.core.publisher.TopicProcessor @@ -25,6 +26,7 @@ import reactor.core.publisher.TopicProcessor class EthereumHeadMock implements Head { private TopicProcessor bus = TopicProcessor.create() + private Publisher predefined = null private BlockContainer latest void nextBlock(BlockContainer block) { @@ -33,8 +35,20 @@ class EthereumHeadMock implements Head { bus.onNext(block) } + void setPredefined(Publisher predefined) { + this.predefined = Flux.from(predefined) + .publish() + .refCount(1) + .doOnNext { latest = it } + // keep the current block as latest, because getFlux is also used to get the current height + } + @Override Flux getFlux() { - return Flux.concat(Mono.justOrEmpty(latest), bus).distinctUntilChanged() + if (predefined != null) { + return Flux.concat(Mono.justOrEmpty(latest), Flux.from(predefined)) + } else { + return Flux.concat(Mono.justOrEmpty(latest), bus).distinctUntilChanged() + } } } diff --git a/src/test/groovy/io/emeraldpay/dshackle/test/EthereumUpstreamMock.groovy b/src/test/groovy/io/emeraldpay/dshackle/test/EthereumUpstreamMock.groovy index 388b6b7b..74ff3398 100644 --- a/src/test/groovy/io/emeraldpay/dshackle/test/EthereumUpstreamMock.groovy +++ b/src/test/groovy/io/emeraldpay/dshackle/test/EthereumUpstreamMock.groovy @@ -29,6 +29,7 @@ import io.emeraldpay.grpc.Chain import io.infinitape.etherjar.domain.TransactionId import io.infinitape.etherjar.rpc.json.BlockJson import org.jetbrains.annotations.NotNull +import org.reactivestreams.Publisher class EthereumUpstreamMock extends EthereumUpstream { @@ -58,6 +59,10 @@ class EthereumUpstreamMock extends EthereumUpstream { ethereumHeadMock.nextBlock(block) } + void setBlocks(Publisher blocks) { + ethereumHeadMock.predefined = blocks + } + @Override Head createHead() { return ethereumHeadMock