From 5dc1841907c123d8c06a0866d85eab17f06df561 Mon Sep 17 00:00:00 2001 From: Lazar Petrovic Date: Mon, 22 Jul 2024 12:45:27 +0200 Subject: [PATCH 1/6] implemented Signed-off-by: Lazar Petrovic --- .../builder/PlatformComponentBuilder.java | 6 ++++- .../event/hashing/DefaultEventHasher.java | 23 +++++++++++++++++++ .../platform/event/hashing/PbjHasher.java | 20 ++++++++++++---- .../event/hashing/StatefulEventHasher.java | 8 +++---- .../event/hashing/UnsignedEventHasher.java | 16 +++++++++++++ .../platform/eventhandling/EventConfig.java | 6 ++++- .../test/fixtures/event/RandomEventUtils.java | 2 +- .../platform/test/consensus/TestIntake.java | 6 ++++- 8 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/UnsignedEventHasher.java diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/builder/PlatformComponentBuilder.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/builder/PlatformComponentBuilder.java index e0861b56f402..af00dcd08ffe 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/builder/PlatformComponentBuilder.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/builder/PlatformComponentBuilder.java @@ -67,6 +67,7 @@ import com.swirlds.platform.event.validation.InternalEventValidator; import com.swirlds.platform.eventhandling.DefaultTransactionHandler; import com.swirlds.platform.eventhandling.DefaultTransactionPrehandler; +import com.swirlds.platform.eventhandling.EventConfig; import com.swirlds.platform.eventhandling.TransactionHandler; import com.swirlds.platform.eventhandling.TransactionPrehandler; import com.swirlds.platform.gossip.SyncGossip; @@ -257,7 +258,10 @@ public PlatformComponentBuilder withEventHasher(@NonNull final EventHasher event @NonNull public EventHasher buildEventHasher() { if (eventHasher == null) { - eventHasher = new DefaultEventHasher(); + eventHasher = new DefaultEventHasher( + blocks.appVersion().getPbjSemanticVersion(), + blocks.platformContext().getConfiguration().getConfigData(EventConfig.class).migrateEventHashing() + ); } return eventHasher; } diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java index d09cce0303df..f818332b62dc 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java @@ -16,16 +16,39 @@ package com.swirlds.platform.event.hashing; +import com.hedera.hapi.node.base.SemanticVersion; import com.swirlds.platform.event.PlatformEvent; +import com.swirlds.state.spi.HapiUtils; import edu.umd.cs.findbugs.annotations.NonNull; /** * Default implementation of the {@link EventHasher}. */ public class DefaultEventHasher implements EventHasher { + private final SemanticVersion currentSoftwareVersion; + private final boolean migrateEventHashing; + + /** + * Constructs a new {@link DefaultEventHasher} with the given {@link SemanticVersion} and migration flag. + * + * @param currentSoftwareVersion the current software version + * @param migrateEventHashing if true then use the new event hashing algorithm for new events, events created by + * previous software versions will still need to be hashed using the old algorithm. + */ + public DefaultEventHasher(final SemanticVersion currentSoftwareVersion, final boolean migrateEventHashing) { + this.currentSoftwareVersion = currentSoftwareVersion; + this.migrateEventHashing = migrateEventHashing; + } + @Override @NonNull public PlatformEvent hashEvent(@NonNull final PlatformEvent event) { + if (migrateEventHashing + && HapiUtils.SEMANTIC_VERSION_COMPARATOR.compare(currentSoftwareVersion, event.getSoftwareVersion()) + == 0) { + new PbjHasher().hashEvent(event); + return event; + } new StatefulEventHasher().hashEvent(event); return event; } diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/PbjHasher.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/PbjHasher.java index 8ee0415e7a5e..42c5ca41fd56 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/PbjHasher.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/PbjHasher.java @@ -22,6 +22,7 @@ import com.swirlds.common.crypto.Hash; import com.swirlds.common.crypto.HashingOutputStream; import com.swirlds.platform.event.PlatformEvent; +import com.swirlds.platform.system.events.UnsignedEvent; import edu.umd.cs.findbugs.annotations.NonNull; import java.io.IOException; @@ -29,7 +30,7 @@ * Hashes the PBJ representation of an event. This hasher double hashes each payload in order to allow redaction of * payloads without invalidating the event hash. */ -public class PbjHasher implements EventHasher { +public class PbjHasher implements EventHasher, UnsignedEventHasher { /** The hashing stream for the event. */ private final HashingOutputStream eventHashingStream = new HashingOutputStream(DigestType.SHA_384.buildDigest()); @@ -37,9 +38,20 @@ public class PbjHasher implements EventHasher { private final HashingOutputStream payloadHashingStream = new HashingOutputStream(DigestType.SHA_384.buildDigest()); @Override + @NonNull public PlatformEvent hashEvent(@NonNull final PlatformEvent event) { - EventCore.PROTOBUF.toBytes(event.getUnsignedEvent().getEventCore()).writeTo(eventHashingStream); - event.getUnsignedEvent().getPayloads().forEach(payload -> { + hashUnsignedEvent(event.getUnsignedEvent()); + return event; + } + + /** + * Hashes the given {@link UnsignedEvent} and sets the hash on the event. + * + * @param event the event to hash + */ + public void hashUnsignedEvent(@NonNull final UnsignedEvent event) { + EventCore.PROTOBUF.toBytes(event.getEventCore()).writeTo(eventHashingStream); + event.getPayloads().forEach(payload -> { EventPayload.PROTOBUF.toBytes(payload).writeTo(payloadHashingStream); try { eventHashingStream.write(payloadHashingStream.getDigest()); @@ -49,7 +61,5 @@ public PlatformEvent hashEvent(@NonNull final PlatformEvent event) { }); event.setHash(new Hash(eventHashingStream.getDigest(), DigestType.SHA_384)); - - return event; } } diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/StatefulEventHasher.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/StatefulEventHasher.java index 1fe839be9069..185dd30643e7 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/StatefulEventHasher.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/StatefulEventHasher.java @@ -28,7 +28,7 @@ /** * An implementation of {@link EventHasher} that is stateful and thus not safe to use by multiple threads concurrently. */ -public class StatefulEventHasher implements EventHasher { +public class StatefulEventHasher implements EventHasher, UnsignedEventHasher { private final HashingOutputStream hashingOutputStream = new HashingOutputStream(DigestType.SHA_384.buildDigest()); private final SerializableDataOutputStream outputStream = new SerializableDataOutputStream(hashingOutputStream); @@ -54,14 +54,12 @@ public PlatformEvent hashEvent(@NonNull final PlatformEvent event) { * Hashes the given {@link UnsignedEvent} and sets the hash on the event. * * @param event the event to hash - * @return the hashed event */ - @NonNull - public UnsignedEvent hashEvent(@NonNull final UnsignedEvent event) { + @Override + public void hashUnsignedEvent(@NonNull final UnsignedEvent event) { try { event.serializeLegacyHashBytes(outputStream); event.setHash(new Hash(hashingOutputStream.getDigest(), DigestType.SHA_384)); - return event; } catch (final IOException e) { throw new RuntimeException("An exception occurred while trying to hash an event!", e); } diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/UnsignedEventHasher.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/UnsignedEventHasher.java new file mode 100644 index 000000000000..1ff4a20e1366 --- /dev/null +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/UnsignedEventHasher.java @@ -0,0 +1,16 @@ +package com.swirlds.platform.event.hashing; + +import com.swirlds.platform.system.events.UnsignedEvent; +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * Hashes unsigned events. + */ +public interface UnsignedEventHasher { + /** + * Hashes the event and builds the event descriptor. + * + * @param event the event to hash + */ + void hashUnsignedEvent(@NonNull final UnsignedEvent event); +} diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/eventhandling/EventConfig.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/eventhandling/EventConfig.java index 7b58ebf4fc9f..0c1c810ebbe5 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/eventhandling/EventConfig.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/eventhandling/EventConfig.java @@ -35,6 +35,9 @@ * ancient or not. Once this setting has been enabled on a network, it can * never be disabled again (migration pathway is one-way). * @param useOldStyleIntakeQueue if true then use an old style queue between gossip and the intake queue + * @param migrateEventHashing if true then use the new event hashing algorithm for new events, events + * created by previous software versions will still need to be hashed using the + * old algorithm. */ @ConfigData("event") public record EventConfig( @@ -44,7 +47,8 @@ public record EventConfig( @ConfigProperty(defaultValue = "/opt/hgcapp/eventsStreams") String eventsLogDir, @ConfigProperty(defaultValue = "true") boolean enableEventStreaming, @ConfigProperty(defaultValue = "false") boolean useBirthRoundAncientThreshold, - @ConfigProperty(defaultValue = "true") boolean useOldStyleIntakeQueue) { + @ConfigProperty(defaultValue = "true") boolean useOldStyleIntakeQueue, + @ConfigProperty(defaultValue = "false") boolean migrateEventHashing) { /** * @return the {@link AncientMode} based on useBirthRoundAncientThreshold diff --git a/platform-sdk/swirlds-platform-core/src/testFixtures/java/com/swirlds/platform/test/fixtures/event/RandomEventUtils.java b/platform-sdk/swirlds-platform-core/src/testFixtures/java/com/swirlds/platform/test/fixtures/event/RandomEventUtils.java index f80be4a0d900..764a61ec84ee 100644 --- a/platform-sdk/swirlds-platform-core/src/testFixtures/java/com/swirlds/platform/test/fixtures/event/RandomEventUtils.java +++ b/platform-sdk/swirlds-platform-core/src/testFixtures/java/com/swirlds/platform/test/fixtures/event/RandomEventUtils.java @@ -111,7 +111,7 @@ public static UnsignedEvent randomUnsignedEventWithTimestamp( if (fakeHash) { unsignedEvent.setHash(RandomUtils.randomHash(random)); } else { - new StatefulEventHasher().hashEvent(unsignedEvent); + new StatefulEventHasher().hashUnsignedEvent(unsignedEvent); } return unsignedEvent; } diff --git a/platform-sdk/swirlds-unit-tests/core/swirlds-platform-test/src/main/java/com/swirlds/platform/test/consensus/TestIntake.java b/platform-sdk/swirlds-unit-tests/core/swirlds-platform-test/src/main/java/com/swirlds/platform/test/consensus/TestIntake.java index 22c03da6f31f..ce9c2791951d 100644 --- a/platform-sdk/swirlds-unit-tests/core/swirlds-platform-test/src/main/java/com/swirlds/platform/test/consensus/TestIntake.java +++ b/platform-sdk/swirlds-unit-tests/core/swirlds-platform-test/src/main/java/com/swirlds/platform/test/consensus/TestIntake.java @@ -41,9 +41,11 @@ import com.swirlds.platform.event.hashing.EventHasher; import com.swirlds.platform.event.orphan.DefaultOrphanBuffer; import com.swirlds.platform.event.orphan.OrphanBuffer; +import com.swirlds.platform.eventhandling.EventConfig; import com.swirlds.platform.gossip.IntakeEventCounter; import com.swirlds.platform.gossip.NoOpIntakeEventCounter; import com.swirlds.platform.internal.ConsensusRound; +import com.swirlds.platform.system.BasicSoftwareVersion; import com.swirlds.platform.system.address.AddressBook; import com.swirlds.platform.test.consensus.framework.ConsensusOutput; import com.swirlds.platform.test.fixtures.event.IndexedEvent; @@ -82,7 +84,9 @@ public TestIntake(@NonNull final PlatformContext platformContext, @NonNull final model = WiringModelBuilder.create(platformContext).build(); hasherWiring = new ComponentWiring<>(model, EventHasher.class, directScheduler("eventHasher")); - final EventHasher eventHasher = new DefaultEventHasher(); + final EventHasher eventHasher = new DefaultEventHasher( + new BasicSoftwareVersion(1).getPbjSemanticVersion(), + platformContext.getConfiguration().getConfigData(EventConfig.class).migrateEventHashing()); hasherWiring.bind(eventHasher); final PassThroughWiring postHashCollectorWiring = From 848e1befd31a944bf36f0415dc218a73f04ca298 Mon Sep 17 00:00:00 2001 From: Lazar Petrovic Date: Mon, 22 Jul 2024 12:45:36 +0200 Subject: [PATCH 2/6] oops Signed-off-by: Lazar Petrovic --- .../event/creation/tipset/TipsetEventCreator.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/creation/tipset/TipsetEventCreator.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/creation/tipset/TipsetEventCreator.java index 1b0bdeacecda..e9434f628da5 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/creation/tipset/TipsetEventCreator.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/creation/tipset/TipsetEventCreator.java @@ -34,7 +34,9 @@ import com.swirlds.platform.event.PlatformEvent; import com.swirlds.platform.event.creation.EventCreationConfig; import com.swirlds.platform.event.creation.EventCreator; +import com.swirlds.platform.event.hashing.PbjHasher; import com.swirlds.platform.event.hashing.StatefulEventHasher; +import com.swirlds.platform.event.hashing.UnsignedEventHasher; import com.swirlds.platform.eventhandling.EventConfig; import com.swirlds.platform.system.SoftwareVersion; import com.swirlds.platform.system.address.AddressBook; @@ -119,7 +121,7 @@ public class TipsetEventCreator implements EventCreator { /** * Event hasher for unsigned events. */ - private final StatefulEventHasher statefulEventHasher = new StatefulEventHasher(); + private final UnsignedEventHasher eventHasher; /** * Create a new tipset event creator. @@ -169,6 +171,12 @@ public TipsetEventCreator( noParentFoundLogger = new RateLimitedLogger(logger, time, Duration.ofMinutes(1)); this.eventWindow = EventWindow.getGenesisEventWindow(ancientMode); + this.eventHasher = platformContext + .getConfiguration() + .getConfigData(EventConfig.class) + .migrateEventHashing() + ? new PbjHasher() + : new StatefulEventHasher(); } /** @@ -441,7 +449,7 @@ private UnsignedEvent assembleEventObject(@Nullable final EventDescriptor otherP : ConsensusConstants.ROUND_FIRST, timeCreated, transactionSupplier.getTransactions()); - statefulEventHasher.hashEvent(event); + eventHasher.hashUnsignedEvent(event); return event; } From 478ef68ac99cb29e98e624b4b127fe176a28912f Mon Sep 17 00:00:00 2001 From: Lazar Petrovic Date: Mon, 22 Jul 2024 12:50:51 +0200 Subject: [PATCH 3/6] spot Signed-off-by: Lazar Petrovic --- .../builder/PlatformComponentBuilder.java | 6 ++++-- .../creation/tipset/TipsetEventCreator.java | 6 +++--- .../event/hashing/DefaultEventHasher.java | 2 +- .../event/hashing/UnsignedEventHasher.java | 16 ++++++++++++++++ .../platform/test/consensus/TestIntake.java | 5 ++++- 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/builder/PlatformComponentBuilder.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/builder/PlatformComponentBuilder.java index af00dcd08ffe..db17da09268d 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/builder/PlatformComponentBuilder.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/builder/PlatformComponentBuilder.java @@ -260,8 +260,10 @@ public EventHasher buildEventHasher() { if (eventHasher == null) { eventHasher = new DefaultEventHasher( blocks.appVersion().getPbjSemanticVersion(), - blocks.platformContext().getConfiguration().getConfigData(EventConfig.class).migrateEventHashing() - ); + blocks.platformContext() + .getConfiguration() + .getConfigData(EventConfig.class) + .migrateEventHashing()); } return eventHasher; } diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/creation/tipset/TipsetEventCreator.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/creation/tipset/TipsetEventCreator.java index e9434f628da5..5f21ce9d3e0f 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/creation/tipset/TipsetEventCreator.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/creation/tipset/TipsetEventCreator.java @@ -172,9 +172,9 @@ public TipsetEventCreator( this.eventWindow = EventWindow.getGenesisEventWindow(ancientMode); this.eventHasher = platformContext - .getConfiguration() - .getConfigData(EventConfig.class) - .migrateEventHashing() + .getConfiguration() + .getConfigData(EventConfig.class) + .migrateEventHashing() ? new PbjHasher() : new StatefulEventHasher(); } diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java index f818332b62dc..535c2cb4e5ee 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java @@ -45,7 +45,7 @@ public DefaultEventHasher(final SemanticVersion currentSoftwareVersion, final bo public PlatformEvent hashEvent(@NonNull final PlatformEvent event) { if (migrateEventHashing && HapiUtils.SEMANTIC_VERSION_COMPARATOR.compare(currentSoftwareVersion, event.getSoftwareVersion()) - == 0) { + == 0) { new PbjHasher().hashEvent(event); return event; } diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/UnsignedEventHasher.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/UnsignedEventHasher.java index 1ff4a20e1366..33cd84051e1f 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/UnsignedEventHasher.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/UnsignedEventHasher.java @@ -1,3 +1,19 @@ +/* + * Copyright (C) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.swirlds.platform.event.hashing; import com.swirlds.platform.system.events.UnsignedEvent; diff --git a/platform-sdk/swirlds-unit-tests/core/swirlds-platform-test/src/main/java/com/swirlds/platform/test/consensus/TestIntake.java b/platform-sdk/swirlds-unit-tests/core/swirlds-platform-test/src/main/java/com/swirlds/platform/test/consensus/TestIntake.java index ce9c2791951d..617f1aa00430 100644 --- a/platform-sdk/swirlds-unit-tests/core/swirlds-platform-test/src/main/java/com/swirlds/platform/test/consensus/TestIntake.java +++ b/platform-sdk/swirlds-unit-tests/core/swirlds-platform-test/src/main/java/com/swirlds/platform/test/consensus/TestIntake.java @@ -86,7 +86,10 @@ public TestIntake(@NonNull final PlatformContext platformContext, @NonNull final hasherWiring = new ComponentWiring<>(model, EventHasher.class, directScheduler("eventHasher")); final EventHasher eventHasher = new DefaultEventHasher( new BasicSoftwareVersion(1).getPbjSemanticVersion(), - platformContext.getConfiguration().getConfigData(EventConfig.class).migrateEventHashing()); + platformContext + .getConfiguration() + .getConfigData(EventConfig.class) + .migrateEventHashing()); hasherWiring.bind(eventHasher); final PassThroughWiring postHashCollectorWiring = From e5b30489daf59fd31dae44a527a5c4dfdd258f13 Mon Sep 17 00:00:00 2001 From: Lazar Petrovic Date: Mon, 22 Jul 2024 12:54:28 +0200 Subject: [PATCH 4/6] fix Signed-off-by: Lazar Petrovic --- .../main/java/com/swirlds/platform/event/hashing/PbjHasher.java | 1 + 1 file changed, 1 insertion(+) diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/PbjHasher.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/PbjHasher.java index 42c5ca41fd56..d8863af33659 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/PbjHasher.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/PbjHasher.java @@ -41,6 +41,7 @@ public class PbjHasher implements EventHasher, UnsignedEventHasher { @NonNull public PlatformEvent hashEvent(@NonNull final PlatformEvent event) { hashUnsignedEvent(event.getUnsignedEvent()); + event.setHash(event.getUnsignedEvent().getHash()); return event; } From 50e3eea9c02cfea46efa30cf193c55311af4ea3f Mon Sep 17 00:00:00 2001 From: Lazar Petrovic Date: Mon, 22 Jul 2024 16:08:48 +0200 Subject: [PATCH 5/6] PR Signed-off-by: Lazar Petrovic --- .../swirlds/platform/event/hashing/DefaultEventHasher.java | 6 ++++-- .../java/com/swirlds/platform/event/hashing/PbjHasher.java | 2 ++ .../swirlds/platform/event/hashing/UnsignedEventHasher.java | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java index 535c2cb4e5ee..d0637a9800ea 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java @@ -20,6 +20,7 @@ import com.swirlds.platform.event.PlatformEvent; import com.swirlds.state.spi.HapiUtils; import edu.umd.cs.findbugs.annotations.NonNull; +import java.util.Objects; /** * Default implementation of the {@link EventHasher}. @@ -35,14 +36,15 @@ public class DefaultEventHasher implements EventHasher { * @param migrateEventHashing if true then use the new event hashing algorithm for new events, events created by * previous software versions will still need to be hashed using the old algorithm. */ - public DefaultEventHasher(final SemanticVersion currentSoftwareVersion, final boolean migrateEventHashing) { - this.currentSoftwareVersion = currentSoftwareVersion; + public DefaultEventHasher(@NonNull final SemanticVersion currentSoftwareVersion, final boolean migrateEventHashing) { + this.currentSoftwareVersion = Objects.requireNonNull(currentSoftwareVersion); this.migrateEventHashing = migrateEventHashing; } @Override @NonNull public PlatformEvent hashEvent(@NonNull final PlatformEvent event) { + Objects.requireNonNull(event); if (migrateEventHashing && HapiUtils.SEMANTIC_VERSION_COMPARATOR.compare(currentSoftwareVersion, event.getSoftwareVersion()) == 0) { diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/PbjHasher.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/PbjHasher.java index d8863af33659..db8f4963b3e5 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/PbjHasher.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/PbjHasher.java @@ -25,6 +25,7 @@ import com.swirlds.platform.system.events.UnsignedEvent; import edu.umd.cs.findbugs.annotations.NonNull; import java.io.IOException; +import java.util.Objects; /** * Hashes the PBJ representation of an event. This hasher double hashes each payload in order to allow redaction of @@ -40,6 +41,7 @@ public class PbjHasher implements EventHasher, UnsignedEventHasher { @Override @NonNull public PlatformEvent hashEvent(@NonNull final PlatformEvent event) { + Objects.requireNonNull(event); hashUnsignedEvent(event.getUnsignedEvent()); event.setHash(event.getUnsignedEvent().getHash()); return event; diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/UnsignedEventHasher.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/UnsignedEventHasher.java index 33cd84051e1f..acfc4a2d980a 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/UnsignedEventHasher.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/UnsignedEventHasher.java @@ -23,6 +23,7 @@ * Hashes unsigned events. */ public interface UnsignedEventHasher { + /** * Hashes the event and builds the event descriptor. * From 717d425ac1c8a0c2b7d82a3ed93e5a1c48e610dc Mon Sep 17 00:00:00 2001 From: Lazar Petrovic Date: Mon, 22 Jul 2024 16:09:02 +0200 Subject: [PATCH 6/6] s Signed-off-by: Lazar Petrovic --- .../com/swirlds/platform/event/hashing/DefaultEventHasher.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java index d0637a9800ea..d91fa9c07737 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/DefaultEventHasher.java @@ -36,7 +36,8 @@ public class DefaultEventHasher implements EventHasher { * @param migrateEventHashing if true then use the new event hashing algorithm for new events, events created by * previous software versions will still need to be hashed using the old algorithm. */ - public DefaultEventHasher(@NonNull final SemanticVersion currentSoftwareVersion, final boolean migrateEventHashing) { + public DefaultEventHasher( + @NonNull final SemanticVersion currentSoftwareVersion, final boolean migrateEventHashing) { this.currentSoftwareVersion = Objects.requireNonNull(currentSoftwareVersion); this.migrateEventHashing = migrateEventHashing; }