@@ -94,6 +94,7 @@ use crate::collections::*;
9494use crate :: BlockId ;
9595use crate :: CanonicalIter ;
9696use crate :: CanonicalReason ;
97+ use crate :: CanonicalizationParams ;
9798use crate :: ObservedIn ;
9899use crate :: { Anchor , Balance , ChainOracle , ChainPosition , FullTxOut , Merge } ;
99100use alloc:: collections:: vec_deque:: VecDeque ;
@@ -797,25 +798,46 @@ impl<A: Anchor> TxGraph<A> {
797798 & ' a self ,
798799 chain : & ' a C ,
799800 chain_tip : BlockId ,
801+ mods : CanonicalizationParams ,
800802 ) -> impl Iterator < Item = Result < CanonicalTx < ' a , Arc < Transaction > , A > , C :: Error > > {
801- self . canonical_iter ( chain, chain_tip) . flat_map ( move |res| {
802- res. map ( |( txid, _, canonical_reason) | {
803- let tx_node = self . get_tx_node ( txid) . expect ( "must contain tx" ) ;
804- let chain_position = match canonical_reason {
805- CanonicalReason :: Anchor { anchor, descendant } => match descendant {
806- Some ( _) => {
807- let direct_anchor = tx_node
808- . anchors
809- . iter ( )
810- . find_map ( |a| -> Option < Result < A , C :: Error > > {
811- match chain. is_block_in_chain ( a. anchor_block ( ) , chain_tip) {
812- Ok ( Some ( true ) ) => Some ( Ok ( a. clone ( ) ) ) ,
813- Ok ( Some ( false ) ) | Ok ( None ) => None ,
814- Err ( err) => Some ( Err ( err) ) ,
815- }
816- } )
817- . transpose ( ) ?;
818- match direct_anchor {
803+ fn find_direct_anchor < A : Anchor , C : ChainOracle > (
804+ tx_node : & TxNode < ' _ , Arc < Transaction > , A > ,
805+ chain : & C ,
806+ chain_tip : BlockId ,
807+ ) -> Result < Option < A > , C :: Error > {
808+ tx_node
809+ . anchors
810+ . iter ( )
811+ . find_map ( |a| -> Option < Result < A , C :: Error > > {
812+ match chain. is_block_in_chain ( a. anchor_block ( ) , chain_tip) {
813+ Ok ( Some ( true ) ) => Some ( Ok ( a. clone ( ) ) ) ,
814+ Ok ( Some ( false ) ) | Ok ( None ) => None ,
815+ Err ( err) => Some ( Err ( err) ) ,
816+ }
817+ } )
818+ . transpose ( )
819+ }
820+ self . canonical_iter ( chain, chain_tip, mods)
821+ . flat_map ( move |res| {
822+ res. map ( |( txid, _, canonical_reason) | {
823+ let tx_node = self . get_tx_node ( txid) . expect ( "must contain tx" ) ;
824+ let chain_position = match canonical_reason {
825+ CanonicalReason :: Assumed { descendant } => match descendant {
826+ Some ( _) => match find_direct_anchor ( & tx_node, chain, chain_tip) ? {
827+ Some ( anchor) => ChainPosition :: Confirmed {
828+ anchor,
829+ transitively : None ,
830+ } ,
831+ None => ChainPosition :: Unconfirmed {
832+ last_seen : tx_node. last_seen_unconfirmed ,
833+ } ,
834+ } ,
835+ None => ChainPosition :: Unconfirmed {
836+ last_seen : tx_node. last_seen_unconfirmed ,
837+ } ,
838+ } ,
839+ CanonicalReason :: Anchor { anchor, descendant } => match descendant {
840+ Some ( _) => match find_direct_anchor ( & tx_node, chain, chain_tip) ? {
819841 Some ( anchor) => ChainPosition :: Confirmed {
820842 anchor,
821843 transitively : None ,
@@ -824,26 +846,25 @@ impl<A: Anchor> TxGraph<A> {
824846 anchor,
825847 transitively : descendant,
826848 } ,
827- }
828- }
829- None => ChainPosition :: Confirmed {
830- anchor ,
831- transitively : None ,
849+ } ,
850+ None => ChainPosition :: Confirmed {
851+ anchor ,
852+ transitively : None ,
853+ } ,
832854 } ,
833- } ,
834- CanonicalReason :: ObservedIn { observed_in, .. } => match observed_in {
835- ObservedIn :: Mempool ( last_seen) => ChainPosition :: Unconfirmed {
836- last_seen : Some ( last_seen) ,
855+ CanonicalReason :: ObservedIn { observed_in, .. } => match observed_in {
856+ ObservedIn :: Mempool ( last_seen) => ChainPosition :: Unconfirmed {
857+ last_seen : Some ( last_seen) ,
858+ } ,
859+ ObservedIn :: Block ( _) => ChainPosition :: Unconfirmed { last_seen : None } ,
837860 } ,
838- ObservedIn :: Block ( _) => ChainPosition :: Unconfirmed { last_seen : None } ,
839- } ,
840- } ;
841- Ok ( CanonicalTx {
842- chain_position,
843- tx_node,
861+ } ;
862+ Ok ( CanonicalTx {
863+ chain_position,
864+ tx_node,
865+ } )
844866 } )
845867 } )
846- } )
847868 }
848869
849870 /// List graph transactions that are in `chain` with `chain_tip`.
@@ -855,8 +876,9 @@ impl<A: Anchor> TxGraph<A> {
855876 & ' a self ,
856877 chain : & ' a C ,
857878 chain_tip : BlockId ,
879+ mods : CanonicalizationParams ,
858880 ) -> impl Iterator < Item = CanonicalTx < ' a , Arc < Transaction > , A > > {
859- self . try_list_canonical_txs ( chain, chain_tip)
881+ self . try_list_canonical_txs ( chain, chain_tip, mods )
860882 . map ( |res| res. expect ( "infallible" ) )
861883 }
862884
@@ -883,11 +905,12 @@ impl<A: Anchor> TxGraph<A> {
883905 & ' a self ,
884906 chain : & ' a C ,
885907 chain_tip : BlockId ,
908+ mods : CanonicalizationParams ,
886909 outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > + ' a ,
887910 ) -> Result < impl Iterator < Item = ( OI , FullTxOut < A > ) > + ' a , C :: Error > {
888911 let mut canon_txs = HashMap :: < Txid , CanonicalTx < Arc < Transaction > , A > > :: new ( ) ;
889912 let mut canon_spends = HashMap :: < OutPoint , Txid > :: new ( ) ;
890- for r in self . try_list_canonical_txs ( chain, chain_tip) {
913+ for r in self . try_list_canonical_txs ( chain, chain_tip, mods ) {
891914 let canonical_tx = r?;
892915 let txid = canonical_tx. tx_node . txid ;
893916
@@ -956,8 +979,9 @@ impl<A: Anchor> TxGraph<A> {
956979 & ' a self ,
957980 chain : & ' a C ,
958981 chain_tip : BlockId ,
982+ mods : CanonicalizationParams ,
959983 ) -> CanonicalIter < ' a , A , C > {
960- CanonicalIter :: new ( self , chain, chain_tip)
984+ CanonicalIter :: new ( self , chain, chain_tip, mods )
961985 }
962986
963987 /// Get a filtered list of outputs from the given `outpoints` that are in `chain` with
@@ -970,9 +994,10 @@ impl<A: Anchor> TxGraph<A> {
970994 & ' a self ,
971995 chain : & ' a C ,
972996 chain_tip : BlockId ,
997+ mods : CanonicalizationParams ,
973998 outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > + ' a ,
974999 ) -> impl Iterator < Item = ( OI , FullTxOut < A > ) > + ' a {
975- self . try_filter_chain_txouts ( chain, chain_tip, outpoints)
1000+ self . try_filter_chain_txouts ( chain, chain_tip, mods , outpoints)
9761001 . expect ( "oracle is infallible" )
9771002 }
9781003
@@ -998,10 +1023,11 @@ impl<A: Anchor> TxGraph<A> {
9981023 & ' a self ,
9991024 chain : & ' a C ,
10001025 chain_tip : BlockId ,
1026+ mods : CanonicalizationParams ,
10011027 outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > + ' a ,
10021028 ) -> Result < impl Iterator < Item = ( OI , FullTxOut < A > ) > + ' a , C :: Error > {
10031029 Ok ( self
1004- . try_filter_chain_txouts ( chain, chain_tip, outpoints) ?
1030+ . try_filter_chain_txouts ( chain, chain_tip, mods , outpoints) ?
10051031 . filter ( |( _, full_txo) | full_txo. spent_by . is_none ( ) ) )
10061032 }
10071033
@@ -1015,9 +1041,10 @@ impl<A: Anchor> TxGraph<A> {
10151041 & ' a self ,
10161042 chain : & ' a C ,
10171043 chain_tip : BlockId ,
1044+ mods : CanonicalizationParams ,
10181045 txouts : impl IntoIterator < Item = ( OI , OutPoint ) > + ' a ,
10191046 ) -> impl Iterator < Item = ( OI , FullTxOut < A > ) > + ' a {
1020- self . try_filter_chain_unspents ( chain, chain_tip, txouts)
1047+ self . try_filter_chain_unspents ( chain, chain_tip, mods , txouts)
10211048 . expect ( "oracle is infallible" )
10221049 }
10231050
@@ -1037,6 +1064,7 @@ impl<A: Anchor> TxGraph<A> {
10371064 & self ,
10381065 chain : & C ,
10391066 chain_tip : BlockId ,
1067+ mods : CanonicalizationParams ,
10401068 outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > ,
10411069 mut trust_predicate : impl FnMut ( & OI , ScriptBuf ) -> bool ,
10421070 ) -> Result < Balance , C :: Error > {
@@ -1045,7 +1073,7 @@ impl<A: Anchor> TxGraph<A> {
10451073 let mut untrusted_pending = Amount :: ZERO ;
10461074 let mut confirmed = Amount :: ZERO ;
10471075
1048- for ( spk_i, txout) in self . try_filter_chain_unspents ( chain, chain_tip, outpoints) ? {
1076+ for ( spk_i, txout) in self . try_filter_chain_unspents ( chain, chain_tip, mods , outpoints) ? {
10491077 match & txout. chain_position {
10501078 ChainPosition :: Confirmed { .. } => {
10511079 if txout. is_confirmed_and_spendable ( chain_tip. height ) {
@@ -1081,10 +1109,11 @@ impl<A: Anchor> TxGraph<A> {
10811109 & self ,
10821110 chain : & C ,
10831111 chain_tip : BlockId ,
1112+ mods : CanonicalizationParams ,
10841113 outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > ,
10851114 trust_predicate : impl FnMut ( & OI , ScriptBuf ) -> bool ,
10861115 ) -> Balance {
1087- self . try_balance ( chain, chain_tip, outpoints, trust_predicate)
1116+ self . try_balance ( chain, chain_tip, mods , outpoints, trust_predicate)
10881117 . expect ( "oracle is infallible" )
10891118 }
10901119}
0 commit comments