web3j
web3j copied to clipboard
replayPastAndFutureBlocksFlowable does not emit current block if startBlock is "latest"
replayPastAndFutureBlocksFlowable
does not emit current block if startBlock
is "latest"
I'm developing a module in a spring webflux project for block number and hash tracking. I want to emit a event of "current block" at application starting and then emit any new blocks. I found replayPastAndFutureBlocksFlowable
and try to make use of it.
I tried to build a flowable like this
web3j.replayPastAndFutureBlocksFlowable(DefaultBlockParameterName.LATEST, false)
But this flowable doesn't emit the latest block ("current block") and only emits newly mined blocks, i.e. if current block number is 125, it emits a block from 126.
Then I tried this
Mono.from(web3j.ethBlockNumber().flowable())
.flatMapMany {
web3j.replayPastAndFutureBlocksFlowable(
DefaultBlockParameter.valueOf(it.blockNumber),
false
)
}
This does not work too. And then this
Mono.from(web3j.ethBlockNumber().flowable())
.flatMapMany {
web3j.replayPastAndFutureBlocksFlowable(
DefaultBlockParameter.valueOf(it.blockNumber - BigInteger.ONE),
false
)
}
This time the flow does emit the latest block and one block before it, i.e. if current block number is 125, it emits 124 and 125 and any later blocks.
I investigate the source code and found https://github.com/web3j/web3j/blob/bb276caf4b63508fef66149d8d0bafc510669628/core/src/main/java/org/web3j/protocol/rx/JsonRpc2_0Rx.java#L194
It means web3j.replayPastAndFutureBlocksFlowable(DefaultBlockParameterName.LATEST, false)
is effectively equal to web3j.blockFlowable(false)
. Is it intended behavior? Or the condition above should be startBlockNumber.compareTo(latestBlockNumber) > 0
?
I think I encountered the same issue. Any plans to have it fixed?