mirror of
https://github.com/Qortal/piratewallet-light-cli.git
synced 2025-01-30 18:42:15 +00:00
Add checkpoints
This commit is contained in:
parent
66da04b40b
commit
cb5c55a829
@ -25,6 +25,8 @@ use crate::grpcconnector::{self, *};
|
||||
use crate::SaplingParams;
|
||||
use crate::ANCHOR_OFFSET;
|
||||
|
||||
mod checkpoints;
|
||||
|
||||
pub const DEFAULT_SERVER: &str = "https://lightd-main.zecwallet.co:443";
|
||||
pub const WALLET_NAME: &str = "zecwallet-light-wallet.dat";
|
||||
pub const LOGFILE_NAME: &str = "zecwallet-light-wallet.debug.log";
|
||||
@ -117,18 +119,8 @@ impl LightClientConfig {
|
||||
log_path.into_boxed_path()
|
||||
}
|
||||
|
||||
pub fn get_initial_state(&self) -> Option<(u64, &str, &str)> {
|
||||
match &self.chain_name[..] {
|
||||
"test" => Some((600000,
|
||||
"0107385846c7451480912c294b6ce1ee1feba6c2619079fd9104f6e71e4d8fe7",
|
||||
"01690698411e3f8badea7da885e556d7aba365a797e9b20b44ac0946dced14b23c001001ab2a18a5a86aa5d77e43b69071b21770b6fe6b3c26304dcaf7f96c0bb3fed74d000186482712fa0f2e5aa2f2700c4ed49ef360820f323d34e2b447b78df5ec4dfa0401a332e89a21afb073cb1db7d6f07396b56a95e97454b9bca5a63d0ebc575d3a33000000000001c9d3564eff54ebc328eab2e4f1150c3637f4f47516f879a0cfebdf49fe7b1d5201c104705fac60a85596010e41260d07f3a64f38f37a112eaef41cd9d736edc5270145e3d4899fcd7f0f1236ae31eafb3f4b65ad6b11a17eae1729cec09bd3afa01a000000011f8322ef806eb2430dc4a7a41c1b344bea5be946efc7b4349c1c9edb14ff9d39"
|
||||
)),
|
||||
"main" => Some((610000,
|
||||
"000000000218882f481e3b49ca3df819734b8d74aac91f69e848d7499b34b472",
|
||||
"0192943f1eca6525cea7ea8e26b37c792593ed50cfe2be7a1ff551a08dc64b812f001000000001deef7ae5162a9942b4b9aa797137c5bdf60750e9548664127df99d1981dda66901747ad24d5daf294ce2a27aba923e16e52e7348eea3048c5b5654b99ab0a371200149d8aff830305beb3887529f6deb150ab012916c3ce88a6b47b78228f8bfeb3f01ff84a89890cfae65e0852bc44d9aa82be2c5d204f5aebf681c9e966aa46f540e000001d58f1dfaa9db0996996129f8c474acb813bfed452d347fb17ebac2e775e209120000000001319312241b0031e3a255b0d708750b4cb3f3fe79e3503fe488cc8db1dd00753801754bb593ea42d231a7ddf367640f09bbf59dc00f2c1d2003cc340e0c016b5b13"
|
||||
)),
|
||||
_ => None
|
||||
}
|
||||
pub fn get_initial_state(&self, height: u64) -> Option<(u64, &str, &str)> {
|
||||
checkpoints::get_closest_checkpoint(&self.chain_name, height)
|
||||
}
|
||||
|
||||
pub fn get_server_or_default(server: Option<String>) -> http::Uri {
|
||||
@ -213,10 +205,10 @@ pub struct LightClient {
|
||||
|
||||
impl LightClient {
|
||||
|
||||
pub fn set_wallet_initial_state(&self) {
|
||||
pub fn set_wallet_initial_state(&self, height: u64) {
|
||||
use std::convert::TryInto;
|
||||
|
||||
let state = self.config.get_initial_state();
|
||||
let state = self.config.get_initial_state(height);
|
||||
|
||||
match state {
|
||||
Some((height, hash, tree)) => self.wallet.read().unwrap().set_initial_block(height.try_into().unwrap(), hash, tree),
|
||||
@ -242,7 +234,7 @@ impl LightClient {
|
||||
sapling_spend : vec![]
|
||||
};
|
||||
|
||||
l.set_wallet_initial_state();
|
||||
l.set_wallet_initial_state(0);
|
||||
l.read_sapling_params();
|
||||
|
||||
info!("Created new wallet!");
|
||||
@ -266,7 +258,7 @@ impl LightClient {
|
||||
sapling_spend : vec![]
|
||||
};
|
||||
|
||||
l.set_wallet_initial_state();
|
||||
l.set_wallet_initial_state(latest_block);
|
||||
l.read_sapling_params();
|
||||
|
||||
info!("Created new wallet with a new seed!");
|
||||
@ -288,7 +280,7 @@ impl LightClient {
|
||||
sapling_spend : vec![]
|
||||
};
|
||||
|
||||
l.set_wallet_initial_state();
|
||||
l.set_wallet_initial_state(latest_block);
|
||||
l.read_sapling_params();
|
||||
|
||||
info!("Created new wallet!");
|
||||
@ -727,7 +719,7 @@ impl LightClient {
|
||||
self.wallet.read().unwrap().clear_blocks();
|
||||
|
||||
// Then set the initial block
|
||||
self.set_wallet_initial_state();
|
||||
self.set_wallet_initial_state(self.wallet.read().unwrap().get_birthday());
|
||||
|
||||
// Then, do a sync, which will force a full rescan from the initial state
|
||||
let response = self.do_sync(true);
|
||||
|
85
lib/src/lightclient/checkpoints.rs
Normal file
85
lib/src/lightclient/checkpoints.rs
Normal file
@ -0,0 +1,85 @@
|
||||
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![
|
||||
(600000, "0107385846c7451480912c294b6ce1ee1feba6c2619079fd9104f6e71e4d8fe7",
|
||||
"01690698411e3f8badea7da885e556d7aba365a797e9b20b44ac0946dced14b23c001001ab2a18a5a86aa5d77e43b69071b21770b6fe6b3c26304dcaf7f96c0bb3fed74d000186482712fa0f2e5aa2f2700c4ed49ef360820f323d34e2b447b78df5ec4dfa0401a332e89a21afb073cb1db7d6f07396b56a95e97454b9bca5a63d0ebc575d3a33000000000001c9d3564eff54ebc328eab2e4f1150c3637f4f47516f879a0cfebdf49fe7b1d5201c104705fac60a85596010e41260d07f3a64f38f37a112eaef41cd9d736edc5270145e3d4899fcd7f0f1236ae31eafb3f4b65ad6b11a17eae1729cec09bd3afa01a000000011f8322ef806eb2430dc4a7a41c1b344bea5be946efc7b4349c1c9edb14ff9d39"
|
||||
)
|
||||
];
|
||||
|
||||
find_checkpoint(height, checkpoints)
|
||||
}
|
||||
|
||||
|
||||
fn get_main_checkpoint(height: u64) -> Option<(u64, &'static str, &'static str)> {
|
||||
let checkpoints: Vec<(u64, &str, &str)> = vec![
|
||||
(610000, "000000000218882f481e3b49ca3df819734b8d74aac91f69e848d7499b34b472",
|
||||
"0192943f1eca6525cea7ea8e26b37c792593ed50cfe2be7a1ff551a08dc64b812f001000000001deef7ae5162a9942b4b9aa797137c5bdf60750e9548664127df99d1981dda66901747ad24d5daf294ce2a27aba923e16e52e7348eea3048c5b5654b99ab0a371200149d8aff830305beb3887529f6deb150ab012916c3ce88a6b47b78228f8bfeb3f01ff84a89890cfae65e0852bc44d9aa82be2c5d204f5aebf681c9e966aa46f540e000001d58f1dfaa9db0996996129f8c474acb813bfed452d347fb17ebac2e775e209120000000001319312241b0031e3a255b0d708750b4cb3f3fe79e3503fe488cc8db1dd00753801754bb593ea42d231a7ddf367640f09bbf59dc00f2c1d2003cc340e0c016b5b13"
|
||||
)
|
||||
];
|
||||
|
||||
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(500000), None);
|
||||
assert_eq!(get_test_checkpoint(600000).unwrap().0, 600000);
|
||||
assert_eq!(get_test_checkpoint(625000).unwrap().0, 600000);
|
||||
|
||||
assert_eq!(get_main_checkpoint(500000), None);
|
||||
assert_eq!(get_main_checkpoint(610000).unwrap().0, 610000);
|
||||
assert_eq!(get_main_checkpoint(625000).unwrap().0, 610000);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user