@@ -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,43 @@ 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+ let spend_tx = db_spend_transaction ( & db_path, & txid) . expect ( "Database must be available" ) ;
210+
211+ if let Some ( unwrap_spend_tx) = spend_tx {
212+ // If the transaction is of type SpendTransaction
213+ cpfp_txs. push ( ToBeCpfped :: Spend ( unwrap_spend_tx. psbt ) ) ;
214+ } else {
215+ // The transaction type is asserted to be UnvaultTransaction
216+ let unvault_tx = db_vault_by_unvault_txid ( & db_path, & txid)
217+ . expect ( "Database must be available" )
218+ . unwrap ( )
219+ . 1
220+ . psbt
221+ . assert_unvault ( ) ;
222+ cpfp_txs. push ( ToBeCpfped :: Unvault ( unvault_tx) ) ;
223+ }
224+ }
225+
226+ // sats/vbyte -> sats/WU
227+ let sats_wu = feerate / 4.0 ;
228+ // sats/WU -> msats/WU
229+ let msats_wu = ( sats_wu * 1000.0 ) as u64 ;
230+
231+ cpfp_package ( & revaultd, & bitcoind. read ( ) . unwrap ( ) , cpfp_txs, msats_wu)
232+ }
233+
190234/// The bitcoind event loop.
191235/// Listens for bitcoind requests (wallet / chain) and poll bitcoind every 30 seconds,
192236/// updating our state accordingly.
@@ -208,7 +252,8 @@ pub fn bitcoind_main_loop(
208252 let _bitcoind = bitcoind. clone ( ) ;
209253 let _sync_progress = sync_progress. clone ( ) ;
210254 let _shutdown = shutdown. clone ( ) ;
211- move || poller_main ( revaultd, _bitcoind, _sync_progress, _shutdown)
255+ let _revaultd = revaultd. clone ( ) ;
256+ move || poller_main ( _revaultd, _bitcoind, _sync_progress, _shutdown)
212257 } ) ;
213258
214259 for msg in rx {
@@ -252,6 +297,16 @@ pub fn bitcoind_main_loop(
252297 ) )
253298 } ) ?;
254299 }
300+ BitcoindMessageOut :: CPFPTransaction ( txids, feerate, resp_tx) => {
301+ log:: trace!( "Received 'cpfptransaction' from main thread" ) ;
302+
303+ resp_tx
304+ . send ( cpfp ( revaultd, bitcoind, txids, feerate) )
305+ . map_err ( |e| {
306+ BitcoindError :: Custom ( format ! ( "Sending transaction for CPFP: {}" , e) )
307+ } ) ?;
308+ return Ok ( ( ) ) ;
309+ }
255310 }
256311 }
257312
0 commit comments