Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: merge the dual state into the platform state #10429

Merged
merged 20 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@
import com.swirlds.common.crypto.CryptographyHolder;
import com.swirlds.common.platform.NodeId;
import com.swirlds.platform.listeners.PlatformStatusChangeListener;
import com.swirlds.platform.state.PlatformState;
import com.swirlds.platform.system.InitTrigger;
import com.swirlds.platform.system.Platform;
import com.swirlds.platform.system.Round;
import com.swirlds.platform.system.SoftwareVersion;
import com.swirlds.platform.system.SwirldDualState;
import com.swirlds.platform.system.SwirldMain;
import com.swirlds.platform.system.SwirldState;
import com.swirlds.platform.system.events.Event;
Expand Down Expand Up @@ -370,7 +370,7 @@
private void onStateInitialized(
@NonNull final MerkleHederaState state,
@NonNull final Platform platform,
@NonNull final SwirldDualState dualState,
@NonNull final PlatformState platformState,
@NonNull final InitTrigger trigger,
@Nullable final SoftwareVersion previousVersion) {
// Initialize the configuration from disk. We must do this BEFORE we run migration, because the various
Expand Down Expand Up @@ -407,7 +407,7 @@
}

//noinspection ConstantValue
assert dualState != null : "Platform should never pass a null dual state";
assert platformState != null : "Platform should never pass a null platform state";
logger.info(
"Initializing Hedera state with trigger {} and previous version {}",
() -> trigger,
Expand Down Expand Up @@ -449,11 +449,11 @@
// assertion will hold true.
assert configProvider != null : "Config Provider *must* have been set by now!";

// Some logging on what we found about freeze in the dual state
// Some logging on what we found about freeze in the platform state
logger.info(
"Dual state includes freeze time={} and last frozen={}",
dualState.getFreezeTime(),
dualState.getLastFrozenTime());
"Platform state includes freeze time={} and last frozen={}",
platformState.getFreezeTime(),
platformState.getLastFrozenTime());

Check warning on line 456 in hedera-node/hedera-app/src/main/java/com/hedera/node/app/Hedera.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-app/src/main/java/com/hedera/node/app/Hedera.java#L455-L456

Added lines #L455 - L456 were not covered by tests
}
/**
* Called by this class when we detect it is time to do migration. The {@code deserializedVersion} must not be newer
Expand Down Expand Up @@ -720,9 +720,9 @@
* called.
*/
private void onHandleConsensusRound(
@NonNull final Round round, @NonNull final SwirldDualState dualState, @NonNull final HederaState state) {
@NonNull final Round round, @NonNull final PlatformState platformState, @NonNull final HederaState state) {
daggerApp.workingStateAccessor().setHederaState(state);
daggerApp.handleWorkflow().handleRound(state, dualState, round);
daggerApp.handleWorkflow().handleRound(state, platformState, round);

Check warning on line 725 in hedera-node/hedera-app/src/main/java/com/hedera/node/app/Hedera.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-app/src/main/java/com/hedera/node/app/Hedera.java#L725

Added line #L725 was not covered by tests
}

