View Wallet - fn rewind_hash & scan_rewind_hash (#632)

* fn rewind_hash & scan_rewind_hash

* update comments

* update doctest
This commit is contained in:
deevope
2021-12-14 13:23:17 +01:00
committed by GitHub
parent b425107368
commit d70423af57
11 changed files with 660 additions and 7 deletions
+75
View File
@@ -29,6 +29,7 @@ use crate::libwallet::{
use crate::util::secp::key::SecretKey;
use crate::util::{Mutex, ZeroingString};
use crate::{controller, display};
use ::core::time;
use serde_json as json;
use std::convert::TryFrom;
use std::fs::File;
@@ -116,6 +117,80 @@ where
Ok(())
}
pub fn rewind_hash<'a, L, C, K>(
owner_api: &mut Owner<L, C, K>,
keychain_mask: Option<&SecretKey>,
) -> Result<(), Error>
where
L: WalletLCProvider<'static, C, K>,
C: NodeClient + 'static,
K: keychain::Keychain + 'static,
{
controller::owner_single_use(None, keychain_mask, Some(owner_api), |api, m| {
let rewind_hash = api.get_rewind_hash(m)?;
println!();
println!("Wallet Rewind Hash");
println!("-------------------------------------");
println!("{}", rewind_hash);
println!();
Ok(())
})?;
Ok(())
}
/// Arguments for rewind hash view wallet scan command
pub struct ViewWalletScanArgs {
pub rewind_hash: String,
pub start_height: Option<u64>,
pub backwards_from_tip: Option<u64>,
}
pub fn scan_rewind_hash<L, C, K>(
owner_api: &mut Owner<L, C, K>,
args: ViewWalletScanArgs,
dark_scheme: bool,
) -> Result<(), Error>
where
L: WalletLCProvider<'static, C, K> + 'static,
C: NodeClient + 'static,
K: keychain::Keychain + 'static,
{
controller::owner_single_use(None, None, Some(owner_api), |api, m| {
let rewind_hash = args.rewind_hash;
let tip_height = api.node_height(m)?.height;
let start_height = match args.backwards_from_tip {
Some(b) => tip_height.saturating_sub(b),
None => match args.start_height {
Some(s) => s,
None => 1,
},
};
warn!(
"Starting view wallet output scan from height {} ...",
start_height
);
let result = api.scan_rewind_hash(rewind_hash, Some(start_height));
let deci_sec = time::Duration::from_millis(100);
thread::sleep(deci_sec);
match result {
Ok(res) => {
warn!("View wallet check complete");
if res.total_balance != 0 {
display::view_wallet_output(res.clone(), tip_height, dark_scheme)?;
}
display::view_wallet_balance(res.clone(), tip_height, dark_scheme);
Ok(())
}
Err(e) => {
error!("View wallet check failed: {}", e);
error!("Backtrace: {}", e.backtrace().unwrap());
Err(e)
}
}
})?;
Ok(())
}
/// Arguments for listen command
pub struct ListenArgs {}
+93 -1
View File
@@ -16,7 +16,7 @@ use crate::core::core::FeeFields;
use crate::core::core::{self, amount_to_hr_string};
use crate::core::global;
use crate::libwallet::{
AcctPathMapping, Error, OutputCommitMapping, OutputStatus, TxLogEntry, WalletInfo,
AcctPathMapping, Error, OutputCommitMapping, OutputStatus, TxLogEntry, ViewWallet, WalletInfo,
};
use crate::util::ToHex;
use grin_wallet_util::OnionV3Address;
@@ -289,6 +289,98 @@ pub fn txs(
}
Ok(())
}
pub fn view_wallet_balance(w: ViewWallet, cur_height: u64, dark_background_color_scheme: bool) {
println!(
"\n____ View Wallet Summary Info - Block Height: {} ____\n Rewind Hash - {}\n",
cur_height, w.rewind_hash
);
let mut table = table!();
if dark_background_color_scheme {
table.add_row(row![
bFG->"Total Balance",
FG->amount_to_hr_string(w.total_balance, false)
]);
} else {
table.add_row(row![
bFG->"Total Balance",
FG->amount_to_hr_string(w.total_balance, false)
]);
};
table.set_format(*prettytable::format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
table.printstd();
println!();
}
pub fn view_wallet_output(
view_wallet: ViewWallet,
cur_height: u64,
dark_background_color_scheme: bool,
) -> Result<(), Error> {
println!();
let title = format!("View Wallet Outputs - Block Height: {}", cur_height);
if term::stdout().is_none() {
println!("Could not open terminal");
return Ok(());
}
let mut t = term::stdout().unwrap();
t.fg(term::color::MAGENTA).unwrap();
writeln!(t, "{}", title).unwrap();
t.reset().unwrap();
let mut table = table!();
table.set_titles(row![
bMG->"Output Commitment",
bMG->"MMR Index",
bMG->"Block Height",
bMG->"Locked Until",
bMG->"Coinbase?",
bMG->"# Confirms",
bMG->"Value",
]);
for m in view_wallet.output_result {
let commit = format!("{}", m.commit);
let index = m.mmr_index;
let height = format!("{}", m.height);
let lock_height = format!("{}", m.lock_height);
let is_coinbase = format!("{}", m.is_coinbase);
let num_confirmations = format!("{}", m.num_confirmations(cur_height));
let value = format!("{}", core::amount_to_hr_string(m.value, false));
if dark_background_color_scheme {
table.add_row(row![
bFC->commit,
bFB->index,
bFB->height,
bFB->lock_height,
bFY->is_coinbase,
bFB->num_confirmations,
bFG->value,
]);
} else {
table.add_row(row![
bFD->commit,
bFB->index,
bFB->height,
bFB->lock_height,
bFD->is_coinbase,
bFB->num_confirmations,
bFG->value,
]);
}
}
table.set_format(*prettytable::format::consts::FORMAT_NO_COLSEP);
table.printstd();
println!();
Ok(())
}
/// Display summary info in a pretty way
pub fn info(
account: &str,