Skip to content

Commit

Permalink
Enable easy access to repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
ktdlr committed Oct 18, 2024
1 parent ebd953e commit a82c528
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/ciphernode/enclave_node/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rand::SeedableRng;
use rand_chacha::rand_core::OsRng;
use router::{
E3RequestRouter, FheFeature, PlaintextAggregatorFeature, PublicKeyAggregatorFeature,
Repositories,
RepositoriesFactory,
};
use sortition::Sortition;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -56,7 +56,7 @@ impl MainAggregator {
));

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

let sortition = Sortition::attach(bus.clone(), repositories.sortition());
let selector =
Expand Down
2 changes: 1 addition & 1 deletion packages/ciphernode/router/src/committee_meta.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{E3Feature, E3RequestContext, E3RequestContextSnapshot};
use crate::{E3Feature, E3RequestContext, RepositoriesFactory, E3RequestContextSnapshot};
use anyhow::*;
use async_trait::async_trait;
use enclave_core::{E3Requested, EnclaveEvent, Seed};
Expand Down
12 changes: 7 additions & 5 deletions packages/ciphernode/router/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use crate::{CommitteeMeta, E3Feature, EventBuffer, Repositories};
use crate::{CommitteeMeta, E3Feature, EventBuffer, Repositories, RepositoriesFactory};
use actix::{Addr, Recipient};
use aggregator::{PlaintextAggregator, PublicKeyAggregator};
use anyhow::Result;
Expand Down Expand Up @@ -82,10 +82,6 @@ impl E3RequestContext {
});
}

pub fn repositories(&self) -> Repositories {
self.repository().into()
}

/// Accept a DataStore ID and a Keystore actor address
pub fn set_keyshare(&mut self, value: Addr<Keyshare>) {
self.keyshare = Some(value);
Expand Down Expand Up @@ -137,6 +133,12 @@ impl E3RequestContext {
}
}

impl RepositoriesFactory for E3RequestContext {
fn repositories(&self) -> Repositories {
self.repository().into()
}
}

#[async_trait]
impl Snapshot for E3RequestContext {
type Snapshot = E3RequestContextSnapshot;
Expand Down
10 changes: 5 additions & 5 deletions packages/ciphernode/router/src/e3_request_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::CommitteeMetaFeature;
use crate::E3RequestContext;
use crate::E3RequestContextParams;
use crate::E3RequestContextSnapshot;
use crate::Repositories;
use crate::RepositoriesFactory;
use actix::{Actor, Addr, Context, Handler};
use anyhow::*;
use async_trait::async_trait;
Expand Down Expand Up @@ -74,7 +74,7 @@ pub struct E3RequestRouterParams {

impl E3RequestRouter {
pub fn builder(bus: Addr<EventBus>, store: DataStore) -> E3RequestRouterBuilder {
let repositories: Repositories = store.into();
let repositories = store.repositories();
let builder = E3RequestRouterBuilder {
bus,
features: vec![],
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Handler<EnclaveEvent> for E3RequestRouter {
return;
}

let repositories: Repositories = self.repository().into();
let repositories = self.repository().repositories();
let context = self.contexts.entry(e3_id.clone()).or_insert_with(|| {
E3RequestContext::from_params(E3RequestContextParams {
e3_id: e3_id.clone(),
Expand Down Expand Up @@ -187,7 +187,7 @@ impl FromSnapshotWithParams for E3RequestRouter {
async fn from_snapshot(params: Self::Params, snapshot: Self::Snapshot) -> Result<Self> {
let mut contexts = HashMap::new();

let repositories: Repositories = params.store.into();
let repositories = params.store.repositories();
for e3_id in snapshot.contexts {
let Some(ctx_snapshot) = repositories.context(&e3_id).read().await? else {
continue;
Expand Down Expand Up @@ -232,7 +232,7 @@ impl E3RequestRouterBuilder {
}

pub async fn build(self) -> Result<Addr<E3RequestRouter>> {
let repositories: Repositories = self.store.into();
let repositories = self.store.repositories();
let router_repo = repositories.router();
let snapshot: Option<E3RequestRouterSnapshot> = router_repo.read().await?;
let params = E3RequestRouterParams {
Expand Down
2 changes: 1 addition & 1 deletion packages/ciphernode/router/src/hooks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{E3Feature, E3RequestContext, E3RequestContextSnapshot};
use crate::{E3Feature, E3RequestContext, E3RequestContextSnapshot, RepositoriesFactory};
use actix::{Actor, Addr};
use aggregator::{
PlaintextAggregator, PlaintextAggregatorParams, PlaintextAggregatorState, PublicKeyAggregator,
Expand Down
24 changes: 24 additions & 0 deletions packages/ciphernode/router/src/repositories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ impl From<DataStore> for Repositories {
Repositories { store: value }
}
}
impl From<&DataStore> for Repositories {
fn from(value: &DataStore) -> Self {
Repositories {
store: value.clone(),
}
}
}

impl Repositories {
pub fn new(store: DataStore) -> Self {
Expand Down Expand Up @@ -62,3 +69,20 @@ impl Repositories {
Repository::new(self.store.scope(format!("//sortition")))
}
}

pub trait RepositoriesFactory {
fn repositories(&self) -> Repositories;
}

impl RepositoriesFactory for DataStore {
fn repositories(&self) -> Repositories {
self.into()
}
}

impl<T> RepositoriesFactory for Repository<T> {
fn repositories(&self) -> Repositories {
let store:DataStore = self.clone().into();
store.repositories()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use logger::SimpleLogger;
use p2p::P2p;
use router::{
CiphernodeSelector, E3RequestRouter, FheFeature, KeyshareFeature, PlaintextAggregatorFeature,
PublicKeyAggregatorFeature, Repositories,
PublicKeyAggregatorFeature, Repositories, RepositoriesFactory,
};
use sortition::Sortition;

Expand Down Expand Up @@ -38,7 +38,7 @@ async fn setup_local_ciphernode(
// create data actor for saving data
let data_actor = InMemStore::new(logging).start(); // TODO: Use a sled backed Data Actor
let store = DataStore::from_in_mem(data_actor);
let repositories: Repositories = store.clone().into();
let repositories = store.repositories();

// create ciphernode actor for managing ciphernode flow
let sortition = Sortition::attach(bus.clone(), repositories.sortition());
Expand Down

0 comments on commit a82c528

Please sign in to comment.