@@ -559,3 +559,116 @@ fn test_sync_with_coinbase() -> anyhow::Result<()> {
559559
560560 Ok ( ( ) )
561561}
562+
563+ #[ test]
564+ fn test_check_fee_calculation ( ) -> anyhow:: Result < ( ) > {
565+ const SEND_AMOUNT : Amount = Amount :: from_sat ( 10_000 ) ;
566+ let env = TestEnv :: new ( ) ?;
567+ let electrum_client = electrum_client:: Client :: new ( env. electrsd . electrum_url . as_str ( ) ) ?;
568+ let client = BdkElectrumClient :: new ( electrum_client) ;
569+
570+ let spk_to_track = ScriptBuf :: new_p2wsh ( & WScriptHash :: all_zeros ( ) ) ;
571+ let addr_to_track = Address :: from_script ( & spk_to_track, bdk_chain:: bitcoin:: Network :: Regtest ) ?;
572+
573+ // Setup receiver.
574+ let ( mut recv_chain, _) = LocalChain :: from_genesis_hash ( env. bitcoind . client . get_block_hash ( 0 ) ?) ;
575+ let mut recv_graph = IndexedTxGraph :: < ConfirmationBlockTime , _ > :: new ( {
576+ let mut recv_index = SpkTxOutIndex :: default ( ) ;
577+ recv_index. insert_spk ( ( ) , spk_to_track. clone ( ) ) ;
578+ recv_index
579+ } ) ;
580+
581+ // Mine some blocks.
582+ env. mine_blocks ( 101 , None ) ?;
583+
584+ // Send a preliminary tx such that the new utxo in Core's wallet
585+ // becomes the input of the next tx
586+ let new_addr = env
587+ . rpc_client ( )
588+ . get_new_address ( None , None ) ?
589+ . assume_checked ( ) ;
590+ let prev_amt = SEND_AMOUNT + Amount :: from_sat ( 1650 ) ;
591+ env. send ( & new_addr, prev_amt) ?;
592+ let prev_hash_block = env. mine_blocks ( 1 , None ) ?. into_iter ( ) . next ( ) ;
593+
594+ let txid = env. send ( & addr_to_track, SEND_AMOUNT ) ?;
595+
596+ // Mine a block to confirm sent tx.
597+ let hash_block = env. mine_blocks ( 1 , None ) ?. into_iter ( ) . next ( ) ;
598+
599+ // Look at the tx we just sent, it should have 1 input and 1 output
600+ let tx = env
601+ . rpc_client ( )
602+ . get_raw_transaction_info ( & txid, hash_block. as_ref ( ) ) ?;
603+ assert_eq ! ( tx. vin. len( ) , 1 ) ;
604+ assert_eq ! ( tx. vout. len( ) , 1 ) ;
605+ let vin = & tx. vin [ 0 ] ;
606+ let prev_txid = vin. txid . unwrap ( ) ;
607+ let vout = vin. vout . unwrap ( ) ;
608+ let outpoint = bdk_chain:: bitcoin:: OutPoint :: new ( prev_txid, vout) ;
609+
610+ // Get the txout of the previous tx
611+ let prev_tx = env
612+ . rpc_client ( )
613+ . get_raw_transaction_info ( & prev_txid, prev_hash_block. as_ref ( ) ) ?;
614+ let txout = prev_tx
615+ . vout
616+ . iter ( )
617+ . find ( |txout| txout. value == prev_amt)
618+ . unwrap ( ) ;
619+ let script_pubkey = ScriptBuf :: from_bytes ( txout. script_pub_key . hex . to_vec ( ) ) ;
620+ let txout = bdk_chain:: bitcoin:: TxOut {
621+ value : txout. value ,
622+ script_pubkey,
623+ } ;
624+
625+ // Sync up to tip.
626+ env. wait_until_electrum_sees_block ( Duration :: from_secs ( 6 ) ) ?;
627+ let _ = sync_with_electrum (
628+ & client,
629+ [ spk_to_track. clone ( ) ] ,
630+ & mut recv_chain,
631+ & mut recv_graph,
632+ ) ?;
633+
634+ // Check the graph update contains the right floating txout
635+ let graph_txout = recv_graph
636+ . graph ( )
637+ . all_txouts ( )
638+ . find ( |( _op, txout) | txout. value == prev_amt)
639+ . unwrap ( ) ;
640+ assert_eq ! ( graph_txout, ( outpoint, & txout) ) ;
641+
642+ // Check to see if tx is confirmed.
643+ assert_eq ! (
644+ get_balance( & recv_chain, & recv_graph) ?,
645+ Balance {
646+ confirmed: SEND_AMOUNT ,
647+ ..Balance :: default ( )
648+ } ,
649+ ) ;
650+
651+ for tx in recv_graph. graph ( ) . full_txs ( ) {
652+ // Retrieve the calculated fee from `TxGraph`, which will panic if we do not have the
653+ // floating txouts available from the transaction's previous outputs.
654+ let fee = recv_graph
655+ . graph ( )
656+ . calculate_fee ( & tx. tx )
657+ . expect ( "fee must exist" ) ;
658+
659+ // Retrieve the fee in the transaction data from `bitcoind`.
660+ let tx_fee = env
661+ . bitcoind
662+ . client
663+ . get_transaction ( & tx. txid , None )
664+ . expect ( "Tx must exist" )
665+ . fee
666+ . expect ( "Fee must exist" )
667+ . abs ( )
668+ . to_sat ( ) as u64 ;
669+
670+ // Check that the calculated fee matches the fee from the transaction data.
671+ assert_eq ! ( fee, Amount :: from_sat( tx_fee) ) ; // 1650sat
672+ }
673+ Ok ( ( ) )
674+ }
0 commit comments