@@ -19,6 +19,7 @@ use alloc::{
1919 sync:: Arc ,
2020 vec:: Vec ,
2121} ;
22+ use chain:: CanonicalizationParams ;
2223use core:: { cmp:: Ordering , fmt, mem, ops:: Deref } ;
2324
2425use bdk_chain:: {
@@ -57,6 +58,7 @@ mod params;
5758mod persisted;
5859pub mod signer;
5960pub mod tx_builder;
61+ pub mod unbroadcasted;
6062pub ( crate ) mod utils;
6163
6264use crate :: collections:: { BTreeMap , HashMap , HashSet } ;
@@ -80,6 +82,7 @@ pub use bdk_chain::Balance;
8082pub use changeset:: ChangeSet ;
8183pub use params:: * ;
8284pub use persisted:: * ;
85+ pub use unbroadcasted:: Unbroadcasted ;
8386pub use utils:: IsDust ;
8487
8588/// A Bitcoin wallet
@@ -105,6 +108,7 @@ pub struct Wallet {
105108 change_signers : Arc < SignersContainer > ,
106109 chain : LocalChain ,
107110 indexed_graph : IndexedTxGraph < ConfirmationBlockTime , KeychainTxOutIndex < KeychainKind > > ,
111+ unbroadcasted : Unbroadcasted ,
108112 stage : ChangeSet ,
109113 network : Network ,
110114 secp : SecpCtx ,
@@ -405,13 +409,15 @@ impl Wallet {
405409 let change_descriptor = index. get_descriptor ( KeychainKind :: Internal ) . cloned ( ) ;
406410 let indexed_graph = IndexedTxGraph :: new ( index) ;
407411 let indexed_graph_changeset = indexed_graph. initial_changeset ( ) ;
412+ let unbroadcasted = Unbroadcasted :: default ( ) ;
408413
409414 let stage = ChangeSet {
410415 descriptor,
411416 change_descriptor,
412417 local_chain : chain_changeset,
413418 tx_graph : indexed_graph_changeset. tx_graph ,
414419 indexer : indexed_graph_changeset. indexer ,
420+ unbroadcasted : Default :: default ( ) ,
415421 network : Some ( network) ,
416422 } ;
417423
@@ -421,6 +427,7 @@ impl Wallet {
421427 network,
422428 chain,
423429 indexed_graph,
430+ unbroadcasted,
424431 stage,
425432 secp,
426433 } )
@@ -605,13 +612,16 @@ impl Wallet {
605612 indexed_graph. apply_changeset ( changeset. indexer . into ( ) ) ;
606613 indexed_graph. apply_changeset ( changeset. tx_graph . into ( ) ) ;
607614
615+ let unbroadcasted = Unbroadcasted :: from_changeset ( changeset. unbroadcasted ) ;
616+
608617 let stage = ChangeSet :: default ( ) ;
609618
610619 Ok ( Some ( Wallet {
611620 signers,
612621 change_signers,
613622 chain,
614623 indexed_graph,
624+ unbroadcasted,
615625 stage,
616626 network,
617627 secp,
@@ -809,13 +819,31 @@ impl Wallet {
809819 self . indexed_graph . index . index_of_spk ( spk) . cloned ( )
810820 }
811821
822+ /// Modifies canonicalization by assuming that all unbroadcasted transactions are canonical.
823+ pub fn include_unbroadcasted_canonicalization_params ( & self ) -> CanonicalizationParams {
824+ CanonicalizationParams {
825+ assume_canonical : self . unbroadcasted . txids ( ) . collect ( ) ,
826+ }
827+ }
828+
829+ /// TODO: Return this in the order in which it needs to be broadcasted.
830+ pub fn unbroadcasted ( & self ) -> impl Iterator < Item = ( Txid , Arc < Transaction > ) > + ' _ {
831+ self . unbroadcasted
832+ . txids ( )
833+ . filter_map ( |txid| self . tx_graph ( ) . get_tx ( txid) . map ( |tx| ( txid, tx) ) )
834+ }
835+
812836 /// Return the list of unspent outputs of this wallet
813- pub fn list_unspent ( & self ) -> impl Iterator < Item = LocalOutput > + ' _ {
837+ pub fn list_unspent (
838+ & self ,
839+ params : CanonicalizationParams ,
840+ ) -> impl Iterator < Item = LocalOutput > + ' _ {
814841 self . indexed_graph
815842 . graph ( )
816843 . filter_chain_unspents (
817844 & self . chain ,
818845 self . chain . tip ( ) . block_id ( ) ,
846+ params,
819847 self . indexed_graph . index . outpoints ( ) . iter ( ) . cloned ( ) ,
820848 )
821849 . map ( |( ( k, i) , full_txo) | new_local_utxo ( k, i, full_txo) )
@@ -824,12 +852,16 @@ impl Wallet {
824852 /// List all relevant outputs (includes both spent and unspent, confirmed and unconfirmed).
825853 ///
826854 /// To list only unspent outputs (UTXOs), use [`Wallet::list_unspent`] instead.
827- pub fn list_output ( & self ) -> impl Iterator < Item = LocalOutput > + ' _ {
855+ pub fn list_output (
856+ & self ,
857+ params : CanonicalizationParams ,
858+ ) -> impl Iterator < Item = LocalOutput > + ' _ {
828859 self . indexed_graph
829860 . graph ( )
830861 . filter_chain_txouts (
831862 & self . chain ,
832863 self . chain . tip ( ) . block_id ( ) ,
864+ params,
833865 self . indexed_graph . index . outpoints ( ) . iter ( ) . cloned ( ) ,
834866 )
835867 . map ( |( ( k, i) , full_txo) | new_local_utxo ( k, i, full_txo) )
@@ -876,13 +908,14 @@ impl Wallet {
876908
877909 /// Returns the utxo owned by this wallet corresponding to `outpoint` if it exists in the
878910 /// wallet's database.
879- pub fn get_utxo ( & self , op : OutPoint ) -> Option < LocalOutput > {
911+ pub fn get_utxo ( & self , params : CanonicalizationParams , op : OutPoint ) -> Option < LocalOutput > {
880912 let ( ( keychain, index) , _) = self . indexed_graph . index . txout ( op) ?;
881913 self . indexed_graph
882914 . graph ( )
883915 . filter_chain_unspents (
884916 & self . chain ,
885917 self . chain . tip ( ) . block_id ( ) ,
918+ params,
886919 core:: iter:: once ( ( ( ) , op) ) ,
887920 )
888921 . map ( |( _, full_txo) | new_local_utxo ( keychain, index, full_txo) )
@@ -1055,10 +1088,10 @@ impl Wallet {
10551088 /// ```
10561089 ///
10571090 /// [`Anchor`]: bdk_chain::Anchor
1058- pub fn get_tx ( & self , txid : Txid ) -> Option < WalletTx > {
1091+ pub fn get_tx ( & self , params : CanonicalizationParams , txid : Txid ) -> Option < WalletTx > {
10591092 let graph = self . indexed_graph . graph ( ) ;
10601093 graph
1061- . list_canonical_txs ( & self . chain , self . chain . tip ( ) . block_id ( ) )
1094+ . list_canonical_txs ( & self . chain , self . chain . tip ( ) . block_id ( ) , params )
10621095 . find ( |tx| tx. tx_node . txid == txid)
10631096 }
10641097
@@ -1073,11 +1106,14 @@ impl Wallet {
10731106 ///
10741107 /// To iterate over all canonical transactions, including those that are irrelevant, use
10751108 /// [`TxGraph::list_canonical_txs`].
1076- pub fn transactions ( & self ) -> impl Iterator < Item = WalletTx > + ' _ {
1109+ pub fn transactions (
1110+ & self ,
1111+ params : CanonicalizationParams ,
1112+ ) -> impl Iterator < Item = WalletTx > + ' _ {
10771113 let tx_graph = self . indexed_graph . graph ( ) ;
10781114 let tx_index = & self . indexed_graph . index ;
10791115 tx_graph
1080- . list_canonical_txs ( & self . chain , self . chain . tip ( ) . block_id ( ) )
1116+ . list_canonical_txs ( & self . chain , self . chain . tip ( ) . block_id ( ) , params )
10811117 . filter ( |c_tx| tx_index. is_tx_relevant ( & c_tx. tx_node . tx ) )
10821118 }
10831119
@@ -1097,21 +1133,26 @@ impl Wallet {
10971133 /// wallet.transactions_sort_by(|tx1, tx2| tx2.chain_position.cmp(&tx1.chain_position));
10981134 /// # Ok::<(), anyhow::Error>(())
10991135 /// ```
1100- pub fn transactions_sort_by < F > ( & self , compare : F ) -> Vec < WalletTx >
1136+ pub fn transactions_sort_by < F > (
1137+ & self ,
1138+ params : CanonicalizationParams ,
1139+ compare : F ,
1140+ ) -> Vec < WalletTx >
11011141 where
11021142 F : FnMut ( & WalletTx , & WalletTx ) -> Ordering ,
11031143 {
1104- let mut txs: Vec < WalletTx > = self . transactions ( ) . collect ( ) ;
1144+ let mut txs: Vec < WalletTx > = self . transactions ( params ) . collect ( ) ;
11051145 txs. sort_unstable_by ( compare) ;
11061146 txs
11071147 }
11081148
11091149 /// Return the balance, separated into available, trusted-pending, untrusted-pending and immature
11101150 /// values.
1111- pub fn balance ( & self ) -> Balance {
1151+ pub fn balance ( & self , params : CanonicalizationParams ) -> Balance {
11121152 self . indexed_graph . graph ( ) . balance (
11131153 & self . chain ,
11141154 self . chain . tip ( ) . block_id ( ) ,
1155+ params,
11151156 self . indexed_graph . index . outpoints ( ) . iter ( ) . cloned ( ) ,
11161157 |& ( k, _) , _| k == KeychainKind :: Internal ,
11171158 )
@@ -1590,7 +1631,7 @@ impl Wallet {
15901631 let txout_index = & self . indexed_graph . index ;
15911632 let chain_tip = self . chain . tip ( ) . block_id ( ) ;
15921633 let chain_positions = graph
1593- . list_canonical_txs ( & self . chain , chain_tip)
1634+ . list_canonical_txs ( & self . chain , chain_tip, CanonicalizationParams :: default ( ) )
15941635 . map ( |canon_tx| ( canon_tx. tx_node . txid , canon_tx. chain_position ) )
15951636 . collect :: < HashMap < Txid , _ > > ( ) ;
15961637
0 commit comments