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