Skip to content

Commit afbe59c

Browse files
authored
Merge pull request #408 from consensus-shipyard/use-cli-for-nwtwork
Use cli parameter to set netowrk
2 parents 6412e88 + a4f1139 commit afbe59c

4 files changed

Lines changed: 45 additions & 17 deletions

File tree

ipc/cli/src/commands/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use clap_complete::{generate, Generator, Shell};
2121
use fvm_shared::econ::TokenAmount;
2222
use ipc_sdk::ethers_address_to_fil_address;
2323

24+
use fvm_shared::address::set_current_network;
2425
use ipc_provider::config::{Config, Subnet};
2526
use ipc_sdk::subnet_id::SubnetID;
2627
use std::fmt::Debug;
@@ -65,6 +66,23 @@ struct IPCAgentCliCommands {
6566
command: Option<Commands>,
6667
}
6768

69+
/// A version of options that does partial matching on the arguments, with its only interest
70+
/// being the capture of global parameters that need to take effect first, before we parse [Options],
71+
/// because their value affects how others arse parsed.
72+
///
73+
/// This one doesn't handle `--help` or `help` so that it is passed on to the next parser,
74+
/// where the full set of commands and arguments can be printed properly.
75+
#[derive(Parser, Debug)]
76+
#[command(version, disable_help_flag = true)]
77+
struct GlobalOptions {
78+
#[command(flatten)]
79+
global_params: GlobalArguments,
80+
81+
/// Capture all the normal commands, basically to ingore them.
82+
#[arg(allow_hyphen_values = true, trailing_var_arg = true)]
83+
pub cmd: Vec<String>,
84+
}
85+
6886
/// The `cli` method exposed to handle all the cli commands, ideally from main.
6987
///
7088
/// # Examples
@@ -94,6 +112,9 @@ struct IPCAgentCliCommands {
94112
/// }
95113
/// ```
96114
pub async fn cli() -> anyhow::Result<()> {
115+
let global = GlobalOptions::parse();
116+
set_current_network(global.global_params.network);
117+
97118
// parse the arguments
98119
let args = IPCAgentCliCommands::parse();
99120

ipc/cli/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use anyhow::Result;
44
use async_trait::async_trait;
55
use clap::Args;
6+
use fvm_shared::address::Network;
7+
use num_traits::cast::FromPrimitive;
68

79
mod commands;
810

@@ -36,6 +38,10 @@ pub struct GlobalArguments {
3638
help = "The toml config file path for IPC Agent, default to ${HOME}/.ipc-agent/config.toml"
3739
)]
3840
config_path: Option<String>,
41+
42+
/// Set the FVM Address Network. It's value affects whether `f` (main) or `t` (test) prefixed addresses are accepted.
43+
#[arg(short, long, default_value = "mainnet", env = "NETWORK", value_parser = parse_network)]
44+
pub network: Network,
3945
}
4046

4147
impl GlobalArguments {
@@ -50,3 +56,20 @@ impl GlobalArguments {
5056
Config::from_file(config_path)
5157
}
5258
}
59+
60+
/// Parse the FVM network and set the global value.
61+
fn parse_network(s: &str) -> Result<Network, String> {
62+
match s.to_lowercase().as_str() {
63+
"main" | "mainnet" | "f" => Ok(Network::Mainnet),
64+
"test" | "testnet" | "t" => Ok(Network::Testnet),
65+
n => {
66+
let n: u8 = n
67+
.parse()
68+
.map_err(|e| format!("expected 0 or 1 for network: {e}"))?;
69+
70+
let n = Network::from_u8(n).ok_or_else(|| format!("unexpected network: {s}"))?;
71+
72+
Ok(n)
73+
}
74+
}
75+
}

ipc/cli/src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
// Copyright 2022-2023 Protocol Labs
22
// SPDX-License-Identifier: MIT
3-
use ipc_provider::set_fil_network_from_env;
43

54
#[tokio::main]
65
async fn main() {
76
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
8-
set_fil_network_from_env();
97

108
if let Err(e) = ipc_cli::cli().await {
119
log::error!("main process failed: {e:#}");

ipc/provider/src/lib.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ use anyhow::anyhow;
77
use base64::Engine;
88
use config::Config;
99
use fvm_shared::{
10-
address::{set_current_network, Address, Network},
11-
clock::ChainEpoch,
12-
crypto::signature::SignatureType,
13-
econ::TokenAmount,
10+
address::Address, clock::ChainEpoch, crypto::signature::SignatureType, econ::TokenAmount,
1411
};
1512
use ipc_identity::{
1613
EthKeyAddress, EvmKeyStore, KeyStore, KeyStoreConfig, PersistentKeyStore, Wallet,
@@ -24,7 +21,6 @@ use ipc_sdk::{
2421
};
2522
use lotus::message::wallet::WalletKeyType;
2623
use manager::{EthSubnetManager, SubnetGenesisInfo, SubnetInfo, SubnetManager};
27-
use num_traits::FromPrimitive;
2824
use serde::{Deserialize, Serialize};
2925
use std::{
3026
borrow::Borrow,
@@ -44,16 +40,6 @@ pub mod manager;
4440
const DEFAULT_REPO_PATH: &str = ".ipc";
4541
const DEFAULT_CONFIG_NAME: &str = "config.toml";
4642

47-
pub fn set_fil_network_from_env() {
48-
let network_raw: u8 = std::env::var("LOTUS_NETWORK")
49-
// default to testnet
50-
.unwrap_or_else(|_| String::from("1"))
51-
.parse()
52-
.unwrap();
53-
let network = Network::from_u8(network_raw).unwrap();
54-
set_current_network(network);
55-
}
56-
5743
/// The subnet manager connection that holds the subnet config and the manager instance.
5844
pub struct Connection {
5945
subnet: config::Subnet,

0 commit comments

Comments
 (0)