Search in sources :

Example 1 with BytesValueRLPInput

use of org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput 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 BytesValueRLPInput

use of org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput in project besu by hyperledger.

the class PkiQbftExtraDataCodec method encode.

private Bytes encode(final BftExtraData bftExtraData, final EncodingType encodingType, final boolean includeCms) {
    final Bytes encoded = super.encode(bftExtraData, encodingType);
    if (!(bftExtraData instanceof PkiQbftExtraData) || !includeCms) {
        return encoded;
    }
    final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput();
    rlpOutput.startList();
    // Read through extraData RLP list elements and write them to the new RLP output
    new BytesValueRLPInput(encoded, false).readList(RLPInput::readAsRlp).stream().map(RLPInput::raw).forEach(rlpOutput::writeRLPBytes);
    rlpOutput.writeBytes(((PkiQbftExtraData) bftExtraData).getCms());
    rlpOutput.endList();
    return rlpOutput.encoded();
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput) BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput)

Example 3 with BytesValueRLPInput

use of org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput in project besu by hyperledger.

the class StateBackupService method visitAccount.

private TrieIterator.State visitAccount(final Bytes32 nodeKey, final Node<Bytes> node) {
    if (node.getValue().isEmpty()) {
        return State.CONTINUE;
    }
    backupStatus.currentAccount = nodeKey;
    final Bytes nodeValue = node.getValue().orElse(Hash.EMPTY);
    final StateTrieAccountValue account = StateTrieAccountValue.readFrom(new BytesValueRLPInput(nodeValue, false));
    final Bytes code = worldStateStorage.getCode(account.getCodeHash(), null).orElse(Bytes.EMPTY);
    backupStatus.codeSize.addAndGet(code.size());
    final BytesValueRLPOutput accountOutput = new BytesValueRLPOutput();
    accountOutput.startList();
    // trie hash
    accountOutput.writeBytes(nodeKey);
    // account rlp
    accountOutput.writeBytes(nodeValue);
    // code
    accountOutput.writeBytes(code);
    accountOutput.endList();
    try {
        accountFileWriter.writeBytes(accountOutput.encoded().toArrayUnsafe());
    } catch (final IOException ioe) {
        LOG.error("Failure writing backup", ioe);
        return State.STOP;
    }
    // storage is written for each leaf, otherwise the whole trie would have to fit in memory
    final StoredMerklePatriciaTrie<Bytes32, Bytes> storageTrie = new StoredMerklePatriciaTrie<>(worldStateStorage::getAccountStateTrieNode, account.getStorageRoot(), Function.identity(), Function.identity());
    storageTrie.visitLeafs((storageKey, storageValue) -> visitAccountStorage(storageKey, storageValue, accountFileWriter));
    try {
        accountFileWriter.writeBytes(ACCOUNT_END_MARKER.toArrayUnsafe());
    } catch (final IOException ioe) {
        LOG.error("Failure writing backup", ioe);
        return State.STOP;
    }
    backupStatus.accountCount.incrementAndGet();
    return State.CONTINUE;
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) StoredMerklePatriciaTrie(org.hyperledger.besu.ethereum.trie.StoredMerklePatriciaTrie) StateTrieAccountValue(org.hyperledger.besu.ethereum.worldstate.StateTrieAccountValue) IOException(java.io.IOException) BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput) Bytes32(org.apache.tuweni.bytes.Bytes32)

Example 4 with BytesValueRLPInput

use of org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput in project besu by hyperledger.

the class PrivateTransactionTest method testReadFromInvalid.

@Test
public void testReadFromInvalid() {
    final BytesValueRLPInput input = new BytesValueRLPInput(Bytes.fromHexString(INVALID_RLP), false);
    assertThatThrownBy(() -> PrivateTransaction.readFrom(input)).isInstanceOf(RLPException.class);
}
Also used : BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) Test(org.junit.Test)

Example 5 with BytesValueRLPInput

use of org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput in project besu by hyperledger.

the class PrivateTransactionTest method testReadFromWithLargeChainId.

@Test
public void testReadFromWithLargeChainId() {
    final PrivateTransaction p = PrivateTransaction.readFrom(new BytesValueRLPInput(Bytes.fromHexString(VALID_SIGNED_PRIVATE_TRANSACTION_LARGE_CHAINID_RLP), false));
    assertThat(p).isEqualTo(VALID_SIGNED_PRIVATE_TRANSACTION_LARGE_CHAINID);
}
Also used : BytesValueRLPInput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput) Test(org.junit.Test)

Aggregations

BytesValueRLPInput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput)55 RLPInput (org.hyperledger.besu.ethereum.rlp.RLPInput)33 Bytes (org.apache.tuweni.bytes.Bytes)22 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)12 Hash (org.hyperledger.besu.datatypes.Hash)9 Bytes32 (org.apache.tuweni.bytes.Bytes32)8 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)8 MessageData (org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData)7 ArrayDeque (kotlin.collections.ArrayDeque)6 BytesValueRLPOutput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)6 ByteBuffer (java.nio.ByteBuffer)5 BlockBody (org.hyperledger.besu.ethereum.core.BlockBody)5 Transaction (org.hyperledger.besu.ethereum.core.Transaction)5 MainnetBlockHeaderFunctions (org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions)5 List (java.util.List)4 SECPSignature (org.hyperledger.besu.crypto.SECPSignature)4 Block (org.hyperledger.besu.ethereum.core.Block)4 BftExtraData (org.hyperledger.besu.consensus.common.bft.BftExtraData)3 Address (org.hyperledger.besu.datatypes.Address)3