Search in sources :

Example 1 with BlockHeaderFunctions

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

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

the class CliqueBlockCreator method createFinalBlockHeader.

/**
 * Responsible for signing (hash of) the block (including MixHash and Nonce), and then injecting
 * the seal into the extraData. This is called after a suitable set of transactions have been
 * identified, and all resulting hashes have been inserted into the passed-in SealableBlockHeader.
 *
 * @param sealableBlockHeader A block header containing StateRoots, TransactionHashes etc.
 * @return The blockhead which is to be added to the block being proposed.
 */
@Override
protected BlockHeader createFinalBlockHeader(final SealableBlockHeader sealableBlockHeader) {
    final BlockHeaderFunctions blockHeaderFunctions = ScheduleBasedBlockHeaderFunctions.create(protocolSchedule);
    final BlockHeaderBuilder builder = BlockHeaderBuilder.create().populateFrom(sealableBlockHeader).mixHash(Hash.ZERO).blockHeaderFunctions(blockHeaderFunctions);
    final Optional<ValidatorVote> vote = determineCliqueVote(sealableBlockHeader);
    final BlockHeaderBuilder builderIncludingProposedVotes = CliqueBlockInterface.createHeaderBuilderWithVoteHeaders(builder, vote);
    final CliqueExtraData sealedExtraData = constructSignedExtraData(builderIncludingProposedVotes.buildBlockHeader());
    // Replace the extraData in the BlockHeaderBuilder, and return header.
    return builderIncludingProposedVotes.extraData(sealedExtraData.encode()).buildBlockHeader();
}
Also used : ValidatorVote(org.hyperledger.besu.consensus.common.validator.ValidatorVote) BlockHeaderFunctions(org.hyperledger.besu.ethereum.core.BlockHeaderFunctions) ScheduleBasedBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.ScheduleBasedBlockHeaderFunctions) BlockHeaderBuilder(org.hyperledger.besu.ethereum.core.BlockHeaderBuilder) CliqueExtraData(org.hyperledger.besu.consensus.clique.CliqueExtraData)

Example 3 with BlockHeaderFunctions

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

the class TransactionSimulatorTest method mockProcessorStatusForTransaction.

private void mockProcessorStatusForTransaction(final long blockNumber, final Transaction transaction, final Status status) {
    final BlockHeaderFunctions blockHeaderFunctions = mock(BlockHeaderFunctions.class);
    when(protocolSchedule.getChainId()).thenReturn(Optional.of(BigInteger.ONE));
    when(protocolSchedule.getByBlockNumber(eq(blockNumber))).thenReturn(protocolSpec);
    when(protocolSpec.getTransactionProcessor()).thenReturn(transactionProcessor);
    when(protocolSpec.getMiningBeneficiaryCalculator()).thenReturn(BlockHeader::getCoinbase);
    when(protocolSpec.getBlockHeaderFunctions()).thenReturn(blockHeaderFunctions);
    final TransactionProcessingResult result = mock(TransactionProcessingResult.class);
    switch(status) {
        case SUCCESSFUL:
            when(result.isSuccessful()).thenReturn(true);
            break;
        case INVALID:
        case FAILED:
            when(result.isSuccessful()).thenReturn(false);
            break;
    }
    when(transactionProcessor.processTransaction(any(), any(), any(), eq(transaction), any(), any(), anyBoolean(), any(), any())).thenReturn(result);
}
Also used : BlockHeaderFunctions(org.hyperledger.besu.ethereum.core.BlockHeaderFunctions) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) TransactionProcessingResult(org.hyperledger.besu.ethereum.processing.TransactionProcessingResult)

Example 4 with BlockHeaderFunctions

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

the class IbftBlockCreator method createFinalBlockHeader.

/**
 * Responsible for signing (hash of) the block (including MixHash and Nonce), and then injecting
 * the seal into the extraData. This is called after a suitable set of transactions have been
 * identified, and all resulting hashes have been inserted into the passed-in SealableBlockHeader.
 *
 * @param sealableBlockHeader A block header containing StateRoots, TransactionHashes etc.
 * @return The blockhead which is to be added to the block being proposed.
 */
