new BlockOptions() .setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkStart) .setDifficulty(chain.get(forkStart).getHeader().getDifficulty().minus(5L)); forkBlocks.add(gen.block(options)); .plus(chain.get(forkStart + 2).getHeader().getDifficulty()); options = new BlockOptions() .setParentHash(forkBlocks.get(0).getHash()) .setBlockNumber(forkStart + 1) .setDifficulty(remainingDifficultyToOutpace.plus(10L)); forkBlocks.add(gen.block(options));
final Block fakeFirstBlock = gen.block(BlockOptions.create().setBlockNumber(5L)); final Block firstBlock = getBlockAtNumber(5L); final Block secondBlock = getBlockAtNumber(6L);
final int commonAncestor = 1; final BlockOptions options = new BlockOptions() .setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkBlock) .setDifficulty(chain.get(forkBlock).getHeader().getDifficulty().plus(10L)) .addTransaction(overlappingTx) .addTransaction(gen.transaction()); final Block fork = gen.block(options); final List<TransactionReceipt> forkReceipts = gen.receipts(fork);
new BlockOptions() .setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkStart) .setDifficulty(chain.get(forkStart).getHeader().getDifficulty().minus(5L)); forkBlocks.add(gen.block(options)); new BlockOptions() .setParentHash(forkBlocks.get(0).getHash()) .setBlockNumber(forkStart + 1) .setDifficulty(chain.get(forkStart + 1).getHeader().getDifficulty().minus(5L)); forkBlocks.add(gen.block(options)); new BlockOptions() .setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkStart) .setDifficulty(chain.get(forkStart).getHeader().getDifficulty().minus(5L)); final Block secondFork = gen.block(options); blockchain.appendBlock(secondFork, gen.receipts(secondFork));
dataGen.block(BlockOptions.create().setStateRoot(stateRoot).setBlockNumber(10)).getHeader(); BlockHeader otherHeader = dataGen .block(BlockOptions.create().setStateRoot(otherStateRoot).setBlockNumber(11)) .getHeader();
new BlockOptions() .setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkStart) .setDifficulty(chain.get(forkStart).getHeader().getDifficulty().minus(5L)); forkBlocks.add(gen.block(options)); new BlockOptions() .setParentHash(forkBlocks.get(0).getHash()) .setBlockNumber(forkStart + 1) .setDifficulty(UInt256.of(10L)); forkBlocks.add(gen.block(options));
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); new BlockDataGenerator.BlockOptions() .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); for (long i = 1; i <= 9; i++) { final BlockDataGenerator.BlockOptions options01 = new BlockDataGenerator.BlockOptions() .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); new BlockDataGenerator.BlockOptions() .setDifficulty(UInt256.ONE) // differentiator .setBlockNumber(i) .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); for (long i = 1; i <= 100; i++) { final BlockDataGenerator.BlockOptions options01 = new BlockDataGenerator.BlockOptions() .setDifficulty(UInt256.ONE) .setBlockNumber(i) .setParentHash(remoteBlockchain.getBlockHashByNumber(i - 1).get()); final Block block01 = blockDataGenerator.block(options01); final List<TransactionReceipt> receipts01 = blockDataGenerator.receipts(block01);
gen.block(BlockOptions.create().setBlockNumber(blockchain.getChainHeadBlockNumber()));
new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block block00 = blockDataGenerator.block(options00); final List<TransactionReceipt> receipts00 = blockDataGenerator.receipts(block00); for (long i = 1; i <= 99; i++) { final BlockDataGenerator.BlockOptions options01 = new BlockDataGenerator.BlockOptions() .setDifficulty(UInt256.ONE) .setBlockNumber(i) .setParentHash(remoteBlockchain.getBlockHashByNumber(i - 1).get()); final Block block01 = blockDataGenerator.block(options01); final List<TransactionReceipt> receipts01 = blockDataGenerator.receipts(block01);
dataGen.block(BlockOptions.create().setStateRoot(stateRoot).setBlockNumber(10)).getHeader();
new BlockDataGenerator.BlockOptions() .setBlockNumber(1L) .setParentHash(blockchain.getBlockHashByNumber(0L).get()); final Block block = gen.block(options); final List<TransactionReceipt> receipts = gen.receipts(block);
final int commonAncestor = 1; final BlockOptions options = new BlockOptions() .setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkBlock) .setDifficulty(chain.get(forkBlock).getHeader().getDifficulty().plus(10L)); final Block fork = gen.block(options); final List<TransactionReceipt> forkReceipts = gen.receipts(fork);
new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block localBlock = blockDataGenerator.block(localOptions); final List<TransactionReceipt> localReceipts = blockDataGenerator.receipts(localBlock); new BlockDataGenerator.BlockOptions() .setDifficulty(UInt256.ONE) // differentiator .setBlockNumber(i) .setParentHash(remoteBlockchain.getBlockHashByNumber(i - 1).get()); final Block remoteBlock = blockDataGenerator.block(remoteOptions); final List<TransactionReceipt> remoteReceipts = blockDataGenerator.receipts(remoteBlock);
final Block chainHead = localBlockchain.getChainHeadBlock(); final Block forkBlock = gen.block(gen.nextBlockOptions(chainHead).setDifficulty(UInt256.of(0L))); localBlockchain.appendBlock(forkBlock, gen.receipts(forkBlock));
final BlockHeader header = dataGen .block(BlockOptions.create().setStateRoot(EMPTY_TRIE_ROOT).setBlockNumber(10)) .getHeader();
@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(); }
@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); }