@@ -16,7 +16,7 @@ use crate::prelude::*;
16
16
17
17
use bitcoin:: hashes:: hmac:: Hmac ;
18
18
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
19
- use crate :: blinded_path:: { BlindedHop , BlindedPath , IntroductionNode , NextMessageHop , NodeIdLookUp } ;
19
+ use crate :: blinded_path:: { BlindedHop , BlindedPath , Direction , IntroductionNode , NextMessageHop , NodeIdLookUp } ;
20
20
use crate :: blinded_path:: utils;
21
21
use crate :: io;
22
22
use crate :: io:: Cursor ;
@@ -25,8 +25,10 @@ use crate::ln::msgs::DecodeError;
25
25
use crate :: ln:: { PaymentHash , onion_utils} ;
26
26
use crate :: offers:: nonce:: Nonce ;
27
27
use crate :: onion_message:: packet:: ControlTlvs ;
28
+ use crate :: routing:: gossip:: { NodeId , ReadOnlyNetworkGraph } ;
28
29
use crate :: sign:: { EntropySource , NodeSigner , Recipient } ;
29
30
use crate :: crypto:: streams:: ChaChaPolyReadAdapter ;
31
+ use crate :: util:: scid_utils;
30
32
use crate :: util:: ser:: { FixedLengthReader , LengthReadableArgs , Readable , Writeable , Writer } ;
31
33
32
34
use core:: mem;
@@ -81,6 +83,35 @@ impl BlindedMessagePath {
81
83
) . map_err ( |_| ( ) ) ?,
82
84
} ) )
83
85
}
86
+
87
+ /// Attempts to a use a compact representation for the [`IntroductionNode`] by using a directed
88
+ /// short channel id from a channel in `network_graph` leading to the introduction node.
89
+ ///
90
+ /// While this may result in a smaller encoding, there is a trade off in that the path may
91
+ /// become invalid if the channel is closed or hasn't been propagated via gossip. Therefore,
92
+ /// calling this may not be suitable for long-lived blinded paths.
93
+ pub fn use_compact_introduction_node ( & mut self , network_graph : & ReadOnlyNetworkGraph ) {
94
+ if let IntroductionNode :: NodeId ( pubkey) = & self . 0 . introduction_node {
95
+ let node_id = NodeId :: from_pubkey ( pubkey) ;
96
+ if let Some ( node_info) = network_graph. node ( & node_id) {
97
+ if let Some ( ( scid, channel_info) ) = node_info
98
+ . channels
99
+ . iter ( )
100
+ . filter_map ( |scid| network_graph. channel ( * scid) . map ( |info| ( * scid, info) ) )
101
+ . min_by_key ( |( scid, _) | scid_utils:: block_from_scid ( * scid) )
102
+ {
103
+ let direction = if node_id == channel_info. node_one {
104
+ Direction :: NodeOne
105
+ } else {
106
+ debug_assert_eq ! ( node_id, channel_info. node_two) ;
107
+ Direction :: NodeTwo
108
+ } ;
109
+ self . 0 . introduction_node =
110
+ IntroductionNode :: DirectedShortChannelId ( direction, scid) ;
111
+ }
112
+ }
113
+ }
114
+ }
84
115
}
85
116
86
117
/// An intermediate node, and possibly a short channel id leading to the next node.
0 commit comments