Files
ObsidianDragon/third_party/silentdragonxlite/silentdragonxlite-cli/lib/src/lightclient.rs
T
DanSandClaude Opus 4.8 be69c9bafe fix(litebe-sync): bound the reorg recovery window to the max depth
When sync hit a reorg it stepped the scan back to just before the
invalidated block, then set the top of the re-scan range to that
point plus the full forward batch size. That window is far larger
than any reorg can be: the batch size is tuned for forward streaming
(tens of thousands of blocks), while a reorg is capped at the maximum
reorg depth and the pass aborts once it is exceeded. So the recovery
pass re-fetched and re-scanned a large span of already-good blocks
before it could resume moving forward.

- The recovery range now reaches only the maximum reorg depth (plus
  one) above the rewind point, which is the most a reorg can ever
  disturb. Forward syncing still uses the full batch size; only the
  pass immediately after a reorg is bounded this way.
- The set of blocks that ultimately get scanned is unchanged - the
  next pass picks up from where recovery stops - so the resulting
  wallet is identical, just reached with far less redundant fetching
  and scanning.

The wallet file format is unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-14 08:50:12 -05:00

3105 lines
141 KiB
Rust

// Copyright The Hush Developers 2019-2022
// Released under the GPLv3
use crate::lightwallet::LightWallet;
use std::borrow::Cow;
use std::sync::{Arc, RwLock, Mutex, mpsc::channel};
use std::sync::atomic::{AtomicBool, AtomicI32, AtomicUsize, Ordering};
use std::path::{Path, PathBuf};
use std::fs::File;
use std::collections::{HashSet, HashMap};
use std::cmp::{max, min};
use std::io;
use std::io::prelude::*;
use std::io::{BufReader, BufWriter, Error, ErrorKind};
use tokio::runtime::Runtime;
use tokio::time::{Duration};
use crate::grpc_client::RawTransaction;
use protobuf::parse_from_bytes;
use threadpool::ThreadPool;
use json::{object, array, JsonValue};
use zcash_primitives::transaction::{TxId, Transaction};
use zcash_client_backend::{constants::testnet, constants::mainnet, constants::regtest,};
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::grpcconnector::{self, *};
use crate::ANCHOR_OFFSET;
mod checkpoints;
pub const DEFAULT_SERVER: &str = "https://lite.dragonx.is";
pub const WALLET_NAME: &str = "silentdragonxlite-wallet.dat";
pub const LOGFILE_NAME: &str = "silentdragonxlite-wallet.debug.log";
// C254: the data dir, the wallet files and the log are kept private to the owner on unix. On Windows these helpers
// are the plain std calls (the per-user AppData ACL confines the files). Each one is an item-level
// #[cfg(unix)] / #[cfg(not(unix))] pair, so the mingw build has nothing unused.
/// A chmod that fails (drvfs /mnt/c, FAT, NFS root-squash, a file of another uid) is warned about once per process:
/// get_zcash_data_path() runs on every save. An AtomicBool, not a Once (which poisons if its closure panics).
#[cfg(unix)]
static PERM_WARNED: AtomicBool = AtomicBool::new(false);
#[cfg(unix)]
fn perm_warn(p: &Path, e: &io::Error) {
if !PERM_WARNED.swap(true, Ordering::Relaxed) {
warn!("Couldn't restrict {:?}: {}", p, e);
}
}
/// Create `d` and any missing parents; on unix every created component is 0700.
#[cfg(unix)]
fn create_private_dir_all(d: &Path) -> io::Result<()> {
use std::os::unix::fs::DirBuilderExt;
std::fs::DirBuilder::new().recursive(true).mode(0o700).create(d)
}
#[cfg(not(unix))]
fn create_private_dir_all(d: &Path) -> io::Result<()> {
std::fs::create_dir_all(d)
}
/// Strip the group/other bits from an existing dir (`want_dir`) or regular file. The owner bits are kept (none is
/// ever added), a symlink is never followed, and a missing path or a path of the other kind is left alone.
#[cfg(unix)]
fn tighten(p: &Path, want_dir: bool) {
use std::os::unix::fs::PermissionsExt;
if let Ok(md) = std::fs::symlink_metadata(p) {
let m = md.permissions().mode();
let kind_ok = if want_dir { md.is_dir() } else { md.is_file() };
if kind_ok && m & 0o077 != 0 {
if let Err(e) = std::fs::set_permissions(p, std::fs::Permissions::from_mode(m & 0o7700)) {
perm_warn(p, &e);
}
}
}
}
#[cfg(not(unix))]
fn tighten(_p: &Path, _want_dir: bool) {}
/// A fresh inode, owner-only from creation (s1-D9). A stale file at `p` (a crash-leftover .dat.tmp, or a symlink) is
/// unlinked, never reused or followed, so an fd opened on it earlier never sees the new content. A concurrent creator
/// in between makes this fail, which is the safe outcome.
#[cfg(unix)]
fn create_private_file_new(p: &Path) -> io::Result<File> {
use std::os::unix::fs::OpenOptionsExt;
match std::fs::remove_file(p) {
Ok(()) => {},
Err(ref e) if e.kind() == ErrorKind::NotFound => {},
Err(e) => return Err(e),
}
std::fs::OpenOptions::new().write(true).create_new(true).mode(0o600).open(p)
}
#[cfg(not(unix))]
fn create_private_file_new(p: &Path) -> io::Result<File> {
File::create(p)
}
/// X3: the encryption flag in a wallet file's header (u64 LE version, then the flag byte that LightWallet::read reads
/// for every version >= 4). None when the header cannot tell: a short read or an I/O error, v5 (gzip: its byte 8 is
/// not the flag), v<=3 (no flag) or v>9 (a format this build does not know).
fn wallet_header_encrypted(p: &Path) -> Option<bool> {
let mut h = [0u8; 9];
File::open(p).and_then(|mut f| f.read_exact(&mut h)).ok()?;
let mut v = [0u8; 8];
v.copy_from_slice(&h[..8]);
match u64::from_le_bytes(v) {
4 | 6 | 7 | 8 | 9 => Some(h[8] != 0),
_ => None,
}
}
#[derive(Clone, Debug)]
pub struct WalletStatus {
pub is_syncing: bool,
pub total_blocks: u64,
pub synced_blocks: u64,
}
impl WalletStatus {
pub fn new() -> Self {
WalletStatus {
is_syncing: false,
total_blocks: 0,
synced_blocks: 0
}
}
}
#[derive(Clone, Debug)]
pub struct LightClientConfig {
pub server : http::Uri,
pub chain_name : String,
pub sapling_activation_height : u64,
pub consensus_branch_id : String,
pub anchor_offset : u32,
pub no_cert_verification : bool,
pub data_dir : Option<String>
}
impl LightClientConfig {
// Create an unconnected (to any server) config to test for local wallet etc...
pub fn create_unconnected(chain_name: String, dir: Option<String>) -> LightClientConfig {
LightClientConfig {
server : http::Uri::default(),
chain_name : chain_name,
sapling_activation_height : 0,
consensus_branch_id : "".to_string(),
anchor_offset : ANCHOR_OFFSET,
no_cert_verification : false,
data_dir : dir,
}
}
pub fn create(server: http::Uri, dangerous: bool) -> io::Result<(LightClientConfig, u64)> {
use std::net::ToSocketAddrs;
// Test for a connection first
format!("{}:{}", server.host().unwrap(), server.port().unwrap())
.to_socket_addrs()?
.next()
.ok_or(std::io::Error::new(ErrorKind::ConnectionRefused, "Couldn't resolve server!"))?;
// Do a getinfo first, before opening the wallet
let info = grpcconnector::get_info(&server, dangerous)
.map_err(|e| std::io::Error::new(ErrorKind::ConnectionRefused, e))?;
// Create a Light Client Config
let config = LightClientConfig {
server,
chain_name : info.chain_name,
sapling_activation_height : info.sapling_activation_height,
consensus_branch_id : info.consensus_branch_id,
anchor_offset : ANCHOR_OFFSET,
no_cert_verification : dangerous,
data_dir : None,
};
Ok((config, info.block_height))
}
/// The log roller's file pattern, anchored in the data dir (s1-D7): rolled logs stay inside the owner-only data
/// dir instead of the process's working directory (which may be read-only, on Windows too). FixedWindowRoller
/// replaces every "{}" in the pattern, so a data path that contains "{}" itself (or is not UTF-8) keeps the old
/// relative pattern.
fn log_roll_pattern(&self) -> String {
const PATTERN: &str = "zecwallet-light-wallet-log{}";
let data_dir = self.get_zcash_data_path();
match (data_dir.to_str(), data_dir.join(PATTERN).to_str()) {
(Some(d), Some(p)) if !d.contains("{}") => p.to_string(),
_ => PATTERN.to_string(),
}
}
/// Build the Logging config
pub fn get_log_config(&self) -> io::Result<Config> {
// C254: on unix the log is created owner-only (0600) before log4rs opens it for appending (log4rs 0.8.3 has
// no mode option), and an existing one loses its group/other bits. That mode lasts only until the first 5 MB
// roll, after which log4rs re-creates the file with the umask mode: the 0700 data dir is the lasting control
// (s1-D11).
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
let log_path = self.get_log_path();
let _ = std::fs::OpenOptions::new().append(true).create(true).mode(0o600).open(&log_path);
tighten(&log_path, false);
}
let window_size = 3; // log0, log1, log2
let fixed_window_roller =
FixedWindowRoller::builder().build(&self.log_roll_pattern(),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() {
zcash_data_location = PathBuf::from(&self.data_dir.as_ref().unwrap());
} else {
if cfg!(target_os="macos") || cfg!(target_os="windows") {
zcash_data_location = dirs::data_dir().expect("Couldn't determine app data directory!");
zcash_data_location.push("silentdragonxlite");
} else {
zcash_data_location = dirs::home_dir().expect("Couldn't determine home directory!");
zcash_data_location.push(".silentdragonxlite");
};
match &self.chain_name[..] {
"main" => {},
"test" => zcash_data_location.push("testnet3"),
"regtest" => zcash_data_location.push("regtest"),
c => panic!("Unknown chain {}", c),
};
}
// Create directory if it doesn't exist. C254: a caller-supplied data_dir (tests, embedders) is created as
// before and never chmodded. The default one is created owner-only (0700 on unix), and an existing one (for
// test/regtest also its parent) loses its group/other bits; the owner bits are kept and a symlink is never
// followed. The params dir (get_zcash_params_path) is left alone (s1-D5).
let created = if self.data_dir.is_some() {
std::fs::create_dir_all(zcash_data_location.clone())
} else {
create_private_dir_all(&zcash_data_location)
};
match created {
Ok(_) => {
if self.data_dir.is_none() {
tighten(&zcash_data_location, true);
if self.chain_name != "main" {
if let Some(parent) = zcash_data_location.parent() {
tighten(parent, true);
}
}
}
zcash_data_location.into_boxed_path()
},
Err(e) => {
eprintln!("Couldn't create zcash directory!\n{}", e);
panic!("Couldn't create zcash directory!");
}
}
}
pub fn get_wallet_path(&self) -> Box<Path> {
let mut wallet_location = self.get_zcash_data_path().into_path_buf();
wallet_location.push(WALLET_NAME);
wallet_location.into_boxed_path()
}
pub fn wallet_exists(&self) -> bool {
return self.get_wallet_path().exists()
}
pub fn get_zcash_params_path(&self) -> io::Result<Box<Path>> {
let mut zcash_params = self.get_zcash_data_path().into_path_buf();
zcash_params.push("..");
if cfg!(target_os="macos") || cfg!(target_os="windows") {
zcash_params.push("ZcashParams");
} else {
zcash_params.push(".zcash-params");
}
match std::fs::create_dir_all(zcash_params.clone()) {
Ok(_) => Ok(zcash_params.into_boxed_path()),
Err(e) => {
eprintln!("Couldn't create zcash params directory\n{}", e);
Err(e)
}
}
}
pub fn get_log_path(&self) -> Box<Path> {
let mut log_path = self.get_zcash_data_path().into_path_buf();
log_path.push(LOGFILE_NAME);
log_path.into_boxed_path()
}
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 {
match server {
Some(s) => {
let mut s = if s.starts_with("http") {s} else { "http://".to_string() + &s};
let uri: http::Uri = s.parse().unwrap();
if uri.port().is_none() {
s = s + ":443";
}
s
}
None => DEFAULT_SERVER.to_string()
}.parse().unwrap()
}
pub fn get_coin_type(&self) -> u32 {
match &self.chain_name[..] {
"main" => mainnet::COIN_TYPE,
"test" => testnet::COIN_TYPE,
"regtest" => regtest::COIN_TYPE,
c => panic!("Unknown chain {}", c)
}
}
pub fn hrp_sapling_address(&self) -> &str {
match &self.chain_name[..] {
"main" => mainnet::HRP_SAPLING_PAYMENT_ADDRESS,
"test" => testnet::HRP_SAPLING_PAYMENT_ADDRESS,
"regtest" => regtest::HRP_SAPLING_PAYMENT_ADDRESS,
c => panic!("Unknown chain {}", c)
}
}
pub fn hrp_sapling_private_key(&self) -> &str {
match &self.chain_name[..] {
"main" => mainnet::HRP_SAPLING_EXTENDED_SPENDING_KEY,
"test" => testnet::HRP_SAPLING_EXTENDED_SPENDING_KEY,
"regtest" => regtest::HRP_SAPLING_EXTENDED_SPENDING_KEY,
c => panic!("Unknown chain {}", c)
}
}
pub fn hrp_sapling_viewing_key(&self) -> &str {
match &self.chain_name[..] {
"main" => mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
"test" => testnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
"regtest" => regtest::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY,
c => panic!("Unknown chain {}", c)
}
}
pub fn base58_pubkey_address(&self) -> [u8; 1] {
match &self.chain_name[..] {
"main" => mainnet::B58_PUBKEY_ADDRESS_PREFIX,
c => panic!("Unknown chain {}", c)
}
}
pub fn base58_script_address(&self) -> [u8; 1] {
match &self.chain_name[..] {
"main" => mainnet::B58_SCRIPT_ADDRESS_PREFIX,
c => panic!("Unknown chain {}", c)
}
}
pub fn base58_secretkey_prefix(&self) -> [u8; 1] {
match &self.chain_name[..] {
"main" => [0xBC],
"test" => [0xEF],
"regtest" => [0xEF],
c => panic!("Unknown chain {}", c)
}
}
}
/// A block range that the wallet has already block-scanned but whose t-address (transparent) scan did not complete,
/// with the block times needed to date the transactions found in it. Memory only; never written to the wallet file.
#[derive(Debug, Clone, Default, PartialEq)]
struct TaddrPending {
start: u64,
times: HashMap<u64, u32>,
}
pub struct LightClient {
pub wallet : Arc<RwLock<LightWallet>>,
pub config : LightClientConfig,
// zcash-params
pub sapling_output : Cow<'static, [u8]>,
pub sapling_spend : Cow<'static, [u8]>,
sync_lock : Mutex<()>,
t_pending : Mutex<Option<TaddrPending>>, // leaf lock: never held while taking sync_lock or the wallet lock
sync_status : Arc<RwLock<WalletStatus>>, // The current syncing status of the Wallet.
pub shutdown_flag : Arc<AtomicBool>, // Signal mempool threads to stop
}
impl LightClient {
pub fn set_wallet_initial_state(&self, height: u64) {
use std::convert::TryInto;
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),
_ => true,
};
}
fn write_file_if_not_exists(dir: &Box<Path>, name: &str, bytes: &[u8]) -> io::Result<()> {
let mut file_path = dir.to_path_buf();
file_path.push(name);
if !file_path.exists() {
let mut file = File::create(&file_path)?;
file.write_all(bytes)?;
}
Ok(())
}
#[cfg(feature = "embed_params")]
fn read_sapling_params(&mut self) {
// Read Sapling Params
use crate::SaplingParams;
self.sapling_output = SaplingParams::get("sapling-output.params").unwrap();
self.sapling_spend = SaplingParams::get("sapling-spend.params").unwrap();
}
pub fn set_sapling_params(&mut self, sapling_output: &[u8], sapling_spend: &[u8]) -> Result<(), String> {
use sha2::{Sha256, Digest};
// The hashes of the params need to match
const SAPLING_OUTPUT_HASH: &str = "2f0ebbcbb9bb0bcffe95a397e7eba89c29eb4dde6191c339db88570e3f3fb0e4";
const SAPLING_SPEND_HASH: &str = "8e48ffd23abb3a5fd9c5589204f32d9c31285a04b78096ba40a79b75677efc13";
if SAPLING_OUTPUT_HASH.to_string() != hex::encode(Sha256::digest(&sapling_output)) {
return Err(format!("sapling-output hash didn't match. expected {}, found {}", SAPLING_OUTPUT_HASH, hex::encode(Sha256::digest(&sapling_output)) ))
}
if SAPLING_SPEND_HASH.to_string() != hex::encode(Sha256::digest(&sapling_spend)) {
return Err(format!("sapling-spend hash didn't match. expected {}, found {}", SAPLING_SPEND_HASH, hex::encode(Sha256::digest(&sapling_spend)) ))
}
// Will not overwrite previous params
if self.sapling_output.is_empty() {
self.sapling_output = Cow::Owned(sapling_output.to_vec());
}
if self.sapling_spend.is_empty() {
self.sapling_spend = Cow::Owned(sapling_spend.to_vec());
}
// Ensure that the sapling params are stored on disk properly as well.
match self.config.get_zcash_params_path() {
Ok(zcash_params_dir) => {
// Create the sapling output and spend params files
match LightClient::write_file_if_not_exists(&zcash_params_dir, "sapling-output.params", &self.sapling_output) {
Ok(_) => {},
Err(e) => eprintln!("Warning: Couldn't write the output params!\n{}", e)
};
match LightClient::write_file_if_not_exists(&zcash_params_dir, "sapling-spend.params", &self.sapling_spend) {
Ok(_) => {},
Err(e) => eprintln!("Warning: Couldn't write the output params!\n{}", e)
}
},
Err(e) => {
eprintln!("{}", e);
}
};
Ok(())
}
/// Method to create a test-only version of the LightClient
#[allow(dead_code)]
pub fn unconnected(seed_phrase: String, dir: Option<String>) -> io::Result<Self> {
let config = LightClientConfig::create_unconnected("test".to_string(), dir);
let mut l = LightClient {
wallet : Arc::new(RwLock::new(LightWallet::new(Some(seed_phrase), &config, 0,0 )?)),
config : config.clone(),
sapling_output : Cow::Borrowed(&[]),
sapling_spend : Cow::Borrowed(&[]),
sync_lock : Mutex::new(()),
sync_status : Arc::new(RwLock::new(WalletStatus::new())),
shutdown_flag : Arc::new(AtomicBool::new(false)),
t_pending : Mutex::new(None),
};
l.set_wallet_initial_state(0);
#[cfg(feature = "embed_params")]
l.read_sapling_params();
info!("Created new wallet!");
info!("Created LightClient to {}", &config.server);
Ok(l)
}
/// Create a brand new wallet with a new seed phrase. Will fail if a wallet file
/// already exists on disk
pub fn new(config: &LightClientConfig, latest_block: 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(None, config, latest_block, 0)?)),
config : config.clone(),
sapling_output : Cow::Borrowed(&[]),
sapling_spend : Cow::Borrowed(&[]),
sync_lock : Mutex::new(()),
sync_status : Arc::new(RwLock::new(WalletStatus::new())),
shutdown_flag : Arc::new(AtomicBool::new(false)),
t_pending : Mutex::new(None),
};
l.set_wallet_initial_state(latest_block);
#[cfg(feature = "embed_params")]
l.read_sapling_params();
info!("Created new wallet with a new seed!");
info!("Created LightClient to {}", &config.server);
l.do_save().map_err(|s| io::Error::new(ErrorKind::PermissionDenied, s))?;
Ok(l)
}
pub fn new_from_phrase(seed_phrase: String, config: &LightClientConfig, birthday: u64,number: u64, overwrite: bool) -> io::Result<Self> {
if !overwrite && 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, birthday, number)?)),
config : config.clone(),
sapling_output : Cow::Borrowed(&[]),
sapling_spend : Cow::Borrowed(&[]),
sync_lock : Mutex::new(()),
sync_status : Arc::new(RwLock::new(WalletStatus::new())),
shutdown_flag : Arc::new(AtomicBool::new(false)),
t_pending : Mutex::new(None),
};
// println!("Setting birthday to {}", birthday);
l.set_wallet_initial_state(birthday);
#[cfg(feature = "embed_params")]
l.read_sapling_params();
info!("Created new wallet!");
info!("Created LightClient to {}", &config.server);
l.do_save().map_err(|s| io::Error::new(ErrorKind::PermissionDenied, s))?;
Ok(l)
}
pub fn read_from_disk(config: &LightClientConfig) -> io::Result<Self> {
Self::read_from_disk_reporting(config).map(|(lc, _)| lc)
}
/// read_from_disk, plus what the open did to the files (s2-D14): Some("recovered from backup; damaged file kept as
/// <name>") when a corrupt wallet was replaced by its backup, for the FFI to pass on.
pub fn read_from_disk_reporting(config: &LightClientConfig) -> io::Result<(Self, Option<String>)> {
if !config.wallet_exists() {
// Try to recover from backup
let bak_path = config.get_wallet_path().with_extension("dat.bak");
if bak_path.exists() {
warn!("Wallet file missing but backup found, attempting recovery from {:?}", bak_path);
std::fs::copy(&bak_path, config.get_wallet_path())?;
tighten(&config.get_wallet_path(), false);
info!("Wallet recovered from backup");
} else {
return Err(Error::new(ErrorKind::AlreadyExists,
format!("Cannot read wallet. No file at {}", config.get_wallet_path().display())));
}
}
// C244: fall back to the backup ONLY for a corrupt or torn file: a parser error, or a parser panic, which
// read_wallet_file turns into InvalidData. Never for a transient I/O error (a sharing violation, EACCES) or a
// wallet written by a newer app (Unsupported, s2-D4): both would replace good data with an older .bak.
let wallet_path = config.get_wallet_path();
let bak_path = wallet_path.with_extension("dat.bak");
let (wallet, recovered) = match Self::read_wallet_file(&wallet_path, config) {
Ok(w) => (w, None),
Err(e) if matches!(e.kind(), ErrorKind::InvalidData | ErrorKind::UnexpectedEof | ErrorKind::InvalidInput) => {
// The logger is not set up yet (the FFI initialises it after the open), so this goes to stderr.
let _ = writeln!(io::stderr(), "Wallet file unreadable ({}); trying the backup", e);
if !bak_path.exists() {
return Err(e);
}
// Read the backup FIRST: if it is bad too, both files are left exactly as they were.
let w = Self::read_wallet_file(&bak_path, config)?;
// s2-D12: never silently drop wallet encryption. Only a .bak last saved by Lite <= 1.1.1 can predate
// the encryption of its wallet (since s1-D6 the first encrypted save backs up the encrypted file).
if !w.is_encrypted() && wallet_header_encrypted(&wallet_path) == Some(true) {
return Err(io::Error::new(ErrorKind::InvalidData,
"The wallet file is damaged and its backup was made before the wallet was encrypted. \
Nothing was changed. Restore from your seed phrase, or move the damaged wallet file aside \
to open the older, unencrypted backup."));
}
// s2-D13: keep the damaged file, and never overwrite an earlier one: move it aside, or copy it aside
// when it cannot be moved, before the backup replaces it.
let corrupt = Self::unused_corrupt_path(&wallet_path);
if let Err(re) = std::fs::rename(&wallet_path, &corrupt) {
std::fs::copy(&wallet_path, &corrupt).map_err(|ce| io::Error::new(ce.kind(),
format!("Couldn't keep the damaged wallet file: {} / {}", re, ce)))?;
}
std::fs::copy(&bak_path, &wallet_path)?;
tighten(&wallet_path, false);
let name = corrupt.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
let _ = writeln!(io::stderr(), "Wallet restored from backup; damaged file kept as {}", corrupt.display());
(w, Some(format!("recovered from backup; damaged file kept as {}", name)))
}
Err(e) => return Err(e),
};
// C254: a wallet or .bak written before owner-only saves (0644 under the default umask) is healed on every
// open, not only at the next save.
tighten(&config.get_wallet_path(), false);
tighten(&config.get_wallet_path().with_extension("dat.bak"), false);
let mut lc = LightClient {
wallet : Arc::new(RwLock::new(wallet)),
config : config.clone(),
sapling_output : Cow::Borrowed(&[]),
sapling_spend : Cow::Borrowed(&[]),
sync_lock : Mutex::new(()),
sync_status : Arc::new(RwLock::new(WalletStatus::new())),
shutdown_flag : Arc::new(AtomicBool::new(false)),
t_pending : Mutex::new(None),
};
#[cfg(feature = "embed_params")]
lc.read_sapling_params();
info!("Read wallet with birthday {}", lc.wallet.read().unwrap().get_first_tx_block());
info!("Created LightClient to {}", &config.server);
Ok((lc, recovered))
}
/// C244: one parse of a wallet file. A parser panic on a corrupt file becomes InvalidData (lib/Cargo.toml keeps
/// panic = "unwind" for release builds, so the catch is effective).
fn read_wallet_file(path: &Path, config: &LightClientConfig) -> io::Result<LightWallet> {
let f = File::open(path)?;
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| LightWallet::read(BufReader::new(f), config))) {
Ok(r) => r,
Err(_) => Err(io::Error::new(ErrorKind::InvalidData, "wallet file is corrupt (parser panic)")),
}
}
/// s2-D13: <wallet>.dat.corrupt-<unix secs>, with a -1, -2, ... suffix while that name is taken. Any entry counts
/// as taken, a dangling symlink too (symlink_metadata), so an earlier damaged file is never overwritten and a
/// symlink is never followed.
fn unused_corrupt_path(wallet_path: &Path) -> PathBuf {
let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0);
let taken = |p: &Path| std::fs::symlink_metadata(p).is_ok();
let mut p = wallet_path.with_extension(format!("dat.corrupt-{}", ts));
let mut i: u64 = 1;
while taken(&p) {
p = wallet_path.with_extension(format!("dat.corrupt-{}-{}", ts, i));
i += 1;
}
p
}
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, password: Option<String>) -> Result<String, String> {
use std::io::prelude::*;
use byteorder::{LittleEndian, ReadBytesExt};
use libflate::gzip::Decoder;
use bip39::{Mnemonic, Language};
use zcash_primitives::serialize::Vector;
let mut inp = BufReader::new(File::open(config.get_wallet_path()).unwrap());
let version = inp.read_u64::<LittleEndian>().unwrap();
println!("Reading wallet version {}", version);
// At version 5, we're writing the rest of the file as a compressed stream (gzip)
let mut reader: Box<dyn Read> = if version != 5 {
Box::new(inp)
} else {
Box::new(Decoder::new(inp).unwrap())
};
let encrypted = if version >= 4 {
reader.read_u8().unwrap() > 0
} else {
false
};
if encrypted && password.is_none() {
return Err("The wallet is encrypted and a password was not specified. Please specify the password with '--password'!".to_string());
}
let mut enc_seed = [0u8; 48];
if version >= 4 {
reader.read_exact(&mut enc_seed).unwrap();
}
let nonce = if version >= 4 {
Vector::read(&mut reader, |r| r.read_u8()).unwrap()
} else {
vec![]
};
let phrase = if encrypted {
use sodiumoxide::crypto::secretbox;
use crate::lightwallet::double_sha256;
// Get the doublesha256 of the password, which is the right length
let key = secretbox::Key::from_slice(&double_sha256(password.unwrap().as_bytes())).unwrap();
let nonce = secretbox::Nonce::from_slice(&nonce).unwrap();
let seed = match secretbox::open(&enc_seed, &nonce, &key) {
Ok(s) => s,
Err(_) => return Err("Decryption failed. Is your password correct?".to_string())
};
Mnemonic::from_entropy(&seed, Language::English)
} else {
// Seed
let mut seed_bytes = [0u8; 32];
reader.read_exact(&mut seed_bytes).unwrap();
Mnemonic::from_entropy(&seed_bytes, Language::English)
}.map_err(|e| format!("Failed to read seed. {:?}", e));
phrase.map(|m| m.phrase().to_string())
}
pub fn last_scanned_height(&self) -> u64 {
self.wallet.read().unwrap().last_scanned_height() as u64
}
// Export private keys
pub fn do_export(&self, addr: Option<String>) -> Result<JsonValue, &str> {
if !self.wallet.read().unwrap().is_unlocked_for_spending() {
error!("Wallet is locked");
return Err("Wallet is locked");
}
// Clone address so it can be moved into the closure
let address = addr.clone();
let wallet = self.wallet.read().unwrap();
// Go over all z addresses
let z_keys = wallet.get_z_private_keys().iter()
.filter( move |(addr, _, _)| address.is_none() || address.as_ref() == Some(addr))
.map( |(addr, pk, vk)|
object!{
"address" => addr.clone(),
"private_key" => pk.clone(),
"viewing_key" => vk.clone(),
}
).collect::<Vec<JsonValue>>();
// Clone address so it can be moved into the closure
let address = addr.clone();
// Go over all t addresses
let t_keys = wallet.get_t_secret_keys().iter()
.filter( move |(addr, _)| address.is_none() || address.as_ref() == Some(addr))
.map( |(addr, sk)|
object!{
"address" => addr.clone(),
"private_key" => sk.clone(),
}
).collect::<Vec<JsonValue>>();
let mut all_keys = vec![];
all_keys.extend_from_slice(&z_keys);
all_keys.extend_from_slice(&t_keys);
Ok(all_keys.into())
}
pub fn do_address(&self) -> JsonValue {
let wallet = self.wallet.read().unwrap();
// Collect z addresses
let z_addresses = wallet.get_all_zaddresses();
// Collect t addresses
let t_addresses = wallet.get_all_taddresses().iter().map( |a| a.clone() )
.collect::<Vec<String>>();
object!{
"z_addresses" => z_addresses,
"t_addresses" => t_addresses,
}
}
pub fn do_balance(&self) -> JsonValue {
let wallet = self.wallet.read().unwrap();
// Collect z addresses
let z_addresses = wallet.z_balances_by_address().into_iter()
.map(|(address, zbalance, unconfirmed, verified_zbalance, spendable_zbalance)| {
object!{
"address" => address,
"zbalance" => zbalance,
"unconfirmed" => unconfirmed,
"verified_zbalance" => verified_zbalance,
"spendable_zbalance" => spendable_zbalance
}
}).collect::<Vec<JsonValue>>();
// Collect t addresses
let t_addresses = wallet.get_all_taddresses().iter().map( |address| {
// Get the balance for this address
let balance = wallet.tbalance(Some(address.clone())) ;
object!{
"address" => address.clone(),
"balance" => balance,
}
}).collect::<Vec<JsonValue>>();
object!{
"zbalance" => wallet.zbalance(None),
"unconfirmed" => wallet.unconfirmed_zbalance(None),
"verified_zbalance" => wallet.verified_zbalance(None),
"spendable_zbalance" => wallet.spendable_zbalance(None),
"tbalance" => wallet.tbalance(None),
"z_addresses" => z_addresses,
"t_addresses" => t_addresses,
}
}
pub fn do_save(&self) -> Result<(), String> {
// If the wallet is encrypted but unlocked, lock it again.
{
let mut wallet = self.wallet.write().unwrap();
if wallet.is_encrypted() && wallet.is_unlocked_for_spending() {
match wallet.lock() {
Ok(_) => {},
Err(e) => {
let err = format!("ERR: {}", e);
error!("{}", err);
return Err(e.to_string());
}
}
}
}
let r;
{
// Prevent any overlapping syncs during save, and don't save in the middle of a sync
let _lock = self.sync_lock.lock().unwrap();
let wallet_path = self.config.get_wallet_path();
let tmp_path = wallet_path.with_extension("dat.tmp");
let bak_path = wallet_path.with_extension("dat.bak");
// Write to a temporary file first (atomic save). C254/s1-D9: a fresh owner-only file (0600 on unix); a stale
// .dat.tmp is unlinked first, never reused. The rename carries the mode to the wallet.
let wallet = self.wallet.write().unwrap();
let mut file_buffer = BufWriter::with_capacity(
1_000_000, // 1 MB write buffer
create_private_file_new(&tmp_path).map_err(|e| format!("Failed to create temp file: {}", e))?);
r = match wallet.write(&mut file_buffer) {
Ok(_) => Ok(()),
Err(e) => {
let err = format!("ERR: {}", e);
error!("{}", err);
// Clean up temp file on write failure
let _ = std::fs::remove_file(&tmp_path);
Err(e.to_string())
}
};
file_buffer.flush().map_err(|e| {
let _ = std::fs::remove_file(&tmp_path);
format!("{}", e)
})?;
// Only proceed with rename if the write succeeded
if r.is_ok() {
// C244: make the new bytes durable BEFORE they can replace the old file (FlushFileBuffers on Windows,
// F_FULLFSYNC on macOS). Order: the tmp file, the .bak, the rename, then the directory (unix), so every
// crash point leaves a complete wallet or a complete old wallet.
file_buffer.get_ref().sync_all().map_err(|e| {
let _ = std::fs::remove_file(&tmp_path);
format!("Failed to sync wallet file: {}", e)
})?;
// Create backup of existing wallet (if it exists)
if wallet_path.exists() {
// s1-D6: at the plaintext -> encrypted transition, back up the NEW (encrypted) file, so no
// plaintext seed survives in .bak
let bak_src: &Path = if wallet.is_encrypted() && wallet_header_encrypted(&wallet_path) == Some(false)
{ tmp_path.as_path() } else { &*wallet_path };
match std::fs::copy(bak_src, &bak_path) {
// fs::copy gives the .bak its source's mode: strip the group/other bits (C254). Then make it
// durable (C244) through a write handle: FlushFileBuffers fails on a read-only one on Windows.
Ok(_) => {
tighten(&bak_path, false);
if let Ok(f) = std::fs::OpenOptions::new().write(true).open(&bak_path) { let _ = f.sync_all(); }
}
Err(e) => {
warn!("Failed to create wallet backup: {}", e);
// Non-fatal: continue with the save
}
}
}
// Atomically replace the wallet file with the new one
if let Err(e) = std::fs::rename(&tmp_path, &wallet_path) {
error!("Failed to rename temp wallet file: {}", e);
// Try direct copy as fallback (rename can fail across filesystems)
if let Err(e2) = std::fs::copy(&tmp_path, &wallet_path) {
let _ = std::fs::remove_file(&tmp_path);
return Err(format!("Failed to save wallet: {} / {}", e, e2));
}
if let Ok(f) = std::fs::OpenOptions::new().write(true).open(&wallet_path) { let _ = f.sync_all(); }
let _ = std::fs::remove_file(&tmp_path);
}
// C244: the rename is durable once its directory is. Unix only: a directory cannot be fsynced on
// Windows, and the rename is not write-through there (s2-D3).
#[cfg(unix)]
{
if let Some(dir) = wallet_path.parent() {
if let Ok(d) = File::open(dir) { let _ = d.sync_all(); }
}
}
}
}
r
}
pub fn get_server_uri(&self) -> http::Uri {
self.config.server.clone()
}
// DEBUG: dump the wallet's latest Sapling commitment tree so it can be diffed against the
// node's `getblockmerkletree <height>` to localize a tree/anchor divergence.
pub fn do_sapling_tree(&self) -> JsonValue {
match self.wallet.read().unwrap().get_sapling_tree() {
Ok((height, hash, tree)) => object!{
"height" => height,
"hash" => hash,
"tree" => tree,
"anchor_offset" => self.config.anchor_offset,
},
Err(e) => object!{ "error" => e },
}
}
pub fn do_info(&self) -> String {
match get_info(&self.get_server_uri(), self.config.no_cert_verification) {
Ok(i) => {
let o = object!{
"version" => i.version,
"vendor" => i.vendor,
"taddr_support" => i.taddr_support,
"chain_name" => i.chain_name,
"sapling_activation_height" => i.sapling_activation_height,
"consensus_branch_id" => i.consensus_branch_id,
"latest_block_height" => i.block_height,
"difficulty" => i.difficulty,
"longestchain" => i.longestchain,
"notarized" => i.notarized,
};
o.pretty(2)
},
Err(e) => e
}
}
pub fn do_coinsupply(&self) -> String {
match get_coinsupply(self.get_server_uri(), self.config.no_cert_verification) {
Ok(i) => {
let o = object!{
"result" => i.result,
"coin" => i.coin,
"height" => i.height,
"supply" => i.supply,
"zfunds" => i.zfunds,
"total" => i.total,
};
o.pretty(2)
},
Err(e) => e
}
}
pub fn do_seed_phrase(&self) -> Result<JsonValue, &str> {
if !self.wallet.read().unwrap().is_unlocked_for_spending() {
error!("Wallet is locked");
return Err("Wallet is locked");
}
let wallet = self.wallet.read().unwrap();
Ok(object!{
"seed" => wallet.get_seed_phrase(),
"birthday" => wallet.get_birthday()
})
}
// Return a list of all notes, spent and unspent
pub fn do_list_notes(&self, all_notes: bool) -> JsonValue {
let mut unspent_notes: Vec<JsonValue> = vec![];
let mut spent_notes : Vec<JsonValue> = vec![];
let mut pending_notes: Vec<JsonValue> = vec![];
let anchor_height: i32 = self.wallet.read().unwrap().get_anchor_height() as i32;
{
let wallet = self.wallet.read().unwrap();
// First, collect all extfvk's that are spendable (i.e., we have the private key)
let spendable_address_set: HashSet<String> = wallet.spendable_zaddress_set();
let spendable_address: &HashSet<String> = &spendable_address_set;
// Collect Sapling notes
wallet.txs.read().unwrap().iter()
.flat_map( |(txid, wtx)| {
let spendable_address: &HashSet<String> = spendable_address;
wtx.notes.iter().filter_map(move |nd|
if !all_notes && nd.spent.is_some() {
None
} else {
let address = LightWallet::note_address(self.config.hrp_sapling_address(), nd);
let spendable = address.is_some() &&
spendable_address.contains(&address.clone().unwrap()) &&
wtx.block <= anchor_height && nd.spent.is_none() && nd.unconfirmed_spent.is_none();
Some(object!{
"created_in_block" => wtx.block,
"datetime" => wtx.datetime,
"created_in_txid" => format!("{}", txid),
"value" => nd.note.value,
"is_change" => nd.is_change,
"address" => address,
"spendable" => spendable,
"spent" => nd.spent.map(|spent_txid| format!("{}", spent_txid)),
"unconfirmed_spent" => nd.unconfirmed_spent.map(|spent_txid| format!("{}", spent_txid)),
})
}
)
})
.for_each( |note| {
if note["spent"].is_null() && note["unconfirmed_spent"].is_null() {
unspent_notes.push(note);
} else if !note["spent"].is_null() {
spent_notes.push(note);
} else {
pending_notes.push(note);
}
});
}
let mut unspent_utxos: Vec<JsonValue> = vec![];
let mut spent_utxos : Vec<JsonValue> = vec![];
let mut pending_utxos: Vec<JsonValue> = vec![];
{
let wallet = self.wallet.read().unwrap();
wallet.txs.read().unwrap().iter()
.flat_map( |(txid, wtx)| {
wtx.utxos.iter().filter_map(move |utxo|
if !all_notes && utxo.spent.is_some() {
None
} else {
Some(object!{
"created_in_block" => wtx.block,
"datetime" => wtx.datetime,
"created_in_txid" => format!("{}", txid),
"value" => utxo.value,
"scriptkey" => hex::encode(utxo.script.clone()),
"is_change" => false, // TODO: Identify notes as change if we send change to taddrs
"address" => utxo.address.clone(),
"spent" => utxo.spent.map(|spent_txid| format!("{}", spent_txid)),
"unconfirmed_spent" => utxo.unconfirmed_spent.map(|spent_txid| format!("{}", spent_txid)),
})
}
)
})
.for_each( |utxo| {
if utxo["spent"].is_null() && utxo["unconfirmed_spent"].is_null() {
unspent_utxos.push(utxo);
} else if !utxo["spent"].is_null() {
spent_utxos.push(utxo);
} else {
pending_utxos.push(utxo);
}
});
}
let mut res = object!{
"unspent_notes" => unspent_notes,
"pending_notes" => pending_notes,
"utxos" => unspent_utxos,
"pending_utxos" => pending_utxos,
};
if all_notes {
res["spent_notes"] = JsonValue::Array(spent_notes);
res["spent_utxos"] = JsonValue::Array(spent_utxos);
}
res
}
pub fn do_encryption_status(&self) -> JsonValue {
let wallet = self.wallet.read().unwrap();
object!{
"encrypted" => wallet.is_encrypted(),
"locked" => !wallet.is_unlocked_for_spending()
}
}
pub fn do_list_transactions(&self) -> JsonValue {
let wallet = self.wallet.read().unwrap();
// Create a list of TransactionItems from wallet txns
let mut tx_list = wallet.txs.read().unwrap().iter()
.flat_map(| (_k, v) | {
let mut txns: Vec<JsonValue> = vec![];
if v.total_shielded_value_spent + v.total_transparent_value_spent > 0 {
// If money was spent, create a transaction. For this, we'll subtract
// all the change notes. TODO: Add transparent change here to subtract it also
let total_change: u64 = v.notes.iter()
.filter( |nd| nd.is_change )
.map( |nd| nd.note.value )
.sum();
// TODO: What happens if change is > than sent ?
// Collect outgoing metadata
let outgoing_json = v.outgoing_metadata.iter()
.map(|om|
object!{
"address" => om.address.clone(),
"value" => om.value,
"memo" => LightWallet::memo_str(&Some(om.memo.clone())),
})
.collect::<Vec<JsonValue>>();
txns.push(object! {
"block_height" => v.block,
"datetime" => v.datetime,
"txid" => format!("{}", v.txid),
"amount" => total_change as i64
- v.total_shielded_value_spent as i64
- v.total_transparent_value_spent as i64,
"outgoing_metadata" => outgoing_json,
});
}
// For each sapling note that is not a change, add a Tx.
txns.extend(v.notes.iter()
.filter( |nd| !nd.is_change )
.enumerate()
.map ( |(i, nd)|
object! {
"block_height" => v.block,
"datetime" => v.datetime,
"position" => i,
"txid" => format!("{}", v.txid),
"amount" => nd.note.value as i64,
"address" => LightWallet::note_address(self.config.hrp_sapling_address(), nd),
"memo" => LightWallet::memo_str(&nd.memo),
})
);
// Get the total transparent received
let total_transparent_received = v.utxos.iter().map(|u| u.value).sum::<u64>();
if total_transparent_received > v.total_transparent_value_spent {
// Create an input transaction for the transparent value as well.
txns.push(object!{
"block_height" => v.block,
"datetime" => v.datetime,
"txid" => format!("{}", v.txid),
"amount" => total_transparent_received as i64 - v.total_transparent_value_spent as i64,
"address" => v.utxos.iter().map(|u| u.address.clone()).collect::<Vec<String>>().join(","),
"memo" => None::<String>
})
}
txns
})
.collect::<Vec<JsonValue>>();
// Add the incoming Mempool - incoming_mempool flag is atm useless, but we can use that in future maybe
tx_list.extend(
wallet.incoming_mempool_txs.read().unwrap().iter().flat_map(|(_, wtxs)| {
wtxs.iter().flat_map(|wtx| {
wtx.incoming_metadata.iter()
.enumerate()
.map(move |(i, om)|
object! {
"block_height" => wtx.block.clone(),
"datetime" => wtx.datetime.clone(),
"position" => i,
"txid" => format!("{}", wtx.txid),
"amount" => om.value as i64,
"address" => om.address.clone(),
"memo" => LightWallet::memo_str(&Some(om.memo.clone())),
"unconfirmed" => true,
"incoming_mempool" => true,
}
)
})
})
);
// Add in all mempool txns
tx_list.extend(wallet.mempool_txs.read().unwrap().iter().map( |(_, wtx)| {
use zcash_primitives::transaction::components::amount::DEFAULT_FEE;
use std::convert::TryInto;
let amount: u64 = wtx.outgoing_metadata.iter().map(|om| om.value).sum::<u64>();
let fee: u64 = DEFAULT_FEE.try_into().unwrap();
// Collect outgoing metadata
let outgoing_json = wtx.outgoing_metadata.iter()
.map(|om|
object!{
"address" => om.address.clone(),
"value" => om.value,
"memo" => LightWallet::memo_str(&Some(om.memo.clone())),
}).collect::<Vec<JsonValue>>();
object! {
"block_height" => wtx.block,
"datetime" => wtx.datetime,
"txid" => format!("{}", wtx.txid),
"amount" => -1 * (fee + amount) as i64,
"unconfirmed" => true,
"outgoing_metadata" => outgoing_json,
}
}));
tx_list.sort_by( |a, b| if a["block_height"] == b["block_height"] {
a["txid"].as_str().cmp(&b["txid"].as_str())
} else {
a["block_height"].as_i32().cmp(&b["block_height"].as_i32())
}
);
JsonValue::Array(tx_list)
}
/// Create a new address, deriving it from the seed.
pub fn do_new_address(&self, addr_type: &str) -> Result<JsonValue, String> {
if !self.wallet.read().unwrap().is_unlocked_for_spending() {
error!("Wallet is locked");
return Err("Wallet is locked".to_string());
}
let new_address = {
let wallet = self.wallet.write().unwrap();
let addr = match addr_type {
"zs" => wallet.add_zaddr(),
"R" => wallet.add_taddr(),
_ => {
let e = format!("Unrecognized address type: {}", addr_type);
error!("{}", e);
return Err(e);
}
};
if addr.starts_with("Error") {
let e = format!("Error creating new address: {}", addr);
error!("{}", e);
return Err(e);
}
addr
};
self.do_save()?;
Ok(array![new_address])
}
/// Import a new private key
pub fn do_import_tk(&self, sk: String, birthday: u64) -> Result<JsonValue, String> {
// Checked before "locked": unlocking would not help (s1-D1). No "Error: " prefix here; the console
// import/timport commands add it.
if self.wallet.read().unwrap().is_encrypted() {
let e = "Can't import a private key while the wallet is encrypted - remove encryption, import, then encrypt again".to_string();
error!("{}", e);
return Err(e);
}
if !self.wallet.read().unwrap().is_unlocked_for_spending() {
error!("Wallet is locked");
return Err("Wallet is locked".to_string());
}
let new_address = {
let mut wallet = self.wallet.write().unwrap();
let addr = wallet.import_taddr(sk, birthday);
if addr.starts_with("Error") {
let e = format!("Error creating new address{}", addr);
error!("{}", e);
return Err(e);
}
addr
};
self.do_save()?;
Ok(array![new_address])
}
// Start Mempool-Monitor
pub fn start_mempool_monitor(lc: Arc<LightClient>) -> Result<(), String> {
let config = lc.config.clone();
let uri = config.server.clone();
// TLS verification follows the config (default on), as block sync does; `dangerous` is the only bypass.
let no_cert = config.no_cert_verification;
let shutdown = lc.shutdown_flag.clone();
let (incoming_mempool_tx, incoming_mempool_rx) = std::sync::mpsc::channel::<RawTransaction>();
// Thread for receive transactions
let shutdown_rx = lc.shutdown_flag.clone();
std::thread::spawn(move || {
while let Ok(rtx) = incoming_mempool_rx.recv() {
if shutdown_rx.load(Ordering::Relaxed) {
break;
}
if let Ok(tx) = Transaction::read(
&rtx.data[..])
{
let light_wallet_clone = lc.wallet.clone();
// One bad mempool tx must not end this thread: the mempool monitor would stop silently.
let scanned = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
light_wallet_clone.read().unwrap().scan_full_mempool_tx(&tx, rtx.height as i32, 0, true);
}));
if scanned.is_err() {
error!("Scanning mempool tx {} panicked; skipped it", tx.txid());
}
}
}
});
// Thread mempool monitor
std::thread::spawn(move || {
let mut rt = Runtime::new().unwrap();
rt.block_on(async {
loop {
if shutdown.load(Ordering::Relaxed) {
info!("Mempool monitor shutting down");
break;
}
let incoming_mempool_tx_clone = incoming_mempool_tx.clone();
let send_closure = move |rtx: RawTransaction| {
incoming_mempool_tx_clone.send(rtx).map_err(|e| Box::new(e) as Box<dyn std::error::Error>)
};
match grpcconnector::monitor_mempool(&uri.clone(), no_cert, send_closure).await {
Ok(_) => info!("Mempool monitor loop successful"),
Err(e) => {
// If the server doesn't implement GetMempoolStream, stop the monitor instead
// of reconnecting forever against a method that will never succeed (log spam
// + needless churn). Other errors fall through to the periodic retry below.
if e.to_string().contains("Unimplemented") {
info!("Server does not implement GetMempoolStream; stopping mempool monitor");
return;
}
warn!("Mempool monitor returned {:?}, will restart listening", e);
}
}
// Sleep in 1-second increments so we can check shutdown flag
for _ in 0..10 {
if shutdown.load(Ordering::Relaxed) {
info!("Mempool monitor shutting down during sleep");
return;
}
std::thread::sleep(Duration::from_secs(1));
}
}
});
});
Ok(())
}
/// Signal all background threads to stop
pub fn shutdown(&self) {
self.shutdown_flag.store(true, Ordering::Relaxed);
info!("Shutdown flag set");
}
/// Convinence function to determine what type of key this is and import it
pub fn do_import_key(&self, key: String, birthday: u64) -> Result<JsonValue, String> {
if key.starts_with(self.config.hrp_sapling_private_key()) {
self.do_import_sk(key, birthday)
} else if key.starts_with(self.config.hrp_sapling_viewing_key()) {
self.do_import_vk(key, birthday)
} else {
Err(format!("'{}' was not recognized as either a spending key or a viewing key because it didn't start with either '{}' or '{}'",
key, self.config.hrp_sapling_private_key(), self.config.hrp_sapling_viewing_key()))
}
}
/// Import a new private key
pub fn do_import_sk(&self, sk: String, birthday: u64) -> Result<JsonValue, String> {
// Checked before "locked": unlocking would not help (s1-D1). No "Error: " prefix here; the console
// import/timport commands add it.
if self.wallet.read().unwrap().is_encrypted() {
let e = "Can't import a private key while the wallet is encrypted - remove encryption, import, then encrypt again".to_string();
error!("{}", e);
return Err(e);
}
if !self.wallet.read().unwrap().is_unlocked_for_spending() {
error!("Wallet is locked");
return Err("Wallet is locked".to_string());
}
let new_address = {
let mut wallet = self.wallet.write().unwrap();
let addr = wallet.add_imported_sk(sk, birthday);
if addr.starts_with("Error") {
let e = format!("Error creating new address{}", addr);
error!("{}", e);
return Err(e);
}
addr
};
self.do_save()?;
Ok(array![new_address])
}
/// Import a new viewing key
pub fn do_import_vk(&self, vk: String, birthday: u64) -> Result<JsonValue, String> {
if !self.wallet.read().unwrap().is_unlocked_for_spending() {
error!("Wallet is locked");
return Err("Wallet is locked".to_string());
}
let new_address = {
let mut wallet = self.wallet.write().unwrap();
let addr = wallet.add_imported_vk(vk, birthday);
if addr.starts_with("Error") {
let e = format!("Error creating new address{}", addr);
error!("{}", e);
return Err(e);
}
addr
};
self.do_save()?;
Ok(array![new_address])
}
pub fn do_new_sietchaddress(&self, addr_type: &str) -> Result<JsonValue, String> {
let zdust_address = {
let wallet = self.wallet.write().unwrap();
match addr_type {
"zs" => wallet.add_zaddrdust(),
_ => {
let e = format!("Unrecognized address type: {}", addr_type);
error!("{}", e);
return Err(e);
}
}
};
Ok(array![zdust_address])
}
pub fn clear_state(&self) {
// Read the birthday into a local first: the wallet read guard must NOT be alive while
// clear_state_from waits for sync_lock (lock order is sync_lock -> wallet, as in do_save/do_sync).
// Held across the call it was also a recursive read lock, which a queued writer (a save) deadlocks.
let birthday = self.wallet.read().unwrap().get_birthday();
self.clear_state_from(birthday);
}
pub fn clear_state_from(&self, height: u64) {
// Serialize with sync/save/send: a sync that has already snapshotted the wallet's notes would
// otherwise find them gone mid-scan. No caller holds sync_lock, and the order is sync_lock -> wallet.
let _sync = self.sync_lock.lock().unwrap();
// The whole chain is scanned again from `height`: nothing is owed any more.
*self.t_pending.lock().unwrap_or_else(|p| p.into_inner()) = None;
// First, clear the state from the wallet
self.wallet.read().unwrap().clear_blocks();
// Then set the initial block
self.set_wallet_initial_state(height);
info!("Cleared wallet state to height {}", height);
}
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);
self.do_save()?;
info!("Rescan finished");
response
}
/// Return the syncing status of the wallet
pub fn do_scan_status(&self) -> WalletStatus {
self.sync_status.read().unwrap().clone()
}
pub fn do_sync(&self, print_updates: bool) -> Result<JsonValue, String> {
let mut retry_count = 0;
loop {
match self.do_sync_internal(print_updates) {
Ok(j) => return Ok(j),
Err(e) => {
retry_count += 1;
if retry_count > 5 {
return Err(e);
}
// Sleep exponentially backing off
std::thread::sleep(std::time::Duration::from_secs((2 as u64).pow(retry_count)));
}
}
}
}
pub fn do_shield(&self, address: Option<String>) -> Result<String, String> {
use zcash_primitives::transaction::components::amount::DEFAULT_FEE;
use std::convert::TryInto;
let fee = DEFAULT_FEE.try_into().unwrap();
let tbal = self.wallet.read().unwrap().tbalance(None);
// Make sure there is a balance, and it is greated than the amount
if tbal <= fee {
return Err(format!("Not enough transparent balance to shield. Have {} puposhis, need more than {} puposhis to cover tx fee", tbal, fee));
}
let addr = address.or(self.wallet.read().unwrap().get_all_zaddresses().get(0).map(|s| s.clone())).unwrap();
let fee: u64 = DEFAULT_FEE.try_into().unwrap();
let result = {
let _lock = self.sync_lock.lock().unwrap();
self.wallet.read().unwrap().send_to_address(
u32::from_str_radix(&self.config.consensus_branch_id, 16).unwrap(),
&self.sapling_spend, &self.sapling_output,
true,
vec![(&addr, tbal - fee, None)],
&fee,
|txbytes| broadcast_raw_tx(&self.get_server_uri(),self.config.no_cert_verification, txbytes)
)
};
result.map(|(txid, _)| txid)
}
fn do_sync_internal(&self, print_updates: bool) -> Result<JsonValue, 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
// 3. Find all new Txns that don't have the full Tx, and get them as full transactions
// and scan them, mainly to get the memos
let mut last_scanned_height = self.wallet.read().unwrap().last_scanned_height() as u64;
// This will hold the latest block fetched from the RPC
let latest_block = fetch_latest_block(&self.get_server_uri(), self.config.no_cert_verification)?.height;
if latest_block < last_scanned_height {
let w = format!("Server's latest block({}) is behind ours({})", latest_block, last_scanned_height);
warn!("{}", w);
return Err(w);
}
info!("Latest block is {}", latest_block);
// Get the end height to scan to.
// get_block_range streams an ENTIRE range over ONE gRPC connection, so the batch size is
// the dominant factor in how many TCP connections a sync opens. The old 1000 meant ~1300
// connections for a full sync, which on Windows exhausted socket buffers (WSAENOBUFS / os
// error 10055). A large batch (25k) cuts that ~50x (~52 connections for a full sync) with
// no extra memory — blocks are still streamed + scanned incrementally, not buffered.
let scan_batch_size = 25000;
let mut end_height = std::cmp::min(last_scanned_height + scan_batch_size, latest_block);
// If there's nothing to scan, just return. A t-address scan that failed earlier is still owed even when no
// block is: pay it now, or a transient failure leaves a hole in the transparent history that nothing revisits.
if last_scanned_height == latest_block {
if self.taddr_has_pending() {
let pool = ThreadPool::new(max(2, min(8, num_cpus::get())));
let block_times: Arc<RwLock<HashMap<u64, u32>>> = Arc::new(RwLock::new(HashMap::new()));
self.taddress_stage(last_scanned_height + 1, last_scanned_height, &block_times, |s, e, bt| {
self.scan_taddress_txids(&pool, bt, s, e, self.config.no_cert_verification).map(|_| ())
})?;
}
info!("Nothing to sync, returning");
return Ok(object!{ "result" => "success" })
}
{
let mut status = self.sync_status.write().unwrap();
status.is_syncing = true;
status.synced_blocks = last_scanned_height;
status.total_blocks = latest_block;
}
// Count how many bytes we've downloaded
let bytes_downloaded = Arc::new(AtomicUsize::new(0));
let mut total_reorg = 0;
// Collect all txns in blocks that we have a tx in. We'll fetch all these
// txs along with our own, so that the server doesn't learn which ones
// belong to us.
let all_new_txs = Arc::new(RwLock::new(vec![]));
// Create a new threadpool (upto 8, atleast 2 threads) to scan with
let pool = ThreadPool::new(max(2, min(8, num_cpus::get())));
// Create a single Tokio runtime for all block fetches to avoid per-batch overhead
let mut rt = tokio::runtime::Runtime::new()
.map_err(|e| format!("Error creating runtime: {:?}", e))?;
// Fetch CompactBlocks in increments
loop {
// Collect all block times, because we'll need to update transparent tx
// datetime via the block height timestamp
let block_times = Arc::new(RwLock::new(HashMap::new()));
let local_light_wallet = self.wallet.clone();
let local_bytes_downloaded = bytes_downloaded.clone();
let start_height = last_scanned_height + 1;
info!("Start height is {}", start_height);
// Show updates only if we're syncing a lot of blocks
if print_updates && (latest_block - start_height) > 100 {
print!("Syncing {}/{}\r", start_height, latest_block);
io::stdout().flush().ok().expect("Could not flush stdout");
}
{
let mut status = self.sync_status.write().unwrap();
status.is_syncing = true;
status.synced_blocks = start_height;
status.total_blocks = latest_block;
}
// Fetch compact blocks
info!("Fetching blocks {}-{}", start_height, end_height);
let all_txs = all_new_txs.clone();
let block_times_inner = block_times.clone();
let last_invalid_height = Arc::new(AtomicI32::new(0));
let last_invalid_height_inner = last_invalid_height.clone();
let tpool = pool.clone();
let fetch_result = fetch_blocks_with_runtime(&mut rt, &self.get_server_uri(), start_height, end_height, self.config.no_cert_verification, pool.clone(),
move |encoded_block: &[u8], height: u64| {
// Process the block only if there were no previous errors
if last_invalid_height_inner.load(Ordering::SeqCst) > 0 {
return;
}
// Parse the block and save it's time. We'll use this timestamp for
// transactions in this block that might belong to us.
let block: Result<zcash_client_backend::proto::compact_formats::CompactBlock, _>
= parse_from_bytes(encoded_block);
match block {
Ok(b) => {
block_times_inner.write().unwrap().insert(b.height, b.time);
},
Err(_) => {}
}
match local_light_wallet.read().unwrap().scan_block_with_pool(encoded_block, &tpool) {
Ok(block_txns) => {
// Add to global tx list
all_txs.write().unwrap().extend_from_slice(&block_txns.iter().map(|txid| (txid.clone(), height as i32)).collect::<Vec<_>>()[..]);
},
Err(invalid_height) => {
// Block at this height seems to be invalid, so invalidate up till that point
last_invalid_height_inner.store(invalid_height, Ordering::SeqCst);
}
};
local_bytes_downloaded.fetch_add(encoded_block.len(), Ordering::SeqCst);
});
if let Err(e) = fetch_result {
// Blocks streamed before the failure are already in the wallet and the retry resumes after them, so
// their transparent history has to be scanned later: keep the range and the times seen so far.
self.taddr_note_unscanned(start_height, &block_times);
return Err(e);
}
{
let t = self.wallet.read().unwrap();
let mut d = t.total_scan_duration.write().unwrap();
d.clear();
d.push(std::time::Duration::new(0, 0));
}
// Check if there was any invalid block, which means we might have to do a reorg
let invalid_height = last_invalid_height.load(Ordering::SeqCst);
if invalid_height > 0 {
total_reorg += self.wallet.read().unwrap().invalidate_block(invalid_height);
warn!("Invalidated block at height {}. Total reorg is now {}", invalid_height, total_reorg);
}
// Make sure we're not re-orging too much!
if total_reorg > (crate::lightwallet::MAX_REORG - 1) as u64 {
error!("Reorg has now exceeded {} blocks!", crate::lightwallet::MAX_REORG);
return Err(format!("Reorg has exceeded {} blocks. Aborting.", crate::lightwallet::MAX_REORG));
}
if invalid_height > 0 {
// Blocks [start_height, invalid_height) of this pass were scanned and are kept, but this pass skips
// its t-scan: owe it (the next pass folds it into its range).
self.taddr_note_unscanned(start_height, &block_times);
// Reset the scanning heights
last_scanned_height = (invalid_height - 1) as u64;
end_height = std::cmp::min(last_scanned_height + crate::lightwallet::MAX_REORG as u64 + 1, latest_block);
warn!("Reorg: reset scanning from {} to {}", last_scanned_height, end_height);
continue;
}
// If it got here, that means the blocks are scanning properly now.
// So, reset the total_reorg
total_reorg = 0;
// Fetch the txids our transparent addresses are involved with, from the start of this pass or of any
// range an earlier failed pass left un-t-scanned (taddress_stage). TLS verification follows the config
// (default on); `dangerous` is the only way to turn it off.
self.taddress_stage(start_height, end_height, &block_times, |s, e, bt| {
self.scan_taddress_txids(&pool, bt, s, e, self.config.no_cert_verification).map(|_| ())
})?;
// Do block height accounting
last_scanned_height = end_height;
end_height = last_scanned_height + scan_batch_size;
if last_scanned_height >= latest_block {
break;
} else if end_height > latest_block {
end_height = latest_block;
}
}
if print_updates{
println!(""); // New line to finish up the updates
}
info!("Synced to {}, Downloaded {} kB", latest_block, bytes_downloaded.load(Ordering::SeqCst) / 1024);
{
let mut status = self.sync_status.write().unwrap();
status.is_syncing = false;
status.synced_blocks = latest_block;
status.total_blocks = latest_block;
}
// Get the Raw transaction for all the wallet transactions
{
let decoy_txids = all_new_txs.read().unwrap();
match self.scan_fill_fulltxs(&mut rt, &pool, decoy_txids.to_vec()) {
Ok(_) => Ok(object!{
"result" => "success",
"latest_block" => latest_block,
"downloaded_bytes" => bytes_downloaded.load(Ordering::SeqCst)
}),
Err(e) => Err(format!("Error fetching all txns for memos: {}", e))
}
}
}
/// Start of the next t-address scan: the lower of this pass's first block and any range an earlier pass left
/// un-t-scanned. The saved block times are folded into `block_times` (entries already there win: after a
/// reorg they are the fresher ones). Never subtracts, so it cannot underflow.
fn taddr_scan_start(&self, pass_start: u64, block_times: &Arc<RwLock<HashMap<u64, u32>>>) -> u64 {
let pending = self.t_pending.lock().unwrap_or_else(|p| p.into_inner());
match pending.as_ref() {
None => pass_start,
Some(p) => {
let mut bt = block_times.write().unwrap();
for (h, t) in p.times.iter() {
bt.entry(*h).or_insert(*t);
}
min(p.start, pass_start)
}
}
}
/// Record that blocks from `start` on were block-scanned but not t-scanned. Merges with an earlier record
/// (lowest start; this pass's times win).
fn taddr_note_unscanned(&self, start: u64, block_times: &Arc<RwLock<HashMap<u64, u32>>>) {
let mut times = block_times.read().unwrap().clone();
let mut pending = self.t_pending.lock().unwrap_or_else(|p| p.into_inner());
let start = match pending.take() {
Some(old) => {
for (h, t) in old.times {
times.entry(h).or_insert(t);
}
min(old.start, start)
}
None => start,
};
warn!("t-address scan owed from height {} ({} block times kept); it will be retried", start, times.len());
*pending = Some(TaddrPending { start, times });
}
fn taddr_has_pending(&self) -> bool {
self.t_pending.lock().unwrap_or_else(|p| p.into_inner()).is_some()
}
/// The t-address stage of one pass: scans [min(pass_start, owed range), end_height] with `scan`. On Err the whole
/// range stays owed (and is kept with its block times), on Ok the debt is cleared. `scan` is a seam for tests.
fn taddress_stage<F>(&self, pass_start: u64, end_height: u64, block_times: &Arc<RwLock<HashMap<u64, u32>>>, scan: F)
-> Result<(), String>
where F: FnOnce(u64, u64, Arc<RwLock<HashMap<u64, u32>>>) -> Result<(), String>
{
let t_start = self.taddr_scan_start(pass_start, block_times);
if t_start > end_height {
// Nothing to scan (an owed range above a reorged-down tip): those blocks are fetched again and t-scanned then.
*self.t_pending.lock().unwrap_or_else(|p| p.into_inner()) = None;
return Ok(());
}
match scan(t_start, end_height, block_times.clone()) {
Ok(()) => {
*self.t_pending.lock().unwrap_or_else(|p| p.into_inner()) = None;
Ok(())
}
Err(e) => {
self.taddr_note_unscanned(t_start, block_times);
Err(e)
}
}
}
fn scan_taddress_txids(
&self,
pool: &ThreadPool,
block_times: Arc<RwLock<HashMap<u64, u32>>>,
start_height: u64,
end_height: u64,
no_cert: bool
) -> Result<Vec<()>, String> {
// The per-address job: fetch the address's txids and full-scan each tx (scan_full_tx).
self.scan_taddress_txids_with(pool, {
let wallet = self.wallet.clone();
let server_uri = self.get_server_uri();
move |address: String| -> Result<(), String> {
let wallet = wallet.clone();
let block_times = block_times.clone();
fetch_transparent_txids(
&server_uri,
address,
start_height,
end_height,
no_cert,
move |tx_bytes: &[u8], height: u64| {
let tx_result = Transaction::read(tx_bytes)
.map_err(|e| format!("Failed to read transaction: {:?}", e));
match tx_result {
Ok(tx) => {
let datetime_result = block_times.read()
.map_err(|e| format!("Failed to read block times: {:?}", e))
.and_then(|bt| bt.get(&height).cloned().ok_or_else(|| format!("No datetime for height: {}", height)));
match datetime_result {
Ok(datetime) => {
match wallet.read().map_err(|e| format!("Failed to read wallet: {:?}", e)) {
Ok(w) => {
w.scan_full_tx(&tx, height as i32, datetime as u64);
},
Err(e) => {
error!("Error reading wallet: {}", e);
},
}
},
Err(e) => {
error!("Error processing transaction: {}", e);
},
}
},
Err(e) => {
error!("Error reading transaction: {}", e);
},
}
}
)
}
})
}
// The t-address collector; `job` is the per-address work (a private seam, so a test can make a job panic). Every
// job sends exactly one result, a panicking one included, and only the jobs hold senders: the collector can
// neither wait forever on a job that died (with sync_lock held) nor report a partial scan as success.
fn scan_taddress_txids_with<F>(&self, pool: &ThreadPool, job: F) -> Result<Vec<()>, String>
where F: Fn(String) -> Result<(), String> + Send + Sync + 'static
{
let addresses = self.wallet.read()
.map_err(|e| format!("Failed to read wallet: {:?}", e))?
.get_all_taddresses().iter()
.cloned()
.collect::<Vec<String>>();
let job = Arc::new(job);
let (ctx, crx) = channel();
let num_addresses = addresses.len();
for address in addresses {
let address_clone = address.clone();
let ctx = ctx.clone();
let job = job.clone();
pool.execute(move || {
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (*job)(address)))
.unwrap_or_else(|_| Err("t-address scan job panicked".to_string()));
match ctx.send(r) {
Ok(_) => info!("Successfully sent data for address: {}", address_clone),
Err(e) => error!("Failed to send data for address: {}: {:?}", address_clone, e),
}
});
}
drop(ctx);
// Ends once every job has sent its result and dropped its sender.
let results: Vec<Result<(), String>> = crx.iter().collect();
if results.len() != num_addresses {
return Err(format!("t-address scan: {} of {} jobs reported", results.len(), num_addresses));
}
results.into_iter().collect()
}
fn scan_fill_fulltxs(&self, rt: &mut Runtime, pool: &ThreadPool, decoy_txids: Vec<(TxId, i32)>) -> Result<(), String> {
// We need to first copy over the Txids from the wallet struct, because
// we need to free the read lock from here (Because we'll self.wallet.txs later)
let own_txids: Vec<(TxId, i32)> = self.wallet.read().unwrap().txs.read().unwrap().values()
.filter(|wtx| wtx.full_tx_scanned == false)
.map(|wtx| (wtx.txid.clone(), wtx.block))
.collect::<Vec<(TxId, i32)>>();
info!("Fetching {} new txids, total {} with decoy", own_txids.len(), decoy_txids.len());
// Own txs and decoys go out as ONE (txid, height)-sorted, de-duplicated list, exactly as
// before, over one shared connection on the sync runtime. Request order, retries and the
// stop rule are identical for own txs and decoys, so the server cannot tell them apart.
let (items, own_set) = crate::fulltx_fetch::plan_fetch_list(own_txids, decoy_txids);
let policy = crate::fulltx_fetch::FetchPolicy::for_pool(pool);
let fetch = grpcconnector::make_shared_tx_fetcher(self.get_server_uri(), self.config.no_cert_verification);
let scan = crate::fulltx_fetch::wallet_scan_fn(self.wallet.clone());
crate::fulltx_fetch::run_fetch_scan(rt, items, own_set, policy, pool.clone(), fetch, scan)
}
pub fn do_send(&self, addrs: Vec<(&str, u64, Option<String>)>, fee: &u64) -> Result<String, String> {
if !self.wallet.read().unwrap().is_unlocked_for_spending() {
error!("Wallet is locked");
return Err("Wallet is locked".to_string());
}
info!("Creating transaction");
let result = {
let _lock = self.sync_lock.lock().unwrap();
self.wallet.write().unwrap().send_to_address(
u32::from_str_radix(&self.config.consensus_branch_id, 16).unwrap(),
&self.sapling_spend, &self.sapling_output,
false,
addrs,
fee,
|txbytes| broadcast_raw_tx(&self.get_server_uri(), self.config.no_cert_verification, txbytes)
)
};
result.map(|(txid, _)| txid)
}
}
#[cfg(test)]
pub mod tests {
use lazy_static::lazy_static;
use tempdir::TempDir;
use super::{LightClient, LightClientConfig};
lazy_static!{
static ref TEST_SEED: String = "youth strong sweet gorilla hammer unhappy congress stamp left stereo riot salute road tag clean toilet artefact fork certain leopard entire civil degree wonder".to_string();
}
// Every client here is on the "main" chain (the "test" chain, which LightClient::unconnected
// hard-codes, panics in base58_pubkey_address when the wallet is created) and lives in a temp
// data dir, so nothing is ever written to the real ~/.silentdragonxlite.
fn main_config(tmp: &TempDir) -> LightClientConfig {
LightClientConfig::create_unconnected("main".to_string(), tmp.path().to_str().map(|s| s.to_string()))
}
#[test]
pub fn test_encrypt_decrypt() {
let tmp = TempDir::new("lctest").unwrap();
let lc = LightClient::new_from_phrase(TEST_SEED.to_string(), &main_config(&tmp), 0, 0, false).unwrap();
assert!(!lc.do_export(None).is_err());
assert!(!lc.do_new_address("zs").is_err());
assert!(!lc.do_new_address("R").is_err());
assert_eq!(lc.do_seed_phrase().unwrap()["seed"], TEST_SEED.to_string());
// Encrypt and Lock the wallet
lc.wallet.write().unwrap().encrypt("password".to_string()).unwrap();
assert!(lc.do_export(None).is_err());
assert!(lc.do_seed_phrase().is_err());
assert!(lc.do_new_address("R").is_err());
assert!(lc.do_new_address("zs").is_err());
assert!(lc.do_send(vec![("zs", 0, None)], &10000).is_err());
// Do a unlock, and make sure it all works now
lc.wallet.write().unwrap().unlock("password".to_string()).unwrap();
assert!(!lc.do_export(None).is_err());
assert!(!lc.do_seed_phrase().is_err());
// Can't add keys while unlocked but encrypted
assert!(lc.do_new_address("R").is_err());
assert!(lc.do_new_address("zs").is_err());
// Remove encryption, which will allow adding
lc.wallet.write().unwrap().remove_encryption("password".to_string()).unwrap();
assert!(lc.do_new_address("R").is_ok());
assert!(lc.do_new_address("zs").is_ok());
}
#[test]
pub fn test_addresses() {
let tmp = TempDir::new("lctest").unwrap();
let lc = LightClient::new_from_phrase(TEST_SEED.to_string(), &main_config(&tmp), 0, 50, false).unwrap();
{
let addresses = lc.do_address();
// When restoring from seed with 50 extra z-addresses, there should be 50+1 z and 5+1 t addresses
assert_eq!(addresses["z_addresses"].len(), 51);
assert_eq!(addresses["t_addresses"].len(), 6);
}
// Add new z and t addresses
let taddr1 = lc.do_new_address("R").unwrap()[0].as_str().unwrap().to_string();
let taddr2 = lc.do_new_address("R").unwrap()[0].as_str().unwrap().to_string();
let zaddr1 = lc.do_new_address("zs").unwrap()[0].as_str().unwrap().to_string();
let zaddr2 = lc.do_new_address("zs").unwrap()[0].as_str().unwrap().to_string();
let addresses = lc.do_address();
assert_eq!(addresses["z_addresses"].len(), 53);
assert_eq!(addresses["z_addresses"][51], zaddr1);
assert_eq!(addresses["z_addresses"][52], zaddr2);
assert_eq!(addresses["t_addresses"].len(), 8);
assert_eq!(addresses["t_addresses"][6], taddr1);
assert_eq!(addresses["t_addresses"][7], taddr2);
use std::sync::{Arc, RwLock, Mutex};
use std::sync::atomic::AtomicBool;
use crate::lightclient::{WalletStatus, LightWallet};
// When creating a new wallet, there is only 1 address
let tmp2 = TempDir::new("lctest").unwrap();
let config = main_config(&tmp2);
let lc = LightClient {
wallet : Arc::new(RwLock::new(LightWallet::new(None, &config, 0, 0).unwrap())),
config : config,
sapling_output : std::borrow::Cow::Borrowed(&[]),
sapling_spend : std::borrow::Cow::Borrowed(&[]),
sync_lock : Mutex::new(()),
sync_status : Arc::new(RwLock::new(WalletStatus::new())),
shutdown_flag : Arc::new(AtomicBool::new(false)),
t_pending : Mutex::new(None),
};
{
let addresses = lc.do_address();
// New wallets have only 1 address
assert_eq!(addresses["z_addresses"].len(), 1);
assert_eq!(addresses["t_addresses"].len(), 1);
}
}
#[test]
pub fn test_bad_import() {
let tmp = TempDir::new("lctest").unwrap();
let lc = LightClient::new_from_phrase(TEST_SEED.to_string(), &main_config(&tmp), 0, 0, false).unwrap();
assert!(lc.do_import_sk("bad_priv_key".to_string(), 0).is_err());
assert!(lc.do_import_vk("bad_view_key".to_string(), 0).is_err());
}
#[test]
pub fn test_wallet_creation() {
// Create a new tmp director
{
let tmp = TempDir::new("lctest").unwrap();
let dir_name = tmp.path().to_str().map(|s| s.to_string());
// A lightclient to a new, empty directory works.
let config = LightClientConfig::create_unconnected("main".to_string(), dir_name);
let lc = LightClient::new(&config, 0).unwrap();
let seed = lc.do_seed_phrase().unwrap()["seed"].as_str().unwrap().to_string();
lc.do_save().unwrap();
// Doing another new will fail, because the wallet file now already exists
assert!(LightClient::new(&config, 0).is_err());
// new_from_phrase will not work either, again, because wallet file exists
assert!(LightClient::new_from_phrase(TEST_SEED.to_string(), &config, 0, 0, false).is_err());
// Creating a lightclient to the same dir without a seed should re-read the same wallet
// file and therefore the same seed phrase
let lc2 = LightClient::read_from_disk(&config).unwrap();
assert_eq!(seed, lc2.do_seed_phrase().unwrap()["seed"].as_str().unwrap().to_string());
}
// Now, get a new directory, and try to read from phrase
{
let tmp = TempDir::new("lctest").unwrap();
let dir_name = tmp.path().to_str().map(|s| s.to_string());
let config = LightClientConfig::create_unconnected("main".to_string(), dir_name);
// read_from_disk will fail, because the dir doesn't exist
assert!(LightClient::read_from_disk(&config).is_err());
// New from phrase should work becase a file doesn't exist already
let lc = LightClient::new_from_phrase(TEST_SEED.to_string(), &config, 0, 0, false).unwrap();
assert_eq!(TEST_SEED.to_string(), lc.do_seed_phrase().unwrap()["seed"].as_str().unwrap().to_string());
lc.do_save().unwrap();
// Now a new will fail because wallet exists
assert!(LightClient::new(&config, 0).is_err());
}
}
#[test]
pub fn test_recover_seed() {
// Create a new tmp director
{
let tmp = TempDir::new("lctest").unwrap();
let dir_name = tmp.path().to_str().map(|s| s.to_string());
// A lightclient to a new, empty directory works.
let config = LightClientConfig::create_unconnected("main".to_string(), dir_name);
let lc = LightClient::new(&config, 0).unwrap();
let seed = lc.do_seed_phrase().unwrap()["seed"].as_str().unwrap().to_string();
lc.do_save().unwrap();
assert_eq!(seed, LightClient::attempt_recover_seed(&config, None).unwrap());
// Now encrypt and save the file
let pwd = "password".to_string();
lc.wallet.write().unwrap().encrypt(pwd.clone()).unwrap();
assert_eq!(seed, LightClient::attempt_recover_seed(&config, Some(pwd)).unwrap());
}
}
// C243 tripwire: the t-address scan and the mempool monitor must take TLS verification from the config
// (no_cert_verification, which only `dangerous` sets), never from a hard-coded `true`. The needles are
// joined at runtime so that this test's own source cannot match them.
#[test]
pub fn test_tls_verification_not_hardcoded_off() {
let src = include_str!("lightclient.rs");
let taddr_needle = ["let no_cert", "= true"].join(" ");
let mempool_needle = ["monitor_mempool(&uri.clone(),", "true"].join(" ");
assert!(!src.contains(&taddr_needle), "the t-address scan turns TLS verification off");
assert!(!src.contains(&mempool_needle), "the mempool monitor turns TLS verification off");
}
// U2: clear_state/clear_state_from serialize with a running sync (sync_lock) and hold no wallet guard
// while they wait for it (lock order sync_lock -> wallet, as in do_save/do_sync). No test joins a thread
// that could be deadlocked: they wait on channels with timeouts.
fn shared_client(tmp: &TempDir) -> std::sync::Arc<LightClient> {
std::sync::Arc::new(LightClient::new_from_phrase(TEST_SEED.to_string(), &main_config(tmp), 0, 0, false).unwrap())
}
#[test]
pub fn test_clear_state_waits_for_sync_lock() {
use std::sync::mpsc::{channel, RecvTimeoutError};
use std::time::Duration;
let tmp = TempDir::new("lctest").unwrap();
let lc = shared_client(&tmp);
let sync = lc.sync_lock.lock().unwrap();
let (tx, rx) = channel();
let lc2 = lc.clone();
let t = std::thread::spawn(move || { lc2.clear_state_from(0); let _ = tx.send(()); });
let early = rx.recv_timeout(Duration::from_millis(300));
drop(sync);
assert_eq!(early, Err(RecvTimeoutError::Timeout), "clear_state_from ran while a sync held sync_lock");
assert!(rx.recv_timeout(Duration::from_secs(5)).is_ok(), "clear_state_from did not finish after the sync ended");
t.join().unwrap();
}
#[test]
pub fn test_clear_state_holds_no_wallet_guard_while_waiting() {
use std::sync::mpsc::channel;
use std::time::Duration;
let tmp = TempDir::new("lctest").unwrap();
let lc = shared_client(&tmp);
let sync = lc.sync_lock.lock().unwrap();
let (btx, brx) = channel();
let lc_b = lc.clone();
let b = std::thread::spawn(move || { lc_b.clear_state(); let _ = btx.send(()); });
std::thread::sleep(Duration::from_millis(200));
let (wtx, wrx) = channel();
let lc_w = lc.clone();
let w = std::thread::spawn(move || { let _g = lc_w.wallet.write().unwrap(); let _ = wtx.send(()); });
let writer_ok = wrx.recv_timeout(Duration::from_secs(2)).is_ok();
let b_early = brx.try_recv().is_ok();
drop(sync);
assert!(writer_ok, "clear_state held a wallet read guard while it waited for sync_lock (a concurrent save deadlocks)");
assert!(!b_early, "clear_state finished while a sync held sync_lock");
assert!(brx.recv_timeout(Duration::from_secs(5)).is_ok(), "clear_state did not finish after the sync ended");
b.join().unwrap();
w.join().unwrap();
}
#[test]
pub fn test_clear_state_does_not_leak_sync_lock() {
let tmp = TempDir::new("lctest").unwrap();
let lc = shared_client(&tmp);
lc.clear_state();
assert!(lc.sync_lock.try_lock().is_ok(), "clear_state left sync_lock held");
lc.clear_state_from(0);
assert!(lc.sync_lock.try_lock().is_ok(), "clear_state_from left sync_lock held");
}
// Regression guard only (the race is timing-dependent, so it is no fails-before proof): before the birthday
// hoist, clear_state held a wallet read guard across clear_state_from, which read-locks the wallet again; a
// writer queued in between deadlocks a writer-preferring RwLock (Linux futex, Windows SRW).
#[test]
pub fn test_clear_state_no_recursive_read_deadlock() {
use std::sync::mpsc::channel;
use std::time::{Duration, Instant};
let tmp = TempDir::new("lctest").unwrap();
let lc = shared_client(&tmp);
let (tx, rx) = channel();
let (lc_r, tx_r) = (lc.clone(), tx.clone());
let r = std::thread::spawn(move || { for _ in 0..200 { lc_r.clear_state(); } let _ = tx_r.send(()); });
let lc_w = lc.clone();
let w = std::thread::spawn(move || { for _ in 0..200 { let _g = lc_w.wallet.write().unwrap(); } let _ = tx.send(()); });
let deadline = Instant::now() + Duration::from_secs(20);
for _ in 0..2 {
let left = deadline.saturating_duration_since(Instant::now());
assert!(rx.recv_timeout(left).is_ok(), "clear_state and a wallet writer deadlocked (20 s watchdog)");
}
r.join().unwrap();
w.join().unwrap();
}
// C251 (s2): a WIF that cannot be decoded returns Err and leaves the wallet lock usable. Before, import_taddr
// panicked under the wallet write guard (bs58 unwrap / hex slice), which poisoned the wallet until restart.
#[test]
pub fn test_bad_import_does_not_poison() {
use std::panic::{catch_unwind, AssertUnwindSafe};
let tmp = TempDir::new("lctest").unwrap();
let lc = LightClient::new_from_phrase(TEST_SEED.to_string(), &main_config(&tmp), 0, 0, false).unwrap();
let before = lc.do_address();
for bad in &["zs1notawif", "secret-extended-key-main1qqqqqqqqqq", "", "1"] {
let r = catch_unwind(AssertUnwindSafe(|| lc.do_import_tk(bad.to_string(), 0)));
let e = match r {
Ok(Err(e)) => e,
Ok(Ok(a)) => panic!("{:?} imported as {}", bad, a),
Err(_) => panic!("do_import_tk({:?}) panicked", bad),
};
assert!(e.contains("Couldn't decode the transparent private key"), "{:?}: {}", bad, e);
assert!(bad.len() < 2 || !e.contains(bad), "the error echoes the key: {}", e);
// separate statements: a read guard alive in the same expression would deadlock the write()
assert!(lc.wallet.read().is_ok(), "the wallet lock is poisoned");
assert!(lc.wallet.write().is_ok(), "the wallet lock is poisoned");
}
assert_eq!(lc.do_address(), before);
}
// C252 (s1 test 8): a private-key import into an encrypted wallet is refused with the "encrypted" reason
// whether the wallet is locked or unlocked (never "Wallet is locked", which would send the user to unlock and
// retry), and it leaves the wallet able to lock, save and show its real seed. Viewing keys still import.
#[test]
pub fn test_encrypted_import_is_clean() {
let tmp = TempDir::new("lctest").unwrap();
let lc = LightClient::new_from_phrase(TEST_SEED.to_string(), &main_config(&tmp), 0, 0, false).unwrap();
let tmp2 = TempDir::new("lctest").unwrap();
let other = LightClient::new(&main_config(&tmp2), 0).unwrap();
let (wif, sk, vk) = {
let w = other.wallet.read().unwrap();
let z = w.get_z_private_keys()[0].clone();
(w.get_t_secret_keys()[0].1.clone(), z.1, z.2)
};
// encrypt() also locks the wallet
lc.wallet.write().unwrap().encrypt("password".to_string()).unwrap();
for &unlocked in &[false, true] {
if unlocked {
lc.wallet.write().unwrap().unlock("password".to_string()).unwrap();
}
for r in vec![lc.do_import_sk(sk.clone(), 0), lc.do_import_tk(wif.clone(), 0), lc.do_import_key(sk.clone(), 0)] {
let e = r.err().expect("a private-key import must fail while the wallet is encrypted");
assert!(e.starts_with("Can't import a private key while the wallet is encrypted"), "{}", e);
assert!(!e.contains("locked") && !e.contains("decode"), "{}", e);
assert!(!e.contains(&wif) && !e.contains(&sk), "the error echoes the key");
}
assert_eq!(lc.wallet.read().unwrap().is_unlocked_for_spending(), unlocked);
}
// A viewing key still imports while encrypted (its save locks the wallet again).
assert!(lc.do_import_vk(vk, 0).is_ok());
assert!(!lc.wallet.read().unwrap().is_unlocked_for_spending());
assert!(lc.do_save().is_ok());
lc.wallet.write().unwrap().unlock("password".to_string()).unwrap();
assert_eq!(lc.do_seed_phrase().unwrap()["seed"], TEST_SEED.to_string());
}
// C245 Fix 2 (s2-D6a seam). A t-address job that panicked left the collector waiting forever, with sync_lock
// held, for a result that never came: the original sender stayed alive. Each job now reports exactly once (a
// panic is an Err) and the collector counts the reports. Runs under the 15 s watchdog.
#[test]
pub fn test_taddress_scan_job_panic_is_error() {
let (n, all_ok, one_err, one_panic) = crate::lightwallet::tests::within_watchdog("t-address scan", || {
let tmp = TempDir::new("lctest").unwrap();
let lc = LightClient::new(&main_config(&tmp), 0).unwrap();
lc.do_new_address("R").unwrap();
lc.do_new_address("R").unwrap();
let addrs = lc.wallet.read().unwrap().get_all_taddresses();
let pool = threadpool::ThreadPool::new(2);
let all_ok = lc.scan_taddress_txids_with(&pool, |_a: String| -> Result<(), String> { Ok(()) });
let a1 = addrs[1].clone();
let one_err = lc.scan_taddress_txids_with(&pool, move |a: String| -> Result<(), String> {
if a == a1 { Err("injected error".to_string()) } else { Ok(()) }
});
let a1 = addrs[1].clone();
let one_panic = lc.scan_taddress_txids_with(&pool, move |a: String| -> Result<(), String> {
if a == a1 {
panic!("injected t-address job panic");
}
Ok(())
});
(addrs.len(), all_ok, one_err, one_panic)
});
assert_eq!(n, 3);
assert_eq!(all_ok, Ok(vec![(); 3]));
assert_eq!(one_err, Err("injected error".to_string()));
assert_eq!(one_panic, Err("t-address scan job panicked".to_string()));
}
// C242: the extracted t-address scan seam. These drive taddress_stage / taddr_note_unscanned / taddr_scan_start
// directly (the gRPC sync path can't run offline), covering the three original defects: no remembered range,
// discarded block times, and the wrapping rewind subtraction.
type Bt = std::sync::Arc<std::sync::RwLock<std::collections::HashMap<u64, u32>>>;
fn bt_from(pairs: &[(u64, u32)]) -> Bt {
std::sync::Arc::new(std::sync::RwLock::new(pairs.iter().cloned().collect()))
}
#[test]
pub fn test_taddr_stage_failure_keeps_range_and_times() {
let tmp = TempDir::new("lctest").unwrap();
let lc = LightClient::new(&main_config(&tmp), 0).unwrap();
// Pass 1: block times 101..=200, t-scan fails -> the whole range stays owed with its times.
let bt1: Bt = std::sync::Arc::new(std::sync::RwLock::new(
(101..=200u64).map(|h| (h, h as u32)).collect()));
let r = lc.taddress_stage(101, 200, &bt1, |_s, _e, _bt| Err("boom".to_string()));
assert_eq!(r, Err("boom".to_string()));
assert!(lc.taddr_has_pending());
// Pass 2: new block times 201..=300, t-scan succeeds; it must cover [101, 300] with both passes' times.
let bt2: Bt = std::sync::Arc::new(std::sync::RwLock::new(
(201..=300u64).map(|h| (h, h as u32)).collect()));
let seen = std::sync::Arc::new(std::sync::Mutex::new(None));
let seen_c = seen.clone();
let r = lc.taddress_stage(201, 300, &bt2, move |s, e, bt| {
let m = bt.read().unwrap();
*seen_c.lock().unwrap() = Some((s, e, m.contains_key(&150), m.contains_key(&250)));
Ok(())
});
assert_eq!(r, Ok(()));
let (s, e, has150, has250) = seen.lock().unwrap().unwrap();
assert_eq!(s, 101);
assert_eq!(e, 300);
assert!(has150, "the earlier pass's block time must be folded in");
assert!(has250, "the current pass's block time must be present");
assert!(!lc.taddr_has_pending(), "success clears the debt");
}
#[test]
pub fn test_taddr_stage_no_underflow_below_batch_size() {
let tmp = TempDir::new("lctest").unwrap();
let lc = LightClient::new(&main_config(&tmp), 0).unwrap();
// An owed range below the pass start pulls the scan start down to the owed (lower) height.
lc.taddr_note_unscanned(3, &bt_from(&[(3, 3)]));
let got = std::sync::Arc::new(std::sync::Mutex::new(u64::MAX));
let got_c = got.clone();
lc.taddress_stage(10, 1000, &bt_from(&[(10, 10)]), move |s, _e, _bt| {
*got_c.lock().unwrap() = s;
Ok(())
}).unwrap();
assert_eq!(*got.lock().unwrap(), 3);
// No debt: the scan starts exactly at the pass start. For every start in [0, 25000) the old
// `start_height - scan_batch_size` formula wrapped to ~1.8e19; the fix never subtracts.
for ps in 0u64..=24_999 {
let got = std::sync::Arc::new(std::sync::Mutex::new(u64::MAX));
let got_c = got.clone();
lc.taddress_stage(ps, ps + 1, &bt_from(&[(ps, 1)]), move |s, _e, _bt| {
*got_c.lock().unwrap() = s;
Ok(())
}).unwrap();
let s = *got.lock().unwrap();
assert!(s <= ps, "scan start {} must never exceed pass start {}", s, ps);
assert_eq!(s, ps);
}
}
#[test]
pub fn test_taddr_stage_new_times_win_after_reorg() {
let tmp = TempDir::new("lctest").unwrap();
let lc = LightClient::new(&main_config(&tmp), 0).unwrap();
// Owed from a pass that saw height 150 at time 111; the new pass sees 150 at 222 (a reorg re-dated it).
lc.taddr_note_unscanned(150, &bt_from(&[(150, 111)]));
let seen = std::sync::Arc::new(std::sync::Mutex::new(0u32));
let seen_c = seen.clone();
lc.taddress_stage(150, 300, &bt_from(&[(150, 222)]), move |_s, _e, bt| {
*seen_c.lock().unwrap() = *bt.read().unwrap().get(&150).unwrap();
Ok(())
}).unwrap();
assert_eq!(*seen.lock().unwrap(), 222, "the current pass's fresher time must win");
}
#[test]
pub fn test_taddr_note_unscanned_merges() {
let tmp = TempDir::new("lctest").unwrap();
let lc = LightClient::new(&main_config(&tmp), 0).unwrap();
lc.taddr_note_unscanned(100, &bt_from(&[(100, 1)]));
lc.taddr_note_unscanned(300, &bt_from(&[(300, 3)]));
// taddr_scan_start folds the owed times into its argument without clearing the debt.
let probe = bt_from(&[]);
assert_eq!(lc.taddr_scan_start(10_000, &probe), 100, "the merged start is the lowest seen");
assert!(probe.read().unwrap().contains_key(&100));
assert!(probe.read().unwrap().contains_key(&300));
lc.taddr_note_unscanned(50, &bt_from(&[(50, 5)]));
let probe2 = bt_from(&[]);
assert_eq!(lc.taddr_scan_start(10_000, &probe2), 50, "a lower note lowers the start");
assert!(lc.taddr_has_pending());
}
#[test]
pub fn test_taddr_stage_empty_range_clears() {
let tmp = TempDir::new("lctest").unwrap();
let lc = LightClient::new(&main_config(&tmp), 0).unwrap();
lc.taddr_note_unscanned(500, &bt_from(&[(500, 1)]));
let called = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let called_c = called.clone();
// pass_start 11 -> t_start 11 > end 10: nothing to scan, the closure must not run and the debt clears.
let r = lc.taddress_stage(11, 10, &bt_from(&[]), move |_s, _e, _bt| {
called_c.store(true, std::sync::atomic::Ordering::SeqCst);
Ok(())
});
assert_eq!(r, Ok(()));
assert!(!called.load(std::sync::atomic::Ordering::SeqCst), "scan must not run for an empty range");
assert!(!lc.taddr_has_pending());
}
#[test]
pub fn test_clear_state_clears_taddr_debt() {
let tmp = TempDir::new("lctest").unwrap();
let lc = LightClient::new(&main_config(&tmp), 0).unwrap();
lc.taddr_note_unscanned(100, &bt_from(&[(100, 1)]));
assert!(lc.taddr_has_pending());
lc.clear_state();
assert!(!lc.taddr_has_pending(), "a rescan / clear_state cancels the owed t-scan");
}
// C254 tests (s1 tests 9-14). Every mode is set explicitly before it is checked, never left to the umask.
#[cfg(unix)]
fn mode(p: &std::path::Path) -> u32 {
use std::os::unix::fs::PermissionsExt;
std::fs::symlink_metadata(p).unwrap().permissions().mode() & 0o7777
}
#[cfg(unix)]
fn chmod(p: &std::path::Path, m: u32) {
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(p, std::fs::Permissions::from_mode(m)).unwrap();
}
// s1 test 9: the wallet and its .bak are owner-only (0600) after every save and after an open, whatever their
// earlier mode, and a wallet restored from a loose .bak is owner-only too. The caller's data dir is never chmodded.
#[cfg(unix)]
#[test]
pub fn test_wallet_files_private() {
let tmp = TempDir::new("lctest").unwrap();
chmod(tmp.path(), 0o755);
let config = main_config(&tmp);
let wallet = config.get_wallet_path().into_path_buf();
let bak = wallet.with_extension("dat.bak");
let lc = LightClient::new_from_phrase(TEST_SEED.to_string(), &config, 0, 0, false).unwrap();
lc.do_save().unwrap();
assert!(bak.exists());
for save in 1..=2 {
chmod(&wallet, 0o644);
chmod(&bak, 0o644);
lc.do_save().unwrap();
assert_eq!(mode(&wallet), 0o600, "the wallet after save {}", save);
assert_eq!(mode(&bak), 0o600, "the .bak after save {}", save);
}
drop(lc);
// An open heals both files before any save.
chmod(&wallet, 0o644);
chmod(&bak, 0o644);
let lc = LightClient::read_from_disk(&config).unwrap();
assert_eq!(mode(&wallet), 0o600, "the wallet after an open");
assert_eq!(mode(&bak), 0o600, "the .bak after an open");
drop(lc);
// A missing wallet restored from a loose .bak is owner-only.
std::fs::remove_file(&wallet).unwrap();
chmod(&bak, 0o644);
let lc = LightClient::read_from_disk(&config).unwrap();
assert_eq!(lc.do_seed_phrase().unwrap()["seed"], TEST_SEED.to_string());
assert_eq!(mode(&wallet), 0o600, "the wallet restored from the .bak");
assert!(!wallet.with_extension("dat.tmp").exists());
assert_eq!(mode(tmp.path()), 0o755, "a caller-supplied data dir is never chmodded");
}
// s1 test 10 (D9): a crash-leftover 0644 .dat.tmp is unlinked, not reused. The save writes a fresh 0600 inode, so
// an fd opened on the stale tmp earlier (another account's) never sees the new wallet.
#[cfg(unix)]
#[test]
pub fn test_stale_tmp_replaced() {
use std::io::Read;
use std::os::unix::fs::MetadataExt;
let tmp = TempDir::new("lctest").unwrap();
let config = main_config(&tmp);
let wallet = config.get_wallet_path().into_path_buf();
let tmp_path = wallet.with_extension("dat.tmp");
let lc = LightClient::new_from_phrase(TEST_SEED.to_string(), &config, 0, 0, false).unwrap();
std::fs::write(&tmp_path, b"stale").unwrap();
chmod(&tmp_path, 0o644);
let mut held = std::fs::File::open(&tmp_path).unwrap();
let stale_ino = held.metadata().unwrap().ino();
lc.do_save().unwrap();
assert!(!tmp_path.exists(), "a .dat.tmp is left behind");
assert_eq!(mode(&wallet), 0o600);
assert_ne!(std::fs::metadata(&wallet).unwrap().ino(), stale_ino, "the stale tmp's inode was reused");
let mut seen = Vec::new();
held.read_to_end(&mut seen).unwrap();
assert_eq!(seen, b"stale".to_vec(), "the stale tmp's fd sees the new wallet");
drop(lc);
let lc = LightClient::read_from_disk(&config).unwrap();
assert_eq!(lc.do_seed_phrase().unwrap()["seed"], TEST_SEED.to_string());
}
// s1 test 11: the helpers' semantics.
#[cfg(unix)]
#[test]
pub fn test_tighten_semantics() {
use std::os::unix::fs::MetadataExt;
use super::{tighten, create_private_dir_all, create_private_file_new};
let tmp = TempDir::new("lctest").unwrap();
let t = tmp.path();
// Only the group/other bits go; the owner bits stay as they were (none is added).
for &(from, to) in &[(0o755, 0o700), (0o777, 0o700), (0o750, 0o700), (0o700, 0o700), (0o555, 0o500), (0o500, 0o500)] {
let d = t.join(format!("dir{:o}", from));
std::fs::create_dir(&d).unwrap();
chmod(&d, from);
tighten(&d, false);
assert_eq!(mode(&d), from, "tighten(dir {:o}, want_dir=false) changed it", from);
tighten(&d, true);
assert_eq!(mode(&d), to, "dir {:o}", from);
}
for &(from, to) in &[(0o644, 0o600), (0o666, 0o600), (0o640, 0o600), (0o600, 0o600), (0o444, 0o400), (0o400, 0o400)] {
let f = t.join(format!("file{:o}", from));
std::fs::write(&f, b"x").unwrap();
chmod(&f, from);
tighten(&f, true);
assert_eq!(mode(&f), from, "tighten(file {:o}, want_dir=true) changed it", from);
tighten(&f, false);
assert_eq!(mode(&f), to, "file {:o}", from);
}
tighten(&t.join("missing"), true);
tighten(&t.join("missing"), false);
// A symlink is never followed.
let target_dir = t.join("target-dir");
std::fs::create_dir(&target_dir).unwrap();
chmod(&target_dir, 0o755);
let target_file = t.join("target-file");
std::fs::write(&target_file, b"x").unwrap();
chmod(&target_file, 0o644);
std::os::unix::fs::symlink(&target_dir, t.join("link-dir")).unwrap();
std::os::unix::fs::symlink(&target_file, t.join("link-file")).unwrap();
tighten(&t.join("link-dir"), true);
tighten(&t.join("link-file"), false);
assert_eq!(mode(&target_dir), 0o755, "tighten followed a dir symlink");
assert_eq!(mode(&target_file), 0o644, "tighten followed a file symlink");
// create_private_dir_all: every created component is 0700; an existing dir is accepted as it is.
create_private_dir_all(&t.join("a").join("b").join("c")).unwrap();
for p in &[t.join("a"), t.join("a").join("b"), t.join("a").join("b").join("c")] {
assert_eq!(mode(p), 0o700, "{:?}", p);
}
create_private_dir_all(&target_dir).unwrap();
assert_eq!(mode(&target_dir), 0o755);
// create_private_file_new: a fresh 0600 inode; a stale file or a symlink at the path is unlinked, never
// reused or followed; a directory at the path is an error and stays.
let fresh = t.join("fresh.tmp");
create_private_file_new(&fresh).unwrap();
assert_eq!(mode(&fresh), 0o600);
let stale = t.join("stale.tmp");
std::fs::write(&stale, b"stale").unwrap();
chmod(&stale, 0o644);
let stale_ino = std::fs::metadata(&stale).unwrap().ino();
let _held = std::fs::File::open(&stale).unwrap();
create_private_file_new(&stale).unwrap();
assert_eq!(mode(&stale), 0o600);
assert_eq!(std::fs::read(&stale).unwrap().len(), 0);
assert_ne!(std::fs::metadata(&stale).unwrap().ino(), stale_ino);
let via_link = t.join("via-link.tmp");
std::os::unix::fs::symlink(&target_file, &via_link).unwrap();
create_private_file_new(&via_link).unwrap();
assert!(std::fs::symlink_metadata(&via_link).unwrap().is_file(), "the symlink was followed");
assert_eq!(mode(&via_link), 0o600);
assert_eq!(std::fs::read(&target_file).unwrap(), b"x".to_vec());
assert_eq!(mode(&target_file), 0o644);
assert!(create_private_file_new(&target_dir).is_err());
assert!(target_dir.is_dir());
// A caller-supplied data_dir is never chmodded.
chmod(t, 0o755);
let config = main_config(&tmp);
assert_eq!(&*config.get_zcash_data_path(), t);
assert_eq!(mode(t), 0o755, "a caller-supplied data dir was chmodded");
}
// s1 test 12 (D6): the first save after encrypt() backs up the new, encrypted file, so no plaintext seed survives
// in .bak; that .bak is a full copy that unlocks to the seed. Later saves back up the encrypted wallet too.
#[test]
pub fn test_bak_not_plaintext_after_encrypt() {
use crate::lightwallet::LightWallet;
use super::wallet_header_encrypted;
let tmp = TempDir::new("lctest").unwrap();
let config = main_config(&tmp);
let wallet = config.get_wallet_path().into_path_buf();
let bak = wallet.with_extension("dat.bak");
let header = |p: &std::path::Path| {
let b = std::fs::read(p).unwrap();
(b[..8].to_vec(), b[8])
};
let v9 = 9u64.to_le_bytes().to_vec();
let lc = LightClient::new_from_phrase(TEST_SEED.to_string(), &config, 0, 0, false).unwrap();
lc.do_save().unwrap();
assert_eq!(header(&bak), (v9.clone(), 0), "precondition: a plaintext v9 .bak");
assert_eq!(wallet_header_encrypted(&wallet), Some(false));
lc.wallet.write().unwrap().encrypt("password".to_string()).unwrap();
lc.do_save().unwrap();
assert_eq!(header(&wallet), (v9.clone(), 1), "the wallet is saved encrypted, as v9");
assert_eq!(header(&bak), (v9.clone(), 1), "the .bak still holds the plaintext wallet after the encrypt save");
assert_eq!(wallet_header_encrypted(&bak), Some(true));
assert_eq!(std::fs::read(&bak).unwrap(), std::fs::read(&wallet).unwrap());
let mut from_bak = LightWallet::read(std::io::BufReader::new(std::fs::File::open(&bak).unwrap()), &config).unwrap();
assert!(from_bak.is_encrypted());
from_bak.unlock("password".to_string()).unwrap();
assert_eq!(from_bak.get_seed_phrase(), TEST_SEED.to_string());
lc.do_save().unwrap();
assert_eq!(header(&bak).1, 1, "a later save backs up the encrypted wallet");
// Removing encryption is not the transition: its save backs up the encrypted wallet as before.
lc.wallet.write().unwrap().remove_encryption("password".to_string()).unwrap();
lc.do_save().unwrap();
assert_eq!(header(&wallet).1, 0, "the wallet is saved decrypted");
assert_eq!(header(&bak).1, 1);
}
// X3: wallet_header_encrypted reads the flag only where LightWallet::read has one at byte 8 (v4 and v6-v9).
#[test]
pub fn test_wallet_header_encrypted_probe() {
use super::wallet_header_encrypted;
let tmp = TempDir::new("lctest").unwrap();
let p = tmp.path().join("w.dat");
let probe = |v: u64, rest: &[u8]| {
let mut b = v.to_le_bytes().to_vec();
b.extend_from_slice(rest);
std::fs::write(&p, &b).unwrap();
wallet_header_encrypted(&p)
};
for &v in &[4u64, 6, 7, 8, 9] {
assert_eq!(probe(v, &[0, 7, 7]), Some(false), "v{}", v);
assert_eq!(probe(v, &[1]), Some(true), "v{}", v);
assert_eq!(probe(v, &[2]), Some(true), "v{}: any non-zero flag, as LightWallet::read", v);
assert_eq!(probe(v, &[]), None, "v{} without a flag byte", v);
}
for &v in &[0u64, 1, 2, 3, 5, 10, u64::MAX] {
assert_eq!(probe(v, &[0]), None, "v{}", v);
assert_eq!(probe(v, &[1]), None, "v{}", v);
}
std::fs::write(&p, b"").unwrap();
assert_eq!(wallet_header_encrypted(&p), None, "an empty file");
assert_eq!(wallet_header_encrypted(&tmp.path().join("missing.dat")), None, "a missing file");
assert_eq!(wallet_header_encrypted(tmp.path()), None, "a directory");
}
// s1 test 13 (D7, D11): get_log_config() creates the log owner-only and tightens an existing one; the roller's
// pattern lies in the data dir, so a roll renames the log inside it; a data path containing "{}" keeps the old
// relative pattern (FixedWindowRoller replaces every "{}").
#[cfg(unix)]
#[test]
pub fn test_log_paths() {
use log4rs::append::rolling_file::policy::compound::roll::{Roll, fixed_window::FixedWindowRoller};
let tmp = TempDir::new("lctest").unwrap();
let config = main_config(&tmp);
let log = config.get_log_path().into_path_buf();
assert!(!log.exists());
config.get_log_config().unwrap();
assert_eq!(mode(&log), 0o600, "a new log");
chmod(&log, 0o644);
config.get_log_config().unwrap();
assert_eq!(mode(&log), 0o600, "an existing loose log");
let pattern = config.log_roll_pattern();
assert_eq!(pattern, tmp.path().join("zecwallet-light-wallet-log{}").to_str().unwrap());
FixedWindowRoller::builder().build(&pattern, 3).unwrap().roll(&log).unwrap();
assert!(!log.exists());
assert_eq!(mode(&tmp.path().join("zecwallet-light-wallet-log0")), 0o600, "the rolled log is not in the data dir");
let odd = tmp.path().join("we{}ird");
let odd_config = LightClientConfig::create_unconnected("main".to_string(), odd.to_str().map(|s| s.to_string()));
assert_eq!(odd_config.log_roll_pattern(), "zecwallet-light-wallet-log{}");
odd_config.get_log_config().unwrap();
assert_eq!(mode(&odd_config.get_log_path()), 0o600);
}
// s1 test 14, opt-in: the default data dir (no data_dir) is created 0700, a pre-made 0755 one becomes 0700 (for
// test/regtest also its parent), and HOME, the params dir and a symlinked data dir are left alone. HOME points at
// a fresh temp dir for the test's own duration, so no real ~/.silentdragonxlite is ever seen; still, run it alone
// and only through rtest.sh (scratch HOME):
// DRAGONX_LITE_SCRATCH_HOME=1 rtest.sh --lib --offline test_default_data_dir_private -- --ignored
#[cfg(all(unix, not(target_os = "macos")))]
#[test]
#[ignore]
pub fn test_default_data_dir_private() {
if std::env::var("DRAGONX_LITE_SCRATCH_HOME").ok().as_deref() != Some("1") {
eprintln!("test_default_data_dir_private: needs DRAGONX_LITE_SCRATCH_HOME=1 (through rtest.sh); skipped");
return;
}
let home = TempDir::new("lchome").unwrap();
chmod(home.path(), 0o755);
let saved_home = std::env::var_os("HOME");
std::env::set_var("HOME", home.path());
let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let h = home.path();
let data = h.join(".silentdragonxlite");
let main = LightClientConfig::create_unconnected("main".to_string(), None);
assert!(!data.exists());
assert!(!main.wallet_exists());
assert_eq!(&*main.get_zcash_data_path(), data.as_path());
assert_eq!(mode(&data), 0o700, "a new default data dir");
chmod(&data, 0o755);
assert!(!main.wallet_exists());
assert_eq!(mode(&data), 0o700, "a pre-made 0755 data dir");
chmod(&data, 0o755);
let test = LightClientConfig::create_unconnected("test".to_string(), None);
assert!(!test.wallet_exists());
assert_eq!(mode(&data.join("testnet3")), 0o700, "a new testnet3 dir");
assert_eq!(mode(&data), 0o700, "the parent of testnet3");
let params = h.join(".zcash-params");
std::fs::create_dir(&params).unwrap();
chmod(&params, 0o755);
main.get_zcash_params_path().unwrap();
assert_eq!(mode(&params), 0o755, "the params dir was chmodded");
assert_eq!(mode(h), 0o755, "HOME was chmodded");
let h2 = h.join("h2");
let real = h.join("real");
std::fs::create_dir(&h2).unwrap();
std::fs::create_dir(&real).unwrap();
chmod(&real, 0o755);
std::os::unix::fs::symlink(&real, h2.join(".silentdragonxlite")).unwrap();
std::env::set_var("HOME", &h2);
assert!(!main.wallet_exists());
assert_eq!(mode(&real), 0o755, "a symlinked data dir was followed");
}));
match saved_home {
Some(v) => std::env::set_var("HOME", v),
None => std::env::remove_var("HOME"),
}
if let Err(e) = r {
std::panic::resume_unwind(e);
}
}
// C244 (s2 tests 1-3, the X5 test for s2-D12 and the s2-D13 no-overwrite test): read_from_disk falls back to the
// .bak only for a corrupt wallet, keeps the damaged file, and changes nothing in every other case.
fn corrupt_files(dir: &std::path::Path) -> Vec<std::path::PathBuf> {
let mut v: Vec<std::path::PathBuf> = std::fs::read_dir(dir).unwrap().map(|e| e.unwrap().path())
.filter(|p| p.file_name().unwrap().to_string_lossy().contains(".dat.corrupt-")).collect();
v.sort();
v
}
// A saved wallet and its .bak (the second save makes the .bak).
fn saved_pair(tmp: &TempDir) -> (LightClientConfig, std::path::PathBuf, std::path::PathBuf) {
let config = main_config(tmp);
let wallet = config.get_wallet_path().into_path_buf();
let bak = wallet.with_extension("dat.bak");
let lc = LightClient::new_from_phrase(TEST_SEED.to_string(), &config, 0, 0, false).unwrap();
lc.do_save().unwrap();
lc.do_save().unwrap();
assert!(bak.exists());
(config, wallet, bak)
}
// Version 5 selects the gzip path, and 0xAB is no gzip header: Decoder::new(..).unwrap() (LW:323) panicked on it.
fn gzip_garbage() -> Vec<u8> {
let mut b = 5u64.to_le_bytes().to_vec();
b.extend_from_slice(&[0xAB; 32]);
b
}
fn read_err(config: &LightClientConfig) -> std::io::Error {
match LightClient::read_from_disk(config) {
Ok(_) => panic!("the wallet opened"),
Err(e) => e,
}
}
// s2 C244 test 1: a parse that panicked recovers from the .bak, and the damaged file is kept as .dat.corrupt-<ts>.
#[test]
pub fn test_read_from_disk_recovers_when_parse_would_panic() {
let tmp = TempDir::new("lctest").unwrap();
let (config, wallet, bak) = saved_pair(&tmp);
let bak_bytes = std::fs::read(&bak).unwrap();
std::fs::write(&wallet, gzip_garbage()).unwrap();
let (lc, note) = LightClient::read_from_disk_reporting(&config).unwrap();
assert_eq!(lc.do_seed_phrase().unwrap()["seed"], TEST_SEED.to_string());
let kept = corrupt_files(tmp.path());
assert_eq!(kept.len(), 1, "the damaged file is kept");
assert_eq!(std::fs::read(&kept[0]).unwrap(), gzip_garbage());
assert_eq!(std::fs::read(&wallet).unwrap(), bak_bytes, "the wallet is the backup now");
assert_eq!(std::fs::read(&bak).unwrap(), bak_bytes, "the backup is untouched");
let name = kept[0].file_name().unwrap().to_string_lossy().into_owned();
assert!(name.starts_with("silentdragonxlite-wallet.dat.corrupt-"), "{}", name);
assert_eq!(note, Some(format!("recovered from backup; damaged file kept as {}", name)));
drop(lc);
// The next open is a plain one: nothing to report, nothing moved.
let (lc, note) = LightClient::read_from_disk_reporting(&config).unwrap();
assert_eq!(note, None);
assert_eq!(corrupt_files(tmp.path()), kept);
assert_eq!(lc.do_seed_phrase().unwrap()["seed"], TEST_SEED.to_string());
}
// s2 C244 test 2 (s2-D4): a wallet written by a newer app is an Unsupported error that changes nothing; the older
// .bak never replaces it. A transient I/O error (EACCES here) does not fall back either.
#[test]
pub fn test_read_from_disk_keeps_newer_version_wallet() {
let tmp = TempDir::new("lctest").unwrap();
let (config, wallet, bak) = saved_pair(&tmp);
let bak_bytes = std::fs::read(&bak).unwrap();
let mut newer = u64::MAX.to_le_bytes().to_vec();
newer.extend_from_slice(b"a wallet from a newer app");
std::fs::write(&wallet, &newer).unwrap();
let e = read_err(&config);
assert_eq!(e.kind(), std::io::ErrorKind::Unsupported, "{}", e);
assert!(e.to_string().contains("Don't know how to read wallet version"), "{}", e);
assert_eq!(std::fs::read(&wallet).unwrap(), newer, "the newer wallet was replaced");
assert_eq!(std::fs::read(&bak).unwrap(), bak_bytes, "the backup was changed");
assert!(corrupt_files(tmp.path()).is_empty());
#[cfg(unix)]
{
std::fs::write(&wallet, gzip_garbage()).unwrap();
chmod(&wallet, 0o000);
if std::fs::File::open(&wallet).is_err() { // not when the tests run as root
let e = read_err(&config);
chmod(&wallet, 0o600);
assert_eq!(e.kind(), std::io::ErrorKind::PermissionDenied, "{}", e);
assert_eq!(std::fs::read(&wallet).unwrap(), gzip_garbage(), "an unreadable wallet was replaced");
assert_eq!(std::fs::read(&bak).unwrap(), bak_bytes);
assert!(corrupt_files(tmp.path()).is_empty());
}
chmod(&wallet, 0o600);
}
}
// s2 C244 test 3: a corrupt wallet with a corrupt (or no) .bak is an error that leaves the files as they were.
#[test]
pub fn test_read_from_disk_both_bad_leaves_files() {
let tmp = TempDir::new("lctest").unwrap();
let (config, wallet, bak) = saved_pair(&tmp);
let mut main_bad = std::fs::read(&wallet).unwrap();
main_bad.truncate(main_bad.len() / 2);
std::fs::write(&wallet, &main_bad).unwrap();
std::fs::write(&bak, gzip_garbage()).unwrap();
let _ = read_err(&config);
assert_eq!(std::fs::read(&wallet).unwrap(), main_bad, "the wallet was changed");
assert_eq!(std::fs::read(&bak).unwrap(), gzip_garbage(), "the backup was changed");
assert!(corrupt_files(tmp.path()).is_empty());
std::fs::remove_file(&bak).unwrap();
let e = read_err(&config);
assert!(matches!(e.kind(), std::io::ErrorKind::InvalidData | std::io::ErrorKind::UnexpectedEof
| std::io::ErrorKind::InvalidInput), "{:?}", e);
assert_eq!(std::fs::read(&wallet).unwrap(), main_bad);
assert!(!bak.exists());
assert!(corrupt_files(tmp.path()).is_empty());
}
// X5 (s2-D12): a damaged encrypted wallet whose .bak predates the encryption (a .bak last saved by Lite <= 1.1.1;
// s1-D6 no longer writes one, so it is built by hand) is not opened: Err, both files unchanged, nothing kept aside.
#[test]
pub fn test_read_from_disk_refuses_plaintext_bak_for_encrypted_main() {
use crate::lightwallet::LightWallet;
use super::wallet_header_encrypted;
let tmp = TempDir::new("lctest").unwrap();
let config = main_config(&tmp);
let wallet = config.get_wallet_path().into_path_buf();
let bak = wallet.with_extension("dat.bak");
let lc = LightClient::new_from_phrase(TEST_SEED.to_string(), &config, 0, 0, false).unwrap();
lc.do_save().unwrap();
let plaintext = std::fs::read(&wallet).unwrap();
assert_eq!(plaintext[8], 0, "precondition: a plaintext wallet");
lc.wallet.write().unwrap().encrypt("password".to_string()).unwrap();
lc.do_save().unwrap();
drop(lc);
std::fs::write(&bak, &plaintext).unwrap();
let enc = std::fs::read(&wallet).unwrap();
assert_eq!(enc[8], 1, "precondition: an encrypted wallet");
let damaged = enc[..enc.len() / 2].to_vec(); // the 9-byte header is intact
std::fs::write(&wallet, &damaged).unwrap();
assert_eq!(wallet_header_encrypted(&wallet), Some(true));
let from_bak = LightWallet::read(std::io::BufReader::new(std::fs::File::open(&bak).unwrap()), &config).unwrap();
assert!(!from_bak.is_encrypted(), "precondition: the .bak opens, unencrypted");
assert_eq!(from_bak.get_seed_phrase(), TEST_SEED.to_string());
let e = read_err(&config);
assert_eq!(e.kind(), std::io::ErrorKind::InvalidData, "{}", e);
assert!(e.to_string().contains("its backup was made before the wallet was encrypted"), "{}", e);
assert_eq!(std::fs::read(&wallet).unwrap(), damaged, "the damaged encrypted wallet was changed");
assert_eq!(std::fs::read(&bak).unwrap(), plaintext, "the backup was changed");
assert!(corrupt_files(tmp.path()).is_empty(), "a damaged file was moved aside");
}
// s2-D13: a recovery never overwrites an earlier .dat.corrupt-<ts>; it takes the next free -<n> suffix. Any entry
// counts as taken, a dangling symlink too (unix), whose target is never created.
#[test]
pub fn test_recovery_never_overwrites_previous_corrupt_file() {
let tmp = TempDir::new("lctest").unwrap();
let (config, wallet, _bak) = saved_pair(&tmp);
let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs();
let mut sentinels = vec![];
for t in now..now + 10 { // the recovery may run a few seconds later
let p = wallet.with_extension(format!("dat.corrupt-{}", t));
let s = format!("an earlier damaged wallet {}", t).into_bytes();
std::fs::write(&p, &s).unwrap();
sentinels.push((p, s));
}
#[cfg(unix)]
let (want_suffix, target) = {
let target = tmp.path().join("symlink-target");
for t in now..now + 10 {
std::os::unix::fs::symlink(&target, wallet.with_extension(format!("dat.corrupt-{}-1", t))).unwrap();
}
("-2", Some(target))
};
#[cfg(not(unix))]
let (want_suffix, target): (&str, Option<std::path::PathBuf>) = ("-1", None);
std::fs::write(&wallet, gzip_garbage()).unwrap();
let (_lc, note) = LightClient::read_from_disk_reporting(&config).unwrap();
for (p, s) in &sentinels {
assert_eq!(&std::fs::read(p).unwrap(), s, "{:?} was overwritten", p);
}
let new: Vec<std::path::PathBuf> = corrupt_files(tmp.path()).into_iter()
.filter(|p| std::fs::symlink_metadata(p).unwrap().file_type().is_file() && !sentinels.iter().any(|(q, _)| q == p))
.collect();
assert_eq!(new.len(), 1, "{:?}", new);
let name = new[0].file_name().unwrap().to_string_lossy().into_owned();
assert!(name.ends_with(want_suffix), "{}", name);
assert_eq!(std::fs::read(&new[0]).unwrap(), gzip_garbage());
assert_eq!(note, Some(format!("recovered from backup; damaged file kept as {}", name)));
if let Some(t) = target {
assert!(!t.exists(), "a dangling symlink was followed");
}
}
}