diff --git a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java index 9059d2209b..b3cbab5cc9 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/BesuControllerBuilder.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 createSnapProtocolManager( diff --git a/besu/src/main/java/org/hyperledger/besu/controller/ConsensusScheduleBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/ConsensusScheduleBesuControllerBuilder.java index 3ecd5a6c41..c70367aed6 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/ConsensusScheduleBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/ConsensusScheduleBesuControllerBuilder.java @@ -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 consensusContextsSchedule = new ForksSchedule<>(consensusContextSpecs); - return new MigratingContext(consensusContextsSchedule); + return new MigratingConsensusContext(consensusContextsSchedule); } @Override diff --git a/besu/src/main/java/org/hyperledger/besu/controller/QbftBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/QbftBesuControllerBuilder.java index 75aff30871..b6f579ea68 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/QbftBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/QbftBesuControllerBuilder.java @@ -112,10 +112,7 @@ public class QbftBesuControllerBuilder extends BftBesuControllerBuilder { @Override protected Supplier bftExtraDataCodec() { - return Suppliers.memoize( - () -> { - return new QbftExtraDataCodec(); - }); + return Suppliers.memoize(QbftExtraDataCodec::new); } @Override diff --git a/besu/src/main/java/org/hyperledger/besu/controller/TransitionBesuControllerBuilder.java b/besu/src/main/java/org/hyperledger/besu/controller/TransitionBesuControllerBuilder.java index 3530621fc6..75abca6a57 100644 --- a/besu/src/main/java/org/hyperledger/besu/controller/TransitionBesuControllerBuilder.java +++ b/besu/src/main/java/org/hyperledger/besu/controller/TransitionBesuControllerBuilder.java @@ -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; } diff --git a/besu/src/test/java/org/hyperledger/besu/controller/ConsensusScheduleBesuControllerBuilderTest.java b/besu/src/test/java/org/hyperledger/besu/controller/ConsensusScheduleBesuControllerBuilderTest.java index aefcda4c23..4e38e03c06 100644 --- a/besu/src/test/java/org/hyperledger/besu/controller/ConsensusScheduleBesuControllerBuilderTest.java +++ b/besu/src/test/java/org/hyperledger/besu/controller/ConsensusScheduleBesuControllerBuilderTest.java @@ -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 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 contextSchedule = - migratingContext.getConsensusContextSchedule(); + migratingConsensusContext.getConsensusContextSchedule(); final NavigableSet> expectedConsensusContextSpecs = new TreeSet<>(ForkSpec.COMPARATOR); diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java index fdc068ddef..12467cda4e 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueBlockCreatorTest.java @@ -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 = diff --git a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java index 3d6dbd77db..7000d145b6 100644 --- a/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java +++ b/consensus/clique/src/test/java/org/hyperledger/besu/consensus/clique/blockcreation/CliqueMinerExecutorTest.java @@ -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, diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/MigratingContext.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/MigratingConsensusContext.java similarity index 88% rename from consensus/common/src/main/java/org/hyperledger/besu/consensus/common/MigratingContext.java rename to consensus/common/src/main/java/org/hyperledger/besu/consensus/common/MigratingConsensusContext.java index f71e3ac7f3..91ace1a04b 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/MigratingContext.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/MigratingConsensusContext.java @@ -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 consensusContextSchedule; @@ -26,7 +26,7 @@ public class MigratingContext implements ConsensusContext { * * @param consensusContextSchedule the consensus context schedule */ - public MigratingContext(final ForksSchedule consensusContextSchedule) { + public MigratingConsensusContext(final ForksSchedule consensusContextSchedule) { this.consensusContextSchedule = consensusContextSchedule; } diff --git a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/MigratingProtocolContext.java b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/MigratingProtocolContext.java index c0557485f3..27cce3157b 100644 --- a/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/MigratingProtocolContext.java +++ b/consensus/common/src/main/java/org/hyperledger/besu/consensus/common/MigratingProtocolContext.java @@ -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 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 diff --git a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/MigratingProtocolContextTest.java b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/MigratingProtocolContextTest.java index d62de57eac..39ab9f8cb2 100644 --- a/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/MigratingProtocolContextTest.java +++ b/consensus/common/src/test/java/org/hyperledger/besu/consensus/common/MigratingProtocolContextTest.java @@ -43,9 +43,13 @@ public class MigratingProtocolContextTest { final ForksSchedule 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); diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java index 902d2b0487..2a65b1e7cb 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/JsonRpcTestMethodsFactory.java @@ -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); diff --git a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberLatestDesyncIntegrationTest.java b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberLatestDesyncIntegrationTest.java index 088eff8500..7e26a4d812 100644 --- a/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberLatestDesyncIntegrationTest.java +++ b/ethereum/api/src/integration-test/java/org/hyperledger/besu/ethereum/api/jsonrpc/methods/EthGetBlockByNumberLatestDesyncIntegrationTest.java @@ -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(); diff --git a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/graphql/AbstractEthGraphQLHttpServiceTest.java b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/graphql/AbstractEthGraphQLHttpServiceTest.java index 2b55b42ba2..08cfc3d7bd 100644 --- a/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/graphql/AbstractEthGraphQLHttpServiceTest.java +++ b/ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/graphql/AbstractEthGraphQLHttpServiceTest.java @@ -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(), diff --git a/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/BlockMinerTest.java b/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/BlockMinerTest.java index e5b46925aa..5b893d11f2 100644 --- a/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/BlockMinerTest.java +++ b/ethereum/blockcreation/src/test/java/org/hyperledger/besu/ethereum/blockcreation/BlockMinerTest.java @@ -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 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 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 blockCreatorSupplier = diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/ConsensusContextFactory.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/ConsensusContextFactory.java deleted file mode 100644 index a9381fa199..0000000000 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/ConsensusContextFactory.java +++ /dev/null @@ -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); -} diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/ProtocolContext.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/ProtocolContext.java index 53197ea9c5..33897c3e1e 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/ProtocolContext.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/ProtocolContext.java @@ -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. * diff --git a/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/BlockchainSetupUtil.java b/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/BlockchainSetupUtil.java index b7608bb5de..3391a0e162 100644 --- a/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/BlockchainSetupUtil.java +++ b/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/BlockchainSetupUtil.java @@ -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 as(final Class klass) { - return null; - } - }, - new BadBlockManager()); + blockchain, worldStateArchive, new ConsensusContextFixture(), new BadBlockManager()); } private static BlockchainSetupUtil create( diff --git a/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/ConsensusContextFixture.java b/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/ConsensusContextFixture.java new file mode 100644 index 0000000000..198b3ab813 --- /dev/null +++ b/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/ConsensusContextFixture.java @@ -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 as(final Class klass) { + return klass.cast(this); + } +} diff --git a/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/ExecutionContextTestFixture.java b/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/ExecutionContextTestFixture.java index b2b979e283..b4eaec04d3 100644 --- a/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/ExecutionContextTestFixture.java +++ b/ethereum/core/src/test-support/java/org/hyperledger/besu/ethereum/core/ExecutionContextTestFixture.java @@ -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()); } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/AbstractIsolationTests.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/AbstractIsolationTests.java index cfb3094726..275adc9ff4 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/AbstractIsolationTests.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/trie/diffbased/bonsai/AbstractIsolationTests.java @@ -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 = diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullSyncTargetManagerTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullSyncTargetManagerTest.java index 027bd270d2..9132842ccb 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullSyncTargetManagerTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/fullsync/FullSyncTargetManagerTest.java @@ -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, diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskParameterizedTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskParameterizedTest.java index 73d5e5138b..5686b4ae90 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskParameterizedTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskParameterizedTest.java @@ -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 task = DetermineCommonAncestorTask.create( diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskTest.java index 1b19a076d2..f85e4dd31c 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/tasks/DetermineCommonAncestorTaskTest.java @@ -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 diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/TestNode.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/TestNode.java index 0081dc7d94..d042996ce7 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/TestNode.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/transactions/TestNode.java @@ -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); diff --git a/ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/BlockchainReferenceTestCaseSpec.java b/ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/BlockchainReferenceTestCaseSpec.java index 7ab0091090..771ae4672a 100644 --- a/ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/BlockchainReferenceTestCaseSpec.java +++ b/ethereum/referencetests/src/main/java/org/hyperledger/besu/ethereum/referencetests/BlockchainReferenceTestCaseSpec.java @@ -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() {