merge upstream updates

This commit is contained in:
Denio
2019-11-19 13:08:54 +01:00
5 changed files with 268 additions and 80 deletions

View File

@@ -1,6 +1,5 @@
use crate::lightwallet::LightWallet;
use log::{info, warn, error};
use rand::{rngs::OsRng, seq::SliceRandom};
use std::sync::{Arc, RwLock, Mutex};
@@ -20,6 +19,17 @@ use zcash_client_backend::{
constants::testnet, constants::mainnet, constants::regtest, encoding::encode_payment_address,
};
use log::{info, warn, error, LevelFilter};
use log4rs::append::rolling_file::RollingFileAppender;
use log4rs::encode::pattern::PatternEncoder;
use log4rs::config::{Appender, Config, Root};
use log4rs::filter::threshold::ThresholdFilter;
use log4rs::append::rolling_file::policy::compound::{
CompoundPolicy,
trigger::size::SizeTrigger,
roll::fixed_window::FixedWindowRoller,
};
use crate::grpc_client::{BlockId};
use crate::grpcconnector::{self, *};
use crate::SaplingParams;
@@ -100,6 +110,37 @@ impl LightClientConfig {
Ok((config, info.block_height))
}
/// Build the Logging config
pub fn get_log_config(&self) -> io::Result<Config> {
let window_size = 3; // log0, log1, log2
let fixed_window_roller =
FixedWindowRoller::builder().build("zecwallet-light-wallet-log{}",window_size).unwrap();
let size_limit = 5 * 1024 * 1024; // 5MB as max log file size to roll
let size_trigger = SizeTrigger::new(size_limit);
let compound_policy = CompoundPolicy::new(Box::new(size_trigger),Box::new(fixed_window_roller));
Config::builder()
.appender(
Appender::builder()
.filter(Box::new(ThresholdFilter::new(LevelFilter::Info)))
.build(
"logfile",
Box::new(
RollingFileAppender::builder()
.encoder(Box::new(PatternEncoder::new("{d} {l}::{m}{n}")))
.build(self.get_log_path(), Box::new(compound_policy))?,
),
),
)
.build(
Root::builder()
.appender("logfile")
.build(LevelFilter::Debug),
)
.map_err(|e|Error::new(ErrorKind::Other, format!("{}", e)))
}
pub fn get_zcash_data_path(&self) -> Box<Path> {
let mut zcash_data_location;
if self.data_dir.is_some() {
@@ -361,6 +402,16 @@ impl LightClient {
Ok(lc)
}
pub fn init_logging(&self) -> io::Result<()> {
// Configure logging first.
let log_config = self.config.get_log_config()?;
log4rs::init_config(log_config).map_err(|e| {
std::io::Error::new(ErrorKind::Other, e)
})?;
Ok(())
}
pub fn attempt_recover_seed(config: &LightClientConfig) -> Result<String, String> {
use std::io::prelude::*;
use byteorder::{LittleEndian, ReadBytesExt,};
@@ -791,14 +842,24 @@ impl LightClient {
Ok(array![new_address])
}
pub fn do_rescan(&self) -> Result<JsonValue, String> {
info!("Rescan starting");
pub fn clear_state(&self) {
// First, clear the state from the wallet
self.wallet.read().unwrap().clear_blocks();
// Then set the initial block
self.set_wallet_initial_state(self.wallet.read().unwrap().get_birthday());
info!("Cleared wallet state");
}
pub fn do_rescan(&self) -> Result<JsonValue, String> {
if !self.wallet.read().unwrap().is_unlocked_for_spending() {
warn!("Wallet is locked, new HD addresses won't be added!");
}
info!("Rescan starting");
self.clear_state();
// Then, do a sync, which will force a full rescan from the initial state
let response = self.do_sync(true);
info!("Rescan finished");