@Test public void blockAddedObserver_invokedMultiple() { 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); final AtomicBoolean observer1Invoked = new AtomicBoolean(false); blockchain.observeBlockAdded( (block, chain) -> { observer1Invoked.set(true); }); final AtomicBoolean observer2Invoked = new AtomicBoolean(false); blockchain.observeBlockAdded( (block, chain) -> { observer2Invoked.set(true); }); final AtomicBoolean observer3Invoked = new AtomicBoolean(false); blockchain.observeBlockAdded( (block, chain) -> { observer3Invoked.set(true); }); blockchain.appendBlock(newBlock, receipts); assertThat(observer1Invoked.get()).isTrue(); assertThat(observer2Invoked.get()).isTrue(); assertThat(observer3Invoked.get()).isTrue(); }
new BlockOptions() .setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkBlock) .setDifficulty(chain.get(forkBlock).getHeader().getDifficulty().plus(10L)); final Block fork = gen.block(options);
new BlockOptions() .setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkBlock) .setDifficulty(chain.get(forkBlock).getHeader().getDifficulty().plus(10L)) .addTransaction(overlappingTx)
final BlockDataGenerator.BlockOptions localOptions = new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block localBlock = blockDataGenerator.block(localOptions); new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(remoteBlockchain.getBlockHashByNumber(i - 1).get()); final Block remoteBlock = blockDataGenerator.block(remoteOptions);
final BlockDataGenerator.BlockOptions options00 = new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block block00 = blockDataGenerator.block(options00); new BlockDataGenerator.BlockOptions() .setDifficulty(UInt256.ONE) .setBlockNumber(i) .setParentHash(remoteBlockchain.getBlockHashByNumber(i - 1).get()); final Block block01 = blockDataGenerator.block(options01);
gen.block(BlockOptions.create().setBlockNumber(blockchain.getChainHeadBlockNumber()));
final BlockDataGenerator.BlockOptions options00 = new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block block00 = blockDataGenerator.block(options00); new BlockDataGenerator.BlockOptions() .setDifficulty(UInt256.ONE) .setBlockNumber(i) .setParentHash(remoteBlockchain.getBlockHashByNumber(i - 1).get()); final Block block01 = blockDataGenerator.block(options01);
dataGen.block(BlockOptions.create().setStateRoot(stateRoot).setBlockNumber(10)).getHeader();
.setBlockNumber(1L) .setParentHash(blockchain.getBlockHashByNumber(0L).get()); final Block block = gen.block(options);
final Block fakeFirstBlock = gen.block(BlockOptions.create().setBlockNumber(5L)); final Block firstBlock = getBlockAtNumber(5L); final Block secondBlock = getBlockAtNumber(6L);
final BlockDataGenerator.BlockOptions localOptions = new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block localBlock = blockDataGenerator.block(localOptions); new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(remoteBlockchain.getBlockHashByNumber(i - 1).get()); final Block remoteBlock = blockDataGenerator.block(remoteOptions);
final BlockHeader header = dataGen .block(BlockOptions.create().setStateRoot(EMPTY_TRIE_ROOT).setBlockNumber(10)) .getHeader();
@Test public void logsShouldBeFlaggedAsRemovedWhenBlockIsNotInCanonicalChain() { // create initial blockchain final BlockchainWithData data = setupBlockchain(3); final Block targetBlock = data.blockData.get(data.blockData.size() - 1).block; // check that logs have removed = false List<LogWithMetadata> logs = data.blockchainQueries.matchingLogs(targetBlock.getHash(), new LogsQuery.Builder().build()); assertThat(logs).isNotEmpty(); assertThat(logs).allMatch(l -> !l.isRemoved()); // Create parallel fork of length 1 final int forkBlock = 2; final int commonAncestor = 1; final BlockOptions options = new BlockOptions() .setParentHash(data.blockchain.getBlockHashByNumber(commonAncestor).get()) .setBlockNumber(forkBlock) .setDifficulty( data.blockchain.getBlockHeader(forkBlock).get().getDifficulty().plus(10L)); final Block fork = gen.block(options); final List<TransactionReceipt> forkReceipts = gen.receipts(fork); // Add fork data.blockchain.appendBlock(fork, forkReceipts); // check that logs have removed = true logs = data.blockchainQueries.matchingLogs(targetBlock.getHash(), new LogsQuery.Builder().build()); assertThat(logs).isNotEmpty(); assertThat(logs).allMatch(LogWithMetadata::isRemoved); }
@Test public void blockAddedObserver_invokedSingle() { 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); final AtomicBoolean observerInvoked = new AtomicBoolean(false); blockchain.observeBlockAdded( (block, chain) -> { observerInvoked.set(true); }); blockchain.appendBlock(newBlock, receipts); assertThat(observerInvoked.get()).isTrue(); }
@BeforeClass public static void setupClass() { genesisBlock = blockDataGenerator.genesisBlock(); localBlockchain = createInMemoryBlockchain(genesisBlock); // Setup local chain. for (int i = 1; i <= chainHeight; i++) { final BlockDataGenerator.BlockOptions options = new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block block = blockDataGenerator.block(options); final List<TransactionReceipt> receipts = blockDataGenerator.receipts(block); localBlockchain.appendBlock(block, receipts); } }
@Test public void appendBlock() { 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); blockchain.appendBlock(newBlock, receipts); assertBlockIsHead(blockchain, newBlock); assertTotalDifficultiesAreConsistent(blockchain, newBlock); assertThat(blockchain.getForks()).isEmpty(); }
@BeforeClass public static void setupClass() { genesisBlock = blockDataGenerator.genesisBlock(); localBlockchain = createInMemoryBlockchain(genesisBlock); // Setup local chain for (int i = 1; i <= chainHeight; i++) { final BlockDataGenerator.BlockOptions options = new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block block = blockDataGenerator.block(options); final List<TransactionReceipt> receipts = blockDataGenerator.receipts(block); localBlockchain.appendBlock(block, receipts); } }
@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 Hash appendBlockToBlockchain() { final long blockNumber = currentBlock.getHeader().getNumber() + 1; final Hash parentHash = currentBlock.getHash(); final BlockDataGenerator.BlockOptions options = new BlockDataGenerator.BlockOptions().setBlockNumber(blockNumber).setParentHash(parentHash); currentBlock = blockGenerator.block(options); filterManager.recordBlockEvent( BlockAddedEvent.createForHeadAdvancement(currentBlock), blockchainQueries.getBlockchain()); return currentBlock.getHash(); }
@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); }