diff --git a/src/main/java/org/adridadou/ethereum/propeller/EthereumFacade.java b/src/main/java/org/adridadou/ethereum/propeller/EthereumFacade.java index 8287a42..5b6aa5f 100644 --- a/src/main/java/org/adridadou/ethereum/propeller/EthereumFacade.java +++ b/src/main/java/org/adridadou/ethereum/propeller/EthereumFacade.java @@ -1,6 +1,7 @@ package org.adridadou.ethereum.propeller; import org.adridadou.ethereum.propeller.converters.future.FutureConverter; +import org.adridadou.ethereum.propeller.event.BlockInfo; import org.adridadou.ethereum.propeller.event.EthereumEventHandler; import org.adridadou.ethereum.propeller.exception.EthereumApiException; import org.adridadou.ethereum.propeller.service.CryptoProvider; @@ -251,7 +252,6 @@ public EthereumEventHandler events() { return ethereumProxy.events(); } - /** /** * Returns the current best block number * @return The best block number @@ -260,6 +260,26 @@ public long getCurrentBlockNumber() { return ethereumProxy.getCurrentBlockNumber(); } + /** + * Returns block for provided blocknumber + * + * @param blockNumber The block number + * @return The block if found + */ + public Optional getBlock(long blockNumber) { + return ethereumProxy.getBlock(blockNumber); + } + + /* + * Returns block for provided blockhash + * + * @param blockHash The block number + * @return The block if found + */ + public Optional getBlock(EthHash blockHash) { + return ethereumProxy.getBlock(blockHash); + } + /** * Sends ether * @param fromAccount The account that sends ether diff --git a/src/main/java/org/adridadou/ethereum/propeller/EthereumProxy.java b/src/main/java/org/adridadou/ethereum/propeller/EthereumProxy.java index 2506adb..96c21d3 100644 --- a/src/main/java/org/adridadou/ethereum/propeller/EthereumProxy.java +++ b/src/main/java/org/adridadou/ethereum/propeller/EthereumProxy.java @@ -346,6 +346,14 @@ EthValue getBalance(final EthAddress address) { return ethereum.getBalance(address); } + Optional getBlock(long blockNumber) { + return this.ethereum.getBlock(blockNumber); + } + + Optional getBlock(EthHash blockHash) { + return this.ethereum.getBlock(blockHash); + } + private void increasePendingTransactionCounter(EthAddress address, EthHash hash) { Set hashes = pendingTransactions.computeIfAbsent(address, (key) -> Collections.synchronizedSet(new HashSet<>())); hashes.add(hash); diff --git a/src/main/java/org/adridadou/ethereum/propeller/event/BlockInfo.java b/src/main/java/org/adridadou/ethereum/propeller/event/BlockInfo.java index 55b762a..7878dd6 100644 --- a/src/main/java/org/adridadou/ethereum/propeller/event/BlockInfo.java +++ b/src/main/java/org/adridadou/ethereum/propeller/event/BlockInfo.java @@ -2,14 +2,17 @@ import org.adridadou.ethereum.propeller.values.TransactionReceipt; +import java.math.BigInteger; import java.util.List; public class BlockInfo { public final long blockNumber; + public final long timestamp; public final List receipts; - public BlockInfo(long blockNumber, List receipts) { + public BlockInfo(long blockNumber, long timestamp, List receipts) { this.blockNumber = blockNumber; + this.timestamp = timestamp; this.receipts = receipts; } diff --git a/src/main/java/org/adridadou/ethereum/propeller/rpc/EthereumRpc.java b/src/main/java/org/adridadou/ethereum/propeller/rpc/EthereumRpc.java index 05bfea5..d0c55d3 100644 --- a/src/main/java/org/adridadou/ethereum/propeller/rpc/EthereumRpc.java +++ b/src/main/java/org/adridadou/ethereum/propeller/rpc/EthereumRpc.java @@ -1,5 +1,6 @@ package org.adridadou.ethereum.propeller.rpc; +import java.math.BigInteger; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -166,12 +167,12 @@ BlockInfo toBlockInfo(EthBlock ethBlock) { List receiptList = receipts.entrySet().stream() .map(entry -> toReceipt(txObjects.get(entry.getKey()), entry.getValue())).collect(Collectors.toList()); - return new BlockInfo(block.getNumber().longValue(), receiptList); + return new BlockInfo(block.getNumber().longValue(), block.getTimestamp().longValue(), receiptList); } catch (Throwable ex) { logger.error("error while converting to block info", ex); - return new BlockInfo(block.getNumber().longValue(), Collections.emptyList()); + return new BlockInfo(block.getNumber().longValue(), block.getTimestamp().longValue(), Collections.emptyList()); } - }).orElseGet(() -> new BlockInfo(-1, new ArrayList<>())); + }).orElseGet(() -> new BlockInfo(-1, 0, new ArrayList<>())); } private TransactionReceipt toReceipt(Transaction tx, org.web3j.protocol.core.methods.response.TransactionReceipt receipt) { diff --git a/src/test/java/org/adridadou/ethereum/propeller/backend/EthJEventListener.java b/src/test/java/org/adridadou/ethereum/propeller/backend/EthJEventListener.java index c2cbadd..aeb37ed 100644 --- a/src/test/java/org/adridadou/ethereum/propeller/backend/EthJEventListener.java +++ b/src/test/java/org/adridadou/ethereum/propeller/backend/EthJEventListener.java @@ -56,7 +56,7 @@ static org.adridadou.ethereum.propeller.values.TransactionReceipt toReceipt(Tran @Override public void onBlock(Block block, List receipts) { EthHash blockHash = EthHash.of(block.getHash()); - eventHandler.onBlock(new BlockInfo(block.getNumber(), receipts.stream() + eventHandler.onBlock(new BlockInfo(block.getNumber(), block.getTimestamp(), receipts.stream() .map(tx -> EthJEventListener.toReceipt(tx, blockHash)) .collect(Collectors.toList()))); diff --git a/src/test/java/org/adridadou/ethereum/propeller/backend/EthereumTest.java b/src/test/java/org/adridadou/ethereum/propeller/backend/EthereumTest.java index d2731fb..048221d 100644 --- a/src/test/java/org/adridadou/ethereum/propeller/backend/EthereumTest.java +++ b/src/test/java/org/adridadou/ethereum/propeller/backend/EthereumTest.java @@ -215,7 +215,7 @@ private ECKey getKey(CryptoProvider cryptoProvider) { } BlockInfo toBlockInfo(Block block) { - return new BlockInfo(block.getNumber(), block.getTransactionsList().stream() + return new BlockInfo(block.getNumber(), block.getTimestamp(), block.getTransactionsList().stream() .map(tx -> this.toReceipt(tx, EthHash.of(block.getHash()))).collect(Collectors.toList())); }