MCPcopy Create free account
hub / github.com/bwt-dev/bwt / sync

Method sync

src/indexer.rs:87–135  ·  view source on GitHub ↗

initiate a regular sync to catch up with updates and import new addresses (no rescan)

(&mut self)

Source from the content-addressed store, hash-verified

85
86 // initiate a regular sync to catch up with updates and import new addresses (no rescan)
87 pub fn sync(&mut self) -> Result<Vec<IndexChange>> {
88 let mut changelog = Changelog::new(self.tip.is_some());
89
90 // detect reorgs and sync the whole history from scratch when they happen
91 // XXX the reorg test is racey
92 if let Some(BlockId(tip_height, ref tip_hash)) = self.tip {
93 let best_chain_hash = self.rpc.get_block_hash(tip_height as u64)?;
94 if best_chain_hash != *tip_hash {
95 warn!(
96 "reorg detected, block height {} was {} and now is {}. fetching history from scratch...",
97 tip_height, tip_hash, best_chain_hash
98 );
99
100 // notify clients about the reorg, but don't collect additional events (apart from
101 // ChainTip, added below)
102 changelog.push(|| IndexChange::Reorg(tip_height, *tip_hash, best_chain_hash));
103 changelog.track = false;
104
105 // XXX start syncing from N blocks backs instead of from the beginning?
106 self.tip = None;
107 }
108 }
109
110 let synced_tip = self.sync_transactions(false, &mut changelog)?;
111 let tip_updated = self.tip != Some(synced_tip);
112 self.sync_mempool(/*force_refresh=*/ tip_updated);
113 self.watcher.do_imports(&self.rpc, /*rescan=*/ false)?;
114
115 let mut changelog = changelog.into_vec();
116
117 if tip_updated {
118 info!(
119 "synced up to height {}{}",
120 synced_tip.0,
121 iif!(changelog.is_empty(), "", " (found wallet activity)")
122 );
123
124 changelog.push(IndexChange::ChainTip(synced_tip));
125 self.tip = Some(synced_tip);
126 }
127
128 if !changelog.is_empty() && log_enabled!(log::Level::Debug) {
129 for update in &changelog {
130 debug!(" - {:?}", update);
131 }
132 }
133
134 Ok(changelog)
135 }
136
137 fn sync_transactions(
138 &mut self,

Callers

nothing calls this directly

Calls 7

ChainTipFunction · 0.85
get_block_hashMethod · 0.80
pushMethod · 0.80
sync_transactionsMethod · 0.80
sync_mempoolMethod · 0.80
do_importsMethod · 0.80
into_vecMethod · 0.80

Tested by

no test coverage detected