Search in sources :

Example 1 with TransactionReceipt

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

the class RestoreState method restoreBlocks.

private void restoreBlocks() throws IOException {
    try (final RollingFileReader headerReader = new RollingFileReader(this::headerFileName, compressed);
        final RollingFileReader bodyReader = new RollingFileReader(this::bodyFileName, compressed);
        final RollingFileReader receiptReader = new RollingFileReader(this::receiptFileName, compressed)) {
        final MutableBlockchain blockchain = besuController.getProtocolContext().getBlockchain();
        // target block is "including" the target block, so LE test not LT.
        for (long i = 0; i <= targetBlock; i++) {
            if (i % 100000 == 0) {
                LOG.info("Loading chain data {} / {}", i, targetBlock);
            }
            final byte[] headerEntry = headerReader.readBytes();
            final byte[] bodyEntry = bodyReader.readBytes();
            final byte[] receiptEntry = receiptReader.readBytes();
            final BlockHeaderFunctions functions = new MainnetBlockHeaderFunctions();
            final BlockHeader header = BlockHeader.readFrom(new BytesValueRLPInput(Bytes.wrap(headerEntry), false, true), functions);
            final BlockBody body = BlockBody.readFrom(new BytesValueRLPInput(Bytes.wrap(bodyEntry), false, true), functions);
            final RLPInput receiptsRlp = new BytesValueRLPInput(Bytes.wrap(receiptEntry), false, true);
            final int receiptsCount = receiptsRlp.enterList();
            final List<TransactionReceipt> receipts = new ArrayList<>(receiptsCount);
            for (int j = 0; j < receiptsCount; j++) {
                receipts.add(TransactionReceipt.readFrom(receiptsRlp, true));
            }
            receiptsRlp.leaveList();
            blockchain.appendBlock(new Block(header, body), receipts);
        }
    }
    LOG.info("Chain data loaded");
}
Also used : RollingFileReader(org.hyperledger.besu.util.io.RollingFileReader) RLPInput(org.hyperledger.besu.ethereum.rlp.RLPInput) BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions) BlockHeaderFunctions(org.hyperledger.besu.ethereum.core.BlockHeaderFunctions) BlockBody(org.hyperledger.besu.ethereum.core.BlockBody) TransactionReceipt(org.hyperledger.besu.ethereum.core.TransactionReceipt) ArrayList(java.util.ArrayList) Block(org.hyperledger.besu.ethereum.core.Block) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions)

Example 2 with TransactionReceipt

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

the class BlockchainQueries method transactionReceiptByTransactionHash.

/**
 * Returns the transaction receipt associated with the given transaction hash.
 *
 * @param transactionHash The hash of the transaction that corresponds to the receipt to retrieve.
 * @return The transaction receipt associated with the referenced transaction.
 */
public Optional<TransactionReceiptWithMetadata> transactionReceiptByTransactionHash(final Hash transactionHash) {
    final Optional<TransactionLocation> maybeLocation = blockchain.getTransactionLocation(transactionHash);
    if (maybeLocation.isEmpty()) {
        return Optional.empty();
    }
    // getTransactionLocation should not return if the TX or block doesn't exist, so throwing
    // on a missing optional is appropriate.
    final TransactionLocation location = maybeLocation.get();
    final Block block = blockchain.getBlockByHash(location.getBlockHash()).orElseThrow();
    final Transaction transaction = block.getBody().getTransactions().get(location.getTransactionIndex());
    final Hash blockhash = location.getBlockHash();
    final BlockHeader header = block.getHeader();
    final List<TransactionReceipt> transactionReceipts = blockchain.getTxReceipts(blockhash).orElseThrow();
    final TransactionReceipt transactionReceipt = transactionReceipts.get(location.getTransactionIndex());
    long gasUsed = transactionReceipt.getCumulativeGasUsed();
    if (location.getTransactionIndex() > 0) {
        gasUsed = gasUsed - transactionReceipts.get(location.getTransactionIndex() - 1).getCumulativeGasUsed();
    }
    return Optional.of(TransactionReceiptWithMetadata.create(transactionReceipt, transaction, transactionHash, location.getTransactionIndex(), gasUsed, header.getBaseFee(), blockhash, header.getNumber()));
}
Also used : Transaction(org.hyperledger.besu.ethereum.core.Transaction) TransactionLocation(org.hyperledger.besu.ethereum.chain.TransactionLocation) TransactionReceipt(org.hyperledger.besu.ethereum.core.TransactionReceipt) Block(org.hyperledger.besu.ethereum.core.Block) Hash(org.hyperledger.besu.datatypes.Hash) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 3 with TransactionReceipt

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

