Skip to content

Commit

Permalink
fix: 16748 Fixed serialization for AddressBookTestingToolState
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Malygin <[email protected]>
  • Loading branch information
imalygin committed Nov 26, 2024
1 parent 935fdb1 commit cbbff06
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.swirlds.platform.system.address.AddressBookUtils;
import com.swirlds.platform.system.events.ConsensusEvent;
import com.swirlds.platform.system.transaction.ConsensusTransaction;
import com.swirlds.state.merkle.singleton.StringLeaf;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.io.File;
Expand Down Expand Up @@ -98,6 +99,9 @@ private static class ClassVersion {

private static final long CLASS_ID = 0xf052378c7364ef47L;

private static final int RUNNING_SUM_INDEX = 1;
private static final int ROUND_HANDLED_INDEX = 2;

private NodeId selfId;

/** false until the test scenario has been validated, true afterwards. */
Expand Down Expand Up @@ -185,6 +189,17 @@ public void init(
throwIfImmutable();

this.selfId = platform.getSelfId();

final StringLeaf runningSumLeaf = getChild(RUNNING_SUM_INDEX);
if (runningSumLeaf != null && runningSumLeaf.getLabel() != null) {
this.runningSum = Long.parseLong(runningSumLeaf.getLabel());
logger.info(STARTUP.getMarker(), "State initialized with state long {}.", runningSum);
}
final StringLeaf roundsHandledLeaf = getChild(ROUND_HANDLED_INDEX);
if (roundsHandledLeaf != null && roundsHandledLeaf.getLabel() != null) {
this.roundsHandled = Long.parseLong(roundsHandledLeaf.getLabel());
logger.info(STARTUP.getMarker(), "State initialized with {} rounds handled.", roundsHandled);
}
}

/**
Expand All @@ -206,6 +221,7 @@ public void handleConsensusRound(@NonNull final Round round, @NonNull final Plat
}

roundsHandled++;
setChild(ROUND_HANDLED_INDEX, new StringLeaf(Long.toString(roundsHandled)));

final Iterator<ConsensusEvent> eventIterator = round.iterator();

Expand Down Expand Up @@ -236,6 +252,7 @@ private void handleTransaction(@NonNull final ConsensusTransaction transaction)
final int delta =
ByteUtils.byteArrayToInt(transaction.getApplicationTransaction().toByteArray(), 0);
runningSum += delta;
setChild(RUNNING_SUM_INDEX, new StringLeaf(Long.toString(runningSum)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static List<SavedStateInfo> getSavedStateFiles(
(final MerkleDataInputStream in) -> {
readAndCheckSigSetFileVersion(in);
final SigSet sigSet = in.readSerializable();
return new MerkleTreeSnapshotReader.StateFileData(data.state(), data.hash(), sigSet);
return new MerkleTreeSnapshotReader.StateFileData(data.stateRoot(), data.hash(), sigSet);
});
} else {
normalizedData = data;
Expand All @@ -104,7 +104,7 @@ public static List<SavedStateInfo> getSavedStateFiles(
final SignedState newSignedState = new SignedState(
configuration,
CryptoStatic::verifySignature,
(MerkleRoot) normalizedData.state(),
(MerkleRoot) normalizedData.stateRoot(),
"SignedStateFileReader.readStateFile()",
false,
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,6 @@ public void createSnapshot(@NonNull final Path targetPath) {
*/
@Override
public MerkleStateRoot<?> loadSnapshot(@NonNull Path targetPath) throws IOException {
return MerkleTreeSnapshotReader.readStateFileData(targetPath).state();
return (MerkleStateRoot<?>) MerkleTreeSnapshotReader.readStateFileData(targetPath).stateRoot();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.swirlds.common.crypto.Hash;
import com.swirlds.common.io.streams.MerkleDataInputStream;
import com.swirlds.common.merkle.impl.PartialNaryMerkleInternal;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.io.BufferedInputStream;
Expand Down Expand Up @@ -61,11 +62,11 @@ public class MerkleTreeSnapshotReader {

/**
* This is a helper class to hold the data read from a state file.
* @param state the Merkle tree state
* @param stateRoot the root of Merkle tree state
* @param hash the hash of the state
* @param sigSet the signature set
*/
public record StateFileData(@NonNull MerkleStateRoot<?> state, @NonNull Hash hash, @Nullable SigSet sigSet) {}
public record StateFileData(@NonNull PartialNaryMerkleInternal stateRoot, @NonNull Hash hash, @Nullable SigSet sigSet) {}

/**
* Reads a state file from disk
Expand Down Expand Up @@ -100,7 +101,7 @@ private static StateFileData readStateFileDataV1(
@NonNull final Path stateFile, @NonNull final MerkleDataInputStream in, @NonNull final Path directory)
throws IOException {
try {
final MerkleStateRoot<?> state = in.readMerkleTree(directory, MAX_MERKLE_NODES_IN_STATE);
final PartialNaryMerkleInternal state = in.readMerkleTree(directory, MAX_MERKLE_NODES_IN_STATE);
final Hash hash = in.readSerializable();
final SigSet sigSet = in.readSerializable();
return new StateFileData(state, hash, sigSet);
Expand Down

0 comments on commit cbbff06

Please sign in to comment.