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

fix: 16750 Fixed serialization for ISSTestingToolState #16826

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@

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);

Check warning on line 192 in platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java#L189-L192

Added lines #L189 - L192 were not covered by tests
if (runningSumLeaf != null) {
runningSum = Long.parseLong(runningSumLeaf.getLabel());

Check warning on line 194 in platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java#L194

Added line #L194 was not covered by tests
}
StringLeaf genesisTimestampLeaf = getChild(GENESIS_TIMESTAMP_INDEX);

Check warning on line 196 in platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java#L196

Added line #L196 was not covered by tests
if (genesisTimestampLeaf != null) {
genesisTimestamp = Instant.parse(genesisTimestampLeaf.getLabel());

Check warning on line 198 in platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java#L198

Added line #L198 was not covered by tests
}
plannedIssList = readObjectByChildIndex(PLANNED_ISS_LIST_INDEX, PlannedIss::new);
plannedLogErrorList = readObjectByChildIndex(PLANNED_LOG_ERROR_LIST_INDEX, PlannedLogError::new);

Check warning on line 201 in platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java#L200-L201

Added lines #L200 - L201 were not covered by tests
}

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);

Check warning on line 210 in platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java#L210

Added line #L210 was not covered by tests
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);

Check warning on line 217 in platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java#L213-L217

Added lines #L213 - L217 were not covered by tests
}
} else {
return null;

Check warning on line 220 in platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java#L220

Added line #L220 was not covered by tests
}
}

<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);
}
}

Check warning on line 233 in platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java#L226-L233

Added lines #L226 - L233 were not covered by tests

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

Check warning on line 277 in platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java#L277

Added line #L277 was not covered by tests
}
}

Expand All @@ -236,6 +290,7 @@
final int delta =
ByteUtils.byteArrayToInt(transaction.getApplicationTransaction().toByteArray(), 0);
runningSum += delta;
setChild(RUNNING_SUM_INDEX, new StringLeaf(Long.toString(runningSum)));

Check warning on line 293 in platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java

View check run for this annotation

Codecov / codecov/patch

platform-sdk/platform-apps/tests/ISSTestingTool/src/main/java/com/swirlds/demo/iss/ISSTestingToolState.java#L293

Added line #L293 was not covered by tests
}

/**
Expand Down Expand Up @@ -325,7 +380,7 @@
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