@@ -766,6 +766,109 @@ 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
+ assert_eq ! ( wallet. balance( ) . confirmed, Amount :: from_sat( 50_000 ) ) ;
773
+ insert_checkpoint (
774
+ & mut wallet,
775
+ BlockId {
776
+ height : 3_000 ,
777
+ hash : BlockHash :: all_zeros ( ) ,
778
+ } ,
779
+ ) ;
780
+
781
+ let confirmed_tx = Transaction {
782
+ input : vec ! [ ] ,
783
+ output : vec ! [ TxOut {
784
+ script_pubkey: wallet
785
+ . next_unused_address( KeychainKind :: External )
786
+ . script_pubkey( ) ,
787
+ value: Amount :: from_sat( 25_000 ) ,
788
+ } ] ,
789
+ version : transaction:: Version :: non_standard ( 0 ) ,
790
+ lock_time : absolute:: LockTime :: ZERO ,
791
+ } ;
792
+ let confirmed_txid = confirmed_tx. compute_txid ( ) ;
793
+ insert_tx ( & mut wallet, confirmed_tx) ;
794
+ let anchor = ConfirmationBlockTime {
795
+ block_id : wallet. latest_checkpoint ( ) . get ( 3_000 ) . unwrap ( ) . block_id ( ) ,
796
+ confirmation_time : 200 ,
797
+ } ;
798
+ insert_anchor ( & mut wallet, confirmed_txid, anchor) ;
799
+ let unconfirmed_tx = Transaction {
800
+ input : vec ! [ ] ,
801
+ output : vec ! [ TxOut {
802
+ script_pubkey: wallet
803
+ . next_unused_address( KeychainKind :: External )
804
+ . script_pubkey( ) ,
805
+ value: Amount :: from_sat( 25_000 ) ,
806
+ } ] ,
807
+ version : transaction:: Version :: non_standard ( 0 ) ,
808
+ lock_time : absolute:: LockTime :: ZERO ,
809
+ } ;
810
+ let unconfirmed_txid = unconfirmed_tx. compute_txid ( ) ;
811
+ insert_tx ( & mut wallet, unconfirmed_tx) ;
812
+
813
+ let addr = wallet. next_unused_address ( KeychainKind :: External ) ;
814
+
815
+ let mut builder = wallet. build_tx ( ) ;
816
+ builder
817
+ . add_recipient ( addr. script_pubkey ( ) , Amount :: from_sat ( 51_000 ) )
818
+ . only_spend_confirmed ( ) ;
819
+ let ret = builder. finish ( ) . unwrap ( ) ;
820
+ assert_eq ! ( ret. unsigned_tx. input. len( ) , 2 ) ;
821
+ assert ! ( ret. unsigned_tx. input. iter( ) . any( |i| i. previous_output. txid == funding_txid) ) ;
822
+ assert ! ( ret. unsigned_tx. input. iter( ) . any( |i| i. previous_output. txid == confirmed_txid) ) ;
823
+
824
+ let mut builder = wallet. build_tx ( ) ;
825
+ builder
826
+ . add_recipient ( addr. script_pubkey ( ) , Amount :: from_sat ( 51_000 ) )
827
+ . only_spend_confirmed_since ( 3_000 ) ;
828
+ let ret = builder. finish ( ) . unwrap ( ) ;
829
+ assert_eq ! ( ret. unsigned_tx. input. len( ) , 2 ) ;
830
+ assert ! ( ret. unsigned_tx. input. iter( ) . any( |i| i. previous_output. txid == funding_txid) ) ;
831
+ assert ! ( ret. unsigned_tx. input. iter( ) . any( |i| i. previous_output. txid == confirmed_txid) ) ;
832
+
833
+ let mut builder = wallet. build_tx ( ) ;
834
+ builder
835
+ . add_recipient ( addr. script_pubkey ( ) , Amount :: from_sat ( 25_000 ) )
836
+ . only_spend_confirmed_since ( 2_500 ) ;
837
+ let ret = builder. finish ( ) . unwrap ( ) ;
838
+ assert_eq ! ( ret. unsigned_tx. input. len( ) , 1 ) ;
839
+ assert ! ( ret. unsigned_tx. input. iter( ) . any( |i| i. previous_output. txid == funding_txid) ) ;
840
+
841
+ let mut builder = wallet. build_tx ( ) ;
842
+ builder
843
+ . add_recipient ( addr. script_pubkey ( ) , Amount :: from_sat ( 24_000 ) )
844
+ . only_spend_unconfirmed ( ) ;
845
+ let ret = builder. finish ( ) . unwrap ( ) ;
846
+ assert_eq ! ( ret. unsigned_tx. input. len( ) , 1 ) ;
847
+ assert ! ( ret. unsigned_tx. input. iter( ) . any( |i| i. previous_output. txid == unconfirmed_txid) ) ;
848
+
849
+ let mut builder = wallet. build_tx ( ) ;
850
+ builder
851
+ . add_recipient ( addr. script_pubkey ( ) , Amount :: from_sat ( 76_000 ) )
852
+ . only_spend_confirmed ( ) ;
853
+ assert ! ( matches!(
854
+ builder. finish( ) ,
855
+ Err ( CreateTxError :: CoinSelection (
856
+ coin_selection:: InsufficientFunds { .. }
857
+ ) ) ,
858
+ ) ) ;
859
+
860
+ let mut builder = wallet. build_tx ( ) ;
861
+ builder
862
+ . add_recipient ( addr. script_pubkey ( ) , Amount :: from_sat ( 76_000 ) )
863
+ . only_spend_unconfirmed ( ) ;
864
+ assert ! ( matches!(
865
+ builder. finish( ) ,
866
+ Err ( CreateTxError :: CoinSelection (
867
+ coin_selection:: InsufficientFunds { .. }
868
+ ) ) ,
869
+ ) ) ;
870
+ }
871
+
769
872
#[ test]
770
873
fn test_create_tx_default_sequence ( ) {
771
874
let ( mut wallet, _) = get_funded_wallet_wpkh ( ) ;
0 commit comments