Skip to content

Commit

Permalink
Eth2Near-relay: fix linear search (#832)
Browse files Browse the repository at this point in the history
Co-authored-by: Kirill <[email protected]>
  • Loading branch information
olga24912 and sept-en authored Oct 8, 2022
1 parent 2bbc8b5 commit f015f8f
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions eth2near/eth2near-block-relay-rs/src/last_slot_searcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,44 +228,50 @@ impl LastSlotSearcher {
eth_client_contract: &Box<dyn EthClientContractTrait>,
) -> Result<u64, Box<dyn Error>> {
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::<NoBlockForSlotError>() {
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),
Expand All @@ -285,17 +291,20 @@ impl LastSlotSearcher {
max_slot: u64,
beacon_rpc_client: &BeaconRPCClient,
eth_client_contract: &Box<dyn EthClientContractTrait>,
) -> u64 {
) -> Result<u64, Box<dyn Error>> {
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::<NoBlockForSlotError>() {
Some(_) => slot += 1,
None => return Err(err),
}
}
}

slot
Ok(slot)
}

// Returns the slot before the first unknown block on NEAR
Expand All @@ -310,7 +319,7 @@ impl LastSlotSearcher {
last_slot: u64,
beacon_rpc_client: &BeaconRPCClient,
eth_client_contract: &Box<dyn EthClientContractTrait>,
) -> u64 {
) -> Result<u64, Box<dyn Error>> {
let mut slot = last_slot;
let mut last_false_slot = slot + 1;

Expand All @@ -321,11 +330,14 @@ impl LastSlotSearcher {
last_false_slot = slot;
slot -= 1
}
Err(_) => slot -= 1,
Err(err) => match err.downcast_ref::<NoBlockForSlotError>() {
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).
Expand Down Expand Up @@ -611,7 +623,7 @@ mod tests {
finalized_slot + 10,
&beacon_rpc_client,
&eth_client_contract,
);
).unwrap();
assert_eq!(last_submitted_block, finalized_slot + 2);

send_execution_blocks(
Expand All @@ -627,7 +639,7 @@ mod tests {
config_for_test.right_bound_in_slot_search,
&beacon_rpc_client,
&eth_client_contract,
);
).unwrap();
assert_eq!(last_submitted_block, config_for_test.slot_without_block);
}

Expand Down Expand Up @@ -665,7 +677,7 @@ mod tests {
config_for_test.right_bound_in_slot_search,
&beacon_rpc_client,
&eth_client_contract,
);
).unwrap();

assert_eq!(last_block_on_near, config_for_test.slot_without_block - 2);

Expand All @@ -685,7 +697,7 @@ mod tests {
config_for_test.right_bound_in_slot_search,
&beacon_rpc_client,
&eth_client_contract,
);
).unwrap();

assert_eq!(last_block_on_near, config_for_test.slot_without_block);
}
Expand Down

0 comments on commit f015f8f

Please sign in to comment.