Skip to content

Commit

Permalink
chore: remove legacy event serialization (#12999)
Browse files Browse the repository at this point in the history
Signed-off-by: Lazar Petrovic <lpetrovic05@gmail.com>
Signed-off-by: Stanimir Stoyanov <stanimir.stoyanov@limechain.tech>
  • Loading branch information
lpetrovic05 authored and stoyanov-st committed May 15, 2024
1 parent 3a36b72 commit f3e66d3
Show file tree
Hide file tree
Showing 29 changed files with 168 additions and 557 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static com.swirlds.common.io.streams.SerializableStreamConstants.VERSION_BYTES;

import com.swirlds.common.io.FunctionalSerialize;
import com.swirlds.common.io.OptionalSelfSerializable;
import com.swirlds.common.io.SelfSerializable;
import com.swirlds.common.io.SerializableDet;
import com.swirlds.common.io.SerializableWithKnownLength;
Expand Down Expand Up @@ -98,11 +97,6 @@ public void writeSerializable(SelfSerializable serializable, boolean writeClassI
writeSerializable(serializable, writeClassId, serializable);
}

public <E extends Enum<E>> void writeOptionalSerializable(
OptionalSelfSerializable<E> serializable, boolean writeClassId, E option) throws IOException {
writeSerializable(serializable, writeClassId, out -> serializable.serialize(out, option));
}

/**
* Writes a list of objects returned by an {@link Iterator} when the size in known ahead of time. If the class is
* known at the time of deserialization, the {@code writeClassId} param can be set to false. If the class might be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.swirlds.common.io.streams.SerializableDataOutputStream;
import com.swirlds.common.platform.NodeId;
import com.swirlds.platform.system.events.BaseEventHashedData;
import com.swirlds.platform.system.events.BaseEventUnhashedData;
import com.swirlds.platform.system.events.EventDescriptor;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
Expand All @@ -40,8 +39,6 @@ public class GossipEvent implements SelfSerializable {
private static final int MAX_SIG_LENGTH = 384;

private static final class ClassVersion {
public static final int ORIGINAL = 1;
public static final int REMOVED_ROUND = 2;
/**
* Event serialization changes
*
Expand All @@ -50,9 +47,10 @@ private static final class ClassVersion {
public static final int BIRTH_ROUND = 3;
}

private int serializedVersion = ClassVersion.BIRTH_ROUND;
private BaseEventHashedData hashedData;
private BaseEventUnhashedData unhashedData;
/** creator's signature for this event */
private byte[] signature;

private Instant timeReceived;

/**
Expand Down Expand Up @@ -84,11 +82,11 @@ public GossipEvent() {}

/**
* @param hashedData the hashed data for the event
* @param unhashedData the unhashed data for the event
* @param signature the signature for the event
*/
public GossipEvent(final BaseEventHashedData hashedData, final BaseEventUnhashedData unhashedData) {
public GossipEvent(final BaseEventHashedData hashedData, final byte[] signature) {
this.hashedData = hashedData;
this.unhashedData = unhashedData;
this.signature = signature;
this.timeReceived = Instant.now();
this.senderId = null;
}
Expand Down Expand Up @@ -122,29 +120,17 @@ public long getStreamSequenceNumber() {
*/
@Override
public void serialize(final SerializableDataOutputStream out) throws IOException {
if (serializedVersion < ClassVersion.BIRTH_ROUND) {
out.writeSerializable(hashedData, false);
out.writeSerializable(unhashedData, false);
} else {
out.writeSerializable(hashedData, false);
out.writeByteArray(unhashedData.getSignature());
}
out.writeSerializable(hashedData, false);
out.writeByteArray(signature);
}

/**
* {@inheritDoc}
*/
@Override
public void deserialize(final SerializableDataInputStream in, final int version) throws IOException {
serializedVersion = version;
if (version < ClassVersion.BIRTH_ROUND) {
hashedData = in.readSerializable(false, BaseEventHashedData::new);
unhashedData = in.readSerializable(false, BaseEventUnhashedData::new);
} else {
hashedData = in.readSerializable(false, BaseEventHashedData::new);
final byte[] signature = in.readByteArray(MAX_SIG_LENGTH);
unhashedData = new BaseEventUnhashedData(signature);
}
hashedData = in.readSerializable(false, BaseEventHashedData::new);
this.signature = in.readByteArray(MAX_SIG_LENGTH);
timeReceived = Instant.now();
}

Expand All @@ -156,10 +142,10 @@ public BaseEventHashedData getHashedData() {
}

/**
* Get the unhashed data for the event.
* @return the signature for the event
*/
public BaseEventUnhashedData getUnhashedData() {
return unhashedData;
public byte[] getSignature() {
return signature;
}

/**
Expand Down Expand Up @@ -244,12 +230,12 @@ public long getClassId() {
*/
@Override
public int getVersion() {
return serializedVersion;
return ClassVersion.BIRTH_ROUND;
}

@Override
public int getMinimumSupportedVersion() {
return ClassVersion.REMOVED_ROUND;
return ClassVersion.BIRTH_ROUND;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public GossipEvent handleEvent(@NonNull final GossipEvent event) {
}

final Set<ByteBuffer> signatures = observedEvents.computeIfAbsent(event.getDescriptor(), NEW_HASH_SET);
if (signatures.add(ByteBuffer.wrap(event.getUnhashedData().getSignature()))) {
if (signatures.add(ByteBuffer.wrap(event.getSignature()))) {
if (signatures.size() != 1) {
// signature is unique, but descriptor is not
disparateSignatureAccumulator.update(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.swirlds.platform.crypto.PlatformSigner;
import com.swirlds.platform.event.GossipEvent;
import com.swirlds.platform.system.events.BaseEventHashedData;
import com.swirlds.platform.system.events.BaseEventUnhashedData;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Objects;

Expand All @@ -48,7 +47,6 @@ public DefaultSelfEventSigner(@NonNull final KeysAndCerts keysAndCerts) {
@Override
public GossipEvent signEvent(@NonNull final BaseEventHashedData event) {
final Signature signature = new PlatformSigner(keysAndCerts).sign(event.getHash());
final BaseEventUnhashedData unhashedData = new BaseEventUnhashedData(signature.getSignatureBytes());
return new GossipEvent(event, unhashedData);
return new GossipEvent(event, signature.getSignatureBytes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,14 @@ private boolean isSignatureValid(@NonNull final GossipEvent event) {
}

final boolean isSignatureValid = signatureVerifier.verifySignature(
event.getHashedData().getHash().getValue(),
event.getUnhashedData().getSignature(),
publicKey);
event.getHashedData().getHash().getValue(), event.getSignature(), publicKey);

if (!isSignatureValid) {
rateLimitedLogger.error(
EXCEPTION.getMarker(),
"Event failed signature check. Event: {}, Signature: {}, Hash: {}",
event,
CommonUtils.hex(event.getUnhashedData().getSignature()),
CommonUtils.hex(event.getSignature()),
event.getHashedData().getHash());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private boolean areRequiredFieldsNonNull(@NonNull final GossipEvent event) {
return false;
}

if (event.getUnhashedData() == null) {
if (event.getSignature() == null) {
// do not log the event itself, since toString would throw a NullPointerException
nullUnhashedDataLogger.error(EXCEPTION.getMarker(), "Event has null unhashed data");
nullUnhashedDataAccumulator.update(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.swirlds.common.crypto.RunningHash;
import com.swirlds.common.crypto.RunningHashable;
import com.swirlds.common.crypto.SerializableHashable;
import com.swirlds.common.io.OptionalSelfSerializable;
import com.swirlds.common.io.SelfSerializable;
import com.swirlds.common.io.streams.SerializableDataInputStream;
import com.swirlds.common.io.streams.SerializableDataOutputStream;
import com.swirlds.common.platform.NodeId;
Expand All @@ -31,11 +31,9 @@
import com.swirlds.platform.event.GossipEvent;
import com.swirlds.platform.system.SoftwareVersion;
import com.swirlds.platform.system.events.BaseEventHashedData;
import com.swirlds.platform.system.events.BaseEventUnhashedData;
import com.swirlds.platform.system.events.ConsensusData;
import com.swirlds.platform.system.events.ConsensusEvent;
import com.swirlds.platform.system.events.DetailedConsensusEvent;
import com.swirlds.platform.system.events.EventSerializationOptions;
import com.swirlds.platform.system.transaction.ConsensusTransaction;
import com.swirlds.platform.system.transaction.ConsensusTransactionImpl;
import com.swirlds.platform.system.transaction.Transaction;
Expand All @@ -59,7 +57,7 @@ public class EventImpl extends EventMetadata
implements Comparable<EventImpl>,
ConsensusEvent,
SerializableHashable,
OptionalSelfSerializable<EventSerializationOptions>,
SelfSerializable,
RunningHashable,
StreamAligned,
Timestamped {
Expand Down Expand Up @@ -95,36 +93,12 @@ public class EventImpl extends EventMetadata

public EventImpl() {}

public EventImpl(final BaseEventHashedData baseEventHashedData, final BaseEventUnhashedData baseEventUnhashedData) {
this(baseEventHashedData, baseEventUnhashedData, new ConsensusData(), null, null);
}

public EventImpl(
final BaseEventHashedData baseEventHashedData,
final BaseEventUnhashedData baseEventUnhashedData,
final ConsensusData consensusData) {
this(baseEventHashedData, baseEventUnhashedData, consensusData, null, null);
}

public EventImpl(
final BaseEventHashedData baseEventHashedData,
final BaseEventUnhashedData baseEventUnhashedData,
final EventImpl selfParent,
final EventImpl otherParent) {
this(baseEventHashedData, baseEventUnhashedData, new ConsensusData(), selfParent, otherParent);
}

public EventImpl(final GossipEvent gossipEvent, final EventImpl selfParent, final EventImpl otherParent) {
this(gossipEvent, new ConsensusData(), selfParent, otherParent);
}

public EventImpl(
final BaseEventHashedData baseEventHashedData,
final BaseEventUnhashedData baseEventUnhashedData,
final ConsensusData consensusData,
final EventImpl selfParent,
final EventImpl otherParent) {
this(new GossipEvent(baseEventHashedData, baseEventUnhashedData), consensusData, selfParent, otherParent);
public EventImpl(@NonNull final GossipEvent gossipEvent) {
this(gossipEvent, new ConsensusData(), null, null);
}

public EventImpl(
Expand All @@ -135,7 +109,7 @@ public EventImpl(
super(selfParent, otherParent);
Objects.requireNonNull(baseEvent, "baseEvent");
Objects.requireNonNull(baseEvent.getHashedData(), "baseEventDataHashed");
Objects.requireNonNull(baseEvent.getUnhashedData(), "baseEventDataNotHashed");
Objects.requireNonNull(baseEvent.getSignature(), "signature");
Objects.requireNonNull(consensusData, "consensusData");

this.baseEvent = baseEvent;
Expand All @@ -146,6 +120,14 @@ public EventImpl(
findSystemTransactions();
}

/**
* Create an instance based on the given {@link DetailedConsensusEvent}
* @param detailedConsensusEvent the detailed consensus event to build from
*/
public EventImpl(final DetailedConsensusEvent detailedConsensusEvent) {
buildFromConsensusEvent(detailedConsensusEvent);
}

/**
* initialize RunningHash instance
*/
Expand Down Expand Up @@ -235,17 +217,7 @@ public synchronized int compareTo(final EventImpl other) {
*/
@Override
public void serialize(final SerializableDataOutputStream out) throws IOException {
serialize(out, EventSerializationOptions.FULL);
}

/**
* {@inheritDoc}
*/
@Override
public void serialize(final SerializableDataOutputStream out, final EventSerializationOptions option)
throws IOException {
DetailedConsensusEvent.serialize(
out, baseEvent.getHashedData(), baseEvent.getUnhashedData(), consensusData, option);
DetailedConsensusEvent.serialize(out, baseEvent.getHashedData(), baseEvent.getSignature(), consensusData);
}

/**
Expand All @@ -264,7 +236,7 @@ public void deserialize(final SerializableDataInputStream in, final int version)
* @param consensusEvent the consensus event to build from
*/
void buildFromConsensusEvent(final DetailedConsensusEvent consensusEvent) {
baseEvent = new GossipEvent(consensusEvent.getBaseEventHashedData(), consensusEvent.getBaseEventUnhashedData());
baseEvent = new GossipEvent(consensusEvent.getBaseEventHashedData(), consensusEvent.getSignature());
consensusData = consensusEvent.getConsensusData();
// clears metadata in case there is any
super.clear();
Expand Down Expand Up @@ -375,13 +347,6 @@ public BaseEventHashedData getHashedData() {
return baseEvent.getHashedData();
}

/**
* @return The part of a base event which is not hashed
*/
public BaseEventUnhashedData getUnhashedData() {
return baseEvent.getUnhashedData();
}

/**
* @return Consensus data calculated for an event
*/
Expand Down Expand Up @@ -441,7 +406,7 @@ public boolean isCreatedBy(final NodeId id) {
//////////////////////////////////////////

public byte[] getSignature() {
return baseEvent.getUnhashedData().getSignature();
return baseEvent.getSignature();
}

//////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ public EventStreamRoundIterator(
* @return an event impl with the same data as the detailed consensus event
*/
private static EventImpl convertToEventImpl(final DetailedConsensusEvent event) {
return new EventImpl(
event.getBaseEventHashedData(), event.getBaseEventUnhashedData(), event.getConsensusData());
return new EventImpl(event);
}

/**
Expand Down

0 comments on commit f3e66d3

Please sign in to comment.