Skip to content

Commit

Permalink
linera-service: move context creation into Job (linera-io#2125)
Browse files Browse the repository at this point in the history
  • Loading branch information
Twey authored Jun 12, 2024
1 parent e18b349 commit a7a6fe8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 65 deletions.
29 changes: 1 addition & 28 deletions linera-service/src/linera/client_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,11 +102,6 @@ impl chain_listener::ClientContext<NodeProvider> for ClientContext {
}

impl ClientContext {
pub fn from_options(options: &ClientOptions) -> Result<Self, anyhow::Error> {
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()
Expand All @@ -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,
Expand All @@ -139,27 +133,6 @@ impl ClientContext {
}
}

pub fn storage_config(
options: &ClientOptions,
) -> Result<StorageConfigNamespace, anyhow::Error> {
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 {
Expand Down
80 changes: 46 additions & 34 deletions linera-service/src/linera/client_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<StorageConfigNamespace, anyhow::Error> {
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(())
}

Expand Down
9 changes: 6 additions & 3 deletions linera-service/src/linera/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -75,7 +75,7 @@ fn deserialize_response(response: RpcMessage) -> Option<ChainInfoResponse> {
}
}

struct Job(ClientContext, ClientCommand);
struct Job(ClientOptions, WalletState);

fn read_json(string: Option<String>, path: Option<PathBuf>) -> anyhow::Result<Vec<u8>> {
let value = match (string, path) {
Expand All @@ -99,7 +99,10 @@ impl Runnable for Job {
S: Storage + Clone + Send + Sync + 'static,
ViewError: From<S::ContextError>,
{
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 {
Expand Down

0 comments on commit a7a6fe8

Please sign in to comment.