@@ -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;
@@ -72,6 +74,35 @@ impl BlindedMessagePath {
72
74
) . map_err ( |_| ( ) ) ?,
73
75
} ) )
74
76
}
77
+
78
+ /// Attempts to a use a compact representation for the [`IntroductionNode`] by using a directed
79
+ /// short channel id from a channel in `network_graph` leading to the introduction node.
80
+ ///
81
+ /// While this may result in a smaller encoding, there is a trade off in that the path may
82
+ /// become invalid if the channel is closed or hasn't been propagated via gossip. Therefore,
83
+ /// calling this may not be suitable for long-lived blinded paths.
84
+ pub fn use_compact_introduction_node ( & mut self , network_graph : & ReadOnlyNetworkGraph ) {
85
+ if let IntroductionNode :: NodeId ( pubkey) = & self . 0 . introduction_node {
86
+ let node_id = NodeId :: from_pubkey ( pubkey) ;
87
+ if let Some ( node_info) = network_graph. node ( & node_id) {
88
+ if let Some ( ( scid, channel_info) ) = node_info
89
+ . channels
90
+ . iter ( )
91
+ . filter_map ( |scid| network_graph. channel ( * scid) . map ( |info| ( * scid, info) ) )
92
+ . min_by_key ( |( scid, _) | scid_utils:: block_from_scid ( * scid) )
93
+ {
94
+ let direction = if node_id == channel_info. node_one {
95
+ Direction :: NodeOne
96
+ } else {
97
+ debug_assert_eq ! ( node_id, channel_info. node_two) ;
98
+ Direction :: NodeTwo
99
+ } ;
100
+ self . 0 . introduction_node =
101
+ IntroductionNode :: DirectedShortChannelId ( direction, scid) ;
102
+ }
103
+ }
104
+ }
105
+ }
75
106
}
76
107
77
108
/// An intermediate node, and possibly a short channel id leading to the next node.
0 commit comments