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);
}
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);
}
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);
}
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[] {}));
}
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();
}
Aggregations