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

chore: create storage kind #1883

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 5 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::eth::follower::importer::ImporterConfig;
use crate::eth::miner::MinerConfig;
use crate::eth::primitives::Address;
use crate::eth::rpc::RpcServerConfig;
use crate::eth::storage::StratusStorageConfig;
use crate::eth::storage::StorageConfig;
use crate::ext::parse_duration;
use crate::infra::build_info;
use crate::infra::kafka::KafkaConfig;
Expand Down Expand Up @@ -190,7 +190,7 @@ pub struct StratusConfig {
pub rpc_server: RpcServerConfig,

#[clap(flatten)]
pub storage: StratusStorageConfig,
pub storage: StorageConfig,

#[clap(flatten)]
pub executor: ExecutorConfig,
Expand Down Expand Up @@ -290,7 +290,7 @@ pub struct ImporterOfflineConfig {
pub miner: MinerConfig,

#[clap(flatten)]
pub storage: StratusStorageConfig,
pub storage: StorageConfig,

#[clap(flatten)]
pub rpc_storage: ExternalRpcConfig,
Expand Down Expand Up @@ -347,7 +347,7 @@ pub struct IntegrationTestConfig {
pub miner: MinerConfig,

#[clap(flatten)]
pub storage: StratusStorageConfig,
pub storage: StorageConfig,

#[clap(flatten)]
pub rpc_storage: ExternalRpcConfig,
Expand Down Expand Up @@ -391,7 +391,7 @@ impl FromStr for Environment {
"staging" | "test" => Ok(Self::Staging),
"production" | "prod" => Ok(Self::Production),
"canary" => Ok(Self::Canary),
s => Err(anyhow!("unknown environment: \"{}\" - valid values are {:?}", s, Environment::VARIANTS)),
s => Err(anyhow!("unknown environment: \"{}\" - valid values are {:?}", s, Self::VARIANTS)),
}
}
}
Expand Down
30 changes: 28 additions & 2 deletions src/eth/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub use permanent::PermanentStorage;
pub use permanent::PermanentStorageConfig;
pub use permanent::PermanentStorageKind;
pub use stratus_storage::StratusStorage;
use strum::VariantNames;
pub use temporary::InMemoryTemporaryStorage;
pub use temporary::TemporaryStorage;
pub use temporary::TemporaryStorageConfig;
Expand All @@ -14,8 +15,10 @@ pub mod permanent;
mod stratus_storage;
mod temporary;

use std::str::FromStr;
use std::sync::Arc;

use anyhow::anyhow;
use clap::Parser;
use display_json::DebugAsJson;

Expand Down Expand Up @@ -104,21 +107,44 @@ pub trait Storage: Sized {

/// Configuration that can be used by any binary that interacts with Stratus storage.
#[derive(Parser, DebugAsJson, Clone, serde::Serialize)]
pub struct StratusStorageConfig {
pub struct StorageConfig {
#[arg(long = "storage-kind", env = "STORAGE_KIND", default_value = "stratus-storage")]
pub storage_kind: StorageKind,

#[clap(flatten)]
pub temp_storage: TemporaryStorageConfig,

#[clap(flatten)]
pub perm_storage: PermanentStorageConfig,
}

impl StratusStorageConfig {
impl StorageConfig {
/// Initializes Stratus storage.
pub fn init(&self) -> Result<Arc<StratusStorage>, StratusError> {
let temp_storage = self.temp_storage.init()?;
let perm_storage = self.perm_storage.init()?;
let StorageKind::StratusStorage = self.storage_kind;
let storage = StratusStorage::new(temp_storage, perm_storage)?;

Ok(Arc::new(storage))
}
}

#[derive(DebugAsJson, strum::Display, strum::VariantNames, Parser, Clone, serde::Serialize)]
pub enum StorageKind {
#[serde(rename = "stratus-storage")]
#[strum(to_string = "stratus-storage")]
StratusStorage,
}

impl FromStr for StorageKind {
type Err = anyhow::Error;

fn from_str(s: &str) -> anyhow::Result<Self, Self::Err> {
let s = s.trim().to_lowercase();
match s.as_ref() {
"stratus-storage" | "stratus_storage" => Ok(Self::StratusStorage),
s => Err(anyhow!("unknown storage kind: \"{}\" - valid values are {:?}", s, Self::VARIANTS)),
}
}
}
Loading