Skip to content

Commit

Permalink
Tidy up request router
Browse files Browse the repository at this point in the history
  • Loading branch information
ryardley committed Oct 3, 2024
1 parent a505627 commit 7b8ed9c
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 68 deletions.
4 changes: 2 additions & 2 deletions packages/ciphernode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sequenceDiagram
autonumber
participant EVM as EVM
participant CS as CiphernodeSelector
participant E3 as E3RequestManager
participant E3 as E3RequestRouter
participant KS as Keyshare
participant PKA as PublicKeyAggregator
participant S as Sortition
Expand All @@ -31,7 +31,7 @@ sequenceDiagram
sequenceDiagram
autonumber
participant EVM as EVM
participant E3 as E3RequestManager
participant E3 as E3RequestRouter
participant KS as Keyshare
participant PTA as PlaintextAggregator
participant S as Sortition
Expand Down
7 changes: 0 additions & 7 deletions packages/ciphernode/core/src/e3/mod.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// CiphernodeSelector is an actor that determines if a ciphernode
use actix::prelude::*;
use crate::{enclave_core::{CiphernodeSelected, EnclaveEvent, EventBus, Subscribe}, sortition::{GetHasNode, Sortition}};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::enclave_core::{E3Requested, EnclaveEvent, Seed};

use super::e3_request::EventHook;
use super::EventHook;


#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CommitteeMeta {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::{collections::HashMap, sync::Arc};

use std::{collections::HashMap, sync::Arc};
use actix::{Actor, Addr, Context, Handler, Recipient};

use crate::{enclave_core::{E3id, EnclaveEvent, EventBus, Subscribe}, fhe::Fhe, keyshare::Keyshare, plaintext_aggregator::PlaintextAggregator, publickey_aggregator::PublicKeyAggregator};

use super::CommitteeMeta;

#[derive(Default)]
Expand Down Expand Up @@ -66,37 +64,64 @@ impl E3RequestContext {
}
}

/// Format of the hook that needs to be passed to E3RequestRouter
pub type EventHook = Box<dyn FnMut(&mut E3RequestContext, EnclaveEvent)>;

