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

Refactor to use Persistable containers #189

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
01f3ffb
Fix dependency scalability problem
ryardley Nov 26, 2024
fc3dc05
Add Persistable Data to Keyshare
ryardley Nov 26, 2024
4d5be54
Add autopersist to sortition
ryardley Nov 27, 2024
b6f5aff
Apply persistable to plaintext aggregator
ryardley Nov 27, 2024
b468e0a
Apply persistable to publickey aggregator
ryardley Nov 27, 2024
b42333b
Remove comment
ryardley Nov 27, 2024
d98efa3
Use persistable in evm_reader
ryardley Nov 28, 2024
7a67428
Add persistence documentation to README
ryardley Nov 28, 2024
301bb25
Move docs to data package
ryardley Nov 28, 2024
95d77f8
Update persistable docs
ryardley Nov 28, 2024
f289f45
Update persistable docs
ryardley Nov 28, 2024
51c6009
Formatting
ryardley Nov 29, 2024
9e9cd86
Tidy up API and document
ryardley Nov 29, 2024
d3f15b8
Remove rendundant imports
ryardley Nov 29, 2024
cb1bb3e
Formatting
ryardley Nov 29, 2024
94c71b6
Add tests
ryardley Nov 29, 2024
dbe6a56
Fix bugs and add tests
ryardley Nov 29, 2024
3417956
Update docs
ryardley Nov 29, 2024
6129d7b
Add quotes
ryardley Nov 29, 2024
4747fc8
Add write
ryardley Nov 29, 2024
36c9b52
fix syntax
ryardley Nov 29, 2024
b0e4bae
Addquotes
ryardley Nov 29, 2024
69d4ecb
Escape stuff
ryardley Nov 29, 2024
19bb384
Update README.md
ryardley Nov 29, 2024
743839f
Update README.md
ryardley Nov 29, 2024
dab9e02
Update README.md
ryardley Nov 29, 2024
e8c6d57
Update README.md
ryardley Nov 29, 2024
382b53e
Update README.md
ryardley Nov 29, 2024
0b6d347
Update README.md
ryardley Nov 29, 2024
eb52e08
Update formatting
ryardley Nov 29, 2024
bf66a84
Update README.md
ryardley Nov 29, 2024
8b92ce2
Update README.md
ryardley Nov 29, 2024
d7a397f
Update README.md
ryardley Nov 29, 2024
84f57fb
Update README.md
ryardley Nov 29, 2024
2ff82aa
Update README.md
ryardley Nov 29, 2024
f530592
Update README.md
ryardley Nov 29, 2024
746af26
Update README.md
ryardley Nov 29, 2024
198fc6f
Update README.md
ryardley Nov 29, 2024
3147839
Formatting
ryardley Nov 30, 2024
5ba188e
Fix redundant imports
ryardley Nov 30, 2024
9fb36d4
Use a Persistable<EncryptedKeypair> to manage identity for libp2p
ryardley Dec 1, 2024
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
12 changes: 12 additions & 0 deletions packages/ciphernode/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/ciphernode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ bincode = "1.3.3"
bs58 = "0.5.1"
base64 = "0.22.1"
clap = { version = "4.5.17", features = ["derive"] }
config = { path = "./config" }
cipher = { path = "./cipher" }
dirs = "5.0.1"
data = { path = "./data" }
figment = { version = "0.10.19", features = ["yaml", "test"] }
fhe_rs = { package = "fhe", git = "https://github.com/gnosisguild/fhe.rs", version = "0.1.0-beta.7" }
fhe-traits = { git = "https://github.com/gnosisguild/fhe.rs", version = "0.1.0-beta.7" }
Expand Down
1 change: 1 addition & 0 deletions packages/ciphernode/aggregator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ actix = { workspace = true }
anyhow = { workspace = true }
serde = { workspace = true }
bincode = { workspace = true }
config = { path = "../config" }
async-trait = { workspace = true }
enclave-core = { path = "../core" }
fhe = { path = "../fhe" }
Expand Down
3 changes: 3 additions & 0 deletions packages/ciphernode/aggregator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
mod plaintext_aggregator;
mod publickey_aggregator;
mod repositories;

