@@ -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,42 @@ 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 : u64 ,
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+ cpfp_package ( & revaultd, & bitcoind. read ( ) . unwrap ( ) , cpfp_txs, feerate)
231+ }
232+
190233/// The bitcoind event loop.
191234/// Listens for bitcoind requests (wallet / chain) and poll bitcoind every 30 seconds,
192235/// updating our state accordingly.
@@ -208,7 +251,8 @@ pub fn bitcoind_main_loop(
208251 let _bitcoind = bitcoind. clone ( ) ;
209252 let _sync_progress = sync_progress. clone ( ) ;
210253 let _shutdown = shutdown. clone ( ) ;
211- move || poller_main ( revaultd, _bitcoind, _sync_progress, _shutdown)
254+ let _revaultd = revaultd. clone ( ) ;
255+ move || poller_main ( _revaultd, _bitcoind, _sync_progress, _shutdown)
212256 } ) ;
213257
214258 for msg in rx {
@@ -252,6 +296,18 @@ pub fn bitcoind_main_loop(
252296 ) )
253297 } ) ?;
254298 }
299+ BitcoindMessageOut :: CPFPTransaction ( txids, feerate, resp_tx) => {
300+ log:: trace!( "Received 'cpfptransaction' from main thread" ) ;
301+ log:: debug!( "[ZEE] Received 'cpfptransaction' from main thread" ) ;
302+
303+ assert ! ( revaultd. read( ) . unwrap( ) . is_manager( ) ) ;
304+ resp_tx
305+ . send ( cpfp ( revaultd, bitcoind, txids, feerate) )
306+ . map_err ( |e| {
307+ BitcoindError :: Custom ( format ! ( "Sending transaction for CPFP: {}" , e) )
308+ } ) ?;
309+ return Ok ( ( ) ) ;
310+ }
255311 }
256312 }
257313
0 commit comments