merged manually
This commit is contained in:
@@ -3,7 +3,7 @@ use crate::lightwallet::LightWallet;
|
||||
use log::{info, warn, error};
|
||||
use rand::{rngs::OsRng, seq::SliceRandom};
|
||||
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::sync::{Arc, RwLock, Mutex};
|
||||
use std::sync::atomic::{AtomicU64, AtomicI32, AtomicUsize, Ordering};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::fs::File;
|
||||
@@ -25,6 +25,8 @@ use crate::grpcconnector::{self, *};
|
||||
use crate::SaplingParams;
|
||||
use crate::ANCHOR_OFFSET;
|
||||
|
||||
mod checkpoints;
|
||||
|
||||
pub const DEFAULT_SERVER: &str = "https://";
|
||||
pub const WALLET_NAME: &str = "silentdragonlite-cli-wallet.dat";
|
||||
pub const LOGFILE_NAME: &str = "silentdragonlite-cli-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((105942,
|
||||
"00000001c0199f329ee03379bf1387856dbab23765da508bf9b9d8d544f212c0",
|
||||
""
|
||||
)),
|
||||
"main" => Some((105944,
|
||||
"0000000313b0ec7c5a1e9b997ce44a7763b56c5505526c36634a004ed52d7787",
|
||||
""
|
||||
)),
|
||||
_ => 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 {
|
||||
@@ -209,14 +201,16 @@ pub struct LightClient {
|
||||
// zcash-params
|
||||
pub sapling_output : Vec<u8>,
|
||||
pub sapling_spend : Vec<u8>,
|
||||
|
||||
sync_lock : Mutex<()>,
|
||||
}
|
||||
|
||||
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),
|
||||
@@ -239,10 +233,11 @@ impl LightClient {
|
||||
wallet : Arc::new(RwLock::new(LightWallet::new(Some(seed_phrase), &config, 0)?)),
|
||||
config : config.clone(),
|
||||
sapling_output : vec![],
|
||||
sapling_spend : vec![]
|
||||
sapling_spend : vec![],
|
||||
sync_lock : Mutex::new(()),
|
||||
};
|
||||
|
||||
l.set_wallet_initial_state();
|
||||
l.set_wallet_initial_state(0);
|
||||
l.read_sapling_params();
|
||||
|
||||
info!("Created new wallet!");
|
||||
@@ -263,10 +258,11 @@ impl LightClient {
|
||||
wallet : Arc::new(RwLock::new(LightWallet::new(None, config, latest_block)?)),
|
||||
config : config.clone(),
|
||||
sapling_output : vec![],
|
||||
sapling_spend : vec![]
|
||||
sapling_spend : vec![],
|
||||
sync_lock : Mutex::new(()),
|
||||
};
|
||||
|
||||
l.set_wallet_initial_state();
|
||||
l.set_wallet_initial_state(latest_block);
|
||||
l.read_sapling_params();
|
||||
|
||||
info!("Created new wallet with a new seed!");
|
||||
@@ -275,20 +271,22 @@ impl LightClient {
|
||||
Ok(l)
|
||||
}
|
||||
|
||||
pub fn new_from_phrase(seed_phrase: String, config: &LightClientConfig, latest_block: u64) -> io::Result<Self> {
|
||||
pub fn new_from_phrase(seed_phrase: String, config: &LightClientConfig, birthday: u64) -> io::Result<Self> {
|
||||
if config.wallet_exists() {
|
||||
return Err(Error::new(ErrorKind::AlreadyExists,
|
||||
"Cannot create a new wallet from seed, because a wallet already exists"));
|
||||
}
|
||||
|
||||
let mut l = LightClient {
|
||||
wallet : Arc::new(RwLock::new(LightWallet::new(Some(seed_phrase), config, latest_block)?)),
|
||||
wallet : Arc::new(RwLock::new(LightWallet::new(Some(seed_phrase), config, birthday)?)),
|
||||
config : config.clone(),
|
||||
sapling_output : vec![],
|
||||
sapling_spend : vec![]
|
||||
sapling_spend : vec![],
|
||||
sync_lock : Mutex::new(()),
|
||||
};
|
||||
|
||||
l.set_wallet_initial_state();
|
||||
println!("Setting birthday to {}", birthday);
|
||||
l.set_wallet_initial_state(birthday);
|
||||
l.read_sapling_params();
|
||||
|
||||
info!("Created new wallet!");
|
||||
@@ -310,7 +308,8 @@ impl LightClient {
|
||||
wallet : Arc::new(RwLock::new(wallet)),
|
||||
config : config.clone(),
|
||||
sapling_output : vec![],
|
||||
sapling_spend : vec![]
|
||||
sapling_spend : vec![],
|
||||
sync_lock : Mutex::new(()),
|
||||
};
|
||||
|
||||
lc.read_sapling_params();
|
||||
@@ -727,7 +726,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);
|
||||
@@ -737,6 +736,10 @@ impl LightClient {
|
||||
}
|
||||
|
||||
pub fn do_sync(&self, print_updates: bool) -> String {
|
||||
// We can only do one sync at a time because we sync blocks in serial order
|
||||
// If we allow multiple syncs, they'll all get jumbled up.
|
||||
let _lock = self.sync_lock.lock().unwrap();
|
||||
|
||||
// Sync is 3 parts
|
||||
// 1. Get the latest block
|
||||
// 2. Get all the blocks that we don't have
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -360,8 +360,8 @@ impl LightWallet {
|
||||
})?;
|
||||
utils::write_string(&mut writer, &self.config.chain_name)?;
|
||||
|
||||
// While writing the birthday, be sure that we're right, and that we don't
|
||||
// have a tx that is before the current birthday
|
||||
// While writing the birthday, get it from the fn so we recalculate it properly
|
||||
// in case of rescans etc...
|
||||
writer.write_u64::<LittleEndian>(self.get_birthday())?;
|
||||
|
||||
Ok(())
|
||||
@@ -375,7 +375,11 @@ impl LightWallet {
|
||||
}
|
||||
|
||||
pub fn get_birthday(&self) -> u64 {
|
||||
cmp::min(self.get_first_tx_block(), self.birthday)
|
||||
if self.birthday == 0 {
|
||||
self.get_first_tx_block()
|
||||
} else {
|
||||
cmp::min(self.get_first_tx_block(), self.birthday)
|
||||
}
|
||||
}
|
||||
|
||||
// Get the first block that this wallet has a tx in. This is often used as the wallet's "birthday"
|
||||
@@ -388,7 +392,7 @@ impl LightWallet {
|
||||
.collect::<Vec<u64>>();
|
||||
blocks.sort();
|
||||
|
||||
*blocks.first() // Returns optional
|
||||
*blocks.first() // Returns optional, so if there's no txns, it'll get the activation height
|
||||
.unwrap_or(&cmp::max(self.birthday, self.config.sapling_activation_height))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user