@@ -94,6 +94,7 @@ use crate::collections::*;
94
94
use crate :: BlockId ;
95
95
use crate :: CanonicalIter ;
96
96
use crate :: CanonicalReason ;
97
+ use crate :: CanonicalizationParams ;
97
98
use crate :: ObservedIn ;
98
99
use crate :: { Anchor , Balance , ChainOracle , ChainPosition , FullTxOut , Merge } ;
99
100
use alloc:: collections:: vec_deque:: VecDeque ;
@@ -797,25 +798,46 @@ impl<A: Anchor> TxGraph<A> {
797
798
& ' a self ,
798
799
chain : & ' a C ,
799
800
chain_tip : BlockId ,
801
+ mods : CanonicalizationParams ,
800
802
) -> 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) ? {
819
841
Some ( anchor) => ChainPosition :: Confirmed {
820
842
anchor,
821
843
transitively : None ,
@@ -824,26 +846,25 @@ impl<A: Anchor> TxGraph<A> {
824
846
anchor,
825
847
transitively : descendant,
826
848
} ,
827
- }
828
- }
829
- None => ChainPosition :: Confirmed {
830
- anchor ,
831
- transitively : None ,
849
+ } ,
850
+ None => ChainPosition :: Confirmed {
851
+ anchor ,
852
+ transitively : None ,
853
+ } ,
832
854
} ,
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 } ,
837
860
} ,
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
+ } )
844
866
} )
845
867
} )
846
- } )
847
868
}
848
869
849
870
/// List graph transactions that are in `chain` with `chain_tip`.
@@ -855,8 +876,9 @@ impl<A: Anchor> TxGraph<A> {
855
876
& ' a self ,
856
877
chain : & ' a C ,
857
878
chain_tip : BlockId ,
879
+ mods : CanonicalizationParams ,
858
880
) -> 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 )
860
882
. map ( |res| res. expect ( "infallible" ) )
861
883
}
862
884
@@ -883,11 +905,12 @@ impl<A: Anchor> TxGraph<A> {
883
905
& ' a self ,
884
906
chain : & ' a C ,
885
907
chain_tip : BlockId ,
908
+ mods : CanonicalizationParams ,
886
909
outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > + ' a ,
887
910
) -> Result < impl Iterator < Item = ( OI , FullTxOut < A > ) > + ' a , C :: Error > {
888
911
let mut canon_txs = HashMap :: < Txid , CanonicalTx < Arc < Transaction > , A > > :: new ( ) ;
889
912
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 ) {
891
914
let canonical_tx = r?;
892
915
let txid = canonical_tx. tx_node . txid ;
893
916
@@ -956,8 +979,9 @@ impl<A: Anchor> TxGraph<A> {
956
979
& ' a self ,
957
980
chain : & ' a C ,
958
981
chain_tip : BlockId ,
982
+ mods : CanonicalizationParams ,
959
983
) -> CanonicalIter < ' a , A , C > {
960
- CanonicalIter :: new ( self , chain, chain_tip)
984
+ CanonicalIter :: new ( self , chain, chain_tip, mods )
961
985
}
962
986
963
987
/// Get a filtered list of outputs from the given `outpoints` that are in `chain` with
@@ -970,9 +994,10 @@ impl<A: Anchor> TxGraph<A> {
970
994
& ' a self ,
971
995
chain : & ' a C ,
972
996
chain_tip : BlockId ,
997
+ mods : CanonicalizationParams ,
973
998
outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > + ' a ,
974
999
) -> 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)
976
1001
. expect ( "oracle is infallible" )
977
1002
}
978
1003
@@ -998,10 +1023,11 @@ impl<A: Anchor> TxGraph<A> {
998
1023
& ' a self ,
999
1024
chain : & ' a C ,
1000
1025
chain_tip : BlockId ,
1026
+ mods : CanonicalizationParams ,
1001
1027
outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > + ' a ,
1002
1028
) -> Result < impl Iterator < Item = ( OI , FullTxOut < A > ) > + ' a , C :: Error > {
1003
1029
Ok ( self
1004
- . try_filter_chain_txouts ( chain, chain_tip, outpoints) ?
1030
+ . try_filter_chain_txouts ( chain, chain_tip, mods , outpoints) ?
1005
1031
. filter ( |( _, full_txo) | full_txo. spent_by . is_none ( ) ) )
1006
1032
}
1007
1033
@@ -1015,9 +1041,10 @@ impl<A: Anchor> TxGraph<A> {
1015
1041
& ' a self ,
1016
1042
chain : & ' a C ,
1017
1043
chain_tip : BlockId ,
1044
+ mods : CanonicalizationParams ,
1018
1045
txouts : impl IntoIterator < Item = ( OI , OutPoint ) > + ' a ,
1019
1046
) -> 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)
1021
1048
. expect ( "oracle is infallible" )
1022
1049
}
1023
1050
@@ -1037,6 +1064,7 @@ impl<A: Anchor> TxGraph<A> {
1037
1064
& self ,
1038
1065
chain : & C ,
1039
1066
chain_tip : BlockId ,
1067
+ mods : CanonicalizationParams ,
1040
1068
outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > ,
1041
1069
mut trust_predicate : impl FnMut ( & OI , ScriptBuf ) -> bool ,
1042
1070
) -> Result < Balance , C :: Error > {
@@ -1045,7 +1073,7 @@ impl<A: Anchor> TxGraph<A> {
1045
1073
let mut untrusted_pending = Amount :: ZERO ;
1046
1074
let mut confirmed = Amount :: ZERO ;
1047
1075
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) ? {
1049
1077
match & txout. chain_position {
1050
1078
ChainPosition :: Confirmed { .. } => {
1051
1079
if txout. is_confirmed_and_spendable ( chain_tip. height ) {
@@ -1081,10 +1109,11 @@ impl<A: Anchor> TxGraph<A> {
1081
1109
& self ,
1082
1110
chain : & C ,
1083
1111
chain_tip : BlockId ,
1112
+ mods : CanonicalizationParams ,
1084
1113
outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > ,
1085
1114
trust_predicate : impl FnMut ( & OI , ScriptBuf ) -> bool ,
1086
1115
) -> Balance {
1087
- self . try_balance ( chain, chain_tip, outpoints, trust_predicate)
1116
+ self . try_balance ( chain, chain_tip, mods , outpoints, trust_predicate)
1088
1117
. expect ( "oracle is infallible" )
1089
1118
}
1090
1119
}
0 commit comments