Skip to content

Commit

Permalink
Add some information about actors and their intent (#13)
Browse files Browse the repository at this point in the history
* Add some information about actors and their intent

* Add comment
  • Loading branch information
ryardley authored Aug 23, 2024
1 parent 925a853 commit e48b453
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 13 deletions.
8 changes: 4 additions & 4 deletions packages/ciphernode/core/src/committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ use crate::{
fhe::Fhe,
};

pub struct Committee {
pub struct CommitteeManager {
bus: Addr<EventBus>,
fhe: Addr<Fhe>,
aggregators: HashMap<E3id, Addr<CommitteeKey>>,
}

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

impl Committee {
impl CommitteeManager {
pub fn new(bus: Addr<EventBus>, fhe: Addr<Fhe>) -> Self {
Self {
bus,
Expand All @@ -29,7 +29,7 @@ impl Committee {
}
}

impl Handler<EnclaveEvent> for Committee {
impl Handler<EnclaveEvent> for CommitteeManager {
type Result = ();

fn handle(&mut self, event: EnclaveEvent, _ctx: &mut Self::Context) -> Self::Result {
Expand Down
6 changes: 6 additions & 0 deletions packages/ciphernode/core/src/committee_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ pub struct CommitteeKey {
state: CommitteeKeyState,
}

/// Aggregate PublicKey for a committee of nodes. This actor listens for KeyshareCreated events
/// around a particular e3_id and aggregates the public key based on this and once done broadcasts
/// a EnclaveEvent::PublicKeyAggregated event on the event bus. Note events are hashed and
/// identical events will not be triggered twice.
/// It is expected to change this mechanism as we work through adversarial scenarios and write tests
/// for them.
impl CommitteeKey {
pub fn new(fhe: Addr<Fhe>, bus: Addr<EventBus>, e3_id: E3id, nodecount: usize) -> Self {
CommitteeKey {
Expand Down
16 changes: 16 additions & 0 deletions packages/ciphernode/core/src/enclave_contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

use actix::{Actor, Context};

/// Manage an internal web3 instance and express protocol specific behaviour through the events it
/// accepts and emits to the EventBus
/// Monitor contract events using `contract.events().create_filter()` and rebroadcast to eventbus by
/// creating `EnclaveEvent` events
/// Delegate signing to a separate actor responsible for managing Eth keys
/// Accept eventbus events and forward as appropriate contract calls as required
pub struct EnclaveContract;

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


6 changes: 6 additions & 0 deletions packages/ciphernode/core/src/eventbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ impl Subscribe {
#[rtype(result = "Vec<EnclaveEvent>")]
pub struct GetHistory;


/// Central EventBus for each node. Actors publish events to this bus by sending it EnclaveEvents.
/// All events sent to this bus are assumed to be published over the network via pubsub.
/// Other actors such as the P2p and Evm actor connect to outside services and control which events
/// actually get published as well as ensure that local events are not rebroadcast locally after
/// being published.
pub struct EventBus {
capture: bool,
history: Vec<EnclaveEvent>,
Expand Down
11 changes: 10 additions & 1 deletion packages/ciphernode/core/src/fhe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ impl WrappedPublicKeyShare {
}
}

/// Wrapped PublicKey. This is wrapped to provide an inflection point
/// as we use this library elsewhere we only implement traits as we need them
/// and avoid exposing underlying structures from fhe.rs
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WrappedPublicKey(pub PublicKey);

Expand All @@ -80,15 +83,21 @@ impl PartialOrd for WrappedPublicKey {
}
}


/// Wrapped SecretKey. This is wrapped to provide an inflection point
/// as we use this library elsewhere we only implement traits as we need them
/// and avoid exposing underlying structures from fhe.rs
// We should favor consuming patterns and avoid cloning and copying this value around in memory.
// Underlying key Zeroizes on drop
#[derive(PartialEq)]
pub struct WrappedSecretKey(pub SecretKey);

impl WrappedSecretKey {
pub fn unsafe_to_vec(&self) -> Vec<u8> {
serialize_box_i64(self.0.coeffs.clone())
}
}

/// Fhe library adaptor. All FHE computations should happen through this actor.
pub struct Fhe {
params: Arc<BfvParameters>,
crp: CommonRandomPoly,
Expand Down
19 changes: 15 additions & 4 deletions packages/ciphernode/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mod eventbus;
mod events;
mod fhe;
mod ordered_set;
mod p2p;
mod enclave_contract;

// pub struct Core {
// pub name: String,
Expand All @@ -34,7 +36,7 @@ mod tests {

use crate::{
ciphernode::Ciphernode,
committee::Committee,
committee::CommitteeManager,
data::Data,
eventbus::{EventBus, GetHistory, Subscribe},
events::{ComputationRequested, E3id, EnclaveEvent, KeyshareCreated, PublicKeyAggregated},
Expand Down Expand Up @@ -94,8 +96,8 @@ mod tests {
Ok((pk, rng))
}

fn setup_committee_manager(bus: Addr<EventBus>, fhe: Addr<Fhe>) -> Addr<Committee> {
let committee = Committee::new(bus.clone(), fhe.clone()).start();
fn setup_committee_manager(bus: Addr<EventBus>, fhe: Addr<Fhe>) -> Addr<CommitteeManager> {
let committee = CommitteeManager::new(bus.clone(), fhe.clone()).start();

bus.do_send(Subscribe::new(
"ComputationRequested",
Expand All @@ -118,7 +120,7 @@ mod tests {
}

#[actix::test]
async fn test_ciphernode() -> Result<()> {
async fn test_public_key_aggregation() -> Result<()> {
// Setup EventBus
let bus = EventBus::new(true).start();

Expand Down Expand Up @@ -200,4 +202,13 @@ mod tests {

Ok(())
}

// TODO: Test p2p
fn test_p2p_event_broadcasting() {
// Setup two Vec<u8> channels to simulate libp2p
// 1. command channel
// 2. event channel
// Pass them to the p2p actor
// connect the p2p actor to the event bus actor and monitor which events are broadcast
}
}
21 changes: 17 additions & 4 deletions packages/ciphernode/core/src/p2p.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
/// Actor for connecting to an libp2p client via it's mpsc channel interface
/// This Actor should be responsible for
/// 1. Sending and Recieving Vec<u8> messages with libp2p
/// 2. Converting between Vec<u8> and EnclaveEvents::Xxxxxxxxx()
/// 3. Broadcasting over the local eventbus
/// 4. Listening to the local eventbus for messages to be published to libp2p
use actix::{Actor, Context};

use tokio::sync::mpsc::{Receiver, Sender};
use p2p::EnclaveRouter;
pub struct P2pActor;

pub struct P2p;

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


impl P2p {
pub fn new() {
// Construct owning Libp2p module
}
pub fn from_channel(tx:Sender<Vec<u8>>, rx:Receiver<Vec<u8>>){
// Construct from tx/rx
}
}

0 comments on commit e48b453

Please sign in to comment.