private static List<Transaction> toTransactions(EthBlock ethBlock) { // If you ever see an exception thrown here, it's probably due to an incomplete chain in // Geth/Parity. You should resync to solve. return ethBlock.getBlock().getTransactions().stream() .map(transactionResult -> (Transaction) transactionResult.get()) .collect(Collectors.toList()); } }
private BigInteger getBlockNumber( DefaultBlockParameter defaultBlockParameter) throws IOException { if (defaultBlockParameter instanceof DefaultBlockParameterNumber) { return ((DefaultBlockParameterNumber) defaultBlockParameter).getBlockNumber(); } else { EthBlock latestEthBlock = web3j.ethGetBlockByNumber( defaultBlockParameter, false).send(); return latestEthBlock.getBlock().getNumber(); } }
boolean isSynced() throws Exception { EthSyncing ethSyncing = web3j.ethSyncing().send(); if (ethSyncing.isSyncing()) { return false; } else { EthBlock ethBlock = web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send(); long timestamp = ethBlock.getBlock().getTimestamp().longValueExact() * 1000; return System.currentTimeMillis() - syncThreshold < timestamp; } }
@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())); }
@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())); }
@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())); }
@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())); }
@Test public void testEthBlockNull() { buildResponse( "{\n" + " \"result\": null\n" + "}" ); EthBlock ethBlock = deserialiseResponse(EthBlock.class); assertNull(ethBlock.getBlock()); }
.flatMap(it -> it.getBlock().getTransactions().stream()) .map(it -> (Transaction) it.get()) .collect(Collectors.toList());
@Test public void testReplayPastAndFutureBlocksFlowable() throws Exception { EthBlock ethBlock = web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false) .send(); BigInteger latestBlockNumber = ethBlock.getBlock().getNumber(); run(web3j.replayPastAndFutureBlocksFlowable( new DefaultBlockParameterNumber(latestBlockNumber.subtract(BigInteger.ONE)), false)); }
@Test public void testEthGetUncleByBlockHashAndIndex() throws Exception { EthBlock ethBlock = web3j.ethGetUncleByBlockHashAndIndex( config.validUncleBlockHash(), BigInteger.ZERO).send(); assertNotNull(ethBlock.getBlock()); }
assertThat(ethBlock.getBlock(), equalTo(block));
assertThat(ethBlock.getBlock(), equalTo(block));
@Test public void testEthGetUncleByBlockNumberAndIndex() throws Exception { EthBlock ethBlock = web3j.ethGetUncleByBlockNumberAndIndex( DefaultBlockParameter.valueOf(config.validUncleBlock()), BigInteger.ZERO) .send(); assertNotNull(ethBlock.getBlock()); }
assertThat(ethBlock.getBlock(), equalTo(block));
/** * Gets the latest block number. * * @return the latest block number */ public BigInteger getLatestBlockNumber() { return getLatestBlock(false).getBlock().getNumber(); }
/** * Gets the first block number. * * @return the first block number */ public BigInteger getFirstBlockNumber() { return getFirstBlock(false).getBlock().getNumber(); }
@InvokeOnHeader(Web3jConstants.ETH_GET_UNCLE_BY_BLOCK_HASH_AND_INDEX) void ethGetUncleByBlockHashAndIndex(Message message) throws IOException { String blockHash = message.getHeader(Web3jConstants.BLOCK_HASH, configuration::getBlockHash, String.class); BigInteger uncleIndex = message.getHeader(Web3jConstants.INDEX, configuration::getIndex, BigInteger.class); Request<?, EthBlock> request = web3j.ethGetUncleByBlockHashAndIndex(blockHash, uncleIndex); setRequestId(message, request); EthBlock response = request.send(); boolean hasError = checkForError(message, response); if (!hasError) { message.setBody(response.getBlock()); } }
@Override public Block execute(final JsonRequestFactories 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); } } }