Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Querying the Blockchain With web3j

DZone's Guide to

Querying the Blockchain With web3j

In this post we take a look at some new, easier ways to query historical (and future!) data in the Ethereum blockchain via web3j. Read on for some examples.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

The 2.2 release of web3j has just come out and 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.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
java ,blockchain ,web3j ,ethereum ,integration

Published at DZone with permission of Conor Svensson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}