Skip to content

Commit

Permalink
fix for the code rabbit
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhhuang committed Feb 20, 2025
1 parent 38afafb commit be2fdc2
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions chain/api/src/range_locate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use starcoin_storage::Store;

use crate::ChainReader;

const MAX_RANGE_POWER: u32 = 17; // 2^17 = 131,072 blocks

#[derive(Debug)]
pub enum FindCommonHeader {
AllInRange, // all in range
Expand Down Expand Up @@ -106,6 +108,9 @@ pub fn get_range_in_location(

let end_number = if let Some(end_id) = end_id {
if let Some(end_block_header) = storage.get_block_header_by_hash(end_id)? {
if end_block_header.number() < start_block_header.number() {
return Ok(RangeInLocation::NotInSelectedChain);
}
end_block_header.number()
} else {
chain.current_header().number()
Expand All @@ -114,8 +119,14 @@ pub fn get_range_in_location(
chain.current_header().number()
};

for index in 0..=17 {
let block_number = start_block_header.number().saturating_add(2u64.pow(index));
for index in 0..=MAX_RANGE_POWER {
let step = 2u64
.checked_pow(index)
.ok_or_else(|| anyhow::format_err!("Block number step calculation overflow"))?;
let block_number = start_block_header
.number()
.checked_add(step)
.ok_or_else(|| anyhow::format_err!("Block number calculation overflow"))?;
if block_number > chain.current_header().number() {
break;
}
Expand Down

0 comments on commit be2fdc2

Please sign in to comment.