pub use plaintext_aggregator::{
PlaintextAggregator, PlaintextAggregatorParams, PlaintextAggregatorState,
};
pub use publickey_aggregator::{
PublicKeyAggregator, PublicKeyAggregatorParams, PublicKeyAggregatorState,
};
pub use repositories::*;
107 changes: 43 additions & 64 deletions packages/ciphernode/aggregator/src/plaintext_aggregator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use actix::prelude::*;
use anyhow::Result;
use async_trait::async_trait;
use data::{Checkpoint, FromSnapshotWithParams, Repository, Snapshot};
use data::Persistable;
use enclave_core::{
DecryptionshareCreated, Die, E3id, EnclaveEvent, EventBus, OrderedSet, PlaintextAggregated,
Seed,
Expand Down Expand Up @@ -50,65 +49,70 @@ struct ComputeAggregate {
pub struct PlaintextAggregator {
fhe: Arc<Fhe>,
bus: Addr<EventBus>,
store: Repository<PlaintextAggregatorState>,
sortition: Addr<Sortition>,
e3_id: E3id,
state: PlaintextAggregatorState,
state: Persistable<PlaintextAggregatorState>,
src_chain_id: u64,
}

pub struct PlaintextAggregatorParams {
pub fhe: Arc<Fhe>,
pub bus: Addr<EventBus>,
pub store: Repository<PlaintextAggregatorState>,
pub sortition: Addr<Sortition>,
pub e3_id: E3id,
pub src_chain_id: u64,
}

impl PlaintextAggregator {
pub fn new(params: PlaintextAggregatorParams, state: PlaintextAggregatorState) -> Self {
pub fn new(
params: PlaintextAggregatorParams,
state: Persistable<PlaintextAggregatorState>,
) -> Self {
PlaintextAggregator {
fhe: params.fhe,
bus: params.bus,
store: params.store,
sortition: params.sortition,
e3_id: params.e3_id,
src_chain_id: params.src_chain_id,
state,
}
}

pub fn add_share(&mut self, share: Vec<u8>) -> Result<PlaintextAggregatorState> {
let PlaintextAggregatorState::Collecting {
threshold_m,
shares,
ciphertext_output,
..
} = &mut self.state
else {
return Err(anyhow::anyhow!("Can only add share in Collecting state"));
};

shares.insert(share);
if shares.len() == *threshold_m {
return Ok(PlaintextAggregatorState::Computing {
shares: shares.clone(),
ciphertext_output: ciphertext_output.to_vec(),
});
}

Ok(self.state.clone())
pub fn add_share(&mut self, share: Vec<u8>) -> Result<()> {
self.state.try_mutate(|mut state| {
let PlaintextAggregatorState::Collecting {
threshold_m,
shares,
ciphertext_output,
..
} = &mut state
else {
return Err(anyhow::anyhow!("Can only add share in Collecting state"));
};

shares.insert(share);

if shares.len() == *threshold_m {
return Ok(PlaintextAggregatorState::Computing {
shares: shares.clone(),
ciphertext_output: ciphertext_output.to_vec(),
});
}

Ok(state)
})
}

pub fn set_decryption(&mut self, decrypted: Vec<u8>) -> Result<PlaintextAggregatorState> {
let PlaintextAggregatorState::Computing { shares, .. } = &mut self.state else {
return Ok(self.state.clone());
};
pub fn set_decryption(&mut self, decrypted: Vec<u8>) -> Result<()> {
self.state.try_mutate(|mut state| {
let PlaintextAggregatorState::Computing { shares, .. } = &mut state else {
return Ok(state);
};

let shares = shares.to_owned();
let shares = shares.to_owned();

Ok(PlaintextAggregatorState::Complete { decrypted, shares })
Ok(PlaintextAggregatorState::Complete { decrypted, shares })
})
}
}

Expand All @@ -131,9 +135,9 @@ impl Handler<DecryptionshareCreated> for PlaintextAggregator {
type Result = ResponseActFuture<Self, Result<()>>;

fn handle(&mut self, event: DecryptionshareCreated, _: &mut Self::Context) -> Self::Result {
let PlaintextAggregatorState::Collecting {
let Some(PlaintextAggregatorState::Collecting {
threshold_m, seed, ..
} = self.state
}) = self.state.get()
else {
error!(state=?self.state, "Aggregator has been closed for collecting.");
return Box::pin(fut::ready(Ok(())));
Expand Down Expand Up @@ -165,14 +169,13 @@ impl Handler<DecryptionshareCreated> for PlaintextAggregator {
}

// add the keyshare and
act.state = act.add_share(decryption_share)?;
act.checkpoint();
act.add_share(decryption_share)?;

// Check the state and if it has changed to the computing
if let PlaintextAggregatorState::Computing {
if let Some(PlaintextAggregatorState::Computing {
shares,
ciphertext_output,
} = &act.state
}) = &act.state.get()
{
ctx.notify(ComputeAggregate {
shares: shares.clone(),
Expand All @@ -195,8 +198,7 @@ impl Handler<ComputeAggregate> for PlaintextAggregator {
})?;

// Update the local state
self.state = self.set_decryption(decrypted_output.clone())?;
self.checkpoint();
self.set_decryption(decrypted_output.clone())?;

// Dispatch the PlaintextAggregated event
let event = EnclaveEvent::from(PlaintextAggregated {
Expand All @@ -217,26 +219,3 @@ impl Handler<Die> for PlaintextAggregator {
ctx.stop()
}
}

impl Snapshot for PlaintextAggregator {
type Snapshot = PlaintextAggregatorState;

fn snapshot(&self) -> Self::Snapshot {
self.state.clone()
}
}

#[async_trait]
impl FromSnapshotWithParams for PlaintextAggregator {
type Params = PlaintextAggregatorParams;

async fn from_snapshot(params: Self::Params, snapshot: Self::Snapshot) -> Result<Self> {
Ok(PlaintextAggregator::new(params, snapshot))
}
}

impl Checkpoint for PlaintextAggregator {
fn repository(&self) -> &Repository<PlaintextAggregatorState> {
&self.store
}
}
Loading
Loading