Skip to content

Commit

Permalink
feat : provider config file (#320)
Browse files Browse the repository at this point in the history
* working config file

* rustfmt

* minors changes (logs, comments, fmt)
  • Loading branch information
undercover-cactus authored Jan 14, 2025
1 parent 4515110 commit d00d38d
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ test/dist/*

# Ignore cSpell config files
cSpell.json

# Don't commit your config file
config.toml
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ serde_json = { version = "1.0.121", default-features = false }
smallvec = "1.11.0"
thiserror = "1.0.48"
tokio = "1.36.0"
toml = "0.8.19"
trie-db = { version = "0.29.1", default-features = false }

# Substrate
Expand Down
6 changes: 6 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[provider]
provider_type = "bsp" # other choices are 'msp' and 'user'
storage_layer = "memory" # other choice is 'rocksdb'
max_storage_capacity = 4294967295
jump_capacity = 1073741824
extrinsic_retry_timeout = 60
1 change: 1 addition & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ kvdb-rocksdb = { workspace = true }
lazy-static = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
toml = { workspace = true }

# Local
pallet-file-system = { workspace = true }
Expand Down
47 changes: 44 additions & 3 deletions node/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{path::PathBuf, str::FromStr};

use clap::{Parser, ValueEnum};
use serde::{Deserialize, Deserializer};
use std::{path::PathBuf, str::FromStr};
use storage_hub_runtime::StorageDataUnit;

use crate::command::ProviderOptions;
Expand Down Expand Up @@ -54,6 +54,25 @@ pub enum ProviderType {
User,
}

impl<'de> serde::Deserialize<'de> for ProviderType {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let s = String::deserialize(d)?;

let provider_type = match s.as_str() {
"bsp" => ProviderType::Bsp,
"msp" => ProviderType::Msp,
"user" => ProviderType::User,
_ => {
return Err(serde::de::Error::custom(
"Cannot parse `provider_type`. Invalid value.",
))
}
};

Ok(provider_type)
}
}

#[derive(ValueEnum, Clone, Debug)]
pub enum StorageLayer {
/// RocksDB with path.
Expand All @@ -62,6 +81,24 @@ pub enum StorageLayer {
Memory,
}

impl<'de> serde::Deserialize<'de> for StorageLayer {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let s = String::deserialize(d)?;

let storage_layer = match s.as_str() {
"rocksdb" => StorageLayer::RocksDB,
"memory" => StorageLayer::Memory,
_ => {
return Err(serde::de::Error::custom(
"Cannot parse `storage_type`. Invalid value.",
))
}
};

Ok(storage_layer)
}
}

#[derive(Debug, Parser)]
#[group(skip)]
pub struct ProviderConfigurations {
Expand Down Expand Up @@ -158,7 +195,7 @@ pub struct IndexerConfigurations {
}

/// Block authoring scheme to be used by the dev service.
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, Deserialize)]
pub enum Sealing {
/// Author a block immediately upon receiving a transaction into the transaction pool
Instant,
Expand Down Expand Up @@ -228,6 +265,10 @@ pub struct Cli {
#[command(flatten)]
pub provider_config: ProviderConfigurations,

/// Provider configurations file path (allow to specify the provider configuration in a file instead of the cli)
#[clap(long, conflicts_with_all = ["provider", "provider_type", "max_storage_capacity", "jump_capacity", "storage_layer", "storage_path", "extrinsic_retry_timeout", "msp_charging_period"])]
pub provider_config_file: Option<String>,

/// Indexer configurations
#[command(flatten)]
pub indexer_config: IndexerConfigurations,
Expand Down
21 changes: 16 additions & 5 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ use sc_cli::{
NetworkParams, Result, RpcEndpoint, SharedParams, SubstrateCli,
};
use sc_service::config::{BasePath, PrometheusConfig};
use serde::Deserialize;
use storage_hub_runtime::{Block, StorageDataUnit};

use crate::{
chain_spec,
cli::{Cli, ProviderType, RelayChainCli, StorageLayer, Subcommand},
config,
service::new_partial,
};

// TODO: Have specific StorageHub role options (i.e. ProviderOptions, UserOptions).
/// Configuration for the provider.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Deserialize)]
pub struct ProviderOptions {
/// Provider type.
pub provider_type: ProviderType,
Expand Down Expand Up @@ -244,11 +246,20 @@ pub fn run() -> Result<()> {
}
}
None => {
let mut provider_options = None;
let runner = cli.create_runner(&cli.run.normalize())?;
let provider_options = if cli.provider_config.provider {
Some(cli.provider_config.provider_options())
} else {
None

// If we have a provider config file
if let Some(provider_config_file) = cli.provider_config_file {
let config = config::read_config(&provider_config_file);
if let Some(c) = config {
provider_options = Some(c.provider);
};
};

// We then check cli (the cli doesn't allow to have both cli parameters and a config file so we should not have overlap here)
if cli.provider_config.provider {
provider_options = Some(cli.provider_config.provider_options());
};

runner.run_node_until_exit(|config| async move {
Expand Down
42 changes: 42 additions & 0 deletions node/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use log::error;
use serde::Deserialize;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use toml;

use crate::command::ProviderOptions;

#[derive(Clone, Debug, Deserialize)]
pub struct Config {
pub provider: ProviderOptions,
}

pub fn read_config(path: &str) -> Option<Config> {
let path = Path::new(path);

if !path.exists() {
error!("Fail to find config file ({:?})", path);

return None;
}

let mut file = File::open(path).expect("config.toml file should exist");
let mut contents = String::new();
if let Err(err) = file.read_to_string(&mut contents) {
error!("Fail to read config file : {}", err);

return None;
};

let config = match toml::from_str(&contents) {
Err(err) => {
error!("Fail to parse config file : {}", err);

return None;
}
Ok(c) => c,
};

return Some(config);
}
1 change: 1 addition & 0 deletions node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
mod chain_spec;
mod cli;
mod command;
mod config;
mod rpc;
mod service;
mod services;
Expand Down

0 comments on commit d00d38d

Please sign in to comment.