Java Code Examples for org.web3j.protocol.core.methods.response.EthBlock#getBlock()

The following examples show how to use org.web3j.protocol.core.methods.response.EthBlock#getBlock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EthGetBlockTransaction.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public Block execute(final NodeRequests node) {
  try {
    final EthBlock result =
        node.eth().ethGetBlockByNumber(blockParameter, fullTransactionObjects).send();
    assertThat(result).isNotNull();
    assertThat(result.hasError()).isFalse();
    return result.getBlock();
  } catch (final IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 2
Source File: CoreIT.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testEthGetBlockByNumber() throws Exception {
    EthBlock ethBlock = web3j.ethGetBlockByNumber(
        DefaultBlockParameter.valueOf(BigInteger.valueOf(2391)), true).send();

    EthBlock.Block block = ethBlock.getBlock();
    assertNotNull(ethBlock.getBlock());

    for (TransactionResult tx : block.getTransactions()) {
        System.out.println(((TransactionObject) tx).getCreates());
        EthTransaction ethTransaction = web3j.ethGetTransactionByHash(((TransactionObject) tx).getHash()).send();
        System.out.println(ethTransaction.getTransaction().get().getCreates());
    }
    System.out.println(new ObjectMapper().writeValueAsString(block));
}
 
Example 3
Source File: CoreIT.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testEthGetBlockByHashReturnHashObjects() throws Exception {
    EthBlock ethBlock = web3j.ethGetBlockByHash(config.validBlockHash(), false)
            .send();

    EthBlock.Block block = ethBlock.getBlock();
    assertNotNull(ethBlock.getBlock());
    assertThat(block.getNumber(), equalTo(config.validBlock()));
    assertThat(block.getTransactions().size(),
            is(config.validBlockTransactionCount().intValue()));
}
 
Example 4
Source File: CoreIT.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testEthGetBlockByHashReturnFullTransactionObjects() throws Exception {
    EthBlock ethBlock = web3j.ethGetBlockByHash(config.validBlockHash(), true)
            .send();

    EthBlock.Block block = ethBlock.getBlock();
    assertNotNull(ethBlock.getBlock());
    assertThat(block.getNumber(), equalTo(config.validBlock()));
    assertThat(block.getTransactions().size(),
            equalTo(config.validBlockTransactionCount().intValue()));
}
 
Example 5
Source File: CoreIT.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testEthGetBlockByNumberReturnHashObjects() throws Exception {
    EthBlock ethBlock = web3j.ethGetBlockByNumber(
            DefaultBlockParameter.valueOf(config.validBlock()), false).send();

    EthBlock.Block block = ethBlock.getBlock();
    assertNotNull(ethBlock.getBlock());
    assertThat(block.getNumber(), equalTo(config.validBlock()));
    assertThat(block.getTransactions().size(),
            equalTo(config.validBlockTransactionCount().intValue()));
}
 
Example 6
Source File: CoreIT.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testEthGetBlockByNumberReturnTransactionObjects() throws Exception {
    EthBlock ethBlock = web3j.ethGetBlockByNumber(
            DefaultBlockParameter.valueOf(config.validBlock()), true).send();

    EthBlock.Block block = ethBlock.getBlock();
    assertNotNull(ethBlock.getBlock());
    assertThat(block.getNumber(), equalTo(config.validBlock()));
    assertThat(block.getTransactions().size(),
            equalTo(config.validBlockTransactionCount().intValue()));
}
 
Example 7
Source File: EthQueryExecutor.java    From eth-jdbc-connector with Apache License 2.0 5 votes vote down vote up
private Block getBlockByNumber(String blockNumber) throws IOException, Exception {
    LOGGER.info("Getting block - " + blockNumber + " Information ");
    EthBlock block = web3jClient
            .ethGetBlockByNumber(DefaultBlockParameter.valueOf(new BigInteger(blockNumber)), true).send();

    if (block == null || block.hasError())
        throw new Exception("blockNumber not found : " + blockNumber);

    return block.getBlock();
}
 
Example 8
Source File: EthQueryExecutor.java    From eth-jdbc-connector with Apache License 2.0 5 votes vote down vote up
private Block getBlockByHash(String blockHash) throws IOException, Exception {
    LOGGER.info("Getting  information of block with hash - " + blockHash);
    EthBlock block = web3jClient.ethGetBlockByHash(blockHash, true).send();
    if (block == null || block.hasError())
        throw new Exception("blockHash not found : " + blockHash);

    return block.getBlock();
}
 
Example 9
Source File: CoreIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEthGetBlockByHashReturnHashObjects() throws Exception {
    EthBlock ethBlock = web3j.ethGetBlockByHash(config.validBlockHash(), false).send();

    EthBlock.Block block = ethBlock.getBlock();
    assertNotNull(ethBlock.getBlock());
    assertEquals(block.getNumber(), (config.validBlock()));
    assertEquals(
            block.getTransactions().size(), (config.validBlockTransactionCount().intValue()));
}
 
Example 10
Source File: CoreIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEthGetBlockByHashReturnFullTransactionObjects() throws Exception {
    EthBlock ethBlock = web3j.ethGetBlockByHash(config.validBlockHash(), true).send();

    EthBlock.Block block = ethBlock.getBlock();
    assertNotNull(ethBlock.getBlock());
    assertEquals(block.getNumber(), (config.validBlock()));
    assertEquals(
            block.getTransactions().size(), (config.validBlockTransactionCount().intValue()));
}
 
Example 11
Source File: CoreIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEthGetBlockByNumberReturnHashObjects() throws Exception {
    EthBlock ethBlock =
            web3j.ethGetBlockByNumber(DefaultBlockParameter.valueOf(config.validBlock()), false)
                    .send();

    EthBlock.Block block = ethBlock.getBlock();
    assertNotNull(ethBlock.getBlock());
    assertEquals(block.getNumber(), (config.validBlock()));
    assertEquals(
            block.getTransactions().size(), (config.validBlockTransactionCount().intValue()));
}
 
Example 12
Source File: CoreIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEthGetBlockByNumberReturnTransactionObjects() throws Exception {
    EthBlock ethBlock =
            web3j.ethGetBlockByNumber(DefaultBlockParameter.valueOf(config.validBlock()), true)
                    .send();

    EthBlock.Block block = ethBlock.getBlock();
    assertNotNull(ethBlock.getBlock());
    assertEquals(block.getNumber(), (config.validBlock()));
    assertEquals(
            block.getTransactions().size(), (config.validBlockTransactionCount().intValue()));
}
 
Example 13
Source File: PollingBlockSubscriptionStrategy.java    From eventeum with Apache License 2.0 4 votes vote down vote up
@Override
Block convertToEventeumBlock(EthBlock blockObject) {
    return new Web3jBlock(blockObject.getBlock(), nodeName);
}
 
Example 14
Source File: PubSubBlockSubscriptionStrategy.java    From eventeum with Apache License 2.0 4 votes vote down vote up
Block convertToEventeumBlock(EthBlock blockObject) {
    return new Web3jBlock(blockObject.getBlock(), nodeName);
}
 
Example 15
Source File: TransferEtherTest.java    From web3j_demo with Apache License 2.0 4 votes vote down vote up
/**
 * Test accessing transactions, blocks and their attributes using methods {@link Web3j#ethGetTransactionByHash()},  {@link Web3j#ethGetBlockByHash()}  {@link Web3j#ethGetBlockByNumber()}.
 */
@Test
public void testTransactionAndBlockAttributes() throws Exception {
	String account0 = getCoinbase();
	String account1 = getAccount(1);
	BigInteger transferAmount = new BigInteger("31415926");

	String txHash = transferWei(account0, account1, transferAmount);
	waitForReceipt(txHash);

	// query for tx via tx hash value
	EthTransaction ethTx = web3j
			.ethGetTransactionByHash(txHash)
			.sendAsync()
			.get();

	org.web3j.protocol.core.methods.response.Transaction tx = ethTx
			.getTransaction()
			.get();

	String blockHash = tx.getBlockHash();
	BigInteger blockNumber = tx.getBlockNumber();
	String from = tx.getFrom();
	String to = tx.getTo();
	BigInteger amount = tx.getValue();

	// check tx attributes
	assertTrue("Tx hash does not match input hash", txHash.equals(tx.getHash()));
	assertTrue("Tx block index invalid", blockNumber == null || blockNumber.compareTo(new BigInteger("0")) >= 0);
	assertTrue("Tx from account does not match input account", account0.equals(from));
	assertTrue("Tx to account does not match input account", account1.equals(to));
	assertTrue("Tx transfer amount does not match input amount", transferAmount.equals(amount));

	// query for block by hash
	EthBlock ethBlock = web3j
			.ethGetBlockByHash(blockHash, true)
			.sendAsync()
			.get();

	Block blockByHash = ethBlock.getBlock(); 
	assertNotNull(String.format("Failed to get block for hash %s", blockHash), blockByHash);
	System.out.println("Got block for hash " + blockHash);


	// query for block by number
	DefaultBlockParameter blockParameter = DefaultBlockParameter
			.valueOf(blockNumber);

	ethBlock = web3j
			.ethGetBlockByNumber(blockParameter, true)
			.sendAsync()
			.get();
	
	Block blockByNumber = ethBlock.getBlock(); 
	assertNotNull(String.format("Failed to get block for number %d", blockNumber), blockByNumber);
	System.out.println("Got block for number " + blockNumber);

	assertTrue("Bad tx hash for block by number", blockByNumber.getHash().equals(blockHash));
	assertTrue("Bad tx number for block by hash", blockByHash.getNumber().equals(blockNumber));
	assertTrue("Query block by hash and number have different parent hashes", blockByHash.getParentHash().equals(blockByNumber.getParentHash()));
	assertTrue("Query block by hash and number results in different blocks", blockByHash.equals(blockByNumber));

	// find original tx in block
	boolean found = false;
	for(TransactionResult<?> txResult: blockByHash.getTransactions()) {
		TransactionObject txObject = (TransactionObject) txResult;

		// verify tx attributes returned by block query
		if(txObject.getHash().equals(txHash)) {
			assertTrue("Tx from block has bad from", txObject.getFrom().equals(account0));
			assertTrue("Tx from block has bad to", txObject.getTo().equals(account1));
			assertTrue("Tx from block has bad amount", txObject.getValue().equals(transferAmount));
			found = true;
			break;
		}
	}

	assertTrue("Tx not found in blocks transaction list", found);
}