From 141a95bc09316fb353a10bdda8e8d9d92ff3fe21 Mon Sep 17 00:00:00 2001 From: Austin Littley Date: Tue, 19 Dec 2023 11:34:32 -0500 Subject: [PATCH] Create hasher classes Signed-off-by: Austin Littley --- .../platform/event/hashing/EventHasher.java | 50 ++++++++++++++++ .../wiring/components/EventHasherWiring.java | 57 +++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/EventHasher.java create mode 100644 platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/wiring/components/EventHasherWiring.java diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/EventHasher.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/EventHasher.java new file mode 100644 index 000000000000..a651b0a66853 --- /dev/null +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/EventHasher.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2023 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.common.context.PlatformContext; +import com.swirlds.common.crypto.Cryptography; +import com.swirlds.platform.event.GossipEvent; +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * Hashes events. + */ +public class EventHasher { + private final Cryptography cryptography; + + /** + * Constructs a new event hasher. + * + * @param platformContext the platform context + */ + public EventHasher(@NonNull final PlatformContext platformContext) { + this.cryptography = platformContext.getCryptography(); + } + + /** + * Hashes the event and builds the event descriptor. + * + * @param event the event to hash + * @return the hashed event + */ + public GossipEvent hashEvent(@NonNull final GossipEvent event) { + cryptography.digestSync(event.getHashedData()); + event.buildDescriptor(); + return event; + } +} diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/wiring/components/EventHasherWiring.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/wiring/components/EventHasherWiring.java new file mode 100644 index 000000000000..30ada78247f6 --- /dev/null +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/wiring/components/EventHasherWiring.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2023 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.wiring.components; + +import com.swirlds.common.wiring.schedulers.TaskScheduler; +import com.swirlds.common.wiring.wires.input.BindableInputWire; +import com.swirlds.common.wiring.wires.input.InputWire; +import com.swirlds.common.wiring.wires.output.OutputWire; +import com.swirlds.platform.event.GossipEvent; +import com.swirlds.platform.event.hashing.EventHasher; +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * Wiring for the {@link EventHasher}. + * + * @param eventInput the input wire for events to be hashed + * @param eventOutput the output wire for hashed events + * @param flushRunnable the runnable to flush the hasher + */ +public record EventHasherWiring( + @NonNull InputWire eventInput, + @NonNull OutputWire eventOutput, + @NonNull Runnable flushRunnable) { + /** + * Create a new instance of this wiring. + * + * @param taskScheduler the task scheduler for this wiring + * @return the new wiring instance + */ + public static EventHasherWiring create(@NonNull final TaskScheduler taskScheduler) { + return new EventHasherWiring( + taskScheduler.buildInputWire("events to hash"), taskScheduler.getOutputWire(), taskScheduler::flush); + } + + /** + * Bind an event hasher to this wiring. + * + * @param hasher the event hasher to bind + */ + public void bind(@NonNull final EventHasher hasher) { + ((BindableInputWire) eventInput).bind(hasher::hashEvent); + } +}