@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(); }
.setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkStart) .setDifficulty(chain.get(forkStart).getHeader().getDifficulty().minus(5L)); .setParentHash(forkBlocks.get(0).getHash()) .setBlockNumber(forkStart + 1) .setDifficulty(UInt256.of(10L));
final BlockOptions options = new BlockOptions() .setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkBlock) .setDifficulty(chain.get(forkBlock).getHeader().getDifficulty().plus(10L));
.setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkStart) .setDifficulty(chain.get(forkStart).getHeader().getDifficulty().minus(5L)); .setParentHash(forkBlocks.get(0).getHash()) .setBlockNumber(forkStart + 1) .setDifficulty(chain.get(forkStart + 1).getHeader().getDifficulty().minus(5L)); .setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkStart) .setDifficulty(chain.get(forkStart).getHeader().getDifficulty().minus(5L));
new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); commonBlock = blockDataGenerator.block(options); final List<TransactionReceipt> receipts = blockDataGenerator.receipts(commonBlock); new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block block00 = blockDataGenerator.block(options00); final List<TransactionReceipt> receipts00 = blockDataGenerator.receipts(block00); .setDifficulty(UInt256.ONE) .setBlockNumber(i) .setParentHash(remoteBlockchain.getBlockHashByNumber(i - 1).get()); final Block block01 = blockDataGenerator.block(options01); final List<TransactionReceipt> receipts01 = blockDataGenerator.receipts(block01);
new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block localBlock = blockDataGenerator.block(localOptions); final List<TransactionReceipt> localReceipts = blockDataGenerator.receipts(localBlock); .setParentHash(remoteBlockchain.getBlockHashByNumber(i - 1).get()); final Block remoteBlock = blockDataGenerator.block(remoteOptions); final List<TransactionReceipt> remoteReceipts = blockDataGenerator.receipts(remoteBlock);
new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block block00 = blockDataGenerator.block(options00); final List<TransactionReceipt> receipts00 = blockDataGenerator.receipts(block00); .setDifficulty(UInt256.ONE) .setBlockNumber(i) .setParentHash(remoteBlockchain.getBlockHashByNumber(i - 1).get()); final Block block01 = blockDataGenerator.block(options01); final List<TransactionReceipt> receipts01 = blockDataGenerator.receipts(block01);
new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block block00 = blockDataGenerator.block(options00); final List<TransactionReceipt> receipts00 = blockDataGenerator.receipts(block00); .setDifficulty(UInt256.ONE) .setBlockNumber(i) .setParentHash(remoteBlockchain.getBlockHashByNumber(i - 1).get()); final Block block01 = blockDataGenerator.block(options01); final List<TransactionReceipt> receipts01 = blockDataGenerator.receipts(block01);
new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block block00 = blockDataGenerator.block(options00); final List<TransactionReceipt> receipts00 = blockDataGenerator.receipts(block00); .setDifficulty(UInt256.ONE) .setBlockNumber(i) .setParentHash(remoteBlockchain.getBlockHashByNumber(i - 1).get()); final Block block01 = blockDataGenerator.block(options01); final List<TransactionReceipt> receipts01 = blockDataGenerator.receipts(block01);
new BlockDataGenerator.BlockOptions() .setBlockNumber(1L) .setParentHash(blockchain.getBlockHashByNumber(0L).get()); final Block block = gen.block(options); final List<TransactionReceipt> receipts = gen.receipts(block);
final BlockOptions options = new BlockOptions() .setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkBlock) .setDifficulty(chain.get(forkBlock).getHeader().getDifficulty().plus(10L))
new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block localBlock = blockDataGenerator.block(localOptions); final List<TransactionReceipt> localReceipts = blockDataGenerator.receipts(localBlock); .setParentHash(remoteBlockchain.getBlockHashByNumber(i - 1).get()); final Block remoteBlock = blockDataGenerator.block(remoteOptions); final List<TransactionReceipt> remoteReceipts = blockDataGenerator.receipts(remoteBlock);
@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); }