Menu Home

Querying the blockchain with web3j and other updates

The 2.2 release of web3j has just gone out, which contains some great enhancements.

Querying the blockchain

web3j now makes it really simple to query historical data from the Ethereum blockchain (and of course Quorum). Its API has been enhanced allowing you to provide a range of blocks to replay, and it will play them back to you.

To replay a range of blocks from the blockchain:

Subscription subscription = web3j.replayBlocksObservable(
        <startBlockNumber>, <endBlockNumber>, <fullTxObjects>)
        .subscribe(block -> {
                 ...
});

To replay the individual transactions contained within a range of blocks:

Subscription subscription = web3j.replayTransactionsObservable(
        <startBlockNumber>, <endBlockNumber>)
        .subscribe(tx -> {
                 ...
});

You can also get web3j to replay all blocks up to the most current, and provide notification (via the submitted Observable) once you've caught up:

Subscription subscription = web3j.catchUpToLatestBlockObservable(
        <startBlockNumber>, <fullTxObjects>, <onCompleteObservable>)
        .subscribe(block -> {
                 ...
});

Or, if you'd rather replay all blocks to the most current, then be notified of new subsequent blocks being created:

Subscription subscription = web3j.catchUpToLatestAndSubscribeToNewBlocksObservable(
        <startBlockNumber>, <fullTxObjects>)
        .subscribe(block -> {
                 ...
});

I was able to replay the entire Ropsten blockchain excluding transactions (941667 blocks) in 7m22s. All transactions could be replayed in 41m16s on a 2013 Macbook Pro. Just make sure you use IPC to connect to your Ethereum node.

If you'd like a more complete example, the following code will provide details of all historic and future transfers of Ether (displayed in Wei) for the provided account:

CountDownLatch countDownLatch = new CountDownLatch(1);
        Web3j web3j = Web3j.build(new HttpService());
        web3j.catchUpToLatestAndSubscribeToNewTransactionsObservable(DefaultBlockParameterName.EARLIEST)
                .filter(tx -> tx.getFrom().equals("0x<address>"))
                .subscribe(
                        tx -> System.out.println(tx.getValue()),
                        Throwable::printStackTrace,
                        countDownLatch::countDown);
        Thread.sleep(TimeUnit.MINUTES.toMillis(1));

Smart contract verification

Smart contract wrappers now include an isValid() method to verify that the deployed bytecode at the smart contract's address matches that of the smart contract wrapper.

Long parameter values added to Solidity integer types

All of the Uint and Int Solidity types now support construction with long values, instead of only BigIntegers as was previously the case. DefaultBlockParameterNumber's also now accept long values.

Async changes

Behind the scenes in the smart contract wrappers, web3j now performs synchronous requests with Ethereum clients. This is to reduce the overhead that CompletableFutures were placing on the JVM thread pool. Completable futures are now only used in the client API for smart contract wrappers.

Everything else

There are a number of other updates in this release. For more information refer to the release notes and filters documentation.

Categories: Blockchain Development Ethereum Functional Programming Java web3j

Tagged as:

conor

1 reply

  1. You would know how to structure a query to obtain the top 100 list of token holders by dollar amount? Etherscan.io had one but they just got rid of it.

Leave a Reply

Your email address will not be published. Required fields are marked *