Search in sources :

Example 1 with RLPInput

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

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

the class QbftPayloadTest method readingConsensusRoundExpectsScalarValues.

@Test
public void readingConsensusRoundExpectsScalarValues() {
    final long sequence = 100;
    final int round = 8;
    final BytesValueRLPOutput out = new BytesValueRLPOutput();
    out.startList();
    out.writeLongScalar(sequence);
    out.writeIntScalar(round);
    out.endList();
    final RLPInput rlpIn = RLP.input(out.encoded());
    rlpIn.enterList();
    final ConsensusRoundIdentifier roundIdentifier = QbftPayload.readConsensusRound(rlpIn);
    rlpIn.leaveList();
    assertThat(roundIdentifier.getSequenceNumber()).isEqualTo(sequence);
    assertThat(roundIdentifier.getRoundNumber()).isEqualTo(round);
}
Also used : ConsensusRoundIdentifier(org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier) RLPInput(org.hyperledger.besu.ethereum.rlp.RLPInput) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput) Test(org.junit.Test)

Example 3 with RLPInput

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

the class RoundChange method decode.

public static RoundChange decode(final Bytes data, final BftExtraDataCodec bftExtraDataCodec) {
    final RLPInput rlpIn = RLP.input(data);
    rlpIn.enterList();
    final SignedData<RoundChangePayload> payload = readPayload(rlpIn, RoundChangePayload::readFrom);
    final Optional<Block> block;
    if (rlpIn.nextIsList() && rlpIn.nextSize() == 0) {
        rlpIn.skipNext();
        block = Optional.empty();
    } else {
        block = Optional.of(Block.readFrom(rlpIn, BftBlockHeaderFunctions.forCommittedSeal(bftExtraDataCodec)));
    }
    final List<SignedData<PreparePayload>> prepares = rlpIn.readList(r -> readPayload(r, PreparePayload::readFrom));
    rlpIn.leaveList();
    return new RoundChange(payload, block, prepares);
}
Also used : RLPInput(org.hyperledger.besu.ethereum.rlp.RLPInput) SignedData(org.hyperledger.besu.consensus.common.bft.payload.SignedData) RoundChangePayload(org.hyperledger.besu.consensus.qbft.payload.RoundChangePayload) Block(org.hyperledger.besu.ethereum.core.Block)

Example 4 with RLPInput

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

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

Aggregations

RLPInput (org.hyperledger.besu.ethereum.rlp.RLPInput)68 BytesValueRLPInput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPInput)34 Bytes (org.apache.tuweni.bytes.Bytes)24 Test (org.junit.Test)21 BytesValueRLPOutput (org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)15 Hash (org.hyperledger.besu.datatypes.Hash)12 ArrayList (java.util.ArrayList)11 SECPSignature (org.hyperledger.besu.crypto.SECPSignature)11 Block (org.hyperledger.besu.ethereum.core.Block)9 BigInteger (java.math.BigInteger)8 Address (org.hyperledger.besu.datatypes.Address)8 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)7 Transaction (org.hyperledger.besu.ethereum.core.Transaction)7 ArrayDeque (kotlin.collections.ArrayDeque)6 Bytes32 (org.apache.tuweni.bytes.Bytes32)6 ByteBuffer (java.nio.ByteBuffer)5 Optional (java.util.Optional)5 Wei (org.hyperledger.besu.datatypes.Wei)5 MainnetBlockHeaderFunctions (org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions)5 MessageData (org.hyperledger.besu.ethereum.p2p.rlpx.wire.MessageData)5