Skip to content

Commit

Permalink
chore: Sign states only when app says so (#17607)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Brandstätter <[email protected]>
  • Loading branch information
timo0 authored Feb 3, 2025
1 parent 5377ab4 commit 2bd8919
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 27 deletions.
2 changes: 1 addition & 1 deletion platform-sdk/docs/core/wiring-diagram.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -1228,10 +1228,16 @@ public void onStateInitialized(
}
}

/**
* For every 3 consensus rounds, seal the consensus round.
*
* @param round the current consensus round
* @param state the current state of the platform testing tool
* @return {@code true} every 3 consensus rounds
*/
@Override
public boolean onSealConsensusRound(@NonNull Round round, @NonNull PlatformTestingToolState state) {
// no-op
return true;
return round.getRoundNum() % 3 == 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,25 +279,28 @@ private StateAndRound createSignedState(
// Let the swirld state manager know we are about to write the saved state for the freeze period
swirldStateManager.savedStateInFreezePeriod();
}

final boolean isBoundary = swirldStateManager.sealConsensusRound(consensusRound);
if (isBoundary) {
// This logic to be completed in https://github.com/hashgraph/hedera-services/issues/17480
final ReservedSignedState reservedSignedState;
if (isBoundary || freezeRoundReceived) {
handlerMetrics.setPhase(GETTING_STATE_TO_SIGN);
final PlatformMerkleStateRoot immutableStateCons = swirldStateManager.getStateForSigning();

handlerMetrics.setPhase(CREATING_SIGNED_STATE);
final SignedState signedState = new SignedState(
platformContext.getConfiguration(),
CryptoStatic::verifySignature,
immutableStateCons,
"TransactionHandler.createSignedState()",
freezeRoundReceived,
true,
consensusRound.isPcesRound());

reservedSignedState = signedState.reserve("transaction handler output");
} else {
reservedSignedState = null;
}

handlerMetrics.setPhase(GETTING_STATE_TO_SIGN);
final PlatformMerkleStateRoot immutableStateCons = swirldStateManager.getStateForSigning();

handlerMetrics.setPhase(CREATING_SIGNED_STATE);
final SignedState signedState = new SignedState(
platformContext.getConfiguration(),
CryptoStatic::verifySignature,
immutableStateCons,
"TransactionHandler.createSignedState()",
freezeRoundReceived,
true,
consensusRound.isPcesRound());

final ReservedSignedState reservedSignedState = signedState.reserve("transaction handler output");
return new StateAndRound(reservedSignedState, consensusRound, systemTransactions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ private void wire() {

final OutputWire<StateAndRound> transactionHandlerStateAndRoundOutput = transactionHandlerWiring
.getOutputWire()
.buildFilter("notNullStateFilter", "state and round", ras -> ras.reservedSignedState() != null)
.buildAdvancedTransformer(new StateAndRoundReserver("postHandler_stateAndRoundReserver"));

final OutputWire<ReservedSignedState> transactionHandlerRoundOutput =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@
import com.swirlds.platform.internal.ConsensusRound;
import com.swirlds.platform.state.signed.ReservedSignedState;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Objects;
import java.util.Queue;

/**
* Contains a reserved signed state, and the consensus round which caused the state to be created
*
* @param reservedSignedState the state
* @param reservedSignedState the state may null, if the round is not sealed yet
* @param round the round that caused the state to be created
* @param systemTransactions the system transactions that were included in the round
*/
public record StateAndRound(
@NonNull ReservedSignedState reservedSignedState,
@Nullable ReservedSignedState reservedSignedState,
@NonNull ConsensusRound round,
@NonNull Queue<ScopedSystemTransaction<StateSignatureTransaction>> systemTransactions) {
/**
Expand All @@ -42,6 +44,7 @@ public record StateAndRound(
*/
@NonNull
public StateAndRound makeAdditionalReservation(@NonNull final String reservationReason) {
Objects.requireNonNull(reservedSignedState, "reservedSignedState cannot be null");
return new StateAndRound(reservedSignedState.getAndReserve(reservationReason), round, systemTransactions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
SCRIPT_PATH="$(dirname "$(readlink -f "$0")")"

# You must install mermaid to use this script.
# npm install -g @mermaid-js/mermaid-cli
# npm install -g @mermaid-js/[email protected]
# With newer versions, the diagram generated may not look as expected.

# Add the flag "--less-mystery" to add back labels for mystery input wires (noisy diagram warning)

Expand All @@ -26,7 +27,6 @@ pcli diagram \
-s 'getStatusAction:PlatformStatusAction:💀' \
-s 'LatestCompleteStateNotifier:complete state notification:💢' \
-s 'RunningEventHashOverride:hash override:💨' \
-s 'TransactionResubmitterSplitter:submit transaction:♻️' \
-s 'StaleEventDetectorRouter:publishStaleEvent:⚰️' \
-s 'toStateWrittenToDiskAction:PlatformStatusAction:💾' \
-s 'StatusStateMachine:PlatformStatus:🚦' \
Expand All @@ -38,9 +38,9 @@ pcli diagram \
-g 'State File Management:State Snapshot Manager,📀,💾' \
-g 'State Signature Collector:StateSignatureCollector,reservedStateSplitter,allStatesReserver,completeStateFilter,completeStatesReserver,LatestCompleteStateNotifier' \
-g 'State Signature Collection:State Signature Collector,LatestCompleteStateNexus,💢' \
-g 'Transaction Resubmitter:TransactionResubmitter,TransactionResubmitterSplitter' \
-g 'Transaction Resubmitter:TransactionResubmitter' \
-g 'Stale Event Detector:StaleEventDetector,StaleEventDetectorSplitter,StaleEventDetectorRouter' \
-g 'Event Creation:EventCreationManager,TransactionPool,SelfEventSigner,Stale Event Detector,Transaction Resubmitter,⚰️,♻️' \
-g 'Event Creation:EventCreationManager,TransactionPool,SelfEventSigner,Stale Event Detector,Transaction Resubmitter,⚰️' \
-g 'ISS Detector:IssDetector,IssDetectorSplitter,IssHandler,getStatusAction' \
-g 'PCES Replay:pcesReplayer,✅' \
-g 'Transaction Handler:TransactionHandler,postHandler_stateAndRoundReserver,getState,SavedStateController' \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ public TransactionHandlerTester(final AddressBook addressBook) {
platformState = new PlatformStateValueAccumulator();

final PlatformMerkleStateRoot consensusState = mock(PlatformMerkleStateRoot.class);
stateLifecycles = mock(StateLifecycles.class);
;
when(consensusState.copy()).thenReturn(consensusState);
when(consensusState.getReadablePlatformState()).thenReturn(platformState);
when(consensusState.getWritablePlatformState()).thenReturn(platformState);

stateLifecycles = mock(StateLifecycles.class);
when(stateLifecycles.onSealConsensusRound(any(), any())).thenReturn(true);

doAnswer(i -> {
handledRounds.add(i.getArgument(0));
return null;
Expand Down

0 comments on commit 2bd8919

Please sign in to comment.