public static RespondingEthPeer createPeer( final EthProtocolManager ethProtocolManager, final Blockchain blockchain) { final ChainHead head = blockchain.getChainHead(); return RespondingEthPeer.create( ethProtocolManager, head.getHash(), head.getTotalDifficulty(), blockchain.getChainHeadBlockNumber()); } }
private BlockHeader getHeaderForCurrentChainHead() { return blockchain.getBlockHeader(blockchain.getChainHeadHash()).get(); }
private BlockHeader block(final long blockNumber) { return otherBlockchain.getBlockHeader(blockNumber).get(); } }
private MutableBlockchain createShortChain(final long lastBlockToInclude) { final BlockHeader genesisHeader = blockchain.getBlockHeader(BlockHeader.GENESIS_BLOCK_NUMBER).get(); final BlockBody genesisBody = blockchain.getBlockBody(genesisHeader.getHash()).get(); final Block genesisBlock = new Block(genesisHeader, genesisBody); final MutableBlockchain shortChain = createInMemoryBlockchain(genesisBlock); long nextBlock = genesisHeader.getNumber() + 1; while (nextBlock <= lastBlockToInclude) { final BlockHeader header = blockchain.getBlockHeader(nextBlock).get(); final BlockBody body = blockchain.getBlockBody(header.getHash()).get(); final List<TransactionReceipt> receipts = blockchain.getTxReceipts(header.getHash()).get(); final Block block = new Block(header, body); shortChain.appendBlock(block, receipts); nextBlock++; } return shortChain; }
private void assertTotalDifficultiesAreConsistent(final Blockchain blockchain, final Block head) { // Check that total difficulties are summed correctly long num = BlockHeader.GENESIS_BLOCK_NUMBER; UInt256 td = UInt256.of(0); while (num <= head.getHeader().getNumber()) { final Hash curHash = blockchain.getBlockHashByNumber(num).get(); final BlockHeader curHead = blockchain.getBlockHeader(curHash).get(); td = td.plus(curHead.getDifficulty()); assertEquals(td, blockchain.getTotalDifficultyByHash(curHash).get()); num += 1; } // Check reported chainhead td assertEquals(td, blockchain.getChainHead().getTotalDifficulty()); }
private MockPeerConnection setupPeer( final EthProtocolManager ethManager, final PeerSendHandler onSend) { final MockPeerConnection peer = setupPeerWithoutStatusExchange(ethManager, onSend); final StatusMessage statusMessage = StatusMessage.create( EthVersion.V63, 1, blockchain.getChainHead().getTotalDifficulty(), blockchain.getChainHeadHash(), blockchain.getBlockHeader(BlockHeader.GENESIS_BLOCK_NUMBER).get().getHash()); ethManager.processMessage(EthProtocol.ETH63, new DefaultMessage(peer, statusMessage)); return peer; }
private void assertBlockIsHead(final Blockchain blockchain, final Block head) { assertEquals(head.getHash(), blockchain.getChainHeadHash()); assertEquals(head.getHeader().getNumber(), blockchain.getChainHeadBlockNumber()); assertEquals(head.getHash(), blockchain.getChainHead().getHash()); }
@Test public void initializeNew() { final BlockDataGenerator gen = new BlockDataGenerator(); final KeyValueStorage kvStore = new InMemoryKeyValueStorage(); final Block genesisBlock = gen.genesisBlock(); final DefaultMutableBlockchain blockchain = createBlockchain(kvStore, genesisBlock); assertBlockDataIsStored(blockchain, genesisBlock, Collections.emptyList()); assertBlockIsHead(blockchain, genesisBlock); assertTotalDifficultiesAreConsistent(blockchain, genesisBlock); assertThat(blockchain.getForks()).isEmpty(); }
@Test public void blockAddedObserver_addRemoveSingle() { final BlockDataGenerator gen = new BlockDataGenerator(); final KeyValueStorage kvStore = new InMemoryKeyValueStorage(); final Block genesisBlock = gen.genesisBlock(); final DefaultMutableBlockchain blockchain = createBlockchain(kvStore, genesisBlock); final long observerId = blockchain.observeBlockAdded((block, chain) -> {}); assertThat(blockchain.observerCount()).isEqualTo(1); assertThat(blockchain.removeObserver(observerId)).isTrue(); assertThat(blockchain.observerCount()).isEqualTo(0); }
private void recordNewBlockEvent() { filterManager.recordBlockEvent( BlockAddedEvent.createForHeadAdvancement(new BlockDataGenerator().block()), blockchainQueries.getBlockchain()); }
@Test(expected = IllegalArgumentException.class) public void appendUnconnectedBlock() { final BlockDataGenerator gen = new BlockDataGenerator(); final KeyValueStorage kvStore = new InMemoryKeyValueStorage(); final Block genesisBlock = gen.genesisBlock(); final DefaultMutableBlockchain blockchain = createBlockchain(kvStore, genesisBlock); final BlockOptions options = new BlockOptions().setBlockNumber(1L).setParentHash(Hash.ZERO); final Block newBlock = gen.block(options); final List<TransactionReceipt> receipts = gen.receipts(newBlock); blockchain.appendBlock(newBlock, receipts); }
private void createNewBlockHeadersSubscriptionService( final Blockchain blockchain, final WorldStateArchive worldStateArchive, final SubscriptionManager subscriptionManager) { final NewBlockHeadersSubscriptionService newBlockHeadersSubscriptionService = new NewBlockHeadersSubscriptionService( subscriptionManager, new BlockchainQueries(blockchain, worldStateArchive)); blockchain.observeBlockAdded(newBlockHeadersSubscriptionService); }
@Test(expected = NullPointerException.class) public void blockAddedObserver_nullObserver() { final BlockDataGenerator gen = new BlockDataGenerator(); final KeyValueStorage kvStore = new InMemoryKeyValueStorage(); final Block genesisBlock = gen.genesisBlock(); final DefaultMutableBlockchain blockchain = createBlockchain(kvStore, genesisBlock); blockchain.observeBlockAdded(null); }
@Test public void blockAddedObserver_removeNonexistentObserver() { final BlockDataGenerator gen = new BlockDataGenerator(); final KeyValueStorage kvStore = new InMemoryKeyValueStorage(); final Block genesisBlock = gen.genesisBlock(); final DefaultMutableBlockchain blockchain = createBlockchain(kvStore, genesisBlock); assertThat(blockchain.removeObserver(7)).isFalse(); }
private DefaultMutableBlockchain createBlockchain( final KeyValueStorage kvStore, final Block genesisBlock) { return new DefaultMutableBlockchain( genesisBlock, new KeyValueStoragePrefixedKeyBlockchainStorage( kvStore, MainnetBlockHashFunction::createHash), new NoOpMetricsSystem()); } }
private MutableBlockchain createShortChain( final Blockchain blockchain, final long truncateAtBlockNumber) { final BlockHeader genesisHeader = blockchain.getBlockHeader(BlockHeader.GENESIS_BLOCK_NUMBER).get(); final BlockBody genesisBody = blockchain.getBlockBody(genesisHeader.getHash()).get(); final Block genesisBlock = new Block(genesisHeader, genesisBody); final MutableBlockchain shortChain = createInMemoryBlockchain(genesisBlock); long nextBlock = genesisHeader.getNumber() + 1; while (nextBlock <= truncateAtBlockNumber) { final BlockHeader header = blockchain.getBlockHeader(nextBlock).get(); final BlockBody body = blockchain.getBlockBody(header.getHash()).get(); final List<TransactionReceipt> receipts = blockchain.getTxReceipts(header.getHash()).get(); final Block block = new Block(header, body); shortChain.appendBlock(block, receipts); nextBlock++; } return shortChain; } }
@Override protected List<BlockHeader> generateDataToBeRequested() { return singletonList(blockchain.getBlockHeader(PIVOT_BLOCK_NUMBER).get()); }
@Test(expected = IllegalArgumentException.class) public void appendBlockWithMismatchedReceipts() { final BlockDataGenerator gen = new BlockDataGenerator(); final KeyValueStorage kvStore = new InMemoryKeyValueStorage(); final Block genesisBlock = gen.genesisBlock(); final DefaultMutableBlockchain blockchain = createBlockchain(kvStore, genesisBlock); final BlockOptions options = new BlockOptions().setBlockNumber(1L).setParentHash(genesisBlock.getHash()); final Block newBlock = gen.block(options); final List<TransactionReceipt> receipts = gen.receipts(newBlock); receipts.add(gen.receipt()); blockchain.appendBlock(newBlock, receipts); }
private void createLogsSubscriptionService( final Blockchain blockchain, final WorldStateArchive worldStateArchive, final SubscriptionManager subscriptionManager) { final LogsSubscriptionService logsSubscriptionService = new LogsSubscriptionService( subscriptionManager, new BlockchainQueries(blockchain, worldStateArchive)); blockchain.observeBlockAdded(logsSubscriptionService); }
private MutableBlockchain createShortChain(final long truncateAtBlockNumber) { final BlockHeader genesisHeader = blockchain.getBlockHeader(BlockHeader.GENESIS_BLOCK_NUMBER).get(); final BlockBody genesisBody = blockchain.getBlockBody(genesisHeader.getHash()).get(); final Block genesisBlock = new Block(genesisHeader, genesisBody); final MutableBlockchain shortChain = createInMemoryBlockchain(genesisBlock); long nextBlock = genesisHeader.getNumber() + 1; while (nextBlock <= truncateAtBlockNumber) { final BlockHeader header = blockchain.getBlockHeader(nextBlock).get(); final BlockBody body = blockchain.getBlockBody(header.getHash()).get(); final List<TransactionReceipt> receipts = blockchain.getTxReceipts(header.getHash()).get(); final Block block = new Block(header, body); shortChain.appendBlock(block, receipts); nextBlock++; } return shortChain; } }