Need birthday while restoring seed

This commit is contained in:
Aditya Kulkarni
2019-10-22 21:03:23 -07:00
parent adba18e13b
commit 11bc928f8d
3 changed files with 57 additions and 20 deletions

View File

@@ -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;
@@ -201,6 +201,8 @@ pub struct LightClient {
// zcash-params
pub sapling_output : Vec<u8>,
pub sapling_spend : Vec<u8>,
sync_lock : Mutex<()>,
}
impl LightClient {
@@ -231,7 +233,8 @@ 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(0);
@@ -255,7 +258,8 @@ 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(latest_block);
@@ -267,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(latest_block);
println!("Setting birthday to {}", birthday);
l.set_wallet_initial_state(birthday);
l.read_sapling_params();
info!("Created new wallet!");
@@ -302,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();
@@ -729,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

View File

@@ -358,8 +358,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(())
@@ -373,7 +373,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"
@@ -386,7 +390,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))
}