Skip to content

Commit

Permalink
feat(bridge): cli flag for random era1 file with floor (#1245)
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita authored Apr 8, 2024
1 parent fb11990 commit 50e254a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions portal-bridge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Current options include `"trin"` / `"fluffy"`.
- `"--mode single:e100"`: gossip a single epoch #100
- `"--mode fourfours`: will randomly select era1 files from `era1.ethportal.net` and gossip them
- `"--mode fourfours:random_epoch"`: will randomly select a single era1 file from `era1.ethportal.net` and then gossip it
- `"--mode fourfours:random_epoch:100"`: will randomly select a single era1 file from `era1.ethportal.net` that represents an epoch number greater than the floor provided and then gossip it
- `"--mode fourfours:e600`: will select era1 file 600 from `era1.ethportal.net` and gossip it
- `"--mode fourfours:r100-200`: will gossip a block range from an era1 file, range must be from the same epoch

Expand Down
50 changes: 37 additions & 13 deletions portal-bridge/src/bridge/era1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,13 @@ impl Era1Bridge {
info!("Launching era1 bridge: {:?}", self.mode);
match self.mode.clone() {
BridgeMode::FourFours(FourFoursMode::Random) => self.launch_random().await,
BridgeMode::FourFours(FourFoursMode::RandomSingle) => self.launch_single(None).await,
BridgeMode::FourFours(FourFoursMode::Single(epoch)) => {
self.launch_single(Some(epoch)).await
BridgeMode::FourFours(FourFoursMode::RandomSingle) => {
self.launch_random_single(None).await
}
BridgeMode::FourFours(FourFoursMode::RandomSingleWithFloor(floor)) => {
self.launch_random_single(Some(floor)).await
}
BridgeMode::FourFours(FourFoursMode::Single(epoch)) => self.launch_single(epoch).await,
BridgeMode::FourFours(FourFoursMode::Range(start, end)) => {
self.launch_range(start, end).await;
}
Expand All @@ -93,26 +96,47 @@ impl Era1Bridge {
info!("Bridge mode: {:?} complete.", self.mode);
}

pub async fn launch_random(&self) {
async fn launch_random(&self) {
for era1_path in self.era1_files.clone().into_iter() {
self.gossip_era1(era1_path, None).await;
}
}

pub async fn launch_single(&self, epoch: Option<u64>) {
async fn launch_single(&self, epoch: u64) {
let era1_path = self
.era1_files
.clone()
.into_iter()
.find(|file| file.contains(&format!("mainnet-{epoch:05}-")))
.expect("to be able to find era1 file");
self.gossip_era1(era1_path, None).await;
}

async fn launch_random_single(&self, floor: Option<u64>) {
let mut era1_files = self.era1_files.clone().into_iter();
let era1_path = match epoch {
Some(epoch) => era1_files
.find(|file| file.contains(&format!("mainnet-{epoch:05}-")))
.expect("to be able to find era1 file for requested epoch"),
None => era1_files
let era1_path = loop {
let era1_file = era1_files
.next()
.expect("to be able to get first era1 file"),
.expect("to be able to get first era1 file");
let epoch = era1_file
.split('-')
.nth(1)
.expect("to be able to get epoch from era1 file")
.parse::<u64>()
.expect("to be able to parse epoch from era1 file");
match floor {
Some(floor) => {
if epoch >= floor {
break era1_file;
}
}
None => break era1_file,
}
};
self.gossip_era1(era1_path, None).await;
}

pub async fn launch_range(&self, start: u64, end: u64) {
async fn launch_range(&self, start: u64, end: u64) {
let epoch = start / EPOCH_SIZE as u64;
let era1_path =
self.era1_files.clone().into_iter().find(|file| {
Expand All @@ -125,7 +149,7 @@ impl Era1Bridge {
}
}

pub async fn gossip_era1(&self, era1_path: String, gossip_range: Option<Range<u64>>) {
async fn gossip_era1(&self, era1_path: String, gossip_range: Option<Range<u64>>) {
info!("Processing era1 file at path: {era1_path:?}");
// We are using a semaphore to limit the amount of active gossip transfers to make sure
// we don't overwhelm the trin client
Expand Down
26 changes: 24 additions & 2 deletions portal-bridge/src/types/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ pub enum FourFoursMode {
Random,
// Gossips a single, randomly selected epoch
RandomSingle,
// Gossips a single, randomly selected epoch, with a floor epoch
RandomSingleWithFloor(u64),
// Gossips the single epoch given
Single(u64),
// Gossips a block range from within a single epoch
Expand All @@ -183,8 +185,19 @@ impl FromStr for FourFoursMode {
if s.is_empty() {
return Err("Invalid bridge fourfours mode arg: empty string".to_string());
}
if s == RANDOM_SINGLE_MODE {
return Ok(FourFoursMode::RandomSingle);
if s.starts_with(RANDOM_SINGLE_MODE) {
match s.split(':').nth(1) {
Some(floor_epoch) => {
let floor_epoch = floor_epoch
.parse()
.map_err(|_| "Invalid 4444s bridge mode arg: floor epoch number")?;
if floor_epoch > 1896 {
return Err(format!("Invalid 4444s bridge mode arg: era1 epoch greater than 1896 was given: {floor_epoch}"));
}
return Ok(FourFoursMode::RandomSingleWithFloor(floor_epoch));
}
None => return Ok(FourFoursMode::RandomSingle),
}
}
match &s[..1] {
"e" => {
Expand Down Expand Up @@ -257,6 +270,12 @@ mod test {
#[case(format!("fourfours:{RANDOM_SINGLE_MODE}"),
BridgeMode::FourFours(FourFoursMode::RandomSingle)
)]
#[case(format!("fourfours:{RANDOM_SINGLE_MODE}:0"),
BridgeMode::FourFours(FourFoursMode::RandomSingleWithFloor(0))
)]
#[case(format!("fourfours:{RANDOM_SINGLE_MODE}:1500"),
BridgeMode::FourFours(FourFoursMode::RandomSingleWithFloor(1500))
)]
#[case("fourfours:e1", BridgeMode::FourFours(FourFoursMode::Single(1)))]
#[case("fourfours:r1-10", BridgeMode::FourFours(FourFoursMode::Range(1, 10)))]
#[case(
Expand Down Expand Up @@ -288,6 +307,9 @@ mod test {
#[case("fourfours:r1-10000")]
// range is post-merge
#[case("fourfours:r19000000-19000100")]
#[case("fourfours:random_epoch:")]
// epoch is post-merge
#[case("fourfours:random_epoch:1900")]
fn test_invalid_mode_flag(#[case] actual: String) {
let bridge_mode = BridgeMode::from_str(&actual);
assert!(bridge_mode.is_err());
Expand Down
2 changes: 1 addition & 1 deletion portalnet/src/overlay/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3144,7 +3144,7 @@ mod tests {

let (_, enr1) = generate_random_remote_enr();
let (_, enr2) = generate_random_remote_enr();
let peers = vec![enr1, enr2];
let peers = [enr1, enr2];
let peer_node_ids: Vec<NodeId> = peers.iter().map(|p| p.node_id()).collect();

// No nodes in the routing table, so no commands should be in the channel.
Expand Down
1 change: 1 addition & 0 deletions src/bin/historical_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async fn main() -> anyhow::Result<()> {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(file_path.join("era_urls.txt"))
.unwrap();

Expand Down

0 comments on commit 50e254a

Please sign in to comment.