Skip to content

Commit

Permalink
Setup repositries struct for finding repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
ktdlr committed Oct 18, 2024
1 parent c8f31ea commit 05e6d14
Show file tree
Hide file tree
Showing 27 changed files with 192 additions and 348 deletions.
4 changes: 0 additions & 4 deletions packages/ciphernode/aggregator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
mod plaintext_aggregator;
mod plaintext_repository;
mod publickey_aggregator;
mod publickey_repository;
pub use plaintext_aggregator::{
PlaintextAggregator, PlaintextAggregatorParams, PlaintextAggregatorState,
};
pub use plaintext_repository::*;
pub use publickey_aggregator::{
PublicKeyAggregator, PublicKeyAggregatorParams, PublicKeyAggregatorState,
};
pub use publickey_repository::*;
11 changes: 4 additions & 7 deletions packages/ciphernode/aggregator/src/plaintext_aggregator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actix::prelude::*;
use anyhow::Result;
use async_trait::async_trait;
use data::{Checkpoint, FromSnapshotWithParams, Snapshot};
use data::{Checkpoint, FromSnapshotWithParams, Repository, Snapshot};
use enclave_core::{
DecryptionshareCreated, Die, E3id, EnclaveEvent, EventBus, OrderedSet, PlaintextAggregated,
Seed,
Expand All @@ -11,8 +11,6 @@ use sortition::{GetHasNode, Sortition};
use std::sync::Arc;
use tracing::error;

use crate::plaintext_repository::PlaintextAggregatorRepository;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum PlaintextAggregatorState {
Collecting {
Expand Down Expand Up @@ -52,7 +50,7 @@ struct ComputeAggregate {
pub struct PlaintextAggregator {
fhe: Arc<Fhe>,
bus: Addr<EventBus>,
store: PlaintextAggregatorRepository,
store: Repository<PlaintextAggregatorState>,
sortition: Addr<Sortition>,
e3_id: E3id,
state: PlaintextAggregatorState,
Expand All @@ -62,7 +60,7 @@ pub struct PlaintextAggregator {
pub struct PlaintextAggregatorParams {
pub fhe: Arc<Fhe>,
pub bus: Addr<EventBus>,
pub store: PlaintextAggregatorRepository,
pub store: Repository<PlaintextAggregatorState>,
pub sortition: Addr<Sortition>,
pub e3_id: E3id,
pub src_chain_id: u64,
Expand Down Expand Up @@ -238,8 +236,7 @@ impl FromSnapshotWithParams for PlaintextAggregator {
}

impl Checkpoint for PlaintextAggregator {
type Repository = PlaintextAggregatorRepository;
fn get_store(&self) -> PlaintextAggregatorRepository {
fn repository(&self) -> Repository<PlaintextAggregatorState> {
self.store.clone()
}
}
29 changes: 0 additions & 29 deletions packages/ciphernode/aggregator/src/plaintext_repository.rs

This file was deleted.

10 changes: 4 additions & 6 deletions packages/ciphernode/aggregator/src/publickey_aggregator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actix::prelude::*;
use anyhow::Result;
use async_trait::async_trait;
use data::{Checkpoint, FromSnapshotWithParams, Snapshot};
use data::{Checkpoint, FromSnapshotWithParams, Repository, Snapshot};
use enclave_core::{
Die, E3id, EnclaveEvent, EventBus, KeyshareCreated, OrderedSet, PublicKeyAggregated, Seed,
};
Expand All @@ -10,7 +10,6 @@ use sortition::{GetHasNode, GetNodes, Sortition};
use std::sync::Arc;
use tracing::error;

use crate::PublicKeyAggregatorRepository;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum PublicKeyAggregatorState {
Expand Down Expand Up @@ -55,7 +54,7 @@ struct NotifyNetwork {
pub struct PublicKeyAggregator {
fhe: Arc<Fhe>,
bus: Addr<EventBus>,
store: PublicKeyAggregatorRepository,
store: Repository<PublicKeyAggregatorState>,
sortition: Addr<Sortition>,
e3_id: E3id,
state: PublicKeyAggregatorState,
Expand All @@ -65,7 +64,7 @@ pub struct PublicKeyAggregator {
pub struct PublicKeyAggregatorParams {
pub fhe: Arc<Fhe>,
pub bus: Addr<EventBus>,
pub store: PublicKeyAggregatorRepository,
pub store: Repository<PublicKeyAggregatorState>,
pub sortition: Addr<Sortition>,
pub e3_id: E3id,
pub src_chain_id: u64,
Expand Down Expand Up @@ -263,8 +262,7 @@ impl FromSnapshotWithParams for PublicKeyAggregator {
}

impl Checkpoint for PublicKeyAggregator {
type Repository = PublicKeyAggregatorRepository;
fn get_store(&self) -> PublicKeyAggregatorRepository {
fn repository(&self) -> Repository<PublicKeyAggregatorState> {
self.store.clone()
}
}
29 changes: 0 additions & 29 deletions packages/ciphernode/aggregator/src/publickey_repository.rs

This file was deleted.

55 changes: 46 additions & 9 deletions packages/ciphernode/data/src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,55 @@
use std::{marker::PhantomData, ops::Deref};

use anyhow::Result;
use async_trait::async_trait;

use crate::DataStore;

#[async_trait]
pub trait Repository {
type State: for<'de> serde::Deserialize<'de> + serde::Serialize;
fn store(&self) -> DataStore;
pub struct Repository<S> {
store: DataStore,
_p: PhantomData<S>,
}

impl<S> Repository<S> {
pub fn new(store: DataStore) -> Self {
Self {
store,
_p: PhantomData,
}
}
}

impl<S> Deref for Repository<S>{
type Target = DataStore;
fn deref(&self) -> &Self::Target {
&self.store
}
}

impl<S> From<Repository<S>> for DataStore {
fn from(value: Repository<S>) -> Self {
value.store
}
}

/// Clone without phantom data
impl<S> Clone for Repository<S> {
fn clone(&self) -> Self {
Self {
store: self.store.clone(),
_p: PhantomData,
}
}
}

async fn read(&self) -> Result<Option<Self::State>> {
self.store().read().await
impl<T> Repository<T>
where
T: for<'de> serde::Deserialize<'de> + serde::Serialize,
{
pub async fn read(&self) -> Result<Option<T>> {
self.store.read().await
}

fn write(&self, value: &Self::State) {
self.store().write(value)
pub fn write(&self, value: &T) {
self.store.write(value)
}
}
5 changes: 2 additions & 3 deletions packages/ciphernode/data/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ where

/// This trait enables the self type to checkpoint its state
pub trait Checkpoint: Snapshot {
type Repository: Repository<State = Self::Snapshot>;
/// Declare the DataStore instance available on the object
fn get_store(&self) -> Self::Repository;
fn repository(&self) -> Repository<Self::Snapshot>;

/// Write the current snapshot to the DataStore provided by `get_store()` at the object's id returned by `get_id()`
fn checkpoint(&self) {
self.get_store().write(&self.snapshot());
self.repository().write(&self.snapshot());
}
}

Expand Down
10 changes: 7 additions & 3 deletions packages/ciphernode/enclave_node/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ use logger::SimpleLogger;
use p2p::P2p;
use rand::SeedableRng;
use rand_chacha::rand_core::OsRng;
use router::{E3RequestRouter, FheFeature, PlaintextAggregatorFeature, PublicKeyAggregatorFeature};
use sortition::{Sortition, SortitionRepositoryFactory};
use router::{
E3RequestRouter, FheFeature, PlaintextAggregatorFeature, PublicKeyAggregatorFeature,
Repositories,
};
use sortition::Sortition;
use std::sync::{Arc, Mutex};
use test_helpers::{PlaintextWriter, PublicKeyWriter};
use tokio::task::JoinHandle;
Expand Down Expand Up @@ -53,7 +56,8 @@ impl MainAggregator {
));

let store = DataStore::from_in_mem(InMemStore::new(true).start());
let sortition = Sortition::attach(bus.clone(), store.sortition());
let repositories: Repositories = store.clone().into();
let sortition = Sortition::attach(bus.clone(), repositories.sortition());
let signer = pull_eth_signer_from_env("PRIVATE_KEY").await?;
for chain in config
.chains
Expand Down
14 changes: 8 additions & 6 deletions packages/ciphernode/enclave_node/src/ciphernode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use logger::SimpleLogger;
use p2p::P2p;
use rand::SeedableRng;
use rand_chacha::rand_core::OsRng;
use router::{CiphernodeSelector, E3RequestRouter, FheFeature, KeyshareFeature};
use sortition::{Sortition, SortitionRepositoryFactory};
use router::{CiphernodeSelector, E3RequestRouter, FheFeature, KeyshareFeature, Repositories};
use sortition::Sortition;
use std::sync::{Arc, Mutex};
use tokio::task::JoinHandle;

Expand Down Expand Up @@ -58,8 +58,10 @@ impl MainCiphernode {
));
let bus = EventBus::new(true).start();
// TODO: switch to Sled actor
let data = DataStore::from_in_mem(InMemStore::new(true).start());
let sortition = Sortition::attach(bus.clone(), data.sortition());
let store = DataStore::from_in_mem(InMemStore::new(true).start());
let repositories: Repositories = store.clone().into();

let sortition = Sortition::attach(bus.clone(), repositories.sortition());
let selector =
CiphernodeSelector::attach(bus.clone(), sortition.clone(), &address.to_string());

Expand All @@ -79,7 +81,7 @@ impl MainCiphernode {
.await?;
}

let e3_manager = E3RequestRouter::builder(bus.clone(), data.clone())
let e3_manager = E3RequestRouter::builder(bus.clone(), store.clone())
.add_feature(FheFeature::create(rng))
.add_feature(KeyshareFeature::create(bus.clone(), &address.to_string()))
.build()
Expand All @@ -91,7 +93,7 @@ impl MainCiphernode {
let nm = format!("CIPHER({})", &address.to_string()[0..5]);
SimpleLogger::attach(&nm, bus.clone());
let main_addr = MainCiphernode::new(
address, bus, data, sortition, selector, p2p_addr, e3_manager,
address, bus, store, sortition, selector, p2p_addr, e3_manager,
)
.start();
Ok((main_addr, join_handle))
Expand Down
2 changes: 0 additions & 2 deletions packages/ciphernode/fhe/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
mod fhe;
mod repository;
mod utils;

pub use fhe::*;
pub use repository::*;
pub use utils::*;
30 changes: 0 additions & 30 deletions packages/ciphernode/fhe/src/repository.rs

This file was deleted.

9 changes: 3 additions & 6 deletions packages/ciphernode/keyshare/src/keyshare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ use fhe::{DecryptCiphertext, Fhe};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

use crate::KeyshareRepository;

pub struct Keyshare {
fhe: Arc<Fhe>,
store: KeyshareRepository,
store: Repository<KeyshareState>,
bus: Addr<EventBus>,
secret: Option<Vec<u8>>,
address: String,
Expand All @@ -26,7 +24,7 @@ impl Actor for Keyshare {

pub struct KeyshareParams {
pub bus: Addr<EventBus>,
pub store: KeyshareRepository,
pub store: Repository<KeyshareState>,
pub fhe: Arc<Fhe>,
pub address: String,
}
Expand Down Expand Up @@ -59,8 +57,7 @@ impl Snapshot for Keyshare {
}

impl Checkpoint for Keyshare {
type Repository = KeyshareRepository;
fn get_store(&self) -> KeyshareRepository {
fn repository(&self) -> Repository<KeyshareState>{
self.store.clone()
}
}
Expand Down
2 changes: 0 additions & 2 deletions packages/ciphernode/keyshare/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
mod keyshare;
mod repository;
pub use keyshare::*;
pub use repository::*;
Loading

0 comments on commit 05e6d14

Please sign in to comment.