1use std::collections::{BTreeMap, HashMap, VecDeque};
62use std::convert::Infallible;
63
64use anyhow::Result;
65use bsv_sdk::transaction::MerklePath;
66use bsv_sdk::wallet::{InternalizeActionArgs, WalletInterface};
67use bsv_tracker::{
68 decode_envelope, ChainEnvelope, ChainEvent, CheckError, Clock, Evidence, Header, HeaderError,
69 Headers, Height, Hint, HintSource, HintStatus, HostAction, Input, Params, Proof, ProofFetcher,
70 Reask, State, Timestamp, Tracker, TxId, Word,
71};
72use bsv_wallet_toolbox::services::PostBeefResult;
73use bsv_wallet_toolbox::{BroadcastStatus, StorageSqlx, WalletServices};
74use serde::{Deserialize, Serialize};
75use sqlx::Row as _;
76
77pub const DEFAULT_AGE_SECS: u64 = 600;
81pub const DEFAULT_MAX_ASKS: usize = 20;
83const MAX_BACKOFF_DOUBLINGS: u32 = 6;
86const MAX_ASKS_PER_TX: u32 = 16;
89const TIP_RING: usize = 12;
91pub const DEFAULT_SPEND_DEPTH: Height = 1;
94
95#[derive(Debug, Clone)]
104pub struct HeaderSnapshot {
105 tip: std::result::Result<(Height, String), String>,
106 headers: BTreeMap<Height, std::result::Result<Header, String>>,
107}
108
109impl HeaderSnapshot {
110 pub async fn take<V: WalletServices + ?Sized>(
112 services: &V,
113 heights: impl IntoIterator<Item = Height>,
114 ) -> Self {
115 let tip = services
116 .get_chain_tip_header()
117 .await
118 .map(|h| (h.height, h.hash.to_ascii_lowercase()))
119 .map_err(|e| e.to_string());
120 let mut snapshot = Self {
121 tip,
122 headers: BTreeMap::new(),
123 };
124 snapshot.extend(services, heights).await;
125 snapshot
126 }
127
128 pub fn unread() -> Self {
130 Self {
131 tip: Err("the header service was not read".to_string()),
132 headers: BTreeMap::new(),
133 }
134 }
135
136 pub async fn extend<V: WalletServices + ?Sized>(
138 &mut self,
139 services: &V,
140 heights: impl IntoIterator<Item = Height>,
141 ) {
142 for height in heights {
143 if self.headers.contains_key(&height) {
144 continue;
145 }
146 let answer = match services.get_header_for_height(height).await {
147 Ok(bytes) => header_of(&bytes),
148 Err(e) => Err(e.to_string()),
149 };
150 self.headers.insert(height, answer);
151 }
152 }
153
154 pub fn tip(&self) -> Option<(Height, &str)> {
156 self.tip.as_ref().ok().map(|(h, hash)| (*h, hash.as_str()))
157 }
158}
159
160impl Headers for HeaderSnapshot {
161 fn header_at(&self, height: Height) -> std::result::Result<Option<Header>, HeaderError> {
162 match self.headers.get(&height) {
163 Some(Ok(header)) => Ok(Some(header.clone())),
164 Some(Err(fault)) => Err(HeaderError(fault.clone())),
165 None => Ok(None),
166 }
167 }
168
169 fn tip_height(&self) -> std::result::Result<Height, HeaderError> {
170 self.tip
171 .as_ref()
172 .map(|(height, _)| *height)
173 .map_err(|fault| HeaderError(fault.clone()))
174 }
175}
176
177fn header_of(bytes: &[u8]) -> std::result::Result<Header, String> {
180 if bytes.len() != 80 {
181 return Err(format!("a header of {} bytes", bytes.len()));
182 }
183 let display = |raw: &[u8]| {
184 let mut reversed = raw.to_vec();
185 reversed.reverse();
186 hex::encode(reversed)
187 };
188 Ok(Header {
189 hash: display(&bsv_sdk::primitives::hash::sha256d(bytes)),
190 merkle_root: display(&bytes[36..68]),
191 })
192}
193
194#[derive(Debug, Clone, Copy, Default)]
200pub struct SystemClock;
201
202impl Clock for SystemClock {
203 fn now(&self) -> Timestamp {
204 std::time::SystemTime::now()
205 .duration_since(std::time::UNIX_EPOCH)
206 .map(|d| d.as_secs())
207 .unwrap_or(0)
208 }
209}
210
211#[derive(Debug, Default)]
220pub struct MemoryHints {
221 queue: VecDeque<(TxId, Hint)>,
222}
223
224impl MemoryHints {
225 async fn read(storage: &StorageSqlx, rows: &[TrackedRow]) -> Result<Self> {
226 let txids: Vec<String> = rows.iter().map(|r| r.txid.clone()).collect();
227 if txids.is_empty() {
228 return Ok(Self::default());
229 }
230 let mut records = storage.broadcast_records(None, &txids).await?;
231 records.sort_by_key(|r| r.seen_at);
232 let mut queue = VecDeque::new();
233 for record in records {
234 let Some(status) = record.ladder_status() else {
235 continue;
236 };
237 let txid = record.txid.to_ascii_lowercase();
238 let Some(row) = rows.iter().find(|r| r.txid == txid) else {
239 continue;
240 };
241 let (label, status) = match status {
244 BroadcastStatus::Accepted => ("accepted", HintStatus::Accepted),
245 BroadcastStatus::Seen => ("seen", HintStatus::Seen),
246 BroadcastStatus::Mined => ("mined", HintStatus::Unknown),
247 BroadcastStatus::Rejected => (
248 "rejected",
249 HintStatus::Rejected {
250 reason: record.status.clone(),
251 },
252 ),
253 BroadcastStatus::Unknown => ("unknown", HintStatus::Unknown),
254 };
255 let source = format!("{}|{}", record.provider, label);
256 if row.hints.iter().any(|h| h.source == source) {
257 continue;
258 }
259 let observed = record.seen_at.timestamp().max(0) as Timestamp;
260 queue.push_back((txid, Hint::new(source, status, observed)));
261 }
262 Ok(Self { queue })
263 }
264}
265
266impl HintSource for MemoryHints {
267 type Error = Infallible;
268
269 fn next_hint(&mut self) -> std::result::Result<Option<(TxId, Hint)>, Self::Error> {
270 Ok(self.queue.pop_front())
271 }
272}
273
274#[derive(Debug, Default)]
282pub struct Courier {
283 carried: HashMap<TxId, std::result::Result<Option<Proof>, String>>,
284}
285
286impl Courier {
287 pub async fn carry<V: WalletServices + ?Sized>(services: &V, txids: &[TxId]) -> Self {
289 let mut carried = HashMap::new();
290 for txid in txids {
291 let answer = match services.get_merkle_path(txid, false).await {
292 Ok(found) => match found.merkle_path {
293 Some(hex) => MerklePath::from_hex(&hex)
294 .map_err(|e| format!("not a merkle path: {e}"))
295 .and_then(|path| Proof::new(txid.clone(), path).map_err(|e| e.to_string()))
296 .map(Some),
297 None => Ok(None),
298 },
299 Err(e) => Err(e.to_string()),
300 };
301 carried.insert(txid.clone(), answer);
302 }
303 Self { carried }
304 }
305
306 fn heights(&self) -> Vec<Height> {
307 self.carried
308 .values()
309 .filter_map(|answer| answer.as_ref().ok()?.as_ref().map(Proof::height))
310 .collect()
311 }
312}
313
314impl ProofFetcher for Courier {
315 type Error = String;
316
317 fn fetch(
318 &mut self,
319 txid: &str,
320 _reask: &Reask,
321 ) -> std::result::Result<Option<Proof>, Self::Error> {
322 self.carried.remove(txid).unwrap_or(Ok(None))
323 }
324}
325
326#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
331struct StoredHint {
332 source: String,
333 status: String,
334 #[serde(default, skip_serializing_if = "Option::is_none")]
335 height: Option<Height>,
336 #[serde(default, skip_serializing_if = "Option::is_none")]
337 reason: Option<String>,
338 #[serde(default, skip_serializing_if = "Vec::is_empty")]
339 competitors: Vec<TxId>,
340 observed: Timestamp,
341}
342
343impl StoredHint {
344 fn of(hint: &Hint) -> Self {
345 let (status, height, reason, competitors) = match &hint.status {
346 HintStatus::Accepted => ("accepted", None, None, vec![]),
347 HintStatus::Seen => ("seen", None, None, vec![]),
348 HintStatus::Mined { height } => ("mined", Some(*height), None, vec![]),
349 HintStatus::StaleBlock => ("stale_block", None, None, vec![]),
350 HintStatus::Rejected { reason } => ("rejected", None, Some(reason.clone()), vec![]),
351 HintStatus::DoubleSpend { competitors } => {
352 ("double_spend", None, None, competitors.clone())
353 }
354 HintStatus::OrphanMempool => ("orphan_mempool", None, None, vec![]),
355 HintStatus::Unknown => ("unknown", None, None, vec![]),
356 };
357 Self {
358 source: hint.source.clone(),
359 status: status.to_string(),
360 height,
361 reason,
362 competitors,
363 observed: hint.observed,
364 }
365 }
366
367 fn hint(&self) -> Hint {
368 let status = match self.status.as_str() {
369 "accepted" => HintStatus::Accepted,
370 "seen" => HintStatus::Seen,
371 "mined" => self
372 .height
373 .map_or(HintStatus::Unknown, |height| HintStatus::Mined { height }),
374 "stale_block" => HintStatus::StaleBlock,
375 "rejected" => HintStatus::Rejected {
376 reason: self.reason.clone().unwrap_or_default(),
377 },
378 "double_spend" => HintStatus::DoubleSpend {
379 competitors: self.competitors.clone(),
380 },
381 "orphan_mempool" => HintStatus::OrphanMempool,
382 _ => HintStatus::Unknown,
383 };
384 Hint::new(self.source.clone(), status, self.observed)
385 }
386}
387
388#[derive(Debug, Clone, PartialEq, Eq)]
390struct TrackedRow {
391 txid: TxId,
392 built: bool,
393 hints: Vec<StoredHint>,
395 bump: Option<String>,
397 asks: u32,
398 next_ask_at: Option<Timestamp>,
399 reannounce_beef: Option<Vec<u8>>,
401 reannounce_at: Option<Timestamp>,
402 on_accept: Option<String>,
405 chain: Vec<String>,
409}
410
411impl TrackedRow {
412 fn new(txid: &str) -> Self {
413 Self {
414 txid: txid.to_ascii_lowercase(),
415 built: true,
416 hints: vec![],
417 bump: None,
418 asks: 0,
419 next_ask_at: None,
420 reannounce_beef: None,
421 reannounce_at: None,
422 on_accept: None,
423 chain: vec![],
424 }
425 }
426
427 fn chain_events(&self) -> Vec<ChainEvent> {
431 self.chain
432 .iter()
433 .filter_map(|json| match decode_envelope(json.as_bytes()) {
434 Ok(event) => Some(event),
435 Err(fault) => {
436 tracing::warn!(txid = %self.txid, %fault, "a stored chain event does not decode");
437 None
438 }
439 })
440 .collect()
441 }
442
443 fn keep_chain(&mut self, state: &State, event: &ChainEvent) -> Result<()> {
447 let plain = matches!(state.word(), Word::Mined(_)) && !state.suspect();
448 if plain && state.reask().is_none() {
449 self.chain.clear();
450 return Ok(());
451 }
452 if matches!(event, ChainEvent::Tip { .. } | ChainEvent::TipAge { .. }) {
453 self.chain.retain(|json| {
454 !matches!(
455 decode_envelope(json.as_bytes()),
456 Ok(ChainEvent::Tip { .. } | ChainEvent::TipAge { .. })
457 )
458 });
459 }
460 self.chain
461 .push(serde_json::to_string(&ChainEnvelope::new(event.clone()))?);
462 Ok(())
463 }
464
465 fn replay_into<H: Headers>(&self, tracker: &mut Tracker, headers: &H) {
468 tracker.track(self.txid.clone());
469 if self.built {
470 let _ = tracker.apply(&self.txid, headers, Input::Host(HostAction::Build));
471 }
472 for hint in &self.hints {
473 let _ = tracker.apply(&self.txid, headers, Input::Hint(hint.hint()));
474 }
475 if let Some(Ok(proof)) = self.proof() {
476 let _ = tracker.apply(&self.txid, headers, Input::Evidence(Evidence::Proof(proof)));
477 }
478 for event in self.chain_events() {
479 let _ = tracker.apply(&self.txid, headers, Input::Evidence(Evidence::Chain(event)));
480 }
481 }
482
483 fn proof(&self) -> Option<std::result::Result<Proof, CheckError>> {
484 let hex = self.bump.as_deref()?;
485 Some(
486 MerklePath::from_hex(hex)
487 .map_err(|e| CheckError::InvalidProof(e.to_string()))
488 .and_then(|path| Proof::new(self.txid.clone(), path)),
489 )
490 }
491
492 fn proof_height(&self) -> Option<Height> {
493 self.proof()?.ok().map(|p| p.height())
494 }
495
496 fn replay<H: Headers>(&self, params: &Params, headers: &H) -> (State, Option<CheckError>) {
500 let mut state = State::new(self.txid.clone());
501 if self.built {
502 let _ = state.step(params, headers, Input::Host(HostAction::Build));
503 }
504 for hint in &self.hints {
505 let _ = state.step(params, headers, Input::Hint(hint.hint()));
506 }
507 let fault = match self.proof() {
508 Some(Ok(proof)) => state
509 .step(params, headers, Input::Evidence(Evidence::Proof(proof)))
510 .err(),
511 Some(Err(fault)) => Some(fault),
512 None => None,
513 };
514 for event in self.chain_events() {
515 let _ = state.step(params, headers, Input::Evidence(Evidence::Chain(event)));
516 }
517 (state, fault)
518 }
519}
520
521pub fn word_label(word: &Word) -> &'static str {
523 match word {
524 Word::Unknown => "unknown",
525 Word::Built => "built",
526 Word::Announced => "announced",
527 Word::Seen => "seen",
528 Word::Mined(_) => "mined",
529 Word::Stale => "stale",
530 Word::Rejected { .. } => "rejected",
531 Word::Conflicted { .. } => "conflicted",
532 Word::Abandoned => "abandoned",
533 }
534}
535
536fn reask_label(reask: &Reask) -> String {
537 match reask {
538 Reask::Reorg { fork_height } => format!("reorg@{fork_height}"),
539 Reask::Fork { height } => format!("fork@{height}"),
540 Reask::Hint(hint) => format!("hint:{}", hint.source),
541 Reask::ProofFailed { height } => format!("proof_failed@{height}"),
542 Reask::Recheck => "recheck".to_string(),
543 Reask::Age => "age".to_string(),
544 Reask::Spend => "spend".to_string(),
545 }
546}
547
548async fn ensure_tables(storage: &StorageSqlx) -> Result<()> {
549 for sql in [
550 "CREATE TABLE IF NOT EXISTS tracker_states (\
551 txid TEXT PRIMARY KEY, \
552 built INTEGER NOT NULL DEFAULT 1, \
553 hints TEXT NOT NULL DEFAULT '[]', \
554 bump TEXT, \
555 word TEXT NOT NULL DEFAULT 'unknown', \
556 height INTEGER, \
557 header_hash TEXT, \
558 reask TEXT, \
559 asks INTEGER NOT NULL DEFAULT 0, \
560 next_ask_at INTEGER, \
561 reannounce_beef BLOB, \
562 reannounce_at INTEGER, \
563 on_accept TEXT, \
564 updated_at INTEGER NOT NULL DEFAULT 0)",
565 "CREATE INDEX IF NOT EXISTS tracker_states_word ON tracker_states (word)",
566 "CREATE INDEX IF NOT EXISTS tracker_states_height ON tracker_states (height)",
567 "CREATE TABLE IF NOT EXISTS tracker_meta (key TEXT PRIMARY KEY, value TEXT NOT NULL)",
568 ] {
569 sqlx::query(sql).execute(storage.pool()).await?;
570 }
571 let has_chain: i64 = sqlx::query_scalar(
574 "SELECT COUNT(*) FROM pragma_table_info('tracker_states') WHERE name = 'chain'",
575 )
576 .fetch_one(storage.pool())
577 .await?;
578 if has_chain == 0 {
579 sqlx::query("ALTER TABLE tracker_states ADD COLUMN chain TEXT NOT NULL DEFAULT '[]'")
580 .execute(storage.pool())
581 .await?;
582 }
583 Ok(())
584}
585
586const ROW_COLUMNS: &str =
587 "txid, built, hints, bump, asks, next_ask_at, reannounce_beef, reannounce_at, on_accept, chain";
588
589fn row_of(row: &sqlx::sqlite::SqliteRow) -> TrackedRow {
590 let hints: String = row.get("hints");
591 let chain: String = row.get("chain");
592 TrackedRow {
593 txid: row.get("txid"),
594 built: row.get::<i64, _>("built") != 0,
595 hints: serde_json::from_str(&hints).unwrap_or_default(),
596 bump: row.get("bump"),
597 asks: row.get::<i64, _>("asks").max(0) as u32,
598 next_ask_at: row
599 .get::<Option<i64>, _>("next_ask_at")
600 .map(|t| t.max(0) as Timestamp),
601 reannounce_beef: row.get("reannounce_beef"),
602 reannounce_at: row
603 .get::<Option<i64>, _>("reannounce_at")
604 .map(|t| t.max(0) as Timestamp),
605 on_accept: row.get("on_accept"),
606 chain: serde_json::from_str(&chain).unwrap_or_default(),
607 }
608}
609
610async fn load_row(storage: &StorageSqlx, txid: &str) -> Result<Option<TrackedRow>> {
611 let row = sqlx::query(&format!(
612 "SELECT {ROW_COLUMNS} FROM tracker_states WHERE txid = ?"
613 ))
614 .bind(txid.to_ascii_lowercase())
615 .fetch_optional(storage.pool())
616 .await?;
617 Ok(row.as_ref().map(row_of))
618}
619
620async fn store_row(
621 storage: &StorageSqlx,
622 row: &TrackedRow,
623 state: &State,
624 now: Timestamp,
625) -> Result<()> {
626 store_row_in(storage.pool(), row, state, now).await
627}
628
629async fn store_row_in<'e, E: sqlx::Executor<'e, Database = sqlx::Sqlite>>(
630 executor: E,
631 row: &TrackedRow,
632 state: &State,
633 now: Timestamp,
634) -> Result<()> {
635 let (height, header_hash) = match state.word() {
636 Word::Mined(mined) => (
637 Some(mined.height() as i64),
638 Some(mined.checked().header().hash.clone()),
639 ),
640 _ => (None, None),
641 };
642 sqlx::query(
643 "INSERT INTO tracker_states \
644 (txid, built, hints, bump, word, height, header_hash, reask, asks, next_ask_at, \
645 reannounce_beef, reannounce_at, on_accept, chain, updated_at) \
646 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) \
647 ON CONFLICT(txid) DO UPDATE SET built = excluded.built, hints = excluded.hints, \
648 bump = excluded.bump, word = excluded.word, height = excluded.height, \
649 header_hash = excluded.header_hash, reask = excluded.reask, asks = excluded.asks, \
650 next_ask_at = excluded.next_ask_at, reannounce_beef = excluded.reannounce_beef, \
651 reannounce_at = excluded.reannounce_at, on_accept = excluded.on_accept, \
652 chain = excluded.chain, updated_at = excluded.updated_at",
653 )
654 .bind(&row.txid)
655 .bind(row.built as i64)
656 .bind(serde_json::to_string(&row.hints)?)
657 .bind(&row.bump)
658 .bind(word_label(state.word()))
659 .bind(height)
660 .bind(header_hash)
661 .bind(state.reask().map(reask_label))
662 .bind(row.asks as i64)
663 .bind(row.next_ask_at.map(|t| t as i64))
664 .bind(&row.reannounce_beef)
665 .bind(row.reannounce_at.map(|t| t as i64))
666 .bind(&row.on_accept)
667 .bind(serde_json::to_string(&row.chain)?)
668 .bind(now as i64)
669 .execute(executor)
670 .await?;
671 Ok(())
672}
673
674pub async fn stored_word(
678 storage: &StorageSqlx,
679 txid: &str,
680) -> Result<Option<(String, Option<Height>, Option<String>)>> {
681 ensure_tables(storage).await?;
682 let row = sqlx::query("SELECT word, height, reask FROM tracker_states WHERE txid = ?")
683 .bind(txid.to_ascii_lowercase())
684 .fetch_optional(storage.pool())
685 .await?;
686 Ok(row.map(|r| {
687 (
688 r.get("word"),
689 r.get::<Option<i64>, _>("height").map(|h| h as Height),
690 r.get("reask"),
691 )
692 }))
693}
694
695#[derive(Debug, Clone, PartialEq, Eq)]
701pub enum PostAnswer {
702 Accepted { provider: String },
704 NotFinal { provider: String, said: String },
707 Refused { said: String },
709}
710
711pub fn says_not_final(text: &str) -> bool {
715 let lower = text.to_ascii_lowercase();
716 ["non-final", "nonfinal", "non final", "not final"]
717 .iter()
718 .any(|word| lower.contains(word))
719 || lower
720 .split(|c: char| !c.is_ascii_alphanumeric())
721 .any(|token| token == "476")
722}
723
724pub fn read_post(results: &[PostBeefResult]) -> PostAnswer {
726 if let Some(accepted) = results.iter().find(|r| r.is_success()) {
727 return PostAnswer::Accepted {
728 provider: accepted.name.clone(),
729 };
730 }
731 let said: Vec<(String, String)> = results
732 .iter()
733 .map(|r| {
734 let detail = r
735 .error
736 .clone()
737 .or_else(|| r.txid_results.iter().find_map(|t| t.data.clone()))
738 .unwrap_or_else(|| r.status.clone());
739 (r.name.clone(), detail)
740 })
741 .collect();
742 if let Some((provider, detail)) = said.iter().find(|(_, detail)| says_not_final(detail)) {
743 return PostAnswer::NotFinal {
744 provider: provider.clone(),
745 said: detail.clone(),
746 };
747 }
748 PostAnswer::Refused {
749 said: if said.is_empty() {
750 "none answered".to_string()
751 } else {
752 said.iter()
753 .map(|(name, detail)| format!("{name}: {detail}"))
754 .collect::<Vec<_>>()
755 .join("; ")
756 },
757 }
758}
759
760#[derive(Debug, Clone)]
762pub struct Reannounce {
763 pub beef: Vec<u8>,
765 pub at: Timestamp,
767 pub on_accept: Option<InternalizeActionArgs>,
769}
770
771pub async fn heard<C: Clock>(
778 storage: &StorageSqlx,
779 clock: &C,
780 opts: &TickOptions,
781 txid: &str,
782 hint: Hint,
783 not_before: Option<Timestamp>,
784 reannounce: Option<Reannounce>,
785) -> Result<(String, Option<String>)> {
786 ensure_tables(storage).await?;
787 let mut row = match load_row(storage, txid).await? {
788 Some(row) if row.bump.is_some() => {
791 let stored = stored_word(storage, txid).await?.unwrap_or_default();
792 return Ok((stored.0, stored.2));
793 }
794 Some(row) => row,
795 None => TrackedRow::new(txid),
796 };
797 let params = opts.params();
798 let headers = HeaderSnapshot::unread();
799 let (mut state, _) = row.replay(¶ms, &headers);
800 if !row.hints.iter().any(|h| h.source == hint.source) {
801 row.hints.push(StoredHint::of(&hint));
802 let _ = state.step(¶ms, &headers, Input::Hint(hint));
803 }
804 row.asks = 0;
805 row.next_ask_at = not_before;
806 match reannounce {
807 Some(again) => {
808 row.reannounce_beef = Some(again.beef);
809 row.reannounce_at = Some(again.at);
810 row.on_accept = match again.on_accept {
811 Some(args) => Some(serde_json::to_string(&args)?),
812 None => None,
813 };
814 }
815 None => {
816 row.reannounce_beef = None;
817 row.reannounce_at = None;
818 row.on_accept = None;
819 }
820 }
821 store_row(storage, &row, &state, clock.now()).await?;
822 Ok((
823 word_label(state.word()).to_string(),
824 state.reask().map(reask_label),
825 ))
826}
827
828#[derive(Debug, Clone)]
834pub struct TickOptions {
835 pub age_threshold: Timestamp,
838 pub max_asks: usize,
840 pub spend_depth: Height,
848}
849
850impl TickOptions {
851 pub fn from_env() -> Self {
854 fn env<T: std::str::FromStr>(key: &str, default: T) -> T {
855 std::env::var(key)
856 .ok()
857 .and_then(|v| v.trim().parse().ok())
858 .unwrap_or(default)
859 }
860 Self {
861 age_threshold: env("TRACKER_AGE_SECS", DEFAULT_AGE_SECS).max(1),
862 max_asks: env("TRACKER_MAX_ASKS", DEFAULT_MAX_ASKS),
863 spend_depth: env("SPEND_DEPTH", DEFAULT_SPEND_DEPTH),
864 }
865 .floored()
866 }
867
868 #[must_use]
870 pub fn floored(mut self) -> Self {
871 self.spend_depth = self.spend_depth.max(DEFAULT_SPEND_DEPTH);
872 self
873 }
874
875 fn params(&self) -> Params {
876 Params {
877 age_threshold: self.age_threshold,
878 }
879 }
880}
881
882#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
884pub struct TickReport {
885 pub tracked: usize,
887 pub adopted: usize,
889 pub hints: usize,
891 pub asked: usize,
893 pub mined: Vec<String>,
895 pub no_proof: Vec<String>,
897 pub moved: Vec<String>,
899 pub moved_from: Option<Height>,
901 pub reannounced: Vec<String>,
903 pub not_final: Vec<String>,
906 #[serde(skip)]
910 pub on_accept: Vec<(String, String)>,
911 pub faults: Vec<String>,
913 pub stored: Vec<String>,
915}
916
917impl TickReport {
918 pub fn is_quiet(&self) -> bool {
920 self.adopted == 0
921 && self.hints == 0
922 && self.asked == 0
923 && self.reannounced.is_empty()
924 && self.not_final.is_empty()
925 && self.moved.is_empty()
926 && self.faults.is_empty()
927 }
928
929 pub fn summary(&self) -> String {
931 format!(
932 "tracker tick: {} tracked ({} new), {} hint(s), {} announced again ({} still not final), {} re-ask(s): {} mined, {} without a proof, {} moved, {} fault(s)",
933 self.tracked,
934 self.adopted,
935 self.hints,
936 self.reannounced.len(),
937 self.not_final.len(),
938 self.asked,
939 self.mined.len(),
940 self.no_proof.len(),
941 self.moved.len(),
942 self.faults.len(),
943 )
944 }
945}
946
947async fn read_ring(storage: &StorageSqlx) -> Vec<(Height, String)> {
949 sqlx::query("SELECT value FROM tracker_meta WHERE key = 'tips'")
950 .fetch_optional(storage.pool())
951 .await
952 .ok()
953 .flatten()
954 .and_then(|r| serde_json::from_str(&r.get::<String, _>("value")).ok())
955 .unwrap_or_default()
956}
957
958async fn write_ring(storage: &StorageSqlx, ring: &[(Height, String)]) -> Result<()> {
959 sqlx::query(
960 "INSERT INTO tracker_meta (key, value) VALUES ('tips', ?) \
961 ON CONFLICT(key) DO UPDATE SET value = excluded.value",
962 )
963 .bind(serde_json::to_string(ring)?)
964 .execute(storage.pool())
965 .await?;
966 Ok(())
967}
968
969async fn moved_since<V: WalletServices + ?Sized>(
975 services: &V,
976 snapshot: &mut HeaderSnapshot,
977 mut ring: Vec<(Height, String)>,
978) -> (Option<Height>, Vec<(Height, String)>) {
979 let Some((tip_height, tip_hash)) = snapshot.tip().map(|(h, hash)| (h, hash.to_string())) else {
980 return (None, ring);
981 };
982 let mut moved = None;
983 while let Some((height, hash)) = ring.last().cloned() {
984 let active = if height == tip_height {
985 Some(tip_hash.clone())
986 } else if height > tip_height {
987 None
988 } else {
989 snapshot.extend(services, [height]).await;
990 match snapshot.header_at(height) {
991 Ok(Some(header)) => Some(header.hash),
992 _ => return (moved, ring),
994 }
995 };
996 if active.as_deref() == Some(hash.as_str()) {
997 break;
998 }
999 moved = Some(height);
1000 ring.pop();
1001 }
1002 if ring.last().map(|(h, _)| *h) != Some(tip_height) {
1003 ring.push((tip_height, tip_hash));
1004 }
1005 if ring.len() > TIP_RING {
1006 let extra = ring.len() - TIP_RING;
1007 ring.drain(..extra);
1008 }
1009 (moved, ring)
1010}
1011
1012pub async fn tick<V: WalletServices + ?Sized, C: Clock>(
1014 storage: &StorageSqlx,
1015 services: &V,
1016 clock: &C,
1017 opts: &TickOptions,
1018) -> Result<TickReport> {
1019 ensure_tables(storage).await?;
1020 let params = opts.params();
1021 let now = clock.now();
1022 let mut report = TickReport::default();
1023
1024 let adopted = sqlx::query(
1027 "INSERT OR IGNORE INTO tracker_states (txid, word, updated_at) \
1028 SELECT DISTINCT lower(txid), 'built', ? FROM transactions \
1029 WHERE txid IS NOT NULL AND status IN ('unproven', 'sending')",
1030 )
1031 .bind(now as i64)
1032 .execute(storage.pool())
1033 .await?;
1034 report.adopted = adopted.rows_affected() as usize;
1035
1036 let mut snapshot = HeaderSnapshot::take(services, []).await;
1039 let (moved_from, ring) = moved_since(services, &mut snapshot, read_ring(storage).await).await;
1040 report.moved_from = moved_from;
1041
1042 let mut rows: Vec<TrackedRow> = sqlx::query(&format!(
1043 "SELECT {ROW_COLUMNS} FROM tracker_states \
1044 WHERE word IN ('unknown', 'built', 'announced', 'seen', 'stale') \
1045 OR (word = 'mined' AND ? IS NOT NULL AND height >= ?) \
1046 ORDER BY txid"
1047 ))
1048 .bind(moved_from.map(|h| h as i64))
1049 .bind(moved_from.map(|h| h as i64))
1050 .fetch_all(storage.pool())
1051 .await?
1052 .iter()
1053 .map(row_of)
1054 .collect();
1055 report.tracked = rows.len();
1056
1057 snapshot
1059 .extend(services, rows.iter().filter_map(TrackedRow::proof_height))
1060 .await;
1061 let mut hints = MemoryHints::read(storage, &rows).await?;
1062
1063 let mut states: HashMap<TxId, State> = HashMap::new();
1065 for row in rows.iter_mut() {
1066 let (state, fault) = row.replay(¶ms, &snapshot);
1067 if let Some(fault) = fault {
1068 if matches!(fault, CheckError::RootMismatch(_)) {
1071 row.bump = None;
1074 row.chain.clear();
1075 report.moved.push(row.txid.clone());
1076 tracing::warn!(
1077 marker = "tracker_stored_proof_moved",
1078 txid = %row.txid,
1079 "a stored merkle path no longer meets the active header; asking again"
1080 );
1081 } else {
1082 report.faults.push(format!("{}: {fault}", row.txid));
1083 }
1084 row.asks = 0;
1085 row.next_ask_at = None;
1086 }
1087 states.insert(row.txid.clone(), state);
1088 }
1089 while let Ok(Some((txid, hint))) = hints.next_hint() {
1090 let (Some(state), Some(row)) = (
1091 states.get_mut(&txid),
1092 rows.iter_mut().find(|r| r.txid == txid),
1093 ) else {
1094 continue;
1095 };
1096 row.hints.push(StoredHint::of(&hint));
1097 let _ = state.step(¶ms, &snapshot, Input::Hint(hint));
1098 report.hints += 1;
1099 if matches!(state.reask(), Some(Reask::Hint(_))) {
1102 row.asks = 0;
1103 row.next_ask_at = None;
1104 }
1105 }
1106 for row in rows.iter_mut() {
1112 let (Some(beef), Some(at)) = (row.reannounce_beef.clone(), row.reannounce_at) else {
1113 continue;
1114 };
1115 let Some(state) = states.get_mut(&row.txid) else {
1116 continue;
1117 };
1118 if at > now || matches!(state.word(), Word::Mined(_)) {
1119 continue;
1120 }
1121 let pause = Some(now.saturating_add(opts.age_threshold));
1122 let answer = services
1123 .post_beef(&beef, std::slice::from_ref(&row.txid))
1124 .await
1125 .map(|results| read_post(&results));
1126 match answer {
1127 Ok(PostAnswer::Accepted { provider }) => {
1128 let hint = Hint::new(format!("{provider}|accepted"), HintStatus::Accepted, now);
1129 row.hints.push(StoredHint::of(&hint));
1130 let _ = state.step(¶ms, &snapshot, Input::Hint(hint));
1131 row.reannounce_beef = None;
1132 row.reannounce_at = None;
1133 row.asks = 0;
1134 row.next_ask_at = pause;
1135 report.reannounced.push(row.txid.clone());
1136 if let Some(args) = row.on_accept.take() {
1137 report.on_accept.push((row.txid.clone(), args));
1138 }
1139 }
1140 Ok(PostAnswer::NotFinal { .. }) => {
1141 row.asks = row.asks.saturating_add(1);
1142 row.next_ask_at = pause;
1143 if row.asks >= MAX_ASKS_PER_TX {
1144 row.reannounce_at = None;
1145 report.faults.push(format!(
1146 "{}: still not final after {MAX_ASKS_PER_TX} announces; not announced again",
1147 row.txid
1148 ));
1149 } else {
1150 row.reannounce_at = pause;
1151 report.not_final.push(row.txid.clone());
1152 }
1153 }
1154 Ok(PostAnswer::Refused { said }) => {
1155 row.reannounce_at = None;
1156 row.next_ask_at = pause;
1157 report.faults.push(format!(
1158 "{}: refused when announced again: {said}",
1159 row.txid
1160 ));
1161 }
1162 Err(e) => {
1163 row.reannounce_at = pause;
1165 report
1166 .faults
1167 .push(format!("{}: could not announce again: {e}", row.txid));
1168 }
1169 }
1170 }
1171 for state in states.values_mut() {
1172 let _ = state.step(¶ms, &snapshot, Input::Host(HostAction::Tick(now)));
1173 }
1174
1175 let due: Vec<TxId> = rows
1177 .iter()
1178 .filter(|row| {
1179 states[&row.txid].reask().is_some()
1180 && row.asks < MAX_ASKS_PER_TX
1181 && row.next_ask_at.is_none_or(|at| at <= now)
1182 })
1183 .take(opts.max_asks)
1184 .map(|row| row.txid.clone())
1185 .collect();
1186 let mut courier = Courier::carry(services, &due).await;
1187 snapshot.extend(services, courier.heights()).await;
1188
1189 for txid in &due {
1191 let (Some(state), Some(row)) = (
1192 states.get_mut(txid),
1193 rows.iter_mut().find(|r| &r.txid == txid),
1194 ) else {
1195 continue;
1196 };
1197 report.asked += 1;
1198 let reask = state.reask().cloned().unwrap_or(Reask::Age);
1199 let fetched = courier.fetch(txid, &reask);
1200 let proven = match fetched {
1201 Ok(Some(proof)) => {
1202 let hex = proof.path().to_hex();
1203 match state.step(¶ms, &snapshot, Input::Evidence(Evidence::Proof(proof))) {
1204 Ok(()) => {
1205 row.bump = Some(hex);
1206 row.chain.clear();
1207 true
1208 }
1209 Err(fault) => {
1210 report.faults.push(format!("{txid}: {fault}"));
1211 false
1212 }
1213 }
1214 }
1215 Ok(None) => {
1216 report.no_proof.push(txid.clone());
1217 false
1218 }
1219 Err(fault) => {
1220 report
1221 .faults
1222 .push(format!("{txid}: could not ask for a proof: {fault}"));
1223 false
1224 }
1225 };
1226 if proven {
1227 row.asks = 0;
1228 row.next_ask_at = None;
1229 report.mined.push(txid.clone());
1230 } else {
1231 let pause = opts
1232 .age_threshold
1233 .saturating_mul(1 << row.asks.min(MAX_BACKOFF_DOUBLINGS));
1234 row.asks = row.asks.saturating_add(1);
1235 row.next_ask_at = Some(now.saturating_add(pause));
1236 }
1237 }
1238
1239 for row in &rows {
1242 let state = &states[&row.txid];
1243 store_row(storage, row, state, now).await?;
1244 if let (true, Word::Mined(mined)) = (report.mined.contains(&row.txid), state.word()) {
1245 let checked = mined.checked();
1246 let outcome = storage
1247 .ingest_merkle_proof(
1248 &row.txid,
1249 &checked.proof().path().to_binary(),
1250 checked.height(),
1251 &checked.header().hash,
1252 Some(checked.root()),
1253 )
1254 .await;
1255 report.stored.push(match outcome {
1256 Ok(outcome) => format!("{}: {outcome:?}", row.txid),
1257 Err(e) => format!("{}: not stored: {e}", row.txid),
1258 });
1259 }
1260 }
1261 write_ring(storage, &ring).await?;
1262 Ok(report)
1263}
1264
1265pub async fn record_accepted<W: WalletInterface>(wallet: &W, report: &mut TickReport) {
1270 for (txid, args) in std::mem::take(&mut report.on_accept) {
1271 let line = match serde_json::from_str::<InternalizeActionArgs>(&args) {
1272 Ok(args) => match wallet.internalize_action(args, "bsv-wallet-cli").await {
1273 Ok(result) if result.accepted => format!("{txid}: recorded in the wallet"),
1274 Ok(_) => format!("{txid}: the wallet did not accept its own transaction"),
1275 Err(e) => format!(
1276 "{txid}: not recorded in the wallet ({e}); `bsv-wallet receive {txid}` records it once it is mined"
1277 ),
1278 },
1279 Err(e) => format!("{txid}: not recorded in the wallet (stored arguments: {e})"),
1280 };
1281 if !line.ends_with("recorded in the wallet") {
1282 tracing::warn!(marker = "own_transaction_not_recorded", "{line}");
1283 }
1284 report.stored.push(line);
1285 }
1286}
1287
1288fn readme_spend_guard<H: Headers>(
1297 state: &mut State,
1298 params: &Params,
1299 headers: &H,
1300) -> std::result::Result<bool, CheckError> {
1301 state.step(params, headers, Input::Host(HostAction::SpendAttempt))?;
1302 state.step(params, headers, Input::Evidence(Evidence::Recheck))?;
1303 Ok(matches!(state.word(), Word::Mined(_)) && !state.suspect())
1304}
1305
1306pub async fn spend_guard<V: WalletServices + ?Sized, C: Clock>(
1313 storage: &StorageSqlx,
1314 services: &V,
1315 clock: &C,
1316 opts: &TickOptions,
1317 txid: &str,
1318) -> Result<std::result::Result<bool, CheckError>> {
1319 ensure_tables(storage).await?;
1320 let Some(row) = load_row(storage, txid).await? else {
1321 return Ok(Ok(false));
1322 };
1323 let params = opts.params();
1324 let snapshot = HeaderSnapshot::take(services, row.proof_height()).await;
1325 let (mut state, fault) = row.replay(¶ms, &snapshot);
1326 match fault {
1327 Some(fault) if !matches!(fault, CheckError::RootMismatch(_)) => return Ok(Err(fault)),
1331 _ => {}
1332 }
1333 let verdict = readme_spend_guard(&mut state, ¶ms, &snapshot)
1334 .and_then(|offered| Ok(offered && deep_enough(&state, &snapshot, opts.spend_depth)?));
1335 store_row(storage, &row, &state, clock.now()).await?;
1336 Ok(verdict)
1337}
1338
1339#[derive(Debug, Clone, PartialEq, Eq)]
1342pub struct CreatorReading {
1343 pub word: &'static str,
1345 pub depth: u64,
1348 pub suspect: bool,
1350 pub offered: bool,
1353 pub fault: Option<String>,
1356}
1357
1358pub async fn read_creators<V: WalletServices + ?Sized>(
1367 storage: &StorageSqlx,
1368 services: &V,
1369 opts: &TickOptions,
1370 creators: &[(String, Option<Vec<u8>>)],
1371) -> Result<HashMap<String, CreatorReading>> {
1372 ensure_tables(storage).await?;
1373 let params = opts.params();
1374 let mut rows = Vec::with_capacity(creators.len());
1375 for (txid, stored_path) in creators {
1376 let mut row = match load_row(storage, txid).await? {
1377 Some(row) => row,
1378 None => TrackedRow {
1379 built: false,
1380 ..TrackedRow::new(txid)
1381 },
1382 };
1383 if row.bump.is_none() {
1384 row.bump = stored_path
1385 .as_deref()
1386 .and_then(|bytes| MerklePath::from_binary(bytes).ok())
1387 .map(|path| path.to_hex());
1388 }
1389 rows.push(row);
1390 }
1391 let snapshot =
1392 HeaderSnapshot::take(services, rows.iter().filter_map(TrackedRow::proof_height)).await;
1393 let mut readings = HashMap::with_capacity(rows.len());
1394 for row in rows {
1395 let (mut state, fault) = row.replay(¶ms, &snapshot);
1396 let fault = match fault {
1397 Some(fault) if !matches!(fault, CheckError::RootMismatch(_)) => Some(fault.to_string()),
1398 _ => None,
1399 };
1400 let verdict = match fault {
1401 Some(_) => Ok(false),
1402 None => readme_spend_guard(&mut state, ¶ms, &snapshot).and_then(|offered| {
1403 Ok(offered && deep_enough(&state, &snapshot, opts.spend_depth)?)
1404 }),
1405 };
1406 let depth = match (state.word().height(), snapshot.tip()) {
1407 (Some(height), Some((tip, _))) => {
1408 (u64::from(tip) + 1).saturating_sub(u64::from(height))
1409 }
1410 _ => 0,
1411 };
1412 let (offered, fault) = match verdict {
1413 Ok(offered) => (offered, fault),
1414 Err(e) => (false, fault.or(Some(e.to_string()))),
1415 };
1416 readings.insert(
1417 row.txid.clone(),
1418 CreatorReading {
1419 word: word_label(state.word()),
1420 depth,
1421 suspect: state.suspect(),
1422 offered,
1423 fault,
1424 },
1425 );
1426 }
1427 Ok(readings)
1428}
1429
1430fn deep_enough<H: Headers>(
1435 state: &State,
1436 headers: &H,
1437 spend_depth: Height,
1438) -> std::result::Result<bool, CheckError> {
1439 let Some(height) = state.word().height() else {
1440 return Ok(false);
1441 };
1442 let tip = headers.tip_height().map_err(CheckError::Headers)?;
1443 let depth = (u64::from(tip) + 1).saturating_sub(u64::from(height));
1444 Ok(depth >= u64::from(spend_depth.max(DEFAULT_SPEND_DEPTH)))
1445}
1446
1447pub async fn feed_cursor(storage: &StorageSqlx) -> Result<u64> {
1454 ensure_tables(storage).await?;
1455 let value: Option<String> =
1456 sqlx::query_scalar("SELECT value FROM tracker_meta WHERE key = 'feed_cursor'")
1457 .fetch_optional(storage.pool())
1458 .await?;
1459 Ok(match value {
1460 Some(v) => v
1461 .parse()
1462 .map_err(|e| anyhow::anyhow!("the stored feed cursor {v:?}: {e}"))?,
1463 None => 0,
1464 })
1465}
1466
1467#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1469pub struct ChainMove {
1470 pub txid: String,
1472 pub word: String,
1474 pub reask: Option<String>,
1476 pub suspect: bool,
1478 pub fault: Option<String>,
1480}
1481
1482#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1484pub struct ChainDelivery {
1485 pub cursor: u64,
1487 pub kind: &'static str,
1489 pub moved: Vec<ChainMove>,
1491}
1492
1493pub fn event_kind(event: &ChainEvent) -> &'static str {
1495 match event {
1496 ChainEvent::Tip { .. } => "tip",
1497 ChainEvent::Fork { .. } => "fork",
1498 ChainEvent::Reorg { .. } => "reorg",
1499 ChainEvent::Invalidated { .. } => "invalidated",
1500 ChainEvent::Frozen { .. } => "frozen",
1501 ChainEvent::TipAge { .. } => "tipAge",
1502 ChainEvent::Split { .. } => "split",
1503 ChainEvent::SplitResolved { .. } => "splitResolved",
1504 }
1505}
1506
1507struct Replaced<'a> {
1514 snapshot: &'a HeaderSnapshot,
1515 replaced: BTreeMap<Height, Header>,
1516 prior_tip: Option<Height>,
1517}
1518
1519impl Headers for Replaced<'_> {
1520 fn header_at(&self, height: Height) -> std::result::Result<Option<Header>, HeaderError> {
1521 match self.replaced.get(&height) {
1522 Some(header) => Ok(Some(header.clone())),
1523 None => self.snapshot.header_at(height),
1524 }
1525 }
1526
1527 fn tip_height(&self) -> std::result::Result<Height, HeaderError> {
1528 match self.prior_tip {
1529 Some(tip) => Ok(tip),
1530 None => self.snapshot.tip_height(),
1531 }
1532 }
1533}
1534
1535pub async fn deliver_chain_event<V: WalletServices + ?Sized, C: Clock>(
1548 storage: &StorageSqlx,
1549 services: &V,
1550 clock: &C,
1551 opts: &TickOptions,
1552 cursor: u64,
1553 event: ChainEvent,
1554) -> Result<ChainDelivery> {
1555 let kind = event_kind(&event);
1556 if cursor <= feed_cursor(storage).await? {
1557 return Ok(ChainDelivery {
1558 cursor,
1559 kind,
1560 moved: vec![],
1561 });
1562 }
1563 let named = format!("SELECT {ROW_COLUMNS} FROM tracker_states");
1564 let query = match &event {
1565 ChainEvent::Fork { height: from, .. }
1566 | ChainEvent::Reorg {
1567 fork_height: from, ..
1568 }
1569 | ChainEvent::Split {
1570 fork_height: from, ..
1571 }
1572 | ChainEvent::SplitResolved {
1573 fork_height: from, ..
1574 } => Some(
1575 sqlx::query(&format!(
1576 "{named} WHERE word = 'mined' AND height >= ? ORDER BY txid"
1577 ))
1578 .bind(i64::from(*from))
1579 .fetch_all(storage.pool())
1580 .await?,
1581 ),
1582 ChainEvent::Tip { .. } => Some(
1583 sqlx::query(&format!(
1584 "{named} WHERE word = 'mined' AND chain <> '[]' ORDER BY txid"
1585 ))
1586 .fetch_all(storage.pool())
1587 .await?,
1588 ),
1589 ChainEvent::Invalidated { block_hash } => Some(
1590 sqlx::query(&format!("{named} WHERE header_hash = ? ORDER BY txid"))
1591 .bind(block_hash.to_ascii_lowercase())
1592 .fetch_all(storage.pool())
1593 .await?,
1594 ),
1595 ChainEvent::Frozen { .. } | ChainEvent::TipAge { .. } => None,
1596 };
1597 let mut rows: Vec<TrackedRow> = query.iter().flatten().map(row_of).collect();
1598
1599 let now = clock.now();
1600 let mut moved = Vec::new();
1601 let mut tx = storage.pool().begin().await?;
1602 if !rows.is_empty() {
1603 let snapshot =
1604 HeaderSnapshot::take(services, rows.iter().filter_map(TrackedRow::proof_height)).await;
1605 let (replaced, prior_tip) = match &event {
1606 ChainEvent::Reorg {
1607 deactivated_headers,
1608 ..
1609 } => (
1610 deactivated_headers
1611 .iter()
1612 .map(|h| {
1613 (
1614 h.height,
1615 Header {
1616 hash: h.hash.to_ascii_lowercase(),
1617 merkle_root: h.merkle_root.to_ascii_lowercase(),
1618 },
1619 )
1620 })
1621 .collect(),
1622 deactivated_headers.first().map(|h| h.height),
1623 ),
1624 _ => (BTreeMap::new(), None),
1625 };
1626 let replay_view = Replaced {
1627 snapshot: &snapshot,
1628 replaced,
1629 prior_tip,
1630 };
1631 let mut tracker = Tracker::new(opts.params());
1632 for row in &rows {
1633 row.replay_into(&mut tracker, &replay_view);
1634 }
1635 for update in tracker.on_chain(&snapshot, event.clone()) {
1636 let (Some(state), Some(row)) = (
1637 tracker.get(&update.txid),
1638 rows.iter_mut().find(|r| r.txid == update.txid),
1639 ) else {
1640 continue;
1641 };
1642 row.keep_chain(state, &event)?;
1643 if matches!(update.reask, Some(Reask::Reorg { .. })) {
1644 row.asks = 0;
1646 row.next_ask_at = None;
1647 }
1648 store_row_in(&mut *tx, row, state, now).await?;
1649 let fault = update.result.err().map(|e| e.to_string());
1650 if let Some(fault) = &fault {
1651 tracing::warn!(txid = %update.txid, kind, cursor, %fault, "a chain event's recheck could not look");
1652 }
1653 moved.push(ChainMove {
1654 txid: update.txid,
1655 word: word_label(state.word()).to_string(),
1656 reask: state.reask().map(reask_label),
1657 suspect: state.suspect(),
1658 fault,
1659 });
1660 }
1661 }
1662 sqlx::query(
1663 "INSERT INTO tracker_meta (key, value) VALUES ('feed_cursor', ?) \
1664 ON CONFLICT(key) DO UPDATE SET value = excluded.value",
1665 )
1666 .bind(cursor.to_string())
1667 .execute(&mut *tx)
1668 .await?;
1669 tx.commit().await?;
1670 Ok(ChainDelivery {
1671 cursor,
1672 kind,
1673 moved,
1674 })
1675}
1676
1677#[cfg(test)]
1678pub(crate) mod tests {
1679 use super::*;
1680 use bsv_wallet_toolbox::services::mock::{MockResponse, MockWalletServices};
1681 use bsv_wallet_toolbox::services::{BlockHeader, GetMerklePathResult};
1682 use bsv_wallet_toolbox::{
1683 WalletStorageWriter, BROADCAST_STATUS_ACCEPTED, BROADCAST_STATUS_MINED,
1684 };
1685 use chrono::Utc;
1686
1687 pub(crate) struct FixedClock(pub Timestamp);
1689 impl Clock for FixedClock {
1690 fn now(&self) -> Timestamp {
1691 self.0
1692 }
1693 }
1694
1695 pub(crate) fn opts() -> TickOptions {
1696 TickOptions {
1697 age_threshold: 600,
1698 max_asks: 20,
1699 spend_depth: DEFAULT_SPEND_DEPTH,
1700 }
1701 }
1702
1703 pub(crate) async fn wallet_storage() -> (StorageSqlx, i64) {
1705 let storage = StorageSqlx::in_memory().await.unwrap();
1706 storage
1707 .migrate("tracker-tests", &("02".to_string() + &"ab".repeat(32)))
1708 .await
1709 .unwrap();
1710 storage.make_available().await.unwrap();
1711 let (user, _) = storage
1712 .find_or_insert_user(&("02".to_string() + &"cd".repeat(32)))
1713 .await
1714 .unwrap();
1715 (storage, user.user_id)
1716 }
1717
1718 pub(crate) async fn insert_tx(storage: &StorageSqlx, user_id: i64, txid: &str, status: &str) {
1719 sqlx::query(
1720 "INSERT INTO transactions (user_id, status, reference, is_outgoing, satoshis, version, lock_time, description, txid, raw_tx, created_at, updated_at) \
1721 VALUES (?, ?, ?, 1, 0, 1, 0, 'd', ?, X'01000000', ?, ?)",
1722 )
1723 .bind(user_id)
1724 .bind(status)
1725 .bind(&txid[..8])
1726 .bind(txid)
1727 .bind(Utc::now())
1728 .bind(Utc::now())
1729 .execute(storage.pool())
1730 .await
1731 .unwrap();
1732 }
1733
1734 pub(crate) fn header(height: u32, root: &str, nonce: u32) -> BlockHeader {
1736 BlockHeader {
1737 version: 1,
1738 previous_hash: "00".repeat(32),
1739 merkle_root: root.to_string(),
1740 time: 1_700_000_000,
1741 bits: 0x1d00_ffff,
1742 nonce,
1743 hash: String::new(),
1744 height,
1745 }
1746 }
1747
1748 pub(crate) fn path_of(txid: &str, height: u32) -> String {
1751 MerklePath::from_coinbase_txid(txid, height).to_hex()
1752 }
1753
1754 pub(crate) fn proof_answer(path: Option<String>) -> MockResponse<GetMerklePathResult> {
1755 MockResponse::Success(GetMerklePathResult {
1756 name: Some("Services".to_string()),
1757 merkle_path: path,
1758 header: None,
1759 error: None,
1760 notes: vec![],
1761 })
1762 }
1763
1764 #[tokio::test]
1771 async fn a_pass_tracks_announces_and_proves_one_named_transaction() {
1772 let (storage, user) = wallet_storage().await;
1773 let txid = "a1".repeat(32);
1774 insert_tx(&storage, user, &txid, "unproven").await;
1775 storage
1776 .record_broadcast_status(&txid, "arcade", BROADCAST_STATUS_ACCEPTED)
1777 .await
1778 .unwrap();
1779 let services = MockWalletServices::builder()
1780 .get_merkle_path_response(proof_answer(Some(path_of(&txid, 900))))
1781 .build();
1782 services.set_header_for_height(header(900, &txid, 0));
1783 let now = Utc::now().timestamp() as u64;
1784
1785 let report = tick(&storage, &services, &FixedClock(now), &opts())
1787 .await
1788 .unwrap();
1789 assert_eq!((report.adopted, report.hints, report.asked), (1, 1, 0));
1790 assert_eq!(
1791 stored_word(&storage, &txid).await.unwrap(),
1792 Some(("announced".to_string(), None, None))
1793 );
1794 assert_eq!(services.call_count("get_merkle_path"), 0);
1795
1796 let report = tick(&storage, &services, &FixedClock(now + 601), &opts())
1798 .await
1799 .unwrap();
1800 assert_eq!(report.asked, 1);
1801 assert_eq!(report.mined, vec![txid.clone()]);
1802 assert_eq!(
1803 stored_word(&storage, &txid).await.unwrap(),
1804 Some(("mined".to_string(), Some(900), None))
1805 );
1806 assert_eq!(services.call_count("get_merkle_path"), 1);
1807
1808 let report = tick(&storage, &services, &FixedClock(now + 1300), &opts())
1810 .await
1811 .unwrap();
1812 assert_eq!((report.tracked, report.asked), (0, 0));
1813 assert_eq!(services.call_count("get_merkle_path"), 1);
1814 }
1815
1816 #[tokio::test]
1820 async fn a_broadcasters_mined_is_a_hint_that_asks_for_the_proof() {
1821 let (storage, user) = wallet_storage().await;
1822 let txid = "b2".repeat(32);
1823 insert_tx(&storage, user, &txid, "unproven").await;
1824 storage
1825 .record_broadcast_status(&txid, "arcade", BROADCAST_STATUS_MINED)
1826 .await
1827 .unwrap();
1828 let services = MockWalletServices::builder()
1829 .get_merkle_path_response(proof_answer(None))
1830 .build();
1831 let now = Utc::now().timestamp() as u64;
1832
1833 let report = tick(&storage, &services, &FixedClock(now), &opts())
1834 .await
1835 .unwrap();
1836 assert_eq!(report.asked, 1);
1837 assert_eq!(report.no_proof, vec![txid.clone()]);
1838 assert!(report.mined.is_empty());
1839 let (word, height, reask) = stored_word(&storage, &txid).await.unwrap().unwrap();
1840 assert_eq!((word.as_str(), height), ("built", None));
1841 assert_eq!(reask.as_deref(), Some("hint:arcade|mined"));
1842
1843 let report = tick(&storage, &services, &FixedClock(now + 60), &opts())
1845 .await
1846 .unwrap();
1847 assert_eq!(report.asked, 0);
1848 let report = tick(&storage, &services, &FixedClock(now + 600), &opts())
1850 .await
1851 .unwrap();
1852 assert_eq!(report.asked, 1);
1853 let report = tick(&storage, &services, &FixedClock(now + 1200), &opts())
1854 .await
1855 .unwrap();
1856 assert_eq!(report.asked, 0);
1857 assert_eq!(services.call_count("get_merkle_path"), 2);
1858 }
1859
1860 #[tokio::test]
1863 async fn a_moved_header_reasks_the_rows_at_or_above_it_and_no_other() {
1864 let (storage, user) = wallet_storage().await;
1865 let low = "c3".repeat(32);
1866 let high = "d4".repeat(32);
1867 let services = MockWalletServices::builder()
1868 .get_merkle_path_response(MockResponse::Sequence(vec![
1869 proof_answer(Some(path_of(&low, 100))),
1870 proof_answer(Some(path_of(&high, 200))),
1871 proof_answer(Some(path_of(&high, 201))),
1872 ]))
1873 .build();
1874 services.set_header_for_height(header(100, &low, 0));
1875 services.set_header_for_height(header(200, &high, 0));
1876 services.set_tip_header(header_with_hash(200, &high, 0));
1877 let now = Utc::now().timestamp() as u64;
1878 for txid in [&low, &high] {
1879 insert_tx(&storage, user, txid, "unproven").await;
1880 storage
1881 .record_broadcast_status(txid, "arcade", BROADCAST_STATUS_MINED)
1882 .await
1883 .unwrap();
1884 }
1885 let report = tick(&storage, &services, &FixedClock(now), &opts())
1886 .await
1887 .unwrap();
1888 assert_eq!(report.mined.len(), 2, "{report:?}");
1889
1890 services.set_header_for_height(header(200, &"ee".repeat(32), 7));
1893 services.set_header_for_height(header(201, &high, 0));
1894 services.set_tip_header(header_with_hash(201, &high, 0));
1895 let report = tick(&storage, &services, &FixedClock(now + 60), &opts())
1896 .await
1897 .unwrap();
1898 assert_eq!(report.moved_from, Some(200));
1899 assert_eq!(
1900 report.tracked, 1,
1901 "only the row at or above the moved header"
1902 );
1903 assert_eq!(report.moved, vec![high.clone()]);
1904 assert_eq!(report.mined, vec![high.clone()]);
1905 assert_eq!(
1906 stored_word(&storage, &high).await.unwrap().unwrap().1,
1907 Some(201)
1908 );
1909 assert_eq!(
1910 stored_word(&storage, &low).await.unwrap().unwrap().1,
1911 Some(100)
1912 );
1913 }
1914
1915 #[tokio::test]
1921 async fn a_not_final_transaction_is_announced_again_at_its_lock_time() {
1922 use bsv_wallet_toolbox::services::mock::{
1923 error_post_beef_result, success_post_beef_result,
1924 };
1925 let (storage, _user) = wallet_storage().await;
1926 let txid = "c7".repeat(32);
1927 let lock = 2_000_000_000u64;
1928 let services = MockWalletServices::builder()
1929 .post_beef_response(MockResponse::Sequence(vec![
1930 MockResponse::Success(vec![error_post_beef_result("Arcade", "476 non-final")]),
1931 MockResponse::Success(vec![success_post_beef_result("Arcade", &[&txid])]),
1932 ]))
1933 .get_merkle_path_response(proof_answer(None))
1934 .build();
1935
1936 let hint = Hint::new(
1937 "Arcade|rejected",
1938 HintStatus::Rejected {
1939 reason: "476 non-final".to_string(),
1940 },
1941 lock - 3600,
1942 );
1943 let again = Reannounce {
1944 beef: vec![1, 2, 3],
1945 at: lock,
1946 on_accept: None,
1947 };
1948 let word = heard(
1949 &storage,
1950 &FixedClock(lock - 3600),
1951 &opts(),
1952 &txid,
1953 hint,
1954 Some(lock),
1955 Some(again),
1956 )
1957 .await
1958 .unwrap();
1959 assert_eq!(
1960 word,
1961 (
1962 "built".to_string(),
1963 Some("hint:Arcade|rejected".to_string())
1964 )
1965 );
1966
1967 let report = tick(&storage, &services, &FixedClock(lock - 1), &opts())
1969 .await
1970 .unwrap();
1971 assert_eq!((report.tracked, report.asked), (1, 0), "{report:?}");
1972 assert_eq!(services.call_count("post_beef"), 0);
1973
1974 let report = tick(&storage, &services, &FixedClock(lock), &opts())
1976 .await
1977 .unwrap();
1978 assert_eq!(report.not_final, vec![txid.clone()]);
1979 assert_eq!(services.call_count("post_beef"), 1);
1980 let report = tick(&storage, &services, &FixedClock(lock + 60), &opts())
1981 .await
1982 .unwrap();
1983 assert!(report.not_final.is_empty() && report.reannounced.is_empty());
1984 assert_eq!(services.call_count("post_beef"), 1, "one pause between two");
1985
1986 let report = tick(&storage, &services, &FixedClock(lock + 600), &opts())
1988 .await
1989 .unwrap();
1990 assert_eq!(report.reannounced, vec![txid.clone()]);
1991 assert_eq!(services.call_count("post_beef"), 2);
1992 assert_eq!(
1993 stored_word(&storage, &txid).await.unwrap().unwrap().0,
1994 "announced"
1995 );
1996 assert_eq!(services.call_count("get_merkle_path"), 0);
1997
1998 let report = tick(&storage, &services, &FixedClock(lock + 1300), &opts())
2000 .await
2001 .unwrap();
2002 assert_eq!(report.asked, 1);
2003 assert_eq!(services.call_count("post_beef"), 2);
2004 assert_eq!(services.call_count("get_merkle_path"), 1);
2005 }
2006
2007 #[test]
2009 fn not_final_is_read_from_the_refusal() {
2010 for said in [
2011 "476 non-final",
2012 "bad-txns-nonfinal",
2013 "tx is not final",
2014 "code 476",
2015 ] {
2016 assert!(says_not_final(said), "{said}");
2017 }
2018 for said in [
2019 "fee too low",
2020 "DOUBLE_SPEND_ATTEMPTED",
2021 "txid 4476aa",
2022 "HTTP 500",
2023 ] {
2024 assert!(!says_not_final(said), "{said}");
2025 }
2026 }
2027
2028 pub(crate) fn header_with_hash(height: u32, root: &str, nonce: u32) -> BlockHeader {
2030 let mut h = header(height, root, nonce);
2031 h.hash = header_of(&h.to_binary()).unwrap().hash;
2032 h
2033 }
2034
2035 #[tokio::test]
2040 async fn the_spend_depth_holds_a_coin_until_its_block_is_deep_enough() {
2041 let (storage, user) = wallet_storage().await;
2042 let txid = "e7".repeat(32);
2043 insert_tx(&storage, user, &txid, "unproven").await;
2044 storage
2045 .record_broadcast_status(&txid, "arcade", BROADCAST_STATUS_MINED)
2046 .await
2047 .unwrap();
2048 let services = MockWalletServices::builder()
2049 .get_merkle_path_response(proof_answer(Some(path_of(&txid, 900))))
2050 .build();
2051 services.set_header_for_height(header(900, &txid, 0));
2052 services.set_tip_header(header_with_hash(900, &txid, 0));
2053 let clock = FixedClock(Utc::now().timestamp() as u64);
2054 tick(&storage, &services, &clock, &opts()).await.unwrap();
2055 let at = |spend_depth| TickOptions {
2056 spend_depth,
2057 ..opts()
2058 };
2059
2060 let one = spend_guard(&storage, &services, &clock, &at(1), &txid)
2061 .await
2062 .unwrap();
2063 let three = spend_guard(&storage, &services, &clock, &at(3), &txid)
2064 .await
2065 .unwrap();
2066 let zero = spend_guard(&storage, &services, &clock, &at(0), &txid)
2067 .await
2068 .unwrap();
2069 println!("one block deep: depth 1 {one:?}, depth 3 {three:?}, depth 0 {zero:?}");
2070 assert_eq!(one, Ok(true));
2071 assert_eq!(three, Ok(false));
2072 assert_eq!(zero, Ok(true), "0 reads as the floor, 1");
2073
2074 services.set_tip_header(header_with_hash(902, &"0c".repeat(32), 0));
2075 let three = spend_guard(&storage, &services, &clock, &at(3), &txid)
2076 .await
2077 .unwrap();
2078 println!("three blocks deep: depth 3 {three:?}");
2079 assert_eq!(three, Ok(true));
2080 assert_eq!(
2081 TickOptions {
2082 spend_depth: 0,
2083 ..opts()
2084 }
2085 .floored()
2086 .spend_depth,
2087 1
2088 );
2089 }
2090
2091 #[tokio::test]
2096 async fn the_readme_spend_guard_runs_as_the_host() {
2097 let (storage, user) = wallet_storage().await;
2098 let txid = "e5".repeat(32);
2099 insert_tx(&storage, user, &txid, "unproven").await;
2100 storage
2101 .record_broadcast_status(&txid, "arcade", BROADCAST_STATUS_MINED)
2102 .await
2103 .unwrap();
2104 let services = MockWalletServices::builder()
2105 .get_merkle_path_response(proof_answer(Some(path_of(&txid, 900))))
2106 .build();
2107 services.set_header_for_height(header(900, &txid, 0));
2108 let clock = FixedClock(Utc::now().timestamp() as u64);
2109 tick(&storage, &services, &clock, &opts()).await.unwrap();
2110
2111 let guard = spend_guard(&storage, &services, &clock, &opts(), &txid)
2113 .await
2114 .unwrap();
2115 println!(
2116 "spend guard, header in place: {guard:?}, stored {:?}",
2117 stored_word(&storage, &txid).await.unwrap().unwrap()
2118 );
2119 assert_eq!(guard, Ok(true));
2120
2121 services.set_tip_unavailable();
2123 let guard = spend_guard(&storage, &services, &clock, &opts(), &txid)
2124 .await
2125 .unwrap();
2126 println!(
2127 "spend guard, no header service: {guard:?}, stored {:?}",
2128 stored_word(&storage, &txid).await.unwrap().unwrap()
2129 );
2130 assert!(matches!(guard, Err(CheckError::Headers(_))), "{guard:?}");
2131 assert_eq!(
2132 stored_word(&storage, &txid).await.unwrap().unwrap().0,
2133 "mined",
2134 "could not look changes no word"
2135 );
2136
2137 services.set_tip_header(header_with_hash(901, &"00".repeat(32), 1));
2139 services.set_header_for_height(header(900, &"ee".repeat(32), 7));
2140 let guard = spend_guard(&storage, &services, &clock, &opts(), &txid)
2141 .await
2142 .unwrap();
2143 println!(
2144 "spend guard, header moved: {guard:?}, stored {:?}",
2145 stored_word(&storage, &txid).await.unwrap().unwrap()
2146 );
2147 assert_eq!(guard, Ok(false));
2148
2149 let guard = spend_guard(&storage, &services, &clock, &opts(), &"f6".repeat(32))
2151 .await
2152 .unwrap();
2153 assert_eq!(guard, Ok(false));
2154
2155 let params = opts().params();
2158 services.set_tip_header(header_with_hash(900, &txid, 0));
2159 services.set_header_for_height(header(900, &txid, 0));
2160 let held = HeaderSnapshot::take(&services, [900]).await;
2161 let row = load_row(&storage, &txid).await.unwrap().unwrap();
2162 let (mut state, fault) = row.replay(¶ms, &held);
2163 assert_eq!(fault, None);
2164 let lost = HeaderSnapshot::take(&services, []).await;
2165 let guard = readme_spend_guard(&mut state, ¶ms, &lost);
2166 println!("spend guard, height unanswered: {guard:?}");
2167 assert_eq!(guard, Err(CheckError::Unavailable(900)));
2168 }
2169}