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(agents): improve agent config #171

Merged
merged 20 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from 14 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
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion configuration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ js-sys = "0.3.56"
wasm-bindgen = { version = "0.2.79", features = ["serde-serialize"] }

[dev-dependencies]
dotenv = "0.15.0"
dotenv = "0.15.0"
serial_test = "0.6.0"
nomad-test = { path = "../nomad-test" }
6 changes: 5 additions & 1 deletion configuration/src/agent/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Default for SignerConf {
}

impl FromEnv for SignerConf {
fn from_env(prefix: &str) -> Option<Self> {
fn from_env(prefix: &str, default_prefix: Option<&str>) -> Option<Self> {
// ordering this first preferentially uses AWS if both are specified
if let Ok(id) = std::env::var(&format!("{}_ID", prefix)) {
if let Ok(region) = std::env::var(&format!("{}_REGION", prefix)) {
Expand All @@ -40,6 +40,10 @@ impl FromEnv for SignerConf {
return Some(SignerConf::HexKey(HexString::from_str(&signer_key).ok()?));
}

if let Some(prefix) = default_prefix {
return SignerConf::from_env(prefix, None);
}

luketchang marked this conversation as resolved.
Show resolved Hide resolved
None
}
}
Expand Down
11 changes: 8 additions & 3 deletions configuration/src/chains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ impl Default for ChainConf {
}

impl FromEnv for ChainConf {
fn from_env(prefix: &str) -> Option<Self> {
let rpc_style = std::env::var(&format!("{}_RPCSTYLE", prefix)).ok()?;
fn from_env(prefix: &str, default_prefix: Option<&str>) -> Option<Self> {
let mut rpc_style = std::env::var(&format!("{}_RPCSTYLE", prefix)).ok();

if let (None, Some(prefix)) = (&rpc_style, default_prefix) {
rpc_style = std::env::var(&format!("{}_RPCSTYLE", prefix)).ok();
}

let rpc_url = std::env::var(&format!("{}_CONNECTION_URL", prefix)).ok()?;

let json = json!({
"rpcStyle": rpc_style,
"rpcStyle": rpc_style?,
"connection": rpc_url,
});

Expand Down
96 changes: 86 additions & 10 deletions configuration/src/secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,22 @@ impl AgentSecrets {

for network in networks.iter() {
let network_upper = network.to_uppercase();
let chain_conf = ChainConf::from_env(&format!("RPCS_{}", network_upper))?;
let transaction_signer =
SignerConf::from_env(&format!("TRANSACTIONSIGNERS_{}", network_upper))?;

let chain_conf =
ChainConf::from_env(&format!("RPCS_{}", network_upper), Some("RPCS_DEFAULT"))?;

let transaction_signer = SignerConf::from_env(
&format!("TRANSACTIONSIGNERS_{}", network_upper),
Some("TRANSACTIONSIGNERS_DEFAULT"),
)?;
Comment on lines +40 to +46
Copy link
Collaborator

@luketchang luketchang May 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we currently use the default option no matter what, maybe we can have SignerConf::from_env and ChainConf::from_env look for DEFAULT in env vars rather than passing in arg? As in always look first for DEFAULT, if not there then assume network-specific var

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they should be kept prefix-agnostic. You can call SignerConf::from_env a second time with the default prefix but ChainConf::from_env needs to be able to handle two prefixes (*_CONNECTION_URL has no default). Passing prefix and default prefix separately gives FromEnv implementers the required flexibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you're right about this. Smells to me for some reason but also simplifies.

Copy link
Collaborator

@luketchang luketchang May 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other option is to separate prefix and network and have network be an optional param. You can first check DEFAULT_{prefix} then check {network}_{prefix} or {prefix}_{network} this way. Think this may be more intuitive than using a hardcoded static default string as param

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I'm going to stick with the original. I think it's ok for an impl to have hardcoded keys (makes little sense to pass everything in) but the interface should indicate that a default is going to be checked. Passing in only network/prefix and then having it pull a default value is too much magic imo. The alternative would be a check_default param or sth similar but I think an Option<&str> is fine.

Copy link
Contributor

@arnaud036 arnaud036 May 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected precedence from lowest to highest:

  • TRANSACTIONSIGNERS_DEFAULT
  • TRANSACTIONSIGNERS_RINKEBY


secrets.rpcs.insert(network.to_owned(), chain_conf);
secrets
.transaction_signers
.insert(network.to_owned(), transaction_signer);
}

let attestation_signer = SignerConf::from_env("ATTESTATION_SIGNER");
let attestation_signer = SignerConf::from_env("ATTESTATION_SIGNER", None);
secrets.attestation_signer = attestation_signer;

Some(secrets)
Expand Down Expand Up @@ -110,18 +115,89 @@ impl AgentSecrets {
#[cfg(test)]
mod test {
use super::*;
const SECRETS_JSON_PATH: &str = "../fixtures/test_secrets.json";
const SECRETS_ENV_PATH: &str = "../fixtures/env.test";
use crate::ethereum::Connection;
use nomad_test::test_utils;

#[test]
#[serial_test::serial]
fn it_builds_from_env_mixed() {
test_utils::run_test_with_env_sync("../fixtures/env.test-signer-mixed", move || {
let networks = &crate::get_builtin("test").unwrap().networks;
let secrets =
AgentSecrets::from_env(networks).expect("Failed to load secrets from env");

assert_eq!(
*secrets.transaction_signers.get("moonbeam").unwrap(),
SignerConf::Aws {
id: "moonbeam_id".into(),
region: "moonbeam_region".into(),
}
);
assert_eq!(
*secrets.transaction_signers.get("ethereum").unwrap(),
SignerConf::HexKey(
"0x1111111111111111111111111111111111111111111111111111111111111111"
.parse()
.unwrap()
)
);
assert_eq!(
*secrets.transaction_signers.get("evmos").unwrap(),
SignerConf::Aws {
id: "default_id".into(),
region: "default_region".into(),
}
);
assert_eq!(
*secrets.rpcs.get("moonbeam").unwrap(),
ChainConf::Ethereum(Connection::Http("https://rpc.api.moonbeam.network".into()))
);
assert_eq!(
*secrets.rpcs.get("ethereum").unwrap(),
ChainConf::Ethereum(Connection::Http(
"https://main-light.eth.linkpool.io/".into()
))
);
assert_eq!(
*secrets.rpcs.get("evmos").unwrap(),
ChainConf::Ethereum(Connection::Http("https://eth.bd.evmos.org:8545".into()))
);
});
}

#[test]
#[serial_test::serial]
fn it_builds_from_env_default() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a default test case to kathy/src/settings.rs? We've been using kathy settings as place for testing full e2e configuration of agent from env to agent settings block. We'd just need to essentially copy this test except piping in test-signer-default test fixture.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And on that note we need to DRY up agent settings tests... another chore lol

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. Might have some q's

test_utils::run_test_with_env_sync("../fixtures/env.test-signer-default", move || {
let networks = &crate::get_builtin("test").unwrap().networks;
let secrets =
AgentSecrets::from_env(networks).expect("Failed to load secrets from env");

let default_config = SignerConf::Aws {
id: "default_id".into(),
region: "default_region".into(),
};
for (_, config) in &secrets.transaction_signers {
assert_eq!(*config, default_config);
}
for (_, config) in &secrets.rpcs {
assert!(matches!(*config, ChainConf::Ethereum { .. }));
}
});
}

#[test]
#[serial_test::serial]
fn it_builds_from_env() {
let networks = &crate::get_builtin("test").unwrap().networks;
dotenv::from_filename(SECRETS_ENV_PATH).unwrap();
AgentSecrets::from_env(networks).expect("Failed to load secrets from env");
test_utils::run_test_with_env_sync("../fixtures/env.test", move || {
let networks = &crate::get_builtin("test").unwrap().networks;
AgentSecrets::from_env(networks).expect("Failed to load secrets from env");
});
}

#[test]
fn it_builds_from_file() {
AgentSecrets::from_file(SECRETS_JSON_PATH).expect("Failed to load secrets from file");
AgentSecrets::from_file("../fixtures/test_secrets.json")
.expect("Failed to load secrets from file");
}
}
2 changes: 1 addition & 1 deletion configuration/src/traits/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub trait EnvOverridable {
pub trait FromEnv {
/// Optionally load self from env vars. Return None if any necessary env var
/// is missing.
fn from_env(prefix: &str) -> Option<Self>
fn from_env(prefix: &str, default_prefix: Option<&str>) -> Option<Self>
where
Self: Sized;
}
17 changes: 17 additions & 0 deletions fixtures/env.test-signer-default
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Matches configuration/configs/test.json

RUN_ENV=test
AGENT_HOME_NAME=ethereum
AGENT_REPLICAS_ALL=true

RPCS_DEFAULT_RPCSTYLE=ethereum

RPCS_ETHEREUM_CONNECTION_URL=https://main-light.eth.linkpool.io/
RPCS_MOONBEAM_CONNECTION_URL=https://rpc.api.moonbeam.network
RPCS_EVMOS_CONNECTION_URL=https://eth.bd.evmos.org:8545

TRANSACTIONSIGNERS_DEFAULT_REGION=default_region
TRANSACTIONSIGNERS_DEFAULT_ID=default_id

ATTESTATION_SIGNER_ID=dummy_id
ATTESTATION_SIGNER_REGION=dummy_region
23 changes: 23 additions & 0 deletions fixtures/env.test-signer-mixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Matches configuration/configs/test.json

RUN_ENV=test
AGENT_HOME_NAME=ethereum
AGENT_REPLICAS_ALL=true

RPCS_DEFAULT_RPCSTYLE=ethereum
RPCS_EVMOS_RPCSTYLE=ethereum

RPCS_ETHEREUM_CONNECTION_URL=https://main-light.eth.linkpool.io/
RPCS_MOONBEAM_CONNECTION_URL=https://rpc.api.moonbeam.network
RPCS_EVMOS_CONNECTION_URL=https://eth.bd.evmos.org:8545

TRANSACTIONSIGNERS_ETHEREUM_KEY=0x1111111111111111111111111111111111111111111111111111111111111111

TRANSACTIONSIGNERS_MOONBEAM_ID=moonbeam_id
TRANSACTIONSIGNERS_MOONBEAM_REGION=moonbeam_region

TRANSACTIONSIGNERS_DEFAULT_REGION=default_region
TRANSACTIONSIGNERS_DEFAULT_ID=default_id

ATTESTATION_SIGNER_ID=dummy_id
ATTESTATION_SIGNER_REGION=dummy_region
13 changes: 13 additions & 0 deletions nomad-test/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ where
assert!(result.is_ok())
}

pub fn run_test_with_env_sync<T>(path: impl AsRef<Path>, test: T)
where
T: FnOnce() + panic::UnwindSafe,
{
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
dotenv::from_filename(path).unwrap();
test()
}));

clear_env_vars();
assert!(result.is_ok())
}

pub fn clear_env_vars() {
let env_vars = env::vars();
for (key, _) in env_vars.into_iter() {
Expand Down