merged manually
This commit is contained in:
90
lib/src/lightclient/checkpoints.rs
Normal file
90
lib/src/lightclient/checkpoints.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
pub fn get_closest_checkpoint(chain_name: &str, height: u64) -> Option<(u64, &'static str, &'static str)> {
|
||||
match chain_name {
|
||||
"test" => get_test_checkpoint(height),
|
||||
"main" => get_main_checkpoint(height),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
fn get_test_checkpoint(height: u64) -> Option<(u64, &'static str, &'static str)> {
|
||||
let checkpoints: Vec<(u64, &str, &str)> = vec![
|
||||
(105942, "",
|
||||
""
|
||||
),
|
||||
(105943, "",
|
||||
""
|
||||
)
|
||||
];
|
||||
|
||||
find_checkpoint(height, checkpoints)
|
||||
}
|
||||
|
||||
|
||||
fn get_main_checkpoint(height: u64) -> Option<(u64, &'static str, &'static str)> {
|
||||
let checkpoints: Vec<(u64, &str, &str)> = vec![
|
||||
(105942, "00000001c0199f329ee03379bf1387856dbab23765da508bf9b9d8d544f212c0",
|
||||
""
|
||||
)
|
||||
];
|
||||
|
||||
find_checkpoint(height, checkpoints)
|
||||
}
|
||||
|
||||
fn find_checkpoint(height: u64, chkpts: Vec<(u64, &'static str, &'static str)>) -> Option<(u64, &'static str, &'static str)> {
|
||||
// Find the closest checkpoint
|
||||
let mut heights = chkpts.iter().map(|(h, _, _)| *h as u64).collect::<Vec<_>>();
|
||||
heights.sort();
|
||||
|
||||
match get_first_lower_than(height, heights) {
|
||||
Some(closest_height) => {
|
||||
chkpts.iter().find(|(h, _, _)| *h == closest_height).map(|t| *t)
|
||||
},
|
||||
None => None
|
||||
}
|
||||
}
|
||||
|
||||
fn get_first_lower_than(height: u64, heights: Vec<u64>) -> Option<u64> {
|
||||
// If it's before the first checkpoint, return None.
|
||||
if heights.len() == 0 || height < heights[0] {
|
||||
return None;
|
||||
}
|
||||
|
||||
for (i, h) in heights.iter().enumerate() {
|
||||
if height < *h {
|
||||
return Some(heights[i-1]);
|
||||
}
|
||||
}
|
||||
|
||||
return Some(*heights.last().unwrap());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_lower_than() {
|
||||
assert_eq!(get_first_lower_than( 9, vec![10, 30, 40]), None);
|
||||
assert_eq!(get_first_lower_than(10, vec![10, 30, 40]).unwrap(), 10);
|
||||
assert_eq!(get_first_lower_than(11, vec![10, 30, 40]).unwrap(), 10);
|
||||
assert_eq!(get_first_lower_than(29, vec![10, 30, 40]).unwrap(), 10);
|
||||
assert_eq!(get_first_lower_than(30, vec![10, 30, 40]).unwrap(), 30);
|
||||
assert_eq!(get_first_lower_than(40, vec![10, 30, 40]).unwrap(), 40);
|
||||
assert_eq!(get_first_lower_than(41, vec![10, 30, 40]).unwrap(), 40);
|
||||
assert_eq!(get_first_lower_than(99, vec![10, 30, 40]).unwrap(), 40);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_checkpoints() {
|
||||
assert_eq!(get_test_checkpoint(990000), None);
|
||||
assert_eq!(get_test_checkpoint(100000).unwrap().0, 100000);
|
||||
assert_eq!(get_test_checkpoint(110000).unwrap().0, 100000);
|
||||
assert_eq!(get_test_checkpoint(111000).unwrap().0, 1100000);
|
||||
assert_eq!(get_test_checkpoint(112000).unwrap().0, 1100000);
|
||||
|
||||
assert_eq!(get_main_checkpoint(990000), None);
|
||||
assert_eq!(get_main_checkpoint(110000).unwrap().0, 110000);
|
||||
assert_eq!(get_main_checkpoint(111000).unwrap().0, 110000);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user