当前位置: 首页>>代码示例>>Java>>正文


Java EthBlock.getBlock方法代码示例

本文整理汇总了Java中org.web3j.protocol.core.methods.response.EthBlock.getBlock方法的典型用法代码示例。如果您正苦于以下问题:Java EthBlock.getBlock方法的具体用法?Java EthBlock.getBlock怎么用?Java EthBlock.getBlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.web3j.protocol.core.methods.response.EthBlock的用法示例。


在下文中一共展示了EthBlock.getBlock方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getTransactionsFromBlock

import org.web3j.protocol.core.methods.response.EthBlock; //导入方法依赖的package包/类
public List<TransactionObject> getTransactionsFromBlock(BigInteger blockNumber) throws BlockNotFoundException {
    try {
        EthBlock ethBlock = web3.ethGetBlockByNumber(new DefaultBlockParameterNumber(blockNumber), true).send();
        if (ethBlock == null || ethBlock.getBlock() == null) {
            throw new BlockNotFoundException("Block number: " + blockNumber + " was not found");
        }
        return ethBlock.getBlock().getTransactions().stream()
                .map(TransactionObject.class::cast)
                .collect(Collectors.toList());
    } catch (IOException e) {
        logger.error("Error getTransactionsFromBlock", e);
    }
    return Collections.emptyList();
}
 
开发者ID:fergarrui,项目名称:ethereum-bytecode-analyzer,代码行数:15,代码来源:BlockchainService.java

示例2: startImport

import org.web3j.protocol.core.methods.response.EthBlock; //导入方法依赖的package包/类
private CompositeFuture startImport(EthBlock fullBlock) {
    StorableBlock block = new StorableBlock(fullBlock.getBlock());
    listener.onImportStarted(block.getHash(), block.getNumber());
    return CompositeFuture.all(
            importBlocks(block),
            importTx(block, getTransactionList(fullBlock))
    );
}
 
开发者ID:codingchili,项目名称:ethereum-ingest,代码行数:9,代码来源:BlockService.java

示例3: testEthGetBlockByHashReturnHashObjects

import org.web3j.protocol.core.methods.response.EthBlock; //导入方法依赖的package包/类
@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()));
}
 
开发者ID:web3j,项目名称:web3j,代码行数:12,代码来源:CoreIT.java

示例4: testEthGetBlockByHashReturnFullTransactionObjects

import org.web3j.protocol.core.methods.response.EthBlock; //导入方法依赖的package包/类
@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()));
}
 
开发者ID:web3j,项目名称:web3j,代码行数:12,代码来源:CoreIT.java

示例5: testEthGetBlockByNumberReturnHashObjects

import org.web3j.protocol.core.methods.response.EthBlock; //导入方法依赖的package包/类
@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()));
}
 
开发者ID:web3j,项目名称:web3j,代码行数:12,代码来源:CoreIT.java

示例6: testEthGetBlockByNumberReturnTransactionObjects

import org.web3j.protocol.core.methods.response.EthBlock; //导入方法依赖的package包/类
@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()));
}
 
开发者ID:web3j,项目名称:web3j,代码行数:12,代码来源:CoreIT.java

示例7: testTransactionAndBlockAttributes

import org.web3j.protocol.core.methods.response.EthBlock; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:matthiaszimmermann,项目名称:web3j_demo,代码行数:82,代码来源:TransferEtherTest.java


注:本文中的org.web3j.protocol.core.methods.response.EthBlock.getBlock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。