use of org.hyperledger.besu.ethereum.chain.Blockchain in project besu by hyperledger.
the class ForkIdTestUtil method mockBlockchain.
public static Blockchain mockBlockchain(final String genesisHash, final LongSupplier chainHeightSupplier) {
final Blockchain mockchain = mock(Blockchain.class);
final BlockHeader mockHeader = mock(BlockHeader.class);
final Block block = new Block(mockHeader, null);
when(mockchain.getGenesisBlock()).thenReturn(block);
when(mockchain.getChainHeadBlockNumber()).thenReturn(chainHeightSupplier.getAsLong());
when(mockHeader.getHash()).thenReturn(Hash.fromHexString(genesisHash));
return mockchain;
}
use of org.hyperledger.besu.ethereum.chain.Blockchain in project besu by hyperledger.
the class MergeCoordinator method updateForkChoice.
@Override
public ForkchoiceResult updateForkChoice(final Hash headBlockHash, final Hash finalizedBlockHash) {
MutableBlockchain blockchain = protocolContext.getBlockchain();
Optional<BlockHeader> currentFinalized = mergeContext.getFinalized();
final Optional<BlockHeader> newFinalized = blockchain.getBlockHeader(finalizedBlockHash);
if (newFinalized.isEmpty() && !finalizedBlockHash.equals(Hash.ZERO)) {
// we should only fail to find when it's the special value 0x000..000
return ForkchoiceResult.withFailure(String.format("should've been able to find block hash %s but couldn't", finalizedBlockHash));
}
if (currentFinalized.isPresent() && newFinalized.isPresent() && !isDescendantOf(currentFinalized.get(), newFinalized.get())) {
return ForkchoiceResult.withFailure(String.format("new finalized block %s is not a descendant of current finalized block %s", finalizedBlockHash, currentFinalized.get().getBlockHash()));
}
// ensure we have headBlock:
BlockHeader newHead = blockchain.getBlockHeader(headBlockHash).orElse(null);
if (newHead == null) {
return ForkchoiceResult.withFailure(String.format("not able to find new head block %s", headBlockHash));
}
// ensure new head is descendant of finalized
Optional<String> descendantError = newFinalized.map(Optional::of).orElse(currentFinalized).filter(finalized -> !isDescendantOf(finalized, newHead)).map(finalized -> String.format("new head block %s is not a descendant of current finalized block %s", newHead.getBlockHash(), finalized.getBlockHash()));
if (descendantError.isPresent()) {
return ForkchoiceResult.withFailure(descendantError.get());
}
// set the new head
blockchain.rewindToBlock(newHead.getHash());
// set and persist the new finalized block if it is present
newFinalized.ifPresent(blockHeader -> {
blockchain.setFinalized(blockHeader.getHash());
mergeContext.setFinalized(blockHeader);
});
return ForkchoiceResult.withResult(newFinalized, Optional.of(newHead));
}
Aggregations