nym-sdk: remove a few unwraps related to creating reply surb storage (#3006)

This commit is contained in:
Jon Häggblad
2023-02-09 16:55:01 +01:00
committed by GitHub
parent dbb7a27441
commit 2e38c5e38e
6 changed files with 48 additions and 13 deletions
@@ -29,13 +29,16 @@ impl Backend {
impl ReplyStorageBackend for Backend {
type StorageError = <Empty as ReplyStorageBackend>::StorageError;
async fn new(debug_config: &crate::config::DebugConfig, _db_path: Option<PathBuf>) -> Self {
Backend {
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.minimum_reply_surb_storage_threshold,
max_surb_threshold: debug_config.maximum_reply_surb_storage_threshold,
},
}
})
}
async fn flush_surb_storage(
@@ -56,4 +56,9 @@ pub enum StorageError {
details: String,
// err: Option<Box<dyn std::error::Error>>
},
#[error("failed to create storage")]
FailedToCreateStorage {
source: Box<dyn std::error::Error + Send + Sync>,
},
}
@@ -368,10 +368,18 @@ impl Backend {
impl ReplyStorageBackend for Backend {
type StorageError = error::StorageError;
async fn new(debug_config: &crate::config::DebugConfig, db_path: Option<PathBuf>) -> Self {
async fn new(
debug_config: &crate::config::DebugConfig,
db_path: Option<PathBuf>,
) -> Result<Self, Self::StorageError> {
non_wasm_helpers::setup_fs_reply_surb_backend(db_path, debug_config)
.await
.unwrap()
.map_err(|err| {
log::error!("Failed to create storage: {err}");
Self::StorageError::FailedToCreateStorage {
source: Box::new(err),
}
})
}
fn is_active(&self) -> bool {
@@ -30,11 +30,14 @@ pub struct Empty {
impl ReplyStorageBackend for Empty {
type StorageError = UndefinedError;
async fn new(debug_config: &crate::config::DebugConfig, _db_path: Option<PathBuf>) -> Self {
Self {
async fn new(
debug_config: &crate::config::DebugConfig,
_db_path: Option<PathBuf>,
) -> Result<Self, Self::StorageError> {
Ok(Self {
min_surb_threshold: debug_config.minimum_reply_surb_storage_threshold,
max_surb_threshold: debug_config.maximum_reply_surb_storage_threshold,
}
})
}
async fn flush_surb_storage(
@@ -70,7 +73,10 @@ impl ReplyStorageBackend for Empty {
pub trait ReplyStorageBackend: Sized {
type StorageError: Error + 'static;
async fn new(debug_config: &crate::config::DebugConfig, db_path: Option<PathBuf>) -> Self;
async fn new(
debug_config: &crate::config::DebugConfig,
db_path: Option<PathBuf>,
) -> Result<Self, Self::StorageError>;
fn is_active(&self) -> bool {
true
+5
View File
@@ -33,6 +33,11 @@ pub enum Error {
ReregisteringGatewayNotSupported,
#[error("no gateway key set")]
NoGatewayKeySet,
#[error("failed to create reply storage backend: {source}")]
StorageError {
source: Box<dyn std::error::Error + Send + Sync>,
},
}
pub type Result<T, E = Error> = std::result::Result<T, E>;
+12 -4
View File
@@ -69,11 +69,12 @@ impl MixnetClientBuilder {
pub async fn build<B>(self) -> Result<DisconnectedMixnetClient<B>>
where
B: ReplyStorageBackend + Send + Sync + 'static,
<B as ReplyStorageBackend>::StorageError: Send + Sync,
{
let config = self.config.unwrap_or_default();
let storage_paths = self.storage_paths;
let mut client = DisconnectedMixnetClient::new(Some(config), storage_paths).await;
let mut client = DisconnectedMixnetClient::new(Some(config), storage_paths).await?;
if let Some(keys) = self.keys {
client.set_keys(keys);
@@ -121,15 +122,22 @@ where
async fn new(
config: Option<Config>,
paths: Option<StoragePaths>,
) -> DisconnectedMixnetClient<B> {
) -> Result<DisconnectedMixnetClient<B>>
where
<B as ReplyStorageBackend>::StorageError: Send + Sync,
{
let config = config.unwrap_or_default();
let reply_surb_database_path = paths.as_ref().map(|p| p.reply_surb_database_path.clone());
let reply_storage_backend =
ReplyStorageBackend::new(&config.debug_config, reply_surb_database_path).await;
ReplyStorageBackend::new(&config.debug_config, reply_surb_database_path)
.await
.map_err(|err| Error::StorageError {
source: Box::new(err),
})?;
create_new_client_with_custom_storage(Some(config), paths, reply_storage_backend).unwrap()
create_new_client_with_custom_storage(Some(config), paths, reply_storage_backend)
}
/// Client keys are generated at client creation if none were found. The gateway shared