@Override
protected BlockHeader createFinalBlockHeader(final SealableBlockHeader sealableBlockHeader) {
    final BlockHeaderFunctions blockHeaderFunctions = ScheduleBasedBlockHeaderFunctions.create(protocolSchedule);
    final BlockHeaderBuilder builder = BlockHeaderBuilder.create().populateFrom(sealableBlockHeader).mixHash(IbftHelpers.EXPECTED_MIX_HASH).nonce(0).blockHeaderFunctions(blockHeaderFunctions);
    final IbftExtraData sealedExtraData = constructSignedExtraData(builder.buildBlockHeader());
    // Replace the extraData in the BlockHeaderBuilder, and return header.
    return builder.extraData(sealedExtraData.encode()).buildBlockHeader();
}
Also used : BlockHeaderFunctions(org.hyperledger.besu.ethereum.core.BlockHeaderFunctions) ScheduleBasedBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.ScheduleBasedBlockHeaderFunctions) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) BlockHeaderBuilder(org.hyperledger.besu.ethereum.core.BlockHeaderBuilder)

Example 5 with BlockHeaderFunctions

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

the class JsonRpcResponseUtils method response.

/**
 * @param values hex encoded values.
 */
public JsonRpcResponse response(final Map<JsonRpcResponseKey, String> values, final List<TransactionResult> transactions) {
    final Hash mixHash = hash(values.get(MIX_HASH));
    final Hash parentHash = hash(values.get(PARENT_HASH));
    final Hash ommersHash = hash(values.get(OMMERS_HASH));
    final Address coinbase = address(values.get(COINBASE));
    final Hash stateRoot = hash(values.get(STATE_ROOT));
    final Hash transactionsRoot = hash(values.get(TRANSACTION_ROOT));
    final Hash receiptsRoot = hash(values.get(RECEIPTS_ROOT));
    final LogsBloomFilter logsBloom = logsBloom(values.get(LOGS_BLOOM));
    final Difficulty difficulty = Difficulty.of(unsignedInt256(values.get(DIFFICULTY)));
    final Bytes extraData = bytes(values.get(EXTRA_DATA));
    final BlockHeaderFunctions blockHeaderFunctions = new MainnetBlockHeaderFunctions();
    final long number = unsignedLong(values.get(NUMBER));
    final long gasLimit = unsignedLong(values.get(GAS_LIMIT));
    final long gasUsed = unsignedLong(values.get(GAS_USED));
    final long timestamp = unsignedLong(values.get(TIMESTAMP));
    final long nonce = unsignedLong(values.get(NONCE));
    final Wei baseFee = values.containsKey(BASEFEE) ? Wei.of(unsignedInt256(values.get(BASEFEE))) : null;
    final Difficulty totalDifficulty = Difficulty.of(unsignedInt256(values.get(TOTAL_DIFFICULTY)));
    final int size = unsignedInt(values.get(SIZE));
    final List<JsonNode> ommers = new ArrayList<>();
    final BlockHeader header = new BlockHeader(parentHash, ommersHash, coinbase, stateRoot, transactionsRoot, receiptsRoot, logsBloom, difficulty, number, gasLimit, gasUsed, timestamp, extraData, baseFee, mixHash, nonce, blockHeaderFunctions);
    return new JsonRpcSuccessResponse(null, new BlockResult(header, transactions, ommers, totalDifficulty, size));
}
Also used : Address(org.hyperledger.besu.datatypes.Address) LogsBloomFilter(org.hyperledger.besu.evm.log.LogsBloomFilter) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions) BlockHeaderFunctions(org.hyperledger.besu.ethereum.core.BlockHeaderFunctions) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Hash(org.hyperledger.besu.datatypes.Hash) Bytes(org.apache.tuweni.bytes.Bytes) BlockResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult) Wei(org.hyperledger.besu.datatypes.Wei) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) MainnetBlockHeaderFunctions(org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions)

Aggregations

BlockHeaderFunctions (org.hyperledger.besu.ethereum.core.BlockHeaderFunctions)6 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)3 MainnetBlockHeaderFunctions (org.hyperledger.besu.ethereum.mainnet.MainnetBlockHeaderFunctions)3 ArrayList (java.util.ArrayList)2 BlockHeaderBuilder (org.hyperledger.besu.ethereum.core.BlockHeaderBuilder)2 ScheduleBasedBlockHeaderFunctions (org.hyperledger.besu.ethereum.mainnet.ScheduleBasedBlockHeaderFunctions)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Bytes (org.apache.tuweni.bytes.Bytes)1 CliqueExtraData (org.hyperledger.besu.consensus.clique.CliqueExtraData)1 ValidatorVote (org.hyperledger.besu.consensus.common.validator.ValidatorVote)1 IbftExtraData (org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData)1 Address (org.hyperledger.besu.datatypes.Address)1 Hash (org.hyperledger.besu.datatypes.Hash)1 Wei (org.hyperledger.besu.datatypes.Wei)1 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)1 BlockResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult)1 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)1 Block (org.hyperledger.besu.ethereum.core.Block)1 BlockBody (org.hyperledger.besu.ethereum.core.BlockBody)1 Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)1