diff --git a/linera-service/src/linera/client_context.rs b/linera-service/src/linera/client_context.rs index 880a5e6774f..b7dcb6620db 100644 --- a/linera-service/src/linera/client_context.rs +++ b/linera-service/src/linera/client_context.rs @@ -29,7 +29,6 @@ use linera_service::{ chain_listener, config::WalletState, node_service::wait_for_next_round, - storage::StorageConfigNamespace, wallet::{UserChain, Wallet}, }; use linera_storage::Storage; @@ -103,11 +102,6 @@ impl chain_listener::ClientContext for ClientContext { } impl ClientContext { - pub fn from_options(options: &ClientOptions) -> Result { - let wallet_state = WalletState::from_file(&options.wallet_path()?)?; - Ok(Self::configure(options, wallet_state)) - } - /// Returns the [`Wallet`] as an immutable reference. fn wallet(&self) -> &Wallet { self.wallet_state.inner() @@ -118,7 +112,7 @@ impl ClientContext { self.wallet_state.inner_mut() } - fn configure(options: &ClientOptions, wallet_state: WalletState) -> Self { + pub fn new(options: &ClientOptions, wallet_state: WalletState) -> Self { let node_options = NodeOptions { send_timeout: options.send_timeout, recv_timeout: options.recv_timeout, @@ -139,27 +133,6 @@ impl ClientContext { } } - pub fn storage_config( - options: &ClientOptions, - ) -> Result { - match &options.storage_config { - Some(config) => config.parse(), - #[cfg(feature = "rocksdb")] - None => { - let storage_config = linera_service::storage::StorageConfig::RocksDb { - path: options.config_path()?.join("wallet.db"), - }; - let namespace = "default".to_string(); - Ok(StorageConfigNamespace { - storage_config, - namespace, - }) - } - #[cfg(not(feature = "rocksdb"))] - None => anyhow::bail!("A storage option must be provided"), - } - } - /// Retrieve the default account. Current this is the common account of the default /// chain. pub fn default_account(&self) -> Account { diff --git a/linera-service/src/linera/client_options.rs b/linera-service/src/linera/client_options.rs index 582c57721b8..8bf04fcce13 100644 --- a/linera-service/src/linera/client_options.rs +++ b/linera-service/src/linera/client_options.rs @@ -13,17 +13,17 @@ use linera_base::{ use linera_core::client::MessagePolicy; use linera_execution::{ committee::ValidatorName, system::SystemChannel, UserApplicationId, WasmRuntime, - WithWasmDefault, + WithWasmDefault as _, }; use linera_service::{ - chain_listener::{ChainListenerConfig, ClientContext as _}, + chain_listener::ChainListenerConfig, config::WalletState, - storage::{full_initialize_storage, run_with_storage}, + storage::{full_initialize_storage, run_with_storage, StorageConfigNamespace}, util, }; use linera_views::common::CommonStoreConfig; -use crate::{ClientContext, GenesisConfig, Job}; +use crate::{GenesisConfig, Job}; #[derive(clap::Parser)] #[command( @@ -121,44 +121,56 @@ impl ClientOptions { Ok(options) } + fn common_config(&self) -> CommonStoreConfig { + CommonStoreConfig { + max_concurrent_queries: self.max_concurrent_queries, + max_stream_queries: self.max_stream_queries, + cache_size: self.cache_size, + } + } + pub async fn run_command_with_storage(self) -> anyhow::Result<()> { - let context = ClientContext::from_options(&self)?; - let genesis_config = context.wallet().genesis_config().clone(); - let wasm_runtime = self.wasm_runtime.with_wasm_default(); - let max_concurrent_queries = self.max_concurrent_queries; - let max_stream_queries = self.max_stream_queries; - let cache_size = self.cache_size; - let storage_config = ClientContext::storage_config(&self)?; - let common_config = CommonStoreConfig { - max_concurrent_queries, - max_stream_queries, - cache_size, - }; - let full_storage_config = storage_config.add_common_config(common_config).await?; + let wallet = self.wallet()?; run_with_storage( - full_storage_config, - &genesis_config, - wasm_runtime, - Job(context, self.command), + self.storage_config()? + .add_common_config(self.common_config()) + .await?, + &wallet.inner().genesis_config().clone(), + self.wasm_runtime.with_wasm_default(), + Job(self, wallet), ) .await?; Ok(()) } + pub fn storage_config(&self) -> Result { + match &self.storage_config { + Some(config) => config.parse(), + #[cfg(feature = "rocksdb")] + None => { + let storage_config = linera_service::storage::StorageConfig::RocksDb { + path: self.config_path()?.join("wallet.db"), + }; + let namespace = "default".to_string(); + Ok(StorageConfigNamespace { + storage_config, + namespace, + }) + } + #[cfg(not(feature = "rocksdb"))] + None => anyhow::bail!("A storage option must be provided"), + } + } + pub async fn initialize_storage(&self) -> anyhow::Result<()> { - let context = ClientContext::from_options(self)?; - let genesis_config = context.wallet().genesis_config().clone(); - let max_concurrent_queries = self.max_concurrent_queries; - let max_stream_queries = self.max_stream_queries; - let cache_size = self.cache_size; - let storage_config = ClientContext::storage_config(self)?; - let common_config = CommonStoreConfig { - max_concurrent_queries, - max_stream_queries, - cache_size, - }; - let full_storage_config = storage_config.add_common_config(common_config).await?; - full_initialize_storage(full_storage_config, &genesis_config).await?; + let wallet = self.wallet()?; + full_initialize_storage( + self.storage_config()? + .add_common_config(self.common_config()) + .await?, + wallet.inner().genesis_config(), + ) + .await?; Ok(()) } diff --git a/linera-service/src/linera/main.rs b/linera-service/src/linera/main.rs index 9c642bbe118..922528ff346 100644 --- a/linera-service/src/linera/main.rs +++ b/linera-service/src/linera/main.rs @@ -33,7 +33,7 @@ use linera_execution::{ use linera_service::{ chain_listener::ClientContext as _, cli_wrappers, - config::{CommitteeConfig, Export, GenesisConfig, Import}, + config::{CommitteeConfig, Export, GenesisConfig, Import, WalletState}, faucet::FaucetService, node_service::NodeService, project::{self, Project}, @@ -75,7 +75,7 @@ fn deserialize_response(response: RpcMessage) -> Option { } } -struct Job(ClientContext, ClientCommand); +struct Job(ClientOptions, WalletState); fn read_json(string: Option, path: Option) -> anyhow::Result> { let value = match (string, path) { @@ -99,7 +99,10 @@ impl Runnable for Job { S: Storage + Clone + Send + Sync + 'static, ViewError: From, { - let Job(mut context, command) = self; + let Job(options, wallet) = self; + let mut context = ClientContext::new(&options, wallet); + let command = options.command; + use ClientCommand::*; match command { Transfer {