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");
}
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();
}
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);
}
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();
}
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));
}
Aggregations