Search in sources :

Example 71 with Blockchain

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;
}
Also used : Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) Block(org.hyperledger.besu.ethereum.core.Block) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 72 with Blockchain

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));
}
Also used : AbstractPendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.AbstractPendingTransactionsSorter) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) Bytes(org.apache.tuweni.bytes.Bytes) MergeContext(org.hyperledger.besu.consensus.merge.MergeContext) Address(org.hyperledger.besu.datatypes.Address) AtomicReference(java.util.concurrent.atomic.AtomicReference) AbstractGasLimitSpecification(org.hyperledger.besu.ethereum.mainnet.AbstractGasLimitSpecification) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) Wei(org.hyperledger.besu.datatypes.Wei) Result(org.hyperledger.besu.ethereum.BlockValidator.Result) Block(org.hyperledger.besu.ethereum.core.Block) Bytes32(org.apache.tuweni.bytes.Bytes32) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) Logger(org.slf4j.Logger) BadBlockManager(org.hyperledger.besu.ethereum.chain.BadBlockManager) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) MiningParameters(org.hyperledger.besu.ethereum.core.MiningParameters) TransitionUtils.isTerminalProofOfWorkBlock(org.hyperledger.besu.consensus.merge.TransitionUtils.isTerminalProofOfWorkBlock) Slf4jLambdaHelper.debugLambda(org.hyperledger.besu.util.Slf4jLambdaHelper.debugLambda) TimeUnit(java.util.concurrent.TimeUnit) BackwardsSyncContext(org.hyperledger.besu.ethereum.eth.sync.backwardsync.BackwardsSyncContext) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Optional(java.util.Optional) HeaderValidationMode(org.hyperledger.besu.ethereum.mainnet.HeaderValidationMode) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) Transaction(org.hyperledger.besu.ethereum.core.Transaction) Collections(java.util.Collections) Hash(org.hyperledger.besu.datatypes.Hash) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Aggregations

Blockchain (org.hyperledger.besu.ethereum.chain.Blockchain)72 Test (org.junit.Test)45 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)42 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)36 Optional (java.util.Optional)31 Hash (org.hyperledger.besu.datatypes.Hash)31 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)31 List (java.util.List)30 Block (org.hyperledger.besu.ethereum.core.Block)30 Bytes (org.apache.tuweni.bytes.Bytes)27 WorldStateArchive (org.hyperledger.besu.ethereum.worldstate.WorldStateArchive)26 Transaction (org.hyperledger.besu.ethereum.core.Transaction)25 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)24 InMemoryKeyValueStorageProvider.createInMemoryBlockchain (org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain)24 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)23 ArrayList (java.util.ArrayList)22 Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)22 CompletableFuture (java.util.concurrent.CompletableFuture)21 Wei (org.hyperledger.besu.datatypes.Wei)21 BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)21