Skip to content

Commit

Permalink
Purge command
Browse files Browse the repository at this point in the history
  • Loading branch information
ryardley committed Dec 2, 2024
1 parent 7396b74 commit bf3be51
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 4 deletions.
23 changes: 23 additions & 0 deletions packages/ciphernode/data/src/data_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,26 @@ impl Get {
}
}

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash)]
#[rtype(result = "()")]
pub struct Remove(pub Vec<u8>);
impl Remove {
pub fn new<K: IntoKey>(key: K) -> Self {
Self(key.into_key())
}

pub fn key(&self) -> &Vec<u8> {
&self.0
}
}

/// Generate proxy for the DB
#[derive(Clone, Debug)]
pub struct DataStore {
scope: Vec<u8>,
get: Recipient<Get>,
insert: Recipient<Insert>,
remove: Recipient<Remove>
}

impl DataStore {
Expand Down Expand Up @@ -69,6 +83,11 @@ impl DataStore {
self.insert.do_send(msg)
}

/// Removes data from the scope location
pub fn clear(&self) {
self.remove.do_send(Remove::new(&self.scope))
}

/// Get the scope as a string
pub fn get_scope(&self) -> Result<Cow<str>> {
Ok(String::from_utf8_lossy(&self.scope))
Expand Down Expand Up @@ -103,6 +122,7 @@ impl DataStore {
Self {
get: self.get.clone(),
insert: self.insert.clone(),
remove: self.remove.clone(),
scope,
}
}
Expand All @@ -111,6 +131,7 @@ impl DataStore {
Self {
get: self.get.clone(),
insert: self.insert.clone(),
remove: self.remove.clone(),
scope: key.into_key(),
}
}
Expand All @@ -121,6 +142,7 @@ impl From<&Addr<SledStore>> for DataStore {
Self {
get: addr.clone().recipient(),
insert: addr.clone().recipient(),
remove: addr.clone().recipient(),
scope: vec![],
}
}
Expand All @@ -131,6 +153,7 @@ impl From<&Addr<InMemStore>> for DataStore {
Self {
get: addr.clone().recipient(),
insert: addr.clone().recipient(),
remove: addr.clone().recipient(),
scope: vec![],
}
}
Expand Down
15 changes: 14 additions & 1 deletion packages/ciphernode/data/src/in_mem.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actix::{Actor, Context, Handler, Message};
use std::collections::BTreeMap;

use crate::{Get, Insert};
use crate::{Get, Insert, Remove};

#[derive(Message, Clone, Debug, PartialEq, Eq, Hash)]
#[rtype(result = "Vec<DataOp>")]
Expand All @@ -10,6 +10,7 @@ pub struct GetLog;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum DataOp {
Insert(Insert),
Remove(Remove)
}

pub struct InMemStore {
Expand Down Expand Up @@ -44,6 +45,18 @@ impl Handler<Insert> for InMemStore {
}
}

impl Handler<Remove> for InMemStore {
type Result = ();
fn handle(&mut self, event: Remove, _: &mut Self::Context) {
// insert data into sled
self.db.remove(&event.key().to_vec());

if self.capture {
self.log.push(DataOp::Remove(event));
}
}
}

impl Handler<Get> for InMemStore {
type Result = Option<Vec<u8>>;
fn handle(&mut self, event: Get, _: &mut Self::Context) -> Option<Vec<u8>> {
Expand Down
6 changes: 5 additions & 1 deletion packages/ciphernode/data/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ where
}

pub fn write(&self, value: &T) {
self.store.write(value)
self.store.write(value);
}

pub fn clear(&self) {
self.store.clear();
}
}
22 changes: 21 additions & 1 deletion packages/ciphernode/data/src/sled_store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;

use crate::{Get, Insert};
use crate::{Get, Insert, Remove};
use actix::{Actor, ActorContext, Addr, Handler};
use anyhow::{Context, Result};
use enclave_core::{BusError, EnclaveErrorType, EnclaveEvent, EventBus, Subscribe};
Expand Down Expand Up @@ -53,6 +53,19 @@ impl Handler<Insert> for SledStore {
}
}

impl Handler<Remove> for SledStore {
type Result = ();

fn handle(&mut self, event: Remove, _: &mut Self::Context) -> Self::Result {
if let Some(ref mut db) = &mut self.db {
match db.remove(event) {
Err(err) => self.bus.err(EnclaveErrorType::Data, err),
_ => (),
}
}
}
}

impl Handler<Get> for SledStore {
type Result = Option<Vec<u8>>;

Expand Down Expand Up @@ -105,6 +118,13 @@ impl SledDb {
Ok(())
}

pub fn remove(&mut self, msg: Remove) -> Result<()> {
self.db
.remove(msg.key())
.context("Could not remove data from db")?;
Ok(())
}

pub fn get(&mut self, event: Get) -> Result<Option<Vec<u8>>> {
let key = event.key();
let str_key = String::from_utf8_lossy(&key).into_owned();
Expand Down
8 changes: 8 additions & 0 deletions packages/ciphernode/enclave/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ pub mod aggregator;
pub mod password;
pub mod start;
pub mod wallet;
pub mod p2p;

use self::password::PasswordCommands;
use aggregator::AggregatorCommands;
use clap::Subcommand;
use p2p::P2pCommands;
use wallet::WalletCommands;

#[derive(Subcommand, Debug)]
Expand All @@ -30,4 +32,10 @@ pub enum Commands {
#[command(subcommand)]
command: WalletCommands,
},

/// Peer related commands
P2p {
#[command(subcommand)]
command: P2pCommands
}
}
19 changes: 19 additions & 0 deletions packages/ciphernode/enclave/src/commands/p2p/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

mod purge;
use anyhow::*;
use clap::Subcommand;
use config::AppConfig;

#[derive(Subcommand, Debug)]
pub enum P2pCommands {
/// Purge the current peer id from the database.
Purge
}

pub async fn execute(command: P2pCommands, config: AppConfig) -> Result<()> {
match command {
P2pCommands::Purge => purge::execute(&config).await?,
};

Ok(())
}
13 changes: 13 additions & 0 deletions packages/ciphernode/enclave/src/commands/p2p/purge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use actix::Actor;
use anyhow::*;
use config::AppConfig;
use enclave_core::EventBus;
use enclave_node::get_repositories;

pub async fn execute(config: &AppConfig) -> Result<()> {
let bus = EventBus::new(true).start();
let repositories = get_repositories(&config, &bus)?;
repositories.libp2pid().clear();
println!("Peer ID has been purged. A new peer will be generated upon restart.");
Ok(())
}
3 changes: 2 additions & 1 deletion packages/ciphernode/enclave/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use clap::Parser;
use commands::{aggregator, password, start, wallet, Commands};
use commands::{aggregator, p2p, password, start, wallet, Commands};
use config::load_config;
use enclave_core::{get_tag, set_tag};
use tracing::instrument;
Expand Down Expand Up @@ -53,6 +53,7 @@ impl Cli {
Commands::Password { command } => password::execute(command, config).await?,
Commands::Aggregator { command } => aggregator::execute(command, config).await?,
Commands::Wallet { command } => wallet::execute(command, config).await?,
Commands::P2p { command } => p2p::execute(command, config).await?
}

Ok(())
Expand Down

0 comments on commit bf3be51

Please sign in to comment.