From 07c7e6cbd97170aeb7dac49833368a4ee5da13f3 Mon Sep 17 00:00:00 2001 From: Rob Date: Wed, 30 Oct 2024 12:30:52 -0400 Subject: [PATCH] hook identifiers --- cdn-broker/src/tasks/broker/handler.rs | 5 +++-- cdn-broker/src/tasks/user/handler.rs | 3 ++- cdn-proto/src/def.rs | 4 ++++ cdn-proto/src/util.rs | 10 ++++++++-- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cdn-broker/src/tasks/broker/handler.rs b/cdn-broker/src/tasks/broker/handler.rs index 3d00e72..9302490 100644 --- a/cdn-broker/src/tasks/broker/handler.rs +++ b/cdn-broker/src/tasks/broker/handler.rs @@ -15,7 +15,7 @@ use cdn_proto::{ discovery::BrokerIdentifier, error::{Error, Result}, message::{Message, Topic}, - util::mnemonic, + util::{hash, mnemonic}, verify_broker, }; use tokio::{spawn, time::timeout}; @@ -123,8 +123,9 @@ impl Inner { broker_identifier: &BrokerIdentifier, connection: Connection, ) -> Result<()> { - // Clone the hook + // Clone the hook and set its identifier let mut local_message_hook = self.broker_message_hook.clone(); + local_message_hook.set_identifier(hash(broker_identifier)); loop { // Receive a message from the broker diff --git a/cdn-broker/src/tasks/user/handler.rs b/cdn-broker/src/tasks/user/handler.rs index 50061b0..d43463c 100644 --- a/cdn-broker/src/tasks/user/handler.rs +++ b/cdn-broker/src/tasks/user/handler.rs @@ -13,7 +13,7 @@ use std::time::Duration; use cdn_proto::connection::{protocols::Connection, UserPublicKey}; use cdn_proto::def::{HookResult, MessageHookDef, RunDef, Topic as _}; use cdn_proto::error::{Error, Result}; -use cdn_proto::util::mnemonic; +use cdn_proto::util::{hash, mnemonic}; use cdn_proto::{connection::auth::broker::BrokerAuth, message::Message}; use tokio::spawn; use tokio::time::timeout; @@ -99,6 +99,7 @@ impl Inner { ) -> Result<()> { // Clone the hook let mut local_message_hook = self.user_message_hook.clone(); + local_message_hook.set_identifier(hash(public_key)); loop { // Receive a message from the user diff --git a/cdn-proto/src/def.rs b/cdn-proto/src/def.rs index f1bd918..0feaa05 100644 --- a/cdn-proto/src/def.rs +++ b/cdn-proto/src/def.rs @@ -85,6 +85,10 @@ pub trait MessageHookDef: Send + Sync + 'static + Clone { fn on_message_received(&mut self, _message: &mut Message) -> AnyhowResult { Ok(HookResult::ProcessMessage) } + + /// Set a unique identifier for the hook. This can be included with the hook and can be + /// used to deterministically identify message producers. + fn set_identifier(&mut self, _identifier: u64) {} } /// The no-op hook diff --git a/cdn-proto/src/util.rs b/cdn-proto/src/util.rs index 81aef83..0c12b1b 100644 --- a/cdn-proto/src/util.rs +++ b/cdn-proto/src/util.rs @@ -7,12 +7,18 @@ use std::{ use tokio::task::{JoinError, JoinHandle}; -/// A function for generating a cute little user mnemonic from a hash +/// A function for generating a cute little user mnemonic from a hashable value #[must_use] pub fn mnemonic(bytes: H) -> String { + mnemonic::to_string(hash(bytes).to_le_bytes()) +} + +/// A helper function for generating a 64-bit hash from a value +#[must_use] +pub fn hash(bytes: H) -> u64 { let mut state = std::collections::hash_map::DefaultHasher::new(); bytes.hash(&mut state); - mnemonic::to_string(state.finish().to_le_bytes()) + state.finish() } /// A wrapper for a `JoinHandle` that will abort the task if dropped