@@ -1098,6 +1098,52 @@ fn update_last_seen_unconfirmed() {
1098
1098
) ;
1099
1099
}
1100
1100
1101
+ #[ test]
1102
+ fn list_canonical_txs ( ) {
1103
+ use bdk_chain:: local_chain:: CheckPoint ;
1104
+ let mut graph = TxGraph :: < BlockId > :: default ( ) ;
1105
+
1106
+ // insert three new txs
1107
+ // all graph transactions are present, but not canonical
1108
+ let txs = vec ! [ new_tx( 0 ) , new_tx( 1 ) , new_tx( 2 ) ] ;
1109
+ let txids: Vec < _ > = txs. iter ( ) . map ( Transaction :: txid) . collect ( ) ;
1110
+ for tx in txs {
1111
+ let _ = graph. insert_tx ( tx) ;
1112
+ }
1113
+ let full_txs: Vec < _ > = graph. full_txs ( ) . collect ( ) ;
1114
+ assert_eq ! ( full_txs. len( ) , 3 ) ;
1115
+
1116
+ let cp = CheckPoint :: from_block_ids (
1117
+ [ ( 0 , BlockHash :: all_zeros ( ) ) , ( 2 , BlockHash :: all_zeros ( ) ) ]
1118
+ . into_iter ( )
1119
+ . map ( BlockId :: from) ,
1120
+ )
1121
+ . unwrap ( ) ;
1122
+ let anchor: BlockId = cp. iter ( ) . next ( ) . unwrap ( ) . block_id ( ) ;
1123
+ let chain = LocalChain :: from_tip ( cp) . unwrap ( ) ;
1124
+
1125
+ let canonical_txs: Vec < _ > = graph
1126
+ . canonical_transactions ( & chain, chain. tip ( ) . block_id ( ) )
1127
+ . collect ( ) ;
1128
+ assert ! ( canonical_txs. is_empty( ) ) ;
1129
+
1130
+ // insert seen_at for tx0, it should be returned by `canonical_transactions`
1131
+ let _ = graph. insert_seen_at ( txids[ 0 ] , 0 ) ;
1132
+ let canonical_txs: Vec < _ > = graph
1133
+ . canonical_transactions ( & chain, chain. tip ( ) . block_id ( ) )
1134
+ . collect ( ) ;
1135
+ assert_eq ! ( canonical_txs. len( ) , 1 ) ;
1136
+ assert_eq ! ( canonical_txs. first( ) . unwrap( ) . tx_node. txid, txids[ 0 ] ) ;
1137
+
1138
+ // insert anchor for tx1, it is now also canonical
1139
+ let _ = graph. insert_anchor ( txids[ 1 ] , anchor) ;
1140
+ let canonical_txs: BTreeSet < _ > = graph
1141
+ . canonical_transactions ( & chain, chain. tip ( ) . block_id ( ) )
1142
+ . collect ( ) ;
1143
+ assert_eq ! ( canonical_txs. len( ) , 2 ) ;
1144
+ assert_eq ! ( canonical_txs. first( ) . unwrap( ) . tx_node. txid, txids[ 1 ] ) ;
1145
+ }
1146
+
1101
1147
#[ test]
1102
1148
/// The `map_anchors` allow a caller to pass a function to reconstruct the [`TxGraph`] with any [`Anchor`],
1103
1149
/// even though the function is non-deterministic.
0 commit comments