From 63dd7a5a0d62ba754670b22f9245191252e00f86 Mon Sep 17 00:00:00 2001 From: ryardley Date: Fri, 28 Nov 2025 20:02:54 +0000 Subject: [PATCH 01/36] prefactor enclave event --- crates/events/src/e3id.rs | 2 +- crates/events/src/enclave_event/mod.rs | 248 ++++++------------------ packages/enclave-sdk/src/enclave-sdk.ts | 6 +- 3 files changed, 65 insertions(+), 191 deletions(-) diff --git a/crates/events/src/e3id.rs b/crates/events/src/e3id.rs index 59ee11e767..716ee90816 100644 --- a/crates/events/src/e3id.rs +++ b/crates/events/src/e3id.rs @@ -9,7 +9,7 @@ use alloy_primitives::ruint::ParseError; use core::fmt; use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct E3id { id: String, chain_id: u64, diff --git a/crates/events/src/enclave_event/mod.rs b/crates/events/src/enclave_event/mod.rs index 4c6cb2cf39..7fbaad0941 100644 --- a/crates/events/src/enclave_event/mod.rs +++ b/crates/events/src/enclave_event/mod.rs @@ -74,9 +74,10 @@ macro_rules! impl_from_event { $( impl From<$variant> for EnclaveEvent { fn from(data: $variant) -> Self { - EnclaveEvent::$variant { - id: EventId::hash(data.clone()), - data: data.clone(), + let id = EventId::hash(&data); + EnclaveEvent { + payload: EnclaveEventData::$variant(data), + id } } } @@ -84,114 +85,42 @@ macro_rules! impl_from_event { }; } +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum EnclaveEventData { + KeyshareCreated(KeyshareCreated), + E3Requested(E3Requested), + PublicKeyAggregated(PublicKeyAggregated), + CiphertextOutputPublished(CiphertextOutputPublished), + DecryptionshareCreated(DecryptionshareCreated), + PlaintextAggregated(PlaintextAggregated), + PublishDocumentRequested(PublishDocumentRequested), + CiphernodeSelected(CiphernodeSelected), + CiphernodeAdded(CiphernodeAdded), + CiphernodeRemoved(CiphernodeRemoved), + TicketBalanceUpdated(TicketBalanceUpdated), + ConfigurationUpdated(ConfigurationUpdated), + OperatorActivationChanged(OperatorActivationChanged), + CommitteePublished(CommitteePublished), + CommitteeRequested(CommitteeRequested), + CommitteeFinalizeRequested(CommitteeFinalizeRequested), + CommitteeFinalized(CommitteeFinalized), + TicketGenerated(TicketGenerated), + TicketSubmitted(TicketSubmitted), + PlaintextOutputPublished(PlaintextOutputPublished), + EnclaveError(EnclaveError), + E3RequestComplete(E3RequestComplete), + Shutdown(Shutdown), + DocumentReceived(DocumentReceived), + ThresholdShareCreated(ThresholdShareCreated), + /// This is a test event to use in testing + TestEvent(TestEvent), +} + #[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] #[rtype(result = "()")] -pub enum EnclaveEvent { - KeyshareCreated { - id: EventId, - data: KeyshareCreated, - }, - E3Requested { - id: EventId, - data: E3Requested, - }, - PublicKeyAggregated { - id: EventId, - data: PublicKeyAggregated, - }, - CiphertextOutputPublished { - id: EventId, - data: CiphertextOutputPublished, - }, - DecryptionshareCreated { - id: EventId, - data: DecryptionshareCreated, - }, - PlaintextAggregated { - id: EventId, - data: PlaintextAggregated, - }, - PublishDocumentRequested { - id: EventId, - data: PublishDocumentRequested, - }, - CiphernodeSelected { - id: EventId, - data: CiphernodeSelected, - }, - CiphernodeAdded { - id: EventId, - data: CiphernodeAdded, - }, - CiphernodeRemoved { - id: EventId, - data: CiphernodeRemoved, - }, - TicketBalanceUpdated { - id: EventId, - data: TicketBalanceUpdated, - }, - ConfigurationUpdated { - id: EventId, - data: ConfigurationUpdated, - }, - OperatorActivationChanged { - id: EventId, - data: OperatorActivationChanged, - }, - CommitteePublished { - id: EventId, - data: CommitteePublished, - }, - CommitteeRequested { - id: EventId, - data: CommitteeRequested, - }, - CommitteeFinalizeRequested { - id: EventId, - data: CommitteeFinalizeRequested, - }, - CommitteeFinalized { - id: EventId, - data: CommitteeFinalized, - }, - TicketGenerated { - id: EventId, - data: TicketGenerated, - }, - TicketSubmitted { - id: EventId, - data: TicketSubmitted, - }, - PlaintextOutputPublished { - id: EventId, - data: PlaintextOutputPublished, - }, - EnclaveError { - id: EventId, - data: EnclaveError, - }, - E3RequestComplete { - id: EventId, - data: E3RequestComplete, - }, - Shutdown { - id: EventId, - data: Shutdown, - }, - DocumentReceived { - id: EventId, - data: DocumentReceived, - }, - ThresholdShareCreated { - id: EventId, - data: ThresholdShareCreated, - }, - /// This is a test event to use in testing - TestEvent { - id: EventId, - data: TestEvent, - }, +pub struct EnclaveEvent { + id: EventId, + payload: EnclaveEventData, } impl EnclaveEvent { @@ -225,8 +154,8 @@ impl ErrorEvent for EnclaveEvent { type Error = EnclaveError; type ErrorType = EnclaveErrorType; fn as_error(&self) -> Option<&Self::Error> { - match self { - EnclaveEvent::EnclaveError { data, .. } => Some(data), + match self.payload { + EnclaveEventData::EnclaveError(ref data) => Some(data), _ => None, } } @@ -238,88 +167,33 @@ impl ErrorEvent for EnclaveEvent { impl From for EventId { fn from(value: EnclaveEvent) -> Self { - match value { - EnclaveEvent::KeyshareCreated { id, .. } => id, - EnclaveEvent::E3Requested { id, .. } => id, - EnclaveEvent::PublicKeyAggregated { id, .. } => id, - EnclaveEvent::CiphertextOutputPublished { id, .. } => id, - EnclaveEvent::DecryptionshareCreated { id, .. } => id, - EnclaveEvent::PlaintextAggregated { id, .. } => id, - EnclaveEvent::PublishDocumentRequested { id, .. } => id, - EnclaveEvent::CiphernodeSelected { id, .. } => id, - EnclaveEvent::CiphernodeAdded { id, .. } => id, - EnclaveEvent::CiphernodeRemoved { id, .. } => id, - EnclaveEvent::TicketBalanceUpdated { id, .. } => id, - EnclaveEvent::ConfigurationUpdated { id, .. } => id, - EnclaveEvent::OperatorActivationChanged { id, .. } => id, - EnclaveEvent::CommitteePublished { id, .. } => id, - EnclaveEvent::CommitteeRequested { id, .. } => id, - EnclaveEvent::CommitteeFinalizeRequested { id, .. } => id, - EnclaveEvent::PlaintextOutputPublished { id, .. } => id, - EnclaveEvent::EnclaveError { id, .. } => id, - EnclaveEvent::E3RequestComplete { id, .. } => id, - EnclaveEvent::Shutdown { id, .. } => id, - EnclaveEvent::TestEvent { id, .. } => id, - EnclaveEvent::DocumentReceived { id, .. } => id, - EnclaveEvent::ThresholdShareCreated { id, .. } => id, - EnclaveEvent::CommitteeFinalized { id, .. } => id, - EnclaveEvent::TicketGenerated { id, .. } => id, - EnclaveEvent::TicketSubmitted { id, .. } => id, - } + value.id } } impl EnclaveEvent { pub fn get_e3_id(&self) -> Option { - match self.clone() { - EnclaveEvent::KeyshareCreated { data, .. } => Some(data.e3_id), - EnclaveEvent::E3Requested { data, .. } => Some(data.e3_id), - EnclaveEvent::PublicKeyAggregated { data, .. } => Some(data.e3_id), - EnclaveEvent::CiphertextOutputPublished { data, .. } => Some(data.e3_id), - EnclaveEvent::DecryptionshareCreated { data, .. } => Some(data.e3_id), - EnclaveEvent::PlaintextAggregated { data, .. } => Some(data.e3_id), - EnclaveEvent::CiphernodeSelected { data, .. } => Some(data.e3_id), - EnclaveEvent::ThresholdShareCreated { data, .. } => Some(data.e3_id), - EnclaveEvent::CommitteePublished { data, .. } => Some(data.e3_id), - EnclaveEvent::CommitteeRequested { data, .. } => Some(data.e3_id), - EnclaveEvent::CommitteeFinalizeRequested { data, .. } => Some(data.e3_id), - EnclaveEvent::PlaintextOutputPublished { data, .. } => Some(data.e3_id), - EnclaveEvent::CommitteeFinalized { data, .. } => Some(data.e3_id), - EnclaveEvent::TicketGenerated { data, .. } => Some(data.e3_id), - EnclaveEvent::TicketSubmitted { data, .. } => Some(data.e3_id), + match self.payload { + EnclaveEventData::KeyshareCreated(ref data) => Some(data.e3_id), + EnclaveEventData::E3Requested(ref data) => Some(data.e3_id), + EnclaveEventData::PublicKeyAggregated(ref data) => Some(data.e3_id), + EnclaveEventData::CiphertextOutputPublished(ref data) => Some(data.e3_id), + EnclaveEventData::DecryptionshareCreated(ref data) => Some(data.e3_id), + EnclaveEventData::PlaintextAggregated(ref data) => Some(data.e3_id), + EnclaveEventData::CiphernodeSelected(ref data) => Some(data.e3_id), + EnclaveEventData::ThresholdShareCreated(ref data) => Some(data.e3_id), + EnclaveEventData::CommitteePublished(ref data) => Some(data.e3_id), + EnclaveEventData::CommitteeRequested(ref data) => Some(data.e3_id), + EnclaveEventData::CommitteeFinalizeRequested(ref data) => Some(data.e3_id), + EnclaveEventData::PlaintextOutputPublished(ref data) => Some(data.e3_id), + EnclaveEventData::CommitteeFinalized(ref data) => Some(data.e3_id), + EnclaveEventData::TicketGenerated(ref data) => Some(data.e3_id), + EnclaveEventData::TicketSubmitted(ref data) => Some(data.e3_id), _ => None, } } - pub fn get_data(&self) -> String { - match self.clone() { - EnclaveEvent::KeyshareCreated { data, .. } => format!("{}", data), - EnclaveEvent::E3Requested { data, .. } => format!("{}", data), - EnclaveEvent::PublicKeyAggregated { data, .. } => format!("{}", data), - EnclaveEvent::CiphertextOutputPublished { data, .. } => format!("{}", data), - EnclaveEvent::DecryptionshareCreated { data, .. } => format!("{}", data), - EnclaveEvent::PlaintextAggregated { data, .. } => format!("{}", data), - EnclaveEvent::PublishDocumentRequested { data, .. } => format!("{}", data), - EnclaveEvent::CiphernodeSelected { data, .. } => format!("{}", data), - EnclaveEvent::CiphernodeAdded { data, .. } => format!("{}", data), - EnclaveEvent::CiphernodeRemoved { data, .. } => format!("{}", data), - EnclaveEvent::TicketBalanceUpdated { data, .. } => format!("{:?}", data), - EnclaveEvent::ConfigurationUpdated { data, .. } => format!("{:?}", data), - EnclaveEvent::OperatorActivationChanged { data, .. } => format!("{:?}", data), - EnclaveEvent::CommitteePublished { data, .. } => format!("{:?}", data), - EnclaveEvent::CommitteeRequested { data, .. } => format!("{:?}", data), - EnclaveEvent::CommitteeFinalizeRequested { data, .. } => format!("{:?}", data), - EnclaveEvent::PlaintextOutputPublished { data, .. } => format!("{:?}", data), - EnclaveEvent::E3RequestComplete { data, .. } => format!("{}", data), - EnclaveEvent::EnclaveError { data, .. } => format!("{:?}", data), - EnclaveEvent::Shutdown { data, .. } => format!("{:?}", data), - EnclaveEvent::ThresholdShareCreated { data, .. } => format!("{:?}", data), - EnclaveEvent::TestEvent { data, .. } => format!("{:?}", data), - EnclaveEvent::DocumentReceived { data, .. } => format!("{:?}", data), - EnclaveEvent::CommitteeFinalized { data, .. } => format!("{:?}", data), - EnclaveEvent::TicketGenerated { data, .. } => format!("{:?}", data), - EnclaveEvent::TicketSubmitted { data, .. } => format!("{:?}", data), - // _ => "".to_string(), - } + pub fn get_data(&self) -> &EnclaveEventData { + &self.payload } } @@ -370,7 +244,7 @@ impl TryFrom<&EnclaveEvent> for EnclaveError { impl TryFrom for EnclaveError { type Error = anyhow::Error; fn try_from(value: EnclaveEvent) -> Result { - if let EnclaveEvent::EnclaveError { data, .. } = value.clone() { + if let EnclaveEventData::EnclaveError(data) = value.payload.clone() { Ok(data) } else { return Err(anyhow::anyhow!("Not an enclave error {:?}", value)); @@ -380,7 +254,7 @@ impl TryFrom for EnclaveError { impl fmt::Display for EnclaveEvent { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(&format!("{}({})", self.event_type(), self.get_data())) + f.write_str(&format!("{:?}", self)) } } diff --git a/packages/enclave-sdk/src/enclave-sdk.ts b/packages/enclave-sdk/src/enclave-sdk.ts index b3c2dbc681..bf29dbbdc4 100644 --- a/packages/enclave-sdk/src/enclave-sdk.ts +++ b/packages/enclave-sdk/src/enclave-sdk.ts @@ -508,9 +508,9 @@ export class EnclaveSDK { const isWebSocket = options.rpcUrl.startsWith('ws://') || options.rpcUrl.startsWith('wss://') const transport = isWebSocket ? webSocket(options.rpcUrl, { - keepAlive: { interval: 30_000 }, - reconnect: { attempts: 5, delay: 2_000 }, - }) + keepAlive: { interval: 30_000 }, + reconnect: { attempts: 5, delay: 2_000 }, + }) : http(options.rpcUrl) const publicClient = createPublicClient({ chain, From a376534e8f2907a93111f1b4ee819244972db5da Mon Sep 17 00:00:00 2001 From: ryardley Date: Fri, 28 Nov 2025 20:05:10 +0000 Subject: [PATCH 02/36] formatting --- packages/enclave-sdk/src/enclave-sdk.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/enclave-sdk/src/enclave-sdk.ts b/packages/enclave-sdk/src/enclave-sdk.ts index bf29dbbdc4..b3c2dbc681 100644 --- a/packages/enclave-sdk/src/enclave-sdk.ts +++ b/packages/enclave-sdk/src/enclave-sdk.ts @@ -508,9 +508,9 @@ export class EnclaveSDK { const isWebSocket = options.rpcUrl.startsWith('ws://') || options.rpcUrl.startsWith('wss://') const transport = isWebSocket ? webSocket(options.rpcUrl, { - keepAlive: { interval: 30_000 }, - reconnect: { attempts: 5, delay: 2_000 }, - }) + keepAlive: { interval: 30_000 }, + reconnect: { attempts: 5, delay: 2_000 }, + }) : http(options.rpcUrl) const publicClient = createPublicClient({ chain, From 067a4b5555cac3d59dfea356f8a2573d797d4105 Mon Sep 17 00:00:00 2001 From: ryardley Date: Fri, 28 Nov 2025 20:51:34 +0000 Subject: [PATCH 03/36] compiling --- crates/aggregator/src/committee_finalizer.rs | 9 ++--- crates/aggregator/src/ext.rs | 8 ++--- crates/aggregator/src/plaintext_aggregator.rs | 10 +++--- crates/aggregator/src/publickey_aggregator.rs | 9 ++--- .../src/threshold_plaintext_aggregator.rs | 10 +++--- crates/data/src/sled_store.rs | 5 +-- crates/events/src/e3id.rs | 2 +- crates/events/src/enclave_event/mod.rs | 33 ++++++++++--------- crates/evm/src/ciphernode_registry_sol.rs | 14 ++++---- crates/evm/src/enclave_sol_writer.rs | 7 ++-- crates/evm/src/event_reader.rs | 6 ++-- crates/fhe/src/ext.rs | 6 ++-- crates/keyshare/src/ext.rs | 6 ++-- crates/keyshare/src/keyshare.rs | 13 ++++---- crates/keyshare/src/threshold_keyshare.rs | 12 +++---- crates/logger/src/logger.rs | 6 ++-- crates/net/src/document_publisher.rs | 28 ++++++++-------- crates/net/src/net_event_translator.rs | 11 ++++--- crates/request/src/meta.rs | 4 +-- crates/request/src/router.rs | 13 ++++---- crates/sortition/src/ciphernode_selector.rs | 12 +++---- crates/sortition/src/sortition.rs | 19 ++++++----- crates/test-helpers/src/plaintext_writer.rs | 4 +-- crates/test-helpers/src/public_key_writer.rs | 4 +-- 24 files changed, 132 insertions(+), 119 deletions(-) diff --git a/crates/aggregator/src/committee_finalizer.rs b/crates/aggregator/src/committee_finalizer.rs index 587c8311ea..e8e6afcf56 100644 --- a/crates/aggregator/src/committee_finalizer.rs +++ b/crates/aggregator/src/committee_finalizer.rs @@ -6,7 +6,8 @@ use actix::prelude::*; use e3_events::{ - CommitteeFinalizeRequested, CommitteeRequested, EnclaveEvent, EventBus, Shutdown, Subscribe, + CommitteeFinalizeRequested, CommitteeRequested, EnclaveEvent, EnclaveEventData, EventBus, + Shutdown, Subscribe, }; use std::collections::HashMap; use std::time::Duration; @@ -47,9 +48,9 @@ impl Actor for CommitteeFinalizer { impl Handler for CommitteeFinalizer { type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { - match msg { - EnclaveEvent::CommitteeRequested { data, .. } => ctx.notify(data), - EnclaveEvent::Shutdown { data, .. } => ctx.notify(data), + match msg.into_data() { + EnclaveEventData::CommitteeRequested(data) => ctx.notify(data), + EnclaveEventData::Shutdown(data) => ctx.notify(data), _ => (), } } diff --git a/crates/aggregator/src/ext.rs b/crates/aggregator/src/ext.rs index 703566af9a..88fcab5013 100644 --- a/crates/aggregator/src/ext.rs +++ b/crates/aggregator/src/ext.rs @@ -15,7 +15,7 @@ use actix::{Actor, Addr}; use anyhow::{anyhow, Result}; use async_trait::async_trait; use e3_data::{AutoPersist, RepositoriesFactory}; -use e3_events::{BusError, EnclaveErrorType, EnclaveEvent, EventBus}; +use e3_events::{BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus}; use e3_fhe::ext::FHE_KEY; use e3_multithread::Multithread; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, META_KEY}; @@ -43,7 +43,7 @@ const ERROR_PLAINTEXT_META_MISSING:&str = "Could not create PlaintextAggregator impl E3Extension for PlaintextAggregatorExtension { fn on_event(&self, ctx: &mut E3Context, evt: &EnclaveEvent) { // Save plaintext aggregator - let EnclaveEvent::CiphertextOutputPublished { data, .. } = evt else { + let EnclaveEventData::CiphertextOutputPublished(data) = evt.get_data() else { return; }; @@ -163,7 +163,7 @@ const ERROR_PUBKEY_META_MISSING:&str = "Could not create PublicKeyAggregator bec impl E3Extension for PublicKeyAggregatorExtension { fn on_event(&self, ctx: &mut E3Context, evt: &EnclaveEvent) { // Saving the publickey aggregator with deps on E3Requested - let EnclaveEvent::E3Requested { data, .. } = evt else { + let EnclaveEventData::E3Requested(data) = evt.get_data() else { return; }; @@ -274,7 +274,7 @@ const ERROR_TRBFV_PLAINTEXT_META_MISSING:&str = "Could not create ThresholdPlain impl E3Extension for ThresholdPlaintextAggregatorExtension { fn on_event(&self, ctx: &mut E3Context, evt: &EnclaveEvent) { // Save plaintext aggregator - let EnclaveEvent::CiphertextOutputPublished { data, .. } = evt else { + let EnclaveEventData::CiphertextOutputPublished(data) = evt.get_data() else { return; }; diff --git a/crates/aggregator/src/plaintext_aggregator.rs b/crates/aggregator/src/plaintext_aggregator.rs index 9b6dae9ff3..0fa37509d3 100644 --- a/crates/aggregator/src/plaintext_aggregator.rs +++ b/crates/aggregator/src/plaintext_aggregator.rs @@ -8,8 +8,8 @@ use actix::prelude::*; use anyhow::Result; use e3_data::Persistable; use e3_events::{ - DecryptionshareCreated, Die, E3id, EnclaveEvent, EventBus, OrderedSet, PlaintextAggregated, - Seed, + DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, EventBus, OrderedSet, + PlaintextAggregated, Seed, }; use e3_fhe::{Fhe, GetAggregatePlaintext}; use e3_sortition::{GetNodeIndex, Sortition}; @@ -145,9 +145,9 @@ impl Actor for PlaintextAggregator { impl Handler for PlaintextAggregator { type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { - match msg { - EnclaveEvent::DecryptionshareCreated { data, .. } => ctx.notify(data), - EnclaveEvent::E3RequestComplete { .. } => ctx.notify(Die), + match msg.into_data() { + EnclaveEventData::DecryptionshareCreated(data) => ctx.notify(data), + EnclaveEventData::E3RequestComplete(_) => ctx.notify(Die), _ => (), } } diff --git a/crates/aggregator/src/publickey_aggregator.rs b/crates/aggregator/src/publickey_aggregator.rs index af7664c5f4..562ac94d31 100644 --- a/crates/aggregator/src/publickey_aggregator.rs +++ b/crates/aggregator/src/publickey_aggregator.rs @@ -8,7 +8,8 @@ use actix::prelude::*; use anyhow::Result; use e3_data::Persistable; use e3_events::{ - Die, E3id, EnclaveEvent, EventBus, KeyshareCreated, OrderedSet, PublicKeyAggregated, Seed, + Die, E3id, EnclaveEvent, EnclaveEventData, EventBus, KeyshareCreated, OrderedSet, + PublicKeyAggregated, Seed, }; use e3_fhe::{Fhe, GetAggregatePublicKey}; use e3_sortition::{GetNodesForE3, Sortition}; @@ -136,9 +137,9 @@ impl Actor for PublicKeyAggregator { impl Handler for PublicKeyAggregator { type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { - match msg { - EnclaveEvent::KeyshareCreated { data, .. } => ctx.notify(data), - EnclaveEvent::E3RequestComplete { .. } => ctx.notify(Die), + match msg.into_data() { + EnclaveEventData::KeyshareCreated(data) => ctx.notify(data), + EnclaveEventData::E3RequestComplete(_) => ctx.notify(Die), _ => (), } } diff --git a/crates/aggregator/src/threshold_plaintext_aggregator.rs b/crates/aggregator/src/threshold_plaintext_aggregator.rs index 092b438b84..73d87ad0d5 100644 --- a/crates/aggregator/src/threshold_plaintext_aggregator.rs +++ b/crates/aggregator/src/threshold_plaintext_aggregator.rs @@ -10,8 +10,8 @@ use actix::prelude::*; use anyhow::{anyhow, bail, Result}; use e3_data::Persistable; use e3_events::{ - ComputeRequest, DecryptionshareCreated, Die, E3id, EnclaveEvent, EventBus, PlaintextAggregated, - Seed, + ComputeRequest, DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, EventBus, + PlaintextAggregated, Seed, }; use e3_multithread::Multithread; use e3_sortition::{GetNodesForE3, Sortition}; @@ -235,9 +235,9 @@ impl Actor for ThresholdPlaintextAggregator { impl Handler for ThresholdPlaintextAggregator { type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { - match msg { - EnclaveEvent::DecryptionshareCreated { data, .. } => ctx.notify(data), - EnclaveEvent::E3RequestComplete { .. } => ctx.notify(Die), + match msg.into_data() { + EnclaveEventData::DecryptionshareCreated(data) => ctx.notify(data), + EnclaveEventData::E3RequestComplete(_) => ctx.notify(Die), _ => (), } } diff --git a/crates/data/src/sled_store.rs b/crates/data/src/sled_store.rs index c45ac70463..a8de536cc9 100644 --- a/crates/data/src/sled_store.rs +++ b/crates/data/src/sled_store.rs @@ -8,7 +8,8 @@ use crate::{Get, Insert, InsertSync, Remove}; use actix::{Actor, ActorContext, Addr, Handler}; use anyhow::{Context, Result}; use e3_events::{ - get_enclave_event_bus, BusError, EnclaveErrorType, EnclaveEvent, EventBus, Subscribe, + get_enclave_event_bus, BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, + Subscribe, }; use once_cell::sync::Lazy; use sled::Db; @@ -112,7 +113,7 @@ impl Handler for SledStore { impl Handler for SledStore { type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { - if let EnclaveEvent::Shutdown { .. } = msg { + if let EnclaveEventData::Shutdown { .. } = msg.get_data() { let _db = self.db.take(); // db will be dropped ctx.stop() } diff --git a/crates/events/src/e3id.rs b/crates/events/src/e3id.rs index 716ee90816..59ee11e767 100644 --- a/crates/events/src/e3id.rs +++ b/crates/events/src/e3id.rs @@ -9,7 +9,7 @@ use alloy_primitives::ruint::ParseError; use core::fmt; use serde::{Deserialize, Serialize}; -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct E3id { id: String, chain_id: u64, diff --git a/crates/events/src/enclave_event/mod.rs b/crates/events/src/enclave_event/mod.rs index 7fbaad0941..d50568ccc1 100644 --- a/crates/events/src/enclave_event/mod.rs +++ b/crates/events/src/enclave_event/mod.rs @@ -174,27 +174,30 @@ impl From for EventId { impl EnclaveEvent { pub fn get_e3_id(&self) -> Option { match self.payload { - EnclaveEventData::KeyshareCreated(ref data) => Some(data.e3_id), - EnclaveEventData::E3Requested(ref data) => Some(data.e3_id), - EnclaveEventData::PublicKeyAggregated(ref data) => Some(data.e3_id), - EnclaveEventData::CiphertextOutputPublished(ref data) => Some(data.e3_id), - EnclaveEventData::DecryptionshareCreated(ref data) => Some(data.e3_id), - EnclaveEventData::PlaintextAggregated(ref data) => Some(data.e3_id), - EnclaveEventData::CiphernodeSelected(ref data) => Some(data.e3_id), - EnclaveEventData::ThresholdShareCreated(ref data) => Some(data.e3_id), - EnclaveEventData::CommitteePublished(ref data) => Some(data.e3_id), - EnclaveEventData::CommitteeRequested(ref data) => Some(data.e3_id), - EnclaveEventData::CommitteeFinalizeRequested(ref data) => Some(data.e3_id), - EnclaveEventData::PlaintextOutputPublished(ref data) => Some(data.e3_id), - EnclaveEventData::CommitteeFinalized(ref data) => Some(data.e3_id), - EnclaveEventData::TicketGenerated(ref data) => Some(data.e3_id), - EnclaveEventData::TicketSubmitted(ref data) => Some(data.e3_id), + EnclaveEventData::KeyshareCreated(ref data) => Some(data.e3_id.clone()), + EnclaveEventData::E3Requested(ref data) => Some(data.e3_id.clone()), + EnclaveEventData::PublicKeyAggregated(ref data) => Some(data.e3_id.clone()), + EnclaveEventData::CiphertextOutputPublished(ref data) => Some(data.e3_id.clone()), + EnclaveEventData::DecryptionshareCreated(ref data) => Some(data.e3_id.clone()), + EnclaveEventData::PlaintextAggregated(ref data) => Some(data.e3_id.clone()), + EnclaveEventData::CiphernodeSelected(ref data) => Some(data.e3_id.clone()), + EnclaveEventData::ThresholdShareCreated(ref data) => Some(data.e3_id.clone()), + EnclaveEventData::CommitteePublished(ref data) => Some(data.e3_id.clone()), + EnclaveEventData::CommitteeRequested(ref data) => Some(data.e3_id.clone()), + EnclaveEventData::CommitteeFinalizeRequested(ref data) => Some(data.e3_id.clone()), + EnclaveEventData::PlaintextOutputPublished(ref data) => Some(data.e3_id.clone()), + EnclaveEventData::CommitteeFinalized(ref data) => Some(data.e3_id.clone()), + EnclaveEventData::TicketGenerated(ref data) => Some(data.e3_id.clone()), + EnclaveEventData::TicketSubmitted(ref data) => Some(data.e3_id.clone()), _ => None, } } pub fn get_data(&self) -> &EnclaveEventData { &self.payload } + pub fn into_data(self) -> EnclaveEventData { + self.payload + } } impl_from_event!( diff --git a/crates/evm/src/ciphernode_registry_sol.rs b/crates/evm/src/ciphernode_registry_sol.rs index 2e12c08c08..3428be9a42 100644 --- a/crates/evm/src/ciphernode_registry_sol.rs +++ b/crates/evm/src/ciphernode_registry_sol.rs @@ -19,8 +19,8 @@ use anyhow::Result; use e3_data::Repository; use e3_events::{ BusError, CommitteeFinalizeRequested, CommitteeFinalized, E3id, EnclaveErrorType, EnclaveEvent, - EventBus, OrderedSet, PublicKeyAggregated, Seed, Shutdown, Subscribe, TicketGenerated, - TicketId, + EnclaveEventData, EventBus, OrderedSet, PublicKeyAggregated, Seed, Shutdown, Subscribe, + TicketGenerated, TicketId, }; use tracing::{error, info, trace}; @@ -312,25 +312,25 @@ impl Handler type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { - match msg { - EnclaveEvent::PublicKeyAggregated { data, .. } => { + match msg.into_data() { + EnclaveEventData::PublicKeyAggregated(data) => { // Only publish if the src and destination chains match if self.provider.chain_id() == data.e3_id.chain_id() { ctx.notify(data); } } - EnclaveEvent::CommitteeFinalizeRequested { data, .. } => { + EnclaveEventData::CommitteeFinalizeRequested(data) => { if self.provider.chain_id() == data.e3_id.chain_id() { ctx.notify(data); } } - EnclaveEvent::TicketGenerated { data, .. } => { + EnclaveEventData::TicketGenerated(data) => { // Submit ticket if chain matches if self.provider.chain_id() == data.e3_id.chain_id() { ctx.notify(data); } } - EnclaveEvent::Shutdown { data, .. } => ctx.notify(data), + EnclaveEventData::Shutdown(data) => ctx.notify(data), _ => (), } } diff --git a/crates/evm/src/enclave_sol_writer.rs b/crates/evm/src/enclave_sol_writer.rs index 66b6b3816c..a77e543831 100644 --- a/crates/evm/src/enclave_sol_writer.rs +++ b/crates/evm/src/enclave_sol_writer.rs @@ -17,6 +17,7 @@ use alloy::{ rpc::types::TransactionReceipt, }; use anyhow::Result; +use e3_events::EnclaveEventData; use e3_events::Shutdown; use e3_events::{BusError, E3id, EnclaveErrorType, PlaintextAggregated, Subscribe}; use e3_events::{EnclaveEvent, EventBus}; @@ -73,14 +74,14 @@ impl Handler for E type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { - match msg { - EnclaveEvent::PlaintextAggregated { data, .. } => { + match msg.into_data() { + EnclaveEventData::PlaintextAggregated(data) => { // Only publish if the src and destination chains match if self.provider.chain_id() == data.e3_id.chain_id() { ctx.notify(data); } } - EnclaveEvent::Shutdown { data, .. } => ctx.notify(data), + EnclaveEventData::Shutdown(data) => ctx.notify(data), _ => (), } } diff --git a/crates/evm/src/event_reader.rs b/crates/evm/src/event_reader.rs index b206b1568e..3f9aa2925b 100644 --- a/crates/evm/src/event_reader.rs +++ b/crates/evm/src/event_reader.rs @@ -14,7 +14,9 @@ use alloy::providers::Provider; use alloy::rpc::types::Filter; use anyhow::{anyhow, Result}; use e3_data::{AutoPersist, Persistable, Repository}; -use e3_events::{BusError, EnclaveErrorType, EnclaveEvent, EventBus, EventId, Subscribe}; +use e3_events::{ + BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, EventId, Subscribe, +}; use futures_util::stream::StreamExt; use std::collections::HashSet; use tokio::select; @@ -291,7 +293,7 @@ impl Handler for EvmEventReader

type Result = (); fn handle(&mut self, msg: EnclaveEvent, _: &mut Self::Context) -> Self::Result { - if let EnclaveEvent::Shutdown { .. } = msg { + if let EnclaveEventData::Shutdown(_) = msg.into_data() { if let Some(shutdown) = self.shutdown_tx.take() { let _ = shutdown.send(()); } diff --git a/crates/fhe/src/ext.rs b/crates/fhe/src/ext.rs index d51a4ac9a0..c00ba4d2a1 100644 --- a/crates/fhe/src/ext.rs +++ b/crates/fhe/src/ext.rs @@ -9,7 +9,9 @@ use actix::Addr; use anyhow::{anyhow, Result}; use async_trait::async_trait; use e3_data::{FromSnapshotWithParams, RepositoriesFactory, Snapshot}; -use e3_events::{BusError, E3Requested, EnclaveErrorType, EnclaveEvent, EventBus}; +use e3_events::{ + BusError, E3Requested, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, +}; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, TypedKey}; use e3_utils::SharedRng; use std::sync::Arc; @@ -37,7 +39,7 @@ const ERROR_FHE_FAILED_TO_DECODE: &str = "Failed to decode encoded FHE params"; impl E3Extension for FheExtension { fn on_event(&self, ctx: &mut E3Context, evt: &EnclaveEvent) { // Saving the fhe on Committee Requested - let EnclaveEvent::E3Requested { data, .. } = evt else { + let EnclaveEventData::E3Requested(data) = evt.get_data() else { return; }; diff --git a/crates/keyshare/src/ext.rs b/crates/keyshare/src/ext.rs index 134c664e23..3ef1a2ffd0 100644 --- a/crates/keyshare/src/ext.rs +++ b/crates/keyshare/src/ext.rs @@ -13,7 +13,7 @@ use anyhow::{anyhow, Result}; use async_trait::async_trait; use e3_crypto::Cipher; use e3_data::{AutoPersist, RepositoriesFactory}; -use e3_events::{BusError, EnclaveErrorType, EnclaveEvent, EventBus}; +use e3_events::{BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus}; use e3_fhe::ext::FHE_KEY; use e3_multithread::Multithread; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, META_KEY}; @@ -46,7 +46,7 @@ const ERROR_KEYSHARE_FHE_MISSING: &str = impl E3Extension for KeyshareExtension { fn on_event(&self, ctx: &mut E3Context, evt: &EnclaveEvent) { // if this is NOT a CiphernodeSelected event then ignore - let EnclaveEvent::CiphernodeSelected { data, .. } = evt else { + let EnclaveEventData::CiphernodeSelected(data) = evt.get_data() else { return; }; @@ -147,7 +147,7 @@ impl ThresholdKeyshareExtension { impl E3Extension for ThresholdKeyshareExtension { fn on_event(&self, ctx: &mut E3Context, evt: &EnclaveEvent) { // if this is NOT a CiphernodeSelected event then ignore - let EnclaveEvent::CiphernodeSelected { data, .. } = evt else { + let EnclaveEventData::CiphernodeSelected(data) = evt.get_data() else { return; }; diff --git a/crates/keyshare/src/keyshare.rs b/crates/keyshare/src/keyshare.rs index 57e3840617..c1711159e4 100644 --- a/crates/keyshare/src/keyshare.rs +++ b/crates/keyshare/src/keyshare.rs @@ -10,7 +10,8 @@ use e3_crypto::Cipher; use e3_data::Persistable; use e3_events::{ BusError, CiphernodeSelected, CiphertextOutputPublished, DecryptionshareCreated, Die, - E3RequestComplete, EnclaveErrorType, EnclaveEvent, EventBus, FromError, KeyshareCreated, + E3RequestComplete, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, FromError, + KeyshareCreated, }; use e3_fhe::{DecryptCiphertext, Fhe}; use e3_utils::utility_types::ArcBytes; @@ -76,11 +77,11 @@ impl Handler for Keyshare { type Result = (); fn handle(&mut self, event: EnclaveEvent, ctx: &mut actix::Context) -> Self::Result { - match event { - EnclaveEvent::CiphernodeSelected { data, .. } => ctx.notify(data), - EnclaveEvent::CiphertextOutputPublished { data, .. } => ctx.notify(data), - EnclaveEvent::E3RequestComplete { data, .. } => ctx.notify(data), - EnclaveEvent::Shutdown { .. } => ctx.notify(Die), + match event.into_data() { + EnclaveEventData::CiphernodeSelected(data) => ctx.notify(data), + EnclaveEventData::CiphertextOutputPublished(data) => ctx.notify(data), + EnclaveEventData::E3RequestComplete(data) => ctx.notify(data), + EnclaveEventData::Shutdown(_) => ctx.notify(Die), _ => (), } } diff --git a/crates/keyshare/src/threshold_keyshare.rs b/crates/keyshare/src/threshold_keyshare.rs index 8d8a5b5918..c521c2bdeb 100644 --- a/crates/keyshare/src/threshold_keyshare.rs +++ b/crates/keyshare/src/threshold_keyshare.rs @@ -10,8 +10,8 @@ use e3_crypto::{Cipher, SensitiveBytes}; use e3_data::Persistable; use e3_events::{ CiphernodeSelected, CiphertextOutputPublished, ComputeRequest, ComputeResponse, - DecryptionshareCreated, E3id, EnclaveEvent, EventBus, KeyshareCreated, PartyId, ThresholdShare, - ThresholdShareCreated, + DecryptionshareCreated, E3id, EnclaveEvent, EnclaveEventData, EventBus, KeyshareCreated, + PartyId, ThresholdShare, ThresholdShareCreated, }; use e3_fhe::create_crp; use e3_multithread::Multithread; @@ -754,10 +754,10 @@ impl ThresholdKeyshare { impl Handler for ThresholdKeyshare { type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { - match msg { - EnclaveEvent::CiphernodeSelected { data, .. } => ctx.notify(data), - EnclaveEvent::CiphertextOutputPublished { data, .. } => ctx.notify(data), - EnclaveEvent::ThresholdShareCreated { data, .. } => { + match msg.into_data() { + EnclaveEventData::CiphernodeSelected(data) => ctx.notify(data), + EnclaveEventData::CiphertextOutputPublished(data) => ctx.notify(data), + EnclaveEventData::ThresholdShareCreated(data) => { let _ = self.handle_threshold_share_created(data, ctx.address()); } _ => (), diff --git a/crates/logger/src/logger.rs b/crates/logger/src/logger.rs index 1cc2270337..acfa7b4a82 100644 --- a/crates/logger/src/logger.rs +++ b/crates/logger/src/logger.rs @@ -5,7 +5,7 @@ // or FITNESS FOR A PARTICULAR PURPOSE. use actix::{Actor, Addr, Context, Handler}; -use e3_events::{EnclaveEvent, Event, EventBus, Subscribe}; +use e3_events::{EnclaveEvent, EnclaveEventData, Event, EventBus, Subscribe}; use std::marker::PhantomData; use tracing::{error, info}; @@ -48,8 +48,8 @@ impl Handler for SimpleLogger { impl EventLogging for EnclaveEvent { fn log(&self, logger_name: &str) { - match self { - EnclaveEvent::EnclaveError { .. } => error!(event=%self, "ERROR!"), + match self.get_data() { + EnclaveEventData::EnclaveError(_) => error!(event=%self, "ERROR!"), _ => match self.get_e3_id() { Some(e3_id) => { println!("{logger_name}: {e3_id} Event Broadcasted"); diff --git a/crates/net/src/document_publisher.rs b/crates/net/src/document_publisher.rs index 9f03994b72..09d89d5739 100644 --- a/crates/net/src/document_publisher.rs +++ b/crates/net/src/document_publisher.rs @@ -15,7 +15,7 @@ use anyhow::Result; use chrono::{DateTime, Utc}; use e3_events::{ BusError, CiphernodeSelected, CorrelationId, DocumentKind, DocumentMeta, DocumentReceived, - E3RequestComplete, E3id, EnclaveErrorType, EnclaveEvent, EventBus, PartyId, + E3RequestComplete, E3id, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, PartyId, PublishDocumentRequested, Subscribe, ThresholdShareCreated, }; use e3_utils::retry::{retry_with_backoff, to_retry}; @@ -72,9 +72,9 @@ impl DocumentPublisher { /// This is needed to create simulation libp2p event routers pub fn is_document_publisher_event(event: &EnclaveEvent) -> bool { // Add a list of events with paylods for the DHT - match event { - EnclaveEvent::PublishDocumentRequested { .. } => true, - EnclaveEvent::ThresholdShareCreated { .. } => true, + match event.get_data() { + EnclaveEventData::PublishDocumentRequested(_) => true, + EnclaveEventData::ThresholdShareCreated(_) => true, _ => false, } } @@ -133,10 +133,10 @@ impl Actor for DocumentPublisher { impl Handler for DocumentPublisher { type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { - match msg { - EnclaveEvent::PublishDocumentRequested { data, .. } => ctx.notify(data), - EnclaveEvent::CiphernodeSelected { data, .. } => ctx.notify(data), - EnclaveEvent::E3RequestComplete { data, .. } => ctx.notify(data), + match msg.into_data() { + EnclaveEventData::PublishDocumentRequested(data) => ctx.notify(data), + EnclaveEventData::CiphernodeSelected(data) => ctx.notify(data), + EnclaveEventData::E3RequestComplete(data) => ctx.notify(data), _ => (), } } @@ -452,9 +452,9 @@ impl Actor for EventConverter { impl Handler for EventConverter { type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { - match msg { - EnclaveEvent::ThresholdShareCreated { data, .. } => ctx.notify(data), - EnclaveEvent::DocumentReceived { data, .. } => ctx.notify(data), + match msg.into_data() { + EnclaveEventData::ThresholdShareCreated(data) => ctx.notify(data), + EnclaveEventData::DocumentReceived(data) => ctx.notify(data), _ => (), } } @@ -788,10 +788,8 @@ mod tests { // Check event was dispatched let events = history.send(GetEvents::new()).await?; - let Some(EnclaveEvent::DocumentReceived { - data: DocumentReceived { value: doc, .. }, - .. - }) = events.last() + let Some(EnclaveEventData::DocumentReceived(DocumentReceived { value: doc, .. })) = + events.last().map(|e| e.get_data()) else { bail!("No event sent"); }; diff --git a/crates/net/src/net_event_translator.rs b/crates/net/src/net_event_translator.rs index 18aa0cbb4a..53a8e12d07 100644 --- a/crates/net/src/net_event_translator.rs +++ b/crates/net/src/net_event_translator.rs @@ -15,6 +15,7 @@ use actix::prelude::*; use anyhow::{bail, Result}; use e3_crypto::Cipher; use e3_data::Repository; +use e3_events::EnclaveEventData; use e3_events::{CorrelationId, EnclaveEvent, EventBus, EventId, Subscribe}; use libp2p::identity::ed25519; use std::collections::HashSet; @@ -99,11 +100,11 @@ impl NetEventTranslator { /// as static means we can keep this maintained here but use this rule elsewhere pub fn is_forwardable_event(event: &EnclaveEvent) -> bool { // Add a list of events allowed to be forwarded to libp2p - match event { - EnclaveEvent::DecryptionshareCreated { .. } => true, - EnclaveEvent::KeyshareCreated { .. } => true, - EnclaveEvent::PlaintextAggregated { .. } => true, - EnclaveEvent::PublicKeyAggregated { .. } => true, + match event.get_data() { + EnclaveEventData::DecryptionshareCreated(_) => true, + EnclaveEventData::KeyshareCreated(_) => true, + EnclaveEventData::PlaintextAggregated(_) => true, + EnclaveEventData::PublicKeyAggregated(_) => true, _ => false, } } diff --git a/crates/request/src/meta.rs b/crates/request/src/meta.rs index 209ad63da2..b5af65cf3c 100644 --- a/crates/request/src/meta.rs +++ b/crates/request/src/meta.rs @@ -8,7 +8,7 @@ use crate::{E3Context, E3ContextSnapshot, E3Extension, MetaRepositoryFactory, Ty use anyhow::*; use async_trait::async_trait; use e3_data::RepositoriesFactory; -use e3_events::{E3Requested, EnclaveEvent, Seed}; +use e3_events::{E3Requested, EnclaveEvent, EnclaveEventData, Seed}; use e3_utils::utility_types::ArcBytes; pub const META_KEY: TypedKey = TypedKey::new("meta"); @@ -34,7 +34,7 @@ impl E3MetaExtension { #[async_trait] impl E3Extension for E3MetaExtension { fn on_event(&self, ctx: &mut crate::E3Context, event: &EnclaveEvent) { - let EnclaveEvent::E3Requested { data, .. } = event else { + let EnclaveEventData::E3Requested(data) = event.get_data() else { return; }; let E3Requested { diff --git a/crates/request/src/router.rs b/crates/request/src/router.rs index 33246db5fe..6291ecc90c 100644 --- a/crates/request/src/router.rs +++ b/crates/request/src/router.rs @@ -21,6 +21,7 @@ use e3_data::RepositoriesFactory; use e3_data::Repository; use e3_data::Snapshot; use e3_events::E3RequestComplete; +use e3_events::EnclaveEventData; use e3_events::Shutdown; use e3_events::{E3id, EnclaveEvent, EventBus, Subscribe}; use serde::Deserialize; @@ -147,9 +148,9 @@ impl Actor for E3Router { impl Handler for E3Router { type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { - // If we are shuttomg down then bail on anything else - if let EnclaveEvent::Shutdown { data, .. } = msg { - ctx.notify(data); + // If we are shutting down then bail on anything else + if let EnclaveEventData::Shutdown(data) = msg.get_data() { + ctx.notify(data.clone()); return; } @@ -179,8 +180,8 @@ impl Handler for E3Router { context.forward_message(&msg, &mut self.buffer); - match &msg { - EnclaveEvent::PlaintextAggregated { .. } => { + match msg.into_data() { + EnclaveEventData::PlaintextAggregated(_) => { // Here we are detemining that by receiving the PlaintextAggregated event our request is // complete and we can notify everyone. This might change as we consider other factors // when determining if the request is complete @@ -191,7 +192,7 @@ impl Handler for E3Router { // Send to bus so all other actors can react to a request being complete. self.bus.do_send(event); } - EnclaveEvent::E3RequestComplete { .. } => { + EnclaveEventData::E3RequestComplete(_) => { // Note this will be sent above to the children who can kill themselves based on // the event self.contexts.remove(&e3_id); diff --git a/crates/sortition/src/ciphernode_selector.rs b/crates/sortition/src/ciphernode_selector.rs index d89b09cba2..484c790988 100644 --- a/crates/sortition/src/ciphernode_selector.rs +++ b/crates/sortition/src/ciphernode_selector.rs @@ -11,8 +11,8 @@ use actix::prelude::*; use e3_config::StoreKeys; use e3_data::{DataStore, RepositoriesFactory}; use e3_events::{ - CiphernodeSelected, CommitteeFinalized, E3Requested, EnclaveEvent, EventBus, Shutdown, - Subscribe, TicketGenerated, TicketId, + CiphernodeSelected, CommitteeFinalized, E3Requested, EnclaveEvent, EnclaveEventData, EventBus, + Shutdown, Subscribe, TicketGenerated, TicketId, }; use e3_request::MetaRepositoryFactory; use tracing::info; @@ -65,10 +65,10 @@ impl CiphernodeSelector { impl Handler for CiphernodeSelector { type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { - match msg { - EnclaveEvent::E3Requested { data, .. } => ctx.notify(data), - EnclaveEvent::CommitteeFinalized { data, .. } => ctx.notify(data), - EnclaveEvent::Shutdown { data, .. } => ctx.notify(data), + match msg.into_data() { + EnclaveEventData::E3Requested(data) => ctx.notify(data), + EnclaveEventData::CommitteeFinalized(data) => ctx.notify(data), + EnclaveEventData::Shutdown(data) => ctx.notify(data), _ => (), } } diff --git a/crates/sortition/src/sortition.rs b/crates/sortition/src/sortition.rs index ac9bad7d9e..f8b323b622 100644 --- a/crates/sortition/src/sortition.rs +++ b/crates/sortition/src/sortition.rs @@ -9,6 +9,7 @@ use actix::prelude::*; use alloy::primitives::U256; use anyhow::Result; use e3_data::{AutoPersist, Persistable, Repository}; +use e3_events::EnclaveEventData; use e3_events::{ BusError, CiphernodeAdded, CiphernodeRemoved, CommitteeFinalized, CommitteePublished, ConfigurationUpdated, EnclaveErrorType, EnclaveEvent, EventBus, OperatorActivationChanged, @@ -231,15 +232,15 @@ impl Handler for Sortition { type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { - match msg { - EnclaveEvent::CiphernodeAdded { data, .. } => ctx.notify(data.clone()), - EnclaveEvent::CiphernodeRemoved { data, .. } => ctx.notify(data.clone()), - EnclaveEvent::TicketBalanceUpdated { data, .. } => ctx.notify(data.clone()), - EnclaveEvent::OperatorActivationChanged { data, .. } => ctx.notify(data.clone()), - EnclaveEvent::ConfigurationUpdated { data, .. } => ctx.notify(data.clone()), - EnclaveEvent::CommitteePublished { data, .. } => ctx.notify(data.clone()), - EnclaveEvent::PlaintextOutputPublished { data, .. } => ctx.notify(data.clone()), - EnclaveEvent::CommitteeFinalized { data, .. } => ctx.notify(data.clone()), + match msg.into_data() { + EnclaveEventData::CiphernodeAdded(data) => ctx.notify(data.clone()), + EnclaveEventData::CiphernodeRemoved(data) => ctx.notify(data.clone()), + EnclaveEventData::TicketBalanceUpdated(data) => ctx.notify(data.clone()), + EnclaveEventData::OperatorActivationChanged(data) => ctx.notify(data.clone()), + EnclaveEventData::ConfigurationUpdated(data) => ctx.notify(data.clone()), + EnclaveEventData::CommitteePublished(data) => ctx.notify(data.clone()), + EnclaveEventData::PlaintextOutputPublished(data) => ctx.notify(data.clone()), + EnclaveEventData::CommitteeFinalized(data) => ctx.notify(data.clone()), _ => (), } } diff --git a/crates/test-helpers/src/plaintext_writer.rs b/crates/test-helpers/src/plaintext_writer.rs index b2a5911bbb..77e1a73168 100644 --- a/crates/test-helpers/src/plaintext_writer.rs +++ b/crates/test-helpers/src/plaintext_writer.rs @@ -8,7 +8,7 @@ use std::path::PathBuf; use super::write_file_with_dirs; use actix::{Actor, Addr, Context, Handler}; -use e3_events::{EnclaveEvent, EventBus, Subscribe}; +use e3_events::{EnclaveEvent, EnclaveEventData, EventBus, Subscribe}; use e3_sdk::bfv_helpers::decode_bytes_to_vec_u64; use tracing::{error, info}; @@ -37,7 +37,7 @@ impl Actor for PlaintextWriter { impl Handler for PlaintextWriter { type Result = (); fn handle(&mut self, msg: EnclaveEvent, _: &mut Self::Context) -> Self::Result { - if let EnclaveEvent::PlaintextAggregated { data, .. } = msg.clone() { + if let EnclaveEventData::PlaintextAggregated(data) = msg.into_data() { let Some(decrypted) = data.decrypted_output.first() else { error!("Decrypted output must not be empty!"); return; diff --git a/crates/test-helpers/src/public_key_writer.rs b/crates/test-helpers/src/public_key_writer.rs index db64b1b00b..24aa0ec0bb 100644 --- a/crates/test-helpers/src/public_key_writer.rs +++ b/crates/test-helpers/src/public_key_writer.rs @@ -8,7 +8,7 @@ use std::path::PathBuf; use super::write_file_with_dirs; use actix::{Actor, Addr, Context, Handler}; -use e3_events::{EnclaveEvent, EventBus, Subscribe}; +use e3_events::{EnclaveEvent, EnclaveEventData, EventBus, Subscribe}; use tracing::info; pub struct PublicKeyWriter { @@ -36,7 +36,7 @@ impl Actor for PublicKeyWriter { impl Handler for PublicKeyWriter { type Result = (); fn handle(&mut self, msg: EnclaveEvent, _: &mut Self::Context) -> Self::Result { - if let EnclaveEvent::PublicKeyAggregated { data, .. } = msg.clone() { + if let EnclaveEventData::PublicKeyAggregated(data) = msg.into_data() { info!(path = ?&self.path, "Writing Pubkey To Path"); write_file_with_dirs(&self.path, &data.pubkey).unwrap(); } From 5778792a5cfadb7e6457da2ac574bba1b3c241d1 Mon Sep 17 00:00:00 2001 From: ryardley Date: Fri, 28 Nov 2025 22:21:36 +0000 Subject: [PATCH 04/36] ensure compilation and fix errors --- crates/config/src/load_config.rs | 2 +- crates/evm-helpers/tests/helpers.rs | 2 -- crates/evm-helpers/tests/integration.rs | 1 - crates/evm/tests/integration.rs | 15 +++++++------- crates/fhe/src/fhe.rs | 4 ++-- crates/indexer/src/callback_queue.rs | 8 ++++---- crates/indexer/tests/helpers.rs | 13 ------------ crates/net/src/bin/p2p_test.rs | 1 - crates/net/src/events.rs | 1 - crates/program-server/src/types.rs | 2 +- crates/tests/tests/integration.rs | 18 ++++++----------- crates/tests/tests/integration_legacy.rs | 20 +++++++++---------- .../src/calculate_threshold_decryption.rs | 3 +-- 13 files changed, 33 insertions(+), 57 deletions(-) diff --git a/crates/config/src/load_config.rs b/crates/config/src/load_config.rs index 27466e0b84..9cffd09065 100644 --- a/crates/config/src/load_config.rs +++ b/crates/config/src/load_config.rs @@ -61,7 +61,7 @@ pub fn resolve_config_path>( mod tests { use super::resolve_config_path; use anyhow::Result; - use std::path::{Path, PathBuf}; + use std::path::PathBuf; #[test] fn test_resolve_cli() -> Result<()> { diff --git a/crates/evm-helpers/tests/helpers.rs b/crates/evm-helpers/tests/helpers.rs index 4fd3d3f9a9..1757f8b9dc 100644 --- a/crates/evm-helpers/tests/helpers.rs +++ b/crates/evm-helpers/tests/helpers.rs @@ -6,7 +6,6 @@ // helpers.rs use alloy::{ - network::Ethereum, node_bindings::{Anvil, AnvilInstance}, providers::{Provider, ProviderBuilder, WsConnect}, signers::local::PrivateKeySigner, @@ -14,7 +13,6 @@ use alloy::{ }; use eyre::Result; use EmitLogs::EmitLogsInstance; -use Enclave::EnclaveInstance; sol!( #[sol(rpc)] diff --git a/crates/evm-helpers/tests/integration.rs b/crates/evm-helpers/tests/integration.rs index 48a15de85a..746490ea56 100644 --- a/crates/evm-helpers/tests/integration.rs +++ b/crates/evm-helpers/tests/integration.rs @@ -8,7 +8,6 @@ mod helpers; use alloy::consensus::BlockHeader; use alloy::providers::ext::AnvilApi; use alloy::{node_bindings::Anvil, providers::ProviderBuilder, sol}; -use e3_evm_helpers::block_listener; use e3_evm_helpers::{block_listener::BlockListener, event_listener::EventListener}; use eyre::Result; use helpers::setup_logs_contract; diff --git a/crates/evm/tests/integration.rs b/crates/evm/tests/integration.rs index 799a254eb3..071792fe56 100644 --- a/crates/evm/tests/integration.rs +++ b/crates/evm/tests/integration.rs @@ -17,7 +17,8 @@ use anyhow::Result; use e3_data::Repository; use e3_entrypoint::helpers::datastore::get_in_mem_store; use e3_events::{ - new_event_bus_with_history, EnclaveEvent, GetEvents, HistoryCollector, Shutdown, TestEvent, + new_event_bus_with_history, EnclaveEvent, EnclaveEventData, GetEvents, HistoryCollector, + Shutdown, TestEvent, }; use e3_evm::{helpers::EthProvider, CoordinatorStart, EvmEventReader, HistoricalEventCoordinator}; use std::time::Duration; @@ -54,8 +55,8 @@ async fn get_msgs(history_collector: &Addr>) -> R .await?; let msgs: Vec = history .into_iter() - .filter_map(|evt| match evt { - EnclaveEvent::TestEvent { data, .. } => Some(data.msg), + .filter_map(|evt| match evt.into_data() { + EnclaveEventData::TestEvent(data) => Some(data.msg), _ => None, }) .collect(); @@ -121,8 +122,8 @@ async fn evm_reader() -> Result<()> { let msgs: Vec<_> = history .into_iter() - .filter_map(|evt| match evt { - EnclaveEvent::TestEvent { data, .. } => Some(data.msg), + .filter_map(|evt| match evt.into_data() { + EnclaveEventData::TestEvent(data) => Some(data.msg), _ => None, }) .collect(); @@ -200,8 +201,8 @@ async fn ensure_historical_events() -> Result<()> { let msgs: Vec<_> = history .into_iter() - .filter_map(|evt| match evt { - EnclaveEvent::TestEvent { data, .. } => Some(data.msg), + .filter_map(|evt| match evt.into_data() { + EnclaveEventData::TestEvent(data) => Some(data.msg), _ => None, }) .collect(); diff --git a/crates/fhe/src/fhe.rs b/crates/fhe/src/fhe.rs index 8e6171d8a1..cd2b3507c2 100644 --- a/crates/fhe/src/fhe.rs +++ b/crates/fhe/src/fhe.rs @@ -15,10 +15,10 @@ use e3_data::{FromSnapshotWithParams, Snapshot}; use e3_events::{OrderedSet, Seed}; use e3_utils::{ArcBytes, SharedRng}; use fhe::{ - bfv::{BfvParameters, Ciphertext, Encoding, Plaintext, PublicKey, SecretKey}, + bfv::{BfvParameters, Ciphertext, Plaintext, PublicKey, SecretKey}, mbfv::{AggregateIter, CommonRandomPoly, DecryptionShare, PublicKeyShare}, }; -use fhe_traits::{Deserialize, DeserializeParametrized, FheDecoder, Serialize}; +use fhe_traits::{Deserialize, DeserializeParametrized, Serialize}; use rand::SeedableRng; use rand_chacha::ChaCha20Rng; use std::sync::{Arc, Mutex}; diff --git a/crates/indexer/src/callback_queue.rs b/crates/indexer/src/callback_queue.rs index 4a5694a152..44ab4a123f 100644 --- a/crates/indexer/src/callback_queue.rs +++ b/crates/indexer/src/callback_queue.rs @@ -99,7 +99,7 @@ mod tests { #[tokio::test] async fn test_single_callback_executes() { - let mut queue = CallbackQueue::new(); + let queue = CallbackQueue::new(); let called = Arc::new(Mutex::new(false)); let called_clone = called.clone(); @@ -117,7 +117,7 @@ mod tests { #[tokio::test] async fn test_callback_not_executed_before_threshold() { - let mut queue = CallbackQueue::new(); + let queue = CallbackQueue::new(); let called = Arc::new(Mutex::new(false)); let called_clone = called.clone(); @@ -135,7 +135,7 @@ mod tests { #[tokio::test] async fn test_multiple_callbacks_execute() { - let mut queue = CallbackQueue::new(); + let queue = CallbackQueue::new(); let counter = Arc::new(Mutex::new(0)); let c1 = counter.clone(); @@ -171,7 +171,7 @@ mod tests { #[tokio::test] async fn test_error_propagation() { - let mut queue = CallbackQueue::new(); + let queue = CallbackQueue::new(); queue.push(100, || async { Err(eyre::eyre!("test error")) }); diff --git a/crates/indexer/tests/helpers.rs b/crates/indexer/tests/helpers.rs index 43dfdb1d7b..bc9f8a0b2e 100644 --- a/crates/indexer/tests/helpers.rs +++ b/crates/indexer/tests/helpers.rs @@ -8,7 +8,6 @@ use std::sync::Arc; // helpers.rs use alloy::{ - network::Ethereum, node_bindings::{Anvil, AnvilInstance}, providers::{Provider, ProviderBuilder, WsConnect}, signers::local::PrivateKeySigner, @@ -30,18 +29,6 @@ sol!( "tests/fixtures/emit_logs.json" ); -pub async fn setup_fake_enclave() -> Result<( - EnclaveInstance, - String, - String, - AnvilInstance, -)> { - let (provider, endpoint, anvil) = setup_provider().await?; - let contract = Enclave::deploy(provider).await?; - let address = contract.address().to_string(); - Ok((contract, address, endpoint, anvil)) -} - pub async fn setup_two_contracts() -> Result<( EnclaveInstance, String, diff --git a/crates/net/src/bin/p2p_test.rs b/crates/net/src/bin/p2p_test.rs index 874ccf25b2..e5865f97b8 100644 --- a/crates/net/src/bin/p2p_test.rs +++ b/crates/net/src/bin/p2p_test.rs @@ -14,7 +14,6 @@ use std::sync::atomic::{AtomicU8, Ordering}; use std::time::Duration; use std::{env, process}; use tokio::sync::{broadcast, mpsc}; -use tokio::task::JoinHandle; use tokio::time::{sleep, timeout}; use tracing::info; use tracing_subscriber::{prelude::*, EnvFilter}; diff --git a/crates/net/src/events.rs b/crates/net/src/events.rs index 0dedd10769..010e7130be 100644 --- a/crates/net/src/events.rs +++ b/crates/net/src/events.rs @@ -21,7 +21,6 @@ use std::{ time::{Duration, Instant}, }; use tokio::sync::{broadcast, mpsc}; -use tracing::{info, warn}; /// Incoming/Outgoing GossipData. We disambiguate on concerns relative to the net package. #[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)] diff --git a/crates/program-server/src/types.rs b/crates/program-server/src/types.rs index 81996753c7..8fcdafb071 100644 --- a/crates/program-server/src/types.rs +++ b/crates/program-server/src/types.rs @@ -181,6 +181,6 @@ mod tests { "#; // Just dont crash - let payload: ComputeRequest = serde_json::from_str(json).unwrap(); + let _payload: ComputeRequest = serde_json::from_str(json).unwrap(); } } diff --git a/crates/tests/tests/integration.rs b/crates/tests/tests/integration.rs index 0e5728b6cd..f47ffeebd9 100644 --- a/crates/tests/tests/integration.rs +++ b/crates/tests/tests/integration.rs @@ -11,8 +11,8 @@ use e3_ciphernode_builder::CiphernodeBuilder; use e3_crypto::Cipher; use e3_events::{ CiphertextOutputPublished, CommitteeFinalized, ConfigurationUpdated, E3Requested, E3id, - EnclaveEvent, EventBus, EventBusConfig, OperatorActivationChanged, PlaintextAggregated, - TicketBalanceUpdated, + EnclaveEvent, EnclaveEventData, EventBus, EventBusConfig, OperatorActivationChanged, + PlaintextAggregated, TicketBalanceUpdated, }; use e3_multithread::{GetReport, Multithread}; use e3_sdk::bfv_helpers::{build_bfv_params_arc, decode_bytes_to_vec_u64, encode_bfv_params}; @@ -322,9 +322,7 @@ async fn test_trbfv_actor() -> Result<()> { // First we get the public key println!("Getting public key"); - let Some(EnclaveEvent::PublicKeyAggregated { - data: pubkey_event, .. - }) = h.last().clone() + let Some(EnclaveEventData::PublicKeyAggregated(pubkey_event)) = h.last().map(|e| e.get_data()) else { panic!("Was expecting event to be PublicKeyAggregated"); }; @@ -391,14 +389,10 @@ async fn test_trbfv_actor() -> Result<()> { publishing_ct_timer.elapsed(), )); - let Some(EnclaveEvent::PlaintextAggregated { - data: - PlaintextAggregated { - decrypted_output: plaintext, - .. - }, + let Some(EnclaveEventData::PlaintextAggregated(PlaintextAggregated { + decrypted_output: plaintext, .. - }) = h.last() + })) = h.last().map(|e| e.get_data()) else { bail!("bad event") }; diff --git a/crates/tests/tests/integration_legacy.rs b/crates/tests/tests/integration_legacy.rs index e07dcb2de2..635429c1b2 100644 --- a/crates/tests/tests/integration_legacy.rs +++ b/crates/tests/tests/integration_legacy.rs @@ -13,6 +13,7 @@ use e3_ciphernode_builder::CiphernodeHandle; use e3_crypto::Cipher; use e3_data::GetDump; use e3_data::InMemStore; +use e3_events::EnclaveEventData; use e3_events::GetEvents; use e3_events::{ CiphernodeSelected, CiphertextOutputPublished, CommitteeFinalized, ConfigurationUpdated, @@ -32,12 +33,11 @@ use e3_test_helpers::{ }; use e3_utils::utility_types::ArcBytes; use e3_utils::SharedRng; -use fhe::bfv::Encoding; use fhe::{ bfv::{BfvParameters, PublicKey, SecretKey}, mbfv::{AggregateIter, CommonRandomPoly, PublicKeyShare}, }; -use fhe_traits::{FheDecoder, Serialize}; +use fhe_traits::Serialize; use rand::SeedableRng; use rand_chacha::ChaCha20Rng; use std::{sync::Arc, time::Duration}; @@ -235,8 +235,8 @@ async fn test_public_key_aggregation_and_decryption() -> Result<()> { let aggregated_event: Vec<_> = history .into_iter() - .filter_map(|e| match e { - EnclaveEvent::PublicKeyAggregated { data, .. } => Some(data), + .filter_map(|e| match e.into_data() { + EnclaveEventData::PublicKeyAggregated(data) => Some(data), _ => None, }) .collect(); @@ -269,8 +269,8 @@ async fn test_public_key_aggregation_and_decryption() -> Result<()> { let actual = history .into_iter() - .filter_map(|e| match e { - EnclaveEvent::PlaintextAggregated { data, .. } => Some(data), + .filter_map(|e| match e.into_data() { + EnclaveEventData::PlaintextAggregated(data) => Some(data), _ => None, }) .collect::>() @@ -395,8 +395,8 @@ async fn test_stopped_keyshares_retain_state() -> Result<()> { // get the public key from history. let pubkey: PublicKey = history .iter() - .filter_map(|evt| match evt { - EnclaveEvent::KeyshareCreated { data, .. } => { + .filter_map(|evt| match evt.get_data() { + EnclaveEventData::KeyshareCreated(data) => { PublicKeyShare::deserialize(&data.pubkey, ¶ms, crpoly.clone()).ok() } _ => None, @@ -424,8 +424,8 @@ async fn test_stopped_keyshares_retain_state() -> Result<()> { let actual = history .into_iter() - .filter_map(|e| match e { - EnclaveEvent::PlaintextAggregated { data, .. } => Some(data), + .filter_map(|e| match e.into_data() { + EnclaveEventData::PlaintextAggregated(data) => Some(data), _ => None, }) .collect::>() diff --git a/crates/trbfv/src/calculate_threshold_decryption.rs b/crates/trbfv/src/calculate_threshold_decryption.rs index 31ed8355ad..8bd4831a7b 100644 --- a/crates/trbfv/src/calculate_threshold_decryption.rs +++ b/crates/trbfv/src/calculate_threshold_decryption.rs @@ -11,11 +11,10 @@ use crate::{helpers::try_poly_from_bytes, PartyId, TrBFVConfig}; use anyhow::*; use e3_bfv_helpers::{decode_plaintext_to_vec_u64, encode_vec_u64_to_bytes}; use e3_utils::utility_types::ArcBytes; -use fhe::bfv::{Encoding, Plaintext}; +use fhe::bfv::Plaintext; use fhe::{bfv::Ciphertext, trbfv::ShareManager}; use fhe_math::rq::Poly; use fhe_traits::DeserializeParametrized; -use fhe_traits::FheDecoder; use tracing::info; /// Shamir shares for a single party to decrypt a batch of ciphertexts. From aebcd8629e7cae396eb29cd79f947fd511df2e5e Mon Sep 17 00:00:00 2001 From: ryardley Date: Fri, 28 Nov 2025 22:23:30 +0000 Subject: [PATCH 05/36] fix unused var --- crates/net/src/document_publisher.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/net/src/document_publisher.rs b/crates/net/src/document_publisher.rs index 09d89d5739..8007ef2950 100644 --- a/crates/net/src/document_publisher.rs +++ b/crates/net/src/document_publisher.rs @@ -462,7 +462,7 @@ impl Handler for EventConverter { impl Handler for EventConverter { type Result = (); - fn handle(&mut self, msg: ThresholdShareCreated, ctx: &mut Self::Context) -> Self::Result { + fn handle(&mut self, msg: ThresholdShareCreated, _ctx: &mut Self::Context) -> Self::Result { match self.handle_threshold_share_created(msg) { Ok(_) => (), Err(err) => error!("{err}"), @@ -472,7 +472,7 @@ impl Handler for EventConverter { impl Handler for EventConverter { type Result = (); - fn handle(&mut self, msg: DocumentReceived, ctx: &mut Self::Context) -> Self::Result { + fn handle(&mut self, msg: DocumentReceived, _ctx: &mut Self::Context) -> Self::Result { match self.handle_document_received(msg) { Ok(_) => (), Err(err) => error!("{err}"), From 4755e3346e7c0836f26b90b390b9516420544122 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sat, 29 Nov 2025 00:47:56 +0000 Subject: [PATCH 06/36] use strum for event type --- Cargo.lock | 1 + crates/events/Cargo.toml | 1 + crates/events/src/enclave_event/mod.rs | 24 ++++---------------- crates/evm/src/event_reader.rs | 1 + crates/net/src/net_event_translator.rs | 1 + crates/test-helpers/src/ciphernode_system.rs | 7 +++--- 6 files changed, 11 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d08c19a95f..d079d18b9e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2866,6 +2866,7 @@ dependencies = [ "once_cell", "serde", "sha2", + "strum", "tokio", "tracing", ] diff --git a/crates/events/Cargo.toml b/crates/events/Cargo.toml index 8cca956b81..568eef6521 100644 --- a/crates/events/Cargo.toml +++ b/crates/events/Cargo.toml @@ -21,6 +21,7 @@ futures-util = { workspace = true } once_cell = { workspace = true } serde = { workspace = true } sha2 = { workspace = true } +strum = { workspace = true } tracing = { workspace = true } tokio = { workspace = true } e3-crypto = { workspace = true } diff --git a/crates/events/src/enclave_event/mod.rs b/crates/events/src/enclave_event/mod.rs index d50568ccc1..670b56757d 100644 --- a/crates/events/src/enclave_event/mod.rs +++ b/crates/events/src/enclave_event/mod.rs @@ -54,6 +54,7 @@ pub use plaintext_output_published::*; pub use publickey_aggregated::*; pub use publish_document::*; pub use shutdown::*; +use strum::IntoStaticStr; pub use test_event::*; pub use threshold_share_created::*; pub use ticket_balance_updated::*; @@ -85,7 +86,7 @@ macro_rules! impl_from_event { }; } -#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive(Clone, Debug, PartialEq, Eq, Hash, IntoStaticStr, Serialize, Deserialize)] pub enum EnclaveEventData { KeyshareCreated(KeyshareCreated), E3Requested(E3Requested), @@ -141,8 +142,8 @@ impl Event for EnclaveEvent { type Id = EventId; fn event_type(&self) -> String { - let s = format!("{:?}", self); - extract_enclave_event_name(&s).to_string() + let name: &'static str = (&self.payload).into(); + name.to_string() } fn event_id(&self) -> Self::Id { @@ -260,20 +261,3 @@ impl fmt::Display for EnclaveEvent { f.write_str(&format!("{:?}", self)) } } - -fn extract_enclave_event_name(s: &str) -> &str { - let bytes = s.as_bytes(); - for (i, &item) in bytes.iter().enumerate() { - if item == b' ' || item == b'(' { - return &s[..i]; - } - } - s -} - -impl EnclaveEvent { - pub fn event_type(&self) -> String { - let s = format!("{:?}", self); - extract_enclave_event_name(&s).to_string() - } -} diff --git a/crates/evm/src/event_reader.rs b/crates/evm/src/event_reader.rs index 3f9aa2925b..725158a5c0 100644 --- a/crates/evm/src/event_reader.rs +++ b/crates/evm/src/event_reader.rs @@ -14,6 +14,7 @@ use alloy::providers::Provider; use alloy::rpc::types::Filter; use anyhow::{anyhow, Result}; use e3_data::{AutoPersist, Persistable, Repository}; +use e3_events::Event; use e3_events::{ BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, EventId, Subscribe, }; diff --git a/crates/net/src/net_event_translator.rs b/crates/net/src/net_event_translator.rs index 53a8e12d07..b8822bce8d 100644 --- a/crates/net/src/net_event_translator.rs +++ b/crates/net/src/net_event_translator.rs @@ -16,6 +16,7 @@ use anyhow::{bail, Result}; use e3_crypto::Cipher; use e3_data::Repository; use e3_events::EnclaveEventData; +use e3_events::Event; use e3_events::{CorrelationId, EnclaveEvent, EventBus, EventId, Subscribe}; use libp2p::identity::ed25519; use std::collections::HashSet; diff --git a/crates/test-helpers/src/ciphernode_system.rs b/crates/test-helpers/src/ciphernode_system.rs index eef4586b2d..1d7bdb3b35 100644 --- a/crates/test-helpers/src/ciphernode_system.rs +++ b/crates/test-helpers/src/ciphernode_system.rs @@ -4,14 +4,13 @@ // without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. +use crate::simulate_libp2p_net; use anyhow::*; use e3_ciphernode_builder::CiphernodeHandle; +use e3_events::Event; use e3_events::{EnclaveEvent, GetEvents, ResetHistory, TakeEvents}; -use tokio::time::timeout; - use std::{future::Future, ops::Deref, pin::Pin, time::Duration}; - -use crate::simulate_libp2p_net; +use tokio::time::timeout; // This type allows us to store various dynamic async callbacks type SetupFn<'a> = From e0a55a8e6b75345eed358b01c57f3e220aba5326 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sat, 29 Nov 2025 04:49:43 +0000 Subject: [PATCH 07/36] fix shutdown pattern --- crates/data/src/sled_store.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/data/src/sled_store.rs b/crates/data/src/sled_store.rs index a8de536cc9..54e6e2b2d8 100644 --- a/crates/data/src/sled_store.rs +++ b/crates/data/src/sled_store.rs @@ -113,7 +113,7 @@ impl Handler for SledStore { impl Handler for SledStore { type Result = (); fn handle(&mut self, msg: EnclaveEvent, ctx: &mut Self::Context) -> Self::Result { - if let EnclaveEventData::Shutdown { .. } = msg.get_data() { + if let EnclaveEventData::Shutdown(_) = msg.get_data() { let _db = self.db.take(); // db will be dropped ctx.stop() } From 8635cafba99a58d0375eef866c3a81740a78a7bd Mon Sep 17 00:00:00 2001 From: ryardley Date: Sat, 29 Nov 2025 19:26:34 +0000 Subject: [PATCH 08/36] update core types --- crates/events/src/enclave_event/mod.rs | 58 ++++++++++++++++++++------ crates/events/src/eventbus_factory.rs | 2 +- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/crates/events/src/enclave_event/mod.rs b/crates/events/src/enclave_event/mod.rs index 670b56757d..a11feca83d 100644 --- a/crates/events/src/enclave_event/mod.rs +++ b/crates/events/src/enclave_event/mod.rs @@ -69,17 +69,13 @@ use std::{ hash::Hash, }; -/// Macro to help define From traits for EnclaveEvent -macro_rules! impl_from_event { +/// Macro to help define From traits for EnclaveEventData +macro_rules! impl_into_event_data { ($($variant:ident),*) => { $( - impl From<$variant> for EnclaveEvent { - fn from(data: $variant) -> Self { - let id = EventId::hash(&data); - EnclaveEvent { - payload: EnclaveEventData::$variant(data), - id - } + impl Into for $variant { + fn into(self) -> EnclaveEventData { + EnclaveEventData::$variant(self) } } )* @@ -162,7 +158,7 @@ impl ErrorEvent for EnclaveEvent { } fn from_error(err_type: Self::ErrorType, error: anyhow::Error) -> Self { - EnclaveEvent::from(EnclaveError::new(err_type, error.to_string().as_str())) + EventFactory::new().create_err(err_type, error) } } @@ -201,7 +197,7 @@ impl EnclaveEvent { } } -impl_from_event!( +impl_into_event_data!( KeyshareCreated, E3Requested, PublicKeyAggregated, @@ -233,8 +229,10 @@ impl_from_event!( impl FromError for EnclaveEvent { type Error = anyhow::Error; fn from_error(err_type: EnclaveErrorType, error: Self::Error) -> Self { - let error_event = EnclaveError::from_error(err_type, error); - EnclaveEvent::from(error_event) + // Errors will always be local - we can get away here with just making an instance of the + // factory using the defautl hlc until we remove all instances of this and just use factory + // methods. We should avoid creating a factory to create an event however. + EventFactory::new().create_err(err_type, error) } } @@ -261,3 +259,37 @@ impl fmt::Display for EnclaveEvent { f.write_str(&format!("{:?}", self)) } } + +#[derive(Clone)] +pub struct EventFactory { + // TODO: hlc: Arc, +} + +impl EventFactory { + pub fn new() -> Self { + Self {} + } + + pub fn create_local(&self, data: impl Into) -> EnclaveEvent { + let payload = data.into(); + let id = EventId::hash(&payload); + // hcl.tick(); NOTE: we may need to handle the error from hlc here and add try_create_local + EnclaveEvent { id, payload } + } + + pub fn create_remote( + &self, + data: impl Into, + _remote_ts: u128, + ) -> EnclaveEvent { + let payload = data.into(); + let id = EventId::hash(&payload); + // hcl.receive(remote_ts)?; + EnclaveEvent { id, payload } + } + + pub fn create_err(&self, err_type: EnclaveErrorType, error: anyhow::Error) -> EnclaveEvent { + let payload = EnclaveError::from_error(err_type, error); + self.create_local(payload) + } +} diff --git a/crates/events/src/eventbus_factory.rs b/crates/events/src/eventbus_factory.rs index 6049b8115a..2dd383ef38 100644 --- a/crates/events/src/eventbus_factory.rs +++ b/crates/events/src/eventbus_factory.rs @@ -60,7 +60,7 @@ impl EventBusFactory { event_bus } - pub fn get_error_collector(&self) -> Addr> { + pub fn get_error_collector(&self) -> Addr> { let type_id = TypeId::of::(); let mut error_collector_cache = self.error_collector_cache.lock().unwrap(); From ae886a3a7ce82558dda4df7f42e66489f9b56a2f Mon Sep 17 00:00:00 2001 From: ryardley Date: Sat, 29 Nov 2025 22:38:03 +0000 Subject: [PATCH 09/36] manager types and refactor wip --- .../src/ciphernode_builder.rs | 8 +- crates/data/src/sled_store.rs | 4 +- .../events/src/enclave_event/enclave_error.rs | 12 ++- crates/events/src/enclave_event/mod.rs | 64 +++++++-------- crates/events/src/event_manager.rs | 80 +++++++++++++++++++ crates/events/src/eventbus.rs | 28 ++++--- crates/events/src/lib.rs | 2 + 7 files changed, 142 insertions(+), 56 deletions(-) create mode 100644 crates/events/src/event_manager.rs diff --git a/crates/ciphernode-builder/src/ciphernode_builder.rs b/crates/ciphernode-builder/src/ciphernode_builder.rs index c871a68f78..267f694755 100644 --- a/crates/ciphernode-builder/src/ciphernode_builder.rs +++ b/crates/ciphernode-builder/src/ciphernode_builder.rs @@ -16,7 +16,9 @@ use e3_aggregator::ext::{ use e3_config::chain_config::ChainConfig; use e3_crypto::Cipher; use e3_data::{DataStore, InMemStore, Repositories, RepositoriesFactory}; -use e3_events::{EnclaveEvent, EventBus, EventBusConfig}; +use e3_events::{ + EnclaveEvent, EnclaveEventDispatcher, EnclaveEventFactory, EventBus, EventBusConfig, +}; use e3_evm::{ helpers::{ load_signer_from_repository, ConcreteReadProvider, ConcreteWriteProvider, EthProvider, @@ -282,6 +284,10 @@ impl CiphernodeBuilder { None => Self::create_local_bus(), }; + // Setup an event dispatcher + let dispatcher = + EnclaveEventDispatcher::new(local_bus, Arc::new(EnclaveEventFactory::new())); + // History collector for taking historical events for analysis and testing let history = if self.testmode_history { info!("Setting up history collector"); diff --git a/crates/data/src/sled_store.rs b/crates/data/src/sled_store.rs index 54e6e2b2d8..c3f6c0114a 100644 --- a/crates/data/src/sled_store.rs +++ b/crates/data/src/sled_store.rs @@ -8,8 +8,8 @@ use crate::{Get, Insert, InsertSync, Remove}; use actix::{Actor, ActorContext, Addr, Handler}; use anyhow::{Context, Result}; use e3_events::{ - get_enclave_event_bus, BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, - Subscribe, + get_enclave_event_bus, BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, Event, + EventBus, Subscribe, }; use once_cell::sync::Lazy; use sled::Db; diff --git a/crates/events/src/enclave_event/enclave_error.rs b/crates/events/src/enclave_event/enclave_error.rs index 35ab08fc94..3710585f67 100644 --- a/crates/events/src/enclave_event/enclave_error.rs +++ b/crates/events/src/enclave_event/enclave_error.rs @@ -9,8 +9,7 @@ use serde::{Deserialize, Serialize}; use std::fmt::{self, Display}; pub trait FromError { - type Error; - fn from_error(err_type: EnclaveErrorType, error: Self::Error) -> Self; + fn from_error(err_type: EnclaveErrorType, error: impl Into) -> Self; } #[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -39,20 +38,19 @@ pub enum EnclaveErrorType { } impl EnclaveError { - pub fn new(err_type: EnclaveErrorType, message: &str) -> Self { + pub fn new(err_type: EnclaveErrorType, message: impl Into) -> Self { Self { err_type, - message: message.to_string(), + message: message.into(), } } } impl FromError for EnclaveError { - type Error = anyhow::Error; - fn from_error(err_type: EnclaveErrorType, error: Self::Error) -> Self { + fn from_error(err_type: EnclaveErrorType, error: impl Into) -> Self { Self { err_type, - message: error.to_string(), + message: error.into(), } } } diff --git a/crates/events/src/enclave_event/mod.rs b/crates/events/src/enclave_event/mod.rs index a11feca83d..4b88404142 100644 --- a/crates/events/src/enclave_event/mod.rs +++ b/crates/events/src/enclave_event/mod.rs @@ -61,7 +61,7 @@ pub use ticket_balance_updated::*; pub use ticket_generated::*; pub use ticket_submitted::*; -use crate::{E3id, ErrorEvent, Event, EventId}; +use crate::{E3id, ErrorEvent, ErrorFactory, Event, EventFactory, EventId}; use actix::Message; use serde::{Deserialize, Serialize}; use std::{ @@ -136,6 +136,9 @@ impl EnclaveEvent { impl Event for EnclaveEvent { type Id = EventId; + type Data = EnclaveEventData; + // type FromError = anyhow::Error; + // type ErrType = EnclaveErrorType; fn event_type(&self) -> String { let name: &'static str = (&self.payload).into(); @@ -145,20 +148,25 @@ impl Event for EnclaveEvent { fn event_id(&self) -> Self::Id { self.get_id() } + + fn get_data(&self) -> &EnclaveEventData { + &self.payload + } + fn into_data(self) -> EnclaveEventData { + self.payload + } } impl ErrorEvent for EnclaveEvent { - type Error = EnclaveError; - type ErrorType = EnclaveErrorType; - fn as_error(&self) -> Option<&Self::Error> { - match self.payload { - EnclaveEventData::EnclaveError(ref data) => Some(data), - _ => None, - } - } + type ErrType = EnclaveErrorType; - fn from_error(err_type: Self::ErrorType, error: anyhow::Error) -> Self { - EventFactory::new().create_err(err_type, error) + fn from_error(err_type: Self::ErrType, msg: impl Into) -> Self { + let payload = EnclaveError::new(err_type, msg); + let id = EventId::hash(&payload); + EnclaveEvent { + payload: payload.into(), + id, + } } } @@ -189,12 +197,6 @@ impl EnclaveEvent { _ => None, } } - pub fn get_data(&self) -> &EnclaveEventData { - &self.payload - } - pub fn into_data(self) -> EnclaveEventData { - self.payload - } } impl_into_event_data!( @@ -226,16 +228,6 @@ impl_into_event_data!( ThresholdShareCreated ); -impl FromError for EnclaveEvent { - type Error = anyhow::Error; - fn from_error(err_type: EnclaveErrorType, error: Self::Error) -> Self { - // Errors will always be local - we can get away here with just making an instance of the - // factory using the defautl hlc until we remove all instances of this and just use factory - // methods. We should avoid creating a factory to create an event however. - EventFactory::new().create_err(err_type, error) - } -} - impl TryFrom<&EnclaveEvent> for EnclaveError { type Error = anyhow::Error; fn try_from(value: &EnclaveEvent) -> Result { @@ -261,34 +253,34 @@ impl fmt::Display for EnclaveEvent { } #[derive(Clone)] -pub struct EventFactory { +pub struct EnclaveEventFactory { // TODO: hlc: Arc, } -impl EventFactory { +impl EnclaveEventFactory { pub fn new() -> Self { Self {} } +} - pub fn create_local(&self, data: impl Into) -> EnclaveEvent { +impl EventFactory for EnclaveEventFactory { + fn create_local(&self, data: impl Into) -> EnclaveEvent { let payload = data.into(); let id = EventId::hash(&payload); // hcl.tick(); NOTE: we may need to handle the error from hlc here and add try_create_local EnclaveEvent { id, payload } } - pub fn create_remote( - &self, - data: impl Into, - _remote_ts: u128, - ) -> EnclaveEvent { + fn create_receive(&self, data: impl Into, _remote_ts: u128) -> EnclaveEvent { let payload = data.into(); let id = EventId::hash(&payload); // hcl.receive(remote_ts)?; EnclaveEvent { id, payload } } +} - pub fn create_err(&self, err_type: EnclaveErrorType, error: anyhow::Error) -> EnclaveEvent { +impl ErrorFactory for EnclaveEventFactory { + fn create_err(&self, err_type: EnclaveErrorType, error: impl Into) -> EnclaveEvent { let payload = EnclaveError::from_error(err_type, error); self.create_local(payload) } diff --git a/crates/events/src/event_manager.rs b/crates/events/src/event_manager.rs new file mode 100644 index 0000000000..5957eb7bbe --- /dev/null +++ b/crates/events/src/event_manager.rs @@ -0,0 +1,80 @@ +use std::sync::Arc; + +use actix::{Addr, Recipient}; + +use crate::{ErrorEvent, Event, EventBus, Subscribe}; + +pub trait EventFactory { + fn create_local(&self, data: impl Into) -> E; + fn create_receive(&self, data: impl Into, ts: u128) -> E; +} + +pub trait ErrorFactory { + fn create_err(&self, err_type: E::ErrType, error: impl Into) -> E; +} + +pub trait EventDispatcher { + fn dispatch(&self, data: impl Into); + fn dispatch_from_remote(&self, data: impl Into, ts: u128); +} + +pub trait EventSubscriber { + fn subscribe(&self, event_type: &str, recipient: Recipient); +} + +pub trait FullFactory: ErrorFactory + EventFactory {} + +pub struct EventManager { + bus: Addr>, + factory: Arc, +} + +impl EventManager { + pub fn new(bus: Addr>, factory: Arc) -> Self { + Self { bus, factory } + } +} + +impl EventDispatcher for EventManager +where + E: Event, + F: EventFactory, +{ + fn dispatch(&self, data: impl Into) { + let evt = self.create_local(data); + self.bus.do_send(evt); + } + + fn dispatch_from_remote(&self, data: impl Into, ts: u128) { + let evt = self.create_receive(data, ts); + self.bus.do_send(evt) + } +} + +impl EventFactory for EventManager +where + E: Event, + F: EventFactory, +{ + fn create_local(&self, data: impl Into) -> E { + self.factory.create_local(data) + } + + fn create_receive(&self, data: impl Into, ts: u128) -> E { + self.factory.create_receive(data, ts) + } +} + +impl> ErrorFactory for EventManager { + fn create_err(&self, err_type: E::ErrType, error: impl Into) -> E { + self.factory.create_err(err_type, error) + } +} + +impl EventSubscriber for EventManager { + fn subscribe(&self, event_type: &str, recipient: Recipient) { + self.bus.do_send(Subscribe::new(event_type, recipient)) + } +} + +impl> FullFactory for EventManager {} diff --git a/crates/events/src/eventbus.rs b/crates/events/src/eventbus.rs index 09fd3ee027..d33ad0b544 100644 --- a/crates/events/src/eventbus.rs +++ b/crates/events/src/eventbus.rs @@ -19,17 +19,22 @@ use tracing::info; /// Trait that must be implemented by events used with EventBus pub trait Event: Message + Clone + Display + Send + Sync + Unpin + 'static { type Id: Hash + Eq + Clone + Unpin + Send + Sync + Display; + + /// Payload for the Event + type Data; + fn event_type(&self) -> String; fn event_id(&self) -> Self::Id; + fn get_data(&self) -> &Self::Data; + fn into_data(self) -> Self::Data; } /// Trait for events that contain an error pub trait ErrorEvent: Event { - type Error: Clone; - type ErrorType; + /// Error type associated with this event + type ErrType; - fn as_error(&self) -> Option<&Self::Error>; - fn from_error(err_type: Self::ErrorType, error: anyhow::Error) -> Self; + fn from_error(err_type: Self::ErrType, error: impl Into) -> Self; } ////////////////////////////////////////////////////////////////////////////// @@ -89,12 +94,15 @@ impl EventBus { source.do_send(Subscribe::new("*", addr.clone().recipient())); addr } + pub fn error(source: &Addr>) -> Addr> { let addr = HistoryCollector::::new().start(); source.do_send(Subscribe::new("EnclaveError", addr.clone().recipient())); addr } + // pub fn manager(source: Addr>) -> M {} + pub fn pipe(source: &Addr>, dest: &Addr>) { source.do_send(Subscribe::new("*", dest.clone().recipient())) } @@ -295,7 +303,7 @@ impl Handler for HistoryCollector { } #[derive(Message)] -#[rtype(result = "Vec")] +#[rtype(result = "Vec")] pub struct GetErrors(PhantomData); impl GetErrors { @@ -310,18 +318,18 @@ impl GetErrors { /// Trait to send errors directly to the bus pub trait BusError { - fn err(&self, err_type: E::ErrorType, err: anyhow::Error); + fn err(&self, err_type: E::ErrType, err: anyhow::Error); } impl BusError for Addr> { - fn err(&self, err_type: E::ErrorType, err: anyhow::Error) { - self.do_send(E::from_error(err_type, err)) + fn err(&self, err_type: E::ErrType, err: anyhow::Error) { + self.do_send(E::from_error(err_type, &err.to_string())) } } impl BusError for Recipient { - fn err(&self, err_type: E::ErrorType, err: anyhow::Error) { - self.do_send(E::from_error(err_type, err)) + fn err(&self, err_type: E::ErrType, err: anyhow::Error) { + self.do_send(E::from_error(err_type, &err.to_string())) } } diff --git a/crates/events/src/lib.rs b/crates/events/src/lib.rs index e410e01d3b..bb254b4111 100644 --- a/crates/events/src/lib.rs +++ b/crates/events/src/lib.rs @@ -8,6 +8,7 @@ mod correlation_id; mod e3id; mod enclave_event; mod event_id; +mod event_manager; mod eventbus; mod eventbus_factory; mod ordered_set; @@ -17,6 +18,7 @@ pub use correlation_id::*; pub use e3id::*; pub use enclave_event::*; pub use event_id::*; +pub use event_manager::*; pub use eventbus::*; pub use eventbus_factory::*; pub use ordered_set::*; From 8678fc1815dc5d273303a11b3e35a282e78b6752 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sat, 29 Nov 2025 23:57:38 +0000 Subject: [PATCH 10/36] stable abstraction --- .../src/ciphernode_builder.rs | 6 +- crates/events/src/enclave_event/mod.rs | 34 ++++-------- crates/events/src/event_manager.rs | 55 +++++++++++-------- crates/events/src/eventbus.rs | 6 +- crates/events/src/eventbus_factory.rs | 1 - crates/net/src/document_publisher.rs | 4 +- crates/request/src/meta.rs | 2 +- crates/request/src/router.rs | 2 +- 8 files changed, 54 insertions(+), 56 deletions(-) diff --git a/crates/ciphernode-builder/src/ciphernode_builder.rs b/crates/ciphernode-builder/src/ciphernode_builder.rs index 267f694755..947dbe5134 100644 --- a/crates/ciphernode-builder/src/ciphernode_builder.rs +++ b/crates/ciphernode-builder/src/ciphernode_builder.rs @@ -17,7 +17,8 @@ use e3_config::chain_config::ChainConfig; use e3_crypto::Cipher; use e3_data::{DataStore, InMemStore, Repositories, RepositoriesFactory}; use e3_events::{ - EnclaveEvent, EnclaveEventDispatcher, EnclaveEventFactory, EventBus, EventBusConfig, + EnclaveEvent, EnclaveEventDispatcher, EnclaveEventFactory, ErrorEvent, EventBus, + EventBusConfig, EventDispatcher, EventFactory, FullFactory, TestEvent, }; use e3_evm::{ helpers::{ @@ -285,8 +286,7 @@ impl CiphernodeBuilder { }; // Setup an event dispatcher - let dispatcher = - EnclaveEventDispatcher::new(local_bus, Arc::new(EnclaveEventFactory::new())); + let dispatcher = EventBus::manager(local_bus, Arc::new(EnclaveEventFactory::new())); // History collector for taking historical events for analysis and testing let history = if self.testmode_history { diff --git a/crates/events/src/enclave_event/mod.rs b/crates/events/src/enclave_event/mod.rs index 4b88404142..b95c4405ce 100644 --- a/crates/events/src/enclave_event/mod.rs +++ b/crates/events/src/enclave_event/mod.rs @@ -61,7 +61,9 @@ pub use ticket_balance_updated::*; pub use ticket_generated::*; pub use ticket_submitted::*; -use crate::{E3id, ErrorEvent, ErrorFactory, Event, EventFactory, EventId}; +use crate::{ + E3id, ErrorEvent, ErrorEventConstructor, Event, EventConstructorWithTimestamp, EventId, +}; use actix::Message; use serde::{Deserialize, Serialize}; use std::{ @@ -252,26 +254,8 @@ impl fmt::Display for EnclaveEvent { } } -#[derive(Clone)] -pub struct EnclaveEventFactory { - // TODO: hlc: Arc, -} - -impl EnclaveEventFactory { - pub fn new() -> Self { - Self {} - } -} - -impl EventFactory for EnclaveEventFactory { - fn create_local(&self, data: impl Into) -> EnclaveEvent { - let payload = data.into(); - let id = EventId::hash(&payload); - // hcl.tick(); NOTE: we may need to handle the error from hlc here and add try_create_local - EnclaveEvent { id, payload } - } - - fn create_receive(&self, data: impl Into, _remote_ts: u128) -> EnclaveEvent { +impl EventConstructorWithTimestamp for EnclaveEvent { + fn new_with_timestamp(data: Self::Data, ts: u128) -> Self { let payload = data.into(); let id = EventId::hash(&payload); // hcl.receive(remote_ts)?; @@ -279,9 +263,11 @@ impl EventFactory for EnclaveEventFactory { } } -impl ErrorFactory for EnclaveEventFactory { - fn create_err(&self, err_type: EnclaveErrorType, error: impl Into) -> EnclaveEvent { +impl ErrorEventConstructor for EnclaveEvent { + fn new_error(err_type: EnclaveErrorType, error: impl Into) -> Self { let payload = EnclaveError::from_error(err_type, error); - self.create_local(payload) + let id = EventId::hash(&payload); + let payload = payload.into(); + EnclaveEvent { id, payload } } } diff --git a/crates/events/src/event_manager.rs b/crates/events/src/event_manager.rs index 5957eb7bbe..5d0321995f 100644 --- a/crates/events/src/event_manager.rs +++ b/crates/events/src/event_manager.rs @@ -1,44 +1,59 @@ -use std::sync::Arc; - use actix::{Addr, Recipient}; use crate::{ErrorEvent, Event, EventBus, Subscribe}; +/// Trait to create events pub trait EventFactory { fn create_local(&self, data: impl Into) -> E; fn create_receive(&self, data: impl Into, ts: u128) -> E; } +/// Trait create errors pub trait ErrorFactory { fn create_err(&self, err_type: E::ErrType, error: impl Into) -> E; } +/// Trait to dispatch events pub trait EventDispatcher { fn dispatch(&self, data: impl Into); fn dispatch_from_remote(&self, data: impl Into, ts: u128); } +/// Trait to subscribe to events pub trait EventSubscriber { fn subscribe(&self, event_type: &str, recipient: Recipient); } -pub trait FullFactory: ErrorFactory + EventFactory {} +/// Trait to create an event with a timestamp from its associated type data +pub trait EventConstructorWithTimestamp: Event + Sized { + fn new_with_timestamp(data: Self::Data, ts: u128) -> Self; +} + +pub trait ErrorEventConstructor: ErrorEvent + Sized { + fn new_error(err_type: Self::ErrType, error: impl Into) -> Self; +} + +pub trait ManagedEvent: ErrorEvent + EventConstructorWithTimestamp + ErrorEventConstructor {} -pub struct EventManager { +impl ManagedEvent for E where + E: ErrorEvent + EventConstructorWithTimestamp + ErrorEventConstructor +{ +} + +#[derive(Clone)] +pub struct EventManager { bus: Addr>, - factory: Arc, } -impl EventManager { - pub fn new(bus: Addr>, factory: Arc) -> Self { - Self { bus, factory } +impl EventManager { + pub fn new(bus: Addr>) -> Self { + Self { bus } } } -impl EventDispatcher for EventManager +impl EventDispatcher for EventManager where - E: Event, - F: EventFactory, + E: ManagedEvent, { fn dispatch(&self, data: impl Into) { let evt = self.create_local(data); @@ -51,30 +66,24 @@ where } } -impl EventFactory for EventManager -where - E: Event, - F: EventFactory, -{ +impl EventFactory for EventManager { fn create_local(&self, data: impl Into) -> E { - self.factory.create_local(data) + E::new_with_timestamp(data.into(), 0) } fn create_receive(&self, data: impl Into, ts: u128) -> E { - self.factory.create_receive(data, ts) + E::new_with_timestamp(data.into(), ts) } } -impl> ErrorFactory for EventManager { +impl ErrorFactory for EventManager { fn create_err(&self, err_type: E::ErrType, error: impl Into) -> E { - self.factory.create_err(err_type, error) + E::new_error(err_type, error) } } -impl EventSubscriber for EventManager { +impl EventSubscriber for EventManager { fn subscribe(&self, event_type: &str, recipient: Recipient) { self.bus.do_send(Subscribe::new(event_type, recipient)) } } - -impl> FullFactory for EventManager {} diff --git a/crates/events/src/eventbus.rs b/crates/events/src/eventbus.rs index d33ad0b544..09aa3f6a64 100644 --- a/crates/events/src/eventbus.rs +++ b/crates/events/src/eventbus.rs @@ -12,6 +12,8 @@ use std::hash::Hash; use std::marker::PhantomData; use tracing::info; +use crate::EventManager; + ////////////////////////////////////////////////////////////////////////////// // Core Traits ////////////////////////////////////////////////////////////////////////////// @@ -101,7 +103,9 @@ impl EventBus { addr } - // pub fn manager(source: Addr>) -> M {} + pub fn manager(source: Addr>) -> EventManager { + EventManager::new(source) + } pub fn pipe(source: &Addr>, dest: &Addr>) { source.do_send(Subscribe::new("*", dest.clone().recipient())) diff --git a/crates/events/src/eventbus_factory.rs b/crates/events/src/eventbus_factory.rs index 2dd383ef38..5864d6d606 100644 --- a/crates/events/src/eventbus_factory.rs +++ b/crates/events/src/eventbus_factory.rs @@ -13,7 +13,6 @@ use std::collections::HashMap; use std::sync::Mutex; use crate::EnclaveEvent; -use crate::ErrorEvent; use crate::Event; use crate::EventBus; use crate::HistoryCollector; diff --git a/crates/net/src/document_publisher.rs b/crates/net/src/document_publisher.rs index 8007ef2950..015f411346 100644 --- a/crates/net/src/document_publisher.rs +++ b/crates/net/src/document_publisher.rs @@ -15,8 +15,8 @@ use anyhow::Result; use chrono::{DateTime, Utc}; use e3_events::{ BusError, CiphernodeSelected, CorrelationId, DocumentKind, DocumentMeta, DocumentReceived, - E3RequestComplete, E3id, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, PartyId, - PublishDocumentRequested, Subscribe, ThresholdShareCreated, + E3RequestComplete, E3id, EnclaveErrorType, EnclaveEvent, EnclaveEventData, Event, EventBus, + PartyId, PublishDocumentRequested, Subscribe, ThresholdShareCreated, }; use e3_utils::retry::{retry_with_backoff, to_retry}; use e3_utils::ArcBytes; diff --git a/crates/request/src/meta.rs b/crates/request/src/meta.rs index b5af65cf3c..25c4551999 100644 --- a/crates/request/src/meta.rs +++ b/crates/request/src/meta.rs @@ -8,7 +8,7 @@ use crate::{E3Context, E3ContextSnapshot, E3Extension, MetaRepositoryFactory, Ty use anyhow::*; use async_trait::async_trait; use e3_data::RepositoriesFactory; -use e3_events::{E3Requested, EnclaveEvent, EnclaveEventData, Seed}; +use e3_events::{E3Requested, EnclaveEvent, EnclaveEventData, Event, Seed}; use e3_utils::utility_types::ArcBytes; pub const META_KEY: TypedKey = TypedKey::new("meta"); diff --git a/crates/request/src/router.rs b/crates/request/src/router.rs index 6291ecc90c..f0a7c4feae 100644 --- a/crates/request/src/router.rs +++ b/crates/request/src/router.rs @@ -23,7 +23,7 @@ use e3_data::Snapshot; use e3_events::E3RequestComplete; use e3_events::EnclaveEventData; use e3_events::Shutdown; -use e3_events::{E3id, EnclaveEvent, EventBus, Subscribe}; +use e3_events::{E3id, EnclaveEvent, Event, EventBus, Subscribe}; use serde::Deserialize; use serde::Serialize; use std::collections::HashSet; From 49e8e24b8fb2e493f5f0707e64d3d581ceee1766 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 02:11:47 +0000 Subject: [PATCH 11/36] fix up traits and types --- crates/aggregator/src/committee_finalizer.rs | 8 +-- crates/aggregator/src/ext.rs | 16 +++-- crates/aggregator/src/plaintext_aggregator.rs | 8 +-- crates/aggregator/src/publickey_aggregator.rs | 6 +- .../src/threshold_plaintext_aggregator.rs | 6 +- crates/data/src/sled_store.rs | 12 ++-- crates/entrypoint/src/helpers/datastore.rs | 9 +-- .../entrypoint/src/start/aggregator_start.rs | 4 +- crates/entrypoint/src/start/start.rs | 4 +- .../events/src/enclave_event/enclave_error.rs | 4 +- crates/events/src/enclave_event/mod.rs | 10 +-- crates/events/src/event_manager.rs | 63 +++++----------- crates/events/src/eventbus.rs | 52 +------------- crates/events/src/eventbus_factory.rs | 7 +- crates/events/src/lib.rs | 3 + crates/events/src/prelude.rs | 2 + crates/events/src/traits.rs | 71 +++++++++++++++++++ crates/evm/src/bonding_registry_sol.rs | 6 +- crates/evm/src/ciphernode_registry_sol.rs | 16 ++--- crates/evm/src/enclave_sol.rs | 4 +- crates/evm/src/enclave_sol_reader.rs | 4 +- crates/evm/src/enclave_sol_writer.rs | 7 +- crates/evm/src/event_reader.rs | 10 +-- .../evm/src/historical_event_coordinator.rs | 8 +-- crates/fhe/src/ext.rs | 6 +- crates/keyshare/src/ext.rs | 12 ++-- crates/keyshare/src/keyshare.rs | 8 +-- crates/keyshare/src/threshold_keyshare.rs | 8 +-- crates/logger/src/logger.rs | 2 +- crates/net/src/document_publisher.rs | 67 +++++++++-------- crates/net/src/net_event_translator.rs | 11 +-- crates/request/src/router.rs | 9 +-- crates/sortition/src/ciphernode_selector.rs | 8 +-- crates/sortition/src/sortition.rs | 8 +-- crates/test-helpers/src/lib.rs | 7 +- crates/test-helpers/src/plaintext_writer.rs | 4 +- crates/test-helpers/src/public_key_writer.rs | 4 +- 37 files changed, 254 insertions(+), 240 deletions(-) create mode 100644 crates/events/src/prelude.rs create mode 100644 crates/events/src/traits.rs diff --git a/crates/aggregator/src/committee_finalizer.rs b/crates/aggregator/src/committee_finalizer.rs index e8e6afcf56..922080aa45 100644 --- a/crates/aggregator/src/committee_finalizer.rs +++ b/crates/aggregator/src/committee_finalizer.rs @@ -7,7 +7,7 @@ use actix::prelude::*; use e3_events::{ CommitteeFinalizeRequested, CommitteeRequested, EnclaveEvent, EnclaveEventData, EventBus, - Shutdown, Subscribe, + EventManager, Shutdown, Subscribe, }; use std::collections::HashMap; use std::time::Duration; @@ -16,19 +16,19 @@ use tracing::{error, info}; /// CommitteeFinalizer is an actor that listens to CommitteeRequested events and dispatches /// CommitteeFinalizeRequested events after the submission deadline has passed. pub struct CommitteeFinalizer { - bus: Addr>, + bus: EventManager, pending_committees: HashMap, } impl CommitteeFinalizer { - pub fn new(bus: &Addr>) -> Self { + pub fn new(bus: &EventManager) -> Self { Self { bus: bus.clone(), pending_committees: HashMap::new(), } } - pub fn attach(bus: &Addr>) -> Addr { + pub fn attach(bus: &EventManager) -> Addr { let addr = CommitteeFinalizer::new(bus).start(); bus.do_send(Subscribe::new( diff --git a/crates/aggregator/src/ext.rs b/crates/aggregator/src/ext.rs index 88fcab5013..8a7d9715b7 100644 --- a/crates/aggregator/src/ext.rs +++ b/crates/aggregator/src/ext.rs @@ -15,7 +15,9 @@ use actix::{Actor, Addr}; use anyhow::{anyhow, Result}; use async_trait::async_trait; use e3_data::{AutoPersist, RepositoriesFactory}; -use e3_events::{BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus}; +use e3_events::{ + BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, EventManager, +}; use e3_fhe::ext::FHE_KEY; use e3_multithread::Multithread; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, META_KEY}; @@ -23,12 +25,12 @@ use e3_sortition::Sortition; #[deprecated = "In favour of ThresholdPlaintextAggregatorExtension"] pub struct PlaintextAggregatorExtension { - bus: Addr>, + bus: EventManager, sortition: Addr, } impl PlaintextAggregatorExtension { - pub fn create(bus: &Addr>, sortition: &Addr) -> Box { + pub fn create(bus: &EventManager, sortition: &Addr) -> Box { Box::new(Self { bus: bus.clone(), sortition: sortition.clone(), @@ -143,12 +145,12 @@ impl E3Extension for PlaintextAggregatorExtension { } pub struct PublicKeyAggregatorExtension { - bus: Addr>, + bus: EventManager, sortition: Addr, } impl PublicKeyAggregatorExtension { - pub fn create(bus: &Addr>, sortition: &Addr) -> Box { + pub fn create(bus: &EventManager, sortition: &Addr) -> Box { Box::new(Self { bus: bus.clone(), sortition: sortition.clone(), @@ -249,14 +251,14 @@ impl E3Extension for PublicKeyAggregatorExtension { } pub struct ThresholdPlaintextAggregatorExtension { - bus: Addr>, + bus: EventManager, sortition: Addr, multithread: Addr, } impl ThresholdPlaintextAggregatorExtension { pub fn create( - bus: &Addr>, + bus: &EventManager, sortition: &Addr, multithread: &Addr, ) -> Box { diff --git a/crates/aggregator/src/plaintext_aggregator.rs b/crates/aggregator/src/plaintext_aggregator.rs index 0fa37509d3..9d5b81fcb2 100644 --- a/crates/aggregator/src/plaintext_aggregator.rs +++ b/crates/aggregator/src/plaintext_aggregator.rs @@ -8,8 +8,8 @@ use actix::prelude::*; use anyhow::Result; use e3_data::Persistable; use e3_events::{ - DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, EventBus, OrderedSet, - PlaintextAggregated, Seed, + DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, EventBus, EventManager, + OrderedSet, PlaintextAggregated, Seed, }; use e3_fhe::{Fhe, GetAggregatePlaintext}; use e3_sortition::{GetNodeIndex, Sortition}; @@ -72,7 +72,7 @@ struct ComputeAggregate { #[deprecated = "To be replaced by ThresholdPlaintextAggregator"] pub struct PlaintextAggregator { fhe: Arc, - bus: Addr>, + bus: EventManager, sortition: Addr, e3_id: E3id, state: Persistable, @@ -80,7 +80,7 @@ pub struct PlaintextAggregator { pub struct PlaintextAggregatorParams { pub fhe: Arc, - pub bus: Addr>, + pub bus: EventManager, pub sortition: Addr, pub e3_id: E3id, } diff --git a/crates/aggregator/src/publickey_aggregator.rs b/crates/aggregator/src/publickey_aggregator.rs index 562ac94d31..8c06616080 100644 --- a/crates/aggregator/src/publickey_aggregator.rs +++ b/crates/aggregator/src/publickey_aggregator.rs @@ -8,7 +8,7 @@ use actix::prelude::*; use anyhow::Result; use e3_data::Persistable; use e3_events::{ - Die, E3id, EnclaveEvent, EnclaveEventData, EventBus, KeyshareCreated, OrderedSet, + Die, E3id, EnclaveEvent, EnclaveEventData, EventBus, EventManager, KeyshareCreated, OrderedSet, PublicKeyAggregated, Seed, }; use e3_fhe::{Fhe, GetAggregatePublicKey}; @@ -59,7 +59,7 @@ struct NotifyNetwork { pub struct PublicKeyAggregator { fhe: Arc, - bus: Addr>, + bus: EventManager, sortition: Addr, e3_id: E3id, state: Persistable, @@ -67,7 +67,7 @@ pub struct PublicKeyAggregator { pub struct PublicKeyAggregatorParams { pub fhe: Arc, - pub bus: Addr>, + pub bus: EventManager, pub sortition: Addr, pub e3_id: E3id, } diff --git a/crates/aggregator/src/threshold_plaintext_aggregator.rs b/crates/aggregator/src/threshold_plaintext_aggregator.rs index 73d87ad0d5..713c60b3e7 100644 --- a/crates/aggregator/src/threshold_plaintext_aggregator.rs +++ b/crates/aggregator/src/threshold_plaintext_aggregator.rs @@ -11,7 +11,7 @@ use anyhow::{anyhow, bail, Result}; use e3_data::Persistable; use e3_events::{ ComputeRequest, DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, EventBus, - PlaintextAggregated, Seed, + EventManager, PlaintextAggregated, Seed, }; use e3_multithread::Multithread; use e3_sortition::{GetNodesForE3, Sortition}; @@ -122,7 +122,7 @@ pub struct ComputeAggregate { pub struct ThresholdPlaintextAggregator { multithread: Addr, - bus: Addr>, + bus: EventManager, sortition: Addr, e3_id: E3id, state: Persistable, @@ -130,7 +130,7 @@ pub struct ThresholdPlaintextAggregator { pub struct ThresholdPlaintextAggregatorParams { pub multithread: Addr, - pub bus: Addr>, + pub bus: EventManager, pub sortition: Addr, pub e3_id: E3id, } diff --git a/crates/data/src/sled_store.rs b/crates/data/src/sled_store.rs index c3f6c0114a..ce88ae4687 100644 --- a/crates/data/src/sled_store.rs +++ b/crates/data/src/sled_store.rs @@ -8,8 +8,8 @@ use crate::{Get, Insert, InsertSync, Remove}; use actix::{Actor, ActorContext, Addr, Handler}; use anyhow::{Context, Result}; use e3_events::{ - get_enclave_event_bus, BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, Event, - EventBus, Subscribe, + get_enclave_event_manager, prelude::*, EnclaveErrorType, EnclaveEvent, EnclaveEventData, + EventManager, }; use once_cell::sync::Lazy; use sled::Db; @@ -22,7 +22,7 @@ use tracing::{error, info}; pub struct SledStore { db: Option, - bus: Addr>, + bus: EventManager, } impl Actor for SledStore { @@ -30,7 +30,7 @@ impl Actor for SledStore { } impl SledStore { - pub fn new(bus: &Addr>, path: &PathBuf) -> Result> { + pub fn new(bus: &EventManager, path: &PathBuf) -> Result> { info!("Starting SledStore with {:?}", path); let db = SledDb::new(PathBuf::from(path))?; @@ -40,7 +40,7 @@ impl SledStore { } .start(); - bus.do_send(Subscribe::new("Shutdown", store.clone().into())); + bus.subscribe("Shutdown", store.clone().into()); Ok(store) } @@ -48,7 +48,7 @@ impl SledStore { pub fn from_db(db: SledDb) -> Result { Ok(Self { db: Some(db), - bus: get_enclave_event_bus(), + bus: get_enclave_event_manager(), }) } } diff --git a/crates/entrypoint/src/helpers/datastore.rs b/crates/entrypoint/src/helpers/datastore.rs index 166ea9d92c..15c859771d 100644 --- a/crates/entrypoint/src/helpers/datastore.rs +++ b/crates/entrypoint/src/helpers/datastore.rs @@ -11,9 +11,9 @@ use anyhow::Result; use e3_config::AppConfig; use e3_data::{DataStore, InMemStore, SledDb, SledStore}; use e3_data::{Repositories, RepositoriesFactory}; -use e3_events::{get_enclave_event_bus, EnclaveEvent, EventBus}; +use e3_events::{get_enclave_event_bus, EnclaveEvent, EventBus, EventManager}; -pub fn get_sled_store(bus: &Addr>, db_file: &PathBuf) -> Result { +pub fn get_sled_store(bus: &EventManager, db_file: &PathBuf) -> Result { Ok((&SledStore::new(bus, db_file)?).into()) } @@ -21,10 +21,7 @@ pub fn get_in_mem_store() -> DataStore { (&InMemStore::new(true).start()).into() } -pub fn setup_datastore( - config: &AppConfig, - bus: &Addr>, -) -> Result { +pub fn setup_datastore(config: &AppConfig, bus: &EventManager) -> Result { let store: DataStore = if !config.use_in_mem_store() { get_sled_store(&bus, &config.db_file())? } else { diff --git a/crates/entrypoint/src/start/aggregator_start.rs b/crates/entrypoint/src/start/aggregator_start.rs index c9a93f386a..4c683eda08 100644 --- a/crates/entrypoint/src/start/aggregator_start.rs +++ b/crates/entrypoint/src/start/aggregator_start.rs @@ -10,7 +10,7 @@ use e3_ciphernode_builder::CiphernodeBuilder; use e3_config::AppConfig; use e3_crypto::Cipher; use e3_data::RepositoriesFactory; -use e3_events::{get_enclave_event_bus, EnclaveEvent, EventBus}; +use e3_events::{get_enclave_event_bus, EnclaveEvent, EventBus, EventManager}; use e3_net::{NetEventTranslator, NetRepositoryFactory}; use e3_test_helpers::{PlaintextWriter, PublicKeyWriter}; use rand::SeedableRng; @@ -28,7 +28,7 @@ pub async fn execute( pubkey_write_path: Option, plaintext_write_path: Option, experimental_trbfv: bool, -) -> Result<(Addr>, JoinHandle>, String)> { +) -> Result<(EventManager, JoinHandle>, String)> { let bus = get_enclave_event_bus(); let rng = Arc::new(Mutex::new(ChaCha20Rng::from_rng(OsRng)?)); let store = setup_datastore(config, &bus)?; diff --git a/crates/entrypoint/src/start/start.rs b/crates/entrypoint/src/start/start.rs index 1cc8bec79a..0574e94bac 100644 --- a/crates/entrypoint/src/start/start.rs +++ b/crates/entrypoint/src/start/start.rs @@ -11,7 +11,7 @@ use e3_ciphernode_builder::CiphernodeBuilder; use e3_config::AppConfig; use e3_crypto::Cipher; use e3_data::RepositoriesFactory; -use e3_events::get_enclave_event_bus; +use e3_events::{get_enclave_event_bus, EventManager}; use e3_events::{EnclaveEvent, EventBus}; use e3_net::{NetEventTranslator, NetRepositoryFactory}; use rand::SeedableRng; @@ -27,7 +27,7 @@ pub async fn execute( config: &AppConfig, address: Address, experimental_trbfv: bool, -) -> Result<(Addr>, JoinHandle>, String)> { +) -> Result<(EventManager, JoinHandle>, String)> { let rng = Arc::new(Mutex::new(rand_chacha::ChaCha20Rng::from_rng(OsRng)?)); let bus = get_enclave_event_bus(); diff --git a/crates/events/src/enclave_event/enclave_error.rs b/crates/events/src/enclave_event/enclave_error.rs index 3710585f67..1487cc7836 100644 --- a/crates/events/src/enclave_event/enclave_error.rs +++ b/crates/events/src/enclave_event/enclave_error.rs @@ -38,10 +38,10 @@ pub enum EnclaveErrorType { } impl EnclaveError { - pub fn new(err_type: EnclaveErrorType, message: impl Into) -> Self { + pub fn new(err_type: EnclaveErrorType, message: impl Into) -> Self { Self { err_type, - message: message.into(), + message: message.into().to_string(), } } } diff --git a/crates/events/src/enclave_event/mod.rs b/crates/events/src/enclave_event/mod.rs index b95c4405ce..18175014d4 100644 --- a/crates/events/src/enclave_event/mod.rs +++ b/crates/events/src/enclave_event/mod.rs @@ -62,7 +62,8 @@ pub use ticket_generated::*; pub use ticket_submitted::*; use crate::{ - E3id, ErrorEvent, ErrorEventConstructor, Event, EventConstructorWithTimestamp, EventId, + traits::{ErrorEvent, ErrorEventConstructor, Event, EventConstructorWithTimestamp}, + E3id, EventId, }; use actix::Message; use serde::{Deserialize, Serialize}; @@ -161,8 +162,9 @@ impl Event for EnclaveEvent { impl ErrorEvent for EnclaveEvent { type ErrType = EnclaveErrorType; + type FromError = anyhow::Error; - fn from_error(err_type: Self::ErrType, msg: impl Into) -> Self { + fn from_error(err_type: Self::ErrType, msg: impl Into) -> Self { let payload = EnclaveError::new(err_type, msg); let id = EventId::hash(&payload); EnclaveEvent { @@ -264,8 +266,8 @@ impl EventConstructorWithTimestamp for EnclaveEvent { } impl ErrorEventConstructor for EnclaveEvent { - fn new_error(err_type: EnclaveErrorType, error: impl Into) -> Self { - let payload = EnclaveError::from_error(err_type, error); + fn new_error(err_type: EnclaveErrorType, error: impl Into) -> Self { + let payload = EnclaveError::from_error(err_type, error.into().to_string()); let id = EventId::hash(&payload); let payload = payload.into(); EnclaveEvent { id, payload } diff --git a/crates/events/src/event_manager.rs b/crates/events/src/event_manager.rs index 5d0321995f..56cc9cd128 100644 --- a/crates/events/src/event_manager.rs +++ b/crates/events/src/event_manager.rs @@ -1,44 +1,12 @@ use actix::{Addr, Recipient}; -use crate::{ErrorEvent, Event, EventBus, Subscribe}; - -/// Trait to create events -pub trait EventFactory { - fn create_local(&self, data: impl Into) -> E; - fn create_receive(&self, data: impl Into, ts: u128) -> E; -} - -/// Trait create errors -pub trait ErrorFactory { - fn create_err(&self, err_type: E::ErrType, error: impl Into) -> E; -} - -/// Trait to dispatch events -pub trait EventDispatcher { - fn dispatch(&self, data: impl Into); - fn dispatch_from_remote(&self, data: impl Into, ts: u128); -} - -/// Trait to subscribe to events -pub trait EventSubscriber { - fn subscribe(&self, event_type: &str, recipient: Recipient); -} - -/// Trait to create an event with a timestamp from its associated type data -pub trait EventConstructorWithTimestamp: Event + Sized { - fn new_with_timestamp(data: Self::Data, ts: u128) -> Self; -} - -pub trait ErrorEventConstructor: ErrorEvent + Sized { - fn new_error(err_type: Self::ErrType, error: impl Into) -> Self; -} - -pub trait ManagedEvent: ErrorEvent + EventConstructorWithTimestamp + ErrorEventConstructor {} - -impl ManagedEvent for E where - E: ErrorEvent + EventConstructorWithTimestamp + ErrorEventConstructor -{ -} +use crate::{ + traits::{ + ErrorDispatcher, ErrorEventConstructor, ErrorFactory, Event, EventConstructorWithTimestamp, + EventDispatcher, EventFactory, EventSubscriber, ManagedEvent, + }, + EventBus, Subscribe, +}; #[derive(Clone)] pub struct EventManager { @@ -51,10 +19,7 @@ impl EventManager { } } -impl EventDispatcher for EventManager -where - E: ManagedEvent, -{ +impl EventDispatcher for EventManager { fn dispatch(&self, data: impl Into) { let evt = self.create_local(data); self.bus.do_send(evt); @@ -66,6 +31,16 @@ where } } +impl ErrorDispatcher for EventManager +where + E: ManagedEvent, +{ + fn err(&self, err_type: E::ErrType, error: impl Into) { + let evt = self.create_err(err_type, error); + self.bus.do_send(evt); + } +} + impl EventFactory for EventManager { fn create_local(&self, data: impl Into) -> E { E::new_with_timestamp(data.into(), 0) @@ -77,7 +52,7 @@ impl EventFactory for EventManager { } impl ErrorFactory for EventManager { - fn create_err(&self, err_type: E::ErrType, error: impl Into) -> E { + fn create_err(&self, err_type: E::ErrType, error: impl Into) -> E { E::new_error(err_type, error) } } diff --git a/crates/events/src/eventbus.rs b/crates/events/src/eventbus.rs index 09aa3f6a64..3a21e6701e 100644 --- a/crates/events/src/eventbus.rs +++ b/crates/events/src/eventbus.rs @@ -4,41 +4,14 @@ // without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. +use crate::traits::{ErrorEvent, Event}; +use crate::EventManager; use actix::prelude::*; use bloom::{BloomFilter, ASMS}; use std::collections::{HashMap, VecDeque}; -use std::fmt::Display; -use std::hash::Hash; use std::marker::PhantomData; use tracing::info; -use crate::EventManager; - -////////////////////////////////////////////////////////////////////////////// -// Core Traits -////////////////////////////////////////////////////////////////////////////// - -/// Trait that must be implemented by events used with EventBus -pub trait Event: Message + Clone + Display + Send + Sync + Unpin + 'static { - type Id: Hash + Eq + Clone + Unpin + Send + Sync + Display; - - /// Payload for the Event - type Data; - - fn event_type(&self) -> String; - fn event_id(&self) -> Self::Id; - fn get_data(&self) -> &Self::Data; - fn into_data(self) -> Self::Data; -} - -/// Trait for events that contain an error -pub trait ErrorEvent: Event { - /// Error type associated with this event - type ErrType; - - fn from_error(err_type: Self::ErrType, error: impl Into) -> Self; -} - ////////////////////////////////////////////////////////////////////////////// // Configuration ////////////////////////////////////////////////////////////////////////////// @@ -316,27 +289,6 @@ impl GetErrors { } } -////////////////////////////////////////////////////////////////////////////// -// Error Bus Trait -////////////////////////////////////////////////////////////////////////////// - -/// Trait to send errors directly to the bus -pub trait BusError { - fn err(&self, err_type: E::ErrType, err: anyhow::Error); -} - -impl BusError for Addr> { - fn err(&self, err_type: E::ErrType, err: anyhow::Error) { - self.do_send(E::from_error(err_type, &err.to_string())) - } -} - -impl BusError for Recipient { - fn err(&self, err_type: E::ErrType, err: anyhow::Error) { - self.do_send(E::from_error(err_type, &err.to_string())) - } -} - ////////////////////////////////////////////////////////////////////////////// // History Collector ////////////////////////////////////////////////////////////////////////////// diff --git a/crates/events/src/eventbus_factory.rs b/crates/events/src/eventbus_factory.rs index 5864d6d606..62ac3773ad 100644 --- a/crates/events/src/eventbus_factory.rs +++ b/crates/events/src/eventbus_factory.rs @@ -12,9 +12,10 @@ use std::any::TypeId; use std::collections::HashMap; use std::sync::Mutex; +use crate::traits::Event; use crate::EnclaveEvent; -use crate::Event; use crate::EventBus; +use crate::EventManager; use crate::HistoryCollector; use crate::Subscribe; @@ -90,3 +91,7 @@ pub fn get_enclave_event_bus() -> Addr> { pub fn get_error_collector() -> Addr> { EventBusFactory::instance().get_error_collector() } + +pub fn get_enclave_event_manager() -> EventManager { + EventBus::manager(get_enclave_event_bus()) +} diff --git a/crates/events/src/lib.rs b/crates/events/src/lib.rs index bb254b4111..b348b7e2d3 100644 --- a/crates/events/src/lib.rs +++ b/crates/events/src/lib.rs @@ -12,7 +12,9 @@ mod event_manager; mod eventbus; mod eventbus_factory; mod ordered_set; +pub mod prelude; mod seed; +mod traits; pub use correlation_id::*; pub use e3id::*; @@ -23,3 +25,4 @@ pub use eventbus::*; pub use eventbus_factory::*; pub use ordered_set::*; pub use seed::*; +pub use traits::*; diff --git a/crates/events/src/prelude.rs b/crates/events/src/prelude.rs new file mode 100644 index 0000000000..0ec4685b02 --- /dev/null +++ b/crates/events/src/prelude.rs @@ -0,0 +1,2 @@ +// Export a prelude to ensure all traits are available +pub use crate::traits::*; diff --git a/crates/events/src/traits.rs b/crates/events/src/traits.rs new file mode 100644 index 0000000000..7dd42535cd --- /dev/null +++ b/crates/events/src/traits.rs @@ -0,0 +1,71 @@ +use actix::{Message, Recipient}; +use std::fmt::Display; +use std::hash::Hash; + +/// Trait that must be implemented by events used with EventBus +pub trait Event: Message + Clone + Display + Send + Sync + Unpin + 'static { + type Id: Hash + Eq + Clone + Unpin + Send + Sync + Display; + + /// Payload for the Event + type Data; + + fn event_type(&self) -> String; + fn event_id(&self) -> Self::Id; + fn get_data(&self) -> &Self::Data; + fn into_data(self) -> Self::Data; +} + +/// Trait for events that contain an error +pub trait ErrorEvent: Event { + /// Error type associated with this event + type ErrType; + type FromError; + + fn from_error(err_type: Self::ErrType, error: impl Into) -> Self; +} + +/// Trait to create events +pub trait EventFactory { + fn create_local(&self, data: impl Into) -> E; + fn create_receive(&self, data: impl Into, ts: u128) -> E; +} + +/// Trait create errors +pub trait ErrorFactory { + fn create_err(&self, err_type: E::ErrType, error: impl Into) -> E; +} + +/// Trait to dispatch events +pub trait EventDispatcher { + fn dispatch(&self, data: impl Into); + fn dispatch_from_remote(&self, data: impl Into, ts: u128); +} + +/// Trait for dispatching errors +pub trait ErrorDispatcher { + fn err(&self, err_type: E::ErrType, error: impl Into); +} + +/// Trait to subscribe to events +pub trait EventSubscriber { + fn subscribe(&self, event_type: &str, recipient: Recipient); +} + +/// Trait to create an event with a timestamp from its associated type data +pub trait EventConstructorWithTimestamp: Event + Sized { + fn new_with_timestamp(data: Self::Data, ts: u128) -> Self; +} + +pub trait ErrorEventConstructor: ErrorEvent + Sized { + fn new_error(err_type: Self::ErrType, error: impl Into) -> Self; +} + +pub trait ManagedEvent: + Sized + ErrorEvent + EventConstructorWithTimestamp + ErrorEventConstructor +{ +} + +impl ManagedEvent for E where + E: Sized + Event + ErrorEvent + EventConstructorWithTimestamp + ErrorEventConstructor +{ +} diff --git a/crates/evm/src/bonding_registry_sol.rs b/crates/evm/src/bonding_registry_sol.rs index b89e5a2bb6..e559fd68a3 100644 --- a/crates/evm/src/bonding_registry_sol.rs +++ b/crates/evm/src/bonding_registry_sol.rs @@ -16,7 +16,7 @@ use alloy::{ }; use anyhow::Result; use e3_data::Repository; -use e3_events::{EnclaveEvent, EventBus}; +use e3_events::{EnclaveEvent, EventBus, EventManager}; use tracing::{error, info, trace}; sol!( @@ -145,7 +145,7 @@ pub struct BondingRegistrySolReader; impl BondingRegistrySolReader { pub async fn attach

( processor: &Recipient, - bus: &Addr>, + bus: &EventManager, provider: EthProvider

, contract_address: &str, repository: &Repository, @@ -179,7 +179,7 @@ pub struct BondingRegistrySol; impl BondingRegistrySol { pub async fn attach

( processor: &Recipient, - bus: &Addr>, + bus: &EventManager, provider: EthProvider

, contract_address: &str, repository: &Repository, diff --git a/crates/evm/src/ciphernode_registry_sol.rs b/crates/evm/src/ciphernode_registry_sol.rs index 3428be9a42..248df3b5ab 100644 --- a/crates/evm/src/ciphernode_registry_sol.rs +++ b/crates/evm/src/ciphernode_registry_sol.rs @@ -19,8 +19,8 @@ use anyhow::Result; use e3_data::Repository; use e3_events::{ BusError, CommitteeFinalizeRequested, CommitteeFinalized, E3id, EnclaveErrorType, EnclaveEvent, - EnclaveEventData, EventBus, OrderedSet, PublicKeyAggregated, Seed, Shutdown, Subscribe, - TicketGenerated, TicketId, + EnclaveEventData, EventBus, EventManager, OrderedSet, PublicKeyAggregated, Seed, Shutdown, + Subscribe, TicketGenerated, TicketId, }; use tracing::{error, info, trace}; @@ -218,7 +218,7 @@ pub struct CiphernodeRegistrySolReader; impl CiphernodeRegistrySolReader { pub async fn attach

( processor: &Recipient, - bus: &Addr>, + bus: &EventManager, provider: EthProvider

, contract_address: &str, repository: &Repository, @@ -250,12 +250,12 @@ impl CiphernodeRegistrySolReader { pub struct CiphernodeRegistrySolWriter

{ provider: EthProvider

, contract_address: Address, - bus: Addr>, + bus: EventManager, } impl CiphernodeRegistrySolWriter

{ pub async fn new( - bus: &Addr>, + bus: &EventManager, provider: EthProvider

, contract_address: Address, ) -> Result { @@ -267,7 +267,7 @@ impl CiphernodeRegistrySolWriter } pub async fn attach( - bus: &Addr>, + bus: &EventManager, provider: EthProvider

, contract_address: &str, is_aggregator: bool, @@ -513,7 +513,7 @@ pub struct CiphernodeRegistrySol; impl CiphernodeRegistrySol { pub async fn attach

( processor: &Recipient, - bus: &Addr>, + bus: &EventManager, provider: EthProvider

, contract_address: &str, repository: &Repository, @@ -537,7 +537,7 @@ impl CiphernodeRegistrySol { } pub async fn attach_writer

( - bus: &Addr>, + bus: &EventManager, provider: EthProvider

, contract_address: &str, is_aggregator: bool, diff --git a/crates/evm/src/enclave_sol.rs b/crates/evm/src/enclave_sol.rs index a0ec11695a..e28a2018c1 100644 --- a/crates/evm/src/enclave_sol.rs +++ b/crates/evm/src/enclave_sol.rs @@ -12,14 +12,14 @@ use actix::{Addr, Recipient}; use alloy::providers::{Provider, WalletProvider}; use anyhow::Result; use e3_data::Repository; -use e3_events::{EnclaveEvent, EventBus}; +use e3_events::{EnclaveEvent, EventBus, EventManager}; pub struct EnclaveSol; impl EnclaveSol { pub async fn attach( processor: &Recipient, - bus: &Addr>, + bus: &EventManager, read_provider: EthProvider, write_provider: EthProvider, contract_address: &str, diff --git a/crates/evm/src/enclave_sol_reader.rs b/crates/evm/src/enclave_sol_reader.rs index c06eff602a..f39847b3d6 100644 --- a/crates/evm/src/enclave_sol_reader.rs +++ b/crates/evm/src/enclave_sol_reader.rs @@ -13,7 +13,7 @@ use alloy::providers::Provider; use alloy::{sol, sol_types::SolEvent}; use anyhow::Result; use e3_data::Repository; -use e3_events::{E3id, EnclaveEvent, EventBus}; +use e3_events::{E3id, EnclaveEvent, EventBus, EventManager}; use e3_utils::utility_types::ArcBytes; use num_bigint::BigUint; use tracing::{error, info, trace}; @@ -105,7 +105,7 @@ pub struct EnclaveSolReader; impl EnclaveSolReader { pub async fn attach

( processor: &Recipient, - bus: &Addr>, + bus: &EventManager, provider: EthProvider

, contract_address: &str, repository: &Repository, diff --git a/crates/evm/src/enclave_sol_writer.rs b/crates/evm/src/enclave_sol_writer.rs index a77e543831..6c97fe79c9 100644 --- a/crates/evm/src/enclave_sol_writer.rs +++ b/crates/evm/src/enclave_sol_writer.rs @@ -18,6 +18,7 @@ use alloy::{ }; use anyhow::Result; use e3_events::EnclaveEventData; +use e3_events::EventManager; use e3_events::Shutdown; use e3_events::{BusError, E3id, EnclaveErrorType, PlaintextAggregated, Subscribe}; use e3_events::{EnclaveEvent, EventBus}; @@ -33,12 +34,12 @@ sol!( pub struct EnclaveSolWriter

{ provider: EthProvider

, contract_address: Address, - bus: Addr>, + bus: EventManager, } impl EnclaveSolWriter

{ pub fn new( - bus: &Addr>, + bus: &EventManager, provider: EthProvider

, contract_address: Address, ) -> Result { @@ -50,7 +51,7 @@ impl EnclaveSolWriter

{ } pub async fn attach( - bus: &Addr>, + bus: &EventManager, provider: EthProvider

, contract_address: &str, ) -> Result>> { diff --git a/crates/evm/src/event_reader.rs b/crates/evm/src/event_reader.rs index 725158a5c0..7eedf9588d 100644 --- a/crates/evm/src/event_reader.rs +++ b/crates/evm/src/event_reader.rs @@ -14,10 +14,10 @@ use alloy::providers::Provider; use alloy::rpc::types::Filter; use anyhow::{anyhow, Result}; use e3_data::{AutoPersist, Persistable, Repository}; -use e3_events::Event; use e3_events::{ BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, EventId, Subscribe, }; +use e3_events::{Event, EventManager}; use futures_util::stream::StreamExt; use std::collections::HashSet; use tokio::select; @@ -56,7 +56,7 @@ pub struct EvmEventReaderParams

{ contract_address: Address, start_block: Option, processor: Recipient, - bus: Addr>, + bus: EventManager, state: Persistable, rpc_url: String, } @@ -85,7 +85,7 @@ pub struct EvmEventReader

{ /// Processor to forward events an actor processor: Recipient, /// Event bus for error propagation only - bus: Addr>, + bus: EventManager, /// The auto persistable state of the event reader state: Persistable, /// The RPC URL for the provider @@ -115,7 +115,7 @@ impl EvmEventReader

{ contract_address: &str, start_block: Option, processor: &Recipient, - bus: &Addr>, + bus: &EventManager, repository: &Repository, rpc_url: String, ) -> Result> { @@ -193,7 +193,7 @@ async fn stream_from_evm( extractor: fn(&LogData, Option<&B256>, u64) -> Option, mut shutdown: oneshot::Receiver<()>, start_block: Option, - bus: &Addr>, + bus: &EventManager, rpc_url: String, ) { let chain_id = provider.chain_id(); diff --git a/crates/evm/src/historical_event_coordinator.rs b/crates/evm/src/historical_event_coordinator.rs index 6b6317494f..26a9eace90 100644 --- a/crates/evm/src/historical_event_coordinator.rs +++ b/crates/evm/src/historical_event_coordinator.rs @@ -6,7 +6,7 @@ use crate::EnclaveEvmEvent; use actix::prelude::*; -use e3_events::{EnclaveEvent, EventBus}; +use e3_events::{EnclaveEvent, EventBus, EventManager}; use tracing::info; #[derive(Clone)] @@ -30,13 +30,13 @@ pub struct HistoricalEventCoordinator { /// Buffered events during historical sync buffered_events: Vec, /// Target to forward events to (typically EventBus) - target: Addr>, + target: EventManager, /// Whether we've started forwarding (after Start message) started: bool, } impl HistoricalEventCoordinator { - pub fn new(target: Addr>) -> Self { + pub fn new(target: EventManager) -> Self { Self { registered_count: 0, completed_count: 0, @@ -46,7 +46,7 @@ impl HistoricalEventCoordinator { } } - pub fn setup(target: Addr>) -> Addr { + pub fn setup(target: EventManager) -> Addr { Self::new(target).start() } diff --git a/crates/fhe/src/ext.rs b/crates/fhe/src/ext.rs index c00ba4d2a1..cb9a741bce 100644 --- a/crates/fhe/src/ext.rs +++ b/crates/fhe/src/ext.rs @@ -10,7 +10,7 @@ use anyhow::{anyhow, Result}; use async_trait::async_trait; use e3_data::{FromSnapshotWithParams, RepositoriesFactory, Snapshot}; use e3_events::{ - BusError, E3Requested, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, + BusError, E3Requested, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, EventManager, }; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, TypedKey}; use e3_utils::SharedRng; @@ -21,11 +21,11 @@ pub const FHE_KEY: TypedKey> = TypedKey::new("fhe"); /// TODO: move these to each package with access on MyStruct::launcher() pub struct FheExtension { rng: SharedRng, - bus: Addr>, + bus: EventManager, } impl FheExtension { - pub fn create(bus: &Addr>, rng: &SharedRng) -> Box { + pub fn create(bus: &EventManager, rng: &SharedRng) -> Box { Box::new(Self { rng: rng.clone(), bus: bus.clone(), diff --git a/crates/keyshare/src/ext.rs b/crates/keyshare/src/ext.rs index 3ef1a2ffd0..71d47ac1e2 100644 --- a/crates/keyshare/src/ext.rs +++ b/crates/keyshare/src/ext.rs @@ -13,21 +13,23 @@ use anyhow::{anyhow, Result}; use async_trait::async_trait; use e3_crypto::Cipher; use e3_data::{AutoPersist, RepositoriesFactory}; -use e3_events::{BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus}; +use e3_events::{ + BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, EventManager, +}; use e3_fhe::ext::FHE_KEY; use e3_multithread::Multithread; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, META_KEY}; use std::sync::Arc; pub struct KeyshareExtension { - bus: Addr>, + bus: EventManager, address: String, cipher: Arc, } impl KeyshareExtension { pub fn create( - bus: &Addr>, + bus: &EventManager, address: &str, cipher: &Arc, ) -> Box { @@ -121,7 +123,7 @@ impl E3Extension for KeyshareExtension { } pub struct ThresholdKeyshareExtension { - bus: Addr>, + bus: EventManager, cipher: Arc, address: String, multithread: Addr, @@ -129,7 +131,7 @@ pub struct ThresholdKeyshareExtension { impl ThresholdKeyshareExtension { pub fn create( - bus: &Addr>, + bus: &EventManager, cipher: &Arc, multithread: &Addr, address: &str, diff --git a/crates/keyshare/src/keyshare.rs b/crates/keyshare/src/keyshare.rs index c1711159e4..70ab22b4ca 100644 --- a/crates/keyshare/src/keyshare.rs +++ b/crates/keyshare/src/keyshare.rs @@ -10,8 +10,8 @@ use e3_crypto::Cipher; use e3_data::Persistable; use e3_events::{ BusError, CiphernodeSelected, CiphertextOutputPublished, DecryptionshareCreated, Die, - E3RequestComplete, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, FromError, - KeyshareCreated, + E3RequestComplete, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, EventManager, + FromError, KeyshareCreated, }; use e3_fhe::{DecryptCiphertext, Fhe}; use e3_utils::utility_types::ArcBytes; @@ -20,7 +20,7 @@ use tracing::warn; pub struct Keyshare { fhe: Arc, - bus: Addr>, + bus: EventManager, secret: Persistable>, address: String, cipher: Arc, @@ -31,7 +31,7 @@ impl Actor for Keyshare { } pub struct KeyshareParams { - pub bus: Addr>, + pub bus: EventManager, pub secret: Persistable>, pub fhe: Arc, pub address: String, diff --git a/crates/keyshare/src/threshold_keyshare.rs b/crates/keyshare/src/threshold_keyshare.rs index c521c2bdeb..74721d2430 100644 --- a/crates/keyshare/src/threshold_keyshare.rs +++ b/crates/keyshare/src/threshold_keyshare.rs @@ -10,8 +10,8 @@ use e3_crypto::{Cipher, SensitiveBytes}; use e3_data::Persistable; use e3_events::{ CiphernodeSelected, CiphertextOutputPublished, ComputeRequest, ComputeResponse, - DecryptionshareCreated, E3id, EnclaveEvent, EnclaveEventData, EventBus, KeyshareCreated, - PartyId, ThresholdShare, ThresholdShareCreated, + DecryptionshareCreated, E3id, EnclaveEvent, EnclaveEventData, EventBus, EventManager, + KeyshareCreated, PartyId, ThresholdShare, ThresholdShareCreated, }; use e3_fhe::create_crp; use e3_multithread::Multithread; @@ -269,14 +269,14 @@ impl TryInto for ThresholdKeyshareState { } pub struct ThresholdKeyshareParams { - pub bus: Addr>, + pub bus: EventManager, pub cipher: Arc, pub multithread: Addr, pub state: Persistable, } pub struct ThresholdKeyshare { - bus: Addr>, + bus: EventManager, cipher: Arc, decryption_key_collector: Option>, multithread: Addr, diff --git a/crates/logger/src/logger.rs b/crates/logger/src/logger.rs index acfa7b4a82..ea222bbe4f 100644 --- a/crates/logger/src/logger.rs +++ b/crates/logger/src/logger.rs @@ -5,7 +5,7 @@ // or FITNESS FOR A PARTICULAR PURPOSE. use actix::{Actor, Addr, Context, Handler}; -use e3_events::{EnclaveEvent, EnclaveEventData, Event, EventBus, Subscribe}; +use e3_events::{prelude::Event, EnclaveEvent, EnclaveEventData, EventBus, Subscribe}; use std::marker::PhantomData; use tracing::{error, info}; diff --git a/crates/net/src/document_publisher.rs b/crates/net/src/document_publisher.rs index 015f411346..b12c6af27d 100644 --- a/crates/net/src/document_publisher.rs +++ b/crates/net/src/document_publisher.rs @@ -14,9 +14,9 @@ use actix::prelude::*; use anyhow::Result; use chrono::{DateTime, Utc}; use e3_events::{ - BusError, CiphernodeSelected, CorrelationId, DocumentKind, DocumentMeta, DocumentReceived, - E3RequestComplete, E3id, EnclaveErrorType, EnclaveEvent, EnclaveEventData, Event, EventBus, - PartyId, PublishDocumentRequested, Subscribe, ThresholdShareCreated, + prelude::*, CiphernodeSelected, CorrelationId, DocumentKind, DocumentMeta, DocumentReceived, + E3RequestComplete, E3id, EnclaveErrorType, EnclaveEvent, EnclaveEventData, Event, EventManager, + PartyId, PublishDocumentRequested, ThresholdShareCreated, }; use e3_utils::retry::{retry_with_backoff, to_retry}; use e3_utils::ArcBytes; @@ -40,7 +40,7 @@ const KADEMLIA_BROADCAST_TIMEOUT: Duration = Duration::from_secs(30); /// bus pub struct DocumentPublisher { /// Enclave EventBus - bus: Addr>, + bus: EventManager, /// NetCommand sender to forward commands to the NetInterface tx: mpsc::Sender, /// NetEvent receiver to resubscribe for events from the NetInterface. This is in an Arc so @@ -55,7 +55,7 @@ pub struct DocumentPublisher { impl DocumentPublisher { /// Create a new NetEventTranslator actor pub fn new( - bus: &Addr>, + bus: &EventManager, tx: &mpsc::Sender, rx: &Arc>, topic: impl Into, @@ -81,7 +81,7 @@ impl DocumentPublisher { /// Setup the DocumentPublisher and start listening for GossipEvents pub fn setup( - bus: &Addr>, + bus: &EventManager, tx: &mpsc::Sender, rx: &Arc>, topic: impl Into, @@ -90,7 +90,7 @@ impl DocumentPublisher { let addr = Self::new(bus, tx, rx, topic).start(); EventConverter::setup(bus); // Listen on all events - bus.do_send(Subscribe::new("*", addr.clone().recipient())); + bus.subscribe("*", addr.clone().recipient()); // Forward gossip data from NetEvent tokio::spawn({ @@ -253,7 +253,7 @@ pub async fn handle_publish_document_requested( pub async fn handle_document_published_notification( net_cmds: mpsc::Sender, net_events: Arc>, - bus: Addr>, + bus: EventManager, ids: HashMap, event: DocumentPublishedNotification, ) -> Result<()> { @@ -280,10 +280,10 @@ pub async fn handle_document_published_notification( .await?; debug!("Sending received event..."); - bus.do_send(EnclaveEvent::from(DocumentReceived { + bus.dispatch(DocumentReceived { meta: event.meta, value, - })); + }); Ok(()) } @@ -374,7 +374,7 @@ async fn broadcast_document_published_notification( /// Convert between ThresholdShareCreated and DocumentPublished events pub struct EventConverter { - bus: Addr>, + bus: EventManager, } #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -399,13 +399,13 @@ impl ReceivableDocument { } impl EventConverter { - pub fn new(bus: &Addr>) -> Self { + pub fn new(bus: &EventManager) -> Self { Self { bus: bus.clone() } } - pub fn setup(bus: &Addr>) -> Addr { + pub fn setup(bus: &EventManager) -> Addr { let addr = Self::new(bus).start(); - bus.do_send(Subscribe::new("ThresholdShareCreated", addr.clone().into())); - bus.do_send(Subscribe::new("DocumentReceived", addr.clone().into())); + bus.subscribe("ThresholdShareCreated", addr.clone().into()); + bus.subscribe("DocumentReceived", addr.clone().into()); addr } /// Local node created a threshold share. Send it as a published document @@ -423,24 +423,22 @@ impl EventConverter { None, ); self.bus - .do_send(EnclaveEvent::from(PublishDocumentRequested::new( - meta, value, - ))); + .dispatch(PublishDocumentRequested::new(meta, value)); Ok(()) } /// Received document externally pub fn handle_document_received(&self, msg: DocumentReceived) -> Result<()> { warn!("Converting DocumentReceived..."); let receivable = ReceivableDocument::from_bytes(&msg.value.extract_bytes())?; - let event = EnclaveEvent::from(match receivable { + let event = match receivable { ReceivableDocument::ThresholdShareCreated(evt) => ThresholdShareCreated { external: true, e3_id: evt.e3_id, share: evt.share, }, - }); + }; - self.bus.do_send(event); + self.bus.dispatch(event); Ok(()) } } @@ -490,7 +488,8 @@ mod tests { use anyhow::{bail, Result}; use e3_events::{ CiphernodeSelected, DocumentKind, DocumentMeta, E3id, EnclaveError, EnclaveEvent, EventBus, - EventBusConfig, GetEvents, HistoryCollector, PublishDocumentRequested, TakeEvents, + EventBusConfig, EventManager, GetEvents, HistoryCollector, PublishDocumentRequested, + TakeEvents, }; use libp2p::kad::{GetRecordError, PutRecordError, RecordKey}; use tokio::{ @@ -501,7 +500,7 @@ mod tests { fn setup_test() -> ( DefaultGuard, - Addr>, + EventManager, mpsc::Sender, mpsc::Receiver, broadcast::Sender, @@ -520,14 +519,14 @@ mod tests { let guard = tracing::subscriber::set_default(subscriber); let bus = EventBus::::new(EventBusConfig { deduplicate: true }).start(); + let bus = EventBus::manager(bus); let (net_cmd_tx, net_cmd_rx) = mpsc::channel(100); let (net_evt_tx, net_evt_rx) = broadcast::channel(100); let net_evt_rx = Arc::new(net_evt_rx); let history = HistoryCollector::::new().start(); let error = HistoryCollector::::new().start(); - bus.do_send(Subscribe::new("*", history.clone().recipient())); - bus.do_send(Subscribe::new("EnclaveError", error.clone().recipient())); - + bus.subscribe("*", history.clone().recipient()); + bus.subscribe("EnclaveError", error.clone().recipient()); let publisher = DocumentPublisher::setup(&bus, &net_cmd_tx, &net_evt_rx, "topic"); ( @@ -544,10 +543,10 @@ mod tests { let e3_id = E3id::new("1243", 1); // 1. Send a request to publish - bus.do_send(EnclaveEvent::from(PublishDocumentRequested { + bus.dispatch(PublishDocumentRequested { meta: DocumentMeta::new(e3_id, DocumentKind::TrBFV, vec![], expires_at), value: value.clone(), - })); + }); // 2. Document publisher should have asked the NetInterface to put the doc on Kademlia let Some(NetCommand::DhtPutRecord { @@ -618,12 +617,12 @@ mod tests { let cid = Cid::from_content(&value); // 1. Ensure the publisher is interested in the id by receiving CiphernodeSelected - bus.do_send(EnclaveEvent::from(CiphernodeSelected { + bus.dispatch(CiphernodeSelected { e3_id: e3_id.clone(), threshold_m: 3, threshold_n: 5, ..CiphernodeSelected::default() - })); + }); net_evt_tx.send(NetEvent::GossipData( GossipData::DocumentPublishedNotification(DocumentPublishedNotification { @@ -680,10 +679,10 @@ mod tests { let e3_id = E3id::new("1243", 1); // Send a request to publish - bus.do_send(EnclaveEvent::from(PublishDocumentRequested { + bus.dispatch(PublishDocumentRequested { meta: DocumentMeta::new(e3_id, DocumentKind::TrBFV, vec![], expires_at), value: value.clone(), - })); + }); for _ in 0..4 { // Expect retry @@ -730,12 +729,12 @@ mod tests { let cid = Cid::from_content(&value); // 1. Ensure the publisher is interested in the id by receiving CiphernodeSelected - bus.do_send(EnclaveEvent::from(CiphernodeSelected { + bus.do_send(CiphernodeSelected { e3_id: e3_id.clone(), threshold_m: 3, threshold_n: 5, ..CiphernodeSelected::default() - })); + }); // 2. Dispatch a NetEvent from the NetInterface signaling that a document was published net_evt_tx.send(NetEvent::GossipData( diff --git a/crates/net/src/net_event_translator.rs b/crates/net/src/net_event_translator.rs index b8822bce8d..c5034270a0 100644 --- a/crates/net/src/net_event_translator.rs +++ b/crates/net/src/net_event_translator.rs @@ -17,7 +17,8 @@ use e3_crypto::Cipher; use e3_data::Repository; use e3_events::EnclaveEventData; use e3_events::Event; -use e3_events::{CorrelationId, EnclaveEvent, EventBus, EventId, Subscribe}; +use e3_events::EventManager; +use e3_events::{CorrelationId, EnclaveEvent, EventId, Subscribe}; use libp2p::identity::ed25519; use std::collections::HashSet; use std::sync::Arc; @@ -32,7 +33,7 @@ use tracing::{error, info, instrument, trace}; /// NetEventTranslator Actor converts between EventBus events and Libp2p events forwarding them to a /// NetInterface for propagation over the p2p network pub struct NetEventTranslator { - bus: Addr>, + bus: EventManager, tx: mpsc::Sender, sent_events: HashSet, topic: String, @@ -50,7 +51,7 @@ struct LibP2pEvent(pub Vec); impl NetEventTranslator { /// Create a new NetEventTranslator actor pub fn new( - bus: Addr>, + bus: EventManager, tx: &mpsc::Sender, topic: &str, ) -> Self { @@ -63,7 +64,7 @@ impl NetEventTranslator { } pub fn setup( - bus: &Addr>, + bus: &EventManager, tx: &mpsc::Sender, rx: &Arc>, topic: &str, @@ -113,7 +114,7 @@ impl NetEventTranslator { /// Spawn a Libp2p interface and hook it up to this actor #[instrument(name = "libp2p", skip_all)] pub async fn setup_with_interface( - bus: Addr>, + bus: EventManager, peers: Vec, cipher: &Arc, quic_port: u16, diff --git a/crates/request/src/router.rs b/crates/request/src/router.rs index f0a7c4feae..4e869a9f4b 100644 --- a/crates/request/src/router.rs +++ b/crates/request/src/router.rs @@ -22,6 +22,7 @@ use e3_data::Repository; use e3_data::Snapshot; use e3_events::E3RequestComplete; use e3_events::EnclaveEventData; +use e3_events::EventManager; use e3_events::Shutdown; use e3_events::{E3id, EnclaveEvent, Event, EventBus, Subscribe}; use serde::Deserialize; @@ -103,19 +104,19 @@ pub struct E3Router { /// A buffer for events to send to the buffer: EventBuffer, /// The EventBus - bus: Addr>, + bus: EventManager, /// A repository for storing snapshots store: Repository, } pub struct E3RouterParams { extensions: Arc>>, - bus: Addr>, + bus: EventManager, store: Repository, } impl E3Router { - pub fn builder(bus: &Addr>, store: DataStore) -> E3RouterBuilder { + pub fn builder(bus: &EventManager, store: DataStore) -> E3RouterBuilder { let repositories = store.repositories(); let builder = E3RouterBuilder { bus: bus.clone(), @@ -280,7 +281,7 @@ impl FromSnapshotWithParams for E3Router { /// Builder for E3Router pub struct E3RouterBuilder { - pub bus: Addr>, + pub bus: EventManager, pub extensions: Vec>, pub store: Repository, } diff --git a/crates/sortition/src/ciphernode_selector.rs b/crates/sortition/src/ciphernode_selector.rs index 484c790988..1114dada58 100644 --- a/crates/sortition/src/ciphernode_selector.rs +++ b/crates/sortition/src/ciphernode_selector.rs @@ -12,13 +12,13 @@ use e3_config::StoreKeys; use e3_data::{DataStore, RepositoriesFactory}; use e3_events::{ CiphernodeSelected, CommitteeFinalized, E3Requested, EnclaveEvent, EnclaveEventData, EventBus, - Shutdown, Subscribe, TicketGenerated, TicketId, + EventManager, Shutdown, Subscribe, TicketGenerated, TicketId, }; use e3_request::MetaRepositoryFactory; use tracing::info; pub struct CiphernodeSelector { - bus: Addr>, + bus: EventManager, sortition: Addr, address: String, data_store: DataStore, @@ -30,7 +30,7 @@ impl Actor for CiphernodeSelector { impl CiphernodeSelector { pub fn new( - bus: &Addr>, + bus: &EventManager, sortition: &Addr, address: &str, data_store: &DataStore, @@ -44,7 +44,7 @@ impl CiphernodeSelector { } pub fn attach( - bus: &Addr>, + bus: &EventManager, sortition: &Addr, address: &str, data_store: &DataStore, diff --git a/crates/sortition/src/sortition.rs b/crates/sortition/src/sortition.rs index f8b323b622..60e266f203 100644 --- a/crates/sortition/src/sortition.rs +++ b/crates/sortition/src/sortition.rs @@ -9,12 +9,12 @@ use actix::prelude::*; use alloy::primitives::U256; use anyhow::Result; use e3_data::{AutoPersist, Persistable, Repository}; -use e3_events::EnclaveEventData; use e3_events::{ BusError, CiphernodeAdded, CiphernodeRemoved, CommitteeFinalized, CommitteePublished, ConfigurationUpdated, EnclaveErrorType, EnclaveEvent, EventBus, OperatorActivationChanged, PlaintextOutputPublished, Seed, Subscribe, TicketBalanceUpdated, }; +use e3_events::{EnclaveEventData, EventManager}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use tracing::info; @@ -139,7 +139,7 @@ pub struct Sortition { /// Persistent map of `chain_id -> NodeStateStore`. node_state: Persistable>, /// Event bus for error reporting and enclave event subscription. - bus: Addr>, + bus: EventManager, /// Persistent map of finalized committees per E3 finalized_committees: Persistable>>, } @@ -148,7 +148,7 @@ pub struct Sortition { #[derive(Debug)] pub struct SortitionParams { /// Event bus address. - pub bus: Addr>, + pub bus: EventManager, /// Persisted per-chain backend map. pub backends: Persistable>, /// Node state store per chain @@ -169,7 +169,7 @@ impl Sortition { #[instrument(name = "sortition_attach", skip_all)] pub async fn attach( - bus: &Addr>, + bus: &EventManager, backends_store: Repository>, node_state_store: Repository>, committees_store: Repository>>, diff --git a/crates/test-helpers/src/lib.rs b/crates/test-helpers/src/lib.rs index 51a40ac77d..1ecb0319f3 100644 --- a/crates/test-helpers/src/lib.rs +++ b/crates/test-helpers/src/lib.rs @@ -15,7 +15,8 @@ use alloy::primitives::Address; use anyhow::*; use e3_ciphernode_builder::CiphernodeHandle; use e3_events::{ - CiphernodeAdded, EnclaveEvent, EventBus, EventBusConfig, HistoryCollector, Seed, Subscribe, + CiphernodeAdded, EnclaveEvent, EventBus, EventBusConfig, EventManager, HistoryCollector, Seed, + Subscribe, }; use e3_fhe::{create_crp, setup_crp_params, ParamsWithCrp}; use e3_net::{DocumentPublisher, NetEventTranslator}; @@ -158,13 +159,13 @@ pub fn create_random_eth_addrs(how_many: u32) -> Vec { /// Test helper to add addresses to the committee by creating events on the event bus #[derive(Clone, Debug)] pub struct AddToCommittee { - bus: Addr>, + bus: EventManager, count: usize, chain_id: u64, } impl AddToCommittee { - pub fn new(bus: &Addr>, chain_id: u64) -> Self { + pub fn new(bus: &EventManager, chain_id: u64) -> Self { Self { bus: bus.clone(), chain_id, diff --git a/crates/test-helpers/src/plaintext_writer.rs b/crates/test-helpers/src/plaintext_writer.rs index 77e1a73168..ad07d839d4 100644 --- a/crates/test-helpers/src/plaintext_writer.rs +++ b/crates/test-helpers/src/plaintext_writer.rs @@ -8,7 +8,7 @@ use std::path::PathBuf; use super::write_file_with_dirs; use actix::{Actor, Addr, Context, Handler}; -use e3_events::{EnclaveEvent, EnclaveEventData, EventBus, Subscribe}; +use e3_events::{EnclaveEvent, EnclaveEventData, EventBus, EventManager, Subscribe}; use e3_sdk::bfv_helpers::decode_bytes_to_vec_u64; use tracing::{error, info}; @@ -17,7 +17,7 @@ pub struct PlaintextWriter { } impl PlaintextWriter { - pub fn attach(path: &PathBuf, bus: Addr>) -> Addr { + pub fn attach(path: &PathBuf, bus: EventManager) -> Addr { let addr = Self { path: path.to_owned(), } diff --git a/crates/test-helpers/src/public_key_writer.rs b/crates/test-helpers/src/public_key_writer.rs index 24aa0ec0bb..266bf46416 100644 --- a/crates/test-helpers/src/public_key_writer.rs +++ b/crates/test-helpers/src/public_key_writer.rs @@ -8,7 +8,7 @@ use std::path::PathBuf; use super::write_file_with_dirs; use actix::{Actor, Addr, Context, Handler}; -use e3_events::{EnclaveEvent, EnclaveEventData, EventBus, Subscribe}; +use e3_events::{EnclaveEvent, EnclaveEventData, EventBus, EventManager, Subscribe}; use tracing::info; pub struct PublicKeyWriter { @@ -16,7 +16,7 @@ pub struct PublicKeyWriter { } impl PublicKeyWriter { - pub fn attach(path: &PathBuf, bus: Addr>) -> Addr { + pub fn attach(path: &PathBuf, bus: EventManager) -> Addr { let addr = Self { path: path.to_owned(), } From 61c09a7d0ad588eb8c97e7293182cc470f004d67 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 03:35:43 +0000 Subject: [PATCH 12/36] major refactor of eventing --- crates/aggregator/src/committee_finalizer.rs | 15 ++-- crates/aggregator/src/ext.rs | 5 +- crates/aggregator/src/plaintext_aggregator.rs | 8 +-- crates/aggregator/src/publickey_aggregator.rs | 10 +-- .../src/threshold_plaintext_aggregator.rs | 8 +-- crates/ciphernode-builder/src/ciphernode.rs | 8 +-- .../src/ciphernode_builder.rs | 54 ++++++++------- crates/cli/src/start.rs | 2 +- crates/entrypoint/src/helpers/datastore.rs | 6 +- crates/entrypoint/src/helpers/shutdown.rs | 13 ++-- .../entrypoint/src/start/aggregator_start.rs | 7 +- crates/entrypoint/src/start/start.rs | 9 ++- crates/events/src/enclave_event/mod.rs | 16 +++-- crates/events/src/event_manager.rs | 17 ++++- crates/events/src/traits.rs | 2 + crates/evm/src/bonding_registry_sol.rs | 26 +++---- crates/evm/src/ciphernode_registry_sol.rs | 69 +++++++++---------- crates/evm/src/enclave_sol_reader.rs | 22 +++--- crates/evm/src/enclave_sol_writer.rs | 13 ++-- crates/evm/src/event_reader.rs | 20 +++--- .../evm/src/historical_event_coordinator.rs | 13 ++-- crates/fhe/src/ext.rs | 3 +- crates/keyshare/src/ext.rs | 4 +- crates/keyshare/src/keyshare.rs | 23 +++---- crates/keyshare/src/threshold_keyshare.rs | 20 +++--- crates/net/src/document_publisher.rs | 2 +- crates/net/src/net_event_translator.rs | 16 ++--- crates/request/src/router.rs | 14 ++-- crates/sortition/src/ciphernode_selector.rs | 21 +++--- crates/sortition/src/sortition.rs | 29 ++++---- crates/test-helpers/src/lib.rs | 14 ++-- crates/test-helpers/src/plaintext_writer.rs | 7 +- crates/test-helpers/src/public_key_writer.rs | 7 +- 33 files changed, 249 insertions(+), 254 deletions(-) diff --git a/crates/aggregator/src/committee_finalizer.rs b/crates/aggregator/src/committee_finalizer.rs index 922080aa45..e1ce6df93c 100644 --- a/crates/aggregator/src/committee_finalizer.rs +++ b/crates/aggregator/src/committee_finalizer.rs @@ -6,8 +6,8 @@ use actix::prelude::*; use e3_events::{ - CommitteeFinalizeRequested, CommitteeRequested, EnclaveEvent, EnclaveEventData, EventBus, - EventManager, Shutdown, Subscribe, + prelude::*, CommitteeFinalizeRequested, CommitteeRequested, EnclaveEvent, EnclaveEventData, + EventManager, Shutdown, }; use std::collections::HashMap; use std::time::Duration; @@ -31,11 +31,10 @@ impl CommitteeFinalizer { pub fn attach(bus: &EventManager) -> Addr { let addr = CommitteeFinalizer::new(bus).start(); - bus.do_send(Subscribe::new( - "CommitteeRequested", + bus.subscribe_all( + &["CommitteeRequested", "Shutdown"], addr.clone().recipient(), - )); - bus.do_send(Subscribe::new("Shutdown", addr.clone().recipient())); + ); addr } @@ -113,9 +112,9 @@ impl Handler for CommitteeFinalizer { move |act, _ctx| { info!(e3_id = %e3_id_clone, "Dispatching CommitteeFinalizeRequested event"); - bus.do_send(EnclaveEvent::from(CommitteeFinalizeRequested { + bus.dispatch(CommitteeFinalizeRequested { e3_id: e3_id_clone.clone(), - })); + }); act.pending_committees.remove(&e3_id_clone.to_string()); }, diff --git a/crates/aggregator/src/ext.rs b/crates/aggregator/src/ext.rs index 8a7d9715b7..6769ded134 100644 --- a/crates/aggregator/src/ext.rs +++ b/crates/aggregator/src/ext.rs @@ -15,9 +15,8 @@ use actix::{Actor, Addr}; use anyhow::{anyhow, Result}; use async_trait::async_trait; use e3_data::{AutoPersist, RepositoriesFactory}; -use e3_events::{ - BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, EventManager, -}; +use e3_events::prelude::*; +use e3_events::{EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventManager}; use e3_fhe::ext::FHE_KEY; use e3_multithread::Multithread; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, META_KEY}; diff --git a/crates/aggregator/src/plaintext_aggregator.rs b/crates/aggregator/src/plaintext_aggregator.rs index 9d5b81fcb2..6ab82c7b25 100644 --- a/crates/aggregator/src/plaintext_aggregator.rs +++ b/crates/aggregator/src/plaintext_aggregator.rs @@ -8,7 +8,7 @@ use actix::prelude::*; use anyhow::Result; use e3_data::Persistable; use e3_events::{ - DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, EventBus, EventManager, + prelude::*, DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, EventManager, OrderedSet, PlaintextAggregated, Seed, }; use e3_fhe::{Fhe, GetAggregatePlaintext}; @@ -234,12 +234,12 @@ impl Handler for PlaintextAggregator { self.set_decryption(decrypted_output.clone())?; // Dispatch the PlaintextAggregated event - let event = EnclaveEvent::from(PlaintextAggregated { + let event = PlaintextAggregated { decrypted_output: vec![ArcBytes::from_bytes(&decrypted_output)], e3_id: self.e3_id.clone(), - }); + }; - self.bus.do_send(event); + self.bus.dispatch(event); Ok(()) } diff --git a/crates/aggregator/src/publickey_aggregator.rs b/crates/aggregator/src/publickey_aggregator.rs index 8c06616080..6f787f3d40 100644 --- a/crates/aggregator/src/publickey_aggregator.rs +++ b/crates/aggregator/src/publickey_aggregator.rs @@ -8,8 +8,8 @@ use actix::prelude::*; use anyhow::Result; use e3_data::Persistable; use e3_events::{ - Die, E3id, EnclaveEvent, EnclaveEventData, EventBus, EventManager, KeyshareCreated, OrderedSet, - PublicKeyAggregated, Seed, + prelude::*, Die, E3id, EnclaveEvent, EnclaveEventData, EventManager, KeyshareCreated, + OrderedSet, PublicKeyAggregated, Seed, }; use e3_fhe::{Fhe, GetAggregatePublicKey}; use e3_sortition::{GetNodesForE3, Sortition}; @@ -224,12 +224,12 @@ impl Handler for PublicKeyAggregator { let pubkey = msg.pubkey.clone(); - let event = EnclaveEvent::from(PublicKeyAggregated { + let event = PublicKeyAggregated { pubkey, e3_id: msg.e3_id.clone(), nodes: OrderedSet::from(nodes), - }); - act.bus.do_send(event); + }; + act.bus.dispatch(event); Ok(()) }), ) diff --git a/crates/aggregator/src/threshold_plaintext_aggregator.rs b/crates/aggregator/src/threshold_plaintext_aggregator.rs index 713c60b3e7..4eda0ae4c3 100644 --- a/crates/aggregator/src/threshold_plaintext_aggregator.rs +++ b/crates/aggregator/src/threshold_plaintext_aggregator.rs @@ -10,7 +10,7 @@ use actix::prelude::*; use anyhow::{anyhow, bail, Result}; use e3_data::Persistable; use e3_events::{ - ComputeRequest, DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, EventBus, + prelude::*, ComputeRequest, DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, EventManager, PlaintextAggregated, Seed, }; use e3_multithread::Multithread; @@ -336,13 +336,13 @@ impl Handler for ThresholdPlaintextAggregator { act.set_decryption(plaintext.clone())?; // Dispatch the PlaintextAggregated event - let event = EnclaveEvent::from(PlaintextAggregated { + let event = PlaintextAggregated { decrypted_output: plaintext, // Extracting here for now e3_id: act.e3_id.clone(), - }); + }; info!("Dispatching plaintext event {:?}", event); - act.bus.do_send(event); + act.bus.dispatch(event); Ok(()) }), ) diff --git a/crates/ciphernode-builder/src/ciphernode.rs b/crates/ciphernode-builder/src/ciphernode.rs index f56e32b117..af44ee5e86 100644 --- a/crates/ciphernode-builder/src/ciphernode.rs +++ b/crates/ciphernode-builder/src/ciphernode.rs @@ -6,13 +6,13 @@ use actix::Addr; use e3_data::{DataStore, InMemStore, StoreAddr}; -use e3_events::{EnclaveEvent, EventBus, HistoryCollector}; +use e3_events::{EnclaveEvent, EventBus, EventManager, HistoryCollector}; #[derive(Clone, Debug)] pub struct CiphernodeHandle { pub address: String, pub store: DataStore, - pub bus: Addr>, + pub bus: EventManager, pub history: Option>>, pub errors: Option>>, } @@ -21,7 +21,7 @@ impl CiphernodeHandle { pub fn new( address: String, store: DataStore, - bus: Addr>, + bus: EventManager, history: Option>>, errors: Option>>, ) -> Self { @@ -35,7 +35,7 @@ impl CiphernodeHandle { } pub fn bus(&self) -> Addr> { - self.bus.clone() + self.bus.bus() } pub fn history(&self) -> Option>> { diff --git a/crates/ciphernode-builder/src/ciphernode_builder.rs b/crates/ciphernode-builder/src/ciphernode_builder.rs index 947dbe5134..4fb3874f4e 100644 --- a/crates/ciphernode-builder/src/ciphernode_builder.rs +++ b/crates/ciphernode-builder/src/ciphernode_builder.rs @@ -16,10 +16,7 @@ use e3_aggregator::ext::{ use e3_config::chain_config::ChainConfig; use e3_crypto::Cipher; use e3_data::{DataStore, InMemStore, Repositories, RepositoriesFactory}; -use e3_events::{ - EnclaveEvent, EnclaveEventDispatcher, EnclaveEventFactory, ErrorEvent, EventBus, - EventBusConfig, EventDispatcher, EventFactory, FullFactory, TestEvent, -}; +use e3_events::{prelude::*, EnclaveEvent, EventBus, EventBusConfig}; use e3_evm::{ helpers::{ load_signer_from_repository, ConcreteReadProvider, ConcreteWriteProvider, EthProvider, @@ -285,9 +282,6 @@ impl CiphernodeBuilder { None => Self::create_local_bus(), }; - // Setup an event dispatcher - let dispatcher = EventBus::manager(local_bus, Arc::new(EnclaveEventFactory::new())); - // History collector for taking historical events for analysis and testing let history = if self.testmode_history { info!("Setting up history collector"); @@ -304,6 +298,9 @@ impl CiphernodeBuilder { None }; + // Setup an event dispatcher + let dispatcher = EventBus::manager(local_bus); + let addr = if let Some(addr) = self.address.clone() { info!("Using eth address = {}", addr); addr @@ -324,7 +321,7 @@ impl CiphernodeBuilder { let default_backend = self.sortition_backend.clone(); let sortition = Sortition::attach( - &local_bus, + &dispatcher, repositories.sortition(), repositories.node_state(), repositories.finalized_committees(), @@ -332,12 +329,12 @@ impl CiphernodeBuilder { ) .await?; - CiphernodeSelector::attach(&local_bus, &sortition, &addr, &store); + CiphernodeSelector::attach(&dispatcher, &sortition, &addr, &store); let mut provider_cache = ProviderCaches::new(); let cipher = &self.cipher; - let coordinator = HistoricalEventCoordinator::setup(local_bus.clone()); + let coordinator = HistoricalEventCoordinator::setup(dispatcher.clone()); let processor = coordinator.clone().recipient(); // TODO: gather an async handle from the event readers that closes when they shutdown and @@ -354,7 +351,7 @@ impl CiphernodeBuilder { .await?; EnclaveSol::attach( &processor, - &local_bus, + &dispatcher, read_provider.clone(), write_provider.clone(), &chain.contracts.enclave.address(), @@ -369,7 +366,7 @@ impl CiphernodeBuilder { let read_provider = provider_cache.ensure_read_provider(chain).await?; EnclaveSolReader::attach( &processor, - &local_bus, + &dispatcher, read_provider.clone(), &chain.contracts.enclave.address(), &repositories.enclave_sol_reader(read_provider.chain_id()), @@ -383,7 +380,7 @@ impl CiphernodeBuilder { let read_provider = provider_cache.ensure_read_provider(chain).await?; BondingRegistrySol::attach( &processor, - &local_bus, + &dispatcher, read_provider.clone(), &chain.contracts.bonding_registry.address(), &repositories.bonding_registry_reader(read_provider.chain_id()), @@ -397,7 +394,7 @@ impl CiphernodeBuilder { let read_provider = provider_cache.ensure_read_provider(chain).await?; CiphernodeRegistrySol::attach( &processor, - &local_bus, + &dispatcher, read_provider.clone(), &chain.contracts.ciphernode_registry.address(), &repositories.ciphernode_registry_reader(read_provider.chain_id()), @@ -412,7 +409,7 @@ impl CiphernodeBuilder { { Ok(write_provider) => { let _writer = CiphernodeRegistrySol::attach_writer( - &local_bus, + &dispatcher, write_provider.clone(), &chain.contracts.ciphernode_registry.address(), self.pubkey_agg, @@ -422,7 +419,7 @@ impl CiphernodeBuilder { if self.pubkey_agg && matches!(self.sortition_backend, SortitionBackend::Score(_)) { info!("Attaching CommitteeFinalizer for score sortition"); - e3_aggregator::CommitteeFinalizer::attach(&local_bus); + e3_aggregator::CommitteeFinalizer::attach(&dispatcher); } } Err(e) => error!( @@ -437,13 +434,13 @@ impl CiphernodeBuilder { coordinator.do_send(CoordinatorStart); // E3 specific setup - let mut e3_builder = E3Router::builder(&local_bus, store.clone()); + let mut e3_builder = E3Router::builder(&dispatcher, store.clone()); if let Some(KeyshareKind::Threshold) = self.keyshare { let multithread = self.ensure_multithread(); info!("Setting up ThresholdKeyshareExtension"); e3_builder = e3_builder.with(ThresholdKeyshareExtension::create( - &local_bus, + &dispatcher, &self.cipher, &multithread, &addr, @@ -455,26 +452,30 @@ impl CiphernodeBuilder { || self.plaintext_agg { info!("Setting up FheExtension"); - e3_builder = e3_builder.with(FheExtension::create(&local_bus, &self.rng)) + e3_builder = e3_builder.with(FheExtension::create(&dispatcher, &self.rng)) } if self.pubkey_agg { info!("Setting up PublicKeyAggregationExtension"); - e3_builder = - e3_builder.with(PublicKeyAggregatorExtension::create(&local_bus, &sortition)) + e3_builder = e3_builder.with(PublicKeyAggregatorExtension::create( + &dispatcher, + &sortition, + )) } if self.plaintext_agg { info!("Setting up PlaintextAggregationExtension (legacy)"); - e3_builder = - e3_builder.with(PlaintextAggregatorExtension::create(&local_bus, &sortition)) + e3_builder = e3_builder.with(PlaintextAggregatorExtension::create( + &dispatcher, + &sortition, + )) } if self.threshold_plaintext_agg { info!("Setting up ThresholdPlaintextAggregatorExtension NEW!"); let multithread = self.ensure_multithread(); e3_builder = e3_builder.with(ThresholdPlaintextAggregatorExtension::create( - &local_bus, + &dispatcher, &sortition, &multithread, )) @@ -482,7 +483,8 @@ impl CiphernodeBuilder { if matches!(self.keyshare, Some(KeyshareKind::NonThreshold)) { info!("Setting up KeyshareExtension (legacy)!"); - e3_builder = e3_builder.with(KeyshareExtension::create(&local_bus, &addr, &self.cipher)) + e3_builder = + e3_builder.with(KeyshareExtension::create(&dispatcher, &addr, &self.cipher)) } info!("building..."); e3_builder.build().await?; @@ -490,7 +492,7 @@ impl CiphernodeBuilder { Ok(CiphernodeHandle::new( addr.to_owned(), store, - local_bus, + dispatcher, history, errors, )) diff --git a/crates/cli/src/start.rs b/crates/cli/src/start.rs index df3897e6f3..e74fbb324f 100644 --- a/crates/cli/src/start.rs +++ b/crates/cli/src/start.rs @@ -53,7 +53,7 @@ pub async fn execute( peer_id ); - tokio::spawn(listen_for_shutdown(bus.into(), handle)).await?; + tokio::spawn(listen_for_shutdown(bus, handle)).await?; Ok(()) } diff --git a/crates/entrypoint/src/helpers/datastore.rs b/crates/entrypoint/src/helpers/datastore.rs index 15c859771d..7c09694f90 100644 --- a/crates/entrypoint/src/helpers/datastore.rs +++ b/crates/entrypoint/src/helpers/datastore.rs @@ -6,12 +6,12 @@ use std::path::PathBuf; -use actix::{Actor, Addr}; +use actix::Actor; use anyhow::Result; use e3_config::AppConfig; use e3_data::{DataStore, InMemStore, SledDb, SledStore}; use e3_data::{Repositories, RepositoriesFactory}; -use e3_events::{get_enclave_event_bus, EnclaveEvent, EventBus, EventManager}; +use e3_events::{get_enclave_event_manager, EnclaveEvent, EventManager}; pub fn get_sled_store(bus: &EventManager, db_file: &PathBuf) -> Result { Ok((&SledStore::new(bus, db_file)?).into()) @@ -31,7 +31,7 @@ pub fn setup_datastore(config: &AppConfig, bus: &EventManager) -> } pub fn get_repositories(config: &AppConfig) -> Result { - let bus = get_enclave_event_bus(); + let bus = get_enclave_event_manager(); let store = setup_datastore(config, &bus)?; Ok(store.repositories()) } diff --git a/crates/entrypoint/src/helpers/shutdown.rs b/crates/entrypoint/src/helpers/shutdown.rs index 851381f6af..e75805ad32 100644 --- a/crates/entrypoint/src/helpers/shutdown.rs +++ b/crates/entrypoint/src/helpers/shutdown.rs @@ -4,9 +4,8 @@ // without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. -use actix::Recipient; use anyhow::Result; -use e3_events::{EnclaveEvent, Shutdown}; +use e3_events::{prelude::*, EnclaveEvent, EventManager, Shutdown}; use std::time::Duration; use tokio::{ select, @@ -15,7 +14,10 @@ use tokio::{ }; use tracing::{error, info}; -pub async fn listen_for_shutdown(bus: Recipient, mut handle: JoinHandle>) { +pub async fn listen_for_shutdown( + bus: EventManager, + mut handle: JoinHandle>, +) { let mut sigterm = signal(SignalKind::terminate()).expect("Failed to create SIGTERM signal stream"); select! { @@ -23,7 +25,10 @@ pub async fn listen_for_shutdown(bus: Recipient, mut handle: JoinH info!("SIGTERM received, initiating graceful shutdown..."); // Stop the actor system - let _ = bus.send(EnclaveEvent::from(Shutdown)).await; + bus.dispatch(Shutdown); + + // Wait for all events to propagate + tokio::time::sleep(Duration::from_secs(2)).await; // Abort the spawned task handle.abort(); diff --git a/crates/entrypoint/src/start/aggregator_start.rs b/crates/entrypoint/src/start/aggregator_start.rs index 4c683eda08..227ffbab48 100644 --- a/crates/entrypoint/src/start/aggregator_start.rs +++ b/crates/entrypoint/src/start/aggregator_start.rs @@ -4,13 +4,12 @@ // without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. -use actix::Addr; use anyhow::Result; use e3_ciphernode_builder::CiphernodeBuilder; use e3_config::AppConfig; use e3_crypto::Cipher; use e3_data::RepositoriesFactory; -use e3_events::{get_enclave_event_bus, EnclaveEvent, EventBus, EventManager}; +use e3_events::{get_enclave_event_manager, EnclaveEvent, EventManager}; use e3_net::{NetEventTranslator, NetRepositoryFactory}; use e3_test_helpers::{PlaintextWriter, PublicKeyWriter}; use rand::SeedableRng; @@ -29,14 +28,14 @@ pub async fn execute( plaintext_write_path: Option, experimental_trbfv: bool, ) -> Result<(EventManager, JoinHandle>, String)> { - let bus = get_enclave_event_bus(); + let bus = get_enclave_event_manager(); let rng = Arc::new(Mutex::new(ChaCha20Rng::from_rng(OsRng)?)); let store = setup_datastore(config, &bus)?; let repositories = store.repositories(); let cipher = Arc::new(Cipher::from_file(config.key_file()).await?); let mut builder = CiphernodeBuilder::new(rng.clone(), cipher.clone()) - .with_source_bus(&bus) + .with_source_bus(&bus.bus()) .with_datastore(store) .with_chains(&config.chains()) .with_sortition_score() diff --git a/crates/entrypoint/src/start/start.rs b/crates/entrypoint/src/start/start.rs index 0574e94bac..ac54ae7b98 100644 --- a/crates/entrypoint/src/start/start.rs +++ b/crates/entrypoint/src/start/start.rs @@ -4,15 +4,14 @@ // without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. -use actix::Addr; use alloy::primitives::Address; use anyhow::Result; use e3_ciphernode_builder::CiphernodeBuilder; use e3_config::AppConfig; use e3_crypto::Cipher; use e3_data::RepositoriesFactory; -use e3_events::{get_enclave_event_bus, EventManager}; -use e3_events::{EnclaveEvent, EventBus}; +use e3_events::{get_enclave_event_manager, EnclaveEvent}; +use e3_events::{prelude::*, EventManager}; use e3_net::{NetEventTranslator, NetRepositoryFactory}; use rand::SeedableRng; use rand_chacha::rand_core::OsRng; @@ -30,14 +29,14 @@ pub async fn execute( ) -> Result<(EventManager, JoinHandle>, String)> { let rng = Arc::new(Mutex::new(rand_chacha::ChaCha20Rng::from_rng(OsRng)?)); - let bus = get_enclave_event_bus(); + let bus = get_enclave_event_manager(); let cipher = Arc::new(Cipher::from_file(&config.key_file()).await?); let store = setup_datastore(&config, &bus)?; let repositories = store.repositories(); let mut builder = CiphernodeBuilder::new(rng.clone(), cipher.clone()) .with_address(&address.to_string()) - .with_source_bus(&bus) + .with_source_bus(&bus.bus()) .with_datastore(store) .with_sortition_score() .with_chains(&config.chains()) diff --git a/crates/events/src/enclave_event/mod.rs b/crates/events/src/enclave_event/mod.rs index 18175014d4..7d41fc14f9 100644 --- a/crates/events/src/enclave_event/mod.rs +++ b/crates/events/src/enclave_event/mod.rs @@ -76,9 +76,9 @@ use std::{ macro_rules! impl_into_event_data { ($($variant:ident),*) => { $( - impl Into for $variant { - fn into(self) -> EnclaveEventData { - EnclaveEventData::$variant(self) + impl From<$variant> for EnclaveEventData { + fn from(data:$variant) -> Self { + EnclaveEventData::$variant(data) } } )* @@ -116,6 +116,13 @@ pub enum EnclaveEventData { TestEvent(TestEvent), } +impl EnclaveEventData { + pub fn event_type(&self) -> String { + let name: &'static str = self.into(); + name.to_string() + } +} + #[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] #[rtype(result = "()")] pub struct EnclaveEvent { @@ -144,8 +151,7 @@ impl Event for EnclaveEvent { // type ErrType = EnclaveErrorType; fn event_type(&self) -> String { - let name: &'static str = (&self.payload).into(); - name.to_string() + self.payload.event_type() } fn event_id(&self) -> Self::Id { diff --git a/crates/events/src/event_manager.rs b/crates/events/src/event_manager.rs index 56cc9cd128..021423fc08 100644 --- a/crates/events/src/event_manager.rs +++ b/crates/events/src/event_manager.rs @@ -8,7 +8,7 @@ use crate::{ EventBus, Subscribe, }; -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct EventManager { bus: Addr>, } @@ -17,6 +17,10 @@ impl EventManager { pub fn new(bus: Addr>) -> Self { Self { bus } } + + pub fn bus(&self) -> Addr> { + self.bus.clone() + } } impl EventDispatcher for EventManager { @@ -29,6 +33,10 @@ impl EventDispatcher for EventManager { let evt = self.create_receive(data, ts); self.bus.do_send(evt) } + + fn naked_dispatch(&self, event: E) { + self.bus.do_send(event); + } } impl ErrorDispatcher for EventManager @@ -61,4 +69,11 @@ impl EventSubscriber for EventManager { fn subscribe(&self, event_type: &str, recipient: Recipient) { self.bus.do_send(Subscribe::new(event_type, recipient)) } + + fn subscribe_all(&self, event_types: &[&str], recipient: Recipient) { + for event_type in event_types.into_iter() { + self.bus + .do_send(Subscribe::new(*event_type, recipient.clone())); + } + } } diff --git a/crates/events/src/traits.rs b/crates/events/src/traits.rs index 7dd42535cd..df4ee31e7f 100644 --- a/crates/events/src/traits.rs +++ b/crates/events/src/traits.rs @@ -39,6 +39,7 @@ pub trait ErrorFactory { pub trait EventDispatcher { fn dispatch(&self, data: impl Into); fn dispatch_from_remote(&self, data: impl Into, ts: u128); + fn naked_dispatch(&self, event: E); } /// Trait for dispatching errors @@ -49,6 +50,7 @@ pub trait ErrorDispatcher { /// Trait to subscribe to events pub trait EventSubscriber { fn subscribe(&self, event_type: &str, recipient: Recipient); + fn subscribe_all(&self, event_types: &[&str], recipient: Recipient); } /// Trait to create an event with a timestamp from its associated type data diff --git a/crates/evm/src/bonding_registry_sol.rs b/crates/evm/src/bonding_registry_sol.rs index e559fd68a3..fe08a4af7d 100644 --- a/crates/evm/src/bonding_registry_sol.rs +++ b/crates/evm/src/bonding_registry_sol.rs @@ -16,7 +16,7 @@ use alloy::{ }; use anyhow::Result; use e3_data::Repository; -use e3_events::{EnclaveEvent, EventBus, EventManager}; +use e3_events::{EnclaveEvent, EnclaveEventData, EventManager}; use tracing::{error, info, trace}; sol!( @@ -40,10 +40,10 @@ impl From for e3_events::TicketBalanceUpdated { } } -impl From for EnclaveEvent { +impl From for EnclaveEventData { fn from(value: TicketBalanceUpdatedWithChainId) -> Self { let payload: e3_events::TicketBalanceUpdated = value.into(); - EnclaveEvent::from(payload) + Self::from(payload) } } @@ -85,28 +85,28 @@ impl From for e3_events::OperatorActivatio } } -impl From for EnclaveEvent { +impl From for EnclaveEventData { fn from(value: OperatorActivationChangedWithChainId) -> Self { let payload: e3_events::OperatorActivationChanged = value.into(); - EnclaveEvent::from(payload) + Self::from(payload) } } -impl From for EnclaveEvent { +impl From for EnclaveEventData { fn from(value: ConfigurationUpdatedWithChainId) -> Self { let payload: e3_events::ConfigurationUpdated = value.into(); - EnclaveEvent::from(payload) + Self::from(payload) } } -pub fn extractor(data: &LogData, topic: Option<&B256>, chain_id: u64) -> Option { +pub fn extractor(data: &LogData, topic: Option<&B256>, chain_id: u64) -> Option { match topic { Some(&IBondingRegistry::TicketBalanceUpdated::SIGNATURE_HASH) => { let Ok(event) = IBondingRegistry::TicketBalanceUpdated::decode_log_data(data) else { error!("Error parsing event TicketBalanceUpdated after topic was matched!"); return None; }; - Some(EnclaveEvent::from(TicketBalanceUpdatedWithChainId( + Some(EnclaveEventData::from(TicketBalanceUpdatedWithChainId( event, chain_id, ))) } @@ -116,16 +116,16 @@ pub fn extractor(data: &LogData, topic: Option<&B256>, chain_id: u64) -> Option< error!("Error parsing event OperatorActivationChanged after topic was matched!"); return None; }; - Some(EnclaveEvent::from(OperatorActivationChangedWithChainId( - event, chain_id, - ))) + Some(EnclaveEventData::from( + OperatorActivationChangedWithChainId(event, chain_id), + )) } Some(&IBondingRegistry::ConfigurationUpdated::SIGNATURE_HASH) => { let Ok(event) = IBondingRegistry::ConfigurationUpdated::decode_log_data(data) else { error!("Error parsing event ConfigurationUpdated after topic was matched!"); return None; }; - Some(EnclaveEvent::from(ConfigurationUpdatedWithChainId( + Some(EnclaveEventData::from(ConfigurationUpdatedWithChainId( event, chain_id, ))) } diff --git a/crates/evm/src/ciphernode_registry_sol.rs b/crates/evm/src/ciphernode_registry_sol.rs index 248df3b5ab..b311ac1f66 100644 --- a/crates/evm/src/ciphernode_registry_sol.rs +++ b/crates/evm/src/ciphernode_registry_sol.rs @@ -18,9 +18,9 @@ use alloy::{ use anyhow::Result; use e3_data::Repository; use e3_events::{ - BusError, CommitteeFinalizeRequested, CommitteeFinalized, E3id, EnclaveErrorType, EnclaveEvent, - EnclaveEventData, EventBus, EventManager, OrderedSet, PublicKeyAggregated, Seed, Shutdown, - Subscribe, TicketGenerated, TicketId, + prelude::*, CommitteeFinalizeRequested, CommitteeFinalized, E3id, EnclaveErrorType, + EnclaveEvent, EnclaveEventData, EventManager, EventSubscriber, OrderedSet, PublicKeyAggregated, + Seed, Shutdown, TicketGenerated, TicketId, }; use tracing::{error, info, trace}; @@ -53,10 +53,10 @@ impl From for e3_events::CiphernodeAdded { } } -impl From for EnclaveEvent { +impl From for EnclaveEventData { fn from(value: CiphernodeAddedWithChainId) -> Self { let payload: e3_events::CiphernodeAdded = value.into(); - EnclaveEvent::from(payload) + EnclaveEventData::from(payload) } } @@ -81,10 +81,10 @@ impl From for e3_events::CiphernodeRemoved { } } -impl From for EnclaveEvent { +impl From for EnclaveEventData { fn from(value: CiphernodeRemovedWithChainId) -> Self { let payload: e3_events::CiphernodeRemoved = value.into(); - EnclaveEvent::from(payload) + EnclaveEventData::from(payload) } } @@ -103,10 +103,10 @@ impl From for e3_events::CommitteeRequested { } } -impl From for EnclaveEvent { +impl From for EnclaveEventData { fn from(value: CommitteeRequestedWithChainId) -> Self { let payload: e3_events::CommitteeRequested = value.into(); - EnclaveEvent::from(payload) + EnclaveEventData::from(payload) } } @@ -127,10 +127,10 @@ impl From for CommitteeFinalized { } } -impl From for EnclaveEvent { +impl From for EnclaveEventData { fn from(value: CommitteeFinalizedWithChainId) -> Self { let payload: e3_events::CommitteeFinalized = value.into(); - EnclaveEvent::from(payload) + EnclaveEventData::from(payload) } } @@ -148,21 +148,21 @@ impl From for e3_events::TicketSubmitted { } } -impl From for EnclaveEvent { +impl From for EnclaveEventData { fn from(value: TicketSubmittedWithChainId) -> Self { let payload: e3_events::TicketSubmitted = value.into(); - EnclaveEvent::from(payload) + EnclaveEventData::from(payload) } } -pub fn extractor(data: &LogData, topic: Option<&B256>, chain_id: u64) -> Option { +pub fn extractor(data: &LogData, topic: Option<&B256>, chain_id: u64) -> Option { match topic { Some(&ICiphernodeRegistry::CiphernodeAdded::SIGNATURE_HASH) => { let Ok(event) = ICiphernodeRegistry::CiphernodeAdded::decode_log_data(data) else { error!("Error parsing event CiphernodeAdded after topic was matched!"); return None; }; - Some(EnclaveEvent::from(CiphernodeAddedWithChainId( + Some(EnclaveEventData::from(CiphernodeAddedWithChainId( event, chain_id, ))) } @@ -171,7 +171,7 @@ pub fn extractor(data: &LogData, topic: Option<&B256>, chain_id: u64) -> Option< error!("Error parsing event CiphernodeRemoved after topic was matched!"); return None; }; - Some(EnclaveEvent::from(CiphernodeRemovedWithChainId( + Some(EnclaveEventData::from(CiphernodeRemovedWithChainId( event, chain_id, ))) } @@ -180,7 +180,7 @@ pub fn extractor(data: &LogData, topic: Option<&B256>, chain_id: u64) -> Option< error!("Error parsing event CommitteeRequested after topic was matched!"); return None; }; - Some(EnclaveEvent::from(CommitteeRequestedWithChainId( + Some(EnclaveEventData::from(CommitteeRequestedWithChainId( event, chain_id, ))) } @@ -189,7 +189,7 @@ pub fn extractor(data: &LogData, topic: Option<&B256>, chain_id: u64) -> Option< error!("Error parsing event CommitteeFinalized after topic was matched!"); return None; }; - Some(EnclaveEvent::from(CommitteeFinalizedWithChainId( + Some(EnclaveEventData::from(CommitteeFinalizedWithChainId( event, chain_id, ))) } @@ -198,7 +198,7 @@ pub fn extractor(data: &LogData, topic: Option<&B256>, chain_id: u64) -> Option< error!("Error parsing event TicketSubmitted after topic was matched!"); return None; }; - Some(EnclaveEvent::from(TicketSubmittedWithChainId( + Some(EnclaveEventData::from(TicketSubmittedWithChainId( event, chain_id, ))) } @@ -277,26 +277,21 @@ impl CiphernodeRegistrySolWriter .start(); if is_aggregator { - let _ = bus - .send(Subscribe::new("PublicKeyAggregated", addr.clone().into())) - .await; - let _ = bus - .send(Subscribe::new( - "CommitteeFinalizeRequested", - addr.clone().into(), - )) - .await; + bus.subscribe_all( + &["PublicKeyAggregated", "CommitteeFinalizeRequested"], + addr.clone().into(), + ) } - // Subscribe to TicketGenerated for ticket submission - let _ = bus - .send(Subscribe::new("TicketGenerated", addr.clone().into())) - .await; - - // Stop gracefully on shutdown - let _ = bus - .send(Subscribe::new("Shutdown", addr.clone().into())) - .await; + bus.subscribe_all( + &[ + // Subscribe to TicketGenerated for ticket submission + "TicketGenerated", + // Stop gracefully on shutdown + "Shutdown", + ], + addr.clone().into(), + ); Ok(addr) } diff --git a/crates/evm/src/enclave_sol_reader.rs b/crates/evm/src/enclave_sol_reader.rs index f39847b3d6..d5a57a385d 100644 --- a/crates/evm/src/enclave_sol_reader.rs +++ b/crates/evm/src/enclave_sol_reader.rs @@ -13,7 +13,7 @@ use alloy::providers::Provider; use alloy::{sol, sol_types::SolEvent}; use anyhow::Result; use e3_data::Repository; -use e3_events::{E3id, EnclaveEvent, EventBus, EventManager}; +use e3_events::{prelude::*, E3id, EnclaveEvent, EnclaveEventData, EventManager}; use e3_utils::utility_types::ArcBytes; use num_bigint::BigUint; use tracing::{error, info, trace}; @@ -44,10 +44,10 @@ impl From for e3_events::E3Requested { } } -impl From for EnclaveEvent { +impl From for EnclaveEventData { fn from(value: E3RequestedWithChainId) -> Self { let payload: e3_events::E3Requested = value.into(); - EnclaveEvent::from(payload) + payload.into() } } @@ -64,30 +64,32 @@ impl From for e3_events::CiphertextOutputP } } -impl From for EnclaveEvent { +impl From for EnclaveEventData { fn from(value: CiphertextOutputPublishedWithChainId) -> Self { let payload: e3_events::CiphertextOutputPublished = value.into(); - EnclaveEvent::from(payload) + payload.into() } } -pub fn extractor(data: &LogData, topic: Option<&B256>, chain_id: u64) -> Option { +pub fn extractor(data: &LogData, topic: Option<&B256>, chain_id: u64) -> Option { match topic { Some(&IEnclave::E3Requested::SIGNATURE_HASH) => { let Ok(event) = IEnclave::E3Requested::decode_log_data(data) else { error!("Error parsing event E3Requested after topic matched!"); return None; }; - Some(EnclaveEvent::from(E3RequestedWithChainId(event, chain_id))) + Some(EnclaveEventData::from(E3RequestedWithChainId( + event, chain_id, + ))) } Some(&IEnclave::CiphertextOutputPublished::SIGNATURE_HASH) => { let Ok(event) = IEnclave::CiphertextOutputPublished::decode_log_data(data) else { error!("Error parsing event CiphertextOutputPublished after topic matched!"); return None; }; - Some(EnclaveEvent::from(CiphertextOutputPublishedWithChainId( - event, chain_id, - ))) + Some(EnclaveEventData::from( + CiphertextOutputPublishedWithChainId(event, chain_id), + )) } _topic => { trace!( diff --git a/crates/evm/src/enclave_sol_writer.rs b/crates/evm/src/enclave_sol_writer.rs index 6c97fe79c9..c8f0661668 100644 --- a/crates/evm/src/enclave_sol_writer.rs +++ b/crates/evm/src/enclave_sol_writer.rs @@ -17,11 +17,12 @@ use alloy::{ rpc::types::TransactionReceipt, }; use anyhow::Result; +use e3_events::prelude::*; +use e3_events::EnclaveEvent; use e3_events::EnclaveEventData; use e3_events::EventManager; use e3_events::Shutdown; -use e3_events::{BusError, E3id, EnclaveErrorType, PlaintextAggregated, Subscribe}; -use e3_events::{EnclaveEvent, EventBus}; +use e3_events::{E3id, EnclaveErrorType, PlaintextAggregated, Subscribe}; use tracing::info; sol!( @@ -56,13 +57,7 @@ impl EnclaveSolWriter

{ contract_address: &str, ) -> Result>> { let addr = EnclaveSolWriter::new(bus, provider, contract_address.parse()?)?.start(); - - bus.send(Subscribe::new("PlaintextAggregated", addr.clone().into())) - .await?; - - bus.send(Subscribe::new("Shutdown", addr.clone().into())) - .await?; - + bus.subscribe_all(&["PlaintextAggregated", "Shutdown"], addr.clone().into()); Ok(addr) } } diff --git a/crates/evm/src/event_reader.rs b/crates/evm/src/event_reader.rs index 7eedf9588d..af29791db3 100644 --- a/crates/evm/src/event_reader.rs +++ b/crates/evm/src/event_reader.rs @@ -14,9 +14,7 @@ use alloy::providers::Provider; use alloy::rpc::types::Filter; use anyhow::{anyhow, Result}; use e3_data::{AutoPersist, Persistable, Repository}; -use e3_events::{ - BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, EventId, Subscribe, -}; +use e3_events::{prelude::*, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventId}; use e3_events::{Event, EventManager}; use futures_util::stream::StreamExt; use std::collections::HashSet; @@ -33,13 +31,13 @@ pub enum EnclaveEvmEvent { HistoricalSyncComplete, /// An actual event from the blockchain Event { - event: EnclaveEvent, + event: EnclaveEventData, block: Option, }, } impl EnclaveEvmEvent { - pub fn new(event: EnclaveEvent, block: Option) -> Self { + pub fn new(event: EnclaveEventData, block: Option) -> Self { Self::Event { event, block } } @@ -52,7 +50,7 @@ pub type ExtractorFn = fn(&LogData, Option<&B256>, u64) -> Option; pub struct EvmEventReaderParams

{ provider: EthProvider

, - extractor: ExtractorFn, + extractor: ExtractorFn, contract_address: Address, start_block: Option, processor: Recipient, @@ -74,7 +72,7 @@ pub struct EvmEventReader

{ /// The contract address contract_address: Address, /// The Extractor function to determine which events to extract and convert to EnclaveEvents - extractor: ExtractorFn, + extractor: ExtractorFn, /// A shutdown receiver to listen to for shutdown signals sent to the loop this is only used /// internally. You should send the Shutdown signal to the reader directly or via the EventBus shutdown_rx: Option>, @@ -111,7 +109,7 @@ impl EvmEventReader

{ pub async fn attach( provider: EthProvider

, - extractor: ExtractorFn, + extractor: ExtractorFn, contract_address: &str, start_block: Option, processor: &Recipient, @@ -139,7 +137,7 @@ impl EvmEventReader

{ processor.do_send(EnclaveEvmEvent::RegisterReader); - bus.do_send(Subscribe::new("Shutdown", addr.clone().into())); + bus.subscribe("Shutdown", addr.clone().into()); Ok(addr) } } @@ -190,7 +188,7 @@ async fn stream_from_evm( provider: EthProvider

, contract_address: &Address, reader_addr: Addr>, - extractor: fn(&LogData, Option<&B256>, u64) -> Option, + extractor: fn(&LogData, Option<&B256>, u64) -> Option, mut shutdown: oneshot::Receiver<()>, start_block: Option, bus: &EventManager, @@ -261,7 +259,7 @@ async fn stream_from_evm( continue; }; - trace!("Extracted EVM Event: {}", event); + trace!("Extracted EVM Event: {:?}", event); reader_addr.do_send(EnclaveEvmEvent::new(event, block_number)); } None => break, // Stream ended diff --git a/crates/evm/src/historical_event_coordinator.rs b/crates/evm/src/historical_event_coordinator.rs index 26a9eace90..0b79882770 100644 --- a/crates/evm/src/historical_event_coordinator.rs +++ b/crates/evm/src/historical_event_coordinator.rs @@ -6,13 +6,13 @@ use crate::EnclaveEvmEvent; use actix::prelude::*; -use e3_events::{EnclaveEvent, EventBus, EventManager}; +use e3_events::{prelude::*, EnclaveEvent, EnclaveEventData, EventManager}; use tracing::info; #[derive(Clone)] struct BufferedEvent { block: u64, - event: EnclaveEvent, + event: EnclaveEventData, } /// Message to start forwarding buffered events after all readers have registered @@ -60,7 +60,7 @@ impl HistoricalEventCoordinator { let count = self.buffered_events.len(); for BufferedEvent { event, .. } in self.buffered_events.drain(..) { - self.target.do_send(event); + self.target.dispatch(event); } info!( @@ -108,13 +108,10 @@ impl Handler for HistoricalEventCoordinator { EnclaveEvmEvent::Event { event, block } => { if !self.started || !self.all_readers_complete() { if let Some(block) = block { - self.buffered_events.push(BufferedEvent { - block, - event: event.clone(), - }); + self.buffered_events.push(BufferedEvent { block, event }); } } else { - self.target.do_send(event); + self.target.dispatch(event); } } } diff --git a/crates/fhe/src/ext.rs b/crates/fhe/src/ext.rs index cb9a741bce..668741c622 100644 --- a/crates/fhe/src/ext.rs +++ b/crates/fhe/src/ext.rs @@ -5,12 +5,11 @@ // or FITNESS FOR A PARTICULAR PURPOSE. use crate::{Fhe, FheRepositoryFactory}; -use actix::Addr; use anyhow::{anyhow, Result}; use async_trait::async_trait; use e3_data::{FromSnapshotWithParams, RepositoriesFactory, Snapshot}; use e3_events::{ - BusError, E3Requested, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, EventManager, + prelude::*, E3Requested, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventManager, }; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, TypedKey}; use e3_utils::SharedRng; diff --git a/crates/keyshare/src/ext.rs b/crates/keyshare/src/ext.rs index 71d47ac1e2..625997fcaf 100644 --- a/crates/keyshare/src/ext.rs +++ b/crates/keyshare/src/ext.rs @@ -13,9 +13,7 @@ use anyhow::{anyhow, Result}; use async_trait::async_trait; use e3_crypto::Cipher; use e3_data::{AutoPersist, RepositoriesFactory}; -use e3_events::{ - BusError, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, EventManager, -}; +use e3_events::{prelude::*, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventManager}; use e3_fhe::ext::FHE_KEY; use e3_multithread::Multithread; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, META_KEY}; diff --git a/crates/keyshare/src/keyshare.rs b/crates/keyshare/src/keyshare.rs index 70ab22b4ca..d5f632369a 100644 --- a/crates/keyshare/src/keyshare.rs +++ b/crates/keyshare/src/keyshare.rs @@ -9,9 +9,9 @@ use anyhow::{anyhow, Result}; use e3_crypto::Cipher; use e3_data::Persistable; use e3_events::{ - BusError, CiphernodeSelected, CiphertextOutputPublished, DecryptionshareCreated, Die, - E3RequestComplete, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventBus, EventManager, - FromError, KeyshareCreated, + prelude::*, CiphernodeSelected, CiphertextOutputPublished, DecryptionshareCreated, Die, + E3RequestComplete, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventManager, FromError, + KeyshareCreated, }; use e3_fhe::{DecryptCiphertext, Fhe}; use e3_utils::utility_types::ArcBytes; @@ -95,27 +95,24 @@ impl Handler for Keyshare { // generate keyshare let Ok((secret, pubkey)) = self.fhe.generate_keyshare() else { - self.bus.do_send(EnclaveEvent::from_error( + self.bus.err( EnclaveErrorType::KeyGeneration, anyhow!("Error creating Keyshare for {e3_id}"), - )); + ); return; }; // Save secret on state if let Err(err) = self.set_secret(secret) { - self.bus.do_send(EnclaveEvent::from_error( - EnclaveErrorType::KeyGeneration, - err, - )) + self.bus.err(EnclaveErrorType::KeyGeneration, err) }; // Broadcast the KeyshareCreated message - self.bus.do_send(EnclaveEvent::from(KeyshareCreated { + self.bus.dispatch(KeyshareCreated { pubkey, e3_id, node: self.address.clone(), - })); + }); } } @@ -159,12 +156,12 @@ impl Handler for Keyshare { return; }; - self.bus.do_send(EnclaveEvent::from(DecryptionshareCreated { + self.bus.dispatch(DecryptionshareCreated { party_id: 0, // Not used e3_id, decryption_share: vec![ArcBytes::from_bytes(&decryption_share)], node: self.address.clone(), - })); + }); } } diff --git a/crates/keyshare/src/threshold_keyshare.rs b/crates/keyshare/src/threshold_keyshare.rs index 74721d2430..f0322711ce 100644 --- a/crates/keyshare/src/threshold_keyshare.rs +++ b/crates/keyshare/src/threshold_keyshare.rs @@ -9,9 +9,9 @@ use anyhow::{anyhow, bail, Result}; use e3_crypto::{Cipher, SensitiveBytes}; use e3_data::Persistable; use e3_events::{ - CiphernodeSelected, CiphertextOutputPublished, ComputeRequest, ComputeResponse, - DecryptionshareCreated, E3id, EnclaveEvent, EnclaveEventData, EventBus, EventManager, - KeyshareCreated, PartyId, ThresholdShare, ThresholdShareCreated, + prelude::*, CiphernodeSelected, CiphertextOutputPublished, ComputeRequest, ComputeResponse, + DecryptionshareCreated, E3id, EnclaveEvent, EnclaveEventData, EventManager, KeyshareCreated, + PartyId, ThresholdShare, ThresholdShareCreated, }; use e3_fhe::create_crp; use e3_multithread::Multithread; @@ -526,7 +526,7 @@ impl ThresholdKeyshare { .map(|s| PvwEncrypted::new(s.decrypt(&self.cipher)?)) .collect::>()?; - self.bus.do_send(EnclaveEvent::from(ThresholdShareCreated { + self.bus.dispatch(ThresholdShareCreated { e3_id, share: Arc::new(ThresholdShare { party_id, @@ -535,7 +535,7 @@ impl ThresholdKeyshare { sk_sss, }), external: false, - })); + }); Ok(()) } @@ -630,11 +630,11 @@ impl ThresholdKeyshare { let address = state.get_address().to_owned(); let current: ReadyForDecryption = state.clone().try_into()?; - self.bus.do_send(EnclaveEvent::from(KeyshareCreated { + self.bus.dispatch(KeyshareCreated { pubkey: current.pk_share, e3_id, node: address, - })); + }); Ok(()) } @@ -689,15 +689,15 @@ impl ThresholdKeyshare { let e3_id = state.e3_id; let decryption_share = msg.d_share_poly; - let event = EnclaveEvent::from(DecryptionshareCreated { + let event = DecryptionshareCreated { party_id, node, e3_id, decryption_share, - }); + }; // send the decryption share - self.bus.do_send(event); + self.bus.dispatch(event); // mark as complete self.state.try_mutate(|s| { diff --git a/crates/net/src/document_publisher.rs b/crates/net/src/document_publisher.rs index b12c6af27d..cd0aafe7db 100644 --- a/crates/net/src/document_publisher.rs +++ b/crates/net/src/document_publisher.rs @@ -729,7 +729,7 @@ mod tests { let cid = Cid::from_content(&value); // 1. Ensure the publisher is interested in the id by receiving CiphernodeSelected - bus.do_send(CiphernodeSelected { + bus.dispatch(CiphernodeSelected { e3_id: e3_id.clone(), threshold_m: 3, threshold_n: 5, diff --git a/crates/net/src/net_event_translator.rs b/crates/net/src/net_event_translator.rs index c5034270a0..9b30fef846 100644 --- a/crates/net/src/net_event_translator.rs +++ b/crates/net/src/net_event_translator.rs @@ -15,10 +15,11 @@ use actix::prelude::*; use anyhow::{bail, Result}; use e3_crypto::Cipher; use e3_data::Repository; +use e3_events::prelude::*; use e3_events::EnclaveEventData; use e3_events::Event; use e3_events::EventManager; -use e3_events::{CorrelationId, EnclaveEvent, EventId, Subscribe}; +use e3_events::{CorrelationId, EnclaveEvent, EventId}; use libp2p::identity::ed25519; use std::collections::HashSet; use std::sync::Arc; @@ -51,12 +52,12 @@ struct LibP2pEvent(pub Vec); impl NetEventTranslator { /// Create a new NetEventTranslator actor pub fn new( - bus: EventManager, + bus: &EventManager, tx: &mpsc::Sender, topic: &str, ) -> Self { Self { - bus, + bus: bus.clone(), tx: tx.clone(), sent_events: HashSet::new(), topic: topic.to_string(), @@ -70,13 +71,10 @@ impl NetEventTranslator { topic: &str, ) -> Addr { let mut rx = rx.resubscribe(); - let addr = NetEventTranslator::new(bus.clone(), tx, topic).start(); + let addr = NetEventTranslator::new(&bus, tx, topic).start(); // Listen on all events - bus.do_send(Subscribe { - event_type: String::from("*"), - listener: addr.clone().recipient(), - }); + bus.subscribe("*", addr.clone().recipient()); tokio::spawn({ let addr = addr.clone(); @@ -168,7 +166,7 @@ impl Handler for NetEventTranslator { let LibP2pEvent(bytes) = msg; match EnclaveEvent::from_bytes(&bytes) { Ok(event) => { - self.bus.do_send(event.clone()); + self.bus.naked_dispatch(event.clone()); // TODO: convert to receive self.sent_events.insert(event.into()); } Err(err) => error!(error=?err, "Could not create EnclaveEvent from Libp2p Bytes!"), diff --git a/crates/request/src/router.rs b/crates/request/src/router.rs index 4e869a9f4b..b8f3e49d13 100644 --- a/crates/request/src/router.rs +++ b/crates/request/src/router.rs @@ -20,11 +20,12 @@ use e3_data::FromSnapshotWithParams; use e3_data::RepositoriesFactory; use e3_data::Repository; use e3_data::Snapshot; +use e3_events::prelude::*; use e3_events::E3RequestComplete; use e3_events::EnclaveEventData; use e3_events::EventManager; use e3_events::Shutdown; -use e3_events::{E3id, EnclaveEvent, Event, EventBus, Subscribe}; +use e3_events::{E3id, EnclaveEvent, Event}; use serde::Deserialize; use serde::Serialize; use std::collections::HashSet; @@ -186,12 +187,12 @@ impl Handler for E3Router { // Here we are detemining that by receiving the PlaintextAggregated event our request is // complete and we can notify everyone. This might change as we consider other factors // when determining if the request is complete - let event = EnclaveEvent::from(E3RequestComplete { + let event = E3RequestComplete { e3_id: e3_id.clone(), - }); + }; // Send to bus so all other actors can react to a request being complete. - self.bus.do_send(event); + self.bus.dispatch(event); } EnclaveEventData::E3RequestComplete(_) => { // Note this will be sent above to the children who can kill themselves based on @@ -209,7 +210,7 @@ impl Handler for E3Router { impl Handler for E3Router { type Result = (); fn handle(&mut self, msg: Shutdown, _ctx: &mut Self::Context) -> Self::Result { - let shutdown_evt = EnclaveEvent::from(msg); + let shutdown_evt = self.bus.create_local(msg); for (_, ctx) in self.contexts.iter() { ctx.forward_message_now(&shutdown_evt) } @@ -306,8 +307,7 @@ impl E3RouterBuilder { }; let addr = e3r.start(); - self.bus - .do_send(Subscribe::new("*", addr.clone().recipient())); + self.bus.subscribe("*", addr.clone().recipient()); Ok(addr) } } diff --git a/crates/sortition/src/ciphernode_selector.rs b/crates/sortition/src/ciphernode_selector.rs index 1114dada58..c5292d2b32 100644 --- a/crates/sortition/src/ciphernode_selector.rs +++ b/crates/sortition/src/ciphernode_selector.rs @@ -11,8 +11,8 @@ use actix::prelude::*; use e3_config::StoreKeys; use e3_data::{DataStore, RepositoriesFactory}; use e3_events::{ - CiphernodeSelected, CommitteeFinalized, E3Requested, EnclaveEvent, EnclaveEventData, EventBus, - EventManager, Shutdown, Subscribe, TicketGenerated, TicketId, + prelude::*, CiphernodeSelected, CommitteeFinalized, E3Requested, EnclaveEvent, + EnclaveEventData, EventManager, Shutdown, TicketGenerated, TicketId, }; use e3_request::MetaRepositoryFactory; use tracing::info; @@ -51,12 +51,9 @@ impl CiphernodeSelector { ) -> Addr { let addr = CiphernodeSelector::new(bus, sortition, address, data_store).start(); - bus.do_send(Subscribe::new("E3Requested", addr.clone().recipient())); - bus.do_send(Subscribe::new( - "CommitteeFinalized", - addr.clone().recipient(), - )); - bus.do_send(Subscribe::new("Shutdown", addr.clone().recipient())); + bus.subscribe("E3Requested", addr.clone().recipient()); + bus.subscribe("CommitteeFinalized", addr.clone().recipient()); + bus.subscribe("Shutdown", addr.clone().recipient()); addr } @@ -112,11 +109,11 @@ impl Handler for CiphernodeSelector { ticket_id = tid, "Ticket generated for score sortition" ); - bus.do_send(EnclaveEvent::from(TicketGenerated { + bus.dispatch(TicketGenerated { e3_id: data.e3_id.clone(), ticket_id: TicketId::Score(tid), node: address.clone(), - })); + }); } } else { info!("This node is not selected"); @@ -168,7 +165,7 @@ impl Handler for CiphernodeSelector { party_id = party_id, "Node is in finalized committee, emitting CiphernodeSelected" ); - bus.do_send(EnclaveEvent::from(CiphernodeSelected { + bus.dispatch(CiphernodeSelected { party_id: party_id as u64, e3_id, threshold_m: e3_meta.threshold_m, @@ -177,7 +174,7 @@ impl Handler for CiphernodeSelector { error_size: e3_meta.error_size, params: e3_meta.params, seed: e3_meta.seed, - })); + }); }) } } diff --git a/crates/sortition/src/sortition.rs b/crates/sortition/src/sortition.rs index 60e266f203..9a225edc0d 100644 --- a/crates/sortition/src/sortition.rs +++ b/crates/sortition/src/sortition.rs @@ -10,8 +10,8 @@ use alloy::primitives::U256; use anyhow::Result; use e3_data::{AutoPersist, Persistable, Repository}; use e3_events::{ - BusError, CiphernodeAdded, CiphernodeRemoved, CommitteeFinalized, CommitteePublished, - ConfigurationUpdated, EnclaveErrorType, EnclaveEvent, EventBus, OperatorActivationChanged, + prelude::*, CiphernodeAdded, CiphernodeRemoved, CommitteeFinalized, CommitteePublished, + ConfigurationUpdated, EnclaveErrorType, EnclaveEvent, OperatorActivationChanged, PlaintextOutputPublished, Seed, Subscribe, TicketBalanceUpdated, }; use e3_events::{EnclaveEventData, EventManager}; @@ -193,20 +193,19 @@ impl Sortition { .start(); // Subscribe to all relevant events - bus.do_send(Subscribe::new("CiphernodeAdded", addr.clone().into())); - bus.do_send(Subscribe::new("CiphernodeRemoved", addr.clone().into())); - bus.do_send(Subscribe::new("TicketBalanceUpdated", addr.clone().into())); - bus.do_send(Subscribe::new( - "OperatorActivationChanged", + bus.subscribe_all( + &[ + "CiphernodeAdded", + "CiphernodeRemoved", + "TicketBalanceUpdated", + "OperatorActivationChanged", + "ConfigurationUpdated", + "CommitteePublished", + "PlaintextOutputPublished", + "CommitteeFinalized", + ], addr.clone().into(), - )); - bus.do_send(Subscribe::new("ConfigurationUpdated", addr.clone().into())); - bus.do_send(Subscribe::new("CommitteePublished", addr.clone().into())); - bus.do_send(Subscribe::new( - "PlaintextOutputPublished", - addr.clone().into(), - )); - bus.do_send(Subscribe::new("CommitteeFinalized", addr.clone().into())); + ); info!("Sortition actor started"); Ok(addr) diff --git a/crates/test-helpers/src/lib.rs b/crates/test-helpers/src/lib.rs index 1ecb0319f3..f0ac875c7e 100644 --- a/crates/test-helpers/src/lib.rs +++ b/crates/test-helpers/src/lib.rs @@ -15,8 +15,8 @@ use alloy::primitives::Address; use anyhow::*; use e3_ciphernode_builder::CiphernodeHandle; use e3_events::{ - CiphernodeAdded, EnclaveEvent, EventBus, EventBusConfig, EventManager, HistoryCollector, Seed, - Subscribe, + CiphernodeAdded, EnclaveEvent, EnclaveEventData, EventBus, EventBusConfig, EventDispatcher, + EventManager, HistoryCollector, Seed, Subscribe, }; use e3_fhe::{create_crp, setup_crp_params, ParamsWithCrp}; use e3_net::{DocumentPublisher, NetEventTranslator}; @@ -172,19 +172,19 @@ impl AddToCommittee { count: 0, } } - pub async fn add(&mut self, address: &str) -> Result { - let evt = EnclaveEvent::from(CiphernodeAdded { + pub async fn add(&mut self, address: &str) -> Result { + let evt = CiphernodeAdded { chain_id: self.chain_id, address: address.to_owned(), index: self.count, num_nodes: self.count + 1, - }); + }; self.count += 1; - self.bus.send(evt.clone()).await?; + self.bus.dispatch(evt.clone()); - Ok(evt) + Ok(evt.into()) } } diff --git a/crates/test-helpers/src/plaintext_writer.rs b/crates/test-helpers/src/plaintext_writer.rs index ad07d839d4..49acbe9bb9 100644 --- a/crates/test-helpers/src/plaintext_writer.rs +++ b/crates/test-helpers/src/plaintext_writer.rs @@ -8,7 +8,7 @@ use std::path::PathBuf; use super::write_file_with_dirs; use actix::{Actor, Addr, Context, Handler}; -use e3_events::{EnclaveEvent, EnclaveEventData, EventBus, EventManager, Subscribe}; +use e3_events::{prelude::*, EnclaveEvent, EnclaveEventData, EventManager}; use e3_sdk::bfv_helpers::decode_bytes_to_vec_u64; use tracing::{error, info}; @@ -22,10 +22,7 @@ impl PlaintextWriter { path: path.to_owned(), } .start(); - bus.do_send(Subscribe { - listener: addr.clone().recipient(), - event_type: "PlaintextAggregated".to_string(), - }); + bus.subscribe("PlaintextAggregated", addr.clone().recipient()); addr } } diff --git a/crates/test-helpers/src/public_key_writer.rs b/crates/test-helpers/src/public_key_writer.rs index 266bf46416..4c12151c11 100644 --- a/crates/test-helpers/src/public_key_writer.rs +++ b/crates/test-helpers/src/public_key_writer.rs @@ -8,7 +8,7 @@ use std::path::PathBuf; use super::write_file_with_dirs; use actix::{Actor, Addr, Context, Handler}; -use e3_events::{EnclaveEvent, EnclaveEventData, EventBus, EventManager, Subscribe}; +use e3_events::{prelude::*, EnclaveEvent, EnclaveEventData, EventManager, EventSubscriber}; use tracing::info; pub struct PublicKeyWriter { @@ -21,10 +21,7 @@ impl PublicKeyWriter { path: path.to_owned(), } .start(); - bus.do_send(Subscribe { - listener: addr.clone().recipient(), - event_type: "PublicKeyAggregated".to_string(), - }); + bus.subscribe("PublicKeyAggregated", addr.clone().recipient()); addr } } From 810711fe5519bfd3bfbeb8c9b2e5b9fe7b757fd2 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 03:36:56 +0000 Subject: [PATCH 13/36] add license --- crates/events/src/event_manager.rs | 6 ++++++ crates/events/src/prelude.rs | 6 ++++++ crates/events/src/traits.rs | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/crates/events/src/event_manager.rs b/crates/events/src/event_manager.rs index 021423fc08..8656b19997 100644 --- a/crates/events/src/event_manager.rs +++ b/crates/events/src/event_manager.rs @@ -1,3 +1,9 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// +// This file is provided WITHOUT ANY WARRANTY; +// without even the implied warranty of MERCHANTABILITY +// or FITNESS FOR A PARTICULAR PURPOSE. + use actix::{Addr, Recipient}; use crate::{ diff --git a/crates/events/src/prelude.rs b/crates/events/src/prelude.rs index 0ec4685b02..e039b5f298 100644 --- a/crates/events/src/prelude.rs +++ b/crates/events/src/prelude.rs @@ -1,2 +1,8 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// +// This file is provided WITHOUT ANY WARRANTY; +// without even the implied warranty of MERCHANTABILITY +// or FITNESS FOR A PARTICULAR PURPOSE. + // Export a prelude to ensure all traits are available pub use crate::traits::*; diff --git a/crates/events/src/traits.rs b/crates/events/src/traits.rs index df4ee31e7f..039d63b12f 100644 --- a/crates/events/src/traits.rs +++ b/crates/events/src/traits.rs @@ -1,3 +1,9 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// +// This file is provided WITHOUT ANY WARRANTY; +// without even the implied warranty of MERCHANTABILITY +// or FITNESS FOR A PARTICULAR PURPOSE. + use actix::{Message, Recipient}; use std::fmt::Display; use std::hash::Hash; From 2b5dc431b4510d1510254db4bb3093fec10c184e Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 05:25:04 +0000 Subject: [PATCH 14/36] fix test compilation --- crates/events/src/eventbus.rs | 16 +- crates/evm/tests/integration.rs | 19 +- crates/test-helpers/src/ciphernode_system.rs | 3 +- crates/test-helpers/src/lib.rs | 4 +- crates/tests/tests/integration.rs | 47 +++-- crates/tests/tests/integration_legacy.rs | 194 ++++++++++--------- 6 files changed, 147 insertions(+), 136 deletions(-) diff --git a/crates/events/src/eventbus.rs b/crates/events/src/eventbus.rs index 3a21e6701e..6263e5744d 100644 --- a/crates/events/src/eventbus.rs +++ b/crates/events/src/eventbus.rs @@ -5,7 +5,7 @@ // or FITNESS FOR A PARTICULAR PURPOSE. use crate::traits::{ErrorEvent, Event}; -use crate::EventManager; +use crate::{prelude::*, EventManager, ManagedEvent}; use actix::prelude::*; use bloom::{BloomFilter, ASMS}; use std::collections::{HashMap, VecDeque}; @@ -137,6 +137,12 @@ impl Handler for EventBus { } } +impl From>> for EventManager { + fn from(value: Addr>) -> Self { + EventManager::new(value) + } +} + ////////////////////////////////////////////////////////////////////////////// // Subscribe Message ////////////////////////////////////////////////////////////////////////////// @@ -415,11 +421,11 @@ impl Handler for HistoryCollector { ////////////////////////////////////////////////////////////////////////////// /// Function to help with testing when we want to maintain a vec of events -pub fn new_event_bus_with_history() -> (Addr>, Addr>) { - let bus = EventBus::::default().start(); +pub fn new_event_bus_with_history() -> (EventManager, Addr>) +{ + let bus: EventManager = EventBus::::default().start().into(); let history = HistoryCollector::new().start(); - - bus.do_send(Subscribe::new("*", history.clone().recipient())); + bus.subscribe("*", history.clone().recipient()); (bus, history) } diff --git a/crates/evm/tests/integration.rs b/crates/evm/tests/integration.rs index 071792fe56..a0b3179ac0 100644 --- a/crates/evm/tests/integration.rs +++ b/crates/evm/tests/integration.rs @@ -17,8 +17,8 @@ use anyhow::Result; use e3_data::Repository; use e3_entrypoint::helpers::datastore::get_in_mem_store; use e3_events::{ - new_event_bus_with_history, EnclaveEvent, EnclaveEventData, GetEvents, HistoryCollector, - Shutdown, TestEvent, + new_event_bus_with_history, prelude::*, EnclaveEvent, EnclaveEventData, GetEvents, + HistoryCollector, Shutdown, TestEvent, }; use e3_evm::{helpers::EthProvider, CoordinatorStart, EvmEventReader, HistoricalEventCoordinator}; use std::time::Duration; @@ -34,16 +34,19 @@ fn test_event_extractor( data: &LogData, topic: Option<&FixedBytes<32>>, _chain_id: u64, -) -> Option { +) -> Option { match topic { Some(&EmitLogs::ValueChanged::SIGNATURE_HASH) => { let Ok(event) = EmitLogs::ValueChanged::decode_log_data(data) else { return None; }; - Some(EnclaveEvent::from(TestEvent { - msg: event.value, - entropy: event.count.try_into().unwrap(), // This prevents de-duplication in tests - })) + Some( + TestEvent { + msg: event.value, + entropy: event.count.try_into().unwrap(), // This prevents de-duplication in tests + } + .into(), + ) } _ => None, } @@ -266,7 +269,7 @@ async fn ensure_resume_after_shutdown() -> Result<()> { // Ensure shutdown doesn't cause event to be lost. sleep(Duration::from_millis(10)).await; - addr1.send(EnclaveEvent::from(Shutdown)).await?; + addr1.send(bus.create_local(Shutdown)).await?; for msg in ["these", "are", "not", "lost"] { contract diff --git a/crates/test-helpers/src/ciphernode_system.rs b/crates/test-helpers/src/ciphernode_system.rs index 1d7bdb3b35..6cdebb4d3c 100644 --- a/crates/test-helpers/src/ciphernode_system.rs +++ b/crates/test-helpers/src/ciphernode_system.rs @@ -197,7 +197,7 @@ mod tests { use super::*; use actix::prelude::*; use e3_data::InMemStore; - use e3_events::{EventBus, EventBusConfig}; + use e3_events::{EventBus, EventBusConfig, EventManager}; async fn mock_setup_node(address: String) -> Result { // Create mock actors for the test @@ -205,6 +205,7 @@ mod tests { let bus = EventBus::::new(EventBusConfig { deduplicate: true }).start(); let history = EventBus::::history(&bus); let errors = EventBus::::error(&bus); + let bus = EventManager::new(bus); Ok(CiphernodeHandle { address, diff --git a/crates/test-helpers/src/lib.rs b/crates/test-helpers/src/lib.rs index f0ac875c7e..65ff87f854 100644 --- a/crates/test-helpers/src/lib.rs +++ b/crates/test-helpers/src/lib.rs @@ -72,7 +72,7 @@ pub fn create_crp_bytes_params( pub fn get_common_setup( param_set: Option, ) -> Result<( - Addr>, + EventManager, SharedRng, Seed, Arc, @@ -95,7 +95,7 @@ pub fn get_common_setup( let (crp_bytes, params) = create_crp_bytes_params(moduli, degree, plaintext_modulus, &seed); let crpoly = CommonRandomPoly::deserialize(&crp_bytes.clone(), ¶ms)?; - Ok((bus, rng, seed, params, crpoly, errors, history)) + Ok((bus.into(), rng, seed, params, crpoly, errors, history)) } /// Simulate libp2p by taking output events on each local bus and filter for !is_local_only() and forward remaining events back to the event bus diff --git a/crates/tests/tests/integration.rs b/crates/tests/tests/integration.rs index f47ffeebd9..d8ae26dc27 100644 --- a/crates/tests/tests/integration.rs +++ b/crates/tests/tests/integration.rs @@ -10,9 +10,9 @@ use anyhow::{bail, Result}; use e3_ciphernode_builder::CiphernodeBuilder; use e3_crypto::Cipher; use e3_events::{ - CiphertextOutputPublished, CommitteeFinalized, ConfigurationUpdated, E3Requested, E3id, - EnclaveEvent, EnclaveEventData, EventBus, EventBusConfig, OperatorActivationChanged, - PlaintextAggregated, TicketBalanceUpdated, + prelude::*, CiphertextOutputPublished, CommitteeFinalized, ConfigurationUpdated, E3Requested, + E3id, EnclaveEvent, EnclaveEventData, EventBus, EventBusConfig, EventManager, + OperatorActivationChanged, PlaintextAggregated, TicketBalanceUpdated, }; use e3_multithread::{GetReport, Multithread}; use e3_sdk::bfv_helpers::{build_bfv_params_arc, decode_bytes_to_vec_u64, encode_bfv_params}; @@ -33,37 +33,34 @@ pub fn save_snapshot(file_name: &str, bytes: &[u8]) { } async fn setup_score_sortition_environment( - bus: &actix::Addr>, + bus: &EventManager, eth_addrs: &Vec, chain_id: u64, ) -> Result<()> { - bus.send(EnclaveEvent::from(ConfigurationUpdated { + bus.dispatch(ConfigurationUpdated { parameter: "ticketPrice".to_string(), old_value: U256::ZERO, new_value: U256::from(10_000_000u64), chain_id, - })) - .await?; + }); let mut adder = AddToCommittee::new(bus, chain_id); for addr in eth_addrs { adder.add(addr).await?; - bus.send(EnclaveEvent::from(TicketBalanceUpdated { + bus.dispatch(TicketBalanceUpdated { operator: addr.clone(), delta: I256::try_from(1_000_000_000u64).unwrap(), new_balance: U256::from(1_000_000_000u64), reason: FixedBytes::ZERO, chain_id, - })) - .await?; + }); - bus.send(EnclaveEvent::from(OperatorActivationChanged { + bus.dispatch(OperatorActivationChanged { operator: addr.clone(), active: true, chain_id, - })) - .await?; + }); } Ok(()) @@ -121,7 +118,10 @@ async fn test_trbfv_actor() -> Result<()> { let rng = create_shared_rng_from_u64(42); // Create "trigger" bus - let bus = EventBus::::new(EventBusConfig { deduplicate: true }).start(); + let bus: EventManager = + EventBus::::new(EventBusConfig { deduplicate: true }) + .start() + .into(); // Parameters (128bits of security) let (degree, plaintext_modulus, moduli) = ( @@ -181,7 +181,7 @@ async fn test_trbfv_actor() -> Result<()> { .with_pubkey_aggregation() .with_sortition_score() .with_threshold_plaintext_aggregation() - .testmode_with_forked_bus(&bus) + .testmode_with_forked_bus(&bus.bus()) .with_logging() .build() .await @@ -194,7 +194,7 @@ async fn test_trbfv_actor() -> Result<()> { .with_injected_multithread(multithread.clone()) .with_trbfv() .with_sortition_score() - .testmode_with_forked_bus(&bus) + .testmode_with_forked_bus(&bus.bus()) .with_logging() .build() .await @@ -238,9 +238,7 @@ async fn test_trbfv_actor() -> Result<()> { params, }; - let event = EnclaveEvent::from(e3_requested); - - bus.do_send(event); + bus.dispatch(e3_requested); // For score sortition, we need to wait for nodes to process E3Requested and run sortition // Since TicketGenerated is a local-only event (not shared across network), we can't collect it @@ -262,12 +260,11 @@ async fn test_trbfv_actor() -> Result<()> { println!("Emitting CommitteeFinalized with {} nodes", committee.len()); - bus.send(EnclaveEvent::from(CommitteeFinalized { + bus.dispatch(CommitteeFinalized { e3_id: e3_id.clone(), committee, chain_id, - })) - .await?; + }); let committee_finalized_timer = Instant::now(); @@ -359,12 +356,12 @@ async fn test_trbfv_actor() -> Result<()> { // Created the event println!("Publishing CiphertextOutputPublished..."); - let ciphertext_published_event = EnclaveEvent::from(CiphertextOutputPublished { + let ciphertext_published_event = CiphertextOutputPublished { ciphertext_output: ciphertexts, e3_id: e3_id.clone(), - }); + }; - bus.send(ciphertext_published_event.clone()).await?; + bus.dispatch(ciphertext_published_event.clone()); println!("CiphertextOutputPublished event has been dispatched!"); diff --git a/crates/tests/tests/integration_legacy.rs b/crates/tests/tests/integration_legacy.rs index 635429c1b2..fb7e3facf8 100644 --- a/crates/tests/tests/integration_legacy.rs +++ b/crates/tests/tests/integration_legacy.rs @@ -14,12 +14,13 @@ use e3_crypto::Cipher; use e3_data::GetDump; use e3_data::InMemStore; use e3_events::EnclaveEventData; +use e3_events::EventManager; use e3_events::GetEvents; use e3_events::{ - CiphernodeSelected, CiphertextOutputPublished, CommitteeFinalized, ConfigurationUpdated, - E3Requested, E3id, EnclaveEvent, EventBus, EventBusConfig, HistoryCollector, - OperatorActivationChanged, OrderedSet, PlaintextAggregated, PublicKeyAggregated, Seed, - Shutdown, Subscribe, TakeEvents, TicketBalanceUpdated, + prelude::*, CiphernodeSelected, CiphertextOutputPublished, CommitteeFinalized, + ConfigurationUpdated, E3Requested, E3id, EnclaveEvent, EventBus, EventBusConfig, + HistoryCollector, OperatorActivationChanged, OrderedSet, PlaintextAggregated, + PublicKeyAggregated, Seed, Shutdown, TakeEvents, TicketBalanceUpdated, }; use e3_net::events::GossipData; use e3_net::{events::NetEvent, NetEventTranslator}; @@ -46,7 +47,7 @@ use tokio::sync::{broadcast, Mutex}; use tokio::time::sleep; async fn setup_local_ciphernode( - bus: &Addr>, + bus: &EventManager, rng: &SharedRng, logging: bool, addr: &str, @@ -56,7 +57,7 @@ async fn setup_local_ciphernode( let mut builder = CiphernodeBuilder::new(rng.clone(), cipher.clone()) .with_keyshare() .with_address(addr) - .testmode_with_forked_bus(bus) + .testmode_with_forked_bus(&bus.bus()) .testmode_with_history() .testmode_with_errors() .with_pubkey_aggregation() @@ -101,7 +102,7 @@ fn generate_pk_shares( } async fn create_local_ciphernodes( - bus: &Addr>, + bus: &EventManager, rng: &SharedRng, count: u32, cipher: &Arc, @@ -119,37 +120,34 @@ async fn create_local_ciphernodes( } async fn setup_score_sortition_environment( - bus: &Addr>, + bus: &EventManager, eth_addrs: &Vec, chain_id: u64, ) -> Result<()> { - bus.send(EnclaveEvent::from(ConfigurationUpdated { + bus.dispatch(ConfigurationUpdated { parameter: "ticketPrice".to_string(), old_value: U256::ZERO, new_value: U256::from(10_000_000u64), chain_id, - })) - .await?; + }); let mut adder = AddToCommittee::new(bus, chain_id); for addr in eth_addrs { adder.add(addr).await?; - bus.send(EnclaveEvent::from(TicketBalanceUpdated { + bus.dispatch(TicketBalanceUpdated { operator: addr.clone(), delta: I256::try_from(1_000_000_000u64).unwrap(), new_balance: U256::from(1_000_000_000u64), reason: FixedBytes::ZERO, chain_id, - })) - .await?; + }); - bus.send(EnclaveEvent::from(OperatorActivationChanged { + bus.dispatch(OperatorActivationChanged { operator: addr.clone(), active: true, chain_id, - })) - .await?; + }); } Ok(()) @@ -193,29 +191,28 @@ async fn test_public_key_aggregation_and_decryption() -> Result<()> { setup_score_sortition_environment(&bus, ð_addrs, 1).await?; - let e3_request_event = EnclaveEvent::from(E3Requested { + let e3_request_event = E3Requested { e3_id: e3_id.clone(), params: ArcBytes::from_bytes(&encode_bfv_params(¶ms)), seed: seed.clone(), threshold_m: 3, threshold_n: 3, // Need to use n now to suggest committee size ..E3Requested::default() - }); + }; println!("Sending E3 event..."); // Send the computation requested event - bus.send(e3_request_event.clone()).await?; + bus.dispatch(e3_request_event.clone()); // Test that we cannot send the same event twice - bus.send(e3_request_event.clone()).await?; + bus.dispatch(e3_request_event.clone()); // Finalize committee with all available nodes - bus.send(EnclaveEvent::from(CommitteeFinalized { + bus.dispatch(CommitteeFinalized { e3_id: e3_id.clone(), committee: eth_addrs.clone(), chain_id: 1, - })) - .await?; + }); // Generate the test shares and pubkey let rng_test = create_shared_rng_from_u64(42); @@ -253,15 +250,15 @@ async fn test_public_key_aggregation_and_decryption() -> Result<()> { let (ciphertext, expected) = encrypt_ciphertext(¶ms, test_pubkey, raw_plaintext)?; // Setup Ciphertext Published Event - let ciphertext_published_event = EnclaveEvent::from(CiphertextOutputPublished { + let ciphertext_published_event = CiphertextOutputPublished { ciphertext_output: ciphertext .iter() .map(|ct| ArcBytes::from_bytes(&ct.to_bytes())) .collect(), e3_id: e3_id.clone(), - }); + }; - bus.send(ciphertext_published_event.clone()).await?; + bus.dispatch(ciphertext_published_event.clone()); let history = history_collector .send(TakeEvents::::new(6)) @@ -309,25 +306,20 @@ async fn test_stopped_keyshares_retain_state() -> Result<()> { }; // Send e3request - bus.send( - EnclaveEvent::from(E3Requested { - e3_id: e3_id.clone(), - threshold_m: 2, - threshold_n: 2, - seed: seed.clone(), - params: ArcBytes::from_bytes(&encode_bfv_params(¶ms)), - ..E3Requested::default() - }) - .clone(), - ) - .await?; - - bus.send(EnclaveEvent::from(CommitteeFinalized { + bus.dispatch(E3Requested { + e3_id: e3_id.clone(), + threshold_m: 2, + threshold_n: 2, + seed: seed.clone(), + params: ArcBytes::from_bytes(&encode_bfv_params(¶ms)), + ..E3Requested::default() + }); + + bus.dispatch(CommitteeFinalized { e3_id: e3_id.clone(), committee: eth_addrs.clone(), chain_id: 1, - })) - .await?; + }); let history_collector = cn1.history().unwrap(); let error_collector = cn1.errors().unwrap(); @@ -339,7 +331,7 @@ async fn test_stopped_keyshares_retain_state() -> Result<()> { assert_eq!(errors.len(), 0); // SEND SHUTDOWN! - bus.send(EnclaveEvent::from(Shutdown)).await?; + bus.dispatch(Shutdown); // This is probably overkill but required to ensure that all the data is written sleep(Duration::from_secs(1)).await; @@ -368,7 +360,9 @@ async fn test_stopped_keyshares_retain_state() -> Result<()> { // Apply the address and data node to two new actors // Here we test that hydration occurred sucessfully - let bus = EventBus::::new(EventBusConfig { deduplicate: true }).start(); + let bus = EventBus::::new(EventBusConfig { deduplicate: true }) + .start() + .into(); let cn1 = setup_local_ciphernode( &bus, &rng, @@ -406,17 +400,13 @@ async fn test_stopped_keyshares_retain_state() -> Result<()> { // Publish the ciphertext let raw_plaintext = vec![vec![1234, 567890]]; let (ciphertext, expected) = encrypt_ciphertext(¶ms, pubkey, raw_plaintext)?; - bus.send( - EnclaveEvent::from(CiphertextOutputPublished { - ciphertext_output: ciphertext - .iter() - .map(|ct| ArcBytes::from_bytes(&ct.to_bytes())) - .collect(), - e3_id: e3_id.clone(), - }) - .clone(), - ) - .await?; + bus.dispatch(CiphertextOutputPublished { + ciphertext_output: ciphertext + .iter() + .map(|ct| ArcBytes::from_bytes(&ct.to_bytes())) + .collect(), + e3_id: e3_id.clone(), + }); let history = history_collector .send(TakeEvents::::new(5)) @@ -453,9 +443,12 @@ async fn test_p2p_actor_forwards_events_to_network() -> Result<()> { // Setup elements in test let (cmd_tx, mut cmd_rx) = mpsc::channel(100); // Transmit byte events to the network let (event_tx, _) = broadcast::channel(100); // Receive byte events from the network - let bus = EventBus::::new(EventBusConfig { deduplicate: true }).start(); + let bus: EventManager = + EventBus::::new(EventBusConfig { deduplicate: true }) + .start() + .into(); let history_collector = HistoryCollector::::new().start(); - bus.do_send(Subscribe::new("*", history_collector.clone().recipient())); + bus.subscribe("*", history_collector.clone().recipient()); let event_rx = Arc::new(event_tx.subscribe()); // Pas cmd and event channels to NetEventTranslator NetEventTranslator::setup(&bus, &cmd_tx, &event_rx, "my-topic"); @@ -485,26 +478,26 @@ async fn test_p2p_actor_forwards_events_to_network() -> Result<()> { anyhow::Ok(()) }); - let evt_1 = EnclaveEvent::from(PlaintextAggregated { + let evt_1 = PlaintextAggregated { e3_id: E3id::new("1235", 1), decrypted_output: vec![ArcBytes::from_bytes(&[1, 2, 3, 4])], - }); + }; - let evt_2 = EnclaveEvent::from(PlaintextAggregated { + let evt_2 = PlaintextAggregated { e3_id: E3id::new("1236", 1), decrypted_output: vec![ArcBytes::from_bytes(&[1, 2, 3, 4])], - }); + }; - let local_evt_3 = EnclaveEvent::from(CiphernodeSelected { + let local_evt_3 = CiphernodeSelected { e3_id: E3id::new("1235", 1), threshold_m: 3, threshold_n: 3, ..CiphernodeSelected::default() - }); + }; - bus.do_send(evt_1.clone()); - bus.do_send(evt_2.clone()); - bus.do_send(local_evt_3.clone()); // This is a local event which should not be broadcast to the network + bus.dispatch(evt_1.clone()); + bus.dispatch(evt_2.clone()); + bus.dispatch(local_evt_3.clone()); // This is a local event which should not be broadcast to the network // check the history of the event bus let history = history_collector @@ -514,15 +507,18 @@ async fn test_p2p_actor_forwards_events_to_network() -> Result<()> { assert_eq!( *msgs.lock().await, vec![ - GossipData::GossipBytes(evt_1.to_bytes()?), - GossipData::GossipBytes(evt_2.to_bytes()?) + GossipData::GossipBytes(bus.create_local(evt_1.clone()).to_bytes()?), + GossipData::GossipBytes(bus.create_local(evt_2.clone()).to_bytes()?) ], // notice no local events "NetEventTranslator did not transmit correct events to the network" ); assert_eq!( - history, - vec![evt_1, evt_2, local_evt_3], // all local events that have been broadcast but no + history + .into_iter() + .map(|e| e.into_data()) + .collect::>(), + vec![evt_1.into(), evt_2.into(), local_evt_3.into()], // all local events that have been broadcast but no // events from the loopback "NetEventTranslator must not retransmit forwarded event to event bus" ); @@ -544,22 +540,21 @@ async fn test_duplicate_e3_id_with_different_chain_id() -> Result<()> { setup_score_sortition_environment(&bus, ð_addrs, 2).await?; // Send the computation requested event - bus.send(EnclaveEvent::from(E3Requested { + bus.dispatch(E3Requested { e3_id: E3id::new("1234", 1), threshold_m: 3, threshold_n: 3, seed: seed.clone(), params: ArcBytes::from_bytes(&encode_bfv_params(¶ms)), ..E3Requested::default() - })) - .await?; + }); - bus.send(EnclaveEvent::from(CommitteeFinalized { + bus.dispatch(CommitteeFinalized { e3_id: E3id::new("1234", 1), committee: eth_addrs.clone(), chain_id: 1, - })) - .await?; + }); + // Generate the test shares and pubkey let rng_test = create_shared_rng_from_u64(42); let test_pubkey = aggregate_public_key(&generate_pk_shares( @@ -572,31 +567,30 @@ async fn test_duplicate_e3_id_with_different_chain_id() -> Result<()> { .await?; assert_eq!( - history.last().unwrap(), - &EnclaveEvent::from(PublicKeyAggregated { + history.last().cloned().unwrap().into_data(), + PublicKeyAggregated { pubkey: test_pubkey.to_bytes(), e3_id: E3id::new("1234", 1), nodes: OrderedSet::from(eth_addrs.clone()), - }) + } + .into() ); // Send the computation requested event - bus.send(EnclaveEvent::from(E3Requested { + bus.dispatch(E3Requested { e3_id: E3id::new("1234", 2), threshold_m: 3, threshold_n: 3, seed: seed.clone(), params: ArcBytes::from_bytes(&encode_bfv_params(¶ms)), ..E3Requested::default() - })) - .await?; + }); - bus.send(EnclaveEvent::from(CommitteeFinalized { + bus.dispatch(CommitteeFinalized { e3_id: E3id::new("1234", 2), committee: eth_addrs.clone(), chain_id: 2, - })) - .await?; + }); let test_pubkey = aggregate_public_key(&generate_pk_shares( ¶ms, &crpoly, &rng_test, ð_addrs, @@ -607,12 +601,13 @@ async fn test_duplicate_e3_id_with_different_chain_id() -> Result<()> { .await?; assert_eq!( - history.last().unwrap(), - &EnclaveEvent::from(PublicKeyAggregated { + history.last().cloned().unwrap().into_data(), + PublicKeyAggregated { pubkey: test_pubkey.to_bytes(), e3_id: E3id::new("1234", 2), nodes: OrderedSet::from(eth_addrs.clone()), - }) + } + .into() ); Ok(()) @@ -625,25 +620,28 @@ async fn test_p2p_actor_forwards_events_to_bus() -> Result<()> { // Setup elements in test let (cmd_tx, _) = mpsc::channel(100); // Transmit byte events to the network let (event_tx, event_rx) = broadcast::channel(100); // Receive byte events from the network - let bus = EventBus::::new(EventBusConfig { deduplicate: true }).start(); + let bus: EventManager = + EventBus::::new(EventBusConfig { deduplicate: true }) + .start() + .into(); let history_collector = HistoryCollector::::new().start(); - bus.do_send(Subscribe::new("*", history_collector.clone().recipient())); + bus.subscribe("*", history_collector.clone().recipient()); NetEventTranslator::setup(&bus, &cmd_tx, &Arc::new(event_rx), "mytopic"); // Capture messages from output on msgs vec - let event = EnclaveEvent::from(E3Requested { + let event = E3Requested { e3_id: E3id::new("1235", 1), threshold_m: 3, threshold_n: 3, seed: seed.clone(), params: ArcBytes::from_bytes(&[1, 2, 3, 4]), ..E3Requested::default() - }); + }; // lets send an event from the network let _ = event_tx.send(NetEvent::GossipData(GossipData::GossipBytes( - event.to_bytes()?, + bus.create_local(event.clone()).to_bytes()?, ))); // check the history of the event bus @@ -651,7 +649,13 @@ async fn test_p2p_actor_forwards_events_to_bus() -> Result<()> { .send(TakeEvents::::new(1)) .await?; - assert_eq!(history, vec![event]); + assert_eq!( + history + .into_iter() + .map(|e| e.into_data()) + .collect::>(), + vec![event.into()] + ); Ok(()) } From 5eb29d072cfb70275e78d6d2a21afb25c40338cc Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 17:19:16 +0000 Subject: [PATCH 15/36] rename event manager --- crates/aggregator/src/committee_finalizer.rs | 8 ++++---- crates/aggregator/src/ext.rs | 14 ++++++------- crates/aggregator/src/plaintext_aggregator.rs | 6 +++--- crates/aggregator/src/publickey_aggregator.rs | 6 +++--- .../src/threshold_plaintext_aggregator.rs | 6 +++--- crates/ciphernode-builder/src/ciphernode.rs | 6 +++--- crates/data/src/sled_store.rs | 6 +++--- crates/entrypoint/src/helpers/datastore.rs | 6 +++--- crates/entrypoint/src/helpers/shutdown.rs | 4 ++-- .../entrypoint/src/start/aggregator_start.rs | 4 ++-- crates/entrypoint/src/start/start.rs | 4 ++-- crates/events/src/event_manager.rs | 14 ++++++------- crates/events/src/eventbus.rs | 14 ++++++------- crates/events/src/eventbus_factory.rs | 4 ++-- crates/evm/src/bonding_registry_sol.rs | 6 +++--- crates/evm/src/ciphernode_registry_sol.rs | 14 ++++++------- crates/evm/src/enclave_sol.rs | 4 ++-- crates/evm/src/enclave_sol_reader.rs | 4 ++-- crates/evm/src/enclave_sol_writer.rs | 8 ++++---- crates/evm/src/event_reader.rs | 10 +++++----- .../evm/src/historical_event_coordinator.rs | 8 ++++---- crates/fhe/src/ext.rs | 6 +++--- crates/keyshare/src/ext.rs | 10 +++++----- crates/keyshare/src/keyshare.rs | 6 +++--- crates/keyshare/src/threshold_keyshare.rs | 6 +++--- crates/net/src/document_publisher.rs | 20 +++++++++---------- crates/net/src/net_event_translator.rs | 10 +++++----- crates/request/src/router.rs | 10 +++++----- crates/sortition/src/ciphernode_selector.rs | 8 ++++---- crates/sortition/src/sortition.rs | 8 ++++---- crates/test-helpers/src/ciphernode_system.rs | 4 ++-- crates/test-helpers/src/lib.rs | 8 ++++---- crates/test-helpers/src/plaintext_writer.rs | 4 ++-- crates/test-helpers/src/public_key_writer.rs | 4 ++-- crates/tests/tests/integration.rs | 6 +++--- crates/tests/tests/integration_legacy.rs | 12 +++++------ 36 files changed, 139 insertions(+), 139 deletions(-) diff --git a/crates/aggregator/src/committee_finalizer.rs b/crates/aggregator/src/committee_finalizer.rs index e1ce6df93c..ef85620c22 100644 --- a/crates/aggregator/src/committee_finalizer.rs +++ b/crates/aggregator/src/committee_finalizer.rs @@ -7,7 +7,7 @@ use actix::prelude::*; use e3_events::{ prelude::*, CommitteeFinalizeRequested, CommitteeRequested, EnclaveEvent, EnclaveEventData, - EventManager, Shutdown, + BusHandle, Shutdown, }; use std::collections::HashMap; use std::time::Duration; @@ -16,19 +16,19 @@ use tracing::{error, info}; /// CommitteeFinalizer is an actor that listens to CommitteeRequested events and dispatches /// CommitteeFinalizeRequested events after the submission deadline has passed. pub struct CommitteeFinalizer { - bus: EventManager, + bus: BusHandle, pending_committees: HashMap, } impl CommitteeFinalizer { - pub fn new(bus: &EventManager) -> Self { + pub fn new(bus: &BusHandle) -> Self { Self { bus: bus.clone(), pending_committees: HashMap::new(), } } - pub fn attach(bus: &EventManager) -> Addr { + pub fn attach(bus: &BusHandle) -> Addr { let addr = CommitteeFinalizer::new(bus).start(); bus.subscribe_all( diff --git a/crates/aggregator/src/ext.rs b/crates/aggregator/src/ext.rs index 6769ded134..1688b447a4 100644 --- a/crates/aggregator/src/ext.rs +++ b/crates/aggregator/src/ext.rs @@ -16,7 +16,7 @@ use anyhow::{anyhow, Result}; use async_trait::async_trait; use e3_data::{AutoPersist, RepositoriesFactory}; use e3_events::prelude::*; -use e3_events::{EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventManager}; +use e3_events::{EnclaveErrorType, EnclaveEvent, EnclaveEventData, BusHandle}; use e3_fhe::ext::FHE_KEY; use e3_multithread::Multithread; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, META_KEY}; @@ -24,12 +24,12 @@ use e3_sortition::Sortition; #[deprecated = "In favour of ThresholdPlaintextAggregatorExtension"] pub struct PlaintextAggregatorExtension { - bus: EventManager, + bus: BusHandle, sortition: Addr, } impl PlaintextAggregatorExtension { - pub fn create(bus: &EventManager, sortition: &Addr) -> Box { + pub fn create(bus: &BusHandle, sortition: &Addr) -> Box { Box::new(Self { bus: bus.clone(), sortition: sortition.clone(), @@ -144,12 +144,12 @@ impl E3Extension for PlaintextAggregatorExtension { } pub struct PublicKeyAggregatorExtension { - bus: EventManager, + bus: BusHandle, sortition: Addr, } impl PublicKeyAggregatorExtension { - pub fn create(bus: &EventManager, sortition: &Addr) -> Box { + pub fn create(bus: &BusHandle, sortition: &Addr) -> Box { Box::new(Self { bus: bus.clone(), sortition: sortition.clone(), @@ -250,14 +250,14 @@ impl E3Extension for PublicKeyAggregatorExtension { } pub struct ThresholdPlaintextAggregatorExtension { - bus: EventManager, + bus: BusHandle, sortition: Addr, multithread: Addr, } impl ThresholdPlaintextAggregatorExtension { pub fn create( - bus: &EventManager, + bus: &BusHandle, sortition: &Addr, multithread: &Addr, ) -> Box { diff --git a/crates/aggregator/src/plaintext_aggregator.rs b/crates/aggregator/src/plaintext_aggregator.rs index 6ab82c7b25..744e7e3d06 100644 --- a/crates/aggregator/src/plaintext_aggregator.rs +++ b/crates/aggregator/src/plaintext_aggregator.rs @@ -8,7 +8,7 @@ use actix::prelude::*; use anyhow::Result; use e3_data::Persistable; use e3_events::{ - prelude::*, DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, EventManager, + prelude::*, DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, BusHandle, OrderedSet, PlaintextAggregated, Seed, }; use e3_fhe::{Fhe, GetAggregatePlaintext}; @@ -72,7 +72,7 @@ struct ComputeAggregate { #[deprecated = "To be replaced by ThresholdPlaintextAggregator"] pub struct PlaintextAggregator { fhe: Arc, - bus: EventManager, + bus: BusHandle, sortition: Addr, e3_id: E3id, state: Persistable, @@ -80,7 +80,7 @@ pub struct PlaintextAggregator { pub struct PlaintextAggregatorParams { pub fhe: Arc, - pub bus: EventManager, + pub bus: BusHandle, pub sortition: Addr, pub e3_id: E3id, } diff --git a/crates/aggregator/src/publickey_aggregator.rs b/crates/aggregator/src/publickey_aggregator.rs index 6f787f3d40..ed28e14a78 100644 --- a/crates/aggregator/src/publickey_aggregator.rs +++ b/crates/aggregator/src/publickey_aggregator.rs @@ -8,7 +8,7 @@ use actix::prelude::*; use anyhow::Result; use e3_data::Persistable; use e3_events::{ - prelude::*, Die, E3id, EnclaveEvent, EnclaveEventData, EventManager, KeyshareCreated, + prelude::*, Die, E3id, EnclaveEvent, EnclaveEventData, BusHandle, KeyshareCreated, OrderedSet, PublicKeyAggregated, Seed, }; use e3_fhe::{Fhe, GetAggregatePublicKey}; @@ -59,7 +59,7 @@ struct NotifyNetwork { pub struct PublicKeyAggregator { fhe: Arc, - bus: EventManager, + bus: BusHandle, sortition: Addr, e3_id: E3id, state: Persistable, @@ -67,7 +67,7 @@ pub struct PublicKeyAggregator { pub struct PublicKeyAggregatorParams { pub fhe: Arc, - pub bus: EventManager, + pub bus: BusHandle, pub sortition: Addr, pub e3_id: E3id, } diff --git a/crates/aggregator/src/threshold_plaintext_aggregator.rs b/crates/aggregator/src/threshold_plaintext_aggregator.rs index 4eda0ae4c3..68bcb7f9ae 100644 --- a/crates/aggregator/src/threshold_plaintext_aggregator.rs +++ b/crates/aggregator/src/threshold_plaintext_aggregator.rs @@ -11,7 +11,7 @@ use anyhow::{anyhow, bail, Result}; use e3_data::Persistable; use e3_events::{ prelude::*, ComputeRequest, DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, - EventManager, PlaintextAggregated, Seed, + BusHandle, PlaintextAggregated, Seed, }; use e3_multithread::Multithread; use e3_sortition::{GetNodesForE3, Sortition}; @@ -122,7 +122,7 @@ pub struct ComputeAggregate { pub struct ThresholdPlaintextAggregator { multithread: Addr, - bus: EventManager, + bus: BusHandle, sortition: Addr, e3_id: E3id, state: Persistable, @@ -130,7 +130,7 @@ pub struct ThresholdPlaintextAggregator { pub struct ThresholdPlaintextAggregatorParams { pub multithread: Addr, - pub bus: EventManager, + pub bus: BusHandle, pub sortition: Addr, pub e3_id: E3id, } diff --git a/crates/ciphernode-builder/src/ciphernode.rs b/crates/ciphernode-builder/src/ciphernode.rs index af44ee5e86..9f1bfa5342 100644 --- a/crates/ciphernode-builder/src/ciphernode.rs +++ b/crates/ciphernode-builder/src/ciphernode.rs @@ -6,13 +6,13 @@ use actix::Addr; use e3_data::{DataStore, InMemStore, StoreAddr}; -use e3_events::{EnclaveEvent, EventBus, EventManager, HistoryCollector}; +use e3_events::{EnclaveEvent, EventBus, BusHandle, HistoryCollector}; #[derive(Clone, Debug)] pub struct CiphernodeHandle { pub address: String, pub store: DataStore, - pub bus: EventManager, + pub bus: BusHandle, pub history: Option>>, pub errors: Option>>, } @@ -21,7 +21,7 @@ impl CiphernodeHandle { pub fn new( address: String, store: DataStore, - bus: EventManager, + bus: BusHandle, history: Option>>, errors: Option>>, ) -> Self { diff --git a/crates/data/src/sled_store.rs b/crates/data/src/sled_store.rs index ce88ae4687..e63414f5be 100644 --- a/crates/data/src/sled_store.rs +++ b/crates/data/src/sled_store.rs @@ -9,7 +9,7 @@ use actix::{Actor, ActorContext, Addr, Handler}; use anyhow::{Context, Result}; use e3_events::{ get_enclave_event_manager, prelude::*, EnclaveErrorType, EnclaveEvent, EnclaveEventData, - EventManager, + BusHandle, }; use once_cell::sync::Lazy; use sled::Db; @@ -22,7 +22,7 @@ use tracing::{error, info}; pub struct SledStore { db: Option, - bus: EventManager, + bus: BusHandle, } impl Actor for SledStore { @@ -30,7 +30,7 @@ impl Actor for SledStore { } impl SledStore { - pub fn new(bus: &EventManager, path: &PathBuf) -> Result> { + pub fn new(bus: &BusHandle, path: &PathBuf) -> Result> { info!("Starting SledStore with {:?}", path); let db = SledDb::new(PathBuf::from(path))?; diff --git a/crates/entrypoint/src/helpers/datastore.rs b/crates/entrypoint/src/helpers/datastore.rs index 7c09694f90..6f2d70c5b3 100644 --- a/crates/entrypoint/src/helpers/datastore.rs +++ b/crates/entrypoint/src/helpers/datastore.rs @@ -11,9 +11,9 @@ use anyhow::Result; use e3_config::AppConfig; use e3_data::{DataStore, InMemStore, SledDb, SledStore}; use e3_data::{Repositories, RepositoriesFactory}; -use e3_events::{get_enclave_event_manager, EnclaveEvent, EventManager}; +use e3_events::{get_enclave_event_manager, EnclaveEvent, BusHandle}; -pub fn get_sled_store(bus: &EventManager, db_file: &PathBuf) -> Result { +pub fn get_sled_store(bus: &BusHandle, db_file: &PathBuf) -> Result { Ok((&SledStore::new(bus, db_file)?).into()) } @@ -21,7 +21,7 @@ pub fn get_in_mem_store() -> DataStore { (&InMemStore::new(true).start()).into() } -pub fn setup_datastore(config: &AppConfig, bus: &EventManager) -> Result { +pub fn setup_datastore(config: &AppConfig, bus: &BusHandle) -> Result { let store: DataStore = if !config.use_in_mem_store() { get_sled_store(&bus, &config.db_file())? } else { diff --git a/crates/entrypoint/src/helpers/shutdown.rs b/crates/entrypoint/src/helpers/shutdown.rs index e75805ad32..1406adecef 100644 --- a/crates/entrypoint/src/helpers/shutdown.rs +++ b/crates/entrypoint/src/helpers/shutdown.rs @@ -5,7 +5,7 @@ // or FITNESS FOR A PARTICULAR PURPOSE. use anyhow::Result; -use e3_events::{prelude::*, EnclaveEvent, EventManager, Shutdown}; +use e3_events::{prelude::*, EnclaveEvent, BusHandle, Shutdown}; use std::time::Duration; use tokio::{ select, @@ -15,7 +15,7 @@ use tokio::{ use tracing::{error, info}; pub async fn listen_for_shutdown( - bus: EventManager, + bus: BusHandle, mut handle: JoinHandle>, ) { let mut sigterm = diff --git a/crates/entrypoint/src/start/aggregator_start.rs b/crates/entrypoint/src/start/aggregator_start.rs index 227ffbab48..bee9d484ab 100644 --- a/crates/entrypoint/src/start/aggregator_start.rs +++ b/crates/entrypoint/src/start/aggregator_start.rs @@ -9,7 +9,7 @@ use e3_ciphernode_builder::CiphernodeBuilder; use e3_config::AppConfig; use e3_crypto::Cipher; use e3_data::RepositoriesFactory; -use e3_events::{get_enclave_event_manager, EnclaveEvent, EventManager}; +use e3_events::{get_enclave_event_manager, EnclaveEvent, BusHandle}; use e3_net::{NetEventTranslator, NetRepositoryFactory}; use e3_test_helpers::{PlaintextWriter, PublicKeyWriter}; use rand::SeedableRng; @@ -27,7 +27,7 @@ pub async fn execute( pubkey_write_path: Option, plaintext_write_path: Option, experimental_trbfv: bool, -) -> Result<(EventManager, JoinHandle>, String)> { +) -> Result<(BusHandle, JoinHandle>, String)> { let bus = get_enclave_event_manager(); let rng = Arc::new(Mutex::new(ChaCha20Rng::from_rng(OsRng)?)); let store = setup_datastore(config, &bus)?; diff --git a/crates/entrypoint/src/start/start.rs b/crates/entrypoint/src/start/start.rs index ac54ae7b98..b50e00edad 100644 --- a/crates/entrypoint/src/start/start.rs +++ b/crates/entrypoint/src/start/start.rs @@ -11,7 +11,7 @@ use e3_config::AppConfig; use e3_crypto::Cipher; use e3_data::RepositoriesFactory; use e3_events::{get_enclave_event_manager, EnclaveEvent}; -use e3_events::{prelude::*, EventManager}; +use e3_events::{prelude::*, BusHandle}; use e3_net::{NetEventTranslator, NetRepositoryFactory}; use rand::SeedableRng; use rand_chacha::rand_core::OsRng; @@ -26,7 +26,7 @@ pub async fn execute( config: &AppConfig, address: Address, experimental_trbfv: bool, -) -> Result<(EventManager, JoinHandle>, String)> { +) -> Result<(BusHandle, JoinHandle>, String)> { let rng = Arc::new(Mutex::new(rand_chacha::ChaCha20Rng::from_rng(OsRng)?)); let bus = get_enclave_event_manager(); diff --git a/crates/events/src/event_manager.rs b/crates/events/src/event_manager.rs index 8656b19997..89598a3f54 100644 --- a/crates/events/src/event_manager.rs +++ b/crates/events/src/event_manager.rs @@ -15,11 +15,11 @@ use crate::{ }; #[derive(Clone, Debug)] -pub struct EventManager { +pub struct BusHandle { bus: Addr>, } -impl EventManager { +impl BusHandle { pub fn new(bus: Addr>) -> Self { Self { bus } } @@ -29,7 +29,7 @@ impl EventManager { } } -impl EventDispatcher for EventManager { +impl EventDispatcher for BusHandle { fn dispatch(&self, data: impl Into) { let evt = self.create_local(data); self.bus.do_send(evt); @@ -45,7 +45,7 @@ impl EventDispatcher for EventManager { } } -impl ErrorDispatcher for EventManager +impl ErrorDispatcher for BusHandle where E: ManagedEvent, { @@ -55,7 +55,7 @@ where } } -impl EventFactory for EventManager { +impl EventFactory for BusHandle { fn create_local(&self, data: impl Into) -> E { E::new_with_timestamp(data.into(), 0) } @@ -65,13 +65,13 @@ impl EventFactory for EventManager { } } -impl ErrorFactory for EventManager { +impl ErrorFactory for BusHandle { fn create_err(&self, err_type: E::ErrType, error: impl Into) -> E { E::new_error(err_type, error) } } -impl EventSubscriber for EventManager { +impl EventSubscriber for BusHandle { fn subscribe(&self, event_type: &str, recipient: Recipient) { self.bus.do_send(Subscribe::new(event_type, recipient)) } diff --git a/crates/events/src/eventbus.rs b/crates/events/src/eventbus.rs index 6263e5744d..9a729c4dac 100644 --- a/crates/events/src/eventbus.rs +++ b/crates/events/src/eventbus.rs @@ -5,7 +5,7 @@ // or FITNESS FOR A PARTICULAR PURPOSE. use crate::traits::{ErrorEvent, Event}; -use crate::{prelude::*, EventManager, ManagedEvent}; +use crate::{prelude::*, BusHandle, ManagedEvent}; use actix::prelude::*; use bloom::{BloomFilter, ASMS}; use std::collections::{HashMap, VecDeque}; @@ -76,8 +76,8 @@ impl EventBus { addr } - pub fn manager(source: Addr>) -> EventManager { - EventManager::new(source) + pub fn manager(source: Addr>) -> BusHandle { + BusHandle::new(source) } pub fn pipe(source: &Addr>, dest: &Addr>) { @@ -137,9 +137,9 @@ impl Handler for EventBus { } } -impl From>> for EventManager { +impl From>> for BusHandle { fn from(value: Addr>) -> Self { - EventManager::new(value) + BusHandle::new(value) } } @@ -421,9 +421,9 @@ impl Handler for HistoryCollector { ////////////////////////////////////////////////////////////////////////////// /// Function to help with testing when we want to maintain a vec of events -pub fn new_event_bus_with_history() -> (EventManager, Addr>) +pub fn new_event_bus_with_history() -> (BusHandle, Addr>) { - let bus: EventManager = EventBus::::default().start().into(); + let bus: BusHandle = EventBus::::default().start().into(); let history = HistoryCollector::new().start(); bus.subscribe("*", history.clone().recipient()); diff --git a/crates/events/src/eventbus_factory.rs b/crates/events/src/eventbus_factory.rs index 62ac3773ad..f4469ce111 100644 --- a/crates/events/src/eventbus_factory.rs +++ b/crates/events/src/eventbus_factory.rs @@ -15,7 +15,7 @@ use std::sync::Mutex; use crate::traits::Event; use crate::EnclaveEvent; use crate::EventBus; -use crate::EventManager; +use crate::BusHandle; use crate::HistoryCollector; use crate::Subscribe; @@ -92,6 +92,6 @@ pub fn get_error_collector() -> Addr> { EventBusFactory::instance().get_error_collector() } -pub fn get_enclave_event_manager() -> EventManager { +pub fn get_enclave_event_manager() -> BusHandle { EventBus::manager(get_enclave_event_bus()) } diff --git a/crates/evm/src/bonding_registry_sol.rs b/crates/evm/src/bonding_registry_sol.rs index fe08a4af7d..0470adad24 100644 --- a/crates/evm/src/bonding_registry_sol.rs +++ b/crates/evm/src/bonding_registry_sol.rs @@ -16,7 +16,7 @@ use alloy::{ }; use anyhow::Result; use e3_data::Repository; -use e3_events::{EnclaveEvent, EnclaveEventData, EventManager}; +use e3_events::{EnclaveEvent, EnclaveEventData, BusHandle}; use tracing::{error, info, trace}; sol!( @@ -145,7 +145,7 @@ pub struct BondingRegistrySolReader; impl BondingRegistrySolReader { pub async fn attach

( processor: &Recipient, - bus: &EventManager, + bus: &BusHandle, provider: EthProvider

, contract_address: &str, repository: &Repository, @@ -179,7 +179,7 @@ pub struct BondingRegistrySol; impl BondingRegistrySol { pub async fn attach

( processor: &Recipient, - bus: &EventManager, + bus: &BusHandle, provider: EthProvider

, contract_address: &str, repository: &Repository, diff --git a/crates/evm/src/ciphernode_registry_sol.rs b/crates/evm/src/ciphernode_registry_sol.rs index b311ac1f66..228b0e627c 100644 --- a/crates/evm/src/ciphernode_registry_sol.rs +++ b/crates/evm/src/ciphernode_registry_sol.rs @@ -19,7 +19,7 @@ use anyhow::Result; use e3_data::Repository; use e3_events::{ prelude::*, CommitteeFinalizeRequested, CommitteeFinalized, E3id, EnclaveErrorType, - EnclaveEvent, EnclaveEventData, EventManager, EventSubscriber, OrderedSet, PublicKeyAggregated, + EnclaveEvent, EnclaveEventData, BusHandle, EventSubscriber, OrderedSet, PublicKeyAggregated, Seed, Shutdown, TicketGenerated, TicketId, }; use tracing::{error, info, trace}; @@ -218,7 +218,7 @@ pub struct CiphernodeRegistrySolReader; impl CiphernodeRegistrySolReader { pub async fn attach

( processor: &Recipient, - bus: &EventManager, + bus: &BusHandle, provider: EthProvider

, contract_address: &str, repository: &Repository, @@ -250,12 +250,12 @@ impl CiphernodeRegistrySolReader { pub struct CiphernodeRegistrySolWriter

{ provider: EthProvider

, contract_address: Address, - bus: EventManager, + bus: BusHandle, } impl CiphernodeRegistrySolWriter

{ pub async fn new( - bus: &EventManager, + bus: &BusHandle, provider: EthProvider

, contract_address: Address, ) -> Result { @@ -267,7 +267,7 @@ impl CiphernodeRegistrySolWriter } pub async fn attach( - bus: &EventManager, + bus: &BusHandle, provider: EthProvider

, contract_address: &str, is_aggregator: bool, @@ -508,7 +508,7 @@ pub struct CiphernodeRegistrySol; impl CiphernodeRegistrySol { pub async fn attach

( processor: &Recipient, - bus: &EventManager, + bus: &BusHandle, provider: EthProvider

, contract_address: &str, repository: &Repository, @@ -532,7 +532,7 @@ impl CiphernodeRegistrySol { } pub async fn attach_writer

( - bus: &EventManager, + bus: &BusHandle, provider: EthProvider

, contract_address: &str, is_aggregator: bool, diff --git a/crates/evm/src/enclave_sol.rs b/crates/evm/src/enclave_sol.rs index e28a2018c1..70fd7b42b1 100644 --- a/crates/evm/src/enclave_sol.rs +++ b/crates/evm/src/enclave_sol.rs @@ -12,14 +12,14 @@ use actix::{Addr, Recipient}; use alloy::providers::{Provider, WalletProvider}; use anyhow::Result; use e3_data::Repository; -use e3_events::{EnclaveEvent, EventBus, EventManager}; +use e3_events::{EnclaveEvent, EventBus, BusHandle}; pub struct EnclaveSol; impl EnclaveSol { pub async fn attach( processor: &Recipient, - bus: &EventManager, + bus: &BusHandle, read_provider: EthProvider, write_provider: EthProvider, contract_address: &str, diff --git a/crates/evm/src/enclave_sol_reader.rs b/crates/evm/src/enclave_sol_reader.rs index d5a57a385d..eac66d09c3 100644 --- a/crates/evm/src/enclave_sol_reader.rs +++ b/crates/evm/src/enclave_sol_reader.rs @@ -13,7 +13,7 @@ use alloy::providers::Provider; use alloy::{sol, sol_types::SolEvent}; use anyhow::Result; use e3_data::Repository; -use e3_events::{prelude::*, E3id, EnclaveEvent, EnclaveEventData, EventManager}; +use e3_events::{prelude::*, E3id, EnclaveEvent, EnclaveEventData, BusHandle}; use e3_utils::utility_types::ArcBytes; use num_bigint::BigUint; use tracing::{error, info, trace}; @@ -107,7 +107,7 @@ pub struct EnclaveSolReader; impl EnclaveSolReader { pub async fn attach

( processor: &Recipient, - bus: &EventManager, + bus: &BusHandle, provider: EthProvider

, contract_address: &str, repository: &Repository, diff --git a/crates/evm/src/enclave_sol_writer.rs b/crates/evm/src/enclave_sol_writer.rs index c8f0661668..8836ac46ad 100644 --- a/crates/evm/src/enclave_sol_writer.rs +++ b/crates/evm/src/enclave_sol_writer.rs @@ -20,7 +20,7 @@ use anyhow::Result; use e3_events::prelude::*; use e3_events::EnclaveEvent; use e3_events::EnclaveEventData; -use e3_events::EventManager; +use e3_events::BusHandle; use e3_events::Shutdown; use e3_events::{E3id, EnclaveErrorType, PlaintextAggregated, Subscribe}; use tracing::info; @@ -35,12 +35,12 @@ sol!( pub struct EnclaveSolWriter

{ provider: EthProvider

, contract_address: Address, - bus: EventManager, + bus: BusHandle, } impl EnclaveSolWriter

{ pub fn new( - bus: &EventManager, + bus: &BusHandle, provider: EthProvider

, contract_address: Address, ) -> Result { @@ -52,7 +52,7 @@ impl EnclaveSolWriter

{ } pub async fn attach( - bus: &EventManager, + bus: &BusHandle, provider: EthProvider

, contract_address: &str, ) -> Result>> { diff --git a/crates/evm/src/event_reader.rs b/crates/evm/src/event_reader.rs index af29791db3..8fc4031526 100644 --- a/crates/evm/src/event_reader.rs +++ b/crates/evm/src/event_reader.rs @@ -15,7 +15,7 @@ use alloy::rpc::types::Filter; use anyhow::{anyhow, Result}; use e3_data::{AutoPersist, Persistable, Repository}; use e3_events::{prelude::*, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventId}; -use e3_events::{Event, EventManager}; +use e3_events::{Event, BusHandle}; use futures_util::stream::StreamExt; use std::collections::HashSet; use tokio::select; @@ -54,7 +54,7 @@ pub struct EvmEventReaderParams

{ contract_address: Address, start_block: Option, processor: Recipient, - bus: EventManager, + bus: BusHandle, state: Persistable, rpc_url: String, } @@ -83,7 +83,7 @@ pub struct EvmEventReader

{ /// Processor to forward events an actor processor: Recipient, /// Event bus for error propagation only - bus: EventManager, + bus: BusHandle, /// The auto persistable state of the event reader state: Persistable, /// The RPC URL for the provider @@ -113,7 +113,7 @@ impl EvmEventReader

{ contract_address: &str, start_block: Option, processor: &Recipient, - bus: &EventManager, + bus: &BusHandle, repository: &Repository, rpc_url: String, ) -> Result> { @@ -191,7 +191,7 @@ async fn stream_from_evm( extractor: fn(&LogData, Option<&B256>, u64) -> Option, mut shutdown: oneshot::Receiver<()>, start_block: Option, - bus: &EventManager, + bus: &BusHandle, rpc_url: String, ) { let chain_id = provider.chain_id(); diff --git a/crates/evm/src/historical_event_coordinator.rs b/crates/evm/src/historical_event_coordinator.rs index 0b79882770..3e3c2ce02b 100644 --- a/crates/evm/src/historical_event_coordinator.rs +++ b/crates/evm/src/historical_event_coordinator.rs @@ -6,7 +6,7 @@ use crate::EnclaveEvmEvent; use actix::prelude::*; -use e3_events::{prelude::*, EnclaveEvent, EnclaveEventData, EventManager}; +use e3_events::{prelude::*, EnclaveEvent, EnclaveEventData, BusHandle}; use tracing::info; #[derive(Clone)] @@ -30,13 +30,13 @@ pub struct HistoricalEventCoordinator { /// Buffered events during historical sync buffered_events: Vec, /// Target to forward events to (typically EventBus) - target: EventManager, + target: BusHandle, /// Whether we've started forwarding (after Start message) started: bool, } impl HistoricalEventCoordinator { - pub fn new(target: EventManager) -> Self { + pub fn new(target: BusHandle) -> Self { Self { registered_count: 0, completed_count: 0, @@ -46,7 +46,7 @@ impl HistoricalEventCoordinator { } } - pub fn setup(target: EventManager) -> Addr { + pub fn setup(target: BusHandle) -> Addr { Self::new(target).start() } diff --git a/crates/fhe/src/ext.rs b/crates/fhe/src/ext.rs index 668741c622..8c568f34f8 100644 --- a/crates/fhe/src/ext.rs +++ b/crates/fhe/src/ext.rs @@ -9,7 +9,7 @@ use anyhow::{anyhow, Result}; use async_trait::async_trait; use e3_data::{FromSnapshotWithParams, RepositoriesFactory, Snapshot}; use e3_events::{ - prelude::*, E3Requested, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventManager, + prelude::*, BusHandle, E3Requested, EnclaveErrorType, EnclaveEvent, EnclaveEventData, }; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, TypedKey}; use e3_utils::SharedRng; @@ -20,11 +20,11 @@ pub const FHE_KEY: TypedKey> = TypedKey::new("fhe"); /// TODO: move these to each package with access on MyStruct::launcher() pub struct FheExtension { rng: SharedRng, - bus: EventManager, + bus: BusHandle, } impl FheExtension { - pub fn create(bus: &EventManager, rng: &SharedRng) -> Box { + pub fn create(bus: &BusHandle, rng: &SharedRng) -> Box { Box::new(Self { rng: rng.clone(), bus: bus.clone(), diff --git a/crates/keyshare/src/ext.rs b/crates/keyshare/src/ext.rs index 625997fcaf..da6efff127 100644 --- a/crates/keyshare/src/ext.rs +++ b/crates/keyshare/src/ext.rs @@ -13,21 +13,21 @@ use anyhow::{anyhow, Result}; use async_trait::async_trait; use e3_crypto::Cipher; use e3_data::{AutoPersist, RepositoriesFactory}; -use e3_events::{prelude::*, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventManager}; +use e3_events::{prelude::*, EnclaveErrorType, EnclaveEvent, EnclaveEventData, BusHandle}; use e3_fhe::ext::FHE_KEY; use e3_multithread::Multithread; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, META_KEY}; use std::sync::Arc; pub struct KeyshareExtension { - bus: EventManager, + bus: BusHandle, address: String, cipher: Arc, } impl KeyshareExtension { pub fn create( - bus: &EventManager, + bus: &BusHandle, address: &str, cipher: &Arc, ) -> Box { @@ -121,7 +121,7 @@ impl E3Extension for KeyshareExtension { } pub struct ThresholdKeyshareExtension { - bus: EventManager, + bus: BusHandle, cipher: Arc, address: String, multithread: Addr, @@ -129,7 +129,7 @@ pub struct ThresholdKeyshareExtension { impl ThresholdKeyshareExtension { pub fn create( - bus: &EventManager, + bus: &BusHandle, cipher: &Arc, multithread: &Addr, address: &str, diff --git a/crates/keyshare/src/keyshare.rs b/crates/keyshare/src/keyshare.rs index d5f632369a..4d7c056d4d 100644 --- a/crates/keyshare/src/keyshare.rs +++ b/crates/keyshare/src/keyshare.rs @@ -10,7 +10,7 @@ use e3_crypto::Cipher; use e3_data::Persistable; use e3_events::{ prelude::*, CiphernodeSelected, CiphertextOutputPublished, DecryptionshareCreated, Die, - E3RequestComplete, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventManager, FromError, + E3RequestComplete, EnclaveErrorType, EnclaveEvent, EnclaveEventData, BusHandle, FromError, KeyshareCreated, }; use e3_fhe::{DecryptCiphertext, Fhe}; @@ -20,7 +20,7 @@ use tracing::warn; pub struct Keyshare { fhe: Arc, - bus: EventManager, + bus: BusHandle, secret: Persistable>, address: String, cipher: Arc, @@ -31,7 +31,7 @@ impl Actor for Keyshare { } pub struct KeyshareParams { - pub bus: EventManager, + pub bus: BusHandle, pub secret: Persistable>, pub fhe: Arc, pub address: String, diff --git a/crates/keyshare/src/threshold_keyshare.rs b/crates/keyshare/src/threshold_keyshare.rs index f0322711ce..7bd3443ee8 100644 --- a/crates/keyshare/src/threshold_keyshare.rs +++ b/crates/keyshare/src/threshold_keyshare.rs @@ -10,7 +10,7 @@ use e3_crypto::{Cipher, SensitiveBytes}; use e3_data::Persistable; use e3_events::{ prelude::*, CiphernodeSelected, CiphertextOutputPublished, ComputeRequest, ComputeResponse, - DecryptionshareCreated, E3id, EnclaveEvent, EnclaveEventData, EventManager, KeyshareCreated, + DecryptionshareCreated, E3id, EnclaveEvent, EnclaveEventData, BusHandle, KeyshareCreated, PartyId, ThresholdShare, ThresholdShareCreated, }; use e3_fhe::create_crp; @@ -269,14 +269,14 @@ impl TryInto for ThresholdKeyshareState { } pub struct ThresholdKeyshareParams { - pub bus: EventManager, + pub bus: BusHandle, pub cipher: Arc, pub multithread: Addr, pub state: Persistable, } pub struct ThresholdKeyshare { - bus: EventManager, + bus: BusHandle, cipher: Arc, decryption_key_collector: Option>, multithread: Addr, diff --git a/crates/net/src/document_publisher.rs b/crates/net/src/document_publisher.rs index cd0aafe7db..2054fc36d8 100644 --- a/crates/net/src/document_publisher.rs +++ b/crates/net/src/document_publisher.rs @@ -15,7 +15,7 @@ use anyhow::Result; use chrono::{DateTime, Utc}; use e3_events::{ prelude::*, CiphernodeSelected, CorrelationId, DocumentKind, DocumentMeta, DocumentReceived, - E3RequestComplete, E3id, EnclaveErrorType, EnclaveEvent, EnclaveEventData, Event, EventManager, + E3RequestComplete, E3id, EnclaveErrorType, EnclaveEvent, EnclaveEventData, Event, BusHandle, PartyId, PublishDocumentRequested, ThresholdShareCreated, }; use e3_utils::retry::{retry_with_backoff, to_retry}; @@ -40,7 +40,7 @@ const KADEMLIA_BROADCAST_TIMEOUT: Duration = Duration::from_secs(30); /// bus pub struct DocumentPublisher { /// Enclave EventBus - bus: EventManager, + bus: BusHandle, /// NetCommand sender to forward commands to the NetInterface tx: mpsc::Sender, /// NetEvent receiver to resubscribe for events from the NetInterface. This is in an Arc so @@ -55,7 +55,7 @@ pub struct DocumentPublisher { impl DocumentPublisher { /// Create a new NetEventTranslator actor pub fn new( - bus: &EventManager, + bus: &BusHandle, tx: &mpsc::Sender, rx: &Arc>, topic: impl Into, @@ -81,7 +81,7 @@ impl DocumentPublisher { /// Setup the DocumentPublisher and start listening for GossipEvents pub fn setup( - bus: &EventManager, + bus: &BusHandle, tx: &mpsc::Sender, rx: &Arc>, topic: impl Into, @@ -253,7 +253,7 @@ pub async fn handle_publish_document_requested( pub async fn handle_document_published_notification( net_cmds: mpsc::Sender, net_events: Arc>, - bus: EventManager, + bus: BusHandle, ids: HashMap, event: DocumentPublishedNotification, ) -> Result<()> { @@ -374,7 +374,7 @@ async fn broadcast_document_published_notification( /// Convert between ThresholdShareCreated and DocumentPublished events pub struct EventConverter { - bus: EventManager, + bus: BusHandle, } #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -399,10 +399,10 @@ impl ReceivableDocument { } impl EventConverter { - pub fn new(bus: &EventManager) -> Self { + pub fn new(bus: &BusHandle) -> Self { Self { bus: bus.clone() } } - pub fn setup(bus: &EventManager) -> Addr { + pub fn setup(bus: &BusHandle) -> Addr { let addr = Self::new(bus).start(); bus.subscribe("ThresholdShareCreated", addr.clone().into()); bus.subscribe("DocumentReceived", addr.clone().into()); @@ -488,7 +488,7 @@ mod tests { use anyhow::{bail, Result}; use e3_events::{ CiphernodeSelected, DocumentKind, DocumentMeta, E3id, EnclaveError, EnclaveEvent, EventBus, - EventBusConfig, EventManager, GetEvents, HistoryCollector, PublishDocumentRequested, + EventBusConfig, BusHandle, GetEvents, HistoryCollector, PublishDocumentRequested, TakeEvents, }; use libp2p::kad::{GetRecordError, PutRecordError, RecordKey}; @@ -500,7 +500,7 @@ mod tests { fn setup_test() -> ( DefaultGuard, - EventManager, + BusHandle, mpsc::Sender, mpsc::Receiver, broadcast::Sender, diff --git a/crates/net/src/net_event_translator.rs b/crates/net/src/net_event_translator.rs index 9b30fef846..5585e0faf5 100644 --- a/crates/net/src/net_event_translator.rs +++ b/crates/net/src/net_event_translator.rs @@ -18,7 +18,7 @@ use e3_data::Repository; use e3_events::prelude::*; use e3_events::EnclaveEventData; use e3_events::Event; -use e3_events::EventManager; +use e3_events::BusHandle; use e3_events::{CorrelationId, EnclaveEvent, EventId}; use libp2p::identity::ed25519; use std::collections::HashSet; @@ -34,7 +34,7 @@ use tracing::{error, info, instrument, trace}; /// NetEventTranslator Actor converts between EventBus events and Libp2p events forwarding them to a /// NetInterface for propagation over the p2p network pub struct NetEventTranslator { - bus: EventManager, + bus: BusHandle, tx: mpsc::Sender, sent_events: HashSet, topic: String, @@ -52,7 +52,7 @@ struct LibP2pEvent(pub Vec); impl NetEventTranslator { /// Create a new NetEventTranslator actor pub fn new( - bus: &EventManager, + bus: &BusHandle, tx: &mpsc::Sender, topic: &str, ) -> Self { @@ -65,7 +65,7 @@ impl NetEventTranslator { } pub fn setup( - bus: &EventManager, + bus: &BusHandle, tx: &mpsc::Sender, rx: &Arc>, topic: &str, @@ -112,7 +112,7 @@ impl NetEventTranslator { /// Spawn a Libp2p interface and hook it up to this actor #[instrument(name = "libp2p", skip_all)] pub async fn setup_with_interface( - bus: EventManager, + bus: BusHandle, peers: Vec, cipher: &Arc, quic_port: u16, diff --git a/crates/request/src/router.rs b/crates/request/src/router.rs index b8f3e49d13..be11876667 100644 --- a/crates/request/src/router.rs +++ b/crates/request/src/router.rs @@ -23,7 +23,7 @@ use e3_data::Snapshot; use e3_events::prelude::*; use e3_events::E3RequestComplete; use e3_events::EnclaveEventData; -use e3_events::EventManager; +use e3_events::BusHandle; use e3_events::Shutdown; use e3_events::{E3id, EnclaveEvent, Event}; use serde::Deserialize; @@ -105,19 +105,19 @@ pub struct E3Router { /// A buffer for events to send to the buffer: EventBuffer, /// The EventBus - bus: EventManager, + bus: BusHandle, /// A repository for storing snapshots store: Repository, } pub struct E3RouterParams { extensions: Arc>>, - bus: EventManager, + bus: BusHandle, store: Repository, } impl E3Router { - pub fn builder(bus: &EventManager, store: DataStore) -> E3RouterBuilder { + pub fn builder(bus: &BusHandle, store: DataStore) -> E3RouterBuilder { let repositories = store.repositories(); let builder = E3RouterBuilder { bus: bus.clone(), @@ -282,7 +282,7 @@ impl FromSnapshotWithParams for E3Router { /// Builder for E3Router pub struct E3RouterBuilder { - pub bus: EventManager, + pub bus: BusHandle, pub extensions: Vec>, pub store: Repository, } diff --git a/crates/sortition/src/ciphernode_selector.rs b/crates/sortition/src/ciphernode_selector.rs index c5292d2b32..689c471257 100644 --- a/crates/sortition/src/ciphernode_selector.rs +++ b/crates/sortition/src/ciphernode_selector.rs @@ -12,13 +12,13 @@ use e3_config::StoreKeys; use e3_data::{DataStore, RepositoriesFactory}; use e3_events::{ prelude::*, CiphernodeSelected, CommitteeFinalized, E3Requested, EnclaveEvent, - EnclaveEventData, EventManager, Shutdown, TicketGenerated, TicketId, + EnclaveEventData, BusHandle, Shutdown, TicketGenerated, TicketId, }; use e3_request::MetaRepositoryFactory; use tracing::info; pub struct CiphernodeSelector { - bus: EventManager, + bus: BusHandle, sortition: Addr, address: String, data_store: DataStore, @@ -30,7 +30,7 @@ impl Actor for CiphernodeSelector { impl CiphernodeSelector { pub fn new( - bus: &EventManager, + bus: &BusHandle, sortition: &Addr, address: &str, data_store: &DataStore, @@ -44,7 +44,7 @@ impl CiphernodeSelector { } pub fn attach( - bus: &EventManager, + bus: &BusHandle, sortition: &Addr, address: &str, data_store: &DataStore, diff --git a/crates/sortition/src/sortition.rs b/crates/sortition/src/sortition.rs index 9a225edc0d..57794a2cb1 100644 --- a/crates/sortition/src/sortition.rs +++ b/crates/sortition/src/sortition.rs @@ -14,7 +14,7 @@ use e3_events::{ ConfigurationUpdated, EnclaveErrorType, EnclaveEvent, OperatorActivationChanged, PlaintextOutputPublished, Seed, Subscribe, TicketBalanceUpdated, }; -use e3_events::{EnclaveEventData, EventManager}; +use e3_events::{EnclaveEventData, BusHandle}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use tracing::info; @@ -139,7 +139,7 @@ pub struct Sortition { /// Persistent map of `chain_id -> NodeStateStore`. node_state: Persistable>, /// Event bus for error reporting and enclave event subscription. - bus: EventManager, + bus: BusHandle, /// Persistent map of finalized committees per E3 finalized_committees: Persistable>>, } @@ -148,7 +148,7 @@ pub struct Sortition { #[derive(Debug)] pub struct SortitionParams { /// Event bus address. - pub bus: EventManager, + pub bus: BusHandle, /// Persisted per-chain backend map. pub backends: Persistable>, /// Node state store per chain @@ -169,7 +169,7 @@ impl Sortition { #[instrument(name = "sortition_attach", skip_all)] pub async fn attach( - bus: &EventManager, + bus: &BusHandle, backends_store: Repository>, node_state_store: Repository>, committees_store: Repository>>, diff --git a/crates/test-helpers/src/ciphernode_system.rs b/crates/test-helpers/src/ciphernode_system.rs index 6cdebb4d3c..d1f6bef06b 100644 --- a/crates/test-helpers/src/ciphernode_system.rs +++ b/crates/test-helpers/src/ciphernode_system.rs @@ -197,7 +197,7 @@ mod tests { use super::*; use actix::prelude::*; use e3_data::InMemStore; - use e3_events::{EventBus, EventBusConfig, EventManager}; + use e3_events::{EventBus, EventBusConfig, BusHandle}; async fn mock_setup_node(address: String) -> Result { // Create mock actors for the test @@ -205,7 +205,7 @@ mod tests { let bus = EventBus::::new(EventBusConfig { deduplicate: true }).start(); let history = EventBus::::history(&bus); let errors = EventBus::::error(&bus); - let bus = EventManager::new(bus); + let bus = BusHandle::new(bus); Ok(CiphernodeHandle { address, diff --git a/crates/test-helpers/src/lib.rs b/crates/test-helpers/src/lib.rs index 65ff87f854..79a2512ebb 100644 --- a/crates/test-helpers/src/lib.rs +++ b/crates/test-helpers/src/lib.rs @@ -16,7 +16,7 @@ use anyhow::*; use e3_ciphernode_builder::CiphernodeHandle; use e3_events::{ CiphernodeAdded, EnclaveEvent, EnclaveEventData, EventBus, EventBusConfig, EventDispatcher, - EventManager, HistoryCollector, Seed, Subscribe, + BusHandle, HistoryCollector, Seed, Subscribe, }; use e3_fhe::{create_crp, setup_crp_params, ParamsWithCrp}; use e3_net::{DocumentPublisher, NetEventTranslator}; @@ -72,7 +72,7 @@ pub fn create_crp_bytes_params( pub fn get_common_setup( param_set: Option, ) -> Result<( - EventManager, + BusHandle, SharedRng, Seed, Arc, @@ -159,13 +159,13 @@ pub fn create_random_eth_addrs(how_many: u32) -> Vec { /// Test helper to add addresses to the committee by creating events on the event bus #[derive(Clone, Debug)] pub struct AddToCommittee { - bus: EventManager, + bus: BusHandle, count: usize, chain_id: u64, } impl AddToCommittee { - pub fn new(bus: &EventManager, chain_id: u64) -> Self { + pub fn new(bus: &BusHandle, chain_id: u64) -> Self { Self { bus: bus.clone(), chain_id, diff --git a/crates/test-helpers/src/plaintext_writer.rs b/crates/test-helpers/src/plaintext_writer.rs index 49acbe9bb9..1875dd3b3f 100644 --- a/crates/test-helpers/src/plaintext_writer.rs +++ b/crates/test-helpers/src/plaintext_writer.rs @@ -8,7 +8,7 @@ use std::path::PathBuf; use super::write_file_with_dirs; use actix::{Actor, Addr, Context, Handler}; -use e3_events::{prelude::*, EnclaveEvent, EnclaveEventData, EventManager}; +use e3_events::{prelude::*, EnclaveEvent, EnclaveEventData, BusHandle}; use e3_sdk::bfv_helpers::decode_bytes_to_vec_u64; use tracing::{error, info}; @@ -17,7 +17,7 @@ pub struct PlaintextWriter { } impl PlaintextWriter { - pub fn attach(path: &PathBuf, bus: EventManager) -> Addr { + pub fn attach(path: &PathBuf, bus: BusHandle) -> Addr { let addr = Self { path: path.to_owned(), } diff --git a/crates/test-helpers/src/public_key_writer.rs b/crates/test-helpers/src/public_key_writer.rs index 4c12151c11..e3e1efba1b 100644 --- a/crates/test-helpers/src/public_key_writer.rs +++ b/crates/test-helpers/src/public_key_writer.rs @@ -8,7 +8,7 @@ use std::path::PathBuf; use super::write_file_with_dirs; use actix::{Actor, Addr, Context, Handler}; -use e3_events::{prelude::*, EnclaveEvent, EnclaveEventData, EventManager, EventSubscriber}; +use e3_events::{prelude::*, EnclaveEvent, EnclaveEventData, BusHandle, EventSubscriber}; use tracing::info; pub struct PublicKeyWriter { @@ -16,7 +16,7 @@ pub struct PublicKeyWriter { } impl PublicKeyWriter { - pub fn attach(path: &PathBuf, bus: EventManager) -> Addr { + pub fn attach(path: &PathBuf, bus: BusHandle) -> Addr { let addr = Self { path: path.to_owned(), } diff --git a/crates/tests/tests/integration.rs b/crates/tests/tests/integration.rs index d8ae26dc27..8c4ed528ad 100644 --- a/crates/tests/tests/integration.rs +++ b/crates/tests/tests/integration.rs @@ -11,7 +11,7 @@ use e3_ciphernode_builder::CiphernodeBuilder; use e3_crypto::Cipher; use e3_events::{ prelude::*, CiphertextOutputPublished, CommitteeFinalized, ConfigurationUpdated, E3Requested, - E3id, EnclaveEvent, EnclaveEventData, EventBus, EventBusConfig, EventManager, + E3id, EnclaveEvent, EnclaveEventData, EventBus, EventBusConfig, BusHandle, OperatorActivationChanged, PlaintextAggregated, TicketBalanceUpdated, }; use e3_multithread::{GetReport, Multithread}; @@ -33,7 +33,7 @@ pub fn save_snapshot(file_name: &str, bytes: &[u8]) { } async fn setup_score_sortition_environment( - bus: &EventManager, + bus: &BusHandle, eth_addrs: &Vec, chain_id: u64, ) -> Result<()> { @@ -118,7 +118,7 @@ async fn test_trbfv_actor() -> Result<()> { let rng = create_shared_rng_from_u64(42); // Create "trigger" bus - let bus: EventManager = + let bus: BusHandle = EventBus::::new(EventBusConfig { deduplicate: true }) .start() .into(); diff --git a/crates/tests/tests/integration_legacy.rs b/crates/tests/tests/integration_legacy.rs index fb7e3facf8..dd4dbb12aa 100644 --- a/crates/tests/tests/integration_legacy.rs +++ b/crates/tests/tests/integration_legacy.rs @@ -14,7 +14,7 @@ use e3_crypto::Cipher; use e3_data::GetDump; use e3_data::InMemStore; use e3_events::EnclaveEventData; -use e3_events::EventManager; +use e3_events::BusHandle; use e3_events::GetEvents; use e3_events::{ prelude::*, CiphernodeSelected, CiphertextOutputPublished, CommitteeFinalized, @@ -47,7 +47,7 @@ use tokio::sync::{broadcast, Mutex}; use tokio::time::sleep; async fn setup_local_ciphernode( - bus: &EventManager, + bus: &BusHandle, rng: &SharedRng, logging: bool, addr: &str, @@ -102,7 +102,7 @@ fn generate_pk_shares( } async fn create_local_ciphernodes( - bus: &EventManager, + bus: &BusHandle, rng: &SharedRng, count: u32, cipher: &Arc, @@ -120,7 +120,7 @@ async fn create_local_ciphernodes( } async fn setup_score_sortition_environment( - bus: &EventManager, + bus: &BusHandle, eth_addrs: &Vec, chain_id: u64, ) -> Result<()> { @@ -443,7 +443,7 @@ async fn test_p2p_actor_forwards_events_to_network() -> Result<()> { // Setup elements in test let (cmd_tx, mut cmd_rx) = mpsc::channel(100); // Transmit byte events to the network let (event_tx, _) = broadcast::channel(100); // Receive byte events from the network - let bus: EventManager = + let bus: BusHandle = EventBus::::new(EventBusConfig { deduplicate: true }) .start() .into(); @@ -620,7 +620,7 @@ async fn test_p2p_actor_forwards_events_to_bus() -> Result<()> { // Setup elements in test let (cmd_tx, _) = mpsc::channel(100); // Transmit byte events to the network let (event_tx, event_rx) = broadcast::channel(100); // Receive byte events from the network - let bus: EventManager = + let bus: BusHandle = EventBus::::new(EventBusConfig { deduplicate: true }) .start() .into(); From cf54412a852ad1480a489a729f6fe1d36e788399 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 17:24:13 +0000 Subject: [PATCH 16/36] format --- crates/aggregator/src/committee_finalizer.rs | 4 ++-- crates/aggregator/src/ext.rs | 2 +- crates/aggregator/src/plaintext_aggregator.rs | 2 +- crates/aggregator/src/publickey_aggregator.rs | 4 ++-- .../aggregator/src/threshold_plaintext_aggregator.rs | 4 ++-- crates/ciphernode-builder/src/ciphernode.rs | 2 +- crates/data/src/sled_store.rs | 4 ++-- crates/entrypoint/src/helpers/datastore.rs | 2 +- crates/entrypoint/src/helpers/shutdown.rs | 7 ++----- crates/entrypoint/src/start/aggregator_start.rs | 2 +- crates/events/src/eventbus.rs | 3 +-- crates/events/src/eventbus_factory.rs | 2 +- crates/evm/src/bonding_registry_sol.rs | 2 +- crates/evm/src/ciphernode_registry_sol.rs | 6 +++--- crates/evm/src/enclave_sol.rs | 2 +- crates/evm/src/enclave_sol_reader.rs | 2 +- crates/evm/src/enclave_sol_writer.rs | 2 +- crates/evm/src/event_reader.rs | 2 +- crates/evm/src/historical_event_coordinator.rs | 2 +- crates/keyshare/src/ext.rs | 8 ++------ crates/keyshare/src/keyshare.rs | 4 ++-- crates/keyshare/src/threshold_keyshare.rs | 4 ++-- crates/net/src/document_publisher.rs | 12 ++++++------ crates/net/src/net_event_translator.rs | 8 ++------ crates/request/src/router.rs | 2 +- crates/sortition/src/ciphernode_selector.rs | 4 ++-- crates/sortition/src/sortition.rs | 2 +- crates/test-helpers/src/ciphernode_system.rs | 2 +- crates/test-helpers/src/lib.rs | 4 ++-- crates/test-helpers/src/plaintext_writer.rs | 2 +- crates/test-helpers/src/public_key_writer.rs | 2 +- crates/tests/tests/integration.rs | 4 ++-- crates/tests/tests/integration_legacy.rs | 2 +- 33 files changed, 52 insertions(+), 64 deletions(-) diff --git a/crates/aggregator/src/committee_finalizer.rs b/crates/aggregator/src/committee_finalizer.rs index ef85620c22..ef4ed1aa96 100644 --- a/crates/aggregator/src/committee_finalizer.rs +++ b/crates/aggregator/src/committee_finalizer.rs @@ -6,8 +6,8 @@ use actix::prelude::*; use e3_events::{ - prelude::*, CommitteeFinalizeRequested, CommitteeRequested, EnclaveEvent, EnclaveEventData, - BusHandle, Shutdown, + prelude::*, BusHandle, CommitteeFinalizeRequested, CommitteeRequested, EnclaveEvent, + EnclaveEventData, Shutdown, }; use std::collections::HashMap; use std::time::Duration; diff --git a/crates/aggregator/src/ext.rs b/crates/aggregator/src/ext.rs index 1688b447a4..e6cfe68497 100644 --- a/crates/aggregator/src/ext.rs +++ b/crates/aggregator/src/ext.rs @@ -16,7 +16,7 @@ use anyhow::{anyhow, Result}; use async_trait::async_trait; use e3_data::{AutoPersist, RepositoriesFactory}; use e3_events::prelude::*; -use e3_events::{EnclaveErrorType, EnclaveEvent, EnclaveEventData, BusHandle}; +use e3_events::{BusHandle, EnclaveErrorType, EnclaveEvent, EnclaveEventData}; use e3_fhe::ext::FHE_KEY; use e3_multithread::Multithread; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, META_KEY}; diff --git a/crates/aggregator/src/plaintext_aggregator.rs b/crates/aggregator/src/plaintext_aggregator.rs index 744e7e3d06..c487c7d1cf 100644 --- a/crates/aggregator/src/plaintext_aggregator.rs +++ b/crates/aggregator/src/plaintext_aggregator.rs @@ -8,7 +8,7 @@ use actix::prelude::*; use anyhow::Result; use e3_data::Persistable; use e3_events::{ - prelude::*, DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, BusHandle, + prelude::*, BusHandle, DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, OrderedSet, PlaintextAggregated, Seed, }; use e3_fhe::{Fhe, GetAggregatePlaintext}; diff --git a/crates/aggregator/src/publickey_aggregator.rs b/crates/aggregator/src/publickey_aggregator.rs index ed28e14a78..062b87ad9c 100644 --- a/crates/aggregator/src/publickey_aggregator.rs +++ b/crates/aggregator/src/publickey_aggregator.rs @@ -8,8 +8,8 @@ use actix::prelude::*; use anyhow::Result; use e3_data::Persistable; use e3_events::{ - prelude::*, Die, E3id, EnclaveEvent, EnclaveEventData, BusHandle, KeyshareCreated, - OrderedSet, PublicKeyAggregated, Seed, + prelude::*, BusHandle, Die, E3id, EnclaveEvent, EnclaveEventData, KeyshareCreated, OrderedSet, + PublicKeyAggregated, Seed, }; use e3_fhe::{Fhe, GetAggregatePublicKey}; use e3_sortition::{GetNodesForE3, Sortition}; diff --git a/crates/aggregator/src/threshold_plaintext_aggregator.rs b/crates/aggregator/src/threshold_plaintext_aggregator.rs index 68bcb7f9ae..c4f3240b57 100644 --- a/crates/aggregator/src/threshold_plaintext_aggregator.rs +++ b/crates/aggregator/src/threshold_plaintext_aggregator.rs @@ -10,8 +10,8 @@ use actix::prelude::*; use anyhow::{anyhow, bail, Result}; use e3_data::Persistable; use e3_events::{ - prelude::*, ComputeRequest, DecryptionshareCreated, Die, E3id, EnclaveEvent, EnclaveEventData, - BusHandle, PlaintextAggregated, Seed, + prelude::*, BusHandle, ComputeRequest, DecryptionshareCreated, Die, E3id, EnclaveEvent, + EnclaveEventData, PlaintextAggregated, Seed, }; use e3_multithread::Multithread; use e3_sortition::{GetNodesForE3, Sortition}; diff --git a/crates/ciphernode-builder/src/ciphernode.rs b/crates/ciphernode-builder/src/ciphernode.rs index 9f1bfa5342..6bf9775d47 100644 --- a/crates/ciphernode-builder/src/ciphernode.rs +++ b/crates/ciphernode-builder/src/ciphernode.rs @@ -6,7 +6,7 @@ use actix::Addr; use e3_data::{DataStore, InMemStore, StoreAddr}; -use e3_events::{EnclaveEvent, EventBus, BusHandle, HistoryCollector}; +use e3_events::{BusHandle, EnclaveEvent, EventBus, HistoryCollector}; #[derive(Clone, Debug)] pub struct CiphernodeHandle { diff --git a/crates/data/src/sled_store.rs b/crates/data/src/sled_store.rs index e63414f5be..78dcaf9dec 100644 --- a/crates/data/src/sled_store.rs +++ b/crates/data/src/sled_store.rs @@ -8,8 +8,8 @@ use crate::{Get, Insert, InsertSync, Remove}; use actix::{Actor, ActorContext, Addr, Handler}; use anyhow::{Context, Result}; use e3_events::{ - get_enclave_event_manager, prelude::*, EnclaveErrorType, EnclaveEvent, EnclaveEventData, - BusHandle, + get_enclave_event_manager, prelude::*, BusHandle, EnclaveErrorType, EnclaveEvent, + EnclaveEventData, }; use once_cell::sync::Lazy; use sled::Db; diff --git a/crates/entrypoint/src/helpers/datastore.rs b/crates/entrypoint/src/helpers/datastore.rs index 6f2d70c5b3..75f8295fdc 100644 --- a/crates/entrypoint/src/helpers/datastore.rs +++ b/crates/entrypoint/src/helpers/datastore.rs @@ -11,7 +11,7 @@ use anyhow::Result; use e3_config::AppConfig; use e3_data::{DataStore, InMemStore, SledDb, SledStore}; use e3_data::{Repositories, RepositoriesFactory}; -use e3_events::{get_enclave_event_manager, EnclaveEvent, BusHandle}; +use e3_events::{get_enclave_event_manager, BusHandle, EnclaveEvent}; pub fn get_sled_store(bus: &BusHandle, db_file: &PathBuf) -> Result { Ok((&SledStore::new(bus, db_file)?).into()) diff --git a/crates/entrypoint/src/helpers/shutdown.rs b/crates/entrypoint/src/helpers/shutdown.rs index 1406adecef..8d1b0174f3 100644 --- a/crates/entrypoint/src/helpers/shutdown.rs +++ b/crates/entrypoint/src/helpers/shutdown.rs @@ -5,7 +5,7 @@ // or FITNESS FOR A PARTICULAR PURPOSE. use anyhow::Result; -use e3_events::{prelude::*, EnclaveEvent, BusHandle, Shutdown}; +use e3_events::{prelude::*, BusHandle, EnclaveEvent, Shutdown}; use std::time::Duration; use tokio::{ select, @@ -14,10 +14,7 @@ use tokio::{ }; use tracing::{error, info}; -pub async fn listen_for_shutdown( - bus: BusHandle, - mut handle: JoinHandle>, -) { +pub async fn listen_for_shutdown(bus: BusHandle, mut handle: JoinHandle>) { let mut sigterm = signal(SignalKind::terminate()).expect("Failed to create SIGTERM signal stream"); select! { diff --git a/crates/entrypoint/src/start/aggregator_start.rs b/crates/entrypoint/src/start/aggregator_start.rs index bee9d484ab..3d42a8653a 100644 --- a/crates/entrypoint/src/start/aggregator_start.rs +++ b/crates/entrypoint/src/start/aggregator_start.rs @@ -9,7 +9,7 @@ use e3_ciphernode_builder::CiphernodeBuilder; use e3_config::AppConfig; use e3_crypto::Cipher; use e3_data::RepositoriesFactory; -use e3_events::{get_enclave_event_manager, EnclaveEvent, BusHandle}; +use e3_events::{get_enclave_event_manager, BusHandle, EnclaveEvent}; use e3_net::{NetEventTranslator, NetRepositoryFactory}; use e3_test_helpers::{PlaintextWriter, PublicKeyWriter}; use rand::SeedableRng; diff --git a/crates/events/src/eventbus.rs b/crates/events/src/eventbus.rs index 9a729c4dac..8954e6bbf9 100644 --- a/crates/events/src/eventbus.rs +++ b/crates/events/src/eventbus.rs @@ -421,8 +421,7 @@ impl Handler for HistoryCollector { ////////////////////////////////////////////////////////////////////////////// /// Function to help with testing when we want to maintain a vec of events -pub fn new_event_bus_with_history() -> (BusHandle, Addr>) -{ +pub fn new_event_bus_with_history() -> (BusHandle, Addr>) { let bus: BusHandle = EventBus::::default().start().into(); let history = HistoryCollector::new().start(); diff --git a/crates/events/src/eventbus_factory.rs b/crates/events/src/eventbus_factory.rs index f4469ce111..6f0467ab13 100644 --- a/crates/events/src/eventbus_factory.rs +++ b/crates/events/src/eventbus_factory.rs @@ -13,9 +13,9 @@ use std::collections::HashMap; use std::sync::Mutex; use crate::traits::Event; +use crate::BusHandle; use crate::EnclaveEvent; use crate::EventBus; -use crate::BusHandle; use crate::HistoryCollector; use crate::Subscribe; diff --git a/crates/evm/src/bonding_registry_sol.rs b/crates/evm/src/bonding_registry_sol.rs index 0470adad24..af199634ae 100644 --- a/crates/evm/src/bonding_registry_sol.rs +++ b/crates/evm/src/bonding_registry_sol.rs @@ -16,7 +16,7 @@ use alloy::{ }; use anyhow::Result; use e3_data::Repository; -use e3_events::{EnclaveEvent, EnclaveEventData, BusHandle}; +use e3_events::{BusHandle, EnclaveEvent, EnclaveEventData}; use tracing::{error, info, trace}; sol!( diff --git a/crates/evm/src/ciphernode_registry_sol.rs b/crates/evm/src/ciphernode_registry_sol.rs index 228b0e627c..a77e635032 100644 --- a/crates/evm/src/ciphernode_registry_sol.rs +++ b/crates/evm/src/ciphernode_registry_sol.rs @@ -18,9 +18,9 @@ use alloy::{ use anyhow::Result; use e3_data::Repository; use e3_events::{ - prelude::*, CommitteeFinalizeRequested, CommitteeFinalized, E3id, EnclaveErrorType, - EnclaveEvent, EnclaveEventData, BusHandle, EventSubscriber, OrderedSet, PublicKeyAggregated, - Seed, Shutdown, TicketGenerated, TicketId, + prelude::*, BusHandle, CommitteeFinalizeRequested, CommitteeFinalized, E3id, EnclaveErrorType, + EnclaveEvent, EnclaveEventData, EventSubscriber, OrderedSet, PublicKeyAggregated, Seed, + Shutdown, TicketGenerated, TicketId, }; use tracing::{error, info, trace}; diff --git a/crates/evm/src/enclave_sol.rs b/crates/evm/src/enclave_sol.rs index 70fd7b42b1..dbb4821960 100644 --- a/crates/evm/src/enclave_sol.rs +++ b/crates/evm/src/enclave_sol.rs @@ -12,7 +12,7 @@ use actix::{Addr, Recipient}; use alloy::providers::{Provider, WalletProvider}; use anyhow::Result; use e3_data::Repository; -use e3_events::{EnclaveEvent, EventBus, BusHandle}; +use e3_events::{BusHandle, EnclaveEvent, EventBus}; pub struct EnclaveSol; diff --git a/crates/evm/src/enclave_sol_reader.rs b/crates/evm/src/enclave_sol_reader.rs index eac66d09c3..aa425cd91d 100644 --- a/crates/evm/src/enclave_sol_reader.rs +++ b/crates/evm/src/enclave_sol_reader.rs @@ -13,7 +13,7 @@ use alloy::providers::Provider; use alloy::{sol, sol_types::SolEvent}; use anyhow::Result; use e3_data::Repository; -use e3_events::{prelude::*, E3id, EnclaveEvent, EnclaveEventData, BusHandle}; +use e3_events::{prelude::*, BusHandle, E3id, EnclaveEvent, EnclaveEventData}; use e3_utils::utility_types::ArcBytes; use num_bigint::BigUint; use tracing::{error, info, trace}; diff --git a/crates/evm/src/enclave_sol_writer.rs b/crates/evm/src/enclave_sol_writer.rs index 8836ac46ad..086bb59444 100644 --- a/crates/evm/src/enclave_sol_writer.rs +++ b/crates/evm/src/enclave_sol_writer.rs @@ -18,9 +18,9 @@ use alloy::{ }; use anyhow::Result; use e3_events::prelude::*; +use e3_events::BusHandle; use e3_events::EnclaveEvent; use e3_events::EnclaveEventData; -use e3_events::BusHandle; use e3_events::Shutdown; use e3_events::{E3id, EnclaveErrorType, PlaintextAggregated, Subscribe}; use tracing::info; diff --git a/crates/evm/src/event_reader.rs b/crates/evm/src/event_reader.rs index 8fc4031526..eec8f1e463 100644 --- a/crates/evm/src/event_reader.rs +++ b/crates/evm/src/event_reader.rs @@ -15,7 +15,7 @@ use alloy::rpc::types::Filter; use anyhow::{anyhow, Result}; use e3_data::{AutoPersist, Persistable, Repository}; use e3_events::{prelude::*, EnclaveErrorType, EnclaveEvent, EnclaveEventData, EventId}; -use e3_events::{Event, BusHandle}; +use e3_events::{BusHandle, Event}; use futures_util::stream::StreamExt; use std::collections::HashSet; use tokio::select; diff --git a/crates/evm/src/historical_event_coordinator.rs b/crates/evm/src/historical_event_coordinator.rs index 3e3c2ce02b..ba9e181f32 100644 --- a/crates/evm/src/historical_event_coordinator.rs +++ b/crates/evm/src/historical_event_coordinator.rs @@ -6,7 +6,7 @@ use crate::EnclaveEvmEvent; use actix::prelude::*; -use e3_events::{prelude::*, EnclaveEvent, EnclaveEventData, BusHandle}; +use e3_events::{prelude::*, BusHandle, EnclaveEvent, EnclaveEventData}; use tracing::info; #[derive(Clone)] diff --git a/crates/keyshare/src/ext.rs b/crates/keyshare/src/ext.rs index da6efff127..c7bb8c95f4 100644 --- a/crates/keyshare/src/ext.rs +++ b/crates/keyshare/src/ext.rs @@ -13,7 +13,7 @@ use anyhow::{anyhow, Result}; use async_trait::async_trait; use e3_crypto::Cipher; use e3_data::{AutoPersist, RepositoriesFactory}; -use e3_events::{prelude::*, EnclaveErrorType, EnclaveEvent, EnclaveEventData, BusHandle}; +use e3_events::{prelude::*, BusHandle, EnclaveErrorType, EnclaveEvent, EnclaveEventData}; use e3_fhe::ext::FHE_KEY; use e3_multithread::Multithread; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, META_KEY}; @@ -26,11 +26,7 @@ pub struct KeyshareExtension { } impl KeyshareExtension { - pub fn create( - bus: &BusHandle, - address: &str, - cipher: &Arc, - ) -> Box { + pub fn create(bus: &BusHandle, address: &str, cipher: &Arc) -> Box { Box::new(Self { bus: bus.clone(), address: address.to_owned(), diff --git a/crates/keyshare/src/keyshare.rs b/crates/keyshare/src/keyshare.rs index 4d7c056d4d..edddb0ccf9 100644 --- a/crates/keyshare/src/keyshare.rs +++ b/crates/keyshare/src/keyshare.rs @@ -9,8 +9,8 @@ use anyhow::{anyhow, Result}; use e3_crypto::Cipher; use e3_data::Persistable; use e3_events::{ - prelude::*, CiphernodeSelected, CiphertextOutputPublished, DecryptionshareCreated, Die, - E3RequestComplete, EnclaveErrorType, EnclaveEvent, EnclaveEventData, BusHandle, FromError, + prelude::*, BusHandle, CiphernodeSelected, CiphertextOutputPublished, DecryptionshareCreated, + Die, E3RequestComplete, EnclaveErrorType, EnclaveEvent, EnclaveEventData, FromError, KeyshareCreated, }; use e3_fhe::{DecryptCiphertext, Fhe}; diff --git a/crates/keyshare/src/threshold_keyshare.rs b/crates/keyshare/src/threshold_keyshare.rs index 7bd3443ee8..7dc606655a 100644 --- a/crates/keyshare/src/threshold_keyshare.rs +++ b/crates/keyshare/src/threshold_keyshare.rs @@ -9,8 +9,8 @@ use anyhow::{anyhow, bail, Result}; use e3_crypto::{Cipher, SensitiveBytes}; use e3_data::Persistable; use e3_events::{ - prelude::*, CiphernodeSelected, CiphertextOutputPublished, ComputeRequest, ComputeResponse, - DecryptionshareCreated, E3id, EnclaveEvent, EnclaveEventData, BusHandle, KeyshareCreated, + prelude::*, BusHandle, CiphernodeSelected, CiphertextOutputPublished, ComputeRequest, + ComputeResponse, DecryptionshareCreated, E3id, EnclaveEvent, EnclaveEventData, KeyshareCreated, PartyId, ThresholdShare, ThresholdShareCreated, }; use e3_fhe::create_crp; diff --git a/crates/net/src/document_publisher.rs b/crates/net/src/document_publisher.rs index 2054fc36d8..86a3effa91 100644 --- a/crates/net/src/document_publisher.rs +++ b/crates/net/src/document_publisher.rs @@ -14,9 +14,9 @@ use actix::prelude::*; use anyhow::Result; use chrono::{DateTime, Utc}; use e3_events::{ - prelude::*, CiphernodeSelected, CorrelationId, DocumentKind, DocumentMeta, DocumentReceived, - E3RequestComplete, E3id, EnclaveErrorType, EnclaveEvent, EnclaveEventData, Event, BusHandle, - PartyId, PublishDocumentRequested, ThresholdShareCreated, + prelude::*, BusHandle, CiphernodeSelected, CorrelationId, DocumentKind, DocumentMeta, + DocumentReceived, E3RequestComplete, E3id, EnclaveErrorType, EnclaveEvent, EnclaveEventData, + Event, PartyId, PublishDocumentRequested, ThresholdShareCreated, }; use e3_utils::retry::{retry_with_backoff, to_retry}; use e3_utils::ArcBytes; @@ -487,9 +487,9 @@ mod tests { use actix::Addr; use anyhow::{bail, Result}; use e3_events::{ - CiphernodeSelected, DocumentKind, DocumentMeta, E3id, EnclaveError, EnclaveEvent, EventBus, - EventBusConfig, BusHandle, GetEvents, HistoryCollector, PublishDocumentRequested, - TakeEvents, + BusHandle, CiphernodeSelected, DocumentKind, DocumentMeta, E3id, EnclaveError, + EnclaveEvent, EventBus, EventBusConfig, GetEvents, HistoryCollector, + PublishDocumentRequested, TakeEvents, }; use libp2p::kad::{GetRecordError, PutRecordError, RecordKey}; use tokio::{ diff --git a/crates/net/src/net_event_translator.rs b/crates/net/src/net_event_translator.rs index 5585e0faf5..f23905184f 100644 --- a/crates/net/src/net_event_translator.rs +++ b/crates/net/src/net_event_translator.rs @@ -16,9 +16,9 @@ use anyhow::{bail, Result}; use e3_crypto::Cipher; use e3_data::Repository; use e3_events::prelude::*; +use e3_events::BusHandle; use e3_events::EnclaveEventData; use e3_events::Event; -use e3_events::BusHandle; use e3_events::{CorrelationId, EnclaveEvent, EventId}; use libp2p::identity::ed25519; use std::collections::HashSet; @@ -51,11 +51,7 @@ struct LibP2pEvent(pub Vec); impl NetEventTranslator { /// Create a new NetEventTranslator actor - pub fn new( - bus: &BusHandle, - tx: &mpsc::Sender, - topic: &str, - ) -> Self { + pub fn new(bus: &BusHandle, tx: &mpsc::Sender, topic: &str) -> Self { Self { bus: bus.clone(), tx: tx.clone(), diff --git a/crates/request/src/router.rs b/crates/request/src/router.rs index be11876667..402c175740 100644 --- a/crates/request/src/router.rs +++ b/crates/request/src/router.rs @@ -21,9 +21,9 @@ use e3_data::RepositoriesFactory; use e3_data::Repository; use e3_data::Snapshot; use e3_events::prelude::*; +use e3_events::BusHandle; use e3_events::E3RequestComplete; use e3_events::EnclaveEventData; -use e3_events::BusHandle; use e3_events::Shutdown; use e3_events::{E3id, EnclaveEvent, Event}; use serde::Deserialize; diff --git a/crates/sortition/src/ciphernode_selector.rs b/crates/sortition/src/ciphernode_selector.rs index 689c471257..0a2292dad1 100644 --- a/crates/sortition/src/ciphernode_selector.rs +++ b/crates/sortition/src/ciphernode_selector.rs @@ -11,8 +11,8 @@ use actix::prelude::*; use e3_config::StoreKeys; use e3_data::{DataStore, RepositoriesFactory}; use e3_events::{ - prelude::*, CiphernodeSelected, CommitteeFinalized, E3Requested, EnclaveEvent, - EnclaveEventData, BusHandle, Shutdown, TicketGenerated, TicketId, + prelude::*, BusHandle, CiphernodeSelected, CommitteeFinalized, E3Requested, EnclaveEvent, + EnclaveEventData, Shutdown, TicketGenerated, TicketId, }; use e3_request::MetaRepositoryFactory; use tracing::info; diff --git a/crates/sortition/src/sortition.rs b/crates/sortition/src/sortition.rs index 57794a2cb1..de674fec58 100644 --- a/crates/sortition/src/sortition.rs +++ b/crates/sortition/src/sortition.rs @@ -14,7 +14,7 @@ use e3_events::{ ConfigurationUpdated, EnclaveErrorType, EnclaveEvent, OperatorActivationChanged, PlaintextOutputPublished, Seed, Subscribe, TicketBalanceUpdated, }; -use e3_events::{EnclaveEventData, BusHandle}; +use e3_events::{BusHandle, EnclaveEventData}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use tracing::info; diff --git a/crates/test-helpers/src/ciphernode_system.rs b/crates/test-helpers/src/ciphernode_system.rs index d1f6bef06b..dd62af9ca5 100644 --- a/crates/test-helpers/src/ciphernode_system.rs +++ b/crates/test-helpers/src/ciphernode_system.rs @@ -197,7 +197,7 @@ mod tests { use super::*; use actix::prelude::*; use e3_data::InMemStore; - use e3_events::{EventBus, EventBusConfig, BusHandle}; + use e3_events::{BusHandle, EventBus, EventBusConfig}; async fn mock_setup_node(address: String) -> Result { // Create mock actors for the test diff --git a/crates/test-helpers/src/lib.rs b/crates/test-helpers/src/lib.rs index 79a2512ebb..0fe346be7d 100644 --- a/crates/test-helpers/src/lib.rs +++ b/crates/test-helpers/src/lib.rs @@ -15,8 +15,8 @@ use alloy::primitives::Address; use anyhow::*; use e3_ciphernode_builder::CiphernodeHandle; use e3_events::{ - CiphernodeAdded, EnclaveEvent, EnclaveEventData, EventBus, EventBusConfig, EventDispatcher, - BusHandle, HistoryCollector, Seed, Subscribe, + BusHandle, CiphernodeAdded, EnclaveEvent, EnclaveEventData, EventBus, EventBusConfig, + EventDispatcher, HistoryCollector, Seed, Subscribe, }; use e3_fhe::{create_crp, setup_crp_params, ParamsWithCrp}; use e3_net::{DocumentPublisher, NetEventTranslator}; diff --git a/crates/test-helpers/src/plaintext_writer.rs b/crates/test-helpers/src/plaintext_writer.rs index 1875dd3b3f..5e77a12a4e 100644 --- a/crates/test-helpers/src/plaintext_writer.rs +++ b/crates/test-helpers/src/plaintext_writer.rs @@ -8,7 +8,7 @@ use std::path::PathBuf; use super::write_file_with_dirs; use actix::{Actor, Addr, Context, Handler}; -use e3_events::{prelude::*, EnclaveEvent, EnclaveEventData, BusHandle}; +use e3_events::{prelude::*, BusHandle, EnclaveEvent, EnclaveEventData}; use e3_sdk::bfv_helpers::decode_bytes_to_vec_u64; use tracing::{error, info}; diff --git a/crates/test-helpers/src/public_key_writer.rs b/crates/test-helpers/src/public_key_writer.rs index e3e1efba1b..bdd2523725 100644 --- a/crates/test-helpers/src/public_key_writer.rs +++ b/crates/test-helpers/src/public_key_writer.rs @@ -8,7 +8,7 @@ use std::path::PathBuf; use super::write_file_with_dirs; use actix::{Actor, Addr, Context, Handler}; -use e3_events::{prelude::*, EnclaveEvent, EnclaveEventData, BusHandle, EventSubscriber}; +use e3_events::{prelude::*, BusHandle, EnclaveEvent, EnclaveEventData, EventSubscriber}; use tracing::info; pub struct PublicKeyWriter { diff --git a/crates/tests/tests/integration.rs b/crates/tests/tests/integration.rs index 8c4ed528ad..33f58a9cea 100644 --- a/crates/tests/tests/integration.rs +++ b/crates/tests/tests/integration.rs @@ -10,8 +10,8 @@ use anyhow::{bail, Result}; use e3_ciphernode_builder::CiphernodeBuilder; use e3_crypto::Cipher; use e3_events::{ - prelude::*, CiphertextOutputPublished, CommitteeFinalized, ConfigurationUpdated, E3Requested, - E3id, EnclaveEvent, EnclaveEventData, EventBus, EventBusConfig, BusHandle, + prelude::*, BusHandle, CiphertextOutputPublished, CommitteeFinalized, ConfigurationUpdated, + E3Requested, E3id, EnclaveEvent, EnclaveEventData, EventBus, EventBusConfig, OperatorActivationChanged, PlaintextAggregated, TicketBalanceUpdated, }; use e3_multithread::{GetReport, Multithread}; diff --git a/crates/tests/tests/integration_legacy.rs b/crates/tests/tests/integration_legacy.rs index dd4dbb12aa..8fba6f20d3 100644 --- a/crates/tests/tests/integration_legacy.rs +++ b/crates/tests/tests/integration_legacy.rs @@ -13,8 +13,8 @@ use e3_ciphernode_builder::CiphernodeHandle; use e3_crypto::Cipher; use e3_data::GetDump; use e3_data::InMemStore; -use e3_events::EnclaveEventData; use e3_events::BusHandle; +use e3_events::EnclaveEventData; use e3_events::GetEvents; use e3_events::{ prelude::*, CiphernodeSelected, CiphertextOutputPublished, CommitteeFinalized, From 49425daf10869f669ba8ab0edee802d9cd463ac6 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 17:35:13 +0000 Subject: [PATCH 17/36] ManagedEvent -> CompositeEvent --- crates/events/src/enclave_event/mod.rs | 13 ++----------- crates/events/src/event_manager.rs | 14 +++++++------- crates/events/src/eventbus.rs | 7 ++++--- crates/events/src/traits.rs | 18 +++++------------- 4 files changed, 18 insertions(+), 34 deletions(-) diff --git a/crates/events/src/enclave_event/mod.rs b/crates/events/src/enclave_event/mod.rs index 7d41fc14f9..0048b85890 100644 --- a/crates/events/src/enclave_event/mod.rs +++ b/crates/events/src/enclave_event/mod.rs @@ -62,7 +62,7 @@ pub use ticket_generated::*; pub use ticket_submitted::*; use crate::{ - traits::{ErrorEvent, ErrorEventConstructor, Event, EventConstructorWithTimestamp}, + traits::{ErrorEvent, Event, EventConstructorWithTimestamp}, E3id, EventId, }; use actix::Message; @@ -263,19 +263,10 @@ impl fmt::Display for EnclaveEvent { } impl EventConstructorWithTimestamp for EnclaveEvent { - fn new_with_timestamp(data: Self::Data, ts: u128) -> Self { + fn new_with_timestamp(data: Self::Data, _ts: u128) -> Self { let payload = data.into(); let id = EventId::hash(&payload); // hcl.receive(remote_ts)?; EnclaveEvent { id, payload } } } - -impl ErrorEventConstructor for EnclaveEvent { - fn new_error(err_type: EnclaveErrorType, error: impl Into) -> Self { - let payload = EnclaveError::from_error(err_type, error.into().to_string()); - let id = EventId::hash(&payload); - let payload = payload.into(); - EnclaveEvent { id, payload } - } -} diff --git a/crates/events/src/event_manager.rs b/crates/events/src/event_manager.rs index 89598a3f54..d15e4577e1 100644 --- a/crates/events/src/event_manager.rs +++ b/crates/events/src/event_manager.rs @@ -8,10 +8,10 @@ use actix::{Addr, Recipient}; use crate::{ traits::{ - ErrorDispatcher, ErrorEventConstructor, ErrorFactory, Event, EventConstructorWithTimestamp, - EventDispatcher, EventFactory, EventSubscriber, ManagedEvent, + CompositeEvent, ErrorDispatcher, ErrorEventConstructor, ErrorFactory, Event, + EventConstructorWithTimestamp, EventDispatcher, EventFactory, EventSubscriber, }, - EventBus, Subscribe, + ErrorEvent, EventBus, Subscribe, }; #[derive(Clone, Debug)] @@ -29,7 +29,7 @@ impl BusHandle { } } -impl EventDispatcher for BusHandle { +impl EventDispatcher for BusHandle { fn dispatch(&self, data: impl Into) { let evt = self.create_local(data); self.bus.do_send(evt); @@ -47,7 +47,7 @@ impl EventDispatcher for BusHandle { impl ErrorDispatcher for BusHandle where - E: ManagedEvent, + E: CompositeEvent, { fn err(&self, err_type: E::ErrType, error: impl Into) { let evt = self.create_err(err_type, error); @@ -65,9 +65,9 @@ impl EventFactory for BusHandle { } } -impl ErrorFactory for BusHandle { +impl ErrorFactory for BusHandle { fn create_err(&self, err_type: E::ErrType, error: impl Into) -> E { - E::new_error(err_type, error) + E::from_error(err_type, error) } } diff --git a/crates/events/src/eventbus.rs b/crates/events/src/eventbus.rs index 8954e6bbf9..df7d4e8867 100644 --- a/crates/events/src/eventbus.rs +++ b/crates/events/src/eventbus.rs @@ -5,7 +5,7 @@ // or FITNESS FOR A PARTICULAR PURPOSE. use crate::traits::{ErrorEvent, Event}; -use crate::{prelude::*, BusHandle, ManagedEvent}; +use crate::{prelude::*, BusHandle, CompositeEvent}; use actix::prelude::*; use bloom::{BloomFilter, ASMS}; use std::collections::{HashMap, VecDeque}; @@ -137,7 +137,7 @@ impl Handler for EventBus { } } -impl From>> for BusHandle { +impl From>> for BusHandle { fn from(value: Addr>) -> Self { BusHandle::new(value) } @@ -421,7 +421,8 @@ impl Handler for HistoryCollector { ////////////////////////////////////////////////////////////////////////////// /// Function to help with testing when we want to maintain a vec of events -pub fn new_event_bus_with_history() -> (BusHandle, Addr>) { +pub fn new_event_bus_with_history() -> (BusHandle, Addr>) +{ let bus: BusHandle = EventBus::::default().start().into(); let history = HistoryCollector::new().start(); diff --git a/crates/events/src/traits.rs b/crates/events/src/traits.rs index 039d63b12f..2783bfe187 100644 --- a/crates/events/src/traits.rs +++ b/crates/events/src/traits.rs @@ -9,7 +9,9 @@ use std::fmt::Display; use std::hash::Hash; /// Trait that must be implemented by events used with EventBus -pub trait Event: Message + Clone + Display + Send + Sync + Unpin + 'static { +pub trait Event: + Message + Clone + Display + Send + Sync + Unpin + Sized + 'static +{ type Id: Hash + Eq + Clone + Unpin + Send + Sync + Display; /// Payload for the Event @@ -64,16 +66,6 @@ pub trait EventConstructorWithTimestamp: Event + Sized { fn new_with_timestamp(data: Self::Data, ts: u128) -> Self; } -pub trait ErrorEventConstructor: ErrorEvent + Sized { - fn new_error(err_type: Self::ErrType, error: impl Into) -> Self; -} +pub trait CompositeEvent: ErrorEvent + EventConstructorWithTimestamp {} -pub trait ManagedEvent: - Sized + ErrorEvent + EventConstructorWithTimestamp + ErrorEventConstructor -{ -} - -impl ManagedEvent for E where - E: Sized + Event + ErrorEvent + EventConstructorWithTimestamp + ErrorEventConstructor -{ -} +impl CompositeEvent for E where E: Sized + Event + ErrorEvent + EventConstructorWithTimestamp {} From 78453b2e1b4ff2f4f723d99c3b51e0c25f400646 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 17:36:24 +0000 Subject: [PATCH 18/36] remove import --- crates/events/src/event_manager.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/events/src/event_manager.rs b/crates/events/src/event_manager.rs index d15e4577e1..234a427ab1 100644 --- a/crates/events/src/event_manager.rs +++ b/crates/events/src/event_manager.rs @@ -8,8 +8,8 @@ use actix::{Addr, Recipient}; use crate::{ traits::{ - CompositeEvent, ErrorDispatcher, ErrorEventConstructor, ErrorFactory, Event, - EventConstructorWithTimestamp, EventDispatcher, EventFactory, EventSubscriber, + CompositeEvent, ErrorDispatcher, ErrorFactory, Event, EventConstructorWithTimestamp, + EventDispatcher, EventFactory, EventSubscriber, }, ErrorEvent, EventBus, Subscribe, }; From 30710a03ee068b4f706425b7f71544f50f5346e8 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 17:41:39 +0000 Subject: [PATCH 19/36] create_local -> event_from --- crates/events/src/event_manager.rs | 4 ++-- crates/events/src/traits.rs | 2 +- crates/evm/tests/integration.rs | 2 +- crates/request/src/router.rs | 2 +- crates/tests/tests/integration_legacy.rs | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/events/src/event_manager.rs b/crates/events/src/event_manager.rs index 234a427ab1..29310a9604 100644 --- a/crates/events/src/event_manager.rs +++ b/crates/events/src/event_manager.rs @@ -31,7 +31,7 @@ impl BusHandle { impl EventDispatcher for BusHandle { fn dispatch(&self, data: impl Into) { - let evt = self.create_local(data); + let evt = self.event_from(data); self.bus.do_send(evt); } @@ -56,7 +56,7 @@ where } impl EventFactory for BusHandle { - fn create_local(&self, data: impl Into) -> E { + fn event_from(&self, data: impl Into) -> E { E::new_with_timestamp(data.into(), 0) } diff --git a/crates/events/src/traits.rs b/crates/events/src/traits.rs index 2783bfe187..fe154edb42 100644 --- a/crates/events/src/traits.rs +++ b/crates/events/src/traits.rs @@ -34,7 +34,7 @@ pub trait ErrorEvent: Event { /// Trait to create events pub trait EventFactory { - fn create_local(&self, data: impl Into) -> E; + fn event_from(&self, data: impl Into) -> E; fn create_receive(&self, data: impl Into, ts: u128) -> E; } diff --git a/crates/evm/tests/integration.rs b/crates/evm/tests/integration.rs index a0b3179ac0..a315276c2a 100644 --- a/crates/evm/tests/integration.rs +++ b/crates/evm/tests/integration.rs @@ -269,7 +269,7 @@ async fn ensure_resume_after_shutdown() -> Result<()> { // Ensure shutdown doesn't cause event to be lost. sleep(Duration::from_millis(10)).await; - addr1.send(bus.create_local(Shutdown)).await?; + addr1.send(bus.event_from(Shutdown)).await?; for msg in ["these", "are", "not", "lost"] { contract diff --git a/crates/request/src/router.rs b/crates/request/src/router.rs index 402c175740..bdb6696d88 100644 --- a/crates/request/src/router.rs +++ b/crates/request/src/router.rs @@ -210,7 +210,7 @@ impl Handler for E3Router { impl Handler for E3Router { type Result = (); fn handle(&mut self, msg: Shutdown, _ctx: &mut Self::Context) -> Self::Result { - let shutdown_evt = self.bus.create_local(msg); + let shutdown_evt = self.bus.event_from(msg); for (_, ctx) in self.contexts.iter() { ctx.forward_message_now(&shutdown_evt) } diff --git a/crates/tests/tests/integration_legacy.rs b/crates/tests/tests/integration_legacy.rs index 8fba6f20d3..098f1377aa 100644 --- a/crates/tests/tests/integration_legacy.rs +++ b/crates/tests/tests/integration_legacy.rs @@ -507,8 +507,8 @@ async fn test_p2p_actor_forwards_events_to_network() -> Result<()> { assert_eq!( *msgs.lock().await, vec![ - GossipData::GossipBytes(bus.create_local(evt_1.clone()).to_bytes()?), - GossipData::GossipBytes(bus.create_local(evt_2.clone()).to_bytes()?) + GossipData::GossipBytes(bus.event_from(evt_1.clone()).to_bytes()?), + GossipData::GossipBytes(bus.event_from(evt_2.clone()).to_bytes()?) ], // notice no local events "NetEventTranslator did not transmit correct events to the network" ); @@ -641,7 +641,7 @@ async fn test_p2p_actor_forwards_events_to_bus() -> Result<()> { // lets send an event from the network let _ = event_tx.send(NetEvent::GossipData(GossipData::GossipBytes( - bus.create_local(event.clone()).to_bytes()?, + bus.event_from(event.clone()).to_bytes()?, ))); // check the history of the event bus From 299ac157305fb5e843c6fea107c01172a0cdebb1 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 17:47:45 +0000 Subject: [PATCH 20/36] fix api design --- crates/events/src/event_manager.rs | 10 ++++++---- crates/events/src/traits.rs | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/crates/events/src/event_manager.rs b/crates/events/src/event_manager.rs index 29310a9604..e238648ed7 100644 --- a/crates/events/src/event_manager.rs +++ b/crates/events/src/event_manager.rs @@ -36,7 +36,7 @@ impl EventDispatcher for BusHandle { } fn dispatch_from_remote(&self, data: impl Into, ts: u128) { - let evt = self.create_receive(data, ts); + let evt = self.event_from_remote_source(data, ts); self.bus.do_send(evt) } @@ -50,23 +50,25 @@ where E: CompositeEvent, { fn err(&self, err_type: E::ErrType, error: impl Into) { - let evt = self.create_err(err_type, error); + let evt = self.err_from(err_type, error); self.bus.do_send(evt); } } impl EventFactory for BusHandle { fn event_from(&self, data: impl Into) -> E { + // TODO: add self.hcl.tick() E::new_with_timestamp(data.into(), 0) } - fn create_receive(&self, data: impl Into, ts: u128) -> E { + fn event_from_remote_source(&self, data: impl Into, ts: u128) -> E { + // TODO: add self.hcl.receive(ts) E::new_with_timestamp(data.into(), ts) } } impl ErrorFactory for BusHandle { - fn create_err(&self, err_type: E::ErrType, error: impl Into) -> E { + fn err_from(&self, err_type: E::ErrType, error: impl Into) -> E { E::from_error(err_type, error) } } diff --git a/crates/events/src/traits.rs b/crates/events/src/traits.rs index fe154edb42..67af2b013a 100644 --- a/crates/events/src/traits.rs +++ b/crates/events/src/traits.rs @@ -35,12 +35,12 @@ pub trait ErrorEvent: Event { /// Trait to create events pub trait EventFactory { fn event_from(&self, data: impl Into) -> E; - fn create_receive(&self, data: impl Into, ts: u128) -> E; + fn event_from_remote_source(&self, data: impl Into, ts: u128) -> E; } /// Trait create errors pub trait ErrorFactory { - fn create_err(&self, err_type: E::ErrType, error: impl Into) -> E; + fn err_from(&self, err_type: E::ErrType, error: impl Into) -> E; } /// Trait to dispatch events From 840fc2c2ed8ee14b7304bd85f6b67a3266902242 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 18:10:35 +0000 Subject: [PATCH 21/36] update api design to use pubsub terminology --- crates/aggregator/src/committee_finalizer.rs | 2 +- crates/aggregator/src/plaintext_aggregator.rs | 2 +- crates/aggregator/src/publickey_aggregator.rs | 2 +- .../src/threshold_plaintext_aggregator.rs | 2 +- crates/entrypoint/src/helpers/shutdown.rs | 2 +- crates/events/src/event_manager.rs | 12 +++---- crates/events/src/traits.rs | 15 +++++--- .../evm/src/historical_event_coordinator.rs | 4 +-- crates/keyshare/src/keyshare.rs | 4 +-- crates/keyshare/src/threshold_keyshare.rs | 6 ++-- crates/net/src/document_publisher.rs | 15 ++++---- crates/request/src/router.rs | 2 +- crates/sortition/src/ciphernode_selector.rs | 4 +-- crates/test-helpers/src/lib.rs | 4 +-- crates/tests/tests/integration.rs | 12 +++---- crates/tests/tests/integration_legacy.rs | 36 +++++++++---------- 16 files changed, 65 insertions(+), 59 deletions(-) diff --git a/crates/aggregator/src/committee_finalizer.rs b/crates/aggregator/src/committee_finalizer.rs index ef4ed1aa96..aef3847ea2 100644 --- a/crates/aggregator/src/committee_finalizer.rs +++ b/crates/aggregator/src/committee_finalizer.rs @@ -112,7 +112,7 @@ impl Handler for CommitteeFinalizer { move |act, _ctx| { info!(e3_id = %e3_id_clone, "Dispatching CommitteeFinalizeRequested event"); - bus.dispatch(CommitteeFinalizeRequested { + bus.publish(CommitteeFinalizeRequested { e3_id: e3_id_clone.clone(), }); diff --git a/crates/aggregator/src/plaintext_aggregator.rs b/crates/aggregator/src/plaintext_aggregator.rs index c487c7d1cf..afc416255b 100644 --- a/crates/aggregator/src/plaintext_aggregator.rs +++ b/crates/aggregator/src/plaintext_aggregator.rs @@ -239,7 +239,7 @@ impl Handler for PlaintextAggregator { e3_id: self.e3_id.clone(), }; - self.bus.dispatch(event); + self.bus.publish(event); Ok(()) } diff --git a/crates/aggregator/src/publickey_aggregator.rs b/crates/aggregator/src/publickey_aggregator.rs index 062b87ad9c..a8bb754f93 100644 --- a/crates/aggregator/src/publickey_aggregator.rs +++ b/crates/aggregator/src/publickey_aggregator.rs @@ -229,7 +229,7 @@ impl Handler for PublicKeyAggregator { e3_id: msg.e3_id.clone(), nodes: OrderedSet::from(nodes), }; - act.bus.dispatch(event); + act.bus.publish(event); Ok(()) }), ) diff --git a/crates/aggregator/src/threshold_plaintext_aggregator.rs b/crates/aggregator/src/threshold_plaintext_aggregator.rs index c4f3240b57..4471338185 100644 --- a/crates/aggregator/src/threshold_plaintext_aggregator.rs +++ b/crates/aggregator/src/threshold_plaintext_aggregator.rs @@ -342,7 +342,7 @@ impl Handler for ThresholdPlaintextAggregator { }; info!("Dispatching plaintext event {:?}", event); - act.bus.dispatch(event); + act.bus.publish(event); Ok(()) }), ) diff --git a/crates/entrypoint/src/helpers/shutdown.rs b/crates/entrypoint/src/helpers/shutdown.rs index 8d1b0174f3..ae4e6123b2 100644 --- a/crates/entrypoint/src/helpers/shutdown.rs +++ b/crates/entrypoint/src/helpers/shutdown.rs @@ -22,7 +22,7 @@ pub async fn listen_for_shutdown(bus: BusHandle, mut handle: JoinH info!("SIGTERM received, initiating graceful shutdown..."); // Stop the actor system - bus.dispatch(Shutdown); + bus.publish(Shutdown); // Wait for all events to propagate tokio::time::sleep(Duration::from_secs(2)).await; diff --git a/crates/events/src/event_manager.rs b/crates/events/src/event_manager.rs index e238648ed7..b0e2e3135c 100644 --- a/crates/events/src/event_manager.rs +++ b/crates/events/src/event_manager.rs @@ -9,7 +9,7 @@ use actix::{Addr, Recipient}; use crate::{ traits::{ CompositeEvent, ErrorDispatcher, ErrorFactory, Event, EventConstructorWithTimestamp, - EventDispatcher, EventFactory, EventSubscriber, + EventFactory, EventPublisher, EventSubscriber, }, ErrorEvent, EventBus, Subscribe, }; @@ -29,13 +29,13 @@ impl BusHandle { } } -impl EventDispatcher for BusHandle { - fn dispatch(&self, data: impl Into) { +impl EventPublisher for BusHandle { + fn publish(&self, data: impl Into) { let evt = self.event_from(data); self.bus.do_send(evt); } - fn dispatch_from_remote(&self, data: impl Into, ts: u128) { + fn publish_from_remote(&self, data: impl Into, ts: u128) { let evt = self.event_from_remote_source(data, ts); self.bus.do_send(evt) } @@ -50,7 +50,7 @@ where E: CompositeEvent, { fn err(&self, err_type: E::ErrType, error: impl Into) { - let evt = self.err_from(err_type, error); + let evt = self.event_from_error(err_type, error); self.bus.do_send(evt); } } @@ -68,7 +68,7 @@ impl EventFactory for BusHandle { } impl ErrorFactory for BusHandle { - fn err_from(&self, err_type: E::ErrType, error: impl Into) -> E { + fn event_from_error(&self, err_type: E::ErrType, error: impl Into) -> E { E::from_error(err_type, error) } } diff --git a/crates/events/src/traits.rs b/crates/events/src/traits.rs index 67af2b013a..ba7ed61f7a 100644 --- a/crates/events/src/traits.rs +++ b/crates/events/src/traits.rs @@ -40,18 +40,24 @@ pub trait EventFactory { /// Trait create errors pub trait ErrorFactory { - fn err_from(&self, err_type: E::ErrType, error: impl Into) -> E; + fn event_from_error(&self, err_type: E::ErrType, error: impl Into) -> E; } /// Trait to dispatch events -pub trait EventDispatcher { - fn dispatch(&self, data: impl Into); - fn dispatch_from_remote(&self, data: impl Into, ts: u128); +pub trait EventPublisher { + /// Create a new event from the given event data, apply a local HLC timestamp and dispatch it + /// to the event bus. This method should be used for events that have originated locally. + fn publish(&self, data: impl Into); + /// Create a new event from the given event data, apply the given remote HLC time to ensure correct + /// event ordering. This method should be used for events that originated from remote sources. + fn publish_from_remote(&self, data: impl Into, ts: u128); + /// Dispatch the given event without applying any HLC transformation fn naked_dispatch(&self, event: E); } /// Trait for dispatching errors pub trait ErrorDispatcher { + /// Dispatch the error to the event bus apply a local HCL fn err(&self, err_type: E::ErrType, error: impl Into); } @@ -63,6 +69,7 @@ pub trait EventSubscriber { /// Trait to create an event with a timestamp from its associated type data pub trait EventConstructorWithTimestamp: Event + Sized { + /// Create an event passing attaching a specific timestamp. fn new_with_timestamp(data: Self::Data, ts: u128) -> Self; } diff --git a/crates/evm/src/historical_event_coordinator.rs b/crates/evm/src/historical_event_coordinator.rs index ba9e181f32..12f1b9e466 100644 --- a/crates/evm/src/historical_event_coordinator.rs +++ b/crates/evm/src/historical_event_coordinator.rs @@ -60,7 +60,7 @@ impl HistoricalEventCoordinator { let count = self.buffered_events.len(); for BufferedEvent { event, .. } in self.buffered_events.drain(..) { - self.target.dispatch(event); + self.target.publish(event); } info!( @@ -111,7 +111,7 @@ impl Handler for HistoricalEventCoordinator { self.buffered_events.push(BufferedEvent { block, event }); } } else { - self.target.dispatch(event); + self.target.publish(event); } } } diff --git a/crates/keyshare/src/keyshare.rs b/crates/keyshare/src/keyshare.rs index edddb0ccf9..5555966def 100644 --- a/crates/keyshare/src/keyshare.rs +++ b/crates/keyshare/src/keyshare.rs @@ -108,7 +108,7 @@ impl Handler for Keyshare { }; // Broadcast the KeyshareCreated message - self.bus.dispatch(KeyshareCreated { + self.bus.publish(KeyshareCreated { pubkey, e3_id, node: self.address.clone(), @@ -156,7 +156,7 @@ impl Handler for Keyshare { return; }; - self.bus.dispatch(DecryptionshareCreated { + self.bus.publish(DecryptionshareCreated { party_id: 0, // Not used e3_id, decryption_share: vec![ArcBytes::from_bytes(&decryption_share)], diff --git a/crates/keyshare/src/threshold_keyshare.rs b/crates/keyshare/src/threshold_keyshare.rs index 7dc606655a..ccea059247 100644 --- a/crates/keyshare/src/threshold_keyshare.rs +++ b/crates/keyshare/src/threshold_keyshare.rs @@ -526,7 +526,7 @@ impl ThresholdKeyshare { .map(|s| PvwEncrypted::new(s.decrypt(&self.cipher)?)) .collect::>()?; - self.bus.dispatch(ThresholdShareCreated { + self.bus.publish(ThresholdShareCreated { e3_id, share: Arc::new(ThresholdShare { party_id, @@ -630,7 +630,7 @@ impl ThresholdKeyshare { let address = state.get_address().to_owned(); let current: ReadyForDecryption = state.clone().try_into()?; - self.bus.dispatch(KeyshareCreated { + self.bus.publish(KeyshareCreated { pubkey: current.pk_share, e3_id, node: address, @@ -697,7 +697,7 @@ impl ThresholdKeyshare { }; // send the decryption share - self.bus.dispatch(event); + self.bus.publish(event); // mark as complete self.state.try_mutate(|s| { diff --git a/crates/net/src/document_publisher.rs b/crates/net/src/document_publisher.rs index 86a3effa91..c18bfd1d61 100644 --- a/crates/net/src/document_publisher.rs +++ b/crates/net/src/document_publisher.rs @@ -280,7 +280,7 @@ pub async fn handle_document_published_notification( .await?; debug!("Sending received event..."); - bus.dispatch(DocumentReceived { + bus.publish(DocumentReceived { meta: event.meta, value, }); @@ -422,8 +422,7 @@ impl EventConverter { vec![], None, ); - self.bus - .dispatch(PublishDocumentRequested::new(meta, value)); + self.bus.publish(PublishDocumentRequested::new(meta, value)); Ok(()) } /// Received document externally @@ -438,7 +437,7 @@ impl EventConverter { }, }; - self.bus.dispatch(event); + self.bus.publish(event); Ok(()) } } @@ -543,7 +542,7 @@ mod tests { let e3_id = E3id::new("1243", 1); // 1. Send a request to publish - bus.dispatch(PublishDocumentRequested { + bus.publish(PublishDocumentRequested { meta: DocumentMeta::new(e3_id, DocumentKind::TrBFV, vec![], expires_at), value: value.clone(), }); @@ -617,7 +616,7 @@ mod tests { let cid = Cid::from_content(&value); // 1. Ensure the publisher is interested in the id by receiving CiphernodeSelected - bus.dispatch(CiphernodeSelected { + bus.publish(CiphernodeSelected { e3_id: e3_id.clone(), threshold_m: 3, threshold_n: 5, @@ -679,7 +678,7 @@ mod tests { let e3_id = E3id::new("1243", 1); // Send a request to publish - bus.dispatch(PublishDocumentRequested { + bus.publish(PublishDocumentRequested { meta: DocumentMeta::new(e3_id, DocumentKind::TrBFV, vec![], expires_at), value: value.clone(), }); @@ -729,7 +728,7 @@ mod tests { let cid = Cid::from_content(&value); // 1. Ensure the publisher is interested in the id by receiving CiphernodeSelected - bus.dispatch(CiphernodeSelected { + bus.publish(CiphernodeSelected { e3_id: e3_id.clone(), threshold_m: 3, threshold_n: 5, diff --git a/crates/request/src/router.rs b/crates/request/src/router.rs index bdb6696d88..a3edf20f48 100644 --- a/crates/request/src/router.rs +++ b/crates/request/src/router.rs @@ -192,7 +192,7 @@ impl Handler for E3Router { }; // Send to bus so all other actors can react to a request being complete. - self.bus.dispatch(event); + self.bus.publish(event); } EnclaveEventData::E3RequestComplete(_) => { // Note this will be sent above to the children who can kill themselves based on diff --git a/crates/sortition/src/ciphernode_selector.rs b/crates/sortition/src/ciphernode_selector.rs index 0a2292dad1..0d2d278a57 100644 --- a/crates/sortition/src/ciphernode_selector.rs +++ b/crates/sortition/src/ciphernode_selector.rs @@ -109,7 +109,7 @@ impl Handler for CiphernodeSelector { ticket_id = tid, "Ticket generated for score sortition" ); - bus.dispatch(TicketGenerated { + bus.publish(TicketGenerated { e3_id: data.e3_id.clone(), ticket_id: TicketId::Score(tid), node: address.clone(), @@ -165,7 +165,7 @@ impl Handler for CiphernodeSelector { party_id = party_id, "Node is in finalized committee, emitting CiphernodeSelected" ); - bus.dispatch(CiphernodeSelected { + bus.publish(CiphernodeSelected { party_id: party_id as u64, e3_id, threshold_m: e3_meta.threshold_m, diff --git a/crates/test-helpers/src/lib.rs b/crates/test-helpers/src/lib.rs index 0fe346be7d..2f1c009dd4 100644 --- a/crates/test-helpers/src/lib.rs +++ b/crates/test-helpers/src/lib.rs @@ -16,7 +16,7 @@ use anyhow::*; use e3_ciphernode_builder::CiphernodeHandle; use e3_events::{ BusHandle, CiphernodeAdded, EnclaveEvent, EnclaveEventData, EventBus, EventBusConfig, - EventDispatcher, HistoryCollector, Seed, Subscribe, + EventPublisher, HistoryCollector, Seed, Subscribe, }; use e3_fhe::{create_crp, setup_crp_params, ParamsWithCrp}; use e3_net::{DocumentPublisher, NetEventTranslator}; @@ -182,7 +182,7 @@ impl AddToCommittee { self.count += 1; - self.bus.dispatch(evt.clone()); + self.bus.publish(evt.clone()); Ok(evt.into()) } diff --git a/crates/tests/tests/integration.rs b/crates/tests/tests/integration.rs index 33f58a9cea..4a5f1ede59 100644 --- a/crates/tests/tests/integration.rs +++ b/crates/tests/tests/integration.rs @@ -37,7 +37,7 @@ async fn setup_score_sortition_environment( eth_addrs: &Vec, chain_id: u64, ) -> Result<()> { - bus.dispatch(ConfigurationUpdated { + bus.publish(ConfigurationUpdated { parameter: "ticketPrice".to_string(), old_value: U256::ZERO, new_value: U256::from(10_000_000u64), @@ -48,7 +48,7 @@ async fn setup_score_sortition_environment( for addr in eth_addrs { adder.add(addr).await?; - bus.dispatch(TicketBalanceUpdated { + bus.publish(TicketBalanceUpdated { operator: addr.clone(), delta: I256::try_from(1_000_000_000u64).unwrap(), new_balance: U256::from(1_000_000_000u64), @@ -56,7 +56,7 @@ async fn setup_score_sortition_environment( chain_id, }); - bus.dispatch(OperatorActivationChanged { + bus.publish(OperatorActivationChanged { operator: addr.clone(), active: true, chain_id, @@ -238,7 +238,7 @@ async fn test_trbfv_actor() -> Result<()> { params, }; - bus.dispatch(e3_requested); + bus.publish(e3_requested); // For score sortition, we need to wait for nodes to process E3Requested and run sortition // Since TicketGenerated is a local-only event (not shared across network), we can't collect it @@ -260,7 +260,7 @@ async fn test_trbfv_actor() -> Result<()> { println!("Emitting CommitteeFinalized with {} nodes", committee.len()); - bus.dispatch(CommitteeFinalized { + bus.publish(CommitteeFinalized { e3_id: e3_id.clone(), committee, chain_id, @@ -361,7 +361,7 @@ async fn test_trbfv_actor() -> Result<()> { e3_id: e3_id.clone(), }; - bus.dispatch(ciphertext_published_event.clone()); + bus.publish(ciphertext_published_event.clone()); println!("CiphertextOutputPublished event has been dispatched!"); diff --git a/crates/tests/tests/integration_legacy.rs b/crates/tests/tests/integration_legacy.rs index 098f1377aa..0c0ba861c3 100644 --- a/crates/tests/tests/integration_legacy.rs +++ b/crates/tests/tests/integration_legacy.rs @@ -124,7 +124,7 @@ async fn setup_score_sortition_environment( eth_addrs: &Vec, chain_id: u64, ) -> Result<()> { - bus.dispatch(ConfigurationUpdated { + bus.publish(ConfigurationUpdated { parameter: "ticketPrice".to_string(), old_value: U256::ZERO, new_value: U256::from(10_000_000u64), @@ -135,7 +135,7 @@ async fn setup_score_sortition_environment( for addr in eth_addrs { adder.add(addr).await?; - bus.dispatch(TicketBalanceUpdated { + bus.publish(TicketBalanceUpdated { operator: addr.clone(), delta: I256::try_from(1_000_000_000u64).unwrap(), new_balance: U256::from(1_000_000_000u64), @@ -143,7 +143,7 @@ async fn setup_score_sortition_environment( chain_id, }); - bus.dispatch(OperatorActivationChanged { + bus.publish(OperatorActivationChanged { operator: addr.clone(), active: true, chain_id, @@ -202,13 +202,13 @@ async fn test_public_key_aggregation_and_decryption() -> Result<()> { println!("Sending E3 event..."); // Send the computation requested event - bus.dispatch(e3_request_event.clone()); + bus.publish(e3_request_event.clone()); // Test that we cannot send the same event twice - bus.dispatch(e3_request_event.clone()); + bus.publish(e3_request_event.clone()); // Finalize committee with all available nodes - bus.dispatch(CommitteeFinalized { + bus.publish(CommitteeFinalized { e3_id: e3_id.clone(), committee: eth_addrs.clone(), chain_id: 1, @@ -258,7 +258,7 @@ async fn test_public_key_aggregation_and_decryption() -> Result<()> { e3_id: e3_id.clone(), }; - bus.dispatch(ciphertext_published_event.clone()); + bus.publish(ciphertext_published_event.clone()); let history = history_collector .send(TakeEvents::::new(6)) @@ -306,7 +306,7 @@ async fn test_stopped_keyshares_retain_state() -> Result<()> { }; // Send e3request - bus.dispatch(E3Requested { + bus.publish(E3Requested { e3_id: e3_id.clone(), threshold_m: 2, threshold_n: 2, @@ -315,7 +315,7 @@ async fn test_stopped_keyshares_retain_state() -> Result<()> { ..E3Requested::default() }); - bus.dispatch(CommitteeFinalized { + bus.publish(CommitteeFinalized { e3_id: e3_id.clone(), committee: eth_addrs.clone(), chain_id: 1, @@ -331,7 +331,7 @@ async fn test_stopped_keyshares_retain_state() -> Result<()> { assert_eq!(errors.len(), 0); // SEND SHUTDOWN! - bus.dispatch(Shutdown); + bus.publish(Shutdown); // This is probably overkill but required to ensure that all the data is written sleep(Duration::from_secs(1)).await; @@ -400,7 +400,7 @@ async fn test_stopped_keyshares_retain_state() -> Result<()> { // Publish the ciphertext let raw_plaintext = vec![vec![1234, 567890]]; let (ciphertext, expected) = encrypt_ciphertext(¶ms, pubkey, raw_plaintext)?; - bus.dispatch(CiphertextOutputPublished { + bus.publish(CiphertextOutputPublished { ciphertext_output: ciphertext .iter() .map(|ct| ArcBytes::from_bytes(&ct.to_bytes())) @@ -495,9 +495,9 @@ async fn test_p2p_actor_forwards_events_to_network() -> Result<()> { ..CiphernodeSelected::default() }; - bus.dispatch(evt_1.clone()); - bus.dispatch(evt_2.clone()); - bus.dispatch(local_evt_3.clone()); // This is a local event which should not be broadcast to the network + bus.publish(evt_1.clone()); + bus.publish(evt_2.clone()); + bus.publish(local_evt_3.clone()); // This is a local event which should not be broadcast to the network // check the history of the event bus let history = history_collector @@ -540,7 +540,7 @@ async fn test_duplicate_e3_id_with_different_chain_id() -> Result<()> { setup_score_sortition_environment(&bus, ð_addrs, 2).await?; // Send the computation requested event - bus.dispatch(E3Requested { + bus.publish(E3Requested { e3_id: E3id::new("1234", 1), threshold_m: 3, threshold_n: 3, @@ -549,7 +549,7 @@ async fn test_duplicate_e3_id_with_different_chain_id() -> Result<()> { ..E3Requested::default() }); - bus.dispatch(CommitteeFinalized { + bus.publish(CommitteeFinalized { e3_id: E3id::new("1234", 1), committee: eth_addrs.clone(), chain_id: 1, @@ -577,7 +577,7 @@ async fn test_duplicate_e3_id_with_different_chain_id() -> Result<()> { ); // Send the computation requested event - bus.dispatch(E3Requested { + bus.publish(E3Requested { e3_id: E3id::new("1234", 2), threshold_m: 3, threshold_n: 3, @@ -586,7 +586,7 @@ async fn test_duplicate_e3_id_with_different_chain_id() -> Result<()> { ..E3Requested::default() }); - bus.dispatch(CommitteeFinalized { + bus.publish(CommitteeFinalized { e3_id: E3id::new("1234", 2), committee: eth_addrs.clone(), chain_id: 2, From fd3b2a21a7bb9bc2c3516a028c352442db376546 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 18:17:11 +0000 Subject: [PATCH 22/36] event_manager.rs -> bus_handle.rs --- crates/events/src/{event_manager.rs => bus_handle.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename crates/events/src/{event_manager.rs => bus_handle.rs} (100%) diff --git a/crates/events/src/event_manager.rs b/crates/events/src/bus_handle.rs similarity index 100% rename from crates/events/src/event_manager.rs rename to crates/events/src/bus_handle.rs From c27fc40c3b138601207eca6286d53cac62b985a7 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 18:26:39 +0000 Subject: [PATCH 23/36] event_manager.rs -> bus_handle.rs --- crates/events/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/events/src/lib.rs b/crates/events/src/lib.rs index b348b7e2d3..48ebf77471 100644 --- a/crates/events/src/lib.rs +++ b/crates/events/src/lib.rs @@ -4,11 +4,11 @@ // without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. +mod bus_handle; mod correlation_id; mod e3id; mod enclave_event; mod event_id; -mod event_manager; mod eventbus; mod eventbus_factory; mod ordered_set; @@ -16,11 +16,11 @@ pub mod prelude; mod seed; mod traits; +pub use bus_handle::*; pub use correlation_id::*; pub use e3id::*; pub use enclave_event::*; pub use event_id::*; -pub use event_manager::*; pub use eventbus::*; pub use eventbus_factory::*; pub use ordered_set::*; From 45002208c54689e07dc09c96047a27cb020a3dc8 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 18:28:07 +0000 Subject: [PATCH 24/36] event_manager.rs -> bus_handle.rs --- crates/data/src/sled_store.rs | 5 ++--- crates/entrypoint/src/helpers/datastore.rs | 4 ++-- crates/entrypoint/src/start/aggregator_start.rs | 4 ++-- crates/entrypoint/src/start/start.rs | 4 ++-- crates/events/src/eventbus_factory.rs | 2 +- 5 files changed, 9 insertions(+), 10 deletions(-) diff --git a/crates/data/src/sled_store.rs b/crates/data/src/sled_store.rs index 78dcaf9dec..87f3d87c79 100644 --- a/crates/data/src/sled_store.rs +++ b/crates/data/src/sled_store.rs @@ -8,8 +8,7 @@ use crate::{Get, Insert, InsertSync, Remove}; use actix::{Actor, ActorContext, Addr, Handler}; use anyhow::{Context, Result}; use e3_events::{ - get_enclave_event_manager, prelude::*, BusHandle, EnclaveErrorType, EnclaveEvent, - EnclaveEventData, + get_enclave_bus_handle, prelude::*, BusHandle, EnclaveErrorType, EnclaveEvent, EnclaveEventData, }; use once_cell::sync::Lazy; use sled::Db; @@ -48,7 +47,7 @@ impl SledStore { pub fn from_db(db: SledDb) -> Result { Ok(Self { db: Some(db), - bus: get_enclave_event_manager(), + bus: get_enclave_bus_handle(), }) } } diff --git a/crates/entrypoint/src/helpers/datastore.rs b/crates/entrypoint/src/helpers/datastore.rs index 75f8295fdc..39fc5129d0 100644 --- a/crates/entrypoint/src/helpers/datastore.rs +++ b/crates/entrypoint/src/helpers/datastore.rs @@ -11,7 +11,7 @@ use anyhow::Result; use e3_config::AppConfig; use e3_data::{DataStore, InMemStore, SledDb, SledStore}; use e3_data::{Repositories, RepositoriesFactory}; -use e3_events::{get_enclave_event_manager, BusHandle, EnclaveEvent}; +use e3_events::{get_enclave_bus_handle, BusHandle, EnclaveEvent}; pub fn get_sled_store(bus: &BusHandle, db_file: &PathBuf) -> Result { Ok((&SledStore::new(bus, db_file)?).into()) @@ -31,7 +31,7 @@ pub fn setup_datastore(config: &AppConfig, bus: &BusHandle) -> Res } pub fn get_repositories(config: &AppConfig) -> Result { - let bus = get_enclave_event_manager(); + let bus = get_enclave_bus_handle(); let store = setup_datastore(config, &bus)?; Ok(store.repositories()) } diff --git a/crates/entrypoint/src/start/aggregator_start.rs b/crates/entrypoint/src/start/aggregator_start.rs index 3d42a8653a..3789ba2f1b 100644 --- a/crates/entrypoint/src/start/aggregator_start.rs +++ b/crates/entrypoint/src/start/aggregator_start.rs @@ -9,7 +9,7 @@ use e3_ciphernode_builder::CiphernodeBuilder; use e3_config::AppConfig; use e3_crypto::Cipher; use e3_data::RepositoriesFactory; -use e3_events::{get_enclave_event_manager, BusHandle, EnclaveEvent}; +use e3_events::{get_enclave_bus_handle, BusHandle, EnclaveEvent}; use e3_net::{NetEventTranslator, NetRepositoryFactory}; use e3_test_helpers::{PlaintextWriter, PublicKeyWriter}; use rand::SeedableRng; @@ -28,7 +28,7 @@ pub async fn execute( plaintext_write_path: Option, experimental_trbfv: bool, ) -> Result<(BusHandle, JoinHandle>, String)> { - let bus = get_enclave_event_manager(); + let bus = get_enclave_bus_handle(); let rng = Arc::new(Mutex::new(ChaCha20Rng::from_rng(OsRng)?)); let store = setup_datastore(config, &bus)?; let repositories = store.repositories(); diff --git a/crates/entrypoint/src/start/start.rs b/crates/entrypoint/src/start/start.rs index b50e00edad..6e977f0478 100644 --- a/crates/entrypoint/src/start/start.rs +++ b/crates/entrypoint/src/start/start.rs @@ -10,7 +10,7 @@ use e3_ciphernode_builder::CiphernodeBuilder; use e3_config::AppConfig; use e3_crypto::Cipher; use e3_data::RepositoriesFactory; -use e3_events::{get_enclave_event_manager, EnclaveEvent}; +use e3_events::{get_enclave_bus_handle, EnclaveEvent}; use e3_events::{prelude::*, BusHandle}; use e3_net::{NetEventTranslator, NetRepositoryFactory}; use rand::SeedableRng; @@ -29,7 +29,7 @@ pub async fn execute( ) -> Result<(BusHandle, JoinHandle>, String)> { let rng = Arc::new(Mutex::new(rand_chacha::ChaCha20Rng::from_rng(OsRng)?)); - let bus = get_enclave_event_manager(); + let bus = get_enclave_bus_handle(); let cipher = Arc::new(Cipher::from_file(&config.key_file()).await?); let store = setup_datastore(&config, &bus)?; let repositories = store.repositories(); diff --git a/crates/events/src/eventbus_factory.rs b/crates/events/src/eventbus_factory.rs index 6f0467ab13..6aecbe69a4 100644 --- a/crates/events/src/eventbus_factory.rs +++ b/crates/events/src/eventbus_factory.rs @@ -92,6 +92,6 @@ pub fn get_error_collector() -> Addr> { EventBusFactory::instance().get_error_collector() } -pub fn get_enclave_event_manager() -> BusHandle { +pub fn get_enclave_bus_handle() -> BusHandle { EventBus::manager(get_enclave_event_bus()) } From 876a1be3663fb4c597329a9fe465b199ef712531 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 18:39:11 +0000 Subject: [PATCH 25/36] add comments --- crates/events/src/traits.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/crates/events/src/traits.rs b/crates/events/src/traits.rs index ba7ed61f7a..ba98fda9da 100644 --- a/crates/events/src/traits.rs +++ b/crates/events/src/traits.rs @@ -32,38 +32,52 @@ pub trait ErrorEvent: Event { fn from_error(err_type: Self::ErrType, error: impl Into) -> Self; } -/// Trait to create events +/// An EventFactory creates events pub trait EventFactory { + /// Create a new event from the given event data, apply a local HLC timestamp. + /// + /// This method should be used for events that have originated locally. fn event_from(&self, data: impl Into) -> E; + /// Create a new event from the given event data, apply the given remote HLC time to ensure correct + /// event ordering. + /// + /// This method should be used for events that originated from remote sources. fn event_from_remote_source(&self, data: impl Into, ts: u128) -> E; } -/// Trait create errors +/// An ErrorFactory creates errors. pub trait ErrorFactory { + /// Create an error event from the given error. fn event_from_error(&self, err_type: E::ErrType, error: impl Into) -> E; } -/// Trait to dispatch events +/// An EventPublisher publishes events on it's internal EventBus pub trait EventPublisher { - /// Create a new event from the given event data, apply a local HLC timestamp and dispatch it - /// to the event bus. This method should be used for events that have originated locally. + /// Create a new event from the given event data, apply a local HLC timestamp and publish it + /// to the event bus. + /// + /// This method should be used for events that have originated locally. fn publish(&self, data: impl Into); /// Create a new event from the given event data, apply the given remote HLC time to ensure correct - /// event ordering. This method should be used for events that originated from remote sources. + /// event ordering and publish it. + /// + /// This method should be used for events that originated from remote sources. fn publish_from_remote(&self, data: impl Into, ts: u128); - /// Dispatch the given event without applying any HLC transformation + /// Dispatch the given event without applying any HLC transformation. fn naked_dispatch(&self, event: E); } -/// Trait for dispatching errors +/// Trait for dispatching errors to an inner event bus pub trait ErrorDispatcher { - /// Dispatch the error to the event bus apply a local HCL + /// Dispatch the error to the event bus. fn err(&self, err_type: E::ErrType, error: impl Into); } /// Trait to subscribe to events pub trait EventSubscriber { + /// Subscribe the recipient to events matching the given event type fn subscribe(&self, event_type: &str, recipient: Recipient); + /// Subscribe the recipient to events matching any of the given event types fn subscribe_all(&self, event_types: &[&str], recipient: Recipient); } From bf7f2e5edc9a7fb8c1ce57a1a5bee3d78ab147c5 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 18:41:04 +0000 Subject: [PATCH 26/36] remove FromError --- crates/keyshare/src/keyshare.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/keyshare/src/keyshare.rs b/crates/keyshare/src/keyshare.rs index 5555966def..065009e39e 100644 --- a/crates/keyshare/src/keyshare.rs +++ b/crates/keyshare/src/keyshare.rs @@ -10,8 +10,7 @@ use e3_crypto::Cipher; use e3_data::Persistable; use e3_events::{ prelude::*, BusHandle, CiphernodeSelected, CiphertextOutputPublished, DecryptionshareCreated, - Die, E3RequestComplete, EnclaveErrorType, EnclaveEvent, EnclaveEventData, FromError, - KeyshareCreated, + Die, E3RequestComplete, EnclaveErrorType, EnclaveEvent, EnclaveEventData, KeyshareCreated, }; use e3_fhe::{DecryptCiphertext, Fhe}; use e3_utils::utility_types::ArcBytes; From b2305651dfb437025ccc8fa7d1a17416e9f55964 Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 19:47:45 +0000 Subject: [PATCH 27/36] i know this sounds crazy but hear me out... --- examples/CRISP/enclave.config.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/CRISP/enclave.config.yaml b/examples/CRISP/enclave.config.yaml index 92bb4b9d16..2104bf53ad 100644 --- a/examples/CRISP/enclave.config.yaml +++ b/examples/CRISP/enclave.config.yaml @@ -3,7 +3,7 @@ chains: rpc_url: ws://localhost:8545 contracts: e3_program: - address: "0xc5a5C42992dECbae36851359345FE25997F5C42d" + address: "0x67d269191c92Caf3cD7723F116c85e6E9bf55933" deploy_block: 1 enclave: address: "0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0" @@ -61,4 +61,3 @@ nodes: autopassword: true role: type: aggregator - From 851f856e593484cb4839120de2bf9e64d3ed289d Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 22:08:58 +0000 Subject: [PATCH 28/36] remove unused imports --- .../src/ciphernode_builder.rs | 47 ++++++++----------- crates/entrypoint/src/start/start.rs | 2 +- crates/events/src/eventbus.rs | 4 -- crates/events/src/eventbus_factory.rs | 2 +- crates/evm/src/enclave_sol.rs | 4 +- crates/evm/src/enclave_sol_reader.rs | 2 +- crates/evm/src/enclave_sol_writer.rs | 2 +- crates/net/src/document_publisher.rs | 5 +- crates/sortition/src/sortition.rs | 2 +- crates/test-helpers/src/lib.rs | 7 --- 10 files changed, 30 insertions(+), 47 deletions(-) diff --git a/crates/ciphernode-builder/src/ciphernode_builder.rs b/crates/ciphernode-builder/src/ciphernode_builder.rs index 4fb3874f4e..6bf4f29514 100644 --- a/crates/ciphernode-builder/src/ciphernode_builder.rs +++ b/crates/ciphernode-builder/src/ciphernode_builder.rs @@ -16,7 +16,7 @@ use e3_aggregator::ext::{ use e3_config::chain_config::ChainConfig; use e3_crypto::Cipher; use e3_data::{DataStore, InMemStore, Repositories, RepositoriesFactory}; -use e3_events::{prelude::*, EnclaveEvent, EventBus, EventBusConfig}; +use e3_events::{EnclaveEvent, EventBus, EventBusConfig}; use e3_evm::{ helpers::{ load_signer_from_repository, ConcreteReadProvider, ConcreteWriteProvider, EthProvider, @@ -298,8 +298,8 @@ impl CiphernodeBuilder { None }; - // Setup an event dispatcher - let dispatcher = EventBus::manager(local_bus); + // Get a handle from the event bus + let bus = local_bus.into(); let addr = if let Some(addr) = self.address.clone() { info!("Using eth address = {}", addr); @@ -321,7 +321,7 @@ impl CiphernodeBuilder { let default_backend = self.sortition_backend.clone(); let sortition = Sortition::attach( - &dispatcher, + &bus, repositories.sortition(), repositories.node_state(), repositories.finalized_committees(), @@ -329,12 +329,12 @@ impl CiphernodeBuilder { ) .await?; - CiphernodeSelector::attach(&dispatcher, &sortition, &addr, &store); + CiphernodeSelector::attach(&bus, &sortition, &addr, &store); let mut provider_cache = ProviderCaches::new(); let cipher = &self.cipher; - let coordinator = HistoricalEventCoordinator::setup(dispatcher.clone()); + let coordinator = HistoricalEventCoordinator::setup(bus.clone()); let processor = coordinator.clone().recipient(); // TODO: gather an async handle from the event readers that closes when they shutdown and @@ -351,7 +351,7 @@ impl CiphernodeBuilder { .await?; EnclaveSol::attach( &processor, - &dispatcher, + &bus, read_provider.clone(), write_provider.clone(), &chain.contracts.enclave.address(), @@ -366,7 +366,7 @@ impl CiphernodeBuilder { let read_provider = provider_cache.ensure_read_provider(chain).await?; EnclaveSolReader::attach( &processor, - &dispatcher, + &bus, read_provider.clone(), &chain.contracts.enclave.address(), &repositories.enclave_sol_reader(read_provider.chain_id()), @@ -380,7 +380,7 @@ impl CiphernodeBuilder { let read_provider = provider_cache.ensure_read_provider(chain).await?; BondingRegistrySol::attach( &processor, - &dispatcher, + &bus, read_provider.clone(), &chain.contracts.bonding_registry.address(), &repositories.bonding_registry_reader(read_provider.chain_id()), @@ -394,7 +394,7 @@ impl CiphernodeBuilder { let read_provider = provider_cache.ensure_read_provider(chain).await?; CiphernodeRegistrySol::attach( &processor, - &dispatcher, + &bus, read_provider.clone(), &chain.contracts.ciphernode_registry.address(), &repositories.ciphernode_registry_reader(read_provider.chain_id()), @@ -409,7 +409,7 @@ impl CiphernodeBuilder { { Ok(write_provider) => { let _writer = CiphernodeRegistrySol::attach_writer( - &dispatcher, + &bus, write_provider.clone(), &chain.contracts.ciphernode_registry.address(), self.pubkey_agg, @@ -419,7 +419,7 @@ impl CiphernodeBuilder { if self.pubkey_agg && matches!(self.sortition_backend, SortitionBackend::Score(_)) { info!("Attaching CommitteeFinalizer for score sortition"); - e3_aggregator::CommitteeFinalizer::attach(&dispatcher); + e3_aggregator::CommitteeFinalizer::attach(&bus); } } Err(e) => error!( @@ -434,13 +434,13 @@ impl CiphernodeBuilder { coordinator.do_send(CoordinatorStart); // E3 specific setup - let mut e3_builder = E3Router::builder(&dispatcher, store.clone()); + let mut e3_builder = E3Router::builder(&bus, store.clone()); if let Some(KeyshareKind::Threshold) = self.keyshare { let multithread = self.ensure_multithread(); info!("Setting up ThresholdKeyshareExtension"); e3_builder = e3_builder.with(ThresholdKeyshareExtension::create( - &dispatcher, + &bus, &self.cipher, &multithread, &addr, @@ -452,30 +452,24 @@ impl CiphernodeBuilder { || self.plaintext_agg { info!("Setting up FheExtension"); - e3_builder = e3_builder.with(FheExtension::create(&dispatcher, &self.rng)) + e3_builder = e3_builder.with(FheExtension::create(&bus, &self.rng)) } if self.pubkey_agg { info!("Setting up PublicKeyAggregationExtension"); - e3_builder = e3_builder.with(PublicKeyAggregatorExtension::create( - &dispatcher, - &sortition, - )) + e3_builder = e3_builder.with(PublicKeyAggregatorExtension::create(&bus, &sortition)) } if self.plaintext_agg { info!("Setting up PlaintextAggregationExtension (legacy)"); - e3_builder = e3_builder.with(PlaintextAggregatorExtension::create( - &dispatcher, - &sortition, - )) + e3_builder = e3_builder.with(PlaintextAggregatorExtension::create(&bus, &sortition)) } if self.threshold_plaintext_agg { info!("Setting up ThresholdPlaintextAggregatorExtension NEW!"); let multithread = self.ensure_multithread(); e3_builder = e3_builder.with(ThresholdPlaintextAggregatorExtension::create( - &dispatcher, + &bus, &sortition, &multithread, )) @@ -483,8 +477,7 @@ impl CiphernodeBuilder { if matches!(self.keyshare, Some(KeyshareKind::NonThreshold)) { info!("Setting up KeyshareExtension (legacy)!"); - e3_builder = - e3_builder.with(KeyshareExtension::create(&dispatcher, &addr, &self.cipher)) + e3_builder = e3_builder.with(KeyshareExtension::create(&bus, &addr, &self.cipher)) } info!("building..."); e3_builder.build().await?; @@ -492,7 +485,7 @@ impl CiphernodeBuilder { Ok(CiphernodeHandle::new( addr.to_owned(), store, - dispatcher, + bus, history, errors, )) diff --git a/crates/entrypoint/src/start/start.rs b/crates/entrypoint/src/start/start.rs index 6e977f0478..9ba3e3ab35 100644 --- a/crates/entrypoint/src/start/start.rs +++ b/crates/entrypoint/src/start/start.rs @@ -10,8 +10,8 @@ use e3_ciphernode_builder::CiphernodeBuilder; use e3_config::AppConfig; use e3_crypto::Cipher; use e3_data::RepositoriesFactory; +use e3_events::BusHandle; use e3_events::{get_enclave_bus_handle, EnclaveEvent}; -use e3_events::{prelude::*, BusHandle}; use e3_net::{NetEventTranslator, NetRepositoryFactory}; use rand::SeedableRng; use rand_chacha::rand_core::OsRng; diff --git a/crates/events/src/eventbus.rs b/crates/events/src/eventbus.rs index df7d4e8867..883663d9fb 100644 --- a/crates/events/src/eventbus.rs +++ b/crates/events/src/eventbus.rs @@ -76,10 +76,6 @@ impl EventBus { addr } - pub fn manager(source: Addr>) -> BusHandle { - BusHandle::new(source) - } - pub fn pipe(source: &Addr>, dest: &Addr>) { source.do_send(Subscribe::new("*", dest.clone().recipient())) } diff --git a/crates/events/src/eventbus_factory.rs b/crates/events/src/eventbus_factory.rs index 6aecbe69a4..f7312d6f71 100644 --- a/crates/events/src/eventbus_factory.rs +++ b/crates/events/src/eventbus_factory.rs @@ -93,5 +93,5 @@ pub fn get_error_collector() -> Addr> { } pub fn get_enclave_bus_handle() -> BusHandle { - EventBus::manager(get_enclave_event_bus()) + get_enclave_event_bus().into() } diff --git a/crates/evm/src/enclave_sol.rs b/crates/evm/src/enclave_sol.rs index dbb4821960..387116f92c 100644 --- a/crates/evm/src/enclave_sol.rs +++ b/crates/evm/src/enclave_sol.rs @@ -8,11 +8,11 @@ use crate::{ enclave_sol_reader::EnclaveSolReader, enclave_sol_writer::EnclaveSolWriter, event_reader::EvmEventReaderState, helpers::EthProvider, EnclaveEvmEvent, }; -use actix::{Addr, Recipient}; +use actix::Recipient; use alloy::providers::{Provider, WalletProvider}; use anyhow::Result; use e3_data::Repository; -use e3_events::{BusHandle, EnclaveEvent, EventBus}; +use e3_events::{BusHandle, EnclaveEvent}; pub struct EnclaveSol; diff --git a/crates/evm/src/enclave_sol_reader.rs b/crates/evm/src/enclave_sol_reader.rs index aa425cd91d..3a2a938005 100644 --- a/crates/evm/src/enclave_sol_reader.rs +++ b/crates/evm/src/enclave_sol_reader.rs @@ -13,7 +13,7 @@ use alloy::providers::Provider; use alloy::{sol, sol_types::SolEvent}; use anyhow::Result; use e3_data::Repository; -use e3_events::{prelude::*, BusHandle, E3id, EnclaveEvent, EnclaveEventData}; +use e3_events::{BusHandle, E3id, EnclaveEvent, EnclaveEventData}; use e3_utils::utility_types::ArcBytes; use num_bigint::BigUint; use tracing::{error, info, trace}; diff --git a/crates/evm/src/enclave_sol_writer.rs b/crates/evm/src/enclave_sol_writer.rs index 086bb59444..35aade7e7f 100644 --- a/crates/evm/src/enclave_sol_writer.rs +++ b/crates/evm/src/enclave_sol_writer.rs @@ -22,7 +22,7 @@ use e3_events::BusHandle; use e3_events::EnclaveEvent; use e3_events::EnclaveEventData; use e3_events::Shutdown; -use e3_events::{E3id, EnclaveErrorType, PlaintextAggregated, Subscribe}; +use e3_events::{E3id, EnclaveErrorType, PlaintextAggregated}; use tracing::info; sol!( diff --git a/crates/net/src/document_publisher.rs b/crates/net/src/document_publisher.rs index c18bfd1d61..8a5b9bb092 100644 --- a/crates/net/src/document_publisher.rs +++ b/crates/net/src/document_publisher.rs @@ -517,8 +517,9 @@ mod tests { let guard = tracing::subscriber::set_default(subscriber); - let bus = EventBus::::new(EventBusConfig { deduplicate: true }).start(); - let bus = EventBus::manager(bus); + let bus = EventBus::::new(EventBusConfig { deduplicate: true }) + .start() + .into(); let (net_cmd_tx, net_cmd_rx) = mpsc::channel(100); let (net_evt_tx, net_evt_rx) = broadcast::channel(100); let net_evt_rx = Arc::new(net_evt_rx); diff --git a/crates/sortition/src/sortition.rs b/crates/sortition/src/sortition.rs index de674fec58..09ecc5beb9 100644 --- a/crates/sortition/src/sortition.rs +++ b/crates/sortition/src/sortition.rs @@ -12,7 +12,7 @@ use e3_data::{AutoPersist, Persistable, Repository}; use e3_events::{ prelude::*, CiphernodeAdded, CiphernodeRemoved, CommitteeFinalized, CommitteePublished, ConfigurationUpdated, EnclaveErrorType, EnclaveEvent, OperatorActivationChanged, - PlaintextOutputPublished, Seed, Subscribe, TicketBalanceUpdated, + PlaintextOutputPublished, Seed, TicketBalanceUpdated, }; use e3_events::{BusHandle, EnclaveEventData}; use serde::{Deserialize, Serialize}; diff --git a/crates/test-helpers/src/lib.rs b/crates/test-helpers/src/lib.rs index 2f1c009dd4..2057b52416 100644 --- a/crates/test-helpers/src/lib.rs +++ b/crates/test-helpers/src/lib.rs @@ -209,10 +209,3 @@ pub fn encrypt_ciphertext( .collect::>>()?; Ok((ciphertext, plaintext)) } - -fn pad_end(input: &[u64], pad: u64, total: usize) -> Vec { - let len = input.len(); - let mut cop = input.to_vec(); - cop.extend(std::iter::repeat(pad).take(total - len)); - cop -} From e36a9c0a2d7499b16c85fca0547b97fbd602842c Mon Sep 17 00:00:00 2001 From: ryardley Date: Sun, 30 Nov 2025 22:13:44 +0000 Subject: [PATCH 29/36] add required type constraints --- crates/net/src/document_publisher.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/net/src/document_publisher.rs b/crates/net/src/document_publisher.rs index 8a5b9bb092..d5be42ec57 100644 --- a/crates/net/src/document_publisher.rs +++ b/crates/net/src/document_publisher.rs @@ -517,9 +517,10 @@ mod tests { let guard = tracing::subscriber::set_default(subscriber); - let bus = EventBus::::new(EventBusConfig { deduplicate: true }) - .start() - .into(); + let bus: BusHandle = + EventBus::::new(EventBusConfig { deduplicate: true }) + .start() + .into(); let (net_cmd_tx, net_cmd_rx) = mpsc::channel(100); let (net_evt_tx, net_evt_rx) = broadcast::channel(100); let net_evt_rx = Arc::new(net_evt_rx); From 14ea69832a376b15ef7b0fd589134022e20a51d6 Mon Sep 17 00:00:00 2001 From: ryardley Date: Mon, 1 Dec 2025 16:03:38 +0000 Subject: [PATCH 30/36] add wait for poseidon --- .../scripts/deployAndSave/poseidonT3.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/enclave-contracts/scripts/deployAndSave/poseidonT3.ts b/packages/enclave-contracts/scripts/deployAndSave/poseidonT3.ts index f3220ceb82..b301480e28 100644 --- a/packages/enclave-contracts/scripts/deployAndSave/poseidonT3.ts +++ b/packages/enclave-contracts/scripts/deployAndSave/poseidonT3.ts @@ -27,23 +27,26 @@ export const deployAndSavePoseidonT3 = async ({ // probably on the hardhat network // fund the keyless account const [sender] = await ethers.getSigners(); - await sender.sendTransaction({ + let tx = await sender.sendTransaction({ to: poseidon.proxy.from, value: poseidon.proxy.gas, }); + await tx.wait(); // then send the presigned transaction deploying the proxy - await ethers.provider.broadcastTransaction(poseidon.proxy.tx); + tx = await ethers.provider.broadcastTransaction(poseidon.proxy.tx); + await tx.wait(); console.log(`Proxy deployed to: ${poseidon.proxy.address}`); } // Then deploy the hasher, if needed if ((await ethers.provider.getCode(poseidon.PoseidonT3.address)) === "0x") { const [sender] = await ethers.getSigners(); - await sender.sendTransaction({ + let tx = await sender.sendTransaction({ to: poseidon.proxy.address, data: poseidon.PoseidonT3.data, }); + await tx.wait(); console.log(`PoseidonT3 deployed to: ${poseidon.PoseidonT3.address}`); } From 5e96380b0bc9b17c26ed2e97eb2babbb65d21732 Mon Sep 17 00:00:00 2001 From: ryardley Date: Mon, 1 Dec 2025 17:20:42 +0000 Subject: [PATCH 31/36] remove logging --- crates/indexer/src/callback_queue.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/indexer/src/callback_queue.rs b/crates/indexer/src/callback_queue.rs index 44ab4a123f..75d7dd99ba 100644 --- a/crates/indexer/src/callback_queue.rs +++ b/crates/indexer/src/callback_queue.rs @@ -82,9 +82,10 @@ impl CallbackQueue { /// Execute all pending callbacks up to and including the given time pub async fn execute_until_including(&self, time: u64) -> Result<()> { - info!("execute_until_including..."); let handlers = self.inner.take_until_including(time); - info!("found {} handlers", handlers.len()); + if handlers.len() > 0 { + info!("found {} handlers", handlers.len()); + } for callback in handlers { callback().await?; } From a860e878cc02d206487c72dbc4332ed730879365 Mon Sep 17 00:00:00 2001 From: ryardley Date: Mon, 1 Dec 2025 17:38:53 +0000 Subject: [PATCH 32/36] add logging --- crates/evm/src/ciphernode_registry_sol.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/evm/src/ciphernode_registry_sol.rs b/crates/evm/src/ciphernode_registry_sol.rs index a77e635032..a6cf0918f4 100644 --- a/crates/evm/src/ciphernode_registry_sol.rs +++ b/crates/evm/src/ciphernode_registry_sol.rs @@ -441,6 +441,7 @@ pub async fn submit_ticket_to_registry( e3_id: E3id, ticket_number: u64, ) -> Result { + info!("Calling: contract.submitTicket(..)"); let e3_id: U256 = e3_id.try_into()?; let ticket_number = U256::from(ticket_number); let from_address = provider.provider().default_signer_address(); @@ -462,6 +463,7 @@ pub async fn finalize_committee_on_registry Result { + info!("Calling: contract.finalizeCommittee(..)"); let e3_id: U256 = e3_id.try_into()?; let from_address = provider.provider().default_signer_address(); let current_nonce = provider @@ -482,6 +484,7 @@ pub async fn publish_committee_to_registry nodes: OrderedSet, public_key: Vec, ) -> Result { + info!("Calling: contract.publishCommittee(..)"); let e3_id: U256 = e3_id.try_into()?; let public_key = Bytes::from(public_key); let nodes_vec: Vec

= nodes From 10cb5250c29ec410fceb784a965e339725be2fdb Mon Sep 17 00:00:00 2001 From: ryardley Date: Mon, 1 Dec 2025 18:08:21 +0000 Subject: [PATCH 33/36] add logging --- crates/aggregator/src/publickey_aggregator.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/aggregator/src/publickey_aggregator.rs b/crates/aggregator/src/publickey_aggregator.rs index a8bb754f93..d14aa8ec8c 100644 --- a/crates/aggregator/src/publickey_aggregator.rs +++ b/crates/aggregator/src/publickey_aggregator.rs @@ -15,7 +15,7 @@ use e3_fhe::{Fhe, GetAggregatePublicKey}; use e3_sortition::{GetNodesForE3, Sortition}; use e3_utils::ArcBytes; use std::sync::Arc; -use tracing::{error, trace}; +use tracing::{error, info, trace}; #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub enum PublicKeyAggregatorState { @@ -104,7 +104,13 @@ impl PublicKeyAggregator { }; keyshares.insert(keyshare); + info!( + "PublicKeyAggregator got keyshares {}/{}", + keyshares.len(), + threshold_n + ); if keyshares.len() == *threshold_n { + info!("Computing aggregate public key..."); return Ok(PublicKeyAggregatorState::Computing { keyshares: keyshares.clone(), }); @@ -194,6 +200,7 @@ impl Handler for PublicKeyAggregator { type Result = Result<()>; fn handle(&mut self, msg: ComputeAggregate, ctx: &mut Self::Context) -> Self::Result { + info!("Computing Aggregate PublicKey..."); let pubkey = self.fhe.get_aggregate_public_key(GetAggregatePublicKey { keyshares: msg.keyshares.clone(), })?; @@ -212,6 +219,7 @@ impl Handler for PublicKeyAggregator { impl Handler for PublicKeyAggregator { type Result = ResponseActFuture>; fn handle(&mut self, msg: NotifyNetwork, _: &mut Self::Context) -> Self::Result { + info!("Notifying network of PublicKey"); Box::pin( self.sortition .send(GetNodesForE3 { @@ -223,7 +231,7 @@ impl Handler for PublicKeyAggregator { let nodes = res?; let pubkey = msg.pubkey.clone(); - + info!("Sending PublicKeyAggregated..."); let event = PublicKeyAggregated { pubkey, e3_id: msg.e3_id.clone(), From f9be99e5aeca31919ad78f46d9299cbd1bc109d5 Mon Sep 17 00:00:00 2001 From: ryardley Date: Mon, 1 Dec 2025 22:11:10 +0000 Subject: [PATCH 34/36] try buffering keyshare created events --- Cargo.lock | 1 + crates/aggregator/src/ext.rs | 75 ++++++++++++------- .../src/keyshare_created_filter_buffer.rs | 67 +++++++++++++++++ crates/aggregator/src/lib.rs | 1 + crates/aggregator/src/publickey_aggregator.rs | 51 ++++--------- crates/sortition/Cargo.toml | 3 +- 6 files changed, 133 insertions(+), 65 deletions(-) create mode 100644 crates/aggregator/src/keyshare_created_filter_buffer.rs diff --git a/Cargo.lock b/Cargo.lock index 7fd9a9112b..1e941168f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3119,6 +3119,7 @@ dependencies = [ "num-bigint", "rand 0.8.5", "serde", + "tokio", "tracing", ] diff --git a/crates/aggregator/src/ext.rs b/crates/aggregator/src/ext.rs index e6cfe68497..98b273739a 100644 --- a/crates/aggregator/src/ext.rs +++ b/crates/aggregator/src/ext.rs @@ -4,6 +4,9 @@ // without even the implied warranty of MERCHANTABILITY // or FITNESS FOR A PARTICULAR PURPOSE. +use std::sync::Arc; + +use crate::keyshare_created_filter_buffer::KeyshareCreatedFilterBuffer; use crate::{ PlaintextAggregator, PlaintextAggregatorParams, PlaintextAggregatorState, PlaintextRepositoryFactory, PublicKeyAggregator, PublicKeyAggregatorParams, @@ -11,13 +14,14 @@ use crate::{ ThresholdPlaintextAggregatorParams, ThresholdPlaintextAggregatorState, TrBfvPlaintextRepositoryFactory, }; -use actix::{Actor, Addr}; +use actix::{Actor, Addr, Recipient}; use anyhow::{anyhow, Result}; use async_trait::async_trait; -use e3_data::{AutoPersist, RepositoriesFactory}; -use e3_events::prelude::*; +use e3_data::{AutoPersist, Persistable, RepositoriesFactory}; +use e3_events::{prelude::*, E3id}; use e3_events::{BusHandle, EnclaveErrorType, EnclaveEvent, EnclaveEventData}; use e3_fhe::ext::FHE_KEY; +use e3_fhe::Fhe; use e3_multithread::Multithread; use e3_request::{E3Context, E3ContextSnapshot, E3Extension, META_KEY}; use e3_sortition::Sortition; @@ -188,22 +192,16 @@ impl E3Extension for PublicKeyAggregatorExtension { meta.threshold_n, meta.seed, ))); - ctx.set_event_recipient( - "publickey", - Some( - PublicKeyAggregator::new( - PublicKeyAggregatorParams { - fhe: fhe.clone(), - bus: self.bus.clone(), - sortition: self.sortition.clone(), - e3_id, - }, - sync_state, - ) - .start() - .into(), - ), + + let value = create_publickey_aggregator( + fhe.clone(), + self.bus.clone(), + self.sortition.clone(), + e3_id, + sync_state, ); + + ctx.set_event_recipient("publickey", Some(value)); } async fn hydrate(&self, ctx: &mut E3Context, snapshot: &E3ContextSnapshot) -> Result<()> { @@ -229,18 +227,13 @@ impl E3Extension for PublicKeyAggregatorExtension { return Ok(()); }; - - let value = PublicKeyAggregator::new( - PublicKeyAggregatorParams { - fhe: fhe.clone(), - bus: self.bus.clone(), - sortition: self.sortition.clone(), - e3_id: ctx.e3_id.clone(), - }, + let value = create_publickey_aggregator( + fhe.clone(), + self.bus.clone(), + self.sortition.clone(), + ctx.e3_id.clone(), sync_state, - ) - .start() - .into(); + ); // send to context ctx.set_event_recipient("publickey", Some(value)); @@ -249,6 +242,30 @@ impl E3Extension for PublicKeyAggregatorExtension { } } +fn create_publickey_aggregator( + fhe: Arc, + bus: BusHandle, + sortition: Addr, + e3_id: E3id, + sync_state: Persistable, +) -> Recipient { + KeyshareCreatedFilterBuffer::new( + PublicKeyAggregator::new( + PublicKeyAggregatorParams { + fhe, + bus, + sortition, + e3_id, + }, + sync_state, + ) + .start() + .into(), + ) + .start() + .into() +} + pub struct ThresholdPlaintextAggregatorExtension { bus: BusHandle, sortition: Addr, diff --git a/crates/aggregator/src/keyshare_created_filter_buffer.rs b/crates/aggregator/src/keyshare_created_filter_buffer.rs new file mode 100644 index 0000000000..68eb61c493 --- /dev/null +++ b/crates/aggregator/src/keyshare_created_filter_buffer.rs @@ -0,0 +1,67 @@ +use actix::prelude::*; + +use e3_events::{prelude::*, EnclaveEvent, EnclaveEventData}; +use std::collections::HashSet; + +use crate::PublicKeyAggregator; + +pub struct KeyshareCreatedFilterBuffer { + dest: Addr, + committee: Option>, + buffer: Vec, +} + +impl KeyshareCreatedFilterBuffer { + pub fn new(dest: Addr) -> Self { + Self { + dest, + committee: None, + buffer: Vec::new(), + } + } + + fn process_buffered_events(&mut self) { + if let Some(ref committee) = self.committee { + for event in self.buffer.drain(..) { + if let EnclaveEventData::KeyshareCreated(data) = event.get_data() { + if committee.contains(&data.node) { + self.dest.do_send(event); + } + } + } + } + } +} + +impl Actor for KeyshareCreatedFilterBuffer { + type Context = Context; +} + +impl Handler for KeyshareCreatedFilterBuffer { + type Result = (); + + fn handle(&mut self, msg: EnclaveEvent, _ctx: &mut Self::Context) -> Self::Result { + match msg.get_data() { + EnclaveEventData::KeyshareCreated(data) => match &self.committee { + Some(committee) if committee.contains(&data.node) => { + // if the committee is ready then process + self.dest.do_send(msg); + } + None => { + // if not buffer + self.buffer.push(msg); + } + _ => {} + }, + EnclaveEventData::CommitteeFinalized(data) => { + self.dest.do_send(msg.clone()); // forward committee first + self.committee = Some(data.committee.iter().cloned().collect()); + self.process_buffered_events(); + } + _ => { + // forward all other events + self.dest.do_send(msg); + } + } + } +} diff --git a/crates/aggregator/src/lib.rs b/crates/aggregator/src/lib.rs index 1b25bee22c..f335f2c737 100644 --- a/crates/aggregator/src/lib.rs +++ b/crates/aggregator/src/lib.rs @@ -6,6 +6,7 @@ mod committee_finalizer; pub mod ext; +mod keyshare_created_filter_buffer; mod plaintext_aggregator; mod publickey_aggregator; mod repo; diff --git a/crates/aggregator/src/publickey_aggregator.rs b/crates/aggregator/src/publickey_aggregator.rs index d14aa8ec8c..20a401b161 100644 --- a/crates/aggregator/src/publickey_aggregator.rs +++ b/crates/aggregator/src/publickey_aggregator.rs @@ -15,7 +15,7 @@ use e3_fhe::{Fhe, GetAggregatePublicKey}; use e3_sortition::{GetNodesForE3, Sortition}; use e3_utils::ArcBytes; use std::sync::Arc; -use tracing::{error, info, trace}; +use tracing::{error, info}; #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub enum PublicKeyAggregatorState { @@ -152,47 +152,27 @@ impl Handler for PublicKeyAggregator { } impl Handler for PublicKeyAggregator { - type Result = ResponseActFuture>; + type Result = Result<()>; - fn handle(&mut self, event: KeyshareCreated, _: &mut Self::Context) -> Self::Result { - let address = event.node.clone(); + fn handle(&mut self, event: KeyshareCreated, ctx: &mut Self::Context) -> Self::Result { let e3_id = event.e3_id.clone(); let pubkey = event.pubkey.clone(); - Box::pin( - self.sortition - .send(GetNodesForE3 { - e3_id: e3_id.clone(), - chain_id: e3_id.chain_id(), - }) - .into_actor(self) - .map(move |res, act, ctx| { - let nodes = res?; - - if !nodes.contains(&address) { - trace!("Node {} not found in finalized committee", address); - return Ok(()); - } - - if e3_id != act.e3_id { - error!("Wrong e3_id sent to aggregator. This should not happen."); - return Ok(()); - } + if e3_id != self.e3_id { + error!("Wrong e3_id sent to aggregator. This should not happen."); + return Ok(()); + } - act.add_keyshare(pubkey)?; + self.add_keyshare(pubkey)?; - if let Some(PublicKeyAggregatorState::Computing { keyshares }) = - &act.state.get() - { - ctx.notify(ComputeAggregate { - keyshares: keyshares.clone(), - e3_id, - }) - } + if let Some(PublicKeyAggregatorState::Computing { keyshares }) = &self.state.get() { + ctx.notify(ComputeAggregate { + keyshares: keyshares.clone(), + e3_id, + }) + } - Ok(()) - }), - ) + Ok(()) } } @@ -222,6 +202,7 @@ impl Handler for PublicKeyAggregator { info!("Notifying network of PublicKey"); Box::pin( self.sortition + // TODO: we can probably ditch this by listening for CommitteeFinalized .send(GetNodesForE3 { e3_id: msg.e3_id.clone(), chain_id: msg.e3_id.chain_id(), diff --git a/crates/sortition/Cargo.toml b/crates/sortition/Cargo.toml index 096f450ec1..d38cd3bf30 100644 --- a/crates/sortition/Cargo.toml +++ b/crates/sortition/Cargo.toml @@ -21,4 +21,5 @@ num = { workspace = true } rand = { workspace = true } serde = { workspace = true } tracing = { workspace = true } -num-bigint = { workspace = true } \ No newline at end of file +tokio = { workspace = true } +num-bigint = { workspace = true } From 71fc523b2514192fe9ac8bc2cb4e8f393313d5e4 Mon Sep 17 00:00:00 2001 From: ryardley Date: Mon, 1 Dec 2025 22:17:27 +0000 Subject: [PATCH 35/36] headers --- crates/aggregator/src/keyshare_created_filter_buffer.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/aggregator/src/keyshare_created_filter_buffer.rs b/crates/aggregator/src/keyshare_created_filter_buffer.rs index 68eb61c493..1f8846efcc 100644 --- a/crates/aggregator/src/keyshare_created_filter_buffer.rs +++ b/crates/aggregator/src/keyshare_created_filter_buffer.rs @@ -1,3 +1,9 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// +// This file is provided WITHOUT ANY WARRANTY; +// without even the implied warranty of MERCHANTABILITY +// or FITNESS FOR A PARTICULAR PURPOSE. + use actix::prelude::*; use e3_events::{prelude::*, EnclaveEvent, EnclaveEventData}; From af374f0c25420a2945749e21a9bcb50760585eb4 Mon Sep 17 00:00:00 2001 From: ryardley Date: Mon, 1 Dec 2025 22:57:33 +0000 Subject: [PATCH 36/36] public key aggregator does not need sortition --- crates/aggregator/src/ext.rs | 32 ++----- .../src/keyshare_created_filter_buffer.rs | 1 + crates/aggregator/src/publickey_aggregator.rs | 89 +++++++------------ .../src/ciphernode_builder.rs | 2 +- crates/events/src/ordered_set.rs | 2 +- crates/utils/src/utility_types.rs | 2 +- 6 files changed, 44 insertions(+), 84 deletions(-) diff --git a/crates/aggregator/src/ext.rs b/crates/aggregator/src/ext.rs index 98b273739a..abdbbdf489 100644 --- a/crates/aggregator/src/ext.rs +++ b/crates/aggregator/src/ext.rs @@ -149,15 +149,11 @@ impl E3Extension for PlaintextAggregatorExtension { pub struct PublicKeyAggregatorExtension { bus: BusHandle, - sortition: Addr, } impl PublicKeyAggregatorExtension { - pub fn create(bus: &BusHandle, sortition: &Addr) -> Box { - Box::new(Self { - bus: bus.clone(), - sortition: sortition.clone(), - }) + pub fn create(bus: &BusHandle) -> Box { + Box::new(Self { bus: bus.clone() }) } } @@ -193,13 +189,7 @@ impl E3Extension for PublicKeyAggregatorExtension { meta.seed, ))); - let value = create_publickey_aggregator( - fhe.clone(), - self.bus.clone(), - self.sortition.clone(), - e3_id, - sync_state, - ); + let value = create_publickey_aggregator(fhe.clone(), self.bus.clone(), e3_id, sync_state); ctx.set_event_recipient("publickey", Some(value)); } @@ -230,7 +220,6 @@ impl E3Extension for PublicKeyAggregatorExtension { let value = create_publickey_aggregator( fhe.clone(), self.bus.clone(), - self.sortition.clone(), ctx.e3_id.clone(), sync_state, ); @@ -245,22 +234,13 @@ impl E3Extension for PublicKeyAggregatorExtension { fn create_publickey_aggregator( fhe: Arc, bus: BusHandle, - sortition: Addr, e3_id: E3id, sync_state: Persistable, ) -> Recipient { KeyshareCreatedFilterBuffer::new( - PublicKeyAggregator::new( - PublicKeyAggregatorParams { - fhe, - bus, - sortition, - e3_id, - }, - sync_state, - ) - .start() - .into(), + PublicKeyAggregator::new(PublicKeyAggregatorParams { fhe, bus, e3_id }, sync_state) + .start() + .into(), ) .start() .into() diff --git a/crates/aggregator/src/keyshare_created_filter_buffer.rs b/crates/aggregator/src/keyshare_created_filter_buffer.rs index 1f8846efcc..43fc32b111 100644 --- a/crates/aggregator/src/keyshare_created_filter_buffer.rs +++ b/crates/aggregator/src/keyshare_created_filter_buffer.rs @@ -11,6 +11,7 @@ use std::collections::HashSet; use crate::PublicKeyAggregator; +/// Buffer KeyshareCreated events until CommitteeFinalized has been published pub struct KeyshareCreatedFilterBuffer { dest: Addr, committee: Option>, diff --git a/crates/aggregator/src/publickey_aggregator.rs b/crates/aggregator/src/publickey_aggregator.rs index 20a401b161..4c66dbd129 100644 --- a/crates/aggregator/src/publickey_aggregator.rs +++ b/crates/aggregator/src/publickey_aggregator.rs @@ -12,7 +12,7 @@ use e3_events::{ PublicKeyAggregated, Seed, }; use e3_fhe::{Fhe, GetAggregatePublicKey}; -use e3_sortition::{GetNodesForE3, Sortition}; +use e3_sortition::Sortition; use e3_utils::ArcBytes; use std::sync::Arc; use tracing::{error, info}; @@ -23,13 +23,16 @@ pub enum PublicKeyAggregatorState { threshold_n: usize, keyshares: OrderedSet, seed: Seed, + nodes: OrderedSet, }, Computing { keyshares: OrderedSet, + nodes: OrderedSet, }, Complete { public_key: Vec, keyshares: OrderedSet, + nodes: OrderedSet, }, } @@ -39,6 +42,7 @@ impl PublicKeyAggregatorState { threshold_n, keyshares: OrderedSet::new(), seed, + nodes: OrderedSet::new(), } } } @@ -50,17 +54,9 @@ struct ComputeAggregate { pub e3_id: E3id, } -#[derive(Message)] -#[rtype(result = "anyhow::Result<()>")] -struct NotifyNetwork { - pub pubkey: Vec, - pub e3_id: E3id, -} - pub struct PublicKeyAggregator { fhe: Arc, bus: BusHandle, - sortition: Addr, e3_id: E3id, state: Persistable, } @@ -68,7 +64,6 @@ pub struct PublicKeyAggregator { pub struct PublicKeyAggregatorParams { pub fhe: Arc, pub bus: BusHandle, - pub sortition: Addr, pub e3_id: E3id, } @@ -86,17 +81,17 @@ impl PublicKeyAggregator { PublicKeyAggregator { fhe: params.fhe, bus: params.bus, - sortition: params.sortition, e3_id: params.e3_id, state, } } - pub fn add_keyshare(&mut self, keyshare: ArcBytes) -> Result<()> { + pub fn add_keyshare(&mut self, keyshare: ArcBytes, node: String) -> Result<()> { self.state.try_mutate(|mut state| { let PublicKeyAggregatorState::Collecting { threshold_n, keyshares, + nodes, .. } = &mut state else { @@ -104,6 +99,7 @@ impl PublicKeyAggregator { }; keyshares.insert(keyshare); + nodes.insert(node); info!( "PublicKeyAggregator got keyshares {}/{}", keyshares.len(), @@ -112,7 +108,8 @@ impl PublicKeyAggregator { if keyshares.len() == *threshold_n { info!("Computing aggregate public key..."); return Ok(PublicKeyAggregatorState::Computing { - keyshares: keyshares.clone(), + keyshares: std::mem::take(keyshares), + nodes: std::mem::take(nodes), }); } @@ -122,15 +119,14 @@ impl PublicKeyAggregator { pub fn set_pubkey(&mut self, pubkey: Vec) -> Result<()> { self.state.try_mutate(|mut state| { - let PublicKeyAggregatorState::Computing { keyshares } = &mut state else { + let PublicKeyAggregatorState::Computing { keyshares, nodes } = &mut state else { return Ok(state); }; - let keyshares = keyshares.to_owned(); - Ok(PublicKeyAggregatorState::Complete { public_key: pubkey, - keyshares, + keyshares: std::mem::take(keyshares), + nodes: std::mem::take(nodes), }) }) } @@ -157,15 +153,16 @@ impl Handler for PublicKeyAggregator { fn handle(&mut self, event: KeyshareCreated, ctx: &mut Self::Context) -> Self::Result { let e3_id = event.e3_id.clone(); let pubkey = event.pubkey.clone(); + let node = event.node.clone(); if e3_id != self.e3_id { error!("Wrong e3_id sent to aggregator. This should not happen."); return Ok(()); } - self.add_keyshare(pubkey)?; + self.add_keyshare(pubkey, node)?; - if let Some(PublicKeyAggregatorState::Computing { keyshares }) = &self.state.get() { + if let Some(PublicKeyAggregatorState::Computing { keyshares, .. }) = &self.state.get() { ctx.notify(ComputeAggregate { keyshares: keyshares.clone(), e3_id, @@ -182,49 +179,31 @@ impl Handler for PublicKeyAggregator { fn handle(&mut self, msg: ComputeAggregate, ctx: &mut Self::Context) -> Self::Result { info!("Computing Aggregate PublicKey..."); let pubkey = self.fhe.get_aggregate_public_key(GetAggregatePublicKey { - keyshares: msg.keyshares.clone(), + keyshares: msg.keyshares, })?; // Update the local state - self.set_pubkey(pubkey.clone())?; - - ctx.notify(NotifyNetwork { - pubkey, - e3_id: msg.e3_id, - }); + self.set_pubkey(pubkey)?; + + if let Some(PublicKeyAggregatorState::Complete { + public_key: pubkey, + nodes, + .. + }) = self.state.get() + { + info!("Notifying network of PublicKey"); + info!("Sending PublicKeyAggregated..."); + let event = PublicKeyAggregated { + pubkey, + e3_id: msg.e3_id, + nodes, + }; + self.bus.publish(event); + } Ok(()) } } -impl Handler for PublicKeyAggregator { - type Result = ResponseActFuture>; - fn handle(&mut self, msg: NotifyNetwork, _: &mut Self::Context) -> Self::Result { - info!("Notifying network of PublicKey"); - Box::pin( - self.sortition - // TODO: we can probably ditch this by listening for CommitteeFinalized - .send(GetNodesForE3 { - e3_id: msg.e3_id.clone(), - chain_id: msg.e3_id.chain_id(), - }) - .into_actor(self) - .map(move |res, act, _| { - let nodes = res?; - - let pubkey = msg.pubkey.clone(); - info!("Sending PublicKeyAggregated..."); - let event = PublicKeyAggregated { - pubkey, - e3_id: msg.e3_id.clone(), - nodes: OrderedSet::from(nodes), - }; - act.bus.publish(event); - Ok(()) - }), - ) - } -} - impl Handler for PublicKeyAggregator { type Result = (); fn handle(&mut self, _: Die, ctx: &mut Self::Context) -> Self::Result { diff --git a/crates/ciphernode-builder/src/ciphernode_builder.rs b/crates/ciphernode-builder/src/ciphernode_builder.rs index 6bf4f29514..861b567a31 100644 --- a/crates/ciphernode-builder/src/ciphernode_builder.rs +++ b/crates/ciphernode-builder/src/ciphernode_builder.rs @@ -457,7 +457,7 @@ impl CiphernodeBuilder { if self.pubkey_agg { info!("Setting up PublicKeyAggregationExtension"); - e3_builder = e3_builder.with(PublicKeyAggregatorExtension::create(&bus, &sortition)) + e3_builder = e3_builder.with(PublicKeyAggregatorExtension::create(&bus)) } if self.plaintext_agg { diff --git a/crates/events/src/ordered_set.rs b/crates/events/src/ordered_set.rs index 976f42467b..13f08295aa 100644 --- a/crates/events/src/ordered_set.rs +++ b/crates/events/src/ordered_set.rs @@ -9,7 +9,7 @@ use std::collections::BTreeSet; use std::fmt; use std::hash::{Hash, Hasher}; -#[derive(Clone, Serialize, Deserialize)] +#[derive(Clone, Default, Serialize, Deserialize)] pub struct OrderedSet(BTreeSet); impl OrderedSet { diff --git a/crates/utils/src/utility_types.rs b/crates/utils/src/utility_types.rs index bb71734dd6..59b2fedd8e 100644 --- a/crates/utils/src/utility_types.rs +++ b/crates/utils/src/utility_types.rs @@ -18,7 +18,7 @@ use crate::formatters::hexf; pub type SharedRng = Arc>; -#[derive(Clone, PartialEq, Eq, Hash)] +#[derive(Clone, Default, PartialEq, Eq, Hash)] pub struct ArcBytes(Arc>); impl ArcBytes {