Search in sources :

Example 1 with BlockBody

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

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

the class PkiQbftBlockCreatorTest method createBlockBeingProposed.

private Block createBlockBeingProposed() {
    final BftExtraData originalExtraData = createExtraData(blockHeaderBuilder.buildHeader(), extraDataCodec);
    final BlockHeader blockHeaderWithExtraData = blockHeaderBuilder.extraData(extraDataCodec.encode(originalExtraData)).buildHeader();
    final Block block = new Block(blockHeaderWithExtraData, new BlockBody(Collections.emptyList(), Collections.emptyList()));
    when(blockCreator.createBlock(eq(1L))).thenReturn(block);
    return block;
}
Also used : BlockBody(org.hyperledger.besu.ethereum.core.BlockBody) Block(org.hyperledger.besu.ethereum.core.Block) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) BftExtraData(org.hyperledger.besu.consensus.common.bft.BftExtraData)

Example 3 with BlockBody

use of org.hyperledger.besu.ethereum.core.BlockBody 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 4 with BlockBody

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

Example 5 with BlockBody

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

the class BaseFeeBlockBodyValidatorTest method BlockBodyValidatorFail_MaxFeePerGas.

@Test
public void BlockBodyValidatorFail_MaxFeePerGas() {
    when(blockHeader.getBaseFee()).thenReturn(Optional.of(Wei.of(10L)));
    when(block.getBody()).thenReturn(new BlockBody(List.of(// underpriced eip1559 transaction
    new TransactionTestFixture().maxFeePerGas(Optional.of(Wei.of(1L))).maxPriorityFeePerGas(Optional.of(Wei.of(10L))).type(TransactionType.EIP1559).createTransaction(keyPair)), Collections.emptyList()));
    assertThat(blockBodyValidator.validateTransactionGasPrice(block)).isFalse();
}
Also used : TransactionTestFixture(org.hyperledger.besu.ethereum.core.TransactionTestFixture) BlockBody(org.hyperledger.besu.ethereum.core.BlockBody) Test(org.junit.Test)

Aggregations

BlockBody (org.hyperledger.besu.ethereum.core.BlockBody)57 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)42 Block (org.hyperledger.besu.ethereum.core.Block)41 Test (org.junit.Test)18 BlockHeaderTestFixture (org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture)16 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)14 Transaction (org.hyperledger.besu.ethereum.core.Transaction)13 TransactionReceipt (org.hyperledger.besu.ethereum.core.TransactionReceipt)10 ArrayList (java.util.ArrayList)9 BftExtraData (org.hyperledger.besu.consensus.common.bft.BftExtraData)9 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)9 BigInteger (java.math.BigInteger)8 Optional (java.util.Optional)8 Before (org.junit.Before)8 Mockito.mock (org.mockito.Mockito.mock)8 Mockito.when (org.mockito.Mockito.when)8 Wei (org.hyperledger.besu.datatypes.Wei)7 Collections (java.util.Collections)6 List (java.util.List)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6