Skip to content

Commit

Permalink
fix: 16750 Fixed serialization for ISSTestingToolState (#16826)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Malygin <ivan@swirldslabs.com>
  • Loading branch information
imalygin authored Nov 28, 2024
1 parent 0759d3d commit 539f059
Showing 1 changed file with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
import com.hedera.hapi.node.state.roster.Roster;
import com.hedera.hapi.node.state.roster.RosterEntry;
import com.swirlds.common.constructable.ConstructableIgnored;
import com.swirlds.common.io.SelfSerializable;
import com.swirlds.common.io.streams.SerializableDataInputStream;
import com.swirlds.common.io.streams.SerializableDataOutputStream;
import com.swirlds.common.merkle.utility.SerializableLong;
import com.swirlds.common.platform.NodeId;
import com.swirlds.common.utility.ByteUtils;
Expand All @@ -50,8 +53,13 @@
import com.swirlds.platform.system.events.ConsensusEvent;
import com.swirlds.platform.system.transaction.ConsensusTransaction;
import com.swirlds.platform.test.fixtures.state.FakeMerkleStateLifecycles;
import com.swirlds.state.merkle.singleton.StringLeaf;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
Expand All @@ -62,6 +70,7 @@
import java.util.Objects;
import java.util.Random;
import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -89,6 +98,11 @@ private static class ClassVersion {
*/
private static final Duration INCIDENT_WINDOW = Duration.ofSeconds(10);

private static final int RUNNING_SUM_INDEX = 1;
private static final int GENESIS_TIMESTAMP_INDEX = 2;
private static final int PLANNED_ISS_LIST_INDEX = 3;
private static final int PLANNED_LOG_ERROR_LIST_INDEX = 4;

private NodeId selfId;

/**
Expand Down Expand Up @@ -172,13 +186,52 @@ public void init(

this.plannedIssList = testingToolConfig.getPlannedISSs();
this.plannedLogErrorList = testingToolConfig.getPlannedLogErrors();
writeObjectByChildIndex(PLANNED_ISS_LIST_INDEX, plannedIssList);
writeObjectByChildIndex(PLANNED_LOG_ERROR_LIST_INDEX, plannedLogErrorList);
} else {
StringLeaf runningSumLeaf = getChild(RUNNING_SUM_INDEX);
if (runningSumLeaf != null) {
runningSum = Long.parseLong(runningSumLeaf.getLabel());
}
StringLeaf genesisTimestampLeaf = getChild(GENESIS_TIMESTAMP_INDEX);
if (genesisTimestampLeaf != null) {
genesisTimestamp = Instant.parse(genesisTimestampLeaf.getLabel());
}
plannedIssList = readObjectByChildIndex(PLANNED_ISS_LIST_INDEX, PlannedIss::new);
plannedLogErrorList = readObjectByChildIndex(PLANNED_LOG_ERROR_LIST_INDEX, PlannedLogError::new);
}

this.selfId = platform.getSelfId();
this.scratchPad =
Scratchpad.create(platform.getContext(), selfId, IssTestingToolScratchpad.class, "ISSTestingTool");
}

<T extends SelfSerializable> List<T> readObjectByChildIndex(int index, Supplier<T> factory) {
StringLeaf stringValue = getChild(index);
if (stringValue != null) {
try {
SerializableDataInputStream in = new SerializableDataInputStream(
new ByteArrayInputStream(stringValue.getLabel().getBytes(StandardCharsets.UTF_8)));
return in.readSerializableList(1024, false, factory);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
return null;
}
}

<T extends SelfSerializable> void writeObjectByChildIndex(int index, List<T> list) {
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
SerializableDataOutputStream out = new SerializableDataOutputStream(byteOut);
out.writeSerializableList(list, false, true);
setChild(index, new StringLeaf(byteOut.toString(StandardCharsets.UTF_8)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -221,6 +274,7 @@ public void handleConsensusRound(final Round round, final PlatformStateModifier
private void captureTimestamp(final ConsensusEvent event) {
if (genesisTimestamp == null) {
genesisTimestamp = event.getConsensusTimestamp();
setChild(GENESIS_TIMESTAMP_INDEX, new StringLeaf(genesisTimestamp.toString()));
}
}

Expand All @@ -236,6 +290,7 @@ private void handleTransaction(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 Expand Up @@ -325,7 +380,7 @@ private int findLargestPartition(@NonNull final Roster roster, @NonNull final Pl
int largestPartition = 0;
long largestPartitionWeight = 0;
for (int partition = 0; partition < plannedIss.getPartitionCount(); partition++) {
if (partitionWeights.get(partition) > largestPartitionWeight) {
if (partitionWeights.get(partition) != null && partitionWeights.get(partition) > largestPartitionWeight) {
largestPartition = partition;
largestPartitionWeight = partitionWeights.getOrDefault(partition, 0L);
}
Expand Down

0 comments on commit 539f059

Please sign in to comment.