Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add (local|testnet|mainnet) default networks #1408

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

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

30 changes: 30 additions & 0 deletions cmd/crates/soroban-test/tests/it/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,36 @@ fn set_and_remove_network() {
});
}

#[test]
fn use_default_futurenet() {
TestEnv::with_default(|sandbox| {
sandbox
.new_assert_cmd("keys")
.args(["generate", "alice", "--network", "futurenet"])
.assert()
.success();
let dir = sandbox.dir().join(".soroban").join("network");
let mut read_dir = std::fs::read_dir(dir).unwrap();
let file = read_dir.next().unwrap().unwrap();
assert_eq!(file.file_name().to_str().unwrap(), "futurenet.toml");
});
}

#[test]
fn use_default_testnet() {
TestEnv::with_default(|sandbox| {
sandbox
.new_assert_cmd("keys")
.args(["generate", "alice", "--network", "testnet"])
.assert()
.success();
let dir = sandbox.dir().join(".soroban").join("network");
let mut read_dir = std::fs::read_dir(dir).unwrap();
let file = read_dir.next().unwrap().unwrap();
assert_eq!(file.file_name().to_str().unwrap(), "testnet.toml");
});
}

fn add_network(sandbox: &TestEnv, name: &str) {
sandbox
.new_assert_cmd("network")
Expand Down
1 change: 1 addition & 0 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ rust-embed = { version = "8.2.0", features = ["debug-embed"] }
bollard = { workspace=true }
futures-util = "0.3.30"
home = "0.5.9"
phf = { version = "0.11.2", features = ["macros"] }
# For hyper-tls
[target.'cfg(unix)'.dependencies]
openssl = { version = "=0.10.55", features = ["vendored"] }
Expand Down
39 changes: 24 additions & 15 deletions cmd/soroban-cli/src/commands/config/locator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::arg;
use itertools::Itertools;
use serde::de::DeserializeOwned;
use std::{
ffi::OsStr,
Expand All @@ -13,7 +14,11 @@ use stellar_strkey::{Contract, DecodeError};

use crate::{utils::find_config_dir, Pwd};

use super::{alias, network::Network, secret::Secret};
use super::{
alias,
network::{self, Network},
secret::Secret,
};

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -93,7 +98,7 @@ impl Display for Location {
Location::Local(_) => "Local",
Location::Global(_) => "Global",
},
self.as_ref().parent().unwrap().parent().unwrap()
self.as_ref()
)
}
}
Expand Down Expand Up @@ -175,40 +180,44 @@ impl Args {
}

pub fn list_networks(&self) -> Result<Vec<String>, Error> {
Ok(KeyType::Network
let saved_networks = KeyType::Network
.list_paths(&self.local_and_global()?)
.into_iter()
.flatten()
.map(|x| x.0)
.collect())
.map(|x| x.0);
let default_networks = network::DEFAULTS.keys().map(ToString::to_string);
Ok(saved_networks.chain(default_networks).unique().collect())
}

pub fn list_networks_long(&self) -> Result<Vec<(String, Network, Location)>, Error> {
Ok(KeyType::Network
pub fn list_networks_long(&self) -> Result<Vec<(String, Network, String)>, Error> {
let saved_networks = KeyType::Network
.list_paths(&self.local_and_global()?)
.into_iter()
.flatten()
.filter_map(|(name, location)| {
Some((
name,
KeyType::read_from_path::<Network>(location.as_ref()).ok()?,
location,
location.to_string(),
))
})
.collect::<Vec<_>>())
});
let default_networks = network::DEFAULTS
.into_iter()
.map(|(name, network)| ((*name).to_string(), network.into(), "Default".to_owned()));
Ok(saved_networks.chain(default_networks).collect())
}

pub fn read_identity(&self, name: &str) -> Result<Secret, Error> {
KeyType::Identity.read_with_global(name, &self.local_config()?)
}

pub fn read_network(&self, name: &str) -> Result<Network, Error> {
let res = KeyType::Network.read_with_global(name, &self.local_config()?);
if let Err(Error::ConfigMissing(_, _)) = &res {
if name == "futurenet" {
let network = Network::futurenet();
self.write_network(name, &network)?;
return Ok(network);
}
let Some(network) = network::DEFAULTS.get(name) else {
return res;
};
return Ok(network.into());
}
res
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/soroban-cli/src/commands/contract/bindings/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ impl NetworkRunnable for Cmd {
rpc_url,
network_passphrase,
..
} = self
.network
.get(&self.locator)
.ok()
.unwrap_or_else(Network::futurenet);
} = self.network.get(&self.locator).ok().unwrap_or_else(|| {
network::DEFAULTS
.get("futurenet")
.expect("why did we remove the default futurenet network?")
.into()
});
let absolute_path = self.output_dir.canonicalize()?;
let file_name = absolute_path
.file_name()
Expand Down
3 changes: 1 addition & 2 deletions cmd/soroban-cli/src/commands/network/ls.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clap::command;

use super::locator;
use crate::commands::config::locator::Location;

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -36,7 +35,7 @@ impl Cmd {
.list_networks_long()?
.iter()
.filter_map(|(name, network, location)| {
(!self.config_locator.global || matches!(location, Location::Global(_)))
(!self.config_locator.global || location == "Global")
chadoh marked this conversation as resolved.
Show resolved Hide resolved
.then(|| Some(format!("{location}\nName: {name}\n{network:#?}\n")))?
})
.collect())
Expand Down
31 changes: 26 additions & 5 deletions cmd/soroban-cli/src/commands/network/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::str::FromStr;

use clap::{arg, Parser};
use phf::phf_map;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use stellar_strkey::ed25519::PublicKey;
Expand Down Expand Up @@ -247,11 +248,31 @@ impl Network {
}
}

impl Network {
pub fn futurenet() -> Self {
Network {
rpc_url: "https://rpc-futurenet.stellar.org:443".to_owned(),
network_passphrase: "Test SDF Future Network ; October 2022".to_owned(),
pub static DEFAULTS: phf::Map<&'static str, (&'static str, &'static str)> = phf_map! {
"local" => (
"http://localhost:8000/rpc",
"Standalone Network ; February 2017",
),
"futurenet" => (
"https://soroban-testnet.stellar.org",
"Test SDF Network ; September 2015",
),
"testnet" => (
"https://rpc-futurenet.stellar.org:443",
"Test SDF Future Network ; October 2022",
),
"mainnet" => (
"Bring Your Own: https://developers.stellar.org/docs/data/rpc/rpc-providers",
"Public Global Stellar Network ; September 2015",
),
};

impl From<&(&str, &str)> for Network {
/// Convert the return value of `DEFAULTS.get()` into a Network
fn from(n: &(&str, &str)) -> Self {
Self {
rpc_url: n.0.to_string(),
network_passphrase: n.1.to_string(),
}
}
}
Loading