From 46ffc34f4047c49d29933ac1099ca952fdc383f9 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 8 Jul 2021 12:42:28 +1000 Subject: [PATCH] Don't spam on transaction status change --- swap/src/bitcoin/wallet.rs | 39 +++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/swap/src/bitcoin/wallet.rs b/swap/src/bitcoin/wallet.rs index d8796ef7..a45e1a84 100644 --- a/swap/src/bitcoin/wallet.rs +++ b/swap/src/bitcoin/wallet.rs @@ -178,9 +178,10 @@ fn print_status_change(txid: Txid, old: Option, new: ScriptStatus) (None, new_status) => { tracing::debug!(%txid, status = %new_status, "Found relevant Bitcoin transaction"); } - (Some(old_status), new_status) => { + (Some(old_status), new_status) if old_status != new_status => { tracing::debug!(%txid, %new_status, %old_status, "Bitcoin transaction status changed"); } + _ => {} } new @@ -854,7 +855,9 @@ impl fmt::Display for ScriptStatus { mod tests { use super::*; use crate::bitcoin::{PublicKey, TxLock}; + use crate::tracing_ext::capture_logs; use proptest::prelude::*; + use tracing::level_filters::LevelFilter; #[test] fn given_depth_0_should_meet_confirmation_target_one() { @@ -1128,4 +1131,38 @@ mod tests { _ => panic!("expected exactly two outputs"), } } + + #[test] + fn printing_status_change_doesnt_spam_on_same_status() { + let writer = capture_logs(LevelFilter::DEBUG); + + let tx = Txid::default(); + let mut old = None; + old = Some(print_status_change(tx, old, ScriptStatus::Unseen)); + old = Some(print_status_change(tx, old, ScriptStatus::InMempool)); + old = Some(print_status_change(tx, old, ScriptStatus::InMempool)); + old = Some(print_status_change(tx, old, ScriptStatus::InMempool)); + old = Some(print_status_change(tx, old, ScriptStatus::InMempool)); + old = Some(print_status_change(tx, old, ScriptStatus::InMempool)); + old = Some(print_status_change(tx, old, ScriptStatus::InMempool)); + old = Some(print_status_change(tx, old, confs(1))); + old = Some(print_status_change(tx, old, confs(2))); + old = Some(print_status_change(tx, old, confs(3))); + old = Some(print_status_change(tx, old, confs(3))); + print_status_change(tx, old, confs(3)); + + assert_eq!( + writer.captured(), + r"DEBUG swap::bitcoin::wallet: Found relevant Bitcoin transaction txid=0000000000000000000000000000000000000000000000000000000000000000 status=unseen +DEBUG swap::bitcoin::wallet: Bitcoin transaction status changed txid=0000000000000000000000000000000000000000000000000000000000000000 new_status=in mempool old_status=unseen +DEBUG swap::bitcoin::wallet: Bitcoin transaction status changed txid=0000000000000000000000000000000000000000000000000000000000000000 new_status=confirmed with 1 blocks old_status=in mempool +DEBUG swap::bitcoin::wallet: Bitcoin transaction status changed txid=0000000000000000000000000000000000000000000000000000000000000000 new_status=confirmed with 2 blocks old_status=confirmed with 1 blocks +DEBUG swap::bitcoin::wallet: Bitcoin transaction status changed txid=0000000000000000000000000000000000000000000000000000000000000000 new_status=confirmed with 3 blocks old_status=confirmed with 2 blocks +" + ) + } + + fn confs(confirmations: u32) -> ScriptStatus { + ScriptStatus::from_confirmations(confirmations) + } }