Skip to content

Commit

Permalink
refactor(bridge): subnetwork configuration changes (#1432)
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita authored Sep 9, 2024
1 parent d83cbe7 commit c9f937b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
18 changes: 13 additions & 5 deletions portal-bridge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion portal-bridge/src/bridge/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
23 changes: 19 additions & 4 deletions portal-bridge/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NetworkKind>,
pub portal_subnetworks: Arc<Vec<NetworkKind>>,

#[arg(
long = "network",
Expand Down Expand Up @@ -189,6 +189,18 @@ pub fn url_to_client(url: Url) -> Result<Client, String> {
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<Arc<Vec<NetworkKind>>, String> {
let active_subnetworks: Vec<NetworkKind> = subnetwork_string
.split(',')
.map(|subnetwork| NetworkKind::from_str(subnetwork).map_err(|e| e.to_string()))
.collect::<Result<Vec<NetworkKind>, 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,
Expand Down Expand Up @@ -316,7 +328,7 @@ mod test {
);
assert_eq!(
bridge_config.portal_subnetworks,
vec![NetworkKind::History, NetworkKind::Beacon]
vec![NetworkKind::History, NetworkKind::Beacon].into()
);
}

Expand Down Expand Up @@ -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]
Expand Down
8 changes: 0 additions & 8 deletions portal-bridge/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

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)?;
}
Expand Down
5 changes: 4 additions & 1 deletion portal-bridge/src/types/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}
}
}

0 comments on commit c9f937b

Please sign in to comment.