Skip to content

Commit

Permalink
Simplify ProtocolContext creation (hyperledger#7792)
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 authored Nov 19, 2024
1 parent dc81641 commit 833a5ce
Show file tree
Hide file tree
Showing 25 changed files with 127 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
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;
Expand Down Expand Up @@ -605,9 +604,11 @@ public BesuController build() {
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(
Expand Down Expand Up @@ -976,7 +977,7 @@ protected SubProtocolConfiguration createSubProtocolConfiguration(
}

/**
* Create mining coordinator mining coordinator.
* Create mining coordinator.
*
* @param protocolSchedule the protocol schedule
* @param protocolContext the protocol context
Expand Down Expand Up @@ -1017,9 +1018,9 @@ protected void validateContext(final ProtocolContext context) {}
* @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.
Expand Down Expand Up @@ -1079,17 +1080,14 @@ protected EthProtocolManager createEthProtocolManager(
*
* @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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
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;
Expand Down Expand Up @@ -168,10 +167,12 @@ protected ProtocolSchedule createProtocolSchedule() {
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
Expand All @@ -188,10 +189,10 @@ protected ConsensusContext createConsensusContext(
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ public QbftBesuControllerBuilder() {}

@Override
protected Supplier<BftExtraDataCodec> bftExtraDataCodec() {
return Suppliers.memoize(
() -> {
return new QbftExtraDataCodec();
});
return Suppliers.memoize(QbftExtraDataCodec::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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;
Expand Down Expand Up @@ -193,11 +192,9 @@ protected ProtocolSchedule createProtocolSchedule() {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
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;
Expand Down Expand Up @@ -166,8 +166,8 @@ public void createsMigratingMiningCoordinator() {

@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);
Expand All @@ -180,15 +180,14 @@ public void createsMigratingContext() {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
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;
Expand Down Expand Up @@ -99,25 +100,27 @@ 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,
new BadBlockManager(),
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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
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;
Expand Down Expand Up @@ -103,6 +104,7 @@ public void setup() {
GENESIS_CONFIG_OPTIONS,
new ForksSchedule<>(List.of()),
proposerNodeKey,
PrivacyParameters.DEFAULT,
false,
EvmConfiguration.DEFAULT,
MiningConfiguration.MINING_DISABLED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.hyperledger.besu.ethereum.ConsensusContext;

/** The Migrating context. */
public class MigratingContext implements ConsensusContext {
public class MigratingConsensusContext implements ConsensusContext {

private final ForksSchedule<ConsensusContext> consensusContextSchedule;

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ public void returnsContextForSpecificChainHeight() {

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -82,7 +83,9 @@ public JsonRpcTestMethodsFactory(final BlockchainImporter importer) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -67,7 +68,8 @@ public static void setUpOnce() throws Exception {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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;
Expand Down Expand Up @@ -110,7 +111,10 @@ public void setupTest() throws Exception {
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(),
Expand Down
Loading

0 comments on commit 833a5ce

Please sign in to comment.