Skip to content

Commit

Permalink
Only allow single network and relayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Dec 13, 2023
1 parent 6abc623 commit da03ab7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 57 deletions.
4 changes: 2 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 10 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PredefinedNetwork>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub predefined: Option<Predefined>,
}

#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub predefined_relayers: Vec<PredefinedRelayer>,
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct Predefined {
pub network: PredefinedNetwork,
pub relayer: PredefinedRelayer,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -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)),
Expand All @@ -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)),
Expand Down
62 changes: 29 additions & 33 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,42 +86,38 @@ impl Service {
async fn initialize_predefined_values(
app: &Arc<App>,
) -> 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(())
}
30 changes: 16 additions & 14 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit da03ab7

Please sign in to comment.