-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Austin Littley <austin@swirldslabs.com>
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...k/swirlds-platform-core/src/main/java/com/swirlds/platform/event/hashing/EventHasher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...platform-core/src/main/java/com/swirlds/platform/wiring/components/EventHasherWiring.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<GossipEvent> eventInput, | ||
@NonNull OutputWire<GossipEvent> 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<GossipEvent> 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<GossipEvent, GossipEvent>) eventInput).bind(hasher::hashEvent); | ||
} | ||
} |