@@ -146,29 +146,12 @@ impl<A> From<TxGraph<A>> for Update<A> {
146
146
impl < A : Ord + Clone > From < Update < A > > for TxGraph < A > {
147
147
fn from ( update : Update < A > ) -> Self {
148
148
let mut graph = TxGraph :: < A > :: default ( ) ;
149
- let _ = graph. apply_update ( update) ;
149
+ let _ = graph. apply_update_at ( update, None ) ;
150
150
graph
151
151
}
152
152
}
153
153
154
154
impl < A : Ord > Update < A > {
155
- /// Update the [`seen_ats`](Self::seen_ats) for all unanchored transactions.
156
- pub fn update_last_seen_unconfirmed ( & mut self , seen_at : u64 ) {
157
- let seen_ats = & mut self . seen_ats ;
158
- let anchors = & self . anchors ;
159
- let unanchored_txids = self . txs . iter ( ) . map ( |tx| tx. compute_txid ( ) ) . filter ( |txid| {
160
- for ( _, anchor_txid) in anchors {
161
- if txid == anchor_txid {
162
- return false ;
163
- }
164
- }
165
- true
166
- } ) ;
167
- for txid in unanchored_txids {
168
- seen_ats. insert ( txid, seen_at) ;
169
- }
170
- }
171
-
172
155
/// Extend this update with `other`.
173
156
pub fn extend ( & mut self , other : Update < A > ) {
174
157
self . txs . extend ( other. txs ) ;
@@ -762,25 +745,56 @@ impl<A: Clone + Ord> TxGraph<A> {
762
745
changeset
763
746
}
764
747
765
- /// Extends this graph with another so that `self` becomes the union of the two sets of
766
- /// transactions.
748
+ /// Extends this graph with the given `update`.
767
749
///
768
750
/// The returned [`ChangeSet`] is the set difference between `update` and `self` (transactions that
769
751
/// exist in `update` but not in `self`).
752
+ #[ cfg( feature = "std" ) ]
753
+ #[ cfg_attr( docsrs, doc( cfg( feature = "std" ) ) ) ]
770
754
pub fn apply_update ( & mut self , update : Update < A > ) -> ChangeSet < A > {
755
+ use std:: time:: * ;
756
+ let now = SystemTime :: now ( )
757
+ . duration_since ( UNIX_EPOCH )
758
+ . expect ( "current time must be greater than epoch anchor" ) ;
759
+ self . apply_update_at ( update, Some ( now. as_secs ( ) ) )
760
+ }
761
+
762
+ /// Extends this graph with the given `update` alongside an optional `seen_at` timestamp.
763
+ ///
764
+ /// `seen_at` represents when the update is seen (in unix seconds). It is used to determine the
765
+ /// `last_seen`s for all transactions in the update which have no corresponding anchor(s). The
766
+ /// `last_seen` value is used internally to determine precedence of conflicting unconfirmed
767
+ /// transactions (where the transaction with the lower `last_seen` value is omitted from the
768
+ /// canonical history).
769
+ ///
770
+ /// Not setting a `seen_at` value means unconfirmed transactions introduced by this update will
771
+ /// not be part of the canonical history of transactions.
772
+ ///
773
+ /// Use [`apply_update`](TxGraph::apply_update) to have the `seen_at` value automatically set
774
+ /// to the current time.
775
+ pub fn apply_update_at ( & mut self , update : Update < A > , seen_at : Option < u64 > ) -> ChangeSet < A > {
771
776
let mut changeset = ChangeSet :: < A > :: default ( ) ;
777
+ let mut unanchored_txs = HashSet :: < Txid > :: new ( ) ;
772
778
for tx in update. txs {
773
- changeset. merge ( self . insert_tx ( tx) ) ;
779
+ if unanchored_txs. insert ( tx. compute_txid ( ) ) {
780
+ changeset. merge ( self . insert_tx ( tx) ) ;
781
+ }
774
782
}
775
783
for ( outpoint, txout) in update. txouts {
776
784
changeset. merge ( self . insert_txout ( outpoint, txout) ) ;
777
785
}
778
786
for ( anchor, txid) in update. anchors {
787
+ unanchored_txs. remove ( & txid) ;
779
788
changeset. merge ( self . insert_anchor ( txid, anchor) ) ;
780
789
}
781
790
for ( txid, seen_at) in update. seen_ats {
782
791
changeset. merge ( self . insert_seen_at ( txid, seen_at) ) ;
783
792
}
793
+ if let Some ( seen_at) = seen_at {
794
+ for txid in unanchored_txs {
795
+ changeset. merge ( self . insert_seen_at ( txid, seen_at) ) ;
796
+ }
797
+ }
784
798
changeset
785
799
}
786
800
0 commit comments