/*==================================================================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
import com.hedera.node.app.throttle.SynchronizedThrottleAccumulator;
import com.hedera.node.app.throttle.ThrottleManager;
import com.hedera.node.app.workflows.WorkflowsInjectionModule;
import com.hedera.node.app.workflows.handle.DualStateUpdateFacility;
import com.hedera.node.app.workflows.handle.HandleWorkflow;
import com.hedera.node.app.workflows.handle.PlatformStateUpdateFacility;
import com.hedera.node.app.workflows.handle.SystemFileUpdateFacility;
import com.hedera.node.app.workflows.handle.record.GenesisRecordsConsensusHook;
import com.hedera.node.app.workflows.prehandle.PreHandleWorkflow;
Expand Down Expand Up @@ -118,7 +118,7 @@ public interface HederaInjectionComponent {

ThrottleManager throttleManager();

DualStateUpdateFacility dualStateUpdateFacility();
PlatformStateUpdateFacility platformStateUpdateFacility();

GenesisRecordsConsensusHook genesisRecordsConsensusHook();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package com.hedera.node.app.state;

import com.swirlds.platform.state.PlatformState;
import com.swirlds.platform.system.Round;
import com.swirlds.platform.system.SwirldDualState;
import edu.umd.cs.findbugs.annotations.NonNull;

/** Listener invoked for each consensus round that occurs. */
@FunctionalInterface
public interface HandleConsensusRoundListener {
void onConsensusRound(@NonNull Round round, @NonNull SwirldDualState dualState, @NonNull HederaState state);
void onConsensusRound(@NonNull Round round, @NonNull PlatformState platformState, @NonNull HederaState state);
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@
import com.swirlds.common.merkle.impl.PartialNaryMerkleInternal;
import com.swirlds.common.utility.Labeled;
import com.swirlds.merkle.map.MerkleMap;
import com.swirlds.platform.state.PlatformState;
import com.swirlds.platform.system.InitTrigger;
import com.swirlds.platform.system.Platform;
import com.swirlds.platform.system.Round;
import com.swirlds.platform.system.SoftwareVersion;
import com.swirlds.platform.system.SwirldDualState;
import com.swirlds.platform.system.SwirldMain;
import com.swirlds.platform.system.SwirldState;
import com.swirlds.platform.system.events.Event;
Expand Down Expand Up @@ -190,14 +190,14 @@
@Override
public void init(
final Platform platform,
final SwirldDualState dualState,
final PlatformState platformState,
final InitTrigger trigger,
final SoftwareVersion deserializedVersion) {
// At some point this method will no longer be defined on SwirldState2, because we want to move
// to a model where SwirldState/SwirldState2 are simply data objects, without this lifecycle.
// Instead, this method will be a callback the app registers with the platform. So for now,
// we simply call the callback handler, which is implemented by the app.
this.onInit.onStateInitialized(this, platform, dualState, trigger, deserializedVersion);
this.onInit.onStateInitialized(this, platform, platformState, trigger, deserializedVersion);

Check warning on line 200 in hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/merkle/MerkleHederaState.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-app/src/main/java/com/hedera/node/app/state/merkle/MerkleHederaState.java#L200

Added line #L200 was not covered by tests
}

/**
Expand Down Expand Up @@ -307,10 +307,10 @@
* {@inheritDoc}
*/
@Override
public void handleConsensusRound(@NonNull final Round round, @NonNull final SwirldDualState swirldDualState) {
public void handleConsensusRound(@NonNull final Round round, @NonNull final PlatformState platformState) {
throwIfImmutable();
if (onHandleConsensusRound != null) {
onHandleConsensusRound.onConsensusRound(round, swirldDualState, this);
onHandleConsensusRound.onConsensusRound(round, platformState, this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package com.hedera.node.app.state.merkle;

import com.swirlds.platform.state.PlatformState;
import com.swirlds.platform.system.InitTrigger;
import com.swirlds.platform.system.Platform;
import com.swirlds.platform.system.SoftwareVersion;
import com.swirlds.platform.system.SwirldDualState;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;

Expand All @@ -30,7 +30,7 @@ public interface OnStateInitialized {
void onStateInitialized(
@NonNull MerkleHederaState state,
@NonNull Platform platform,
@NonNull SwirldDualState dualState,
@NonNull PlatformState platformState,
@NonNull InitTrigger trigger,
@Nullable SoftwareVersion deserializedVersion);
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
import com.hedera.node.config.data.HederaConfig;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.swirlds.config.api.Configuration;
import com.swirlds.platform.state.PlatformState;
import com.swirlds.platform.system.Round;
import com.swirlds.platform.system.SwirldDualState;
import com.swirlds.platform.system.events.ConsensusEvent;
import com.swirlds.platform.system.transaction.ConsensusTransaction;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -134,7 +134,7 @@ public class HandleWorkflow {
private final ChildRecordFinalizer childRecordFinalizer;
private final ParentRecordFinalizer transactionFinalizer;
private final SystemFileUpdateFacility systemFileUpdateFacility;
private final DualStateUpdateFacility dualStateUpdateFacility;
private final PlatformStateUpdateFacility platformStateUpdateFacility;
private final SolvencyPreCheck solvencyPreCheck;
private final Authorizer authorizer;
private final NetworkUtilizationManager networkUtilizationManager;
Expand All @@ -156,7 +156,7 @@ public HandleWorkflow(
@NonNull final ChildRecordFinalizer childRecordFinalizer,
@NonNull final ParentRecordFinalizer transactionFinalizer,
@NonNull final SystemFileUpdateFacility systemFileUpdateFacility,
@NonNull final DualStateUpdateFacility dualStateUpdateFacility,
@NonNull final PlatformStateUpdateFacility platformStateUpdateFacility,
@NonNull final SolvencyPreCheck solvencyPreCheck,
@NonNull final Authorizer authorizer,
@NonNull final NetworkUtilizationManager networkUtilizationManager) {
Expand All @@ -176,8 +176,8 @@ public HandleWorkflow(
this.transactionFinalizer = requireNonNull(transactionFinalizer, "transactionFinalizer must not be null");
this.systemFileUpdateFacility =
requireNonNull(systemFileUpdateFacility, "systemFileUpdateFacility must not be null");
this.dualStateUpdateFacility =
requireNonNull(dualStateUpdateFacility, "dualStateUpdateFacility must not be null");
this.platformStateUpdateFacility =
requireNonNull(platformStateUpdateFacility, "platformStateUpdateFacility must not be null");
this.solvencyPreCheck = requireNonNull(solvencyPreCheck, "solvencyPreCheck must not be null");
this.authorizer = requireNonNull(authorizer, "authorizer must not be null");
this.networkUtilizationManager =
Expand All @@ -191,7 +191,7 @@ public HandleWorkflow(
* @param round the next {@link Round} that needs to be processed
*/
public void handleRound(
@NonNull final HederaState state, @NonNull final SwirldDualState dualState, @NonNull final Round round) {
@NonNull final HederaState state, @NonNull final PlatformState platformState, @NonNull final Round round) {
// Keep track of whether any user transactions were handled. If so, then we will need to close the round
// with the block record manager.
final var userTransactionsHandled = new AtomicBoolean(false);
Expand Down Expand Up @@ -222,7 +222,7 @@ public void handleRound(
// skip system transactions
if (!platformTxn.isSystem()) {
userTransactionsHandled.set(true);
handlePlatformTransaction(state, dualState, event, creator, platformTxn);
handlePlatformTransaction(state, platformState, event, creator, platformTxn);
}
} catch (final Exception e) {
logger.fatal(
Expand All @@ -244,7 +244,7 @@ public void handleRound(

private void handlePlatformTransaction(
@NonNull final HederaState state,
@NonNull final SwirldDualState dualState,
@NonNull final PlatformState platformState,
@NonNull final ConsensusEvent platformEvent,
@NonNull final NodeInfo creator,
@NonNull final ConsensusTransaction platformTxn) {
Expand All @@ -253,13 +253,13 @@ private void handlePlatformTransaction(
final Instant consensusNow = platformTxn.getConsensusTimestamp().minusNanos(1000 - 3L);

// handle user transaction
handleUserTransaction(consensusNow, state, dualState, platformEvent, creator, platformTxn);
handleUserTransaction(consensusNow, state, platformState, platformEvent, creator, platformTxn);
}

private void handleUserTransaction(
@NonNull final Instant consensusNow,
@NonNull final HederaState state,
@NonNull final SwirldDualState dualState,
@NonNull final PlatformState platformState,
@NonNull final ConsensusEvent platformEvent,
@NonNull final NodeInfo creator,
@NonNull final ConsensusTransaction platformTxn) {
Expand Down Expand Up @@ -479,8 +479,8 @@ private void handleUserTransaction(
final var fileUpdateResult = systemFileUpdateFacility.handleTxBody(stack, txBody);
recordBuilder.status(fileUpdateResult);

// Notify if dual state was updated
dualStateUpdateFacility.handleTxBody(stack, dualState, txBody);
// Notify if platform state was updated
platformStateUpdateFacility.handleTxBody(stack, platformState, txBody);

} catch (final HandleException e) {
// In case of a ContractCall when it reverts, the gas charged should not be rolled back
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import com.hedera.node.app.spi.state.ReadableSingletonState;
import com.hedera.node.app.spi.state.ReadableStates;
import com.hedera.node.app.state.HederaState;
import com.swirlds.platform.system.DualState;
import com.swirlds.platform.state.PlatformState;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.time.Instant;
import javax.inject.Inject;
Expand All @@ -41,14 +41,14 @@
* Simple facility that notifies interested parties when the freeze state is updated.
*/
@Singleton
public class DualStateUpdateFacility {
private static final Logger logger = LogManager.getLogger(DualStateUpdateFacility.class);
public class PlatformStateUpdateFacility {
private static final Logger logger = LogManager.getLogger(PlatformStateUpdateFacility.class);

/**
* Creates a new instance of this class.
*/
@Inject
public DualStateUpdateFacility() {
public PlatformStateUpdateFacility() {
// For dagger
}

Expand All @@ -61,7 +61,7 @@
*/
public void handleTxBody(
@NonNull final HederaState state,
@NonNull final DualState dualState,
@NonNull final PlatformState platformState,
@NonNull final TransactionBody txBody) {
requireNonNull(state, "state must not be null");
requireNonNull(txBody, "txBody must not be null");
Expand All @@ -70,20 +70,20 @@
final FreezeType freezeType = txBody.freezeOrThrow().freezeType();
if (freezeType == FREEZE_UPGRADE || freezeType == FREEZE_ONLY) {
logger.info("Transaction freeze of type {} detected", freezeType);
// copy freeze state to dual state
// copy freeze state to platform state
final ReadableStates states = state.createReadableStates(FreezeService.NAME);
final ReadableSingletonState<Timestamp> freezeTime =
states.getSingleton(FreezeServiceImpl.FREEZE_TIME_KEY);
requireNonNull(freezeTime.get());
final Instant freezeTimeInstant = Instant.ofEpochSecond(
freezeTime.get().seconds(), freezeTime.get().nanos());
logger.info("Freeze time will be {}", freezeTimeInstant);
dualState.setFreezeTime(freezeTimeInstant);
platformState.setFreezeTime(freezeTimeInstant);
} else if (freezeType == FREEZE_ABORT) {
logger.info("Aborting freeze");
// copy freeze state (which is null) to dual state
// we just set dual state to null
dualState.setFreezeTime(null);
// copy freeze state (which is null) to platform state
// we just set platform state to null
platformState.setFreezeTime(null);

Check warning on line 86 in hedera-node/hedera-app/src/main/java/com/hedera/node/app/workflows/handle/PlatformStateUpdateFacility.java

View check run for this annotation

Codecov / codecov/patch

hedera-node/hedera-app/src/main/java/com/hedera/node/app/workflows/handle/PlatformStateUpdateFacility.java#L86

Added line #L86 was not covered by tests
}
// else for other freeze types, do nothing
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import com.swirlds.base.state.MutabilityException;
import com.swirlds.common.merkle.MerkleNode;
import com.swirlds.merkle.map.MerkleMap;
import com.swirlds.platform.state.PlatformState;
import com.swirlds.platform.system.Round;
import com.swirlds.platform.system.SwirldDualState;
import com.swirlds.platform.system.events.Event;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -65,7 +65,7 @@ void setUp() {
hederaMerkle = new MerkleHederaState(
(tree, state) -> onPreHandleCalled.set(true),
(evt, meta, state) -> onHandleCalled.set(true),
(state, platform, dual, trigger, version) -> onMigrateCalled.set(true));
(state, platform, platformState, trigger, version) -> onMigrateCalled.set(true));
}

/** Looks for a merkle node with the given label */
Expand Down Expand Up @@ -688,7 +688,7 @@ final class ConsensusRoundTest {
@DisplayName("Notifications are sent to onHandleConsensusRound when handleConsensusRound is called")
void handleConsensusRoundCallback() {
final var round = Mockito.mock(Round.class);
final var dualState = Mockito.mock(SwirldDualState.class);
final var platformState = Mockito.mock(PlatformState.class);
final var state = new MerkleHederaState(
(tree, st) -> onPreHandleCalled.set(true),
(evt, meta, provider) -> {
Expand All @@ -697,7 +697,7 @@ void handleConsensusRoundCallback() {
},
(s, p, d, t, v) -> {});

state.handleConsensusRound(round, dualState);
state.handleConsensusRound(round, platformState);
assertThat(onHandleCalled).isTrue();
}
}
Expand All @@ -712,11 +712,11 @@ void originalLosesConsensusRoundCallbackAfterCopy() {

// The original no longer has the listener
final var round = Mockito.mock(Round.class);
final var dualState = Mockito.mock(SwirldDualState.class);
assertThrows(MutabilityException.class, () -> hederaMerkle.handleConsensusRound(round, dualState));
final var platformState = Mockito.mock(PlatformState.class);
assertThrows(MutabilityException.class, () -> hederaMerkle.handleConsensusRound(round, platformState));

// But the copy does
copy.handleConsensusRound(round, dualState);
copy.handleConsensusRound(round, platformState);
assertThat(onHandleCalled).isTrue();
}

Expand Down