Skip to content

Commit

Permalink
Merge branch 'main' into testnet-praguetime
Browse files Browse the repository at this point in the history
  • Loading branch information
siladu authored Feb 6, 2025
2 parents 639c426 + b24ff90 commit 195feb7
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
public class MergeBesuControllerBuilder extends BesuControllerBuilder {
private final AtomicReference<SyncState> syncState = new AtomicReference<>();
private static final Logger LOG = LoggerFactory.getLogger(MergeBesuControllerBuilder.class);
private final PostMergeContext postMergeContext = new PostMergeContext();

/** Default constructor. */
public MergeBesuControllerBuilder() {}
Expand Down Expand Up @@ -198,7 +199,7 @@ protected MergeContext createConsensusContext(
&& blockchain.getGenesisBlockHeader().getDifficulty().isZero();

final MergeContext mergeContext =
PostMergeContext.get()
postMergeContext
.setSyncState(syncState.get())
.setTerminalTotalDifficulty(
genesisConfigOptions
Expand Down Expand Up @@ -261,7 +262,16 @@ protected List<PeerValidator> createPeerValidators(
@Override
public BesuController build() {
final BesuController controller = super.build();
PostMergeContext.get().setSyncState(controller.getSyncState());
postMergeContext.setSyncState(syncState.get());
return controller;
}

/**
* Gets post merge context.
*
* @return the post merge context
*/
public PostMergeContext getPostMergeContext() {
return postMergeContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ protected MiningCoordinator createMiningCoordinator(
transitionMiningConfiguration,
syncState,
transitionBackwardsSyncContext,
ethProtocolManager.ethContext().getScheduler()));
ethProtocolManager.ethContext().getScheduler()),
mergeBesuControllerBuilder.getPostMergeContext());
initTransitionWatcher(protocolContext, composedCoordinator);
return composedCoordinator;
}
Expand Down Expand Up @@ -185,7 +186,7 @@ protected ProtocolSchedule createProtocolSchedule() {
new TransitionProtocolSchedule(
preMergeBesuControllerBuilder.createProtocolSchedule(),
mergeBesuControllerBuilder.createProtocolSchedule(),
PostMergeContext.get());
mergeBesuControllerBuilder.getPostMergeContext());
return transitionProtocolSchedule;
}

