Skip to content

Commit

Permalink
feat: add localhost as a network option
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiagoPittella committed Jan 23, 2025
1 parent 021c96c commit d0b7f56
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 36 deletions.
24 changes: 14 additions & 10 deletions bin/miden-cli/src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{fs::File, io::Write, path::PathBuf, str::FromStr};

use clap::Parser;
use miden_client::rpc::Endpoint;

use crate::{
config::{CliConfig, CliEndpoint},
Expand All @@ -12,9 +13,10 @@ use crate::{

#[derive(Debug, Clone)]
enum Network {
Custom(String),
Devnet,
Localhost,
Testnet,
Custom(String),
}

impl FromStr for Network {
Expand All @@ -23,6 +25,7 @@ impl FromStr for Network {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"devnet" => Ok(Network::Devnet),
"localhost" => Ok(Network::Localhost),
"testnet" => Ok(Network::Testnet),
custom => Ok(Network::Custom(custom.to_string())),
}
Expand All @@ -31,11 +34,12 @@ impl FromStr for Network {

impl Network {
/// Converts the Network variant to its corresponding RPC endpoint string
pub fn to_rpc_endpoint(&self) -> &str {
pub fn to_rpc_endpoint(&self) -> String {
match self {
Network::Devnet => "https://rpc.devnet.miden.io",
Network::Testnet => "https://rpc.testnet.miden.io",
Network::Custom(custom) => custom,
Network::Custom(custom) => custom.clone(),
Network::Devnet => "https://rpc.devnet.miden.io".to_string(),
Network::Localhost => Endpoint::default().to_string(),
Network::Testnet => "https://rpc.testnet.miden.io".to_string(),
}
}
}
Expand All @@ -47,9 +51,9 @@ the CLI and client configurations, and will be placed by default in the current
directory."
)]
pub struct InitCmd {
/// Network configuration to use. Options are devnet, testnet, or a custom RPC endpoint.
/// Defaults to a local network.
#[clap(long, short, value_enum)]
/// Network configuration to use. Options are devnet, testnet, localhost or a custom RPC
/// endpoint. Defaults to the testnet network.
#[clap(long, short, default_value = "testnet")]
network: Option<Network>,

/// Store file path.
Expand Down Expand Up @@ -77,8 +81,8 @@ impl InitCmd {
let mut cli_config = CliConfig::default();

if let Some(endpoint) = &self.network {
let endpoint =
CliEndpoint::try_from(endpoint.to_rpc_endpoint()).map_err(|err| err.to_string())?;
let endpoint = CliEndpoint::try_from(endpoint.to_rpc_endpoint().as_str())
.map_err(|err| err.to_string())?;

cli_config.rpc.endpoint = endpoint;
}
Expand Down
30 changes: 4 additions & 26 deletions bin/miden-cli/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use miden_client::{
Note, NoteAssets, NoteExecutionHint, NoteExecutionMode, NoteFile, NoteInputs, NoteMetadata,
NoteRecipient, NoteTag, NoteType,
},
rpc::{Endpoint, TonicRpcClient},
rpc::TonicRpcClient,
store::{sqlite_store::SqliteStore, NoteFilter, StoreAuthenticator},
testing::account_id::ACCOUNT_ID_OFF_CHAIN_SENDER,
transaction::{OutputNote, TransactionRequestBuilder},
Expand Down Expand Up @@ -77,16 +77,8 @@ fn test_init_with_params() {
temp_dir.push(format!("{}", uuid::Uuid::new_v4()));
std::fs::create_dir(temp_dir.clone()).unwrap();

let local_node_address = Endpoint::default();

let mut init_cmd = Command::cargo_bin("miden").unwrap();
init_cmd.args([
"init",
"--network",
&local_node_address.to_string(),
"--store-path",
store_path.to_str().unwrap(),
]);
init_cmd.args(["init", "--network", "localhost", "--store-path", store_path.to_str().unwrap()]);
init_cmd.current_dir(&temp_dir).assert().success();

// Assert the config file contains the specified contents
Expand All @@ -103,13 +95,7 @@ fn test_init_with_params() {

// Trying to init twice should result in an error
let mut init_cmd = Command::cargo_bin("miden").unwrap();
init_cmd.args([
"init",
"--network",
&local_node_address.to_string(),
"--store-path",
store_path.to_str().unwrap(),
]);
init_cmd.args(["init", "--network", "localhost", "--store-path", store_path.to_str().unwrap()]);
init_cmd.current_dir(&temp_dir).assert().failure();
}

Expand Down Expand Up @@ -469,15 +455,7 @@ fn test_cli_empty_commands() {

let mut init_cmd = Command::cargo_bin("miden").unwrap();

let local_node_address = Endpoint::default();

init_cmd.args([
"init",
"--network",
&local_node_address.to_string(),
"--store-path",
store_path.to_str().unwrap(),
]);
init_cmd.args(["init", "--network", "localhost", "--store-path", store_path.to_str().unwrap()]);
init_cmd.current_dir(&temp_dir).assert().success();

let mut create_faucet_cmd = Command::cargo_bin("miden").unwrap();
Expand Down
1 change: 1 addition & 0 deletions docs/cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ miden init --network https://18.203.155.106:1234
# You can use one of the pre-defined networks
miden init --network testnet
miden init --network devnet
miden init --network localhost

# You can use the --store_path flag to override the default store config
miden init --store_path db/store.sqlite3
Expand Down

0 comments on commit d0b7f56

Please sign in to comment.