From da03ab795035c0d1e6abd37a291a22e51391c8fd Mon Sep 17 00:00:00 2001 From: Dzejkop Date: Wed, 13 Dec 2023 01:30:15 +0100 Subject: [PATCH] Only allow single network and relayer --- config.toml | 4 +-- src/config.rs | 18 +++++++------ src/service.rs | 62 +++++++++++++++++++++------------------------ tests/common/mod.rs | 30 ++++++++++++---------- 4 files changed, 57 insertions(+), 57 deletions(-) diff --git a/config.toml b/config.toml index d331092..cc2e14e 100644 --- a/config.toml +++ b/config.toml @@ -3,12 +3,12 @@ escalation_interval = "1m" datadog_enabled = false statsd_enabled = false -[[predefined_networks]] +[predefined.network] chain_id = 31337 http_url = "http://127.0.0.1:8545" ws_url = "ws://127.0.0.1:8545" -[[predefined_relayers]] +[predefined.relayer] id = "1b908a34-5dc1-4d2d-a146-5eb46e975830" chain_id = 31337 key_id = "d10607662a85424f02a33fb1e6d095bd0ac7154396ff09762e41f82ff2233aaa" diff --git a/src/config.rs b/src/config.rs index f2748ea..7e1e534 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,11 +24,15 @@ pub struct TxSitterConfig { #[serde(default)] pub statsd_enabled: bool, - #[serde(default, skip_serializing_if = "Vec::is_empty")] - pub predefined_networks: Vec, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub predefined: Option, +} - #[serde(default, skip_serializing_if = "Vec::is_empty")] - pub predefined_relayers: Vec, +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub struct Predefined { + pub network: PredefinedNetwork, + pub relayer: PredefinedRelayer, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -186,8 +190,7 @@ mod tests { escalation_interval: Duration::from_secs(60 * 60), datadog_enabled: false, statsd_enabled: false, - predefined_networks: vec![], - predefined_relayers: vec![], + predefined: None, }, server: ServerConfig { host: SocketAddr::from(([127, 0, 0, 1], 3000)), @@ -213,8 +216,7 @@ mod tests { escalation_interval: Duration::from_secs(60 * 60), datadog_enabled: false, statsd_enabled: false, - predefined_networks: vec![], - predefined_relayers: vec![], + predefined: None, }, server: ServerConfig { host: SocketAddr::from(([127, 0, 0, 1], 3000)), diff --git a/src/service.rs b/src/service.rs index 4cc35c8..a14598d 100644 --- a/src/service.rs +++ b/src/service.rs @@ -86,42 +86,38 @@ impl Service { async fn initialize_predefined_values( app: &Arc, ) -> Result<(), eyre::Error> { - if !app.config.service.predefined_relayers.is_empty() - && !app.config.keys.is_local() - { + if app.config.service.predefined.is_some() && !app.config.keys.is_local() { eyre::bail!("Predefined relayers are only supported with local keys"); } - for predefined_network in &app.config.service.predefined_networks { - app.db - .create_network( - predefined_network.chain_id, - &predefined_network.name, - &predefined_network.http_rpc, - &predefined_network.ws_rpc, - ) - .await?; - - let task_runner = TaskRunner::new(app.clone()); - Service::spawn_chain_tasks(&task_runner, predefined_network.chain_id)?; - } - - for predefined_relayer in &app.config.service.predefined_relayers { - let secret_key = signing_key_from_hex(&predefined_relayer.key_id)?; - - let signer = Wallet::from(secret_key); - let address = signer.address(); - - app.db - .create_relayer( - &predefined_relayer.id, - &predefined_relayer.name, - predefined_relayer.chain_id, - &predefined_relayer.key_id, - address, - ) - .await?; - } + let predefined = app.config.service.predefined.as_ref().unwrap(); + + app.db + .create_network( + predefined.network.chain_id, + &predefined.network.name, + &predefined.network.http_rpc, + &predefined.network.ws_rpc, + ) + .await?; + + let task_runner = TaskRunner::new(app.clone()); + Service::spawn_chain_tasks(&task_runner, predefined.network.chain_id)?; + + let secret_key = signing_key_from_hex(&predefined.relayer.key_id)?; + + let signer = Wallet::from(secret_key); + let address = signer.address(); + + app.db + .create_relayer( + &predefined.relayer.id, + &predefined.relayer.name, + predefined.relayer.chain_id, + &predefined.relayer.key_id, + address, + ) + .await?; Ok(()) } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index ca338e5..033ea32 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -18,8 +18,8 @@ use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::EnvFilter; use tx_sitter::client::TxSitterClient; use tx_sitter::config::{ - Config, DatabaseConfig, KeysConfig, LocalKeysConfig, PredefinedNetwork, - PredefinedRelayer, ServerConfig, TxSitterConfig, + Config, DatabaseConfig, KeysConfig, LocalKeysConfig, Predefined, + PredefinedNetwork, PredefinedRelayer, ServerConfig, TxSitterConfig, }; use tx_sitter::service::Service; @@ -150,18 +150,20 @@ pub async fn setup_service( escalation_interval, datadog_enabled: false, statsd_enabled: false, - predefined_networks: vec![PredefinedNetwork { - chain_id: DEFAULT_ANVIL_CHAIN_ID, - name: "Anvil".to_string(), - http_rpc: format!("http://{}", rpc_url), - ws_rpc: anvil_handle.ws_addr(), - }], - predefined_relayers: vec![PredefinedRelayer { - name: "Anvil".to_string(), - id: DEFAULT_RELAYER_ID.to_string(), - key_id: anvil_private_key, - chain_id: DEFAULT_ANVIL_CHAIN_ID, - }], + predefined: Some(Predefined { + network: PredefinedNetwork { + chain_id: DEFAULT_ANVIL_CHAIN_ID, + name: "Anvil".to_string(), + http_rpc: format!("http://{}", rpc_url), + ws_rpc: anvil_handle.ws_addr(), + }, + relayer: PredefinedRelayer { + name: "Anvil".to_string(), + id: DEFAULT_RELAYER_ID.to_string(), + key_id: anvil_private_key, + chain_id: DEFAULT_ANVIL_CHAIN_ID, + }, + }), }, server: ServerConfig { host: SocketAddr::V4(SocketAddrV4::new(