the class StateBackupService method backupChainData.

private void backupChainData() throws IOException {
    try (final RollingFileWriter headerWriter = new RollingFileWriter(this::headerFileName, backupStatus.compressed);
        final RollingFileWriter bodyWriter = new RollingFileWriter(this::bodyFileName, backupStatus.compressed);
        final RollingFileWriter receiptsWriter = new RollingFileWriter(this::receiptFileName, backupStatus.compressed)) {
        for (long blockNumber = 0; blockNumber <= backupStatus.targetBlock; blockNumber++) {
            final Optional<Block> block = blockchain.getBlockByNumber(blockNumber);
            checkState(block.isPresent(), "Block data for %s was not found in the archive", blockNumber);
            final Optional<List<TransactionReceipt>> receipts = blockchain.getTxReceipts(block.get().getHash());
            checkState(receipts.isPresent(), "Receipts for %s was not found in the archive", blockNumber);
            final BytesValueRLPOutput headerOutput = new BytesValueRLPOutput();
            block.get().getHeader().writeTo(headerOutput);
            headerWriter.writeBytes(headerOutput.encoded().toArrayUnsafe());
            final BytesValueRLPOutput bodyOutput = new BytesValueRLPOutput();
            block.get().getBody().writeTo(bodyOutput);
            bodyWriter.writeBytes(bodyOutput.encoded().toArrayUnsafe());
            final BytesValueRLPOutput receiptsOutput = new BytesValueRLPOutput();
            receiptsOutput.writeList(receipts.get(), TransactionReceipt::writeToWithRevertReason);
            receiptsWriter.writeBytes(receiptsOutput.encoded().toArrayUnsafe());
            backupStatus.storedBlock = blockNumber;
        }
    }
}
Also used : TransactionReceipt(org.hyperledger.besu.ethereum.core.TransactionReceipt) Block(org.hyperledger.besu.ethereum.core.Block) List(java.util.List) RollingFileWriter(org.hyperledger.besu.util.io.RollingFileWriter) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)

Example 4 with TransactionReceipt

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

the class PrivateStateRootResolverTest method setupClass.

@BeforeClass
public static void setupClass() {
    BLOCKCHAIN = InMemoryKeyValueStorageProvider.createInMemoryBlockchain(BLOCK_GENERATOR.genesisBlock());
    for (int i = 1; i <= 69; i++) {
        final BlockDataGenerator.BlockOptions options = new BlockDataGenerator.BlockOptions().setBlockNumber(i).setParentHash(BLOCKCHAIN.getBlockHashByNumber(i - 1).get());
        final Block block = BLOCK_GENERATOR.block(options);
        final List<TransactionReceipt> receipts = BLOCK_GENERATOR.receipts(block);
        BLOCKCHAIN.appendBlock(block, receipts);
    }
}
Also used : TransactionReceipt(org.hyperledger.besu.ethereum.core.TransactionReceipt) Block(org.hyperledger.besu.ethereum.core.Block) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) BeforeClass(org.junit.BeforeClass)

Example 5 with TransactionReceipt

use of org.hyperledger.besu.ethereum.core.TransactionReceipt 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)

Aggregations

TransactionReceipt (org.hyperledger.besu.ethereum.core.TransactionReceipt)62 Block (org.hyperledger.besu.ethereum.core.Block)49 BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)26 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)23 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)22 List (java.util.List)20 Transaction (org.hyperledger.besu.ethereum.core.Transaction)19 Hash (org.hyperledger.besu.datatypes.Hash)14 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)14 InMemoryKeyValueStorage (org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage)12 Optional (java.util.Optional)11 BlockBody (org.hyperledger.besu.ethereum.core.BlockBody)11 BlockOptions (org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions)11 Collections (java.util.Collections)10 Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)10 KeyValueStorage (org.hyperledger.besu.plugin.services.storage.KeyValueStorage)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 Wei (org.hyperledger.besu.datatypes.Wei)9 EthContext (org.hyperledger.besu.ethereum.eth.manager.EthContext)9