@Test public void createSmallChain() { final BlockDataGenerator gen = new BlockDataGenerator(); final List<Block> chain = gen.blockSequence(3); final List<List<TransactionReceipt>> blockReceipts = chain.stream().map(gen::receipts).collect(Collectors.toList()); final KeyValueStorage kvStore = new InMemoryKeyValueStorage(); final DefaultMutableBlockchain blockchain = createBlockchain(kvStore, chain.get(0)); for (int i = 1; i < chain.size(); i++) { blockchain.appendBlock(chain.get(i), blockReceipts.get(i)); } for (int i = 1; i < chain.size(); i++) { assertBlockDataIsStored(blockchain, chain.get(i), blockReceipts.get(i)); } final Block head = chain.get(chain.size() - 1); assertBlockIsHead(blockchain, head); assertTotalDifficultiesAreConsistent(blockchain, head); assertThat(blockchain.getForks()).isEmpty(); }
@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(); }
final DefaultMutableBlockchain blockchain = createBlockchain(kvStore, chain.get(0)); for (int i = 1; i < chain.size(); i++) { blockchain.appendBlock(chain.get(i), blockReceipts.get(i)); blockchain.appendBlock(fork, forkReceipts);
final DefaultMutableBlockchain blockchain = createBlockchain(kvStore, chain.get(0)); for (int i = 1; i < chain.size(); i++) { blockchain.appendBlock(chain.get(i), blockReceipts.get(i)); blockchain.appendBlock(forkBlocks.get(0), forkReceipts.get(0)); blockchain.appendBlock(forkBlocks.get(1), forkReceipts.get(1));
@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(); }
final DefaultMutableBlockchain blockchain = createBlockchain(kvStore, chain.get(0)); for (int i = 1; i < chain.size(); i++) { blockchain.appendBlock(chain.get(i), blockReceipts.get(i)); blockchain.appendBlock(fork, forkReceipts);
final DefaultMutableBlockchain blockchain = createBlockchain(kvStore, chain.get(0)); for (int i = 1; i < chain.size(); i++) { blockchain.appendBlock(chain.get(i), blockReceipts.get(i)); blockchain.appendBlock(forkBlocks.get(0), forkReceipts.get(0)); blockchain.appendBlock(forkBlocks.get(1), forkReceipts.get(1));
final DefaultMutableBlockchain blockchain = createBlockchain(kvStore, chain.get(0)); for (int i = 1; i < chain.size(); i++) { blockchain.appendBlock(chain.get(i), blockReceipts.get(i)); blockchain.appendBlock(forkBlock, forkReceipts.get(i)); .setDifficulty(chain.get(forkStart).getHeader().getDifficulty().minus(5L)); final Block secondFork = gen.block(options); blockchain.appendBlock(secondFork, gen.receipts(secondFork));
@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); }
@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); }
@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(); }