@@ -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 :: CanonicalizationMods ;
97
98
use crate :: ObservedIn ;
98
99
use crate :: { Anchor , Balance , ChainOracle , ChainPosition , FullTxOut , Merge } ;
99
100
use alloc:: collections:: vec_deque:: VecDeque ;
@@ -829,25 +830,46 @@ impl<A: Anchor> TxGraph<A> {
829
830
& ' a self ,
830
831
chain : & ' a C ,
831
832
chain_tip : BlockId ,
833
+ mods : CanonicalizationMods ,
832
834
) -> impl Iterator < Item = Result < CanonicalTx < ' a , Arc < Transaction > , A > , C :: Error > > {
833
- self . canonical_iter ( chain, chain_tip) . flat_map ( move |res| {
834
- res. map ( |( txid, _, canonical_reason) | {
835
- let tx_node = self . get_tx_node ( txid) . expect ( "must contain tx" ) ;
836
- let chain_position = match canonical_reason {
837
- CanonicalReason :: Anchor { anchor, descendant } => match descendant {
838
- Some ( _) => {
839
- let direct_anchor = tx_node
840
- . anchors
841
- . iter ( )
842
- . find_map ( |a| -> Option < Result < A , C :: Error > > {
843
- match chain. is_block_in_chain ( a. anchor_block ( ) , chain_tip) {
844
- Ok ( Some ( true ) ) => Some ( Ok ( a. clone ( ) ) ) ,
845
- Ok ( Some ( false ) ) | Ok ( None ) => None ,
846
- Err ( err) => Some ( Err ( err) ) ,
847
- }
848
- } )
849
- . transpose ( ) ?;
850
- match direct_anchor {
835
+ fn find_direct_anchor < A : Anchor , C : ChainOracle > (
836
+ tx_node : & TxNode < ' _ , Arc < Transaction > , A > ,
837
+ chain : & C ,
838
+ chain_tip : BlockId ,
839
+ ) -> Result < Option < A > , C :: Error > {
840
+ tx_node
841
+ . anchors
842
+ . iter ( )
843
+ . find_map ( |a| -> Option < Result < A , C :: Error > > {
844
+ match chain. is_block_in_chain ( a. anchor_block ( ) , chain_tip) {
845
+ Ok ( Some ( true ) ) => Some ( Ok ( a. clone ( ) ) ) ,
846
+ Ok ( Some ( false ) ) | Ok ( None ) => None ,
847
+ Err ( err) => Some ( Err ( err) ) ,
848
+ }
849
+ } )
850
+ . transpose ( )
851
+ }
852
+ self . canonical_iter ( chain, chain_tip, mods)
853
+ . flat_map ( move |res| {
854
+ res. map ( |( txid, _, canonical_reason) | {
855
+ let tx_node = self . get_tx_node ( txid) . expect ( "must contain tx" ) ;
856
+ let chain_position = match canonical_reason {
857
+ CanonicalReason :: Assumed { descendant } => match descendant {
858
+ Some ( _) => match find_direct_anchor ( & tx_node, chain, chain_tip) ? {
859
+ Some ( anchor) => ChainPosition :: Confirmed {
860
+ anchor,
861
+ transitively : None ,
862
+ } ,
863
+ None => ChainPosition :: Unconfirmed {
864
+ last_seen : tx_node. last_seen_unconfirmed ,
865
+ } ,
866
+ } ,
867
+ None => ChainPosition :: Unconfirmed {
868
+ last_seen : tx_node. last_seen_unconfirmed ,
869
+ } ,
870
+ } ,
871
+ CanonicalReason :: Anchor { anchor, descendant } => match descendant {
872
+ Some ( _) => match find_direct_anchor ( & tx_node, chain, chain_tip) ? {
851
873
Some ( anchor) => ChainPosition :: Confirmed {
852
874
anchor,
853
875
transitively : None ,
@@ -856,26 +878,25 @@ impl<A: Anchor> TxGraph<A> {
856
878
anchor,
857
879
transitively : descendant,
858
880
} ,
859
- }
860
- }
861
- None => ChainPosition :: Confirmed {
862
- anchor ,
863
- transitively : None ,
881
+ } ,
882
+ None => ChainPosition :: Confirmed {
883
+ anchor ,
884
+ transitively : None ,
885
+ } ,
864
886
} ,
865
- } ,
866
- CanonicalReason :: ObservedIn { observed_in, .. } => match observed_in {
867
- ObservedIn :: Mempool ( last_seen) => ChainPosition :: Unconfirmed {
868
- last_seen : Some ( last_seen) ,
887
+ CanonicalReason :: ObservedIn { observed_in, .. } => match observed_in {
888
+ ObservedIn :: Mempool ( last_seen) => ChainPosition :: Unconfirmed {
889
+ last_seen : Some ( last_seen) ,
890
+ } ,
891
+ ObservedIn :: Block ( _) => ChainPosition :: Unconfirmed { last_seen : None } ,
869
892
} ,
870
- ObservedIn :: Block ( _) => ChainPosition :: Unconfirmed { last_seen : None } ,
871
- } ,
872
- } ;
873
- Ok ( CanonicalTx {
874
- chain_position,
875
- tx_node,
893
+ } ;
894
+ Ok ( CanonicalTx {
895
+ chain_position,
896
+ tx_node,
897
+ } )
876
898
} )
877
899
} )
878
- } )
879
900
}
880
901
881
902
/// List graph transactions that are in `chain` with `chain_tip`.
@@ -887,8 +908,9 @@ impl<A: Anchor> TxGraph<A> {
887
908
& ' a self ,
888
909
chain : & ' a C ,
889
910
chain_tip : BlockId ,
911
+ mods : CanonicalizationMods ,
890
912
) -> impl Iterator < Item = CanonicalTx < ' a , Arc < Transaction > , A > > {
891
- self . try_list_canonical_txs ( chain, chain_tip)
913
+ self . try_list_canonical_txs ( chain, chain_tip, mods )
892
914
. map ( |res| res. expect ( "infallible" ) )
893
915
}
894
916
@@ -915,11 +937,12 @@ impl<A: Anchor> TxGraph<A> {
915
937
& ' a self ,
916
938
chain : & ' a C ,
917
939
chain_tip : BlockId ,
940
+ mods : CanonicalizationMods ,
918
941
outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > + ' a ,
919
942
) -> Result < impl Iterator < Item = ( OI , FullTxOut < A > ) > + ' a , C :: Error > {
920
943
let mut canon_txs = HashMap :: < Txid , CanonicalTx < Arc < Transaction > , A > > :: new ( ) ;
921
944
let mut canon_spends = HashMap :: < OutPoint , Txid > :: new ( ) ;
922
- for r in self . try_list_canonical_txs ( chain, chain_tip) {
945
+ for r in self . try_list_canonical_txs ( chain, chain_tip, mods ) {
923
946
let canonical_tx = r?;
924
947
let txid = canonical_tx. tx_node . txid ;
925
948
@@ -988,8 +1011,9 @@ impl<A: Anchor> TxGraph<A> {
988
1011
& ' a self ,
989
1012
chain : & ' a C ,
990
1013
chain_tip : BlockId ,
1014
+ mods : CanonicalizationMods ,
991
1015
) -> CanonicalIter < ' a , A , C > {
992
- CanonicalIter :: new ( self , chain, chain_tip)
1016
+ CanonicalIter :: new ( self , chain, chain_tip, mods )
993
1017
}
994
1018
995
1019
/// Get a filtered list of outputs from the given `outpoints` that are in `chain` with
@@ -1002,9 +1026,10 @@ impl<A: Anchor> TxGraph<A> {
1002
1026
& ' a self ,
1003
1027
chain : & ' a C ,
1004
1028
chain_tip : BlockId ,
1029
+ mods : CanonicalizationMods ,
1005
1030
outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > + ' a ,
1006
1031
) -> impl Iterator < Item = ( OI , FullTxOut < A > ) > + ' a {
1007
- self . try_filter_chain_txouts ( chain, chain_tip, outpoints)
1032
+ self . try_filter_chain_txouts ( chain, chain_tip, mods , outpoints)
1008
1033
. expect ( "oracle is infallible" )
1009
1034
}
1010
1035
@@ -1030,10 +1055,11 @@ impl<A: Anchor> TxGraph<A> {
1030
1055
& ' a self ,
1031
1056
chain : & ' a C ,
1032
1057
chain_tip : BlockId ,
1058
+ mods : CanonicalizationMods ,
1033
1059
outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > + ' a ,
1034
1060
) -> Result < impl Iterator < Item = ( OI , FullTxOut < A > ) > + ' a , C :: Error > {
1035
1061
Ok ( self
1036
- . try_filter_chain_txouts ( chain, chain_tip, outpoints) ?
1062
+ . try_filter_chain_txouts ( chain, chain_tip, mods , outpoints) ?
1037
1063
. filter ( |( _, full_txo) | full_txo. spent_by . is_none ( ) ) )
1038
1064
}
1039
1065
@@ -1047,9 +1073,10 @@ impl<A: Anchor> TxGraph<A> {
1047
1073
& ' a self ,
1048
1074
chain : & ' a C ,
1049
1075
chain_tip : BlockId ,
1076
+ mods : CanonicalizationMods ,
1050
1077
txouts : impl IntoIterator < Item = ( OI , OutPoint ) > + ' a ,
1051
1078
) -> impl Iterator < Item = ( OI , FullTxOut < A > ) > + ' a {
1052
- self . try_filter_chain_unspents ( chain, chain_tip, txouts)
1079
+ self . try_filter_chain_unspents ( chain, chain_tip, mods , txouts)
1053
1080
. expect ( "oracle is infallible" )
1054
1081
}
1055
1082
@@ -1069,6 +1096,7 @@ impl<A: Anchor> TxGraph<A> {
1069
1096
& self ,
1070
1097
chain : & C ,
1071
1098
chain_tip : BlockId ,
1099
+ mods : CanonicalizationMods ,
1072
1100
outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > ,
1073
1101
mut trust_predicate : impl FnMut ( & OI , ScriptBuf ) -> bool ,
1074
1102
) -> Result < Balance , C :: Error > {
@@ -1077,7 +1105,7 @@ impl<A: Anchor> TxGraph<A> {
1077
1105
let mut untrusted_pending = Amount :: ZERO ;
1078
1106
let mut confirmed = Amount :: ZERO ;
1079
1107
1080
- for ( spk_i, txout) in self . try_filter_chain_unspents ( chain, chain_tip, outpoints) ? {
1108
+ for ( spk_i, txout) in self . try_filter_chain_unspents ( chain, chain_tip, mods , outpoints) ? {
1081
1109
match & txout. chain_position {
1082
1110
ChainPosition :: Confirmed { .. } => {
1083
1111
if txout. is_confirmed_and_spendable ( chain_tip. height ) {
@@ -1113,10 +1141,11 @@ impl<A: Anchor> TxGraph<A> {
1113
1141
& self ,
1114
1142
chain : & C ,
1115
1143
chain_tip : BlockId ,
1144
+ mods : CanonicalizationMods ,
1116
1145
outpoints : impl IntoIterator < Item = ( OI , OutPoint ) > ,
1117
1146
trust_predicate : impl FnMut ( & OI , ScriptBuf ) -> bool ,
1118
1147
) -> Balance {
1119
- self . try_balance ( chain, chain_tip, outpoints, trust_predicate)
1148
+ self . try_balance ( chain, chain_tip, mods , outpoints, trust_predicate)
1120
1149
. expect ( "oracle is infallible" )
1121
1150
}
1122
1151
}
0 commit comments