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
1339fn deep_enough<H: Headers>(
1344 state: &State,
1345 headers: &H,
1346 spend_depth: Height,
1347) -> std::result::Result<bool, CheckError> {
1348 let Some(height) = state.word().height() else {
1349 return Ok(false);
1350 };
1351 let tip = headers.tip_height().map_err(CheckError::Headers)?;
1352 let depth = (u64::from(tip) + 1).saturating_sub(u64::from(height));
1353 Ok(depth >= u64::from(spend_depth.max(DEFAULT_SPEND_DEPTH)))
1354}
1355
1356pub async fn feed_cursor(storage: &StorageSqlx) -> Result<u64> {
1363 ensure_tables(storage).await?;
1364 let value: Option<String> =
1365 sqlx::query_scalar("SELECT value FROM tracker_meta WHERE key = 'feed_cursor'")
1366 .fetch_optional(storage.pool())
1367 .await?;
1368 Ok(match value {
1369 Some(v) => v
1370 .parse()
1371 .map_err(|e| anyhow::anyhow!("the stored feed cursor {v:?}: {e}"))?,
1372 None => 0,
1373 })
1374}
1375
1376#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1378pub struct ChainMove {
1379 pub txid: String,
1381 pub word: String,
1383 pub reask: Option<String>,
1385 pub suspect: bool,
1387 pub fault: Option<String>,
1389}
1390
1391#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1393pub struct ChainDelivery {
1394 pub cursor: u64,
1396 pub kind: &'static str,
1398 pub moved: Vec<ChainMove>,
1400}
1401
1402pub fn event_kind(event: &ChainEvent) -> &'static str {
1404 match event {
1405 ChainEvent::Tip { .. } => "tip",
1406 ChainEvent::Fork { .. } => "fork",
1407 ChainEvent::Reorg { .. } => "reorg",
1408 ChainEvent::Invalidated { .. } => "invalidated",
1409 ChainEvent::Frozen { .. } => "frozen",
1410 ChainEvent::TipAge { .. } => "tipAge",
1411 ChainEvent::Split { .. } => "split",
1412 ChainEvent::SplitResolved { .. } => "splitResolved",
1413 }
1414}
1415
1416struct Replaced<'a> {
1423 snapshot: &'a HeaderSnapshot,
1424 replaced: BTreeMap<Height, Header>,
1425 prior_tip: Option<Height>,
1426}
1427
1428impl Headers for Replaced<'_> {
1429 fn header_at(&self, height: Height) -> std::result::Result<Option<Header>, HeaderError> {
1430 match self.replaced.get(&height) {
1431 Some(header) => Ok(Some(header.clone())),
1432 None => self.snapshot.header_at(height),
1433 }
1434 }
1435
1436 fn tip_height(&self) -> std::result::Result<Height, HeaderError> {
1437 match self.prior_tip {
1438 Some(tip) => Ok(tip),
1439 None => self.snapshot.tip_height(),
1440 }
1441 }
1442}
1443
1444pub async fn deliver_chain_event<V: WalletServices + ?Sized, C: Clock>(
1457 storage: &StorageSqlx,
1458 services: &V,
1459 clock: &C,
1460 opts: &TickOptions,
1461 cursor: u64,
1462 event: ChainEvent,
1463) -> Result<ChainDelivery> {
1464 let kind = event_kind(&event);
1465 if cursor <= feed_cursor(storage).await? {
1466 return Ok(ChainDelivery {
1467 cursor,
1468 kind,
1469 moved: vec![],
1470 });
1471 }
1472 let named = format!("SELECT {ROW_COLUMNS} FROM tracker_states");
1473 let query = match &event {
1474 ChainEvent::Fork { height: from, .. }
1475 | ChainEvent::Reorg {
1476 fork_height: from, ..
1477 }
1478 | ChainEvent::Split {
1479 fork_height: from, ..
1480 }
1481 | ChainEvent::SplitResolved {
1482 fork_height: from, ..
1483 } => Some(
1484 sqlx::query(&format!(
1485 "{named} WHERE word = 'mined' AND height >= ? ORDER BY txid"
1486 ))
1487 .bind(i64::from(*from))
1488 .fetch_all(storage.pool())
1489 .await?,
1490 ),
1491 ChainEvent::Tip { .. } => Some(
1492 sqlx::query(&format!(
1493 "{named} WHERE word = 'mined' AND chain <> '[]' ORDER BY txid"
1494 ))
1495 .fetch_all(storage.pool())
1496 .await?,
1497 ),
1498 ChainEvent::Invalidated { block_hash } => Some(
1499 sqlx::query(&format!("{named} WHERE header_hash = ? ORDER BY txid"))
1500 .bind(block_hash.to_ascii_lowercase())
1501 .fetch_all(storage.pool())
1502 .await?,
1503 ),
1504 ChainEvent::Frozen { .. } | ChainEvent::TipAge { .. } => None,
1505 };
1506 let mut rows: Vec<TrackedRow> = query.iter().flatten().map(row_of).collect();
1507
1508 let now = clock.now();
1509 let mut moved = Vec::new();
1510 let mut tx = storage.pool().begin().await?;
1511 if !rows.is_empty() {
1512 let snapshot =
1513 HeaderSnapshot::take(services, rows.iter().filter_map(TrackedRow::proof_height)).await;
1514 let (replaced, prior_tip) = match &event {
1515 ChainEvent::Reorg {
1516 deactivated_headers,
1517 ..
1518 } => (
1519 deactivated_headers
1520 .iter()
1521 .map(|h| {
1522 (
1523 h.height,
1524 Header {
1525 hash: h.hash.to_ascii_lowercase(),
1526 merkle_root: h.merkle_root.to_ascii_lowercase(),
1527 },
1528 )
1529 })
1530 .collect(),
1531 deactivated_headers.first().map(|h| h.height),
1532 ),
1533 _ => (BTreeMap::new(), None),
1534 };
1535 let replay_view = Replaced {
1536 snapshot: &snapshot,
1537 replaced,
1538 prior_tip,
1539 };
1540 let mut tracker = Tracker::new(opts.params());
1541 for row in &rows {
1542 row.replay_into(&mut tracker, &replay_view);
1543 }
1544 for update in tracker.on_chain(&snapshot, event.clone()) {
1545 let (Some(state), Some(row)) = (
1546 tracker.get(&update.txid),
1547 rows.iter_mut().find(|r| r.txid == update.txid),
1548 ) else {
1549 continue;
1550 };
1551 row.keep_chain(state, &event)?;
1552 if matches!(update.reask, Some(Reask::Reorg { .. })) {
1553 row.asks = 0;
1555 row.next_ask_at = None;
1556 }
1557 store_row_in(&mut *tx, row, state, now).await?;
1558 let fault = update.result.err().map(|e| e.to_string());
1559 if let Some(fault) = &fault {
1560 tracing::warn!(txid = %update.txid, kind, cursor, %fault, "a chain event's recheck could not look");
1561 }
1562 moved.push(ChainMove {
1563 txid: update.txid,
1564 word: word_label(state.word()).to_string(),
1565 reask: state.reask().map(reask_label),
1566 suspect: state.suspect(),
1567 fault,
1568 });
1569 }
1570 }
1571 sqlx::query(
1572 "INSERT INTO tracker_meta (key, value) VALUES ('feed_cursor', ?) \
1573 ON CONFLICT(key) DO UPDATE SET value = excluded.value",
1574 )
1575 .bind(cursor.to_string())
1576 .execute(&mut *tx)
1577 .await?;
1578 tx.commit().await?;
1579 Ok(ChainDelivery {
1580 cursor,
1581 kind,
1582 moved,
1583 })
1584}
1585
1586#[cfg(test)]
1587pub(crate) mod tests {
1588 use super::*;
1589 use bsv_wallet_toolbox::services::mock::{MockResponse, MockWalletServices};
1590 use bsv_wallet_toolbox::services::{BlockHeader, GetMerklePathResult};
1591 use bsv_wallet_toolbox::{
1592 WalletStorageWriter, BROADCAST_STATUS_ACCEPTED, BROADCAST_STATUS_MINED,
1593 };
1594 use chrono::Utc;
1595
1596 pub(crate) struct FixedClock(pub Timestamp);
1598 impl Clock for FixedClock {
1599 fn now(&self) -> Timestamp {
1600 self.0
1601 }
1602 }
1603
1604 pub(crate) fn opts() -> TickOptions {
1605 TickOptions {
1606 age_threshold: 600,
1607 max_asks: 20,
1608 spend_depth: DEFAULT_SPEND_DEPTH,
1609 }
1610 }
1611
1612 pub(crate) async fn wallet_storage() -> (StorageSqlx, i64) {
1614 let storage = StorageSqlx::in_memory().await.unwrap();
1615 storage
1616 .migrate("tracker-tests", &("02".to_string() + &"ab".repeat(32)))
1617 .await
1618 .unwrap();
1619 storage.make_available().await.unwrap();
1620 let (user, _) = storage
1621 .find_or_insert_user(&("02".to_string() + &"cd".repeat(32)))
1622 .await
1623 .unwrap();
1624 (storage, user.user_id)
1625 }
1626
1627 pub(crate) async fn insert_tx(storage: &StorageSqlx, user_id: i64, txid: &str, status: &str) {
1628 sqlx::query(
1629 "INSERT INTO transactions (user_id, status, reference, is_outgoing, satoshis, version, lock_time, description, txid, raw_tx, created_at, updated_at) \
1630 VALUES (?, ?, ?, 1, 0, 1, 0, 'd', ?, X'01000000', ?, ?)",
1631 )
1632 .bind(user_id)
1633 .bind(status)
1634 .bind(&txid[..8])
1635 .bind(txid)
1636 .bind(Utc::now())
1637 .bind(Utc::now())
1638 .execute(storage.pool())
1639 .await
1640 .unwrap();
1641 }
1642
1643 pub(crate) fn header(height: u32, root: &str, nonce: u32) -> BlockHeader {
1645 BlockHeader {
1646 version: 1,
1647 previous_hash: "00".repeat(32),
1648 merkle_root: root.to_string(),
1649 time: 1_700_000_000,
1650 bits: 0x1d00_ffff,
1651 nonce,
1652 hash: String::new(),
1653 height,
1654 }
1655 }
1656
1657 pub(crate) fn path_of(txid: &str, height: u32) -> String {
1660 MerklePath::from_coinbase_txid(txid, height).to_hex()
1661 }
1662
1663 pub(crate) fn proof_answer(path: Option<String>) -> MockResponse<GetMerklePathResult> {
1664 MockResponse::Success(GetMerklePathResult {
1665 name: Some("Services".to_string()),
1666 merkle_path: path,
1667 header: None,
1668 error: None,
1669 notes: vec![],
1670 })
1671 }
1672
1673 #[tokio::test]
1680 async fn a_pass_tracks_announces_and_proves_one_named_transaction() {
1681 let (storage, user) = wallet_storage().await;
1682 let txid = "a1".repeat(32);
1683 insert_tx(&storage, user, &txid, "unproven").await;
1684 storage
1685 .record_broadcast_status(&txid, "arcade", BROADCAST_STATUS_ACCEPTED)
1686 .await
1687 .unwrap();
1688 let services = MockWalletServices::builder()
1689 .get_merkle_path_response(proof_answer(Some(path_of(&txid, 900))))
1690 .build();
1691 services.set_header_for_height(header(900, &txid, 0));
1692 let now = Utc::now().timestamp() as u64;
1693
1694 let report = tick(&storage, &services, &FixedClock(now), &opts())
1696 .await
1697 .unwrap();
1698 assert_eq!((report.adopted, report.hints, report.asked), (1, 1, 0));
1699 assert_eq!(
1700 stored_word(&storage, &txid).await.unwrap(),
1701 Some(("announced".to_string(), None, None))
1702 );
1703 assert_eq!(services.call_count("get_merkle_path"), 0);
1704
1705 let report = tick(&storage, &services, &FixedClock(now + 601), &opts())
1707 .await
1708 .unwrap();
1709 assert_eq!(report.asked, 1);
1710 assert_eq!(report.mined, vec![txid.clone()]);
1711 assert_eq!(
1712 stored_word(&storage, &txid).await.unwrap(),
1713 Some(("mined".to_string(), Some(900), None))
1714 );
1715 assert_eq!(services.call_count("get_merkle_path"), 1);
1716
1717 let report = tick(&storage, &services, &FixedClock(now + 1300), &opts())
1719 .await
1720 .unwrap();
1721 assert_eq!((report.tracked, report.asked), (0, 0));
1722 assert_eq!(services.call_count("get_merkle_path"), 1);
1723 }
1724
1725 #[tokio::test]
1729 async fn a_broadcasters_mined_is_a_hint_that_asks_for_the_proof() {
1730 let (storage, user) = wallet_storage().await;
1731 let txid = "b2".repeat(32);
1732 insert_tx(&storage, user, &txid, "unproven").await;
1733 storage
1734 .record_broadcast_status(&txid, "arcade", BROADCAST_STATUS_MINED)
1735 .await
1736 .unwrap();
1737 let services = MockWalletServices::builder()
1738 .get_merkle_path_response(proof_answer(None))
1739 .build();
1740 let now = Utc::now().timestamp() as u64;
1741
1742 let report = tick(&storage, &services, &FixedClock(now), &opts())
1743 .await
1744 .unwrap();
1745 assert_eq!(report.asked, 1);
1746 assert_eq!(report.no_proof, vec![txid.clone()]);
1747 assert!(report.mined.is_empty());
1748 let (word, height, reask) = stored_word(&storage, &txid).await.unwrap().unwrap();
1749 assert_eq!((word.as_str(), height), ("built", None));
1750 assert_eq!(reask.as_deref(), Some("hint:arcade|mined"));
1751
1752 let report = tick(&storage, &services, &FixedClock(now + 60), &opts())
1754 .await
1755 .unwrap();
1756 assert_eq!(report.asked, 0);
1757 let report = tick(&storage, &services, &FixedClock(now + 600), &opts())
1759 .await
1760 .unwrap();
1761 assert_eq!(report.asked, 1);
1762 let report = tick(&storage, &services, &FixedClock(now + 1200), &opts())
1763 .await
1764 .unwrap();
1765 assert_eq!(report.asked, 0);
1766 assert_eq!(services.call_count("get_merkle_path"), 2);
1767 }
1768
1769 #[tokio::test]
1772 async fn a_moved_header_reasks_the_rows_at_or_above_it_and_no_other() {
1773 let (storage, user) = wallet_storage().await;
1774 let low = "c3".repeat(32);
1775 let high = "d4".repeat(32);
1776 let services = MockWalletServices::builder()
1777 .get_merkle_path_response(MockResponse::Sequence(vec![
1778 proof_answer(Some(path_of(&low, 100))),
1779 proof_answer(Some(path_of(&high, 200))),
1780 proof_answer(Some(path_of(&high, 201))),
1781 ]))
1782 .build();
1783 services.set_header_for_height(header(100, &low, 0));
1784 services.set_header_for_height(header(200, &high, 0));
1785 services.set_tip_header(header_with_hash(200, &high, 0));
1786 let now = Utc::now().timestamp() as u64;
1787 for txid in [&low, &high] {
1788 insert_tx(&storage, user, txid, "unproven").await;
1789 storage
1790 .record_broadcast_status(txid, "arcade", BROADCAST_STATUS_MINED)
1791 .await
1792 .unwrap();
1793 }
1794 let report = tick(&storage, &services, &FixedClock(now), &opts())
1795 .await
1796 .unwrap();
1797 assert_eq!(report.mined.len(), 2, "{report:?}");
1798
1799 services.set_header_for_height(header(200, &"ee".repeat(32), 7));
1802 services.set_header_for_height(header(201, &high, 0));
1803 services.set_tip_header(header_with_hash(201, &high, 0));
1804 let report = tick(&storage, &services, &FixedClock(now + 60), &opts())
1805 .await
1806 .unwrap();
1807 assert_eq!(report.moved_from, Some(200));
1808 assert_eq!(
1809 report.tracked, 1,
1810 "only the row at or above the moved header"
1811 );
1812 assert_eq!(report.moved, vec![high.clone()]);
1813 assert_eq!(report.mined, vec![high.clone()]);
1814 assert_eq!(
1815 stored_word(&storage, &high).await.unwrap().unwrap().1,
1816 Some(201)
1817 );
1818 assert_eq!(
1819 stored_word(&storage, &low).await.unwrap().unwrap().1,
1820 Some(100)
1821 );
1822 }
1823
1824 #[tokio::test]
1830 async fn a_not_final_transaction_is_announced_again_at_its_lock_time() {
1831 use bsv_wallet_toolbox::services::mock::{
1832 error_post_beef_result, success_post_beef_result,
1833 };
1834 let (storage, _user) = wallet_storage().await;
1835 let txid = "c7".repeat(32);
1836 let lock = 2_000_000_000u64;
1837 let services = MockWalletServices::builder()
1838 .post_beef_response(MockResponse::Sequence(vec![
1839 MockResponse::Success(vec![error_post_beef_result("Arcade", "476 non-final")]),
1840 MockResponse::Success(vec![success_post_beef_result("Arcade", &[&txid])]),
1841 ]))
1842 .get_merkle_path_response(proof_answer(None))
1843 .build();
1844
1845 let hint = Hint::new(
1846 "Arcade|rejected",
1847 HintStatus::Rejected {
1848 reason: "476 non-final".to_string(),
1849 },
1850 lock - 3600,
1851 );
1852 let again = Reannounce {
1853 beef: vec![1, 2, 3],
1854 at: lock,
1855 on_accept: None,
1856 };
1857 let word = heard(
1858 &storage,
1859 &FixedClock(lock - 3600),
1860 &opts(),
1861 &txid,
1862 hint,
1863 Some(lock),
1864 Some(again),
1865 )
1866 .await
1867 .unwrap();
1868 assert_eq!(
1869 word,
1870 (
1871 "built".to_string(),
1872 Some("hint:Arcade|rejected".to_string())
1873 )
1874 );
1875
1876 let report = tick(&storage, &services, &FixedClock(lock - 1), &opts())
1878 .await
1879 .unwrap();
1880 assert_eq!((report.tracked, report.asked), (1, 0), "{report:?}");
1881 assert_eq!(services.call_count("post_beef"), 0);
1882
1883 let report = tick(&storage, &services, &FixedClock(lock), &opts())
1885 .await
1886 .unwrap();
1887 assert_eq!(report.not_final, vec![txid.clone()]);
1888 assert_eq!(services.call_count("post_beef"), 1);
1889 let report = tick(&storage, &services, &FixedClock(lock + 60), &opts())
1890 .await
1891 .unwrap();
1892 assert!(report.not_final.is_empty() && report.reannounced.is_empty());
1893 assert_eq!(services.call_count("post_beef"), 1, "one pause between two");
1894
1895 let report = tick(&storage, &services, &FixedClock(lock + 600), &opts())
1897 .await
1898 .unwrap();
1899 assert_eq!(report.reannounced, vec![txid.clone()]);
1900 assert_eq!(services.call_count("post_beef"), 2);
1901 assert_eq!(
1902 stored_word(&storage, &txid).await.unwrap().unwrap().0,
1903 "announced"
1904 );
1905 assert_eq!(services.call_count("get_merkle_path"), 0);
1906
1907 let report = tick(&storage, &services, &FixedClock(lock + 1300), &opts())
1909 .await
1910 .unwrap();
1911 assert_eq!(report.asked, 1);
1912 assert_eq!(services.call_count("post_beef"), 2);
1913 assert_eq!(services.call_count("get_merkle_path"), 1);
1914 }
1915
1916 #[test]
1918 fn not_final_is_read_from_the_refusal() {
1919 for said in [
1920 "476 non-final",
1921 "bad-txns-nonfinal",
1922 "tx is not final",
1923 "code 476",
1924 ] {
1925 assert!(says_not_final(said), "{said}");
1926 }
1927 for said in [
1928 "fee too low",
1929 "DOUBLE_SPEND_ATTEMPTED",
1930 "txid 4476aa",
1931 "HTTP 500",
1932 ] {
1933 assert!(!says_not_final(said), "{said}");
1934 }
1935 }
1936
1937 pub(crate) fn header_with_hash(height: u32, root: &str, nonce: u32) -> BlockHeader {
1939 let mut h = header(height, root, nonce);
1940 h.hash = header_of(&h.to_binary()).unwrap().hash;
1941 h
1942 }
1943
1944 #[tokio::test]
1949 async fn the_spend_depth_holds_a_coin_until_its_block_is_deep_enough() {
1950 let (storage, user) = wallet_storage().await;
1951 let txid = "e7".repeat(32);
1952 insert_tx(&storage, user, &txid, "unproven").await;
1953 storage
1954 .record_broadcast_status(&txid, "arcade", BROADCAST_STATUS_MINED)
1955 .await
1956 .unwrap();
1957 let services = MockWalletServices::builder()
1958 .get_merkle_path_response(proof_answer(Some(path_of(&txid, 900))))
1959 .build();
1960 services.set_header_for_height(header(900, &txid, 0));
1961 services.set_tip_header(header_with_hash(900, &txid, 0));
1962 let clock = FixedClock(Utc::now().timestamp() as u64);
1963 tick(&storage, &services, &clock, &opts()).await.unwrap();
1964 let at = |spend_depth| TickOptions {
1965 spend_depth,
1966 ..opts()
1967 };
1968
1969 let one = spend_guard(&storage, &services, &clock, &at(1), &txid)
1970 .await
1971 .unwrap();
1972 let three = spend_guard(&storage, &services, &clock, &at(3), &txid)
1973 .await
1974 .unwrap();
1975 let zero = spend_guard(&storage, &services, &clock, &at(0), &txid)
1976 .await
1977 .unwrap();
1978 println!("one block deep: depth 1 {one:?}, depth 3 {three:?}, depth 0 {zero:?}");
1979 assert_eq!(one, Ok(true));
1980 assert_eq!(three, Ok(false));
1981 assert_eq!(zero, Ok(true), "0 reads as the floor, 1");
1982
1983 services.set_tip_header(header_with_hash(902, &"0c".repeat(32), 0));
1984 let three = spend_guard(&storage, &services, &clock, &at(3), &txid)
1985 .await
1986 .unwrap();
1987 println!("three blocks deep: depth 3 {three:?}");
1988 assert_eq!(three, Ok(true));
1989 assert_eq!(
1990 TickOptions {
1991 spend_depth: 0,
1992 ..opts()
1993 }
1994 .floored()
1995 .spend_depth,
1996 1
1997 );
1998 }
1999
2000 #[tokio::test]
2005 async fn the_readme_spend_guard_runs_as_the_host() {
2006 let (storage, user) = wallet_storage().await;
2007 let txid = "e5".repeat(32);
2008 insert_tx(&storage, user, &txid, "unproven").await;
2009 storage
2010 .record_broadcast_status(&txid, "arcade", BROADCAST_STATUS_MINED)
2011 .await
2012 .unwrap();
2013 let services = MockWalletServices::builder()
2014 .get_merkle_path_response(proof_answer(Some(path_of(&txid, 900))))
2015 .build();
2016 services.set_header_for_height(header(900, &txid, 0));
2017 let clock = FixedClock(Utc::now().timestamp() as u64);
2018 tick(&storage, &services, &clock, &opts()).await.unwrap();
2019
2020 let guard = spend_guard(&storage, &services, &clock, &opts(), &txid)
2022 .await
2023 .unwrap();
2024 println!(
2025 "spend guard, header in place: {guard:?}, stored {:?}",
2026 stored_word(&storage, &txid).await.unwrap().unwrap()
2027 );
2028 assert_eq!(guard, Ok(true));
2029
2030 services.set_tip_unavailable();
2032 let guard = spend_guard(&storage, &services, &clock, &opts(), &txid)
2033 .await
2034 .unwrap();
2035 println!(
2036 "spend guard, no header service: {guard:?}, stored {:?}",
2037 stored_word(&storage, &txid).await.unwrap().unwrap()
2038 );
2039 assert!(matches!(guard, Err(CheckError::Headers(_))), "{guard:?}");
2040 assert_eq!(
2041 stored_word(&storage, &txid).await.unwrap().unwrap().0,
2042 "mined",
2043 "could not look changes no word"
2044 );
2045
2046 services.set_tip_header(header_with_hash(901, &"00".repeat(32), 1));
2048 services.set_header_for_height(header(900, &"ee".repeat(32), 7));
2049 let guard = spend_guard(&storage, &services, &clock, &opts(), &txid)
2050 .await
2051 .unwrap();
2052 println!(
2053 "spend guard, header moved: {guard:?}, stored {:?}",
2054 stored_word(&storage, &txid).await.unwrap().unwrap()
2055 );
2056 assert_eq!(guard, Ok(false));
2057
2058 let guard = spend_guard(&storage, &services, &clock, &opts(), &"f6".repeat(32))
2060 .await
2061 .unwrap();
2062 assert_eq!(guard, Ok(false));
2063
2064 let params = opts().params();
2067 services.set_tip_header(header_with_hash(900, &txid, 0));
2068 services.set_header_for_height(header(900, &txid, 0));
2069 let held = HeaderSnapshot::take(&services, [900]).await;
2070 let row = load_row(&storage, &txid).await.unwrap().unwrap();
2071 let (mut state, fault) = row.replay(¶ms, &held);
2072 assert_eq!(fault, None);
2073 let lost = HeaderSnapshot::take(&services, []).await;
2074 let guard = readme_spend_guard(&mut state, ¶ms, &lost);
2075 println!("spend guard, height unanswered: {guard:?}");
2076 assert_eq!(guard, Err(CheckError::Unavailable(900)));
2077 }
2078}