Search in sources :

Example 1 with RollingFileWriter

use of org.hyperledger.besu.util.io.RollingFileWriter 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 2 with RollingFileWriter

use of org.hyperledger.besu.util.io.RollingFileWriter in project besu by hyperledger.

the class StateBackupService method backupLeaves.

private void backupLeaves() throws IOException {
    final Optional<BlockHeader> header = blockchain.getBlockHeader(backupStatus.targetBlock);
    if (header.isEmpty()) {
        backupStatus.currentAccount = null;
        return;
    }
    final Optional<Bytes> worldStateRoot = worldStateStorage.getAccountStateTrieNode(Bytes.EMPTY, header.get().getStateRoot());
    if (worldStateRoot.isEmpty()) {
        backupStatus.currentAccount = null;
        return;
    }
    try (final RollingFileWriter accountFileWriter = new RollingFileWriter(this::accountFileName, backupStatus.compressed)) {
        this.accountFileWriter = accountFileWriter;
        final StoredMerklePatriciaTrie<Bytes32, Bytes> accountTrie = new StoredMerklePatriciaTrie<>(worldStateStorage::getAccountStateTrieNode, header.get().getStateRoot(), Function.identity(), Function.identity());
        accountTrie.visitLeafs(this::visitAccount);
        backupStatus.currentAccount = null;
    }
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) StoredMerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) RollingFileWriter(org.hyperledger.besu.util.io.RollingFileWriter) Bytes32(org.apache.tuweni.bytes.Bytes32)

Aggregations

RollingFileWriter (org.hyperledger.besu.util.io.RollingFileWriter)2 List (java.util.List)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 Block (org.hyperledger.besu.ethereum.core.Block)1 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)1 TransactionReceipt (org.hyperledger.besu.ethereum.core.TransactionReceipt)1 BytesValueRLPOutput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)1 StoredMerklePatriciaTrie (org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie)1