@@ -19,6 +19,7 @@ use alloc::{
19
19
sync:: Arc ,
20
20
vec:: Vec ,
21
21
} ;
22
+ use chain:: CanonicalizationParams ;
22
23
use core:: { cmp:: Ordering , fmt, mem, ops:: Deref } ;
23
24
24
25
use bdk_chain:: {
@@ -57,6 +58,7 @@ mod params;
57
58
mod persisted;
58
59
pub mod signer;
59
60
pub mod tx_builder;
61
+ pub mod unbroadcasted;
60
62
pub ( crate ) mod utils;
61
63
62
64
use crate :: collections:: { BTreeMap , HashMap , HashSet } ;
@@ -80,6 +82,7 @@ pub use bdk_chain::Balance;
80
82
pub use changeset:: ChangeSet ;
81
83
pub use params:: * ;
82
84
pub use persisted:: * ;
85
+ pub use unbroadcasted:: Unbroadcasted ;
83
86
pub use utils:: IsDust ;
84
87
85
88
/// A Bitcoin wallet
@@ -105,6 +108,7 @@ pub struct Wallet {
105
108
change_signers : Arc < SignersContainer > ,
106
109
chain : LocalChain ,
107
110
indexed_graph : IndexedTxGraph < ConfirmationBlockTime , KeychainTxOutIndex < KeychainKind > > ,
111
+ unbroadcasted : Unbroadcasted ,
108
112
stage : ChangeSet ,
109
113
network : Network ,
110
114
secp : SecpCtx ,
@@ -405,13 +409,15 @@ impl Wallet {
405
409
let change_descriptor = index. get_descriptor ( KeychainKind :: Internal ) . cloned ( ) ;
406
410
let indexed_graph = IndexedTxGraph :: new ( index) ;
407
411
let indexed_graph_changeset = indexed_graph. initial_changeset ( ) ;
412
+ let unbroadcasted = Unbroadcasted :: default ( ) ;
408
413
409
414
let stage = ChangeSet {
410
415
descriptor,
411
416
change_descriptor,
412
417
local_chain : chain_changeset,
413
418
tx_graph : indexed_graph_changeset. tx_graph ,
414
419
indexer : indexed_graph_changeset. indexer ,
420
+ unbroadcasted : Default :: default ( ) ,
415
421
network : Some ( network) ,
416
422
} ;
417
423
@@ -421,6 +427,7 @@ impl Wallet {
421
427
network,
422
428
chain,
423
429
indexed_graph,
430
+ unbroadcasted,
424
431
stage,
425
432
secp,
426
433
} )
@@ -605,13 +612,16 @@ impl Wallet {
605
612
indexed_graph. apply_changeset ( changeset. indexer . into ( ) ) ;
606
613
indexed_graph. apply_changeset ( changeset. tx_graph . into ( ) ) ;
607
614
615
+ let unbroadcasted = Unbroadcasted :: from_changeset ( changeset. unbroadcasted ) ;
616
+
608
617
let stage = ChangeSet :: default ( ) ;
609
618
610
619
Ok ( Some ( Wallet {
611
620
signers,
612
621
change_signers,
613
622
chain,
614
623
indexed_graph,
624
+ unbroadcasted,
615
625
stage,
616
626
network,
617
627
secp,
@@ -809,13 +819,31 @@ impl Wallet {
809
819
self . indexed_graph . index . index_of_spk ( spk) . cloned ( )
810
820
}
811
821
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
+
812
836
/// 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 > + ' _ {
814
841
self . indexed_graph
815
842
. graph ( )
816
843
. filter_chain_unspents (
817
844
& self . chain ,
818
845
self . chain . tip ( ) . block_id ( ) ,
846
+ params,
819
847
self . indexed_graph . index . outpoints ( ) . iter ( ) . cloned ( ) ,
820
848
)
821
849
. map ( |( ( k, i) , full_txo) | new_local_utxo ( k, i, full_txo) )
@@ -824,12 +852,16 @@ impl Wallet {
824
852
/// List all relevant outputs (includes both spent and unspent, confirmed and unconfirmed).
825
853
///
826
854
/// 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 > + ' _ {
828
859
self . indexed_graph
829
860
. graph ( )
830
861
. filter_chain_txouts (
831
862
& self . chain ,
832
863
self . chain . tip ( ) . block_id ( ) ,
864
+ params,
833
865
self . indexed_graph . index . outpoints ( ) . iter ( ) . cloned ( ) ,
834
866
)
835
867
. map ( |( ( k, i) , full_txo) | new_local_utxo ( k, i, full_txo) )
@@ -876,13 +908,14 @@ impl Wallet {
876
908
877
909
/// Returns the utxo owned by this wallet corresponding to `outpoint` if it exists in the
878
910
/// 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 > {
880
912
let ( ( keychain, index) , _) = self . indexed_graph . index . txout ( op) ?;
881
913
self . indexed_graph
882
914
. graph ( )
883
915
. filter_chain_unspents (
884
916
& self . chain ,
885
917
self . chain . tip ( ) . block_id ( ) ,
918
+ params,
886
919
core:: iter:: once ( ( ( ) , op) ) ,
887
920
)
888
921
. map ( |( _, full_txo) | new_local_utxo ( keychain, index, full_txo) )
@@ -1055,10 +1088,10 @@ impl Wallet {
1055
1088
/// ```
1056
1089
///
1057
1090
/// [`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 > {
1059
1092
let graph = self . indexed_graph . graph ( ) ;
1060
1093
graph
1061
- . list_canonical_txs ( & self . chain , self . chain . tip ( ) . block_id ( ) )
1094
+ . list_canonical_txs ( & self . chain , self . chain . tip ( ) . block_id ( ) , params )
1062
1095
. find ( |tx| tx. tx_node . txid == txid)
1063
1096
}
1064
1097
@@ -1073,11 +1106,14 @@ impl Wallet {
1073
1106
///
1074
1107
/// To iterate over all canonical transactions, including those that are irrelevant, use
1075
1108
/// [`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 > + ' _ {
1077
1113
let tx_graph = self . indexed_graph . graph ( ) ;
1078
1114
let tx_index = & self . indexed_graph . index ;
1079
1115
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 )
1081
1117
. filter ( |c_tx| tx_index. is_tx_relevant ( & c_tx. tx_node . tx ) )
1082
1118
}
1083
1119
@@ -1097,21 +1133,26 @@ impl Wallet {
1097
1133
/// wallet.transactions_sort_by(|tx1, tx2| tx2.chain_position.cmp(&tx1.chain_position));
1098
1134
/// # Ok::<(), anyhow::Error>(())
1099
1135
/// ```
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 >
1101
1141
where
1102
1142
F : FnMut ( & WalletTx , & WalletTx ) -> Ordering ,
1103
1143
{
1104
- let mut txs: Vec < WalletTx > = self . transactions ( ) . collect ( ) ;
1144
+ let mut txs: Vec < WalletTx > = self . transactions ( params ) . collect ( ) ;
1105
1145
txs. sort_unstable_by ( compare) ;
1106
1146
txs
1107
1147
}
1108
1148
1109
1149
/// Return the balance, separated into available, trusted-pending, untrusted-pending and immature
1110
1150
/// values.
1111
- pub fn balance ( & self ) -> Balance {
1151
+ pub fn balance ( & self , params : CanonicalizationParams ) -> Balance {
1112
1152
self . indexed_graph . graph ( ) . balance (
1113
1153
& self . chain ,
1114
1154
self . chain . tip ( ) . block_id ( ) ,
1155
+ params,
1115
1156
self . indexed_graph . index . outpoints ( ) . iter ( ) . cloned ( ) ,
1116
1157
|& ( k, _) , _| k == KeychainKind :: Internal ,
1117
1158
)
@@ -1590,7 +1631,7 @@ impl Wallet {
1590
1631
let txout_index = & self . indexed_graph . index ;
1591
1632
let chain_tip = self . chain . tip ( ) . block_id ( ) ;
1592
1633
let chain_positions = graph
1593
- . list_canonical_txs ( & self . chain , chain_tip)
1634
+ . list_canonical_txs ( & self . chain , chain_tip, CanonicalizationParams :: default ( ) )
1594
1635
. map ( |canon_tx| ( canon_tx. tx_node . txid , canon_tx. chain_position ) )
1595
1636
. collect :: < HashMap < Txid , _ > > ( ) ;
1596
1637
0 commit comments