@@ -3,9 +3,16 @@ pub mod poller;
33pub mod utils;
44
55use crate :: config:: BitcoindConfig ;
6- use crate :: { database:: DatabaseError , revaultd:: RevaultD , threadmessages:: BitcoindMessageOut } ;
6+ use crate :: {
7+ database:: {
8+ interface:: { db_spend_transaction, db_vault_by_unvault_txid} ,
9+ DatabaseError ,
10+ } ,
11+ revaultd:: RevaultD ,
12+ threadmessages:: BitcoindMessageOut ,
13+ } ;
714use interface:: { BitcoinD , WalletTransaction } ;
8- use poller:: poller_main;
15+ use poller:: { cpfp_package , poller_main, ToBeCpfped } ;
916use revault_tx:: bitcoin:: { Network , Txid } ;
1017
1118use std:: {
@@ -187,6 +194,47 @@ fn wallet_transaction(bitcoind: &BitcoinD, txid: Txid) -> Option<WalletTransacti
187194 . ok ( )
188195}
189196
197+ fn cpfp (
198+ revaultd : Arc < RwLock < RevaultD > > ,
199+ bitcoind : Arc < RwLock < BitcoinD > > ,
200+ txids : Vec < Txid > ,
201+ feerate : f64 ,
202+ ) -> Result < ( ) , BitcoindError > {
203+ let db_path = revaultd. read ( ) . unwrap ( ) . db_file ( ) ;
204+ assert ! ( revaultd. read( ) . unwrap( ) . is_manager( ) ) ;
205+
206+ let mut cpfp_txs = Vec :: with_capacity ( txids. len ( ) ) ;
207+
208+ for txid in txids. iter ( ) {
209+ log:: debug!( "[ZEE] looping over txids {}" , txid) ;
210+ let spend_tx = db_spend_transaction ( & db_path, & txid) . expect ( "Database must be available" ) ;
211+
212+ if let Some ( unwrap_spend_tx) = spend_tx {
213+ // If the transaction is of type SpendTransaction
214+ log:: debug!( "[ZEE] spend_tx {:?}" , unwrap_spend_tx) ;
215+ cpfp_txs. push ( ToBeCpfped :: Spend ( unwrap_spend_tx. psbt ) ) ;
216+ } else {
217+ // If not SpendTransaction then it must be UnvaultTransaction
218+ let unvault_tx = db_vault_by_unvault_txid ( & db_path, & txid)
219+ . expect ( "Database must be available" )
220+ . unwrap ( )
221+ . 1
222+ . psbt
223+ . unwrap_unvault ( )
224+ . clone ( ) ;
225+ log:: debug!( "[ZEE] unvault_tx {:?}" , unvault_tx) ;
226+ cpfp_txs. push ( ToBeCpfped :: Unvault ( unvault_tx) ) ;
227+ }
228+ }
229+
230+ // sats/vbyte -> sats/WU
231+ let sats_wu = feerate / 4.0 ;
232+ // sats/WU -> msats/WU
233+ let msats_wu = ( sats_wu * 1000.0 ) as u64 ;
234+
235+ cpfp_package ( & revaultd, & bitcoind. read ( ) . unwrap ( ) , cpfp_txs, msats_wu)
236+ }
237+
190238/// The bitcoind event loop.
191239/// Listens for bitcoind requests (wallet / chain) and poll bitcoind every 30 seconds,
192240/// updating our state accordingly.
@@ -208,7 +256,8 @@ pub fn bitcoind_main_loop(
208256 let _bitcoind = bitcoind. clone ( ) ;
209257 let _sync_progress = sync_progress. clone ( ) ;
210258 let _shutdown = shutdown. clone ( ) ;
211- move || poller_main ( revaultd, _bitcoind, _sync_progress, _shutdown)
259+ let _revaultd = revaultd. clone ( ) ;
260+ move || poller_main ( _revaultd, _bitcoind, _sync_progress, _shutdown)
212261 } ) ;
213262
214263 for msg in rx {
@@ -252,6 +301,18 @@ pub fn bitcoind_main_loop(
252301 ) )
253302 } ) ?;
254303 }
304+ BitcoindMessageOut :: CPFPTransaction ( txids, feerate, resp_tx) => {
305+ log:: trace!( "Received 'cpfptransaction' from main thread" ) ;
306+ log:: debug!( "[ZEE] Received 'cpfptransaction' from main thread" ) ;
307+
308+ assert ! ( revaultd. read( ) . unwrap( ) . is_manager( ) ) ;
309+ resp_tx
310+ . send ( cpfp ( revaultd, bitcoind, txids, feerate) )
311+ . map_err ( |e| {
312+ BitcoindError :: Custom ( format ! ( "Sending transaction for CPFP: {}" , e) )
313+ } ) ?;
314+ return Ok ( ( ) ) ;
315+ }
255316 }
256317 }
257318
0 commit comments