Skip to content

Commit

Permalink
Don't start BFT mining coordinators until initial sync has completed
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Whitehead <matthew1001@gmail.com>
  • Loading branch information
matthew1001 committed Sep 12, 2023
1 parent ad7bd96 commit a2bcc2a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule;
import org.hyperledger.besu.ethereum.p2p.config.SubProtocolConfiguration;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.plugin.services.BesuEvents;
import org.hyperledger.besu.util.Subscribers;

import java.util.HashMap;
Expand Down Expand Up @@ -231,7 +232,30 @@ protected MiningCoordinator createMiningCoordinator(
blockCreatorFactory,
blockchain,
bftEventQueue);
ibftMiningCoordinator.enable();

if (syncState.isInitialSyncPhaseDone()) {
LOG.info("Starting IBFT mining coordinator");
ibftMiningCoordinator.enable();
ibftMiningCoordinator.start();
} else {
LOG.info("IBFT mining coordinator not starting while initial sync in progress");
}

syncState.subscribeCompletionReached(
new BesuEvents.InitialSyncCompletionListener() {
@Override
public void onInitialSyncCompleted() {
LOG.info("Starting IBFT mining coordinator following initial sync");
ibftMiningCoordinator.enable();
ibftMiningCoordinator.start();
}

@Override
public void onInitialSyncRestart() {
// Nothing to do. The mining coordinator won't be started until
// sync has completed.
}
});

return ibftMiningCoordinator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import org.hyperledger.besu.ethereum.p2p.config.SubProtocolConfiguration;
import org.hyperledger.besu.ethereum.transaction.TransactionSimulator;
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive;
import org.hyperledger.besu.plugin.services.BesuEvents;
import org.hyperledger.besu.util.Subscribers;

import java.util.HashMap;
Expand Down Expand Up @@ -271,7 +272,30 @@ protected MiningCoordinator createMiningCoordinator(
blockCreatorFactory,
blockchain,
bftEventQueue);
miningCoordinator.enable();

if (syncState.isInitialSyncPhaseDone()) {
LOG.info("Starting QBFT mining coordinator");
miningCoordinator.enable();
miningCoordinator.start();
} else {
LOG.info("QBFT mining coordinator not starting while initial sync in progress");
}

syncState.subscribeCompletionReached(
new BesuEvents.InitialSyncCompletionListener() {
@Override
public void onInitialSyncCompleted() {
LOG.info("Starting QBFT mining coordinator following initial sync");
miningCoordinator.enable();
miningCoordinator.start();
}

@Override
public void onInitialSyncRestart() {
// Nothing to do. The mining coordinator won't be started until
// sync has completed.
}
});

return miningCoordinator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ private enum State {
/** Running state. */
RUNNING,
/** Stopped state. */
STOPPED
STOPPED,
/** Paused state. */
PAUSED,
}

private static final Logger LOG = LoggerFactory.getLogger(BftMiningCoordinator.class);
Expand All @@ -61,7 +63,7 @@ private enum State {
private final BftExecutors bftExecutors;

private long blockAddedObserverId;
private final AtomicReference<State> state = new AtomicReference<>(State.IDLE);
private final AtomicReference<State> state = new AtomicReference<>(State.PAUSED);

/**
* Instantiates a new Bft mining coordinator.
Expand Down Expand Up @@ -122,7 +124,11 @@ public void awaitStop() throws InterruptedException {

@Override
public boolean enable() {
return true;
// Return true if we're already running, or successfully switch to running
if (state.get() == State.RUNNING || state.compareAndSet(State.PAUSED, State.IDLE)) {
return true;
}
return false;
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4384,6 +4384,11 @@
<sha256 value="5dbad4676c7ffa80cf71f63e1b045d05d5e0c912a597d3cef1c28e9e2bf3bb16" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.eclipse.platform" name="org.eclipse.core.expressions" version="3.9.100">
<artifact name="org.eclipse.core.expressions-3.9.100.pom">
<sha256 value="5f7e01a94adbd52546c0d03af525697197165e36b73d914c6a05e3aab8905e6e" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.eclipse.platform" name="org.eclipse.core.filesystem" version="1.9.100">
<artifact name="org.eclipse.core.filesystem-1.9.100.jar">
<sha256 value="36e802c4d9a2c864e7d6b22b8d06f59927d113475ea04b3fd9bf6312d5a97ff6" origin="Generated by Gradle"/>
Expand Down Expand Up @@ -4482,6 +4487,11 @@
<sha256 value="87edfca927216638d1ebb3168cee8821e7e81d4a459325765040d8308d09ba38" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.eclipse.platform" name="org.eclipse.swt" version="3.124.100">
<artifact name="org.eclipse.swt-3.124.100.pom">
<sha256 value="e6a79e8bc53281ecbe3433a52c713b75ba8fa06e03b9f279b027f68ba078085e" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.eclipse.platform" name="org.eclipse.text" version="3.12.0">
<artifact name="org.eclipse.text-3.12.0.jar">
<sha256 value="457c1f8af07e870acce65130a2d9aa1e0fd95c92a7667bbd2db5bf7f9f19dd31" origin="Generated by Gradle"/>
Expand Down

0 comments on commit a2bcc2a

Please sign in to comment.