[Ethereum] Get block information using web3j

Article content

I was making something like a blockchain explorer using Web3j, but I was a little worried about how to get blocks, so I will leave a note.

environment

Get block information

Immediately, here is the code made with the sample.

SampleBlock.java


import java.io.IOException;
import java.math.BigInteger;

import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthBlock;
import org.web3j.protocol.http.HttpService;

public class SampleBlock {

	public static final Admin web3j = Admin.build(new HttpService("http://localhost:8545"));

	public static void main(String[] args) {
		System.out.println("-------------------------Latest Block----------------------------------");
		EthBlock block = getLatestBlock();
		showBlockInfo(block);

		System.out.println("-------------------------Block Number(100)-----------------------------");
		block = getBlock(BigInteger.valueOf(100));
		showBlockInfo(block);
	}

	public static EthBlock getLatestBlock() {

		EthBlock latestBlock = null;
		try {
			latestBlock = web3j.ethGetBlockByNumber(
					DefaultBlockParameterName.LATEST,
					false).send();

		}catch(IOException e) {
			e.printStackTrace();
		}
		return latestBlock;
	}

	public static EthBlock getBlock(BigInteger blockNumber) {

		EthBlock block = null;
		try {
			block =web3j.ethGetBlockByNumber(DefaultBlockParameter.valueOf(blockNumber), true).send();
		}catch(IOException e) {
			e.printStackTrace();
		}
		return block;
	}

	public static void showBlockInfo(EthBlock block) {
		System.out.println("Block Number: " + block.getBlock().getNumber());
		System.out.println("Block Size  : " + block.getBlock().getHash());
		System.out.println("Extra Data  : " + block.getBlock().getExtraData());
		System.out.println("Nonce       : " + block.getBlock().getNonce());
		System.out.println("Block Size  : " + block.getBlock().getSize());
	}
}

The content of the code is

  1. Get the latest block and display the contents
  2. Get the block with the specified block number and display the contents It will be these two.

1. Get the latest block information and display the contents

First, get the latest information.

SampleBlock.java


public static EthBlock getLatestBlock() {

	EthBlock latestBlock = null;
	try {
		latestBlock = web3j.ethGetBlockByNumber(
				DefaultBlockParameterName.LATEST,
				false).send();
	}catch(IOException e) {
		e.printStackTrace();
	}
	return latestBlock;
}
public static void showBlockInfo(EthBlock block) {
	System.out.println("Block Number: " + block.getBlock().getNumber());
	System.out.println("Block Size  : " + block.getBlock().getHash());
	System.out.println("Extra Data  : " + block.getBlock().getExtraData());
	System.out.println("Nonce       : " + block.getBlock().getNonce());
	System.out.println("Block Size  : " + block.getBlock().getSize());
}

It is an argument of ethGetBlockByNumber, First argument: Information of the block to be acquired Second argument: Whether to get detailed transaction information It will be.

The EthBlock class holds an inner class called Block. Basic information about the block can be obtained by using the getter method of the Block class.

2. Get the block with the specified block number and display the contents

Get the block by specifying the block number 100.

SampleBlock.java


public static EthBlock getBlock(BigInteger blockNumber) {

	EthBlock block = null;
	try {
		block =web3j.ethGetBlockByNumber(DefaultBlockParameter.valueOf(blockNumber), true).send();
	}catch(IOException e) {
		e.printStackTrace();
	}
	return block;
}

The method used to get the latest block information is the same. The first argument is DefaultBlockParameter.valueOf (blockNumber) instead of "DefaultBlockParameterName.LATEST".

All you have to do is set the block number you want to get in the argument of DefaultBlockParameter.valueOf (blockNumber) with BigInteger type.

3. Execution result

The result of executing the main method of the first code

-------------------------Latest Block----------------------------------
Block Number: 1126
Block Size  : 0x43b54d8fa0721f48775662bc931f71b84037cf7697e008af630143e4be0d6c68
Extra Data  : 0xda83010900846765746888676f312e31322e378777696e646f7773
Nonce       : 8592711553053470949
Block Size  : 540
-------------------------Block Number(100)-----------------------------
Block Number: 100
Block Size  : 0x0e62799b40cc37228b74de6be9d6424277c7cc2ac50ad9370655851eea2e3feb
Extra Data  : 0xda83010900846765746888676f312e31322e378777696e646f7773
Nonce       : 5891392007347385592
Block Size  : 538

There is a lot of other information that can be obtained, so I hope you will give it a try.

4. Get information about transactions captured in blocks

The information of the transaction captured in the block is stored in the EthBlock.Block class and can be obtained by using the getTransactions method.

For reference, it is a process to get transaction information.

SampleBlock.java


public static List<TransactionObject> getTransactionList(BigInteger blockNumber) {

	List<TransactionObject> transactionList = new ArrayList<>();
	try {
		List<EthBlock.TransactionResult> txs = web3j.ethGetBlockByNumber(
				new DefaultBlockParameterNumber(blockNumber), true).send().getBlock().getTransactions();
			
		for(TransactionResult result: txs) {
			transactionList.add((TransactionObject)result.get());
		}
	}catch(IOException e) {
		e.printStackTrace();
	}
	return transactionList;
}

I think there might be a better way, but I can't get any information from the EthBlock.TransactionResult class. Since the TransactionObject class is held in TransactionResult, it is possible to get transaction information through this class.

Finally

It's easier than you think to get block and transaction information, I was able to create an ethereum blockchain explorer in a few hours.

Recommended Posts

[Ethereum] Get block information using web3j
Get block information with Hyperledger Iroha's Java SDK
Get location information quickly
[Swift5] Get array (individual) information from JSON using library'SwiftyJSON'
Get Android location information
Get error information using DefaultErrorAttributes and ErrorAttributeOptions in Spring Boot 2.3
Get Youtube channel information with Rails app (using Yt gem)
Get EXIF information in Java
Send signed transactions using Web3j
Get Twitter images using twitter4j
Deleting Exif information using sanselan