Search in sources :

Example 16 with Transaction

use of org.hyperledger.besu.ethereum.core.Transaction in project besu by hyperledger.

the class PrivateStorageMigrationTest method successfulMigrationBumpsSchemaVersion.

@Test
public void successfulMigrationBumpsSchemaVersion() {
    final Transaction privateMarkerTransaction = createPrivateMarkerTransaction();
    mockBlockchainWithPrivateMarkerTransaction(privateMarkerTransaction);
    assertThat(privateStateStorage.getSchemaVersion()).isEqualTo(SCHEMA_VERSION_1_0_0);
    migration.migratePrivateStorage();
    assertThat(privateStateStorage.getSchemaVersion()).isEqualTo(SCHEMA_VERSION_1_4_0);
}
Also used : PrivateTransactionDataFixture.privateMarkerTransaction(org.hyperledger.besu.ethereum.core.PrivateTransactionDataFixture.privateMarkerTransaction) Transaction(org.hyperledger.besu.ethereum.core.Transaction) Test(org.junit.Test)

Example 17 with Transaction

use of org.hyperledger.besu.ethereum.core.Transaction in project besu by hyperledger.

the class ValidationTestUtils method readBody.

public static BlockBody readBody(final long num) throws IOException {
    final RLPInput input = new BytesValueRLPInput(Bytes.wrap(Resources.toByteArray(EthHashTest.class.getResource(String.format("block_%d.blocks", num)))), false);
    input.enterList();
    input.skipNext();
    final List<Transaction> transactions = input.readList(Transaction::readFrom);
    final List<BlockHeader> ommers = input.readList(rlp -> BlockHeader.readFrom(rlp, new MainnetBlockHeaderFunctions()));
    return new BlockBody(transactions, ommers);
}
Also used : RLPInput(org.hyperledger.besu.ethereum.rlp.RLPInput) BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) Transaction(org.hyperledger.besu.ethereum.core.Transaction) BlockBody(org.hyperledger.besu.ethereum.core.BlockBody) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput)

Example 18 with Transaction

use of org.hyperledger.besu.ethereum.core.Transaction in project besu by hyperledger.

the class ValidationTestUtils method readBlock.

public static Block readBlock(final long num) throws IOException {
    final RLPInput input = new BytesValueRLPInput(Bytes.wrap(Resources.toByteArray(EthHashTest.class.getResource(String.format("block_%d.blocks", num)))), false);
    input.enterList();
    final BlockHeader header = BlockHeader.readFrom(input, new MainnetBlockHeaderFunctions());
    final List<Transaction> transactions = input.readList(Transaction::readFrom);
    final List<BlockHeader> ommers = input.readList(rlp -> BlockHeader.readFrom(rlp, new MainnetBlockHeaderFunctions()));
    final BlockBody body = new BlockBody(transactions, ommers);
    return new Block(header, body);
}
Also used : RLPInput(org.hyperledger.besu.ethereum.rlp.RLPInput) BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) Transaction(org.hyperledger.besu.ethereum.core.Transaction) BlockBody(org.hyperledger.besu.ethereum.core.BlockBody) Block(org.hyperledger.besu.ethereum.core.Block) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput)

Example 19 with Transaction

use of org.hyperledger.besu.ethereum.core.Transaction 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 20 with Transaction

use of org.hyperledger.besu.ethereum.core.Transaction in project besu by hyperledger.

the class DefaultBlockchainTest method rewindChain.

@Test
public void rewindChain() {
    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));
    for (int i = 1; i < chain.size(); i++) {
        blockchain.appendBlock(chain.get(i), blockReceipts.get(i));
    }
    final Block originalHead = blockchain.getChainHeadBlock();
    final Block targetHead = blockchain.getBlockByHash(originalHead.getHeader().getParentHash()).get();
    // rewind it by 1 block
    blockchain.rewindToBlock(targetHead.getHeader().getNumber());
    // Check chain has the expected blocks
    for (int i = 0; i < chain.size() - 1; i++) {
        assertBlockDataIsStored(blockchain, chain.get(i), blockReceipts.get(i));
    }
    assertBlockIsHead(blockchain, targetHead);
    // Check transactions were not indexed
    for (final Transaction tx : originalHead.getBody().getTransactions()) {
        assertThat(blockchain.getTransactionByHash(tx.getHash())).isNotPresent();
    }
    // Check that blockNumber index for previous chain head has been removed
    assertThat(blockchain.getBlockHashByNumber(originalHead.getHeader().getNumber())).isNotPresent();
    // Old chain head should not be tracked.
    assertThat(blockchain.blockIsOnCanonicalChain(originalHead.getHash())).isFalse();
}
Also used : InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) Transaction(org.hyperledger.besu.ethereum.core.Transaction) KeyValueStorage(org.hyperledger.besu.plugin.services.storage.KeyValueStorage) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) Block(org.hyperledger.besu.ethereum.core.Block) ArrayList(java.util.ArrayList) List(java.util.List) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) Test(org.junit.Test)

Aggregations

Transaction (org.hyperledger.besu.ethereum.core.Transaction)249 Test (org.junit.Test)149 ArrayList (java.util.ArrayList)57 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)45 TransactionTestFixture (org.hyperledger.besu.ethereum.core.TransactionTestFixture)45 Optional (java.util.Optional)42 Hash (org.hyperledger.besu.datatypes.Hash)38 Wei (org.hyperledger.besu.datatypes.Wei)38 Block (org.hyperledger.besu.ethereum.core.Block)35 List (java.util.List)33 Address (org.hyperledger.besu.datatypes.Address)33 Account (org.hyperledger.besu.evm.account.Account)29 TransactionInvalidReason (org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason)28 TransactionReceipt (org.hyperledger.besu.ethereum.core.TransactionReceipt)26 Bytes (org.apache.tuweni.bytes.Bytes)24 BlockBody (org.hyperledger.besu.ethereum.core.BlockBody)22 MiningParameters (org.hyperledger.besu.ethereum.core.MiningParameters)22 TransactionValidationParams (org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams)22 KeyPair (org.hyperledger.besu.crypto.KeyPair)21 EthContext (org.hyperledger.besu.ethereum.eth.manager.EthContext)20