From c9f937bd2c31dd80e1c68bcada73a63a5a61238a Mon Sep 17 00:00:00 2001 From: Nick Gheorghita Date: Mon, 9 Sep 2024 14:29:03 -0400 Subject: [PATCH] refactor(bridge): subnetwork configuration changes (#1432) --- portal-bridge/README.md | 18 +++++++++++++----- portal-bridge/src/bridge/state.rs | 2 +- portal-bridge/src/cli.rs | 23 +++++++++++++++++++---- portal-bridge/src/main.rs | 8 -------- portal-bridge/src/types/network.rs | 5 ++++- 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/portal-bridge/README.md b/portal-bridge/README.md index 22f2780d9..bd3f3cc25 100644 --- a/portal-bridge/README.md +++ b/portal-bridge/README.md @@ -25,6 +25,7 @@ To run Portal-Bridge, you must specify what kind of client exists at the provide Current options include `"trin"` / `"fluffy"`. ### Bridge modes +#### History Subnetwork - `"--mode latest"`: follow the head of the chain and gossip latest blocks - `"--mode test:/path/to/test_data.json"`: gossip content keys & values found in test file. - `"--mode backfill:b100"`: start backfill at block #100 @@ -42,12 +43,19 @@ Current options include `"trin"` / `"fluffy"`. - before gossiping a individual piece of content, the bridge will perform a lookup to see if the content is already in the portal network. If it is, the content will not be gossiped. - `"--mode fourfours:single_hunter:10:50`: sample size = 10, threshold = 50 - same as the above hunter mode, but it will only gossip a single era1 file before exiting +#### Beacon Subnetwork +- `"--mode latest"`: follow the head of the chain and gossip latest blocks +- `"--mode test:/path/to/test_data.json"`: gossip content keys & values found in test file. +#### State Subnetwork +- `"--mode single:b100"`: backfill, always beginning from block #0 until the specified block (#100) -### Network -You can specify the `--network` flag for which network to run the bridge for -- `"--network history"`: Default value. Run the bridge for the history network. -- `"--network beacon"`: Run the bridge for the beacon network. -` "--network history,beacon"`: Run the bridge for the history and beacon network. +### Subnetwork configuration +You can specify the `--portal-subnetworks` flag for which network to run the bridge for +- `"--portal-subnetworks history"`: Default value. Run the bridge for the history network. +- `"--portal-subnetworks beacon"`: Run the bridge for the beacon network. +- `"--portal-subnetworks history,beacon"`: Run the bridge for the history & beacon network. +` "--portal-subnetworks state"`: Run the bridge for the state network. + - Currently, the `"state"` network can only be run by itself! ###Test File example ```json diff --git a/portal-bridge/src/bridge/state.rs b/portal-bridge/src/bridge/state.rs index 4535f301f..f0cdf60ad 100644 --- a/portal-bridge/src/bridge/state.rs +++ b/portal-bridge/src/bridge/state.rs @@ -94,7 +94,7 @@ impl StateBridge { .await .expect("State bridge failed"); } - _ => panic!("State bridge only supports State modes."), + _ => panic!("State bridge only supports backfill using 'single' mode to specify the terminating block number."), } info!("Bridge mode: {:?} complete.", self.mode); } diff --git a/portal-bridge/src/cli.rs b/portal-bridge/src/cli.rs index 567be1b9c..b8b55cfd7 100644 --- a/portal-bridge/src/cli.rs +++ b/portal-bridge/src/cli.rs @@ -57,9 +57,9 @@ pub struct BridgeConfig { long = "portal-subnetworks", help = "Comma-separated list of which portal subnetworks to activate", default_value = DEFAULT_SUBNETWORK, - use_value_delimiter = true + value_parser = subnetwork_parser, )] - pub portal_subnetworks: Vec, + pub portal_subnetworks: Arc>, #[arg( long = "network", @@ -189,6 +189,18 @@ pub fn url_to_client(url: Url) -> Result { Ok(client.with(Retry::default())) } +// parser for subnetworks, makes sure that the state network is not ran alongside other subnetworks +fn subnetwork_parser(subnetwork_string: &str) -> Result>, String> { + let active_subnetworks: Vec = subnetwork_string + .split(',') + .map(|subnetwork| NetworkKind::from_str(subnetwork).map_err(|e| e.to_string())) + .collect::, String>>()?; + if active_subnetworks.contains(&NetworkKind::State) && active_subnetworks.len() > 1 { + return Err("The State network doesn't support being ran with other subnetwork bridges at the same time".to_string()); + } + Ok(Arc::new(active_subnetworks)) +} + #[derive(Debug)] pub struct Retry { attempts: u8, @@ -316,7 +328,7 @@ mod test { ); assert_eq!( bridge_config.portal_subnetworks, - vec![NetworkKind::History, NetworkKind::Beacon] + vec![NetworkKind::History, NetworkKind::Beacon].into() ); } @@ -344,7 +356,10 @@ mod test { BridgeMode::Backfill(ModeType::Epoch(100)) ); assert_eq!(bridge_config.epoch_acc_path, PathBuf::from(EPOCH_ACC_PATH)); - assert_eq!(bridge_config.portal_subnetworks, vec![NetworkKind::History]); + assert_eq!( + bridge_config.portal_subnetworks, + vec![NetworkKind::History].into() + ); } #[test] diff --git a/portal-bridge/src/main.rs b/portal-bridge/src/main.rs index 69639d4b2..84f769131 100644 --- a/portal-bridge/src/main.rs +++ b/portal-bridge/src/main.rs @@ -18,14 +18,6 @@ async fn main() -> Result<(), Box> { let bridge_config = BridgeConfig::parse(); - if bridge_config - .portal_subnetworks - .contains(&NetworkKind::State) - && bridge_config.portal_subnetworks.len() > 1 - { - return Err("The State network doesn't support being ran with the other networks bridges at the same time".into()); - } - if let Some(addr) = bridge_config.metrics_url { prometheus_exporter::start(addr)?; } diff --git a/portal-bridge/src/types/network.rs b/portal-bridge/src/types/network.rs index e06ded45d..eaf2b7419 100644 --- a/portal-bridge/src/types/network.rs +++ b/portal-bridge/src/types/network.rs @@ -26,7 +26,10 @@ impl FromStr for NetworkKind { "beacon" => Ok(NetworkKind::Beacon), "history" => Ok(NetworkKind::History), "state" => Ok(NetworkKind::State), - _ => Err("Invalid network arg. Expected either 'beacon', 'history' or 'state'"), + _ => { + let msg = format!("Invalid network arg. Expected either 'beacon', 'history' or 'state' but got '{s}'"); + Err(Box::leak(Box::new(msg))) + } } } }