/// E3RequestRouter will register hooks that receive an E3_id specific context. After hooks
/// have run e3_id specific messages are forwarded to all instances on the context. This enables
/// hooks to lazily register instances that have the correct dependencies available per e3_id
/// request
// TODO: setup typestate pattern so that we have to place hooks within correct order of
// dependencies
pub struct E3RequestManager {
pub struct E3RequestRouter {
contexts: HashMap<E3id, E3RequestContext>,
hooks: Vec<EventHook>,
buffer: EventBuffer,
}

impl E3RequestManager {
pub fn builder(bus: Addr<EventBus>) -> E3RequestManagerBuilder {
E3RequestManagerBuilder {
impl E3RequestRouter {
pub fn builder(bus: Addr<EventBus>) -> E3RequestRouterBuilder {
E3RequestRouterBuilder {
bus,
hooks: vec![],
}
}
}

pub struct E3RequestManagerBuilder {
impl Actor for E3RequestRouter {
type Context = Context<Self>;
}

impl Handler<EnclaveEvent> for E3RequestRouter {
type Result = ();
fn handle(&mut self, msg: EnclaveEvent, _: &mut Self::Context) -> Self::Result {
let Some(e3_id) = msg.get_e3_id() else {
return;
};

let context = self.contexts.entry(e3_id).or_default();

for hook in &mut self.hooks {
hook(context, msg.clone());
}

context.forward_message(&msg, &mut self.buffer);
}
}


pub struct E3RequestRouterBuilder {
pub bus: Addr<EventBus>,
pub hooks: Vec<EventHook>,
}
impl E3RequestManagerBuilder {
impl E3RequestRouterBuilder {
pub fn add_hook(mut self, listener: EventHook) -> Self {
self.hooks.push(listener);
self
}

pub fn build(self) -> Addr<E3RequestManager> {
let e3r = E3RequestManager {
pub fn build(self) -> Addr<E3RequestRouter> {
let e3r = E3RequestRouter {
contexts: HashMap::new(),
hooks: self.hooks,
buffer: EventBuffer::default(),
Expand All @@ -108,24 +133,3 @@ impl E3RequestManagerBuilder {
addr
}
}

impl Actor for E3RequestManager {
type Context = Context<Self>;
}

impl Handler<EnclaveEvent> for E3RequestManager {
type Result = ();
fn handle(&mut self, msg: EnclaveEvent, _: &mut Self::Context) -> Self::Result {
let Some(e3_id) = msg.get_e3_id() else {
return;
};

let context = self.contexts.entry(e3_id).or_default();

for hook in &mut self.hooks {
hook(context, msg.clone());
}

context.forward_message(&msg, &mut self.buffer);
}
}
6 changes: 6 additions & 0 deletions packages/ciphernode/core/src/enclave_core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
mod events;
mod eventbus;
mod ordered_set;
mod e3_request_router;
mod committee_meta;
mod ciphernode_selector;

pub use events::*;
pub use eventbus::*;
pub use ordered_set::*;
pub use e3_request_router::*;
pub use committee_meta::*;
pub use ciphernode_selector::*;

9 changes: 4 additions & 5 deletions packages/ciphernode/core/src/enclave_node/main_aggregator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
e3::{CommitteeMetaFactory, E3RequestManager},
enclave_core::EventBus,
enclave_core::{EventBus,CommitteeMetaFactory, E3RequestRouter},
evm::{connect_evm_caller, connect_evm_ciphernode_registry, connect_evm_enclave},
fhe::FheFactory,
logger::SimpleLogger,
Expand All @@ -22,7 +21,7 @@ use tokio::task::JoinHandle;
/// Suprvises all children
// TODO: add supervision logic
pub struct MainAggregator {
e3_manager: Addr<E3RequestManager>,
e3_manager: Addr<E3RequestRouter>,
bus: Addr<EventBus>,
sortition: Addr<Sortition>,
p2p: Addr<P2p>,
Expand All @@ -33,7 +32,7 @@ impl MainAggregator {
bus: Addr<EventBus>,
sortition: Addr<Sortition>,
p2p: Addr<P2p>,
e3_manager: Addr<E3RequestManager>,
e3_manager: Addr<E3RequestRouter>,
) -> Self {
Self {
e3_manager,
Expand Down Expand Up @@ -69,7 +68,7 @@ impl MainAggregator {
)
.await;

let e3_manager = E3RequestManager::builder(bus.clone())
let e3_manager = E3RequestRouter::builder(bus.clone())
.add_hook(CommitteeMetaFactory::create())
.add_hook(FheFactory::create(rng))
.add_hook(PublicKeyAggregatorFactory::create(
Expand Down
9 changes: 4 additions & 5 deletions packages/ciphernode/core/src/enclave_node/main_ciphernode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
data::Data,
e3::{CiphernodeSelector, CommitteeMetaFactory, E3RequestManager},
enclave_core::EventBus,
enclave_core::{CiphernodeSelector, CommitteeMetaFactory, E3RequestRouter, EventBus},
evm::{connect_evm_ciphernode_registry, connect_evm_enclave},
fhe::FheFactory,
keyshare::KeyshareFactory,
Expand All @@ -25,7 +24,7 @@ pub struct MainCiphernode {
data: Addr<Data>,
sortition: Addr<Sortition>,
selector: Addr<CiphernodeSelector>,
e3_manager: Addr<E3RequestManager>,
e3_manager: Addr<E3RequestRouter>,
p2p: Addr<P2p>,
}

Expand All @@ -37,7 +36,7 @@ impl MainCiphernode {
sortition: Addr<Sortition>,
selector: Addr<CiphernodeSelector>,
p2p: Addr<P2p>,
e3_manager: Addr<E3RequestManager>,
e3_manager: Addr<E3RequestRouter>,
) -> Self {
Self {
addr,
Expand Down Expand Up @@ -68,7 +67,7 @@ impl MainCiphernode {
connect_evm_enclave(bus.clone(), rpc_url, enclave_contract).await;
let _ = connect_evm_ciphernode_registry(bus.clone(), rpc_url, registry_contract).await;

let e3_manager = E3RequestManager::builder(bus.clone())
let e3_manager = E3RequestRouter::builder(bus.clone())
.add_hook(CommitteeMetaFactory::create())
.add_hook(FheFactory::create(rng))
.add_hook(KeyshareFactory::create(
Expand Down
6 changes: 2 additions & 4 deletions packages/ciphernode/core/src/fhe/fhe.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::set_up_crp;
use crate::enclave_core::{E3Requested, EnclaveEvent, EventHook, OrderedSet, Seed};
use anyhow::*;
use fhe::{
bfv::{
Expand All @@ -10,10 +12,6 @@ use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;
use std::sync::{Arc, Mutex};

use crate::{e3::EventHook, enclave_core::{E3Requested, EnclaveEvent, OrderedSet, Seed}};

use super::set_up_crp;

pub struct GetAggregatePublicKey {
pub keyshares: OrderedSet<Vec<u8>>,
}
Expand Down
4 changes: 1 addition & 3 deletions packages/ciphernode/core/src/keyshare/keyshare.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::{
data::{Data, Get, Insert},
e3::EventHook,
enclave_core::{
CiphernodeSelected, CiphertextOutputPublished, DecryptionshareCreated, EnclaveErrorType,
EnclaveEvent, EventBus, FromError, KeyshareCreated,
CiphernodeSelected, CiphertextOutputPublished, DecryptionshareCreated, EnclaveErrorType, EnclaveEvent, EventBus, EventHook, FromError, KeyshareCreated
},
fhe::{DecryptCiphertext, Fhe},
};
Expand Down
22 changes: 17 additions & 5 deletions packages/ciphernode/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// #![warn(missing_docs, unused_imports)]

mod data;
mod e3;
mod enclave_core;
mod enclave_node;
mod evm;
Expand All @@ -16,16 +15,29 @@ mod publickey_aggregator;
mod sortition;
mod utils;

pub use fhe::encode_bfv_params;
pub use fhe::setup_bfv_params;
pub use enclave_node::MainAggregator;
pub use enclave_node::MainCiphernode;
pub use fhe::encode_bfv_params;
pub use fhe::setup_bfv_params;

// TODO: move these out to a test folder
#[cfg(test)]
mod tests {
use crate::{
data::Data, e3::{CommitteeMetaFactory, CiphernodeSelector, E3RequestManager}, enclave_core::{CiphernodeAdded, CiphernodeSelected, CiphertextOutputPublished, DecryptionshareCreated, E3Requested, E3id, EnclaveEvent, EventBus, GetHistory, KeyshareCreated, PlaintextAggregated, PublicKeyAggregated, ResetHistory, Seed}, fhe::{setup_crp_params, FheFactory, ParamsWithCrp, SharedRng}, keyshare::KeyshareFactory, logger::SimpleLogger, p2p::P2p, plaintext_aggregator::PlaintextAggregatorFactory, publickey_aggregator::PublicKeyAggregatorFactory, sortition::Sortition
data::Data,
enclave_core::{
CiphernodeAdded, CiphernodeSelected, CiphernodeSelector, CiphertextOutputPublished,
CommitteeMetaFactory, DecryptionshareCreated, E3RequestRouter, E3Requested, E3id,
EnclaveEvent, EventBus, GetHistory, KeyshareCreated, PlaintextAggregated,
PublicKeyAggregated, ResetHistory, Seed,
},
fhe::{setup_crp_params, FheFactory, ParamsWithCrp, SharedRng},
keyshare::KeyshareFactory,
logger::SimpleLogger,
p2p::P2p,
plaintext_aggregator::PlaintextAggregatorFactory,
publickey_aggregator::PublicKeyAggregatorFactory,
sortition::Sortition,
};
use actix::prelude::*;
use alloy::primitives::Address;
Expand Down Expand Up @@ -56,7 +68,7 @@ mod tests {
let sortition = Sortition::attach(bus.clone());
CiphernodeSelector::attach(bus.clone(), sortition.clone(), addr);

E3RequestManager::builder(bus.clone())
E3RequestRouter::builder(bus.clone())
.add_hook(CommitteeMetaFactory::create())
.add_hook(FheFactory::create(rng))
.add_hook(PublicKeyAggregatorFactory::create(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
e3::EventHook,
enclave_core::{
DecryptionshareCreated, E3id, EnclaveEvent, EventBus, OrderedSet, PlaintextAggregated, Seed,
DecryptionshareCreated, E3id, EnclaveEvent, EventBus, EventHook, OrderedSet,
PlaintextAggregated, Seed,
},
fhe::{Fhe, GetAggregatePlaintext},
sortition::{GetHasNode, Sortition},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
e3::EventHook,
enclave_core::{
E3id, EnclaveEvent, EventBus, KeyshareCreated, OrderedSet, PublicKeyAggregated, Seed,
E3id, EnclaveEvent, EventBus, EventHook, KeyshareCreated, OrderedSet, PublicKeyAggregated,
Seed,
},
fhe::{Fhe, GetAggregatePublicKey},
sortition::{GetHasNode, Sortition},
Expand Down

0 comments on commit 7b8ed9c

Please sign in to comment.