Skip to content

Kundera with Ethereum Blockchain

Devender Yadav edited this page Nov 24, 2017 · 9 revisions

Ethereum

Ethereum is a decentralized platform that runs smart contracts: applications that run exactly as programmed without any possibility of downtime, censorship, fraud or third party interference. These apps run on a custom built blockchain, an enormously powerful shared global infrastructure that can move value around and represent the ownership of property.

Query ethereum public data

It is not possible to perform queries over multiple blocks or transactions on ethereum public data using various native clients.

Solution

One solution could be to migrate blocks' data to any database and perform queries there. Kundera-Ethereum will help in that migration. It uses Web3j API to fetch blocks' data. It creates Block and Transaction objects and persist them in the Kundera supported database of your choice via JPA route.

Supported Databases

Ideally, all the databases supported by Kundera can be used to import ethereum blocks. But we have tested -

  • MongoDB
  • RethinkDB
  • Cassandra
  • HBase

Feel free to use any database. Let us know if you face any issue with it.

How to use

Add dependency

<dependency>
     <groupId>com.impetus.kundera.client</groupId>
     <artifactId>kundera-ethereum</artifactId>
     <version>${kundera.version}</version>
</dependency>

Add kundera-ethereum.properties file in the classpath

You need to put following properties in this file -

Database specific properties:

  • database.type - Mention database type like MongoDB, HBase, etc.
  • database.host - IP address or hostname of the database
  • database.port - Port number of the database
  • database.name - Name of the database in which you want to store Block and Transaction Table.
  • database.username (optional) - Username of the database
  • database.password (optional) - Password of the database
  • schema.auto.generate (optional) - Set true, if you want Kundera to create database and tables for you.
  • schema.drop.existing (optional) - Set true, if you want Kundera to delete existing database and tables for you.

Ethereum specific properties:

  • ethereum.node.endpoint - There are 3 options here
    • Using RPC HTTP end point. e.g. http://localhost:8545/
    • Using IPC socket file location. e.g. /home/dev/ethereum/geth.ipc
    • Using Infura end point. e.g. https://mainnet.infura.io/<your-token>
    • ethereum.node.os (optional) - Set windows, only needed if you are using IPC socket file to connect in the above property and its located on windows OS.

Check sample properties file here.

That's it! Start importing data now.

Import blocks and transactions

  • To import all the data starting from genesis block:

    BlockchainImporter importer = BlockchainImporter.initialize();
    importer.importUptoLatestBlock();
    
  • To import all the data starting from 1000000th block:

    BlockchainImporter importer = BlockchainImporter.initialize();
    importer.importUptoLatestBlock(BigInteger.valueOf(1000000));
    
  • To import the data starting from 1000000th to 2000000th block:

    BlockchainImporter importer = BlockchainImporter.initialize();
    importer.importBlocks(BigInteger.valueOf(1000000), BigInteger.valueOf(2000000));
    

Sync with upcoming blocks

You can also opt for saving upcoming blocks:

 BlockchainImporter importer = BlockchainImporter.initialize();
 importer.importUptoLatestAndSyncNewBlocks();

Check testcase for more features.

JPA queries over data

After importing data, JPA queries can be performed using normal Kundera way. Sample:

Query query = em.createQuery(
            "Select t.gas,t.gasPrice from Transaction t where t.blockNumber='0x455a56' and t.from='0x6c15291028d082e1b9358e19f15c83b9c54f2b35'");
List<Transaction> results = query.getResultList();

Check testcase for details.

Sample project

kundera-ethereum-example

Clone this wiki locally