Skip to content

Commit

Permalink
chore: cleanup legacy event serialization (#17350)
Browse files Browse the repository at this point in the history
Signed-off-by: Lazar Petrovic <lpetrovic05@gmail.com>
  • Loading branch information
lpetrovic05 authored Jan 14, 2025
1 parent eeea59f commit c0ce03a
Show file tree
Hide file tree
Showing 26 changed files with 26 additions and 619 deletions.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,15 +17,12 @@
package com.swirlds.platform.core.jmh;

import com.hedera.hapi.platform.event.GossipEvent;
import com.swirlds.common.constructable.ConstructableRegistry;
import com.swirlds.common.constructable.ConstructableRegistryException;
import com.swirlds.common.io.streams.MerkleDataInputStream;
import com.swirlds.common.io.streams.MerkleDataOutputStream;
import com.swirlds.platform.event.PlatformEvent;
import com.swirlds.platform.event.hashing.EventHasher;
import com.swirlds.platform.event.hashing.PbjStreamHasher;
import com.swirlds.platform.system.BasicSoftwareVersion;
import com.swirlds.platform.system.StaticSoftwareVersion;
import com.swirlds.platform.test.fixtures.event.TestingEventBuilder;
import java.io.IOException;
import java.io.PipedInputStream;
Expand Down Expand Up @@ -78,9 +75,6 @@ public void setup() throws IOException, ConstructableRegistryException {
.setSelfParent(new TestingEventBuilder(random).build())
.setOtherParent(new TestingEventBuilder(random).build())
.build();
StaticSoftwareVersion.setSoftwareVersion(
new BasicSoftwareVersion(event.getSoftwareVersion().major()));
ConstructableRegistry.getInstance().registerConstructables("com.swirlds.platform.system");
final PipedInputStream inputStream = new PipedInputStream();
final PipedOutputStream outputStream = new PipedOutputStream(inputStream);
outStream = new MerkleDataOutputStream(outputStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import com.swirlds.platform.state.signed.ReservedSignedState;
import com.swirlds.platform.system.Platform;
import com.swirlds.platform.system.SoftwareVersion;
import com.swirlds.platform.system.StaticSoftwareVersion;
import com.swirlds.platform.system.status.StatusActionSubmitter;
import com.swirlds.platform.util.RandomBuilder;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -194,8 +193,6 @@ private PlatformBuilder(
this.selfId = Objects.requireNonNull(selfId);
this.consensusEventStreamName = Objects.requireNonNull(consensusEventStreamName);
this.rosterHistory = Objects.requireNonNull(rosterHistory);

StaticSoftwareVersion.setSoftwareVersion(softwareVersion);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,147 +16,22 @@

package com.swirlds.platform.event;

import com.hedera.hapi.platform.event.EventTransaction;
import com.hedera.hapi.platform.event.EventTransaction.TransactionOneOfType;
import com.hedera.hapi.platform.event.GossipEvent;
import com.hedera.hapi.platform.event.StateSignatureTransaction;
import com.hedera.pbj.runtime.OneOf;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import com.swirlds.common.crypto.SignatureType;
import com.swirlds.common.io.streams.SerializableDataInputStream;
import com.swirlds.common.io.streams.SerializableDataOutputStream;
import com.swirlds.common.platform.NodeId;
import com.swirlds.platform.system.SoftwareVersion;
import com.swirlds.platform.system.StaticSoftwareVersion;
import com.swirlds.platform.system.events.EventDescriptorWrapper;
import com.swirlds.platform.system.events.UnsignedEvent;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* A utility class for serializing and deserializing events using legacy serialization. This will be removed as soon as
* event serialization is completely migrated to protobuf.
*/
public final class EventSerializationUtils {
private static final int MAX_ARRAY_LENGTH = 1_000_000;
private static final long APPLICATION_TRANSACTION_CLASS_ID = 0x9ff79186f4c4db97L;
private static final int APPLICATION_TRANSACTION_VERSION = 1;
private static final long STATE_SIGNATURE_CLASS_ID = 0xaf7024c653caabf4L;
private static final int STATE_SIGNATURE_VERSION = 3;
private static final int UNSIGNED_EVENT_VERSION = 4;
private static final int PLATFORM_EVENT_VERSION = 3;

private EventSerializationUtils() {
// Utility class
}

/**
* Deserialize the event as {@link UnsignedEvent}.
*
* @param in the stream from which this object is to be read
* @return the deserialized event
* @throws IOException if unsupported transaction types are encountered
*/
@NonNull
private static UnsignedEvent deserializeUnsignedEvent(@NonNull final SerializableDataInputStream in)
throws IOException {
Objects.requireNonNull(in, "The input stream must not be null");
final int version = in.readInt();
if (version != UNSIGNED_EVENT_VERSION) {
throw new IOException("Unsupported version: " + version);
}

final SoftwareVersion softwareVersion =
in.readSerializable(StaticSoftwareVersion.getSoftwareVersionClassIdSet());

final NodeId creatorId = in.readSerializable(false, NodeId::new);
if (creatorId == null) {
throw new IOException("creatorId is null");
}
final EventDescriptorWrapper selfParent = EventDescriptorWrapper.deserialize(in);
final List<EventDescriptorWrapper> otherParents = EventDescriptorWrapper.deserializeList(in);
final long birthRound = in.readLong();

final Instant timeCreated = in.readInstant();
in.readInt(); // read serialized length
final List<EventTransaction> transactionList = new ArrayList<>();
final int transactionSize = in.readInt();
if (transactionSize > 0) {
in.readBoolean(); // allSameClass
}
for (int i = 0; i < transactionSize; i++) {
final long classId = in.readLong();
final int classVersion = in.readInt();
if (classId == APPLICATION_TRANSACTION_CLASS_ID) {
final OneOf<TransactionOneOfType> oneOf = new OneOf<>(
TransactionOneOfType.APPLICATION_TRANSACTION,
deserializeApplicationTransaction(in, classVersion));
transactionList.add(new EventTransaction(oneOf));
} else if (classId == STATE_SIGNATURE_CLASS_ID) {
final OneOf<TransactionOneOfType> oneOf = new OneOf<>(
TransactionOneOfType.STATE_SIGNATURE_TRANSACTION,
deserializeStateSignatureTransaction(in, classVersion));
transactionList.add(new EventTransaction(oneOf));
} else {
throw new IOException("Unknown classId: " + classId);
}
}

return new UnsignedEvent(
softwareVersion, creatorId, selfParent, otherParents, birthRound, timeCreated, transactionList);
}

@NonNull
private static Bytes deserializeApplicationTransaction(
@NonNull final SerializableDataInputStream in, final int classVersion) throws IOException {
if (classVersion != APPLICATION_TRANSACTION_VERSION) {
throw new IOException("Unsupported application class version: " + classVersion);
}
return Bytes.wrap(Objects.requireNonNull(in.readByteArray(MAX_ARRAY_LENGTH)));
}

@NonNull
private static StateSignatureTransaction deserializeStateSignatureTransaction(
@NonNull final SerializableDataInputStream in, final int classVersion) throws IOException {
if (classVersion != STATE_SIGNATURE_VERSION) {
throw new IOException("Unsupported state signature class version: " + classVersion);
}
final byte[] sigBytes = in.readByteArray(MAX_ARRAY_LENGTH);
final byte[] hashBytes = in.readByteArray(MAX_ARRAY_LENGTH);
final long round = in.readLong();
in.readInt(); // epochHash is always null
return new StateSignatureTransaction(round, Bytes.wrap(sigBytes), Bytes.wrap(hashBytes));
}

/**
* Deserialize the event as {@link PlatformEvent}.
*
* @param in the stream from which this object is to be read
* @param readVersion if true, the event version number will be read from the stream
* @return the deserialized event
* @throws IOException if unsupported transaction types are encountered
*/
@NonNull
public static PlatformEvent deserializePlatformEvent(
@NonNull final SerializableDataInputStream in, final boolean readVersion) throws IOException {
if (readVersion) {
final int eventVersion = in.readInt();
if (eventVersion != PLATFORM_EVENT_VERSION) {
throw new IOException("Unsupported event version: " + eventVersion);
}
}
final UnsignedEvent unsignedEvent = EventSerializationUtils.deserializeUnsignedEvent(in);
final byte[] signature = in.readByteArray(SignatureType.RSA.signatureLength());

return new PlatformEvent(unsignedEvent, signature);
}

/**
* Serialize and then deserialize the given {@link PlatformEvent}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.swirlds.common.io.IOIterator;
import com.swirlds.common.io.streams.SerializableDataInputStream;
import com.swirlds.platform.event.AncientMode;
import com.swirlds.platform.event.EventSerializationUtils;
import com.swirlds.platform.event.PlatformEvent;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.BufferedInputStream;
Expand Down Expand Up @@ -85,7 +84,6 @@ private void findNext() throws IOException {
try {
final PlatformEvent candidate =
switch (fileVersion) {
case ORIGINAL -> EventSerializationUtils.deserializePlatformEvent(stream, true);
case PROTOBUF_EVENTS -> new PlatformEvent(stream.readPbjRecord(GossipEvent.PROTOBUF));
};
if (candidate.getAncientIndicator(fileType) >= lowerBound) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,8 +22,6 @@
* The version of the file format used to serialize preconsensus events.
*/
public enum PcesFileVersion {
/** The original version of the file format. */
ORIGINAL(1),
/** The version of the file format that serializes events as protobuf. */
PROTOBUF_EVENTS(2);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
* Copyright (C) 2024-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,7 +61,6 @@
import com.swirlds.platform.state.snapshot.SignedStateFileWriter;
import com.swirlds.platform.system.InitTrigger;
import com.swirlds.platform.system.Round;
import com.swirlds.platform.system.StaticSoftwareVersion;
import com.swirlds.platform.system.SwirldMain;
import com.swirlds.platform.system.SwirldState;
import com.swirlds.platform.system.events.CesEvent;
Expand Down Expand Up @@ -153,9 +152,6 @@ public static void recoverState(
try (final ReservedSignedState initialState = SignedStateFileReader.readStateFile(
platformContext.getConfiguration(), signedStateFile)
.reservedSignedState()) {
StaticSoftwareVersion.setSoftwareVersion(
initialState.get().getState().getReadablePlatformState().getCreationSoftwareVersion());

logger.info(
STARTUP.getMarker(),
"State from round {} loaded.",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2024 Hedera Hashgraph, LLC
* Copyright (C) 2016-2025 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,7 +31,6 @@
import com.swirlds.common.platform.NodeId;
import com.swirlds.common.stream.StreamAligned;
import com.swirlds.common.stream.Timestamped;
import com.swirlds.platform.event.EventSerializationUtils;
import com.swirlds.platform.event.PlatformEvent;
import com.swirlds.platform.system.transaction.ConsensusTransaction;
import com.swirlds.platform.system.transaction.Transaction;
Expand All @@ -51,7 +50,6 @@ public class CesEvent extends AbstractSerializableHashable
public static final long UNDEFINED = -1;

public static final long CLASS_ID = 0xe250a9fbdcc4b1baL;
private static final int CES_EVENT_VERSION_ORIGINAL = 1;
private static final int CES_EVENT_VERSION_PBJ_EVENT = 2;
private static final int CONSENSUS_DATA_CLASS_VERSION = 2;

Expand Down Expand Up @@ -108,7 +106,6 @@ public void serialize(@NonNull final SerializableDataOutputStream out) throws IO
@Override
public void deserialize(@NonNull final SerializableDataInputStream in, final int version) throws IOException {
this.platformEvent = switch (version) {
case CES_EVENT_VERSION_ORIGINAL -> EventSerializationUtils.deserializePlatformEvent(in, false);
case CES_EVENT_VERSION_PBJ_EVENT -> new PlatformEvent(in.readPbjRecord(GossipEvent.PROTOBUF));
default -> throw new IOException("Unsupported version " + version);};

Expand Down
Loading

0 comments on commit c0ce03a

Please sign in to comment.