Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename events #38

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions packages/ciphernode/core/src/ciphernode.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
data::{Data, Insert},
eventbus::EventBus,
events::{ComputationRequested, EnclaveEvent, KeyshareCreated},
events::{CommitteeRequested, EnclaveEvent, KeyshareCreated},
fhe::{Fhe, GenerateKeyshare},
DecryptCiphertext, DecryptionRequested, DecryptionshareCreated, Get, Subscribe,
CiphertextOutputPublished, DecryptCiphertext, DecryptionshareCreated, Get, Subscribe,
};
use actix::prelude::*;
use anyhow::Result;
Expand All @@ -26,10 +26,13 @@ impl Ciphernode {
pub async fn attach(bus: Addr<EventBus>, fhe: Addr<Fhe>, data: Addr<Data>) -> Addr<Self> {
let node = Ciphernode::new(bus.clone(), fhe, data).start();
let _ = bus
.send(Subscribe::new("ComputationRequested", node.clone().into()))
.send(Subscribe::new("CommitteeRequested", node.clone().into()))
.await;
let _ = bus
.send(Subscribe::new("DecryptionRequested", node.clone().into()))
.send(Subscribe::new(
"CiphertextOutputPublished",
node.clone().into(),
))
.await;
node
}
Expand All @@ -40,32 +43,28 @@ impl Handler<EnclaveEvent> for Ciphernode {

fn handle(&mut self, event: EnclaveEvent, ctx: &mut Context<Self>) -> Self::Result {
match event {
EnclaveEvent::ComputationRequested { data, .. } => ctx.address().do_send(data),
EnclaveEvent::DecryptionRequested { data, .. } => ctx.address().do_send(data),
EnclaveEvent::CommitteeRequested { data, .. } => ctx.address().do_send(data),
EnclaveEvent::CiphertextOutputPublished { data, .. } => ctx.address().do_send(data),
_ => (),
}
}
}

impl Handler<ComputationRequested> for Ciphernode {
impl Handler<CommitteeRequested> for Ciphernode {
type Result = ResponseFuture<()>;

fn handle(&mut self, event: ComputationRequested, _: &mut Context<Self>) -> Self::Result {
fn handle(&mut self, event: CommitteeRequested, _: &mut Context<Self>) -> Self::Result {
let fhe = self.fhe.clone();
let data = self.data.clone();
let bus = self.bus.clone();
Box::pin(async {
on_computation_requested(fhe, data, bus, event)
.await
.unwrap()
})
Box::pin(async { on_committee_requested(fhe, data, bus, event).await.unwrap() })
}
}

impl Handler<DecryptionRequested> for Ciphernode {
impl Handler<CiphertextOutputPublished> for Ciphernode {
type Result = ResponseFuture<()>;

fn handle(&mut self, event: DecryptionRequested, _: &mut Context<Self>) -> Self::Result {
fn handle(&mut self, event: CiphertextOutputPublished, _: &mut Context<Self>) -> Self::Result {
let fhe = self.fhe.clone();
let data = self.data.clone();
let bus = self.bus.clone();
Expand All @@ -77,13 +76,13 @@ impl Handler<DecryptionRequested> for Ciphernode {
}
}

async fn on_computation_requested(
async fn on_committee_requested(
fhe: Addr<Fhe>,
data: Addr<Data>,
bus: Addr<EventBus>,
event: ComputationRequested,
event: CommitteeRequested,
) -> Result<()> {
let ComputationRequested { e3_id, .. } = event;
let CommitteeRequested { e3_id, .. } = event;
// generate keyshare
let (sk, pubkey) = fhe.send(GenerateKeyshare {}).await??;

Expand All @@ -108,9 +107,12 @@ async fn on_decryption_requested(
fhe: Addr<Fhe>,
data: Addr<Data>,
bus: Addr<EventBus>,
event: DecryptionRequested,
event: CiphertextOutputPublished,
) -> Result<()> {
let DecryptionRequested { e3_id, ciphertext } = event;
let CiphertextOutputPublished {
e3_id,
ciphertext_output,
} = event;

// get secret key by id from data
let Some(unsafe_secret) = data.send(Get(format!("{}/sk", e3_id).into())).await? else {
Expand All @@ -119,7 +121,7 @@ async fn on_decryption_requested(

let decryption_share = fhe
.send(DecryptCiphertext {
ciphertext,
ciphertext: ciphertext_output,
unsafe_secret,
})
.await??;
Expand Down
8 changes: 4 additions & 4 deletions packages/ciphernode/core/src/committee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ impl CommitteeManager {
pub fn attach(bus: Addr<EventBus>, fhe: Addr<Fhe>) -> Addr<Self> {
let addr = CommitteeManager::new(bus.clone(), fhe).start();
bus.do_send(Subscribe::new(
"ComputationRequested",
"CommitteeRequested",
addr.clone().recipient(),
));
bus.do_send(Subscribe::new("KeyshareCreated", addr.clone().into()));
bus.do_send(Subscribe::new("DecryptionRequested", addr.clone().into()));
bus.do_send(Subscribe::new("CiphertextOutputPublished", addr.clone().into()));
bus.do_send(Subscribe::new("DecryptionshareCreated", addr.clone().into()));
bus.do_send(Subscribe::new("DecryptionOutputPublished", addr.clone().into()));
addr
Expand All @@ -61,7 +61,7 @@ impl Handler<EnclaveEvent> for CommitteeManager {

fn handle(&mut self, event: EnclaveEvent, _ctx: &mut Self::Context) -> Self::Result {
match event {
EnclaveEvent::ComputationRequested { data, .. } => {
EnclaveEvent::CommitteeRequested { data, .. } => {
// start up a new key
let key = CommitteeKey::new(
self.fhe.clone(),
Expand Down Expand Up @@ -92,7 +92,7 @@ impl Handler<EnclaveEvent> for CommitteeManager {
key.do_send(Die);
self.keys.remove(&data.e3_id);
}
EnclaveEvent::DecryptionRequested { data, .. } => {
EnclaveEvent::CiphertextOutputPublished { data, .. } => {
let Some(meta) = self.meta.get(&data.e3_id) else {
// TODO: setup proper logger / telemetry
println!("E3Id not found in committee");
Expand Down
30 changes: 15 additions & 15 deletions packages/ciphernode/core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ pub enum EnclaveEvent {
id: EventId,
data: KeyshareCreated,
},
ComputationRequested {
CommitteeRequested {
id: EventId,
data: ComputationRequested,
data: CommitteeRequested,
},
PublicKeyAggregated {
id: EventId,
data: PublicKeyAggregated,
},
DecryptionRequested {
CiphertextOutputPublished {
id: EventId,
data: DecryptionRequested,
data: CiphertextOutputPublished,
},
DecryptionshareCreated {
id: EventId,
Expand Down Expand Up @@ -100,9 +100,9 @@ impl From<EnclaveEvent> for EventId {
fn from(value: EnclaveEvent) -> Self {
match value {
EnclaveEvent::KeyshareCreated { id, .. } => id,
EnclaveEvent::ComputationRequested { id, .. } => id,
EnclaveEvent::CommitteeRequested { id, .. } => id,
EnclaveEvent::PublicKeyAggregated { id, .. } => id,
EnclaveEvent::DecryptionRequested { id, .. } => id,
EnclaveEvent::CiphertextOutputPublished { id, .. } => id,
EnclaveEvent::DecryptionshareCreated { id, .. } => id,
EnclaveEvent::DecryptedOutputPublished { id, .. } => id,
}
Expand All @@ -118,9 +118,9 @@ impl From<KeyshareCreated> for EnclaveEvent {
}
}

impl From<ComputationRequested> for EnclaveEvent {
fn from(data: ComputationRequested) -> Self {
EnclaveEvent::ComputationRequested {
impl From<CommitteeRequested> for EnclaveEvent {
fn from(data: CommitteeRequested) -> Self {
EnclaveEvent::CommitteeRequested {
id: EventId::from(data.clone()),
data: data.clone(),
}
Expand All @@ -136,9 +136,9 @@ impl From<PublicKeyAggregated> for EnclaveEvent {
}
}

impl From<DecryptionRequested> for EnclaveEvent {
fn from(data: DecryptionRequested) -> Self {
EnclaveEvent::DecryptionRequested {
impl From<CiphertextOutputPublished> for EnclaveEvent {
fn from(data: CiphertextOutputPublished) -> Self {
EnclaveEvent::CiphertextOutputPublished {
id: EventId::from(data.clone()),
data: data.clone(),
}
Expand Down Expand Up @@ -191,7 +191,7 @@ pub struct PublicKeyAggregated {

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[rtype(result = "()")]
pub struct ComputationRequested {
pub struct CommitteeRequested {
pub e3_id: E3id,
pub nodecount: usize,
pub threshold: usize,
Expand All @@ -204,9 +204,9 @@ pub struct ComputationRequested {

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[rtype(result = "()")]
pub struct DecryptionRequested {
pub struct CiphertextOutputPublished {
pub e3_id: E3id,
pub ciphertext: Vec<u8>
pub ciphertext_output: Vec<u8>
}

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down
18 changes: 9 additions & 9 deletions packages/ciphernode/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ mod tests {
committee::CommitteeManager,
data::Data,
eventbus::{EventBus, GetHistory},
events::{ComputationRequested, E3id, EnclaveEvent, KeyshareCreated, PublicKeyAggregated},
events::{CommitteeRequested, E3id, EnclaveEvent, KeyshareCreated, PublicKeyAggregated},
fhe::Fhe,
p2p::P2p,
serializers::{
CiphertextSerializer, DecryptionShareSerializer, PublicKeySerializer,
PublicKeyShareSerializer,
},
DecryptedOutputPublished, DecryptionRequested, DecryptionshareCreated, ResetHistory,
DecryptedOutputPublished, CiphertextOutputPublished, DecryptionshareCreated, ResetHistory,
};
use actix::prelude::*;
use anyhow::*;
Expand Down Expand Up @@ -166,7 +166,7 @@ mod tests {

let e3_id = E3id::new("1234");

let event = EnclaveEvent::from(ComputationRequested {
let event = EnclaveEvent::from(CommitteeRequested {
e3_id: e3_id.clone(),
nodecount: 3,
threshold: 123,
Expand Down Expand Up @@ -203,7 +203,7 @@ mod tests {
assert_eq!(
history,
vec![
EnclaveEvent::from(ComputationRequested {
EnclaveEvent::from(CommitteeRequested {
e3_id: e3_id.clone(),
nodecount: 3,
threshold: 123,
Expand Down Expand Up @@ -243,8 +243,8 @@ mod tests {

let ciphertext = pubkey.try_encrypt(&pt, &mut ChaCha20Rng::seed_from_u64(42))?;

let event = EnclaveEvent::from(DecryptionRequested {
ciphertext: CiphertextSerializer::to_bytes(ciphertext.clone(), params.clone())?,
let event = EnclaveEvent::from(CiphertextOutputPublished {
ciphertext_output: CiphertextSerializer::to_bytes(ciphertext.clone(), params.clone())?,
e3_id: e3_id.clone(),
});

Expand Down Expand Up @@ -316,14 +316,14 @@ mod tests {
}
});

let evt_1 = EnclaveEvent::from(ComputationRequested {
let evt_1 = EnclaveEvent::from(CommitteeRequested {
e3_id: E3id::new("1234"),
nodecount: 3,
threshold: 123,
sortition_seed: 123,
});

let evt_2 = EnclaveEvent::from(ComputationRequested {
let evt_2 = EnclaveEvent::from(CommitteeRequested {
e3_id: E3id::new("1235"),
nodecount: 3,
threshold: 123,
Expand Down Expand Up @@ -362,7 +362,7 @@ mod tests {
P2p::spawn_and_listen(bus.clone(), tx.clone(), rx);

// Capture messages from output on msgs vec
let event = EnclaveEvent::from(ComputationRequested {
let event = EnclaveEvent::from(CommitteeRequested {
e3_id: E3id::new("1235"),
nodecount: 3,
threshold: 123,
Expand Down
4 changes: 2 additions & 2 deletions packages/ciphernode/enclave_node/src/bin/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::error::Error;

use enclave_core::Actor;
use enclave_core::ComputationRequested;
use enclave_core::CommitteeRequested;
use enclave_core::E3id;
use enclave_core::EnclaveEvent;
use enclave_core::EventBus;
Expand All @@ -22,7 +22,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
match line.as_str() {
"test" => {
id += 1;
bus.do_send(EnclaveEvent::from(ComputationRequested {
bus.do_send(EnclaveEvent::from(CommitteeRequested {
e3_id: E3id::from(id),
nodecount: 3,
threshold: 3,
Expand Down
Loading