From f015f8f32abfb2bb68515a2bc2b75149009721b1 Mon Sep 17 00:00:00 2001 From: Olga Kunyavskaya Date: Sun, 9 Oct 2022 01:36:43 +0300 Subject: [PATCH] Eth2Near-relay: fix linear search (#832) Co-authored-by: Kirill --- .../src/last_slot_searcher.rs | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/eth2near/eth2near-block-relay-rs/src/last_slot_searcher.rs b/eth2near/eth2near-block-relay-rs/src/last_slot_searcher.rs index 9d56e4c06..45b8b2637 100644 --- a/eth2near/eth2near-block-relay-rs/src/last_slot_searcher.rs +++ b/eth2near/eth2near-block-relay-rs/src/last_slot_searcher.rs @@ -228,44 +228,50 @@ impl LastSlotSearcher { eth_client_contract: &Box, ) -> Result> { if slot == finalized_slot { - return Ok(self.linear_search_forward( + return self.linear_search_forward( slot, last_eth_slot, beacon_rpc_client, eth_client_contract, - )); + ); } match self.block_known_on_near(slot, beacon_rpc_client, eth_client_contract) { - Ok(true) => Ok(self.linear_search_forward( + Ok(true) => self.linear_search_forward( slot, last_eth_slot, beacon_rpc_client, eth_client_contract, - )), - Ok(false) => Ok(self.linear_search_backward( + ), + Ok(false) => self.linear_search_backward( finalized_slot, slot, beacon_rpc_client, eth_client_contract, - )), + ), Err(err) => match err.downcast_ref::() { Some(_) => { - let left_slot = self.linear_search_forward( - slot, - last_eth_slot, + let (left_slot, slot_on_near) = self.find_left_non_error_slot( + slot + 1, + last_eth_slot + 1, + 1, beacon_rpc_client, eth_client_contract, ); - if left_slot > slot { - Ok(left_slot) - } else { - Ok(self.linear_search_backward( + + match slot_on_near { + true => self.linear_search_forward( + left_slot, + last_eth_slot, + beacon_rpc_client, + eth_client_contract, + ), + false => self.linear_search_backward( finalized_slot, - slot, + left_slot - 1, beacon_rpc_client, eth_client_contract, - )) + ), } } None => Err(err), @@ -285,17 +291,20 @@ impl LastSlotSearcher { max_slot: u64, beacon_rpc_client: &BeaconRPCClient, eth_client_contract: &Box, - ) -> u64 { + ) -> Result> { let mut slot = slot; while slot < max_slot { match self.block_known_on_near(slot + 1, beacon_rpc_client, eth_client_contract) { Ok(true) => slot += 1, Ok(false) => break, - Err(_) => slot += 1, + Err(err) => match err.downcast_ref::() { + Some(_) => slot += 1, + None => return Err(err), + } } } - slot + Ok(slot) } // Returns the slot before the first unknown block on NEAR @@ -310,7 +319,7 @@ impl LastSlotSearcher { last_slot: u64, beacon_rpc_client: &BeaconRPCClient, eth_client_contract: &Box, - ) -> u64 { + ) -> Result> { let mut slot = last_slot; let mut last_false_slot = slot + 1; @@ -321,11 +330,14 @@ impl LastSlotSearcher { last_false_slot = slot; slot -= 1 } - Err(_) => slot -= 1, + Err(err) => match err.downcast_ref::() { + Some(_) => slot -= 1, + None => return Err(err), + } } } - last_false_slot - 1 + Ok(last_false_slot - 1) } // Find the leftmost non-empty slot. Search range: [left_slot, right_slot). @@ -611,7 +623,7 @@ mod tests { finalized_slot + 10, &beacon_rpc_client, ð_client_contract, - ); + ).unwrap(); assert_eq!(last_submitted_block, finalized_slot + 2); send_execution_blocks( @@ -627,7 +639,7 @@ mod tests { config_for_test.right_bound_in_slot_search, &beacon_rpc_client, ð_client_contract, - ); + ).unwrap(); assert_eq!(last_submitted_block, config_for_test.slot_without_block); } @@ -665,7 +677,7 @@ mod tests { config_for_test.right_bound_in_slot_search, &beacon_rpc_client, ð_client_contract, - ); + ).unwrap(); assert_eq!(last_block_on_near, config_for_test.slot_without_block - 2); @@ -685,7 +697,7 @@ mod tests { config_for_test.right_bound_in_slot_search, &beacon_rpc_client, ð_client_contract, - ); + ).unwrap(); assert_eq!(last_block_on_near, config_for_test.slot_without_block); }