Search in sources :

Example 1 with KeyValueStorage

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();
}
Also used : InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) KeyValueStorage(org.hyperledger.besu.plugin.services.storage.KeyValueStorage) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) Block(org.hyperledger.besu.ethereum.core.Block) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) Test(org.junit.Test)

Example 2 with KeyValueStorage

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[] {}));
}
Also used : BlockOptions(org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions) BlockOptions(org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) KeyValueStorage(org.hyperledger.besu.plugin.services.storage.KeyValueStorage) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) ArrayList(java.util.ArrayList) Hash(org.hyperledger.besu.datatypes.Hash) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) Transaction(org.hyperledger.besu.ethereum.core.Transaction) LogWithMetadata(org.hyperledger.besu.ethereum.core.LogWithMetadata) Block(org.hyperledger.besu.ethereum.core.Block) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 3 with KeyValueStorage

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);
}
Also used : InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) KeyValueStorage(org.hyperledger.besu.plugin.services.storage.KeyValueStorage) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) ArrayList(java.util.ArrayList) TransactionReceipt(org.hyperledger.besu.ethereum.core.TransactionReceipt) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) Block(org.hyperledger.besu.ethereum.core.Block) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 4 with KeyValueStorage

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();
}
Also used : InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) KeyValueStorage(org.hyperledger.besu.plugin.services.storage.KeyValueStorage) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) Block(org.hyperledger.besu.ethereum.core.Block) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) Test(org.junit.Test)

Example 5 with KeyValueStorage

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");
}
Also used : InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) KeyValueStorage(org.hyperledger.besu.plugin.services.storage.KeyValueStorage) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) Test(org.junit.Test)

Aggregations

KeyValueStorage (org.hyperledger.besu.plugin.services.storage.KeyValueStorage)57 Test (org.junit.Test)54 InMemoryKeyValueStorage (org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage)29 KeyValueStorageTransaction (org.hyperledger.besu.plugin.services.storage.KeyValueStorageTransaction)26 Block (org.hyperledger.besu.ethereum.core.Block)24 BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)23 List (java.util.List)14 ArrayList (java.util.ArrayList)13 BlockOptions (org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions)10 TransactionReceipt (org.hyperledger.besu.ethereum.core.TransactionReceipt)10 Optional (java.util.Optional)9 Hash (org.hyperledger.besu.datatypes.Hash)9 Bytes (org.apache.tuweni.bytes.Bytes)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Transaction (org.hyperledger.besu.ethereum.core.Transaction)8 LogWithMetadata (org.hyperledger.besu.ethereum.core.LogWithMetadata)6 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 Arrays (java.util.Arrays)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4