use of org.hyperledger.besu.ethereum.core.Block in project besu by hyperledger.
the class BackwardChain method merge.
public void merge(final BackwardChain historicalBackwardChain) {
BlockHeader firstHeader = getFirstAncestorHeader().orElseThrow(() -> new BackwardSyncException("Cannot merge when syncing forward..."));
Block historicalPivot = historicalBackwardChain.getPivot();
Block pivot = getPivot();
if (firstHeader.getParentHash().equals(historicalPivot.getHash())) {
Collections.reverse(historicalBackwardChain.successors);
this.ancestors.addAll(historicalBackwardChain.successors.stream().map(Block::getHeader).collect(Collectors.toList()));
this.ancestors.addAll(historicalBackwardChain.ancestors);
debugLambda(LOG, "Merged backward chain led by block {} into chain led by block {}, new backward chain starts at height {} and ends at height {}", () -> historicalPivot.getHash().toString().substring(0, 20), () -> pivot.getHash().toString().substring(0, 20), () -> pivot.getHeader().getNumber(), () -> getFirstAncestorHeader().orElseThrow().getNumber());
trustedBlocks.putAll(historicalBackwardChain.trustedBlocks);
} else {
warnLambda(LOG, "Cannot merge previous historical run because headers of {} and {} do not equal. Ignoring previous run. Did someone lie to us?", () -> firstHeader.getHash().toString().substring(0, 20), () -> historicalPivot.getHash().toString().substring(0, 20));
}
}
use of org.hyperledger.besu.ethereum.core.Block in project besu by hyperledger.
the class JsonBlockImporter method getParentHeader.
private BlockHeader getParentHeader(final BlockData blockData, final List<Block> importedBlocks) {
if (blockData.getParentHash().isPresent()) {
final Hash parentHash = blockData.getParentHash().get();
return controller.getProtocolContext().getBlockchain().getBlockHeader(parentHash).orElseThrow(() -> new IllegalArgumentException("Unable to locate block parent at " + parentHash));
}
if (importedBlocks.size() > 0 && blockData.getNumber().isPresent()) {
final long targetParentBlockNumber = blockData.getNumber().get() - 1L;
Optional<BlockHeader> maybeHeader = importedBlocks.stream().map(Block::getHeader).filter(h -> h.getNumber() == targetParentBlockNumber).findFirst();
if (maybeHeader.isPresent()) {
return maybeHeader.get();
}
}
long blockNumber;
if (blockData.getNumber().isPresent()) {
blockNumber = blockData.getNumber().get() - 1L;
} else if (importedBlocks.size() > 0) {
// If there is no number or hash, import blocks in order
blockNumber = importedBlocks.get(importedBlocks.size() - 1).getHeader().getNumber();
} else {
blockNumber = BlockHeader.GENESIS_BLOCK_NUMBER;
}
if (blockNumber < BlockHeader.GENESIS_BLOCK_NUMBER) {
throw new IllegalArgumentException("Invalid block number: " + blockNumber + 1);
}
return controller.getProtocolContext().getBlockchain().getBlockHeader(blockNumber).orElseThrow(() -> new IllegalArgumentException("Unable to locate block parent at " + blockNumber));
}
use of org.hyperledger.besu.ethereum.core.Block 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.Block in project besu by hyperledger.
the class ProposalPayloadValidatorTest method validationFailsWhenExpectedProposerDoesNotMatchPayloadsAuthor.
@Test
public void validationFailsWhenExpectedProposerDoesNotMatchPayloadsAuthor() {
final ProposalPayloadValidator payloadValidator = new ProposalPayloadValidator(Address.fromHexString("0x1"), roundIdentifier, blockValidator, protocolContext, bftExtraDataCodec);
final Block block = ProposedBlockHelpers.createProposalBlock(emptyList(), roundIdentifier);
final Proposal proposal = messageFactory.createProposal(roundIdentifier, block, emptyList(), emptyList());
assertThat(payloadValidator.validate(proposal.getSignedPayload())).isFalse();
verifyNoMoreInteractions(blockValidator);
}
use of org.hyperledger.besu.ethereum.core.Block in project besu by hyperledger.
the class ProposalPayloadValidatorTest method validationFailsWhenMessageMismatchesExpectedHeight.
@Test
public void validationFailsWhenMessageMismatchesExpectedHeight() {
final ProposalPayloadValidator payloadValidator = new ProposalPayloadValidator(expectedProposer, roundIdentifier, blockValidator, protocolContext, bftExtraDataCodec);
final Block block = ProposedBlockHelpers.createProposalBlock(emptyList(), roundIdentifier);
final Proposal proposal = messageFactory.createProposal(ConsensusRoundHelpers.createFrom(roundIdentifier, +1, 0), block, emptyList(), emptyList());
assertThat(payloadValidator.validate(proposal.getSignedPayload())).isFalse();
verifyNoMoreInteractions(blockValidator);
}
Aggregations