Skip to content

Commit

Permalink
Extract events (#230)
Browse files Browse the repository at this point in the history
* Extract events

* Use a macro to add From traits

* Expose TestEvent
  • Loading branch information
ryardley authored Dec 30, 2024
1 parent ed322f3 commit 76b76e1
Show file tree
Hide file tree
Showing 21 changed files with 649 additions and 641 deletions.
43 changes: 43 additions & 0 deletions packages/ciphernode/events/src/e3id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use alloy::primitives::U256;
use alloy_primitives::ruint::ParseError;
use core::fmt;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct E3id(pub String);
impl fmt::Display for E3id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}

impl E3id {
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
}

impl From<u32> for E3id {
fn from(value: u32) -> Self {
E3id::new(value.to_string())
}
}

impl From<String> for E3id {
fn from(value: String) -> Self {
E3id::new(value)
}
}

impl From<&str> for E3id {
fn from(value: &str) -> Self {
E3id::new(value)
}
}

impl TryFrom<E3id> for U256 {
type Error = ParseError;
fn try_from(value: E3id) -> Result<Self, Self::Error> {
U256::from_str_radix(&value.0, 10)
}
}
21 changes: 21 additions & 0 deletions packages/ciphernode/events/src/enclave_event/ciphernode_added.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use actix::Message;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[rtype(result = "()")]
pub struct CiphernodeAdded {
pub address: String,
pub index: usize,
pub num_nodes: usize,
}

impl Display for CiphernodeAdded {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"address: {}, index: {}, num_nodes: {}",
self.address, self.index, self.num_nodes
)
}
}
21 changes: 21 additions & 0 deletions packages/ciphernode/events/src/enclave_event/ciphernode_removed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use actix::Message;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[rtype(result = "()")]
pub struct CiphernodeRemoved {
pub address: String,
pub index: usize,
pub num_nodes: usize,
}

impl Display for CiphernodeRemoved {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"address: {}, index: {}, num_nodes: {}",
self.address, self.index, self.num_nodes
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::E3id;
use actix::Message;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[rtype(result = "()")]
pub struct CiphernodeSelected {
pub e3_id: E3id,
pub threshold_m: usize,
}

impl Display for CiphernodeSelected {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"e3_id: {}, threshold_m: {}",
self.e3_id, self.threshold_m,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::E3id;
use actix::Message;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};

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

impl Display for CiphertextOutputPublished {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "e3_id: {}", self.e3_id,)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::E3id;
use actix::Message;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[rtype(result = "anyhow::Result<()>")]
pub struct DecryptionshareCreated {
pub decryption_share: Vec<u8>,
pub e3_id: E3id,
pub node: String,
}

impl Display for DecryptionshareCreated {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "e3_id: {}, node: {}", self.e3_id, self.node,)
}
}
12 changes: 12 additions & 0 deletions packages/ciphernode/events/src/enclave_event/die.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use actix::Message;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[rtype(result = "()")]
pub struct Die;
impl Display for Die {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Die",)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::E3id;
use actix::Message;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};

/// E3RequestComplete event is a local only event
#[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[rtype(result = "()")]
pub struct E3RequestComplete {
pub e3_id: E3id,
}

impl Display for E3RequestComplete {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "e3_id: {}", self.e3_id)
}
}
24 changes: 24 additions & 0 deletions packages/ciphernode/events/src/enclave_event/e3_requested.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::{E3id, Seed};
use actix::Message;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[rtype(result = "()")]
pub struct E3Requested {
pub e3_id: E3id,
pub threshold_m: usize,
pub seed: Seed,
pub params: Vec<u8>,
pub src_chain_id: u64,
}

impl Display for E3Requested {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"e3_id: {}, threshold_m: {}, src_chain_id: {}, seed: {}, params: <omitted>",
self.e3_id, self.threshold_m, self.src_chain_id, self.seed
)
}
}
52 changes: 52 additions & 0 deletions packages/ciphernode/events/src/enclave_event/enclave_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use actix::Message;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display};

pub trait FromError {
type Error;
fn from_error(err_type: EnclaveErrorType, error: Self::Error) -> Self;
}

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[rtype(result = "()")]
pub struct EnclaveError {
pub err_type: EnclaveErrorType,
pub message: String,
}

impl Display for EnclaveError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum EnclaveErrorType {
Evm,
KeyGeneration,
PublickeyAggregation,
IO,
PlaintextAggregation,
Decryption,
Sortition,
Data,
}

impl EnclaveError {
pub fn new(err_type: EnclaveErrorType, message: &str) -> Self {
Self {
err_type,
message: message.to_string(),
}
}
}

impl FromError for EnclaveError {
type Error = anyhow::Error;
fn from_error(err_type: EnclaveErrorType, error: Self::Error) -> Self {
Self {
err_type,
message: error.to_string(),
}
}
}
19 changes: 19 additions & 0 deletions packages/ciphernode/events/src/enclave_event/keyshare_created.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::E3id;
use actix::Message;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fmt::Display;

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[rtype(result = "anyhow::Result<()>")]
pub struct KeyshareCreated {
pub pubkey: Vec<u8>,
pub e3_id: E3id,
pub node: String,
}

impl Display for KeyshareCreated {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "e3_id: {}, node: {}", self.e3_id, self.node,)
}
}
Loading

0 comments on commit 76b76e1

Please sign in to comment.