Expand Down Expand Up @@ -255,7 +256,7 @@ protected DefaultSynchronizer createSynchronizer(
private void initTransitionWatcher(
final ProtocolContext protocolContext, final TransitionCoordinator composedCoordinator) {

PostMergeContext postMergeContext = protocolContext.getConsensusContext(PostMergeContext.class);
PostMergeContext postMergeContext = mergeBesuControllerBuilder.getPostMergeContext();
postMergeContext.observeNewIsPostMergeState(
(isPoS, priorState, difficultyStoppedAt) -> {
if (isPoS) {
Expand Down Expand Up @@ -290,7 +291,7 @@ public BesuControllerBuilder storageProvider(final StorageProvider storageProvid
@Override
public BesuController build() {
final BesuController controller = super.build();
PostMergeContext.get().setSyncState(controller.getSyncState());
mergeBesuControllerBuilder.getPostMergeContext().setSyncState(controller.getSyncState());
return controller;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

@ExtendWith(MockitoExtension.class)
public class ForkIdsNetworkConfigTest {
private static final PostMergeContext postMergeContext = new PostMergeContext();

public static Collection<Object[]> parameters() {
return List.of(
Expand Down Expand Up @@ -203,10 +204,10 @@ public static class MilestoneStreamingTransitionProtocolSchedule
public MilestoneStreamingTransitionProtocolSchedule(
final MilestoneStreamingProtocolSchedule preMergeProtocolSchedule,
final MilestoneStreamingProtocolSchedule postMergeProtocolSchedule) {
super(preMergeProtocolSchedule, postMergeProtocolSchedule, PostMergeContext.get());
super(preMergeProtocolSchedule, postMergeProtocolSchedule, postMergeContext);
transitionUtils =
new TransitionUtils<>(
preMergeProtocolSchedule, postMergeProtocolSchedule, PostMergeContext.get());
preMergeProtocolSchedule, postMergeProtocolSchedule, postMergeContext);
}

public Stream<Long> streamMilestoneBlocks() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.EvictingQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -40,7 +39,6 @@ public class PostMergeContext implements MergeContext {
/** The Max blocks in progress. */
static final int MAX_BLOCKS_IN_PROGRESS = 12;

private static final AtomicReference<PostMergeContext> singleton = new AtomicReference<>();
private final AtomicReference<SyncState> syncState;
private final AtomicReference<Difficulty> terminalTotalDifficulty;
// initial postMerge state is indeterminate until it is set:
Expand All @@ -62,8 +60,7 @@ public class PostMergeContext implements MergeContext {
private boolean isPostMergeAtGenesis;

/** Instantiates a new Post merge context. */
@VisibleForTesting
PostMergeContext() {
public PostMergeContext() {
this(Difficulty.ZERO);
}

Expand All @@ -72,24 +69,11 @@ public class PostMergeContext implements MergeContext {
*
* @param difficulty the difficulty
*/
@VisibleForTesting
PostMergeContext(final Difficulty difficulty) {
private PostMergeContext(final Difficulty difficulty) {
this.terminalTotalDifficulty = new AtomicReference<>(difficulty);
this.syncState = new AtomicReference<>();
}

/**
* Get post merge context.
*
* @return the post merge context
*/
public static PostMergeContext get() {
if (singleton.get() == null) {
singleton.compareAndSet(null, new PostMergeContext());
}
return singleton.get();
}

@Override
public <C extends ConsensusContext> C as(final Class<C> klass) {
return klass.cast(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,16 @@
*/
package org.hyperledger.besu.consensus.merge;

import org.hyperledger.besu.config.GenesisConfigOptions;
import org.hyperledger.besu.datatypes.HardforkId;
import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.chain.BadBlockManager;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Difficulty;
import org.hyperledger.besu.ethereum.core.MiningConfiguration;
import org.hyperledger.besu.ethereum.core.PermissionTransactionFilter;
import org.hyperledger.besu.ethereum.core.ProcessableBlockHeader;
import org.hyperledger.besu.ethereum.mainnet.MainnetProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.mainnet.ProtocolSpec;
import org.hyperledger.besu.ethereum.mainnet.ScheduledProtocolSpec;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.plugin.services.MetricsSystem;

import java.math.BigInteger;
import java.util.Optional;
Expand Down Expand Up @@ -60,41 +55,6 @@ public TransitionProtocolSchedule(
new TransitionUtils<>(preMergeProtocolSchedule, postMergeProtocolSchedule, mergeContext);
}

/**
* Create a Proof-of-Stake protocol schedule from a config object
*
* @param genesisConfigOptions {@link GenesisConfigOptions} containing the config options for the
* milestone starting points
* @param miningConfiguration the mining parameters
* @param badBlockManager the cache to use to keep invalid blocks
* @param isParallelTxProcessingEnabled indicates whether parallel transaction is enabled.
* @return an initialised TransitionProtocolSchedule using post-merge defaults
*/
public static TransitionProtocolSchedule fromConfig(
final GenesisConfigOptions genesisConfigOptions,
final MiningConfiguration miningConfiguration,
final BadBlockManager badBlockManager,
final boolean isParallelTxProcessingEnabled,
final MetricsSystem metricsSystem) {
ProtocolSchedule preMergeProtocolSchedule =
MainnetProtocolSchedule.fromConfig(
genesisConfigOptions,
miningConfiguration,
badBlockManager,
isParallelTxProcessingEnabled,
metricsSystem);
ProtocolSchedule postMergeProtocolSchedule =
MergeProtocolSchedule.create(
genesisConfigOptions,
false,
miningConfiguration,
badBlockManager,
isParallelTxProcessingEnabled,
metricsSystem);
return new TransitionProtocolSchedule(
preMergeProtocolSchedule, postMergeProtocolSchedule, PostMergeContext.get());
}

/**
* Gets pre merge schedule.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ public class TransitionCoordinator extends TransitionUtils<MiningCoordinator>
*
* @param miningCoordinator the mining coordinator
* @param mergeCoordinator the merge coordinator
* @param postMergeContext the post merge context
*/
public TransitionCoordinator(
final MiningCoordinator miningCoordinator, final MiningCoordinator mergeCoordinator) {
super(miningCoordinator, mergeCoordinator, PostMergeContext.get());
final MiningCoordinator miningCoordinator,
final MiningCoordinator mergeCoordinator,
final PostMergeContext postMergeContext) {
super(miningCoordinator, mergeCoordinator, postMergeContext);
this.miningCoordinator = miningCoordinator;
this.mergeCoordinator = (MergeMiningCoordinator) mergeCoordinator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class MergeReorgTest implements MergeGenesisConfigHelper {

private MergeCoordinator coordinator;

private final MergeContext mergeContext = PostMergeContext.get();
private final MergeContext mergeContext = new PostMergeContext();
private final ProtocolSchedule mockProtocolSchedule = getMergeProtocolSchedule();
private final GenesisState genesisState =
GenesisState.fromConfig(getPowGenesisConfig(), mockProtocolSchedule);
Expand Down

0 comments on commit 195feb7

Please sign in to comment.