-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description Previously, the scanning threads had some validation for the `start_block` and `end_block` that was incorrect. This PR introduces validation that does the following: - We now allow `start_block` to be omitted by the user and we default to 0 - If there are no blocks in the database, we abort the scan and go to streaming mode rather than erroring (fixes #477) - If the user provides an `end_block`, we validate that it is greater than the `start_block` - If the `start_block` is greater than chain tip, we abort the scan and go to streaming mode rather than erroring (fixes #464) This PR also adds some validation to the `BlockHeights` class. Previously, it was possible to overload the `BlockHeights::BlockRange(start_block, end_block)` function to allocate a lot of memory into an empty array. We now have limits on this class. However, due to the above validation, it should not be possible to pass through parameters that reach theses limits (with the current usage of the class) until a chain height is up to `1_000_000`. --- ### Checklist - [x] All tests pass - [x] Tests added in this PR (if applicable)
- Loading branch information
1 parent
abe0fd5
commit e70025b
Showing
11 changed files
with
475 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use crate::service::ScanningData; | ||
use chainhook_sdk::utils::{BlockHeights, BlockHeightsError}; | ||
use std::collections::VecDeque; | ||
|
||
pub fn get_block_heights_to_scan( | ||
blocks: &Option<Vec<u64>>, | ||
start_block: &Option<u64>, | ||
end_block: &Option<u64>, | ||
chain_tip: &u64, | ||
unfinished_scan_data: &Option<ScanningData>, | ||
) -> Result<Option<VecDeque<u64>>, String> { | ||
let block_heights_to_scan = if let Some(ref blocks) = blocks { | ||
match BlockHeights::Blocks(blocks.clone()).get_sorted_entries() { | ||
Ok(heights) => Some(heights), | ||
Err(e) => match e { | ||
BlockHeightsError::ExceedsMaxEntries(max, specified) => { | ||
return Err(format!("Chainhook specification exceeds max number of blocks to scan. Maximum: {}, Attempted: {}", max, specified)); | ||
} | ||
BlockHeightsError::StartLargerThanEnd => { | ||
// this code path should not be reachable | ||
return Err( | ||
"Chainhook specification field `end_block` should be greater than `start_block`." | ||
.into(), | ||
); | ||
} | ||
}, | ||
} | ||
} else { | ||
let start_block = match &unfinished_scan_data { | ||
Some(scan_data) => scan_data.last_evaluated_block_height, | ||
None => start_block.unwrap_or(0), | ||
}; | ||
|
||
let end_block = if let Some(end_block) = end_block { | ||
if &start_block > end_block { | ||
return Err( | ||
"Chainhook specification field `end_block` should be greater than `start_block`." | ||
.into(), | ||
); | ||
} | ||
end_block | ||
} else { | ||
chain_tip | ||
}; | ||
if &start_block > end_block { | ||
return Ok(None); | ||
} | ||
let block_heights_to_scan = match BlockHeights::BlockRange(start_block, *end_block) | ||
.get_sorted_entries() | ||
{ | ||
Ok(heights) => heights, | ||
Err(e) => match e { | ||
BlockHeightsError::ExceedsMaxEntries(max, specified) => { | ||
return Err(format!("Chainhook specification exceeds max number of blocks to scan. Maximum: {}, Attempted: {}", max, specified)); | ||
} | ||
BlockHeightsError::StartLargerThanEnd => { | ||
return Err( | ||
"Chainhook specification field `end_block` should be greater than `start_block`." | ||
.into(), | ||
); | ||
} | ||
}, | ||
}; | ||
Some(block_heights_to_scan) | ||
}; | ||
Ok(block_heights_to_scan) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
pub mod bitcoin; | ||
pub mod common; | ||
pub mod stacks; | ||
|
||
#[cfg(test)] | ||
pub mod tests; |
Oops, something went wrong.