Skip to content

Commit

Permalink
refactor: 15413 Split PlatformStateAccessor into two interfaces (#1…
Browse files Browse the repository at this point in the history
…5544)

Signed-off-by: Ivan Malygin <ivan@swirldslabs.com>
  • Loading branch information
imalygin committed Sep 19, 2024
1 parent 193821e commit aa75ff7
Show file tree
Hide file tree
Showing 43 changed files with 252 additions and 316 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import com.swirlds.common.merkle.impl.PartialMerkleLeaf;
import com.swirlds.common.platform.NodeId;
import com.swirlds.platform.SwirldsPlatform;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.system.InitTrigger;
import com.swirlds.platform.system.Platform;
import com.swirlds.platform.system.Round;
Expand Down Expand Up @@ -200,7 +200,7 @@ public synchronized CryptocurrencyDemoState copy() {
}

@Override
public void handleConsensusRound(final Round round, final PlatformStateAccessor platformState) {
public void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {
throwIfImmutable();
round.forEachEventTransaction((event, transaction) -> handleTransaction(event.getCreatorId(), transaction));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.swirlds.common.io.streams.SerializableDataOutputStream;
import com.swirlds.common.merkle.MerkleLeaf;
import com.swirlds.common.merkle.impl.PartialMerkleLeaf;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.system.Round;
import com.swirlds.platform.system.SwirldState;
import com.swirlds.platform.system.transaction.Transaction;
Expand Down Expand Up @@ -99,7 +99,7 @@ private HelloSwirldDemoState(final HelloSwirldDemoState sourceState) {
}

@Override
public synchronized void handleConsensusRound(final Round round, final PlatformStateAccessor platformState) {
public synchronized void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {
throwIfImmutable();
round.forEachTransaction(this::handleTransaction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.swirlds.common.io.streams.SerializableDataOutputStream;
import com.swirlds.common.merkle.MerkleLeaf;
import com.swirlds.common.merkle.impl.PartialMerkleLeaf;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.system.Round;
import com.swirlds.platform.system.SwirldState;

Expand Down Expand Up @@ -69,7 +69,7 @@ private StatsDemoState(final StatsDemoState sourceState) {
}

@Override
public void handleConsensusRound(final Round round, final PlatformStateAccessor platformState) {}
public void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {}

/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.swirlds.common.utility.StackTrace;
import com.swirlds.platform.config.AddressBookConfig;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.system.InitTrigger;
import com.swirlds.platform.system.Platform;
import com.swirlds.platform.system.Round;
Expand Down Expand Up @@ -182,7 +183,7 @@ public void init(
* {@inheritDoc}
*/
@Override
public void handleConsensusRound(@NonNull final Round round, @NonNull final PlatformStateAccessor platformState) {
public void handleConsensusRound(@NonNull final Round round, @NonNull final PlatformStateModifier platformState) {
Objects.requireNonNull(round, "the round cannot be null");
Objects.requireNonNull(platformState, "the platform state cannot be null");
throwIfImmutable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.swirlds.common.merkle.impl.PartialMerkleLeaf;
import com.swirlds.common.utility.NonCryptographicHashing;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.system.InitTrigger;
import com.swirlds.platform.system.Platform;
import com.swirlds.platform.system.Round;
Expand Down Expand Up @@ -249,7 +250,7 @@ public void preHandle(@NonNull final Event event) {
* Writes the round and its contents to a log on disk
*/
@Override
public void handleConsensusRound(final @NonNull Round round, final @NonNull PlatformStateAccessor platformState) {
public void handleConsensusRound(final @NonNull Round round, final @NonNull PlatformStateModifier platformState) {
Objects.requireNonNull(round);
Objects.requireNonNull(platformState);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import com.swirlds.common.platform.NodeId;
import com.swirlds.common.utility.ByteUtils;
import com.swirlds.platform.scratchpad.Scratchpad;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.system.InitTrigger;
import com.swirlds.platform.system.Platform;
import com.swirlds.platform.system.Round;
Expand Down Expand Up @@ -172,7 +172,7 @@ public void init(
* {@inheritDoc}
*/
@Override
public void handleConsensusRound(final Round round, final PlatformStateAccessor platformState) {
public void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {
throwIfImmutable();
final Iterator<ConsensusEvent> eventIterator = round.iterator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import com.swirlds.merkledb.MerkleDb;
import com.swirlds.merkledb.MerkleDbDataSourceBuilder;
import com.swirlds.merkledb.MerkleDbTableConfig;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.system.InitTrigger;
import com.swirlds.platform.system.Platform;
import com.swirlds.platform.system.Round;
Expand Down Expand Up @@ -249,7 +249,7 @@ public void init(
* {@inheritDoc}
*/
@Override
public void handleConsensusRound(final Round round, final PlatformStateAccessor platformState) {
public void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {
throwIfImmutable();
for (final Iterator<ConsensusEvent> eventIt = round.iterator(); eventIt.hasNext(); ) {
final ConsensusEvent event = eventIt.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
import com.swirlds.merkle.test.fixtures.map.pta.MapKey;
import com.swirlds.platform.ParameterProvider;
import com.swirlds.platform.Utilities;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.system.InitTrigger;
import com.swirlds.platform.system.Platform;
import com.swirlds.platform.system.Round;
Expand Down Expand Up @@ -1071,7 +1071,7 @@ private void handleControlTransaction(
* Handle the freeze transaction type.
*/
private void handleFreezeTransaction(
final TestTransaction testTransaction, final PlatformStateAccessor platformState) {
final TestTransaction testTransaction, final PlatformStateModifier platformState) {
final FreezeTransaction freezeTx = testTransaction.getFreezeTransaction();
FreezeTransactionHandler.freeze(freezeTx, platformState);
}
Expand All @@ -1093,7 +1093,7 @@ protected void preHandleTransaction(final Transaction transaction) {
}

@Override
public synchronized void handleConsensusRound(final Round round, final PlatformStateAccessor platformState) {
public synchronized void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {
throwIfImmutable();
if (!initialized.get()) {
throw new IllegalStateException("handleConsensusRound() called before init()");
Expand Down Expand Up @@ -1125,7 +1125,7 @@ private void updateTransactionCounters() {
private void handleConsensusTransaction(
final ConsensusEvent event,
final ConsensusTransaction trans,
final PlatformStateAccessor platformState,
final PlatformStateModifier platformState,
final long roundNum) {
if (trans.isSystem()) {
return;
Expand Down Expand Up @@ -1164,7 +1164,7 @@ private void handleTransaction(
@NonNull final Instant timeCreated,
@NonNull final Instant timestamp,
@NonNull final ConsensusTransaction trans,
@NonNull final PlatformStateAccessor platformState) {
@NonNull final PlatformStateModifier platformState) {
if (getConfig().isAppendSig()) {
try {
final TestTransactionWrapper testTransactionWrapper = TestTransactionWrapper.parseFrom(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.swirlds.demo.platform.freeze;

import com.swirlds.demo.platform.fs.stresstest.proto.FreezeTransaction;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.PlatformStateModifier;
import java.time.Instant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -28,7 +28,7 @@ public class FreezeTransactionHandler {
private static final Logger logger = LogManager.getLogger(FreezeTransactionHandler.class);
private static final Marker LOGM_FREEZE = MarkerManager.getMarker("FREEZE");

public static boolean freeze(final FreezeTransaction transaction, final PlatformStateAccessor platformState) {
public static boolean freeze(final FreezeTransaction transaction, final PlatformStateModifier platformState) {
logger.debug(LOGM_FREEZE, "Handling FreezeTransaction: " + transaction);
try {
platformState.setFreezeTime(Instant.ofEpochSecond(transaction.getStartTimeEpochSecond()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import com.swirlds.common.io.streams.SerializableDataOutputStream;
import com.swirlds.common.merkle.MerkleLeaf;
import com.swirlds.common.merkle.impl.PartialMerkleLeaf;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.system.Round;
import com.swirlds.platform.system.SwirldState;
import com.swirlds.platform.system.events.Event;
Expand Down Expand Up @@ -129,7 +129,7 @@ public void preHandle(final Event event) {
* {@inheritDoc}
*/
@Override
public void handleConsensusRound(final Round round, final PlatformStateAccessor platformState) {
public void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {
throwIfImmutable();
round.forEachTransaction(this::handleTransaction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.swirlds.common.merkle.MerkleLeaf;
import com.swirlds.common.merkle.impl.PartialMerkleLeaf;
import com.swirlds.common.utility.ByteUtils;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.system.InitTrigger;
import com.swirlds.platform.system.Platform;
import com.swirlds.platform.system.Round;
Expand Down Expand Up @@ -99,7 +99,7 @@ public void preHandle(final Event event) {
* {@inheritDoc}
*/
@Override
public void handleConsensusRound(final Round round, final PlatformStateAccessor platformState) {
public void handleConsensusRound(final Round round, final PlatformStateModifier platformState) {
throwIfImmutable();
round.forEachTransaction(this::handleTransaction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.swirlds.platform.config.DefaultConfiguration;
import com.swirlds.platform.consensus.SyntheticSnapshot;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.state.signed.ReservedSignedState;
import com.swirlds.platform.state.snapshot.DeserializedSignedState;
import com.swirlds.platform.state.snapshot.SignedStateFileReader;
Expand Down Expand Up @@ -75,7 +76,7 @@ public Integer call() throws IOException, ExecutionException, InterruptedExcepti
final DeserializedSignedState deserializedSignedState =
SignedStateFileReader.readStateFile(platformContext, statePath, SignedStateFileUtils::readState);
try (final ReservedSignedState reservedSignedState = deserializedSignedState.reservedSignedState()) {
final PlatformStateAccessor platformState =
final PlatformStateModifier platformState =
reservedSignedState.get().getState().getWritablePlatformState();
platformState.bulkUpdate(v -> {
System.out.printf("Replacing platform data %n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import com.swirlds.platform.internal.ConsensusRound;
import com.swirlds.platform.metrics.RoundHandlingMetrics;
import com.swirlds.platform.state.MerkleRoot;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.state.SwirldStateManager;
import com.swirlds.platform.state.signed.ReservedSignedState;
import com.swirlds.platform.state.signed.SignedState;
Expand Down Expand Up @@ -215,12 +215,12 @@ public StateAndRound handleConsensusRound(@NonNull final ConsensusRound consensu
}

/**
* Populate the {@link PlatformStateAccessor} with all needed data for this round.
* Populate the {@link PlatformStateModifier} with all needed data for this round.
*
* @param round the consensus round
*/
private void updatePlatformState(@NonNull final ConsensusRound round) {
final PlatformStateAccessor platformState =
final PlatformStateModifier platformState =
swirldStateManager.getConsensusState().getWritablePlatformState();
platformState.bulkUpdate(v -> {
v.setRound(round.getRoundNum());
Expand All @@ -238,7 +238,7 @@ private void updatePlatformState(@NonNull final ConsensusRound round) {
* @throws InterruptedException if this thread is interrupted
*/
private void updateRunningEventHash(@NonNull final ConsensusRound round) throws InterruptedException {
final PlatformStateAccessor platformState =
final PlatformStateModifier platformState =
swirldStateManager.getConsensusState().getWritablePlatformState();

if (writeLegacyRunningEventHash) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.swirlds.platform.recovery.internal.StreamedRound;
import com.swirlds.platform.state.MerkleRoot;
import com.swirlds.platform.state.PlatformStateAccessor;
import com.swirlds.platform.state.PlatformStateModifier;
import com.swirlds.platform.state.signed.ReservedSignedState;
import com.swirlds.platform.state.signed.SignedState;
import com.swirlds.platform.state.snapshot.SignedStateFileReader;
Expand Down Expand Up @@ -380,7 +381,7 @@ private static ReservedSignedState handleNextRound(
new DefaultEventHasher().hashEvent(lastEvent);

final PlatformStateAccessor newReadablePlatformState = newState.getReadablePlatformState();
final PlatformStateAccessor newWritablePlatformState = newState.getWritablePlatformState();
final PlatformStateModifier newWritablePlatformState = newState.getWritablePlatformState();
final PlatformStateAccessor previousReadablePlatformState =
previousState.get().getState().getReadablePlatformState();

Expand Down Expand Up @@ -478,7 +479,7 @@ static ConsensusEvent getLastEvent(final Round round) {
static void applyTransactions(
final SwirldState immutableState,
final SwirldState mutableState,
final PlatformStateAccessor platformState,
final PlatformStateModifier platformState,
final Round round) {

mutableState.throwIfImmutable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void modifyStateForBirthRoundMigration(
}

final MerkleRoot state = initialState.getState();
final PlatformStateAccessor writablePlatformState = state.getWritablePlatformState();
final PlatformStateModifier writablePlatformState = state.getWritablePlatformState();

final boolean alreadyMigrated = writablePlatformState.getFirstVersionInBirthRoundMode() != null;
if (alreadyMigrated) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private GenesisStateBuilder() {}
*/
public static void initGenesisPlatformState(
final PlatformContext platformContext,
final PlatformStateAccessor platformState,
final PlatformStateModifier platformState,
final AddressBook addressBook,
final SoftwareVersion appVersion) {
platformState.bulkUpdate(v -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public interface MerkleRoot extends MerkleInternal {
@NonNull
SwirldState getSwirldState();

/**
* This method makes sure that the platform state is initialized.
* If it's already initialized, it does nothing.
*/
void initPlatformState();

/**
* Get readable platform state.
* Works on both - mutable and immutable {@link MerkleRoot} and, therefore, this method should be preferred.
Expand All @@ -48,14 +54,14 @@ public interface MerkleRoot extends MerkleInternal {
* @return mutable platform state
*/
@NonNull
PlatformStateAccessor getWritablePlatformState();
PlatformStateModifier getWritablePlatformState();

/**
* Set the platform state.
*
* @param platformState the platform state
*/
void updatePlatformState(@NonNull final PlatformStateAccessor platformState);
void updatePlatformState(@NonNull final PlatformStateModifier platformState);

/**
* Generate a string that describes this state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*/
public interface MerkleStateLifecycles {
/**
* Called when a {@link MerkleStateRoot} needs to ensure its {@link PlatformStateAccessor} implementation
* Called when a {@link MerkleStateRoot} needs to ensure its {@link PlatformStateModifier} implementation
* is initialized.
*
* @param state the root of the state to be initialized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public MerkleStateRoot copy() {
* {@inheritDoc}
*/
@Override
public void handleConsensusRound(@NonNull final Round round, @NonNull final PlatformStateAccessor platformState) {
public void handleConsensusRound(@NonNull final Round round, @NonNull final PlatformStateModifier platformState) {
throwIfImmutable();
lifecycles.onHandleConsensusRound(round, this);
}
Expand Down Expand Up @@ -1014,20 +1014,30 @@ public PlatformStateAccessor getReadablePlatformState() {
*/
@NonNull
@Override
public PlatformStateAccessor getWritablePlatformState() {
public PlatformStateModifier getWritablePlatformState() {
if (isImmutable()) {
throw new IllegalStateException("Cannot get writable platform state when state is immutable");
}
return writablePlatformStateStore();
}

/**
* Updates the platform state with the values from the provided instance of {@link PlatformStateAccessor}
* {@inheritDoc}
*/
@Override
public void initPlatformState() {
if (!services.containsKey(PlatformStateService.NAME)) {
platformStateInitChanges = lifecycles.initPlatformState(this);
}
}

/**
* Updates the platform state with the values from the provided instance of {@link PlatformStateModifier}
*
* @param accessor a source of values
*/
@Override
public void updatePlatformState(@NonNull final PlatformStateAccessor accessor) {
public void updatePlatformState(@NonNull final PlatformStateModifier accessor) {
writablePlatformStateStore().setAllFrom(accessor);
}

Expand Down
Loading

0 comments on commit aa75ff7

Please sign in to comment.