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()); } }
public Block getBlock() { return getResult(); }
@Test public void testErrorResponseComplexData() { buildResponse( "{" + " \"jsonrpc\":\"2.0\"," + " \"id\":1," + " \"error\":{" + " \"code\":-32602," + " \"message\":\"Invalid address length, expected 40 got 64 bytes\"," + " \"data\":{\"foo\":\"bar\"}" + " }" + "}" ); EthBlock ethBlock = deserialiseResponse(EthBlock.class); assertTrue(ethBlock.hasError()); assertThat(ethBlock.getError().getData(), equalTo("{\"foo\":\"bar\"}")); }
@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); } } }
private EthBlock createBlockWithTransactions(int blockNumber, List<Transaction> transactions) { EthBlock ethBlock = new EthBlock(); EthBlock.Block block = new EthBlock.Block(); block.setNumber(Numeric.encodeQuantity(BigInteger.valueOf(blockNumber))); List<EthBlock.TransactionResult> transactionResults = transactions.stream() .map(it -> (EthBlock.TransactionResult<Transaction>) () -> it) .collect(Collectors.toList()); block.setTransactions(transactionResults); ethBlock.setResult(block); return ethBlock; }
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(); } }
@Test public void testErrorResponse() { buildResponse( "{" + " \"jsonrpc\":\"2.0\"," + " \"id\":1," + " \"error\":{" + " \"code\":-32602," + " \"message\":\"Invalid address length, expected 40 got 64 bytes\"," + " \"data\":null" + " }" + "}" ); EthBlock ethBlock = deserialiseResponse(EthBlock.class); assertTrue(ethBlock.hasError()); assertThat(ethBlock.getError(), equalTo( new Response.Error(-32602, "Invalid address length, expected 40 got 64 bytes"))); }
private void configureLatestBlock(long timestamp) throws IOException { EthBlock.Block block = new EthBlock.Block(); block.setTimestamp(Numeric.encodeQuantity(BigInteger.valueOf(timestamp))); EthBlock ethBlock = new EthBlock(); ethBlock.setResult(block); when(web3jService.send(any(Request.class), eq(EthBlock.class))) .thenReturn(ethBlock); }
/** * Persist blocks and transactions. * * @param block * the block */ private void persistBlocksAndTransactions(EthBlock block) { Block blk = EtherObjectConverterUtil.convertEtherBlockToKunderaBlock(block, false); try { em.persist(blk); LOGGER.debug("Block number " + getBlockNumberWithRawData(blk.getNumber()) + " is stored!"); } catch (Exception ex) { LOGGER.error("Block number " + getBlockNumberWithRawData(blk.getNumber()) + " is not stored. ", ex); throw new KunderaException("Block number " + getBlockNumberWithRawData(blk.getNumber()) + " is not stored. ", ex); } for (TransactionResult tx : block.getResult().getTransactions()) { persistTransactions(EtherObjectConverterUtil.convertEtherTxToKunderaTx(tx), blk.getNumber()); } em.clear(); }
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));