From b50e36ff523d2cb0e6f52348cb58f4c5ec72228f Mon Sep 17 00:00:00 2001 From: lerencao Date: Mon, 15 Mar 2021 09:27:17 +0800 Subject: [PATCH] log: add tx status log (#2271) --- txpool/src/pool/listener.rs | 46 +++++++++++++++++++++++++++++++++++++ txpool/src/pool/queue.rs | 5 +++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/txpool/src/pool/listener.rs b/txpool/src/pool/listener.rs index 0c42e3277d..bc47469ec0 100644 --- a/txpool/src/pool/listener.rs +++ b/txpool/src/pool/listener.rs @@ -61,6 +61,52 @@ impl tx_pool::Listener for Logger { } } +/// Transaction status logger. +#[derive(Default, Debug)] +pub struct StatusLogger; +impl StatusLogger { + fn log_status(tx: &Arc, status: TxStatus) { + debug!( + target: "tx-status", + "[tx-status] hash: {hash}, status: {status}", + hash = tx.hash(), + status = status + ); + } +} +impl tx_pool::Listener for StatusLogger { + fn added(&mut self, tx: &Arc, old: Option<&Arc>) { + Self::log_status(tx, TxStatus::Added); + if let Some(old) = old { + Self::log_status(old, TxStatus::Dropped); + } + } + + fn rejected( + &mut self, + tx: &Arc, + _reason: &tx_pool::Error, + ) { + Self::log_status(tx, TxStatus::Rejected); + } + + fn dropped(&mut self, tx: &Arc, _new: Option<&Transaction>) { + Self::log_status(tx, TxStatus::Dropped); + } + + fn invalid(&mut self, tx: &Arc) { + Self::log_status(tx, TxStatus::Invalid); + } + + fn canceled(&mut self, tx: &Arc) { + Self::log_status(tx, TxStatus::Canceled); + } + + fn culled(&mut self, tx: &Arc) { + Self::log_status(tx, TxStatus::Culled); + } +} + /// Transactions pool notifier #[derive(Default)] pub struct TransactionsPoolNotifier { diff --git a/txpool/src/pool/queue.rs b/txpool/src/pool/queue.rs index a5c48549f7..2e6b4ea95e 100644 --- a/txpool/src/pool/queue.rs +++ b/txpool/src/pool/queue.rs @@ -27,7 +27,10 @@ use types::{account_address::AccountAddress as Address, transaction}; type Listener = ( LocalTransactionsList, - (listener::TransactionsPoolNotifier, listener::Logger), + ( + listener::TransactionsPoolNotifier, + (listener::Logger, listener::StatusLogger), + ), ); type Pool = tx_pool::Pool;