mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-02 19:46:13 -04:00
Merge #1231
1231: feat(asb): allow config overrides from env vars r=delta1 a=delta1 - upgrades config crate to 0.13.2 #1087 - adds environment source for config overrides This change allows the ASB config to be overridden by env vars starting with `ASB__`. Adds a test to check this, and updates the de/serialization of the network `listen` and `external_addresses` fields to be a comma separated string from the environment. The `separator` is a pair of underscores, which is necessary since some of the fields already have underscores in their names. Example env var: `ASB__NETWORK__EXTERNAL_ADDRESSES="/dns4/example.org/tcp/9939,/ip4/1.2.3.4/tcp/9940"` This particular change will help when running the ASB as a docker container (wip), allowing the config to be set in the env for docker-compose etc. Co-authored-by: Byron Hambly <bhambly@blockstream.com>
This commit is contained in:
commit
ee4273d2d5
3 changed files with 177 additions and 45 deletions
|
@ -20,7 +20,7 @@ big-bytes = "1"
|
|||
bitcoin = { version = "0.29", features = [ "rand", "serde" ] }
|
||||
bmrng = "0.5"
|
||||
comfy-table = "6.1"
|
||||
config = { version = "0.11", default-features = false, features = [ "toml" ] }
|
||||
config = { version = "0.13", default-features = false, features = [ "toml" ] }
|
||||
conquer-once = "0.3"
|
||||
curve25519-dalek = { package = "curve25519-dalek-ng", version = "4" }
|
||||
data-encoding = "2.3"
|
||||
|
@ -81,10 +81,11 @@ monero-harness = { path = "../monero-harness" }
|
|||
port_check = "0.1"
|
||||
proptest = "1"
|
||||
serde_cbor = "0.11"
|
||||
serial_test = "0.9"
|
||||
spectral = "0.6"
|
||||
tempfile = "3"
|
||||
testcontainers = "0.12"
|
||||
|
||||
[build-dependencies]
|
||||
vergen = { version = "7", default-features = false, features = [ "git", "build" ] }
|
||||
anyhow = "1"
|
||||
vergen = { version = "7", default-features = false, features = [ "git", "build" ] }
|
||||
|
|
|
@ -102,12 +102,27 @@ impl Config {
|
|||
{
|
||||
let config_file = Path::new(&config_file);
|
||||
|
||||
let mut config = config::Config::new();
|
||||
config.merge(config::File::from(config_file))?;
|
||||
let config = config::Config::builder()
|
||||
.add_source(config::File::from(config_file))
|
||||
.add_source(
|
||||
config::Environment::with_prefix("ASB")
|
||||
.separator("__")
|
||||
.list_separator(","),
|
||||
)
|
||||
.build()?;
|
||||
|
||||
config.try_into()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<config::Config> for Config {
|
||||
type Error = config::ConfigError;
|
||||
|
||||
fn try_from(value: config::Config) -> Result<Self, Self::Error> {
|
||||
value.try_deserialize()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Data {
|
||||
|
@ -117,13 +132,55 @@ pub struct Data {
|
|||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Network {
|
||||
#[serde(deserialize_with = "addr_list::deserialize")]
|
||||
pub listen: Vec<Multiaddr>,
|
||||
#[serde(default)]
|
||||
pub rendezvous_point: Option<Multiaddr>,
|
||||
#[serde(default)]
|
||||
#[serde(default, deserialize_with = "addr_list::deserialize")]
|
||||
pub external_addresses: Vec<Multiaddr>,
|
||||
}
|
||||
|
||||
mod addr_list {
|
||||
use libp2p::Multiaddr;
|
||||
use serde::de::Unexpected;
|
||||
use serde::{de, Deserialize, Deserializer};
|
||||
use serde_json::Value;
|
||||
|
||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Multiaddr>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = Value::deserialize(deserializer)?;
|
||||
return match s {
|
||||
Value::String(s) => {
|
||||
let list: Result<Vec<_>, _> = s
|
||||
.split(',')
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|s| s.parse().map_err(de::Error::custom))
|
||||
.collect();
|
||||
Ok(list?)
|
||||
}
|
||||
Value::Array(a) => {
|
||||
let list: Result<Vec<_>, _> = a
|
||||
.iter()
|
||||
.map(|v| {
|
||||
if let Value::String(s) = v {
|
||||
s.parse().map_err(de::Error::custom)
|
||||
} else {
|
||||
Err(de::Error::custom("expected a string"))
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Ok(list?)
|
||||
}
|
||||
value => Err(de::Error::invalid_type(
|
||||
Unexpected::Other(&value.to_string()),
|
||||
&"a string or array",
|
||||
)),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Bitcoin {
|
||||
|
@ -172,7 +229,7 @@ impl Default for TorConf {
|
|||
|
||||
#[derive(thiserror::Error, Debug, Clone, Copy)]
|
||||
#[error("config not initialized")]
|
||||
pub struct ConfigNotInitialized {}
|
||||
pub struct ConfigNotInitialized;
|
||||
|
||||
pub fn read_config(config_path: PathBuf) -> Result<Result<Config, ConfigNotInitialized>> {
|
||||
if config_path.exists() {
|
||||
|
@ -334,9 +391,12 @@ pub fn query_user_for_initial_config(testnet: bool) -> Result<Config> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serial_test::serial;
|
||||
use tempfile::tempdir;
|
||||
|
||||
// these tests are run serially since env vars affect the whole process
|
||||
#[test]
|
||||
#[serial]
|
||||
fn config_roundtrip_testnet() {
|
||||
let temp_dir = tempdir().unwrap().path().to_path_buf();
|
||||
let config_path = Path::join(&temp_dir, "config.toml");
|
||||
|
@ -358,7 +418,6 @@ mod tests {
|
|||
rendezvous_point: None,
|
||||
external_addresses: vec![],
|
||||
},
|
||||
|
||||
monero: Monero {
|
||||
wallet_rpc_url: defaults.monero_wallet_rpc_url,
|
||||
finality_confirmations: None,
|
||||
|
@ -380,6 +439,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn config_roundtrip_mainnet() {
|
||||
let temp_dir = tempdir().unwrap().path().to_path_buf();
|
||||
let config_path = Path::join(&temp_dir, "config.toml");
|
||||
|
@ -401,7 +461,6 @@ mod tests {
|
|||
rendezvous_point: None,
|
||||
external_addresses: vec![],
|
||||
},
|
||||
|
||||
monero: Monero {
|
||||
wallet_rpc_url: defaults.monero_wallet_rpc_url,
|
||||
finality_confirmations: None,
|
||||
|
@ -421,4 +480,60 @@ mod tests {
|
|||
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[serial]
|
||||
fn env_override() {
|
||||
let temp_dir = tempfile::tempdir().unwrap().path().to_path_buf();
|
||||
let config_path = Path::join(&temp_dir, "config.toml");
|
||||
|
||||
let defaults = Mainnet::getConfigFileDefaults().unwrap();
|
||||
|
||||
let dir = PathBuf::from("/tmp/dir");
|
||||
std::env::set_var("ASB__DATA__DIR", dir.clone());
|
||||
let addr1 = "/dns4/example.com/tcp/9939";
|
||||
let addr2 = "/ip4/1.2.3.4/tcp/9940";
|
||||
let external_addresses = vec![addr1.parse().unwrap(), addr2.parse().unwrap()];
|
||||
let listen = external_addresses.clone();
|
||||
std::env::set_var(
|
||||
"ASB__NETWORK__EXTERNAL_ADDRESSES",
|
||||
format!("{},{}", addr1, addr2),
|
||||
);
|
||||
std::env::set_var("ASB__NETWORK__LISTEN", format!("{},{}", addr1, addr2));
|
||||
|
||||
let expected = Config {
|
||||
data: Data { dir },
|
||||
bitcoin: Bitcoin {
|
||||
electrum_rpc_url: defaults.electrum_rpc_url,
|
||||
target_block: defaults.bitcoin_confirmation_target,
|
||||
finality_confirmations: None,
|
||||
network: bitcoin::Network::Bitcoin,
|
||||
},
|
||||
network: Network {
|
||||
listen,
|
||||
rendezvous_point: None,
|
||||
external_addresses,
|
||||
},
|
||||
monero: Monero {
|
||||
wallet_rpc_url: defaults.monero_wallet_rpc_url,
|
||||
finality_confirmations: None,
|
||||
network: monero::Network::Mainnet,
|
||||
},
|
||||
tor: Default::default(),
|
||||
maker: Maker {
|
||||
min_buy_btc: bitcoin::Amount::from_btc(DEFAULT_MIN_BUY_AMOUNT).unwrap(),
|
||||
max_buy_btc: bitcoin::Amount::from_btc(DEFAULT_MAX_BUY_AMOUNT).unwrap(),
|
||||
ask_spread: Decimal::from_f64(DEFAULT_SPREAD).unwrap(),
|
||||
price_ticker_ws_url: defaults.price_ticker_ws_url,
|
||||
},
|
||||
};
|
||||
|
||||
initial_setup(config_path.clone(), expected.clone()).unwrap();
|
||||
let actual = read_config(config_path).unwrap().unwrap();
|
||||
|
||||
assert_eq!(expected, actual);
|
||||
std::env::remove_var("ASB__DATA__DIR");
|
||||
std::env::remove_var("ASB__NETWORK__EXTERNAL_ADDRESSES");
|
||||
std::env::remove_var("ASB__NETWORK__LISTEN");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue