Simplify ProtocolContext creation (#7792)

Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>
pull/7877/head
Fabio Di Fabio 5 days ago committed by GitHub
parent dc81641c58
commit 833a5ce5dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 24
      besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java
  2. 17
      besu/src/main/java/org/hyperledger/besu/controller/ConsensusScheduleBesuControllerBuilder.java
  3. 5
      besu/src/main/java/org/hyperledger/besu/controller/QbftBesuControllerBuilder.java
  4. 7
      besu/src/main/java/org/hyperledger/besu/controller/TransitionBesuControllerBuilder.java
  5. 17
      besu/src/test/java/org/hyperledger/besu/controller/ConsensusScheduleBesuControllerBuilderTest.java
  6. 17
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java
  7. 2
      consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java
  8. 4
      consensus/common/src/main/java/org/hyperledger/besu/consensus/common/MigratingConsensusContext.java
  9. 36
      consensus/common/src/main/java/org/hyperledger/besu/consensus/common/MigratingProtocolContext.java
  10. 6
      consensus/common/src/test/java/org/hyperledger/besu/consensus/common/MigratingProtocolContextTest.java
  11. 5
      ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java
  12. 4
      ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberLatestDesyncIntegrationTest.java
  13. 6
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/graphql/AbstractEthGraphQLHttpServiceTest.java
  14. 7
      ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/BlockMinerTest.java
  15. 38
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/ConsensusContextFactory.java
  16. 29
      ethereum/core/src/main/java/org/hyperledger/besu/ethereum/ProtocolContext.java
  17. 11
      ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/BlockchainSetupUtil.java
  18. 24
      ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/ConsensusContextFixture.java
  19. 3
      ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/ExecutionContextTestFixture.java
  20. 5
      ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/AbstractIsolationTests.java
  21. 4
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullSyncTargetManagerTest.java
  22. 7
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskParameterizedTest.java
  23. 7
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskTest.java
  24. 4
      ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/TestNode.java
  25. 7
      ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/BlockchainReferenceTestCaseSpec.java

@ -26,7 +26,6 @@ import org.hyperledger.besu.consensus.qbft.BFTPivotSelectorFromPeers;
import org.hyperledger.besu.cryptoservices.NodeKey;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ConsensusContextFactory;
import org.hyperledger.besu.ethereum.GasLimitCalculator;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods;
@ -605,9 +604,11 @@ public abstract class BesuControllerBuilder implements MiningParameterOverrides
genesisState.writeStateTo(worldStateArchive.getMutable());
}
final var consensusContext =
createConsensusContext(blockchain, worldStateArchive, protocolSchedule);
final ProtocolContext protocolContext =
createProtocolContext(
blockchain, worldStateArchive, protocolSchedule, this::createConsensusContext);
createProtocolContext(blockchain, worldStateArchive, consensusContext);
validateContext(protocolContext);
protocolSchedule.setPublicWorldStateArchiveForPrivacyBlockProcessor(
@ -976,7 +977,7 @@ public abstract class BesuControllerBuilder implements MiningParameterOverrides
}
/**
* Create mining coordinator mining coordinator.
* Create mining coordinator.
*
* @param protocolSchedule the protocol schedule
* @param protocolContext the protocol context
@ -1017,9 +1018,9 @@ public abstract class BesuControllerBuilder implements MiningParameterOverrides
* @return the consensus context
*/
protected abstract ConsensusContext createConsensusContext(
Blockchain blockchain,
WorldStateArchive worldStateArchive,
ProtocolSchedule protocolSchedule);
final Blockchain blockchain,
final WorldStateArchive worldStateArchive,
final ProtocolSchedule protocolSchedule);
/**
* Gets supported protocol.
@ -1079,17 +1080,14 @@ public abstract class BesuControllerBuilder implements MiningParameterOverrides
*
* @param blockchain the blockchain
* @param worldStateArchive the world state archive
* @param protocolSchedule the protocol schedule
* @param consensusContextFactory the consensus context factory
* @param consensusContext the consensus context
* @return the protocol context
*/
protected ProtocolContext createProtocolContext(
final MutableBlockchain blockchain,
final WorldStateArchive worldStateArchive,
final ProtocolSchedule protocolSchedule,
final ConsensusContextFactory consensusContextFactory) {
return ProtocolContext.init(
blockchain, worldStateArchive, protocolSchedule, consensusContextFactory, badBlockManager);
final ConsensusContext consensusContext) {
return new ProtocolContext(blockchain, worldStateArchive, consensusContext, badBlockManager);
}
private Optional<SnapProtocolManager> createSnapProtocolManager(

@ -20,13 +20,12 @@ import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.consensus.common.CombinedProtocolScheduleFactory;
import org.hyperledger.besu.consensus.common.ForkSpec;
import org.hyperledger.besu.consensus.common.ForksSchedule;
import org.hyperledger.besu.consensus.common.MigratingContext;
import org.hyperledger.besu.consensus.common.MigratingConsensusContext;
import org.hyperledger.besu.consensus.common.MigratingMiningCoordinator;
import org.hyperledger.besu.consensus.common.MigratingProtocolContext;
import org.hyperledger.besu.cryptoservices.NodeKey;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ConsensusContextFactory;
import org.hyperledger.besu.ethereum.GasLimitCalculator;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethods;
@ -168,10 +167,12 @@ public class ConsensusScheduleBesuControllerBuilder extends BesuControllerBuilde
protected ProtocolContext createProtocolContext(
final MutableBlockchain blockchain,
final WorldStateArchive worldStateArchive,
final ProtocolSchedule protocolSchedule,
final ConsensusContextFactory consensusContextFactory) {
return MigratingProtocolContext.init(
blockchain, worldStateArchive, protocolSchedule, consensusContextFactory, badBlockManager);
final ConsensusContext consensusContext) {
return new MigratingProtocolContext(
blockchain,
worldStateArchive,
consensusContext.as(MigratingConsensusContext.class),
badBlockManager);
}
@Override
@ -188,10 +189,10 @@ public class ConsensusScheduleBesuControllerBuilder extends BesuControllerBuilde
e.getValue()
.createConsensusContext(
blockchain, worldStateArchive, protocolSchedule)))
.collect(Collectors.toList());
.toList();
final ForksSchedule<ConsensusContext> consensusContextsSchedule =
new ForksSchedule<>(consensusContextSpecs);
return new MigratingContext(consensusContextsSchedule);
return new MigratingConsensusContext(consensusContextsSchedule);
}
@Override

@ -112,10 +112,7 @@ public class QbftBesuControllerBuilder extends BftBesuControllerBuilder {
@Override
protected Supplier<BftExtraDataCodec> bftExtraDataCodec() {
return Suppliers.memoize(
() -> {
return new QbftExtraDataCodec();
});
return Suppliers.memoize(QbftExtraDataCodec::new);
}
@Override

@ -24,7 +24,6 @@ import org.hyperledger.besu.consensus.merge.blockcreation.TransitionCoordinator;
import org.hyperledger.besu.cryptoservices.NodeKey;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ConsensusContextFactory;
import org.hyperledger.besu.ethereum.GasLimitCalculator;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.blockcreation.MiningCoordinator;
@ -193,11 +192,9 @@ public class TransitionBesuControllerBuilder extends BesuControllerBuilder {
protected ProtocolContext createProtocolContext(
final MutableBlockchain blockchain,
final WorldStateArchive worldStateArchive,
final ProtocolSchedule protocolSchedule,
final ConsensusContextFactory consensusContextFactory) {
final ConsensusContext consensusContext) {
final ProtocolContext protocolContext =
super.createProtocolContext(
blockchain, worldStateArchive, protocolSchedule, consensusContextFactory);
super.createProtocolContext(blockchain, worldStateArchive, consensusContext);
transitionProtocolSchedule.setProtocolContext(protocolContext);
return protocolContext;
}

@ -24,7 +24,7 @@ import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.config.StubGenesisConfigOptions;
import org.hyperledger.besu.consensus.common.ForkSpec;
import org.hyperledger.besu.consensus.common.ForksSchedule;
import org.hyperledger.besu.consensus.common.MigratingContext;
import org.hyperledger.besu.consensus.common.MigratingConsensusContext;
import org.hyperledger.besu.consensus.common.MigratingMiningCoordinator;
import org.hyperledger.besu.consensus.common.bft.blockcreation.BftMiningCoordinator;
import org.hyperledger.besu.ethereum.ConsensusContext;
@ -166,8 +166,8 @@ public class ConsensusScheduleBesuControllerBuilderTest {
@Test
public void createsMigratingContext() {
final ConsensusContext context1 = Mockito.mock(ConsensusContext.class);
final ConsensusContext context2 = Mockito.mock(ConsensusContext.class);
final ConsensusContext context1 = mock(ConsensusContext.class);
final ConsensusContext context2 = mock(ConsensusContext.class);
final Map<Long, BesuControllerBuilder> besuControllerBuilderSchedule = new TreeMap<>();
besuControllerBuilderSchedule.put(0L, besuControllerBuilder1);
@ -180,15 +180,14 @@ public class ConsensusScheduleBesuControllerBuilderTest {
new ConsensusScheduleBesuControllerBuilder(besuControllerBuilderSchedule);
final ConsensusContext consensusContext =
controllerBuilder.createConsensusContext(
Mockito.mock(Blockchain.class),
Mockito.mock(WorldStateArchive.class),
Mockito.mock(ProtocolSchedule.class));
mock(Blockchain.class), mock(WorldStateArchive.class), mock(ProtocolSchedule.class));
assertThat(consensusContext).isInstanceOf(MigratingContext.class);
final MigratingContext migratingContext = (MigratingContext) consensusContext;
assertThat(consensusContext).isInstanceOf(MigratingConsensusContext.class);
final MigratingConsensusContext migratingConsensusContext =
(MigratingConsensusContext) consensusContext;
final ForksSchedule<ConsensusContext> contextSchedule =
migratingContext.getConsensusContextSchedule();
migratingConsensusContext.getConsensusContextSchedule();
final NavigableSet<ForkSpec<ConsensusContext>> expectedConsensusContextSpecs =
new TreeSet<>(ForkSpec.COMPARATOR);

@ -52,6 +52,7 @@ import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.core.ImmutableMiningConfiguration;
import org.hyperledger.besu.ethereum.core.ImmutableMiningConfiguration.MutableInitValues;
import org.hyperledger.besu.ethereum.core.MiningConfiguration;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.Util;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
@ -99,11 +100,20 @@ public class CliqueBlockCreatorTest {
@BeforeEach
void setup() {
final Address otherAddress = Util.publicKeyToAddress(otherKeyPair.getPublicKey());
validatorList.add(otherAddress);
validatorProvider = mock(ValidatorProvider.class);
voteProvider = mock(VoteProvider.class);
when(validatorProvider.getVoteProviderAtHead()).thenReturn(Optional.of(voteProvider));
when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList);
protocolSchedule =
CliqueProtocolSchedule.create(
GenesisConfigFile.DEFAULT.getConfigOptions(),
new ForksSchedule<>(List.of()),
proposerNodeKey,
PrivacyParameters.DEFAULT,
false,
EvmConfiguration.DEFAULT,
MiningConfiguration.MINING_DISABLED,
@ -111,13 +121,6 @@ public class CliqueBlockCreatorTest {
false,
new NoOpMetricsSystem());
final Address otherAddress = Util.publicKeyToAddress(otherKeyPair.getPublicKey());
validatorList.add(otherAddress);
validatorProvider = mock(ValidatorProvider.class);
voteProvider = mock(VoteProvider.class);
when(validatorProvider.getVoteProviderAtHead()).thenReturn(Optional.of(voteProvider));
when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList);
final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface);
final Block genesis =

@ -42,6 +42,7 @@ import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;
import org.hyperledger.besu.ethereum.core.ImmutableMiningConfiguration;
import org.hyperledger.besu.ethereum.core.ImmutableMiningConfiguration.MutableInitValues;
import org.hyperledger.besu.ethereum.core.MiningConfiguration;
import org.hyperledger.besu.ethereum.core.PrivacyParameters;
import org.hyperledger.besu.ethereum.core.Util;
import org.hyperledger.besu.ethereum.eth.manager.EthContext;
import org.hyperledger.besu.ethereum.eth.manager.EthScheduler;
@ -103,6 +104,7 @@ public class CliqueMinerExecutorTest {
GENESIS_CONFIG_OPTIONS,
new ForksSchedule<>(List.of()),
proposerNodeKey,
PrivacyParameters.DEFAULT,
false,
EvmConfiguration.DEFAULT,
MiningConfiguration.MINING_DISABLED,

@ -17,7 +17,7 @@ package org.hyperledger.besu.consensus.common;
import org.hyperledger.besu.ethereum.ConsensusContext;
/** The Migrating context. */
public class MigratingContext implements ConsensusContext {
public class MigratingConsensusContext implements ConsensusContext {
private final ForksSchedule<ConsensusContext> consensusContextSchedule;
@ -26,7 +26,7 @@ public class MigratingContext implements ConsensusContext {
*
* @param consensusContextSchedule the consensus context schedule
*/
public MigratingContext(final ForksSchedule<ConsensusContext> consensusContextSchedule) {
public MigratingConsensusContext(final ForksSchedule<ConsensusContext> consensusContextSchedule) {
this.consensusContextSchedule = consensusContextSchedule;
}

@ -15,11 +15,9 @@
package org.hyperledger.besu.consensus.common;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ConsensusContextFactory;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.BadBlockManager;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
/** The Migrating protocol context. */
@ -32,42 +30,16 @@ public class MigratingProtocolContext extends ProtocolContext {
*
* @param blockchain the blockchain
* @param worldStateArchive the world state archive
* @param consensusContextSchedule the consensus context schedule
* @param migratingConsensusContext the consensus context
* @param badBlockManager the cache to use to keep invalid blocks
*/
public MigratingProtocolContext(
final MutableBlockchain blockchain,
final WorldStateArchive worldStateArchive,
final ForksSchedule<ConsensusContext> consensusContextSchedule,
final MigratingConsensusContext migratingConsensusContext,
final BadBlockManager badBlockManager) {
super(blockchain, worldStateArchive, null, badBlockManager);
this.consensusContextSchedule = consensusContextSchedule;
}
/**
* Init protocol context.
*
* @param blockchain the blockchain
* @param worldStateArchive the world state archive
* @param protocolSchedule the protocol schedule
* @param consensusContextFactory the consensus context factory
* @param badBlockManager the cache to use to keep invalid blocks
* @return the protocol context
*/
public static ProtocolContext init(
final MutableBlockchain blockchain,
final WorldStateArchive worldStateArchive,
final ProtocolSchedule protocolSchedule,
final ConsensusContextFactory consensusContextFactory,
final BadBlockManager badBlockManager) {
final ConsensusContext consensusContext =
consensusContextFactory.create(blockchain, worldStateArchive, protocolSchedule);
final MigratingContext migratingContext = consensusContext.as(MigratingContext.class);
return new MigratingProtocolContext(
blockchain,
worldStateArchive,
migratingContext.getConsensusContextSchedule(),
badBlockManager);
super(blockchain, worldStateArchive, migratingConsensusContext, badBlockManager);
this.consensusContextSchedule = migratingConsensusContext.getConsensusContextSchedule();
}
@Override

@ -43,9 +43,13 @@ public class MigratingProtocolContextTest {
final ForksSchedule<ConsensusContext> contextSchedule =
new ForksSchedule<>(List.of(new ForkSpec<>(0L, context1), new ForkSpec<>(10L, context2)));
final MigratingProtocolContext migratingProtocolContext =
new MigratingProtocolContext(
blockchain, worldStateArchive, contextSchedule, new BadBlockManager());
blockchain,
worldStateArchive,
new MigratingConsensusContext(contextSchedule),
new BadBlockManager());
assertThat(migratingProtocolContext.getConsensusContext(ConsensusContext.class))
.isSameAs(context1);

@ -19,6 +19,7 @@ import static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider
import static org.mockito.Mockito.mock;
import org.hyperledger.besu.config.StubGenesisConfigOptions;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.ImmutableApiConfiguration;
import org.hyperledger.besu.ethereum.api.graphql.GraphQLConfiguration;
@ -82,7 +83,9 @@ public class JsonRpcTestMethodsFactory {
this.blockchain = createInMemoryBlockchain(importer.getGenesisBlock());
this.stateArchive = createInMemoryWorldStateArchive();
this.importer.getGenesisState().writeStateTo(stateArchive.getMutable());
this.context = new ProtocolContext(blockchain, stateArchive, null, new BadBlockManager());
this.context =
new ProtocolContext(
blockchain, stateArchive, mock(ConsensusContext.class), new BadBlockManager());
this.protocolSchedule = importer.getProtocolSchedule();
this.synchronizer = mock(Synchronizer.class);

@ -20,6 +20,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.BlockchainImporter;
import org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcTestMethodsFactory;
@ -67,7 +68,8 @@ public class EthGetBlockByNumberLatestDesyncIntegrationTest {
InMemoryKeyValueStorageProvider.createInMemoryBlockchain(importer.getGenesisBlock());
WorldStateArchive state = InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive();
importer.getGenesisState().writeStateTo(state.getMutable());
ProtocolContext context = new ProtocolContext(chain, state, null, new BadBlockManager());
ProtocolContext context =
new ProtocolContext(chain, state, mock(ConsensusContext.class), new BadBlockManager());
for (final Block block : importer.getBlocks()) {
final ProtocolSchedule protocolSchedule = importer.getProtocolSchedule();

@ -19,6 +19,7 @@ import static org.mockito.Mockito.when;
import org.hyperledger.besu.datatypes.TransactionType;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.api.ImmutableApiConfiguration;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
@ -110,7 +111,10 @@ public abstract class AbstractEthGraphQLHttpServiceTest {
final MutableBlockchain blockchain = blockchainSetupUtil.getBlockchain();
ProtocolContext context =
new ProtocolContext(
blockchain, blockchainSetupUtil.getWorldArchive(), null, new BadBlockManager());
blockchain,
blockchainSetupUtil.getWorldArchive(),
mock(ConsensusContext.class),
new BadBlockManager());
final BlockchainQueries blockchainQueries =
new BlockchainQueries(
blockchainSetupUtil.getProtocolSchedule(),

@ -22,6 +22,7 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.blockcreation.BlockCreator.BlockCreationResult;
import org.hyperledger.besu.ethereum.blockcreation.txselection.TransactionSelectionResults;
@ -58,7 +59,7 @@ public class BlockMinerTest {
headerBuilder.buildHeader(), new BlockBody(Lists.newArrayList(), Lists.newArrayList()));
final ProtocolContext protocolContext =
new ProtocolContext(null, null, null, new BadBlockManager());
new ProtocolContext(null, null, mock(ConsensusContext.class), new BadBlockManager());
final PoWBlockCreator blockCreator = mock(PoWBlockCreator.class);
final Function<BlockHeader, PoWBlockCreator> blockCreatorSupplier =
@ -102,7 +103,7 @@ public class BlockMinerTest {
headerBuilder.buildHeader(), new BlockBody(Lists.newArrayList(), Lists.newArrayList()));
final ProtocolContext protocolContext =
new ProtocolContext(null, null, null, new BadBlockManager());
new ProtocolContext(null, null, mock(ConsensusContext.class), new BadBlockManager());
final PoWBlockCreator blockCreator = mock(PoWBlockCreator.class);
final Function<BlockHeader, PoWBlockCreator> blockCreatorSupplier =
@ -150,7 +151,7 @@ public class BlockMinerTest {
headerBuilder.buildHeader(), new BlockBody(Lists.newArrayList(), Lists.newArrayList()));
final ProtocolContext protocolContext =
new ProtocolContext(null, null, null, new BadBlockManager());
new ProtocolContext(null, null, mock(ConsensusContext.class), new BadBlockManager());
final PoWBlockCreator blockCreator = mock(PoWBlockCreator.class);
final Function<BlockHeader, PoWBlockCreator> blockCreatorSupplier =

@ -1,38 +0,0 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
/** The ConsensusContextFactory interface defines a method for creating a consensus context. */
@FunctionalInterface
public interface ConsensusContextFactory {
/**
* Creates a consensus context with the given blockchain, world state archive, and protocol
* schedule.
*
* @param blockchain the blockchain
* @param worldStateArchive the world state archive
* @param protocolSchedule the protocol schedule
* @return the created consensus context
*/
ConsensusContext create(
Blockchain blockchain,
WorldStateArchive worldStateArchive,
ProtocolSchedule protocolSchedule);
}

@ -16,7 +16,6 @@ package org.hyperledger.besu.ethereum;
import org.hyperledger.besu.ethereum.chain.BadBlockManager;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import java.util.Optional;
@ -29,8 +28,8 @@ import java.util.Optional;
public class ProtocolContext {
private final MutableBlockchain blockchain;
private final WorldStateArchive worldStateArchive;
private final BadBlockManager badBlockManager;
private final ConsensusContext consensusContext;
private final BadBlockManager badBlockManager;
/**
* Constructs a new ProtocolContext with the given blockchain, world state archive, consensus
@ -38,7 +37,7 @@ public class ProtocolContext {
*
* @param blockchain the blockchain of the protocol context
* @param worldStateArchive the world state archive of the protocol context
* @param consensusContext the consensus context of the protocol context
* @param consensusContext the consensus context
* @param badBlockManager the bad block manager of the protocol context
*/
public ProtocolContext(
@ -52,30 +51,6 @@ public class ProtocolContext {
this.badBlockManager = badBlockManager;
}
/**
* Initializes a new ProtocolContext with the given blockchain, world state archive, protocol
* schedule, consensus context factory, and bad block manager.
*
* @param blockchain the blockchain of the protocol context
* @param worldStateArchive the world state archive of the protocol context
* @param protocolSchedule the protocol schedule of the protocol context
* @param consensusContextFactory the consensus context factory of the protocol context
* @param badBlockManager the bad block manager of the protocol context
* @return the initialized ProtocolContext
*/
public static ProtocolContext init(
final MutableBlockchain blockchain,
final WorldStateArchive worldStateArchive,
final ProtocolSchedule protocolSchedule,
final ConsensusContextFactory consensusContextFactory,
final BadBlockManager badBlockManager) {
return new ProtocolContext(
blockchain,
worldStateArchive,
consensusContextFactory.create(blockchain, worldStateArchive, protocolSchedule),
badBlockManager);
}
/**
* Gets the blockchain of the protocol context.
*

@ -21,7 +21,6 @@ import static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider
import static org.mockito.Mockito.mock;
import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.BadBlockManager;
import org.hyperledger.besu.ethereum.chain.Blockchain;
@ -161,15 +160,7 @@ public class BlockchainSetupUtil {
private static ProtocolContext mainnetProtocolContextProvider(
final MutableBlockchain blockchain, final WorldStateArchive worldStateArchive) {
return new ProtocolContext(
blockchain,
worldStateArchive,
new ConsensusContext() {
@Override
public <C extends ConsensusContext> C as(final Class<C> klass) {
return null;
}
},
new BadBlockManager());
blockchain, worldStateArchive, new ConsensusContextFixture(), new BadBlockManager());
}
private static BlockchainSetupUtil create(

@ -0,0 +1,24 @@
/*
* Copyright contributors to Besu.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.core;
import org.hyperledger.besu.ethereum.ConsensusContext;
public class ConsensusContextFixture implements ConsensusContext {
@Override
public <C extends ConsensusContext> C as(final Class<C> klass) {
return klass.cast(this);
}
}

@ -76,7 +76,8 @@ public class ExecutionContextTestFixture {
else this.stateArchive = createInMemoryWorldStateArchive();
this.protocolSchedule = protocolSchedule;
this.protocolContext =
new ProtocolContext(blockchain, stateArchive, null, new BadBlockManager());
new ProtocolContext(
blockchain, stateArchive, new ConsensusContextFixture(), new BadBlockManager());
genesisState.writeStateTo(stateArchive.getMutable());
}

@ -30,6 +30,7 @@ import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Hash;
import org.hyperledger.besu.datatypes.Wei;
import org.hyperledger.besu.ethereum.BlockProcessingResult;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.blockcreation.AbstractBlockCreator;
import org.hyperledger.besu.ethereum.chain.BadBlockManager;
@ -172,7 +173,9 @@ public abstract class AbstractIsolationTests {
throwingWorldStateHealerSupplier());
var ws = archive.getMutable();
genesisState.writeStateTo(ws);
protocolContext = new ProtocolContext(blockchain, archive, null, new BadBlockManager());
protocolContext =
new ProtocolContext(
blockchain, archive, mock(ConsensusContext.class), new BadBlockManager());
ethContext = mock(EthContext.class, RETURNS_DEEP_STUBS);
when(ethContext.getEthPeers().subscribeConnect(any())).thenReturn(1L);
transactionPool =

@ -18,6 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.BadBlockManager;
import org.hyperledger.besu.ethereum.chain.Blockchain;
@ -77,7 +78,8 @@ public class FullSyncTargetManagerTest {
final ProtocolSchedule protocolSchedule = ProtocolScheduleFixture.MAINNET;
final ProtocolContext protocolContext =
new ProtocolContext(localBlockchain, localWorldState, null, new BadBlockManager());
new ProtocolContext(
localBlockchain, localWorldState, mock(ConsensusContext.class), new BadBlockManager());
ethProtocolManager =
EthProtocolManagerTestUtil.create(
protocolSchedule,

@ -19,6 +19,7 @@ import static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider
import static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive;
import static org.mockito.Mockito.mock;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.BadBlockManager;
import org.hyperledger.besu.ethereum.chain.MutableBlockchain;
@ -151,7 +152,11 @@ public class DetermineCommonAncestorTaskParameterizedTest {
final EthContext ethContext = ethProtocolManager.ethContext();
final ProtocolContext protocolContext =
new ProtocolContext(localBlockchain, worldStateArchive, null, new BadBlockManager());
new ProtocolContext(
localBlockchain,
worldStateArchive,
mock(ConsensusContext.class),
new BadBlockManager());
final EthTask<BlockHeader> task =
DetermineCommonAncestorTask.create(

@ -27,6 +27,7 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.BadBlockManager;
import org.hyperledger.besu.ethereum.chain.Blockchain;
@ -88,7 +89,11 @@ public class DetermineCommonAncestorTaskTest {
EthProtocolConfiguration.defaultConfig());
ethContext = ethProtocolManager.ethContext();
protocolContext =
new ProtocolContext(localBlockchain, worldStateArchive, null, new BadBlockManager());
new ProtocolContext(
localBlockchain,
worldStateArchive,
mock(ConsensusContext.class),
new BadBlockManager());
}
@Test

@ -27,6 +27,7 @@ import org.hyperledger.besu.config.GenesisConfigFile;
import org.hyperledger.besu.crypto.KeyPair;
import org.hyperledger.besu.cryptoservices.NodeKey;
import org.hyperledger.besu.cryptoservices.NodeKeyUtils;
import org.hyperledger.besu.ethereum.ConsensusContext;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.BadBlockManager;
import org.hyperledger.besu.ethereum.chain.GenesisState;
@ -135,7 +136,8 @@ public class TestNode implements Closeable {
final WorldStateArchive worldStateArchive = createInMemoryWorldStateArchive();
genesisState.writeStateTo(worldStateArchive.getMutable());
final ProtocolContext protocolContext =
new ProtocolContext(blockchain, worldStateArchive, null, new BadBlockManager());
new ProtocolContext(
blockchain, worldStateArchive, mock(ConsensusContext.class), new BadBlockManager());
final SyncState syncState = mock(SyncState.class);
final SynchronizerConfiguration syncConfig = mock(SynchronizerConfiguration.class);

@ -27,6 +27,7 @@ import org.hyperledger.besu.ethereum.core.Block;
import org.hyperledger.besu.ethereum.core.BlockBody;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderFunctions;
import org.hyperledger.besu.ethereum.core.ConsensusContextFixture;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider;
import org.hyperledger.besu.ethereum.core.MutableWorldState;
@ -108,7 +109,11 @@ public class BlockchainReferenceTestCaseSpec {
this.blockchain = buildBlockchain(genesisBlockHeader);
this.sealEngine = sealEngine;
this.protocolContext =
new ProtocolContext(this.blockchain, this.worldStateArchive, null, new BadBlockManager());
new ProtocolContext(
this.blockchain,
this.worldStateArchive,
new ConsensusContextFixture(),
new BadBlockManager());
}
public String getNetwork() {

Loading…
Cancel
Save