@@ -113,12 +113,15 @@ use alloc::vec::Vec;
113
113
use bdk_core:: ConfirmationBlockTime ;
114
114
pub use bdk_core:: TxUpdate ;
115
115
use bitcoin:: { Amount , Block , OutPoint , ScriptBuf , SignedAmount , Transaction , TxOut , Txid } ;
116
+ use changeset:: { self as tx, indexed:: ChangeSet } ;
116
117
use core:: fmt:: { self , Formatter } ;
117
118
use core:: {
118
119
convert:: Infallible ,
119
120
ops:: { Deref , RangeInclusive } ,
120
121
} ;
121
122
123
+ pub mod changeset;
124
+
122
125
impl < A : Ord , X > From < TxGraph < A , X > > for TxUpdate < A > {
123
126
fn from ( graph : TxGraph < A , X > ) -> Self {
124
127
Self {
@@ -631,7 +634,7 @@ impl<A: Anchor, X: Indexer> TxGraph<A, X> {
631
634
) ;
632
635
}
633
636
None => {
634
- changeset. txouts . insert ( outpoint, txout) ;
637
+ changeset. tx_graph . txouts . insert ( outpoint, txout) ;
635
638
}
636
639
}
637
640
}
@@ -669,7 +672,7 @@ impl<A: Anchor, X: Indexer> TxGraph<A, X> {
669
672
. insert ( txid) ;
670
673
}
671
674
* partial_tx = TxNodeInternal :: Whole ( tx. clone ( ) ) ;
672
- changeset. txs . insert ( tx) ;
675
+ changeset. tx_graph . txs . insert ( tx) ;
673
676
}
674
677
}
675
678
@@ -830,7 +833,7 @@ impl<A: Anchor, X: Indexer> TxGraph<A, X> {
830
833
}
831
834
self . txs_by_highest_conf_heights . insert ( ( new_top_h, txid) ) ;
832
835
}
833
- changeset. anchors . insert ( ( anchor, txid) ) ;
836
+ changeset. tx_graph . anchors . insert ( ( anchor, txid) ) ;
834
837
}
835
838
changeset
836
839
}
@@ -862,7 +865,7 @@ impl<A: Anchor, X: Indexer> TxGraph<A, X> {
862
865
self . txs_by_last_seen . remove ( & ( old_last_seen, txid) ) ;
863
866
}
864
867
self . txs_by_last_seen . insert ( ( seen_at, txid) ) ;
865
- changeset. last_seen . insert ( txid, seen_at) ;
868
+ changeset. tx_graph . last_seen . insert ( txid, seen_at) ;
866
869
}
867
870
changeset
868
871
}
@@ -970,25 +973,28 @@ impl<A: Anchor, X: Indexer> TxGraph<A, X> {
970
973
971
974
/// Determines the [`ChangeSet`] between `self` and an empty [`TxGraph`].
972
975
pub fn initial_changeset ( & self ) -> ChangeSet < A , X :: ChangeSet > {
973
- ChangeSet {
976
+ let tx_graph = tx :: ChangeSet {
974
977
txs : self . full_txs ( ) . map ( |tx_node| tx_node. tx ) . collect ( ) ,
975
978
txouts : self
976
979
. floating_txouts ( )
977
980
. map ( |( op, txout) | ( op, txout. clone ( ) ) )
978
981
. collect ( ) ,
979
982
anchors : self . rev_anchors ( ) ,
980
983
last_seen : self . last_seen . clone ( ) . into_iter ( ) . collect ( ) ,
984
+ } ;
985
+ ChangeSet {
986
+ tx_graph,
981
987
indexer : self . index . initial_changeset ( ) ,
982
988
}
983
989
}
984
990
985
991
/// Indexes the txs and txouts of `changeset` and merges the resulting changes.
986
992
fn index_changeset ( & mut self , changeset : & mut ChangeSet < A , X :: ChangeSet > ) {
987
993
let indexer = & mut changeset. indexer ;
988
- for tx in & changeset. txs {
994
+ for tx in & changeset. tx_graph . txs {
989
995
indexer. merge ( self . index . index_tx ( tx) ) ;
990
996
}
991
- for ( & outpoint, txout) in & changeset. txouts {
997
+ for ( & outpoint, txout) in & changeset. tx_graph . txouts {
992
998
indexer. merge ( self . index . index_txout ( outpoint, txout) ) ;
993
999
}
994
1000
}
@@ -999,6 +1005,7 @@ impl<A: Anchor, X: Indexer> TxGraph<A, X> {
999
1005
// transactions so we do that first.
1000
1006
self . index . apply_changeset ( changeset. indexer ) ;
1001
1007
1008
+ let changeset = changeset. tx_graph ;
1002
1009
for tx in & changeset. txs {
1003
1010
self . index . index_tx ( tx) ;
1004
1011
}
@@ -1397,142 +1404,6 @@ where
1397
1404
}
1398
1405
}
1399
1406
1400
- /// The [`ChangeSet`] represents changes to a [`TxGraph`].
1401
- ///
1402
- /// Since [`TxGraph`] is monotone, the "changeset" can only contain transactions to be added and
1403
- /// not removed.
1404
- ///
1405
- /// Refer to [module-level documentation](self) for more.
1406
- #[ derive( Debug , Clone , PartialEq ) ]
1407
- #[ cfg_attr(
1408
- feature = "serde" ,
1409
- derive( serde:: Deserialize , serde:: Serialize ) ,
1410
- serde( bound(
1411
- deserialize = "A: Ord + serde::Deserialize<'de>, X: serde::Deserialize<'de>" ,
1412
- serialize = "A: Ord + serde::Serialize, X: serde::Serialize" ,
1413
- ) )
1414
- ) ]
1415
- #[ must_use]
1416
- pub struct ChangeSet < A = ( ) , X = ( ) > {
1417
- /// Added transactions.
1418
- pub txs : BTreeSet < Arc < Transaction > > ,
1419
- /// Added txouts.
1420
- pub txouts : BTreeMap < OutPoint , TxOut > ,
1421
- /// Added anchors.
1422
- pub anchors : BTreeSet < ( A , Txid ) > ,
1423
- /// Added last-seen unix timestamps of transactions.
1424
- pub last_seen : BTreeMap < Txid , u64 > ,
1425
- /// [`Indexer`] changes
1426
- pub indexer : X ,
1427
- }
1428
-
1429
- impl < A , X : Default > Default for ChangeSet < A , X > {
1430
- fn default ( ) -> Self {
1431
- Self {
1432
- txs : Default :: default ( ) ,
1433
- txouts : Default :: default ( ) ,
1434
- anchors : Default :: default ( ) ,
1435
- last_seen : Default :: default ( ) ,
1436
- indexer : Default :: default ( ) ,
1437
- }
1438
- }
1439
- }
1440
-
1441
- impl < A , X > From < X > for ChangeSet < A , X > {
1442
- fn from ( indexer : X ) -> Self {
1443
- Self {
1444
- indexer,
1445
- txs : Default :: default ( ) ,
1446
- txouts : Default :: default ( ) ,
1447
- anchors : Default :: default ( ) ,
1448
- last_seen : Default :: default ( ) ,
1449
- }
1450
- }
1451
- }
1452
-
1453
- impl < A , X > ChangeSet < A , X > {
1454
- /// Iterates over all outpoints contained within [`ChangeSet`].
1455
- pub fn txouts ( & self ) -> impl Iterator < Item = ( OutPoint , & TxOut ) > {
1456
- self . txs
1457
- . iter ( )
1458
- . flat_map ( |tx| {
1459
- tx. output
1460
- . iter ( )
1461
- . enumerate ( )
1462
- . map ( move |( vout, txout) | ( OutPoint :: new ( tx. compute_txid ( ) , vout as _ ) , txout) )
1463
- } )
1464
- . chain ( self . txouts . iter ( ) . map ( |( op, txout) | ( * op, txout) ) )
1465
- }
1466
-
1467
- /// Iterates over the heights of that the new transaction anchors in this changeset.
1468
- ///
1469
- /// This is useful if you want to find which heights you need to fetch data about in order to
1470
- /// confirm or exclude these anchors.
1471
- pub fn anchor_heights ( & self ) -> impl Iterator < Item = u32 > + ' _
1472
- where
1473
- A : Anchor ,
1474
- {
1475
- let mut dedup = None ;
1476
- self . anchors
1477
- . iter ( )
1478
- . map ( |( a, _) | a. anchor_block ( ) . height )
1479
- . filter ( move |height| {
1480
- let duplicate = dedup == Some ( * height) ;
1481
- dedup = Some ( * height) ;
1482
- !duplicate
1483
- } )
1484
- }
1485
- }
1486
-
1487
- impl < A : Ord , X : Merge > Merge for ChangeSet < A , X > {
1488
- fn merge ( & mut self , other : Self ) {
1489
- // We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
1490
- // Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
1491
- self . txs . extend ( other. txs ) ;
1492
- self . txouts . extend ( other. txouts ) ;
1493
- self . anchors . extend ( other. anchors ) ;
1494
-
1495
- // last_seen timestamps should only increase
1496
- self . last_seen . extend (
1497
- other
1498
- . last_seen
1499
- . into_iter ( )
1500
- . filter ( |( txid, update_ls) | self . last_seen . get ( txid) < Some ( update_ls) )
1501
- . collect :: < Vec < _ > > ( ) ,
1502
- ) ;
1503
- self . indexer . merge ( other. indexer ) ;
1504
- }
1505
-
1506
- fn is_empty ( & self ) -> bool {
1507
- self . txs . is_empty ( )
1508
- && self . txouts . is_empty ( )
1509
- && self . anchors . is_empty ( )
1510
- && self . last_seen . is_empty ( )
1511
- && self . indexer . is_empty ( )
1512
- }
1513
- }
1514
-
1515
- impl < A : Ord , X > ChangeSet < A , X > {
1516
- /// Transform the [`ChangeSet`] to have [`Anchor`]s of another type.
1517
- ///
1518
- /// This takes in a closure of signature `FnMut(A) -> A2` which is called for each [`Anchor`] to
1519
- /// transform it.
1520
- pub fn map_anchors < A2 : Ord , F > ( self , mut f : F ) -> ChangeSet < A2 , X >
1521
- where
1522
- F : FnMut ( A ) -> A2 ,
1523
- {
1524
- ChangeSet {
1525
- txs : self . txs ,
1526
- txouts : self . txouts ,
1527
- anchors : BTreeSet :: < ( A2 , Txid ) > :: from_iter (
1528
- self . anchors . into_iter ( ) . map ( |( a, txid) | ( f ( a) , txid) ) ,
1529
- ) ,
1530
- last_seen : self . last_seen ,
1531
- indexer : self . indexer ,
1532
- }
1533
- }
1534
- }
1535
-
1536
1407
impl < A , X > AsRef < TxGraph < A , X > > for TxGraph < A , X > {
1537
1408
fn as_ref ( & self ) -> & TxGraph < A , X > {
1538
1409
self
0 commit comments