DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • How AI Will Change Agile Project Management
  • Java Concurrency: Condition
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Decoding eBPF Observability: How eBPF Transforms Observability as We Know It

Trending

  • How AI Will Change Agile Project Management
  • Java Concurrency: Condition
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Decoding eBPF Observability: How eBPF Transforms Observability as We Know It
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Querying the Blockchain With web3j

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.

Conor Svensson user avatar by
Conor Svensson
·
May. 17, 17 · Tutorial
Like (7)
Save
Tweet
Share
10.90K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Blockchain Smart contract

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

Opinions expressed by DZone contributors are their own.

Trending

  • How AI Will Change Agile Project Management
  • Java Concurrency: Condition
  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Decoding eBPF Observability: How eBPF Transforms Observability as We Know It

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: