This commit is contained in:
Jędrzej Stuczyński
2023-05-05 10:01:57 +01:00
parent 487f07ef35
commit c5b678d4e9
7 changed files with 16 additions and 158 deletions
@@ -27,22 +27,6 @@ impl Backend {
impl ReplyStorageBackend for Backend {
type StorageError = <Empty as ReplyStorageBackend>::StorageError;
// async fn new(
// debug_config: &crate::config::DebugConfig,
// _db_path: Option<PathBuf>,
// ) -> Result<Self, Self::StorageError> {
// Ok(Backend {
// empty: Empty {
// min_surb_threshold: debug_config
// .reply_surbs
// .minimum_reply_surb_storage_threshold,
// max_surb_threshold: debug_config
// .reply_surbs
// .maximum_reply_surb_storage_threshold,
// },
// })
// }
async fn flush_surb_storage(
&mut self,
storage: &CombinedReplyStorage,
@@ -57,8 +41,4 @@ impl ReplyStorageBackend for Backend {
async fn load_surb_storage(&self) -> Result<CombinedReplyStorage, Self::StorageError> {
self.empty.load_surb_storage().await
}
// fn get_inactive_storage(&self) -> Result<CombinedReplyStorage, Self::StorageError> {
// self.empty.get_inactive_storage()
// }
}
@@ -22,50 +22,11 @@ mod error;
mod manager;
mod models;
// #[derive(Debug)]
// enum StorageManagerState {
// Storage(StorageManager),
// Inactive(InactiveMetadata),
// }
//
// // When the storage backaed is initialized as inactive, it will still contain metadata parameters
// // that will be needed when the in-mem storage is fetched for use.
// #[derive(Debug)]
// struct InactiveMetadata {
// pub minimum_reply_surb_storage_threshold: usize,
// pub maximum_reply_surb_storage_threshold: usize,
// }
//
// impl StorageManagerState {
// fn get(&self) -> &StorageManager {
// match self {
// StorageManagerState::Storage(manager) => manager,
// StorageManagerState::Inactive(_) => {
// panic!("tried to get storage of an inactive backend")
// }
// }
// }
//
// fn get_mut(&mut self) -> &mut StorageManager {
// match self {
// StorageManagerState::Storage(manager) => manager,
// StorageManagerState::Inactive(_) => {
// panic!("tried to get storage of an inactive backend")
// }
// }
// }
//
// fn is_active(&self) -> bool {
// matches!(self, StorageManagerState::Storage(_))
// }
// }
#[derive(Debug)]
pub struct Backend {
temporary_old_path: Option<PathBuf>,
database_path: PathBuf,
manager: StorageManager,
// manager: StorageManagerState,
}
impl Backend {
@@ -85,27 +46,12 @@ impl Backend {
let backend = Backend {
temporary_old_path: None,
database_path: owned_path,
// manager: StorageManagerState::Storage(manager),
manager,
};
Ok(backend)
}
// pub fn new_inactive(
// minimum_reply_surb_storage_threshold: usize,
// maximum_reply_surb_storage_threshold: usize,
// ) -> Self {
// Backend {
// temporary_old_path: None,
// database_path: PathBuf::new(),
// manager: StorageManagerState::Inactive(InactiveMetadata {
// minimum_reply_surb_storage_threshold,
// maximum_reply_surb_storage_threshold,
// }),
// }
// }
pub async fn try_load<P: AsRef<Path>>(database_path: P) -> Result<Self, StorageError> {
let owned_path: PathBuf = database_path.as_ref().into();
if owned_path.file_name().is_none() {
@@ -184,7 +130,6 @@ impl Backend {
async fn close_pool(&mut self) {
self.manager.connection_pool.close().await;
// self.manager.get_mut().connection_pool.close().await;
}
async fn rotate(&mut self) -> Result<(), StorageError> {
@@ -365,24 +310,6 @@ impl Backend {
impl ReplyStorageBackend for Backend {
type StorageError = error::StorageError;
// async fn new(
// surb_config: &crate::config::ReplySurbs,
// db_path: Option<PathBuf>,
// ) -> Result<Self, Self::StorageError> {
// non_wasm_helpers::setup_fs_reply_surb_backend(db_path, surb_config)
// .await
// .map_err(|err| {
// log::error!("Failed to create storage: {err}");
// Self::StorageError::FailedToCreateStorage {
// source: Box::new(err),
// }
// })
// }
// fn is_active(&self) -> bool {
// self.manager.is_active()
// }
async fn start_storage_session(&self) -> Result<(), Self::StorageError> {
self.start_client_use().await
}
@@ -420,18 +347,6 @@ impl ReplyStorageBackend for Backend {
Ok(CombinedReplyStorage::load(reply_keys, reply_surbs, tags))
}
// fn get_inactive_storage(&self) -> Result<CombinedReplyStorage, Self::StorageError> {
// match self.manager {
// StorageManagerState::Storage(_) => {
// panic!("tried to get inactive storage from an active storage backend")
// }
// StorageManagerState::Inactive(ref state) => Ok(CombinedReplyStorage::new(
// state.minimum_reply_surb_storage_threshold,
// state.maximum_reply_surb_storage_threshold,
// )),
// }
// }
async fn stop_storage_session(self) -> Result<(), Self::StorageError> {
self.stop_client_use().await
}
@@ -39,20 +39,6 @@ impl Default for Empty {
impl ReplyStorageBackend for Empty {
type StorageError = UndefinedError;
// async fn new(
// debug_config: &crate::config::DebugConfig,
// _db_path: Option<PathBuf>,
// ) -> Result<Self, Self::StorageError> {
// Ok(Self {
// min_surb_threshold: debug_config
// .reply_surbs
// .minimum_reply_surb_storage_threshold,
// max_surb_threshold: debug_config
// .reply_surbs
// .maximum_reply_surb_storage_threshold,
// })
// }
async fn flush_surb_storage(
&mut self,
_storage: &CombinedReplyStorage,
@@ -73,28 +59,12 @@ impl ReplyStorageBackend for Empty {
self.max_surb_threshold,
))
}
// fn get_inactive_storage(&self) -> Result<CombinedReplyStorage, Self::StorageError> {
// Ok(CombinedReplyStorage::new(
// self.min_surb_threshold,
// self.max_surb_threshold,
// ))
// }
}
#[async_trait]
pub trait ReplyStorageBackend: Sized {
type StorageError: Error + 'static;
// async fn new(
// surb_config: &crate::config::ReplySurbs,
// db_path: Option<PathBuf>,
// ) -> Result<Self, Self::StorageError>;
//
// fn is_active(&self) -> bool {
// true
// }
async fn start_storage_session(&self) -> Result<(), Self::StorageError> {
Ok(())
}
@@ -112,11 +82,6 @@ pub trait ReplyStorageBackend: Sized {
async fn load_surb_storage(&self) -> Result<CombinedReplyStorage, Self::StorageError>;
// /// In the case the storage backend is initialized in an inactive state (persisting data is
// /// disabled), we might still need to fetch the (in-mem) storage and the parameters it was
// /// created with.
// fn get_inactive_storage(&self) -> Result<CombinedReplyStorage, Self::StorageError>;
async fn stop_storage_session(self) -> Result<(), Self::StorageError> {
Ok(())
}
-15
View File
@@ -250,21 +250,6 @@ where
Ok(shared_keys)
}
// #[cfg(not(target_arch = "wasm32"))]
// pub(super) async fn store_keys_on_disk<T>(
// key_manager: &KeyManager,
// config: &Config<T>,
// ) -> Result<(), ClientCoreError>
// where
// T: NymConfig,
// {
// let pathfinder = ClientKeyPathfinder::new_from_config(config);
// Ok(key_manager
// .persist_keys(&OnDiskKeys::new(pathfinder))
// .await
// .tap_err(|err| log::error!("Failed to generate keys: {err}"))?)
// }
// TODO: make it generic
#[cfg(not(target_arch = "wasm32"))]
pub(super) fn on_disk_key_store<T>(config: &Config<T>) -> OnDiskKeys
@@ -71,6 +71,11 @@ pub enum BackendError {
#[error("unable to parse the specified gateway")]
UnableToParseGateway,
#[error("unable to load keys: {source}")]
UnableToLoadKeys {
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error("HTTP get request failed: {status_code}")]
RequestFail {
url: reqwest::Url,
@@ -5,6 +5,7 @@ use crate::{
error::{BackendError, Result},
state::State,
};
use nym_client_core::client::key_manager::persistence::OnDiskKeys;
use nym_client_core::client::key_manager::KeyManager;
use nym_client_core::config::persistence::key_pathfinder::ClientKeyPathfinder;
use nym_crypto::asymmetric::identity;
@@ -18,7 +19,15 @@ pub async fn get_identity_key(
};
let pathfinder = ClientKeyPathfinder::new_from_config(config.get_base());
let key_manager = KeyManager::load_keys(&pathfinder)?;
// wtf, why are we loading EVERYTHING to just get identity key??
let key_store = OnDiskKeys::from(pathfinder);
let key_manager =
KeyManager::load_keys(&key_store)
.await
.map_err(|err| BackendError::UnableToLoadKeys {
source: Box::new(err),
})?;
let identity_keypair = key_manager.identity_keypair();
Ok(identity_keypair)
@@ -7,8 +7,7 @@ async fn main() {
// Specify some config options
let config_dir = PathBuf::from("/tmp/mixnet-client");
let storage_paths =
mixnet::StoragePaths::new_from_dir(mixnet::KeyMode::Keep, &config_dir).unwrap();
let storage_paths = mixnet::StoragePaths::new_from_dir(&config_dir).unwrap();
// Create the client with a storage backend, and enable it by giving it some paths. If keys
// exists at these paths, they will be loaded, otherwise they will be generated.