use of org.hyperledger.besu.plugin.services.storage.KeyValueStorage in project besu by hyperledger.
the class DefaultBlockchainTest method initializeExisting.
@Test
public void initializeExisting() {
final BlockDataGenerator gen = new BlockDataGenerator();
// Write to kv store
final KeyValueStorage kvStore = new InMemoryKeyValueStorage();
final Block genesisBlock = gen.genesisBlock();
createMutableBlockchain(kvStore, genesisBlock);
// Initialize a new blockchain store with kvStore that already contains data
final DefaultBlockchain blockchain = createMutableBlockchain(kvStore, genesisBlock);
assertBlockDataIsStored(blockchain, genesisBlock, Collections.emptyList());
assertBlockIsHead(blockchain, genesisBlock);
assertTotalDifficultiesAreConsistent(blockchain, genesisBlock);
assertThat(blockchain.getForks()).isEmpty();
}
use of org.hyperledger.besu.plugin.services.storage.KeyValueStorage in project besu by hyperledger.
the class DefaultBlockchainTest method appendBlockWithReorgToLongerChain.
@Test
public void appendBlockWithReorgToLongerChain() {
final BlockDataGenerator gen = new BlockDataGenerator(2);
// Setup an initial blockchain
final int originalChainLength = 4;
final List<Block> chain = gen.blockSequence(originalChainLength);
final List<List<TransactionReceipt>> blockReceipts = chain.stream().map(gen::receipts).collect(Collectors.toList());
final KeyValueStorage kvStore = new InMemoryKeyValueStorage();
final DefaultBlockchain blockchain = createMutableBlockchain(kvStore, chain.get(0));
// Listen to block events and add the Logs here
List<LogWithMetadata> logsWithMetadata = new ArrayList<>();
blockchain.observeBlockAdded(event -> logsWithMetadata.addAll(event.getLogsWithMetadata()));
List<LogWithMetadata> expectedLogsWithMetadata = new ArrayList<>();
for (int i = 1; i < chain.size(); i++) {
blockchain.appendBlock(chain.get(i), blockReceipts.get(i));
expectedLogsWithMetadata.addAll(LogWithMetadata.generate(chain.get(i), blockReceipts.get(i), false));
}
final Block originalHead = chain.get(originalChainLength - 1);
// Create parallel fork of length 2 from 3 blocks back
final List<Block> forkBlocks = new ArrayList<>();
final int forkStart = 3;
final int commonAncestor = 2;
// Generate first block
BlockDataGenerator.BlockOptions options = new BlockDataGenerator.BlockOptions().setParentHash(chain.get(commonAncestor).getHash()).setBlockNumber(forkStart).setDifficulty(chain.get(forkStart).getHeader().getDifficulty().subtract(5L));
forkBlocks.add(gen.block(options));
// Generate second block
options = new BlockDataGenerator.BlockOptions().setParentHash(forkBlocks.get(0).getHash()).setBlockNumber(forkStart + 1).setDifficulty(Difficulty.of(10L));
forkBlocks.add(gen.block(options));
// Generate corresponding receipts
final List<List<TransactionReceipt>> forkReceipts = forkBlocks.stream().map(gen::receipts).collect(Collectors.toList());
// Collect fork data
final List<Block> reorgedChain = new ArrayList<>(chain.subList(0, forkStart));
reorgedChain.addAll(forkBlocks);
final List<List<TransactionReceipt>> reorgedReceipts = new ArrayList<>(blockReceipts.subList(0, forkStart));
reorgedReceipts.addAll(forkReceipts);
// Add first block in fork, which should not cause a reorg
blockchain.appendBlock(forkBlocks.get(0), forkReceipts.get(0));
// Check chain has not reorganized
for (int i = 0; i < chain.size(); i++) {
assertBlockDataIsStored(blockchain, chain.get(i), blockReceipts.get(i));
}
assertBlockIsHead(blockchain, originalHead);
assertTotalDifficultiesAreConsistent(blockchain, originalHead);
// Check transactions were not indexed
for (final Transaction tx : forkBlocks.get(0).getBody().getTransactions()) {
assertThat(blockchain.getTransactionByHash(tx.getHash())).isNotPresent();
}
// Appended block should be tracked as a fork
assertThat(blockchain.blockIsOnCanonicalChain(forkBlocks.get(0).getHash())).isFalse();
Set<Hash> forks = blockchain.getForks();
assertThat(forks.size()).isEqualTo(1);
assertThat(forks.stream().anyMatch(f -> f.equals(forkBlocks.get(0).getHash()))).isTrue();
// Add second block in fork, which should cause a reorg
blockchain.appendBlock(forkBlocks.get(1), forkReceipts.get(1));
// Check chain has reorganized
for (int i = 0; i < reorgedChain.size(); i++) {
assertBlockDataIsStored(blockchain, reorgedChain.get(i), reorgedReceipts.get(i));
}
assertBlockIsHead(blockchain, forkBlocks.get(1));
assertTotalDifficultiesAreConsistent(blockchain, forkBlocks.get(1));
// Check old transactions have been removed
final List<Transaction> removedTransactions = new ArrayList<>();
for (int i = forkStart; i < originalChainLength; i++) {
removedTransactions.addAll(chain.get(i).getBody().getTransactions());
}
for (final Transaction tx : removedTransactions) {
assertThat(blockchain.getTransactionByHash(tx.getHash())).isNotPresent();
}
// LogWithMetadata reflecting removal of logs
for (int i = originalChainLength - 1; i >= forkStart; i--) {
final Block currentBlock = chain.get(i);
expectedLogsWithMetadata.addAll(Lists.reverse(LogWithMetadata.generate(currentBlock, blockchain.getTxReceipts(currentBlock.getHash()).get(), true)));
}
// LogWithMetadata reflecting addition of logs
for (int i = 0; i < forkBlocks.size(); i++) {
expectedLogsWithMetadata.addAll(LogWithMetadata.generate(forkBlocks.get(i), forkReceipts.get(i), false));
}
// Old chain head should now be tracked as a fork.
forks = blockchain.getForks();
assertThat(forks.size()).isEqualTo(1);
assertThat(forks.stream().anyMatch(f -> f.equals(originalHead.getHash()))).isTrue();
// Old chain should not be on canonical chain.
for (int i = commonAncestor + 1; i < originalChainLength; i++) {
assertThat(blockchain.blockIsOnCanonicalChain(chain.get(i).getHash())).isFalse();
}
assertThat(logsWithMetadata).containsExactly(expectedLogsWithMetadata.toArray(new LogWithMetadata[] {}));
}
use of org.hyperledger.besu.plugin.services.storage.KeyValueStorage in project besu by hyperledger.
the class DefaultBlockchainTest method initializeReadOnly_withSmallChain.
@Test
public void initializeReadOnly_withSmallChain() {
final BlockDataGenerator gen = new BlockDataGenerator();
final KeyValueStorage kvStore = new InMemoryKeyValueStorage();
final List<Block> blocks = gen.blockSequence(10);
final List<List<TransactionReceipt>> blockReceipts = new ArrayList<>(blocks.size());
blockReceipts.add(Collections.emptyList());
// Write small chain to storage
final MutableBlockchain mutableBlockchain = createMutableBlockchain(kvStore, blocks.get(0));
for (int i = 1; i < blocks.size(); i++) {
final Block block = blocks.get(i);
final List<TransactionReceipt> receipts = gen.receipts(block);
blockReceipts.add(receipts);
mutableBlockchain.appendBlock(block, receipts);
}
// Create read only chain
final Blockchain blockchain = createBlockchain(kvStore);
for (int i = 0; i < blocks.size(); i++) {
assertBlockDataIsStored(blockchain, blocks.get(i), blockReceipts.get(i));
}
final Block lastBlock = blocks.get(blocks.size() - 1);
assertBlockIsHead(blockchain, lastBlock);
assertTotalDifficultiesAreConsistent(blockchain, lastBlock);
}
use of org.hyperledger.besu.plugin.services.storage.KeyValueStorage in project besu by hyperledger.
the class DefaultBlockchainTest method blockAddedObserver_removeNonexistentObserver.
@Test
public void blockAddedObserver_removeNonexistentObserver() {
final BlockDataGenerator gen = new BlockDataGenerator();
final KeyValueStorage kvStore = new InMemoryKeyValueStorage();
final Block genesisBlock = gen.genesisBlock();
final DefaultBlockchain blockchain = createMutableBlockchain(kvStore, genesisBlock);
assertThat(blockchain.removeObserver(7)).isFalse();
}
use of org.hyperledger.besu.plugin.services.storage.KeyValueStorage in project besu by hyperledger.
the class DefaultBlockchainTest method initializeReadOnly_emptyStorage.
@Test
public void initializeReadOnly_emptyStorage() {
final KeyValueStorage kvStore = new InMemoryKeyValueStorage();
assertThatThrownBy(() -> createBlockchain(kvStore)).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("Cannot create Blockchain from empty storage");
}
Aggregations