Skip to content

Commit

Permalink
Create hasher classes
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Littley <austin@swirldslabs.com>
  • Loading branch information
alittley committed Dec 19, 2023
1 parent e2ef0f6 commit 141a95b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
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;
}
}
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);
}
}

0 comments on commit 141a95b

Please sign in to comment.