@@ -766,6 +766,92 @@ fn test_create_tx_change_policy() {
766
766
) ) ;
767
767
}
768
768
769
+ #[ test]
770
+ fn test_create_tx_confirmation_policy ( ) {
771
+ let ( mut wallet, funding_txid) = get_funded_wallet_wpkh ( ) ;
772
+ // confirm the funding tx
773
+ let anchor = ConfirmationBlockTime {
774
+ block_id : wallet. latest_checkpoint ( ) . get ( 2000 ) . unwrap ( ) . block_id ( ) ,
775
+ confirmation_time : 200 ,
776
+ } ;
777
+ insert_anchor ( & mut wallet, funding_txid, anchor) ;
778
+ assert_eq ! ( wallet. balance( ) . confirmed, Amount :: from_sat( 50_000 ) ) ;
779
+
780
+ let confirmed_tx = Transaction {
781
+ input : vec ! [ ] ,
782
+ output : vec ! [ TxOut {
783
+ script_pubkey: wallet
784
+ . next_unused_address( KeychainKind :: External )
785
+ . script_pubkey( ) ,
786
+ value: Amount :: from_sat( 25_000 ) ,
787
+ } ] ,
788
+ version : transaction:: Version :: non_standard ( 0 ) ,
789
+ lock_time : absolute:: LockTime :: ZERO ,
790
+ } ;
791
+ let confirmed_txid = confirmed_tx. compute_txid ( ) ;
792
+ insert_tx ( & mut wallet, confirmed_tx) ;
793
+ let anchor = ConfirmationBlockTime {
794
+ block_id : wallet. latest_checkpoint ( ) . get ( 2000 ) . unwrap ( ) . block_id ( ) ,
795
+ confirmation_time : 200 ,
796
+ } ;
797
+ insert_anchor ( & mut wallet, confirmed_txid, anchor) ;
798
+ let unconfirmed_tx = Transaction {
799
+ input : vec ! [ ] ,
800
+ output : vec ! [ TxOut {
801
+ script_pubkey: wallet
802
+ . next_unused_address( KeychainKind :: External )
803
+ . script_pubkey( ) ,
804
+ value: Amount :: from_sat( 25_000 ) ,
805
+ } ] ,
806
+ version : transaction:: Version :: non_standard ( 0 ) ,
807
+ lock_time : absolute:: LockTime :: ZERO ,
808
+ } ;
809
+ let unconfirmed_txid = unconfirmed_tx. compute_txid ( ) ;
810
+ insert_tx ( & mut wallet, unconfirmed_tx) ;
811
+
812
+ let addr = wallet. next_unused_address ( KeychainKind :: External ) ;
813
+
814
+ let mut builder = wallet. build_tx ( ) ;
815
+ builder
816
+ . add_recipient ( addr. script_pubkey ( ) , Amount :: from_sat ( 51_000 ) )
817
+ . only_spend_confirmed ( ) ;
818
+ let ret = builder. finish ( ) . unwrap ( ) ;
819
+ assert_eq ! ( ret. unsigned_tx. input. len( ) , 2 ) ;
820
+ assert ! ( ret. unsigned_tx. input. iter( ) . find( |i| i. previous_output. txid == confirmed_txid) . is_some( ) ) ;
821
+ assert ! ( ret. unsigned_tx. input. iter( ) . find( |i| i. previous_output. txid == unconfirmed_txid) . is_none( ) ) ;
822
+
823
+ let mut builder = wallet. build_tx ( ) ;
824
+ builder
825
+ . add_recipient ( addr. script_pubkey ( ) , Amount :: from_sat ( 24_000 ) )
826
+ . only_spend_unconfirmed ( ) ;
827
+ let ret = builder. finish ( ) . unwrap ( ) ;
828
+ assert_eq ! ( ret. unsigned_tx. input. len( ) , 1 ) ;
829
+ assert ! ( ret. unsigned_tx. input. iter( ) . find( |i| i. previous_output. txid == unconfirmed_txid) . is_some( ) ) ;
830
+ assert ! ( ret. unsigned_tx. input. iter( ) . find( |i| i. previous_output. txid == confirmed_txid) . is_none( ) ) ;
831
+
832
+ let mut builder = wallet. build_tx ( ) ;
833
+ builder
834
+ . add_recipient ( addr. script_pubkey ( ) , Amount :: from_sat ( 76_000 ) )
835
+ . only_spend_confirmed ( ) ;
836
+ assert ! ( matches!(
837
+ builder. finish( ) ,
838
+ Err ( CreateTxError :: CoinSelection (
839
+ coin_selection:: InsufficientFunds { .. }
840
+ ) ) ,
841
+ ) ) ;
842
+
843
+ let mut builder = wallet. build_tx ( ) ;
844
+ builder
845
+ . add_recipient ( addr. script_pubkey ( ) , Amount :: from_sat ( 76_000 ) )
846
+ . only_spend_unconfirmed ( ) ;
847
+ assert ! ( matches!(
848
+ builder. finish( ) ,
849
+ Err ( CreateTxError :: CoinSelection (
850
+ coin_selection:: InsufficientFunds { .. }
851
+ ) ) ,
852
+ ) ) ;
853
+ }
854
+
769
855
#[ test]
770
856
fn test_create_tx_default_sequence ( ) {
771
857
let ( mut wallet, _) = get_funded_wallet_wpkh ( ) ;
0 commit comments