Skip to content

Commit 5eadf5c

Browse files
committed
Add some logging to script_sync
1 parent aaad560 commit 5eadf5c

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

src/blockchain/script_sync.rs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ returns associated transactions i.e. electrum.
55
#![allow(dead_code)]
66
use crate::{
77
database::{BatchDatabase, BatchOperations, DatabaseUtils},
8+
wallet::time::Instant,
89
ConfirmationTime, Error, KeychainKind, LocalUtxo, TransactionDetails,
910
};
1011
use bitcoin::{OutPoint, Script, Transaction, TxOut, Txid};
12+
use log::*;
1113
use std::collections::{HashMap, HashSet, VecDeque};
1214

1315
/// A reqeust for on-chain information
@@ -33,14 +35,7 @@ pub fn start<D: BatchDatabase>(db: &D, stop_gap: usize) -> Result<Request<'_, D>
3335
.iter_script_pubkeys(Some(keychain))?
3436
.into_iter()
3537
.collect();
36-
let state = State {
37-
db,
38-
last_active_index: HashMap::default(),
39-
conftime_needed: VecDeque::default(),
40-
observed_txs: vec![],
41-
tx_needed: VecDeque::default(),
42-
tx_missing_conftime: HashMap::default(),
43-
};
38+
let state = State::new(db);
4439

4540
Ok(Request::Script(ScriptReq {
4641
state,
@@ -76,7 +71,12 @@ impl<'a, D: BatchDatabase> ScriptReq<'a, D> {
7671
// we want to know the txids assoiciated with the script and their height
7772
txids: Vec<Vec<(Txid, Option<u32>)>>,
7873
) -> Result<Request<'a, D>, Error> {
79-
for txid_list in txids.iter() {
74+
for (txid_list, script) in txids.iter().zip(self.scripts_needed.iter()) {
75+
debug!(
76+
"found {} transactions for script pubkey {}",
77+
txid_list.len(),
78+
script
79+
);
8080
if !txid_list.is_empty() {
8181
// the address is active
8282
self.state
@@ -131,6 +131,10 @@ impl<'a, D: BatchDatabase> ScriptReq<'a, D> {
131131
if self.script_index > last_active_index + self.stop_gap
132132
|| self.scripts_needed.is_empty()
133133
{
134+
debug!(
135+
"finished scanning for transactions for keychain {:?} at index {}",
136+
self.keychain, last_active_index
137+
);
134138
// we're done here -- check if we need to do the next keychain
135139
if let Some(keychain) = self.next_keychains.pop() {
136140
self.keychain = keychain;
@@ -154,11 +158,6 @@ impl<'a, D: BatchDatabase> ScriptReq<'a, D> {
154158
}
155159
}
156160

157-
/// Next step is to get confirmation times for those we are interested in.
158-
pub struct ConftimeReq<'a, D> {
159-
state: State<'a, D>,
160-
}
161-
162161
/// Then we get full transactions
163162
pub struct TxReq<'a, D> {
164163
state: State<'a, D>,
@@ -177,6 +176,7 @@ impl<'a, D: BatchDatabase> TxReq<'a, D> {
177176
.into_iter()
178177
.zip(self.state.tx_needed.iter())
179178
.map(|((vin, tx), txid)| {
179+
debug!("found tx_details for {}", txid);
180180
assert_eq!(tx.txid(), *txid);
181181
let mut sent: u64 = 0;
182182
let mut received: u64 = 0;
@@ -240,6 +240,11 @@ impl<'a, D: BatchDatabase> TxReq<'a, D> {
240240
}
241241
}
242242

243+
/// Final step is to get confirmation times
244+
pub struct ConftimeReq<'a, D> {
245+
state: State<'a, D>,
246+
}
247+
243248
impl<'a, D: BatchDatabase> ConftimeReq<'a, D> {
244249
pub fn request(&self) -> impl Iterator<Item = &Txid> + Clone {
245250
self.state.conftime_needed.iter()
@@ -252,6 +257,7 @@ impl<'a, D: BatchDatabase> ConftimeReq<'a, D> {
252257
let n = confirmation_times.len();
253258
let conftime_needed = self.state.conftime_needed.iter();
254259
for (confirmation_time, txid) in confirmation_times.into_iter().zip(conftime_needed) {
260+
debug!("confirmation time for {} was {:?}", txid, confirmation_time);
255261
// this is written awkwardly to avoid lifetime issues with using cleaner .or_else
256262
let mut tx_details = self.state.tx_missing_conftime.remove(txid);
257263
if tx_details.is_none() {
@@ -283,10 +289,22 @@ struct State<'a, D> {
283289
conftime_needed: VecDeque<Txid>,
284290
observed_txs: Vec<TransactionDetails>,
285291
tx_missing_conftime: HashMap<Txid, TransactionDetails>,
292+
start_time: Instant,
286293
}
287294

288295
impl<'a, D: BatchDatabase> State<'a, D> {
289-
pub fn into_db_update(self) -> Result<D::Batch, Error> {
296+
fn new(db: &'a D) -> Self {
297+
State {
298+
db,
299+
last_active_index: HashMap::default(),
300+
conftime_needed: VecDeque::default(),
301+
observed_txs: vec![],
302+
tx_needed: VecDeque::default(),
303+
tx_missing_conftime: HashMap::default(),
304+
start_time: Instant::new(),
305+
}
306+
}
307+
fn into_db_update(self) -> Result<D::Batch, Error> {
290308
debug_assert!(
291309
self.tx_needed.is_empty()
292310
&& self.tx_missing_conftime.is_empty()
@@ -356,6 +374,10 @@ impl<'a, D: BatchDatabase> State<'a, D> {
356374
batch.set_last_index(keychain, last_active_index as u32)?;
357375
}
358376

377+
info!(
378+
"finished setup, elapsed {:?}ms",
379+
self.start_time.elapsed().as_millis()
380+
);
359381
Ok(batch)
360382
}
361383
}

0 commit comments

Comments
 (0)