Skip to content

Commit 970250e

Browse files
authored
feat: disentangle consensus and gossip (#12428)
Signed-off-by: Cody Littley <[email protected]>
1 parent c781d6b commit 970250e

40 files changed

+909
-575
lines changed

platform-sdk/docs/core/wiring-diagram.svg

+1-1
Loading

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/Consensus.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.swirlds.platform.internal.ConsensusRound;
2323
import com.swirlds.platform.internal.EventImpl;
2424
import edu.umd.cs.findbugs.annotations.NonNull;
25-
import edu.umd.cs.findbugs.annotations.Nullable;
2625
import java.util.List;
2726

2827
/** An interface for classes that calculate consensus of events */
@@ -33,15 +32,15 @@ public interface Consensus extends GraphGenerations, RoundNumberProvider {
3332
*
3433
* @param event the event to be added
3534
* @return A list of consensus rounds, each with a list of consensus events (that can be empty). The rounds are
36-
* stored in consensus order (round at index 0 occurs before the round at index 1 in consensus time). Returns null
37-
* if no consensus was reached
35+
* stored in consensus order (round at index 0 occurs before the round at index 1 in consensus time). Returns an
36+
* empty list if no rounds reached consensus.
3837
*/
39-
@Nullable
38+
@NonNull
4039
List<ConsensusRound> addEvent(@NonNull EventImpl event);
4140

4241
/**
43-
* Load consensus from a snapshot. This will continue consensus from the round of the snapshot
44-
* once all the required events are provided. This method is called at restart and reconnect boundaries.
42+
* Load consensus from a snapshot. This will continue consensus from the round of the snapshot once all the required
43+
* events are provided. This method is called at restart and reconnect boundaries.
4544
*/
4645
void loadSnapshot(@NonNull ConsensusSnapshot snapshot);
4746
}

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/ConsensusImpl.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -251,23 +251,24 @@ private void reset() {
251251
* round.
252252
*
253253
* @param event the event to be added
254-
* @return A list of consensus rounds, or null if no consensus was reached
254+
* @return A list of consensus rounds or an empty list if no consensus was reached
255255
*/
256+
@NonNull
256257
@Override
257-
public @Nullable List<ConsensusRound> addEvent(@NonNull final EventImpl event) {
258+
public List<ConsensusRound> addEvent(@NonNull final EventImpl event) {
258259
recentEvents.add(event);
259-
final List<ConsensusRound> toReturn = new ArrayList<>();
260+
final List<ConsensusRound> rounds = new ArrayList<>();
260261
// set its round to undefined so that it gets calculated
261262
event.setRoundCreated(ConsensusConstants.ROUND_UNDEFINED);
262263
checkInitJudges(event);
263264
ConsensusRound consensusRound = calculateAndVote(event);
264265

265266
while (consensusRound != null) {
266-
toReturn.add(consensusRound);
267+
rounds.add(consensusRound);
267268

268269
consensusRound = recalculateAndVote();
269270
}
270-
return toReturn.isEmpty() ? null : toReturn;
271+
return rounds;
271272
}
272273

273274
/**

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/SwirldsPlatform.java

+8-19
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@
6262
import com.swirlds.platform.builder.PlatformBuildingBlocks;
6363
import com.swirlds.platform.builder.PlatformComponentBuilder;
6464
import com.swirlds.platform.components.AppNotifier;
65-
import com.swirlds.platform.components.ConsensusEngine;
6665
import com.swirlds.platform.components.DefaultAppNotifier;
67-
import com.swirlds.platform.components.DefaultConsensusEngine;
6866
import com.swirlds.platform.components.DefaultEventWindowManager;
6967
import com.swirlds.platform.components.DefaultSavedStateController;
7068
import com.swirlds.platform.components.EventWindowManager;
7169
import com.swirlds.platform.components.SavedStateController;
7270
import com.swirlds.platform.components.appcomm.DefaultLatestCompleteStateNotifier;
7371
import com.swirlds.platform.components.appcomm.LatestCompleteStateNotifier;
72+
import com.swirlds.platform.components.consensus.ConsensusEngine;
73+
import com.swirlds.platform.components.consensus.DefaultConsensusEngine;
7474
import com.swirlds.platform.config.StateConfig;
7575
import com.swirlds.platform.config.TransactionConfig;
7676
import com.swirlds.platform.consensus.ConsensusSnapshot;
@@ -82,6 +82,7 @@
8282
import com.swirlds.platform.event.EventCounter;
8383
import com.swirlds.platform.event.GossipEvent;
8484
import com.swirlds.platform.event.creation.EventCreationManager;
85+
import com.swirlds.platform.event.linking.GossipLinker;
8586
import com.swirlds.platform.event.linking.InOrderLinker;
8687
import com.swirlds.platform.event.orphan.OrphanBuffer;
8788
import com.swirlds.platform.event.preconsensus.DefaultPcesSequencer;
@@ -109,8 +110,6 @@
109110
import com.swirlds.platform.listeners.PlatformStatusChangeNotification;
110111
import com.swirlds.platform.listeners.ReconnectCompleteNotification;
111112
import com.swirlds.platform.listeners.StateLoadedFromDiskNotification;
112-
import com.swirlds.platform.metrics.ConsensusMetrics;
113-
import com.swirlds.platform.metrics.ConsensusMetricsImpl;
114113
import com.swirlds.platform.metrics.RuntimeMetrics;
115114
import com.swirlds.platform.metrics.SwirldStateMetrics;
116115
import com.swirlds.platform.metrics.SyncMetrics;
@@ -174,7 +173,6 @@
174173
import java.util.Objects;
175174
import java.util.concurrent.ExecutionException;
176175
import java.util.concurrent.atomic.AtomicLong;
177-
import java.util.concurrent.atomic.AtomicReference;
178176
import java.util.function.Consumer;
179177
import java.util.function.LongSupplier;
180178
import org.apache.logging.log4j.LogManager;
@@ -197,12 +195,6 @@ public class SwirldsPlatform implements Platform {
197195
*/
198196
private final Shadowgraph shadowGraph;
199197

200-
/**
201-
* the object used to calculate consensus. it is volatile because the whole object is replaced when reading a state
202-
* from disk or getting it through reconnect
203-
*/
204-
private final AtomicReference<Consensus> consensusRef = new AtomicReference<>();
205-
206198
/**
207199
* the current nodes in the network and their information
208200
*/
@@ -394,12 +386,10 @@ public SwirldsPlatform(@NonNull final PlatformComponentBuilder builder) {
394386

395387
registerAddressBookMetrics(metrics, currentAddressBook, selfId);
396388

397-
final ConsensusMetrics consensusMetrics = new ConsensusMetricsImpl(selfId, metrics);
398-
399389
final SyncMetrics syncMetrics = new SyncMetrics(metrics);
400390
RuntimeMetrics.setup(metrics);
401391

402-
shadowGraph = new Shadowgraph(platformContext, currentAddressBook);
392+
shadowGraph = new Shadowgraph(platformContext, currentAddressBook, blocks.intakeEventCounter());
403393

404394
final EventConfig eventConfig = platformContext.getConfiguration().getConfigData(EventConfig.class);
405395

@@ -591,10 +581,11 @@ public SwirldsPlatform(@NonNull final PlatformComponentBuilder builder) {
591581
initialState.getState().getPlatformState().getPreviousAddressBook(),
592582
currentAddressBook,
593583
blocks.intakeEventCounter());
584+
594585
final OrphanBuffer orphanBuffer = new OrphanBuffer(platformContext, blocks.intakeEventCounter());
595-
final InOrderLinker inOrderLinker = new InOrderLinker(platformContext, time, blocks.intakeEventCounter());
596-
final ConsensusEngine consensusEngine = new DefaultConsensusEngine(
597-
platformContext, selfId, consensusRef::get, shadowGraph, blocks.intakeEventCounter(), e -> {});
586+
final InOrderLinker inOrderLinker = new GossipLinker(platformContext, blocks.intakeEventCounter());
587+
588+
final ConsensusEngine consensusEngine = new DefaultConsensusEngine(platformContext, currentAddressBook, selfId);
598589

599590
final LongSupplier intakeQueueSizeSupplier =
600591
oldStyleIntakeQueue == null ? platformWiring.getIntakeQueueSizeSupplier() : oldStyleIntakeQueue::size;
@@ -717,8 +708,6 @@ public SwirldsPlatform(@NonNull final PlatformComponentBuilder builder) {
717708
blocks.intakeEventCounter(),
718709
() -> emergencyState.getState("emergency reconnect")) {};
719710

720-
consensusRef.set(new ConsensusImpl(platformContext, consensusMetrics, getAddressBook()));
721-
722711
latestImmutableStateNexus.setState(initialState.reserve("set latest immutable to initial state"));
723712

724713
if (startedFromGenesis) {

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/components/AppNotifier.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,39 @@ public interface AppNotifier {
3434
*
3535
* @param notification the notification
3636
*/
37-
@InputWireLabel("StateWriteToDiskCompleteNotification")
37+
@InputWireLabel("state written notification")
3838
void sendStateWrittenToDiskNotification(@NonNull final StateWriteToDiskCompleteNotification notification);
3939

4040
/**
4141
* Send a notification to the app that the state has been loaded from disk.
4242
*
4343
* @param notification the notification
4444
*/
45-
@InputWireLabel("StateLoadedFromDiskNotification")
45+
@InputWireLabel("state loaded notification")
4646
void sendStateLoadedFromDiskNotification(@NonNull final StateLoadedFromDiskNotification notification);
4747

4848
/**
4949
* Send a notification to the app that a reconnect has completed.
5050
*
5151
* @param notification the notification
5252
*/
53-
@InputWireLabel("ReconnectCompleteNotification")
53+
@InputWireLabel("reconnect notification")
5454
void sendReconnectCompleteNotification(@NonNull final ReconnectCompleteNotification notification);
5555

5656
/**
5757
* Send a notification to the app that the platform status has changed.
5858
*
5959
* @param notification the notification
6060
*/
61-
@InputWireLabel("PlatformStatusChangeNotification")
61+
@InputWireLabel("platform status notification")
6262
void sendPlatformStatusChangeNotification(@NonNull final PlatformStatusChangeNotification notification);
6363

6464
/**
6565
* Send a notification to the app with the latest complete state.
6666
*
6767
* @param notificationWithCleanup the notification, with required cleanup
6868
*/
69-
@InputWireLabel("CompleteStateNotificationWithCleanup")
69+
@InputWireLabel("complete state notification")
7070
void sendLatestCompleteStateNotification(
7171
@NonNull final CompleteStateNotificationWithCleanup notificationWithCleanup);
7272

@@ -75,6 +75,6 @@ void sendLatestCompleteStateNotification(
7575
*
7676
* @param notification the notification
7777
*/
78-
@InputWireLabel("IssNotification")
78+
@InputWireLabel("Iss Notification")
7979
void sendIssNotification(@NonNull final IssNotification notification);
8080
}

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/components/DefaultConsensusEngine.java

-163
This file was deleted.

platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/components/ConsensusEngine.java renamed to platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/components/consensus/ConsensusEngine.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.swirlds.platform.components;
17+
package com.swirlds.platform.components.consensus;
1818

1919
import com.swirlds.common.wiring.component.InputWireLabel;
2020
import com.swirlds.platform.Consensus;
2121
import com.swirlds.platform.consensus.ConsensusSnapshot;
22+
import com.swirlds.platform.event.GossipEvent;
2223
import com.swirlds.platform.internal.ConsensusRound;
2324
import com.swirlds.platform.internal.EventImpl;
2425
import edu.umd.cs.findbugs.annotations.NonNull;
@@ -35,8 +36,8 @@ public interface ConsensusEngine {
3536
* @return a list of rounds that came to consensus as a result of adding the event
3637
*/
3738
@NonNull
38-
@InputWireLabel("EventImpl")
39-
List<ConsensusRound> addEvent(@NonNull EventImpl event);
39+
@InputWireLabel("GossipEvent")
40+
List<ConsensusRound> addEvent(@NonNull GossipEvent event);
4041

4142
/**
4243
* Perform an out-of-band snapshot update. This happens at restart/reconnect boundaries.

0 commit comments

Comments
 (0)