1010#![ warn( missing_docs) ]
1111
1212use bdk_core:: { BlockId , CheckPoint } ;
13- use bitcoin:: { block:: Header , Block , BlockHash , Transaction } ;
13+ use bitcoin:: { block:: Header , Block , BlockHash , Transaction , Txid } ;
1414use bitcoincore_rpc:: bitcoincore_rpc_json;
15+ use std:: collections:: HashSet ;
1516
1617pub mod bip158;
1718
@@ -43,6 +44,9 @@ pub struct Emitter<'c, C> {
4344 /// The last emitted block during our last mempool emission. This is used to determine whether
4445 /// there has been a reorg since our last mempool emission.
4546 last_mempool_tip : Option < u32 > ,
47+
48+ /// Expected mempool txs. TODO: Docs.
49+ expected_mempool_txids : HashSet < Txid > ,
4650}
4751
4852impl < ' c , C : bitcoincore_rpc:: RpcApi > Emitter < ' c , C > {
@@ -53,28 +57,36 @@ impl<'c, C: bitcoincore_rpc::RpcApi> Emitter<'c, C> {
5357 ///
5458 /// `start_height` starts emission from a given height (if there are no conflicts with the
5559 /// original chain).
56- pub fn new ( client : & ' c C , last_cp : CheckPoint , start_height : u32 ) -> Self {
60+ pub fn new (
61+ client : & ' c C ,
62+ last_cp : CheckPoint ,
63+ start_height : u32 ,
64+ expected_mempool_txids : HashSet < Txid > ,
65+ ) -> Self {
5766 Self {
5867 client,
5968 start_height,
6069 last_cp,
6170 last_block : None ,
6271 last_mempool_time : 0 ,
6372 last_mempool_tip : None ,
73+ expected_mempool_txids,
6474 }
6575 }
6676
67- /// Emit mempool transactions, alongside their first-seen unix timestamps .
77+ /// Emit mempool transactions and capture the initial snapshot of all mempool [`Txid`]s .
6878 ///
69- /// This method emits each transaction only once, unless we cannot guarantee the transaction's
70- /// ancestors are already emitted.
79+ /// This method returns a [`MempoolEvent`] containing the full transactions (with their
80+ /// first-seen unix timestamps) that were emitted, and the set of all [`Txid`]s present from the
81+ /// initial mempool query. Each transaction is emitted only once, unless we cannot guarantee the
82+ /// transaction's ancestors are already emitted.
7183 ///
7284 /// To understand why, consider a receiver which filters transactions based on whether it
7385 /// alters the UTXO set of tracked script pubkeys. If an emitted mempool transaction spends a
7486 /// tracked UTXO which is confirmed at height `h`, but the receiver has only seen up to block
7587 /// of height `h-1`, we want to re-emit this transaction until the receiver has seen the block
7688 /// at height `h`.
77- pub fn mempool ( & mut self ) -> Result < Vec < ( Transaction , u64 ) > , bitcoincore_rpc:: Error > {
89+ pub fn mempool ( & mut self ) -> Result < MempoolEvent , bitcoincore_rpc:: Error > {
7890 let client = self . client ;
7991
8092 // This is the emitted tip height during the last mempool emission.
@@ -84,24 +96,65 @@ impl<'c, C: bitcoincore_rpc::RpcApi> Emitter<'c, C> {
8496 // `start_height` has been emitted.
8597 . unwrap_or ( self . start_height . saturating_sub ( 1 ) ) ;
8698
99+ // Get the raw mempool result from the RPC client.
100+ let raw_mempool = client. get_raw_mempool_verbose ( ) ?;
101+ let raw_mempool_txids: HashSet < Txid > = raw_mempool. keys ( ) . copied ( ) . collect ( ) ;
102+
103+ // Determine the latest block height from the raw mempool data.
104+ let latest_height = raw_mempool
105+ . values ( )
106+ . map ( |entry| entry. height )
107+ . max ( )
108+ . unwrap_or ( 0 ) ;
109+
110+ // Determine if we are at tip: if the highest mempool height equals the current chain tip.
111+ let at_tip = latest_height == self . last_cp . height ( ) as u64 ;
112+
113+ // Clear out `expected_mempool_txids` if we are at the latest block height or if we are on a
114+ // different block.
115+ if prev_mempool_tip != self . last_cp . height ( ) {
116+ self . expected_mempool_txids . clear ( ) ;
117+ } else if at_tip {
118+ let blockhash = client. get_block_hash ( latest_height) ?;
119+ let block = client. get_block ( & blockhash) ?;
120+ let confirmed_txids = block
121+ . txdata
122+ . into_iter ( )
123+ . map ( |tx| tx. compute_txid ( ) )
124+ . collect :: < Vec < _ > > ( ) ;
125+ println ! ( "CONFIRMED TXIDS: {:?}" , confirmed_txids) ;
126+ self . expected_mempool_txids
127+ . retain ( |txid| !confirmed_txids. contains ( txid) ) ;
128+ }
129+
130+ // If at tip, any expected txid missing from raw mempool is considered evicted;
131+ // if not at tip, we don't evict anything.
132+ let mut evicted_txids: HashSet < Txid > = if at_tip {
133+ self . expected_mempool_txids
134+ . difference ( & raw_mempool_txids)
135+ . copied ( )
136+ . collect ( )
137+ } else {
138+ HashSet :: new ( )
139+ } ;
140+
87141 // Mempool txs come with a timestamp of when the tx is introduced to the mempool. We keep
88142 // track of the latest mempool tx's timestamp to determine whether we have seen a tx
89143 // before. `prev_mempool_time` is the previous timestamp and `last_time` records what will
90144 // be the new latest timestamp.
91145 let prev_mempool_time = self . last_mempool_time ;
92146 let mut latest_time = prev_mempool_time;
93147
94- let txs_to_emit = client
95- . get_raw_mempool_verbose ( ) ?
148+ let new_txs = raw_mempool
96149 . into_iter ( )
97150 . filter_map ( {
98151 let latest_time = & mut latest_time;
152+ let evicted_txids = & mut evicted_txids;
99153 move |( txid, tx_entry) | -> Option < Result < _ , bitcoincore_rpc:: Error > > {
100154 let tx_time = tx_entry. time as usize ;
101155 if tx_time > * latest_time {
102156 * latest_time = tx_time;
103157 }
104-
105158 // Avoid emitting transactions that are already emitted if we can guarantee
106159 // blocks containing ancestors are already emitted. The bitcoind rpc interface
107160 // provides us with the block height that the tx is introduced to the mempool.
@@ -112,23 +165,36 @@ impl<'c, C: bitcoincore_rpc::RpcApi> Emitter<'c, C> {
112165 if is_already_emitted && is_within_height {
113166 return None ;
114167 }
115-
116168 let tx = match client. get_raw_transaction ( & txid, None ) {
117169 Ok ( tx) => tx,
118- // the tx is confirmed or evicted since `get_raw_mempool_verbose`
119- Err ( err) if err. is_not_found_error ( ) => return None ,
170+ Err ( err) if err. is_not_found_error ( ) => {
171+ // If at tip and the transaction isn't found, mark it as evicted.
172+ if at_tip {
173+ evicted_txids. insert ( txid) ;
174+ }
175+ return None ;
176+ }
120177 Err ( err) => return Some ( Err ( err) ) ,
121178 } ;
122-
123179 Some ( Ok ( ( tx, tx_time as u64 ) ) )
124180 }
125181 } )
126182 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
127183
128184 self . last_mempool_time = latest_time;
129185 self . last_mempool_tip = Some ( self . last_cp . height ( ) ) ;
130-
131- Ok ( txs_to_emit)
186+ self . expected_mempool_txids . extend (
187+ new_txs
188+ . iter ( )
189+ . map ( |( tx, _) | tx. compute_txid ( ) )
190+ . collect :: < Vec < _ > > ( ) ,
191+ ) ;
192+
193+ Ok ( MempoolEvent {
194+ new_txs,
195+ evicted_txids,
196+ latest_update_time : latest_time as u64 ,
197+ } )
132198 }
133199
134200 /// Emit the next block height and header (if any).
@@ -144,6 +210,27 @@ impl<'c, C: bitcoincore_rpc::RpcApi> Emitter<'c, C> {
144210 }
145211}
146212
213+ /// A new emission from mempool.
214+ #[ derive( Debug ) ]
215+ pub struct MempoolEvent {
216+ /// Unemitted transactions or transactions with ancestors that are unseen by the receiver.
217+ ///
218+ /// To understand the second condition, consider a receiver which filters transactions based on
219+ /// whether it alters the UTXO set of tracked script pubkeys. If an emitted mempool transaction
220+ /// spends a tracked UTXO which is confirmed at height `h`, but the receiver has only seen up to
221+ /// block of height `h-1`, we want to re-emit this transaction until the receiver has seen the
222+ /// block at height `h`.
223+ pub new_txs : Vec < ( Transaction , u64 ) > ,
224+
225+ /// [`Txid`]s of all transactions that have been evicted from mempool.
226+ pub evicted_txids : HashSet < Txid > ,
227+
228+ /// The latest timestamp of when a transaction entered the mempool.
229+ ///
230+ /// This is useful for setting the timestamp for evicted transactions.
231+ pub latest_update_time : u64 ,
232+ }
233+
147234/// A newly emitted block from [`Emitter`].
148235#[ derive( Debug ) ]
149236pub struct BlockEvent < B > {
0 commit comments