@@ -50,12 +50,15 @@ const MAX_EXCESS_BYTES_FOR_RELAY: usize = 1024;
50
50
/// This value ensures a reply fits within the 65k payload limit and is consistent with other implementations.
51
51
const MAX_SCIDS_PER_REPLY : usize = 8000 ;
52
52
53
+ /// Represents the compressed public key of a node
54
+ pub type NodeId = [ u8 ; 33 ] ;
55
+
53
56
/// Represents the network as nodes and channels between them
54
57
pub struct NetworkGraph {
55
58
genesis_hash : BlockHash ,
56
59
// Lock order: channels -> nodes
57
60
channels : RwLock < BTreeMap < u64 , ChannelInfo > > ,
58
- nodes : RwLock < BTreeMap < PublicKey , NodeInfo > > ,
61
+ nodes : RwLock < BTreeMap < NodeId , NodeInfo > > ,
59
62
}
60
63
61
64
impl Clone for NetworkGraph {
@@ -73,7 +76,7 @@ impl Clone for NetworkGraph {
73
76
/// A read-only view of [`NetworkGraph`].
74
77
pub struct ReadOnlyNetworkGraph < ' a > {
75
78
channels : RwLockReadGuard < ' a , BTreeMap < u64 , ChannelInfo > > ,
76
- nodes : RwLockReadGuard < ' a , BTreeMap < PublicKey , NodeInfo > > ,
79
+ nodes : RwLockReadGuard < ' a , BTreeMap < NodeId , NodeInfo > > ,
77
80
}
78
81
79
82
/// Update to the [`NetworkGraph`] based on payment failure information conveyed via the Onion
@@ -209,7 +212,7 @@ where C::Target: chain::Access, L::Target: Logger
209
212
NetworkUpdate :: NodeFailure { ref node_id, is_permanent } => {
210
213
let action = if is_permanent { "Removing" } else { "Disabling" } ;
211
214
log_debug ! ( self . logger, "{} node graph entry for {} due to a payment failure." , action, node_id) ;
212
- self . network_graph . fail_node ( node_id, is_permanent) ;
215
+ self . network_graph . fail_node ( & node_id. serialize ( ) , is_permanent) ;
213
216
} ,
214
217
}
215
218
}
@@ -277,11 +280,11 @@ where C::Target: chain::Access, L::Target: Logger
277
280
let mut result = Vec :: with_capacity ( batch_amount as usize ) ;
278
281
let nodes = self . network_graph . nodes . read ( ) . unwrap ( ) ;
279
282
let mut iter = if let Some ( pubkey) = starting_point {
280
- let mut iter = nodes. range ( ( * pubkey) ..) ;
283
+ let mut iter = nodes. range ( ( pubkey. serialize ( ) ) ..) ;
281
284
iter. next ( ) ;
282
285
iter
283
286
} else {
284
- nodes. range ( ..)
287
+ nodes. range :: < NodeId , _ > ( ..)
285
288
} ;
286
289
while result. len ( ) < batch_amount as usize {
287
290
if let Some ( ( _, ref node) ) = iter. next ( ) {
@@ -314,7 +317,7 @@ where C::Target: chain::Access, L::Target: Logger
314
317
}
315
318
316
319
// Check if we need to perform a full synchronization with this peer
317
- if !self . should_request_full_sync ( their_node_id) {
320
+ if !self . should_request_full_sync ( & their_node_id) {
318
321
return ( ) ;
319
322
}
320
323
@@ -551,11 +554,11 @@ pub struct ChannelInfo {
551
554
/// Protocol features of a channel communicated during its announcement
552
555
pub features : ChannelFeatures ,
553
556
/// Source node of the first direction of a channel
554
- pub node_one : PublicKey ,
557
+ pub node_one : NodeId ,
555
558
/// Details about the first direction of a channel
556
559
pub one_to_two : Option < DirectionalChannelInfo > ,
557
560
/// Source node of the second direction of a channel
558
- pub node_two : PublicKey ,
561
+ pub node_two : NodeId ,
559
562
/// Details about the second direction of a channel
560
563
pub two_to_one : Option < DirectionalChannelInfo > ,
561
564
/// The channel capacity as seen on-chain, if chain lookup is available.
@@ -570,7 +573,7 @@ pub struct ChannelInfo {
570
573
impl fmt:: Display for ChannelInfo {
571
574
fn fmt ( & self , f : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
572
575
write ! ( f, "features: {}, node_one: {}, one_to_two: {:?}, node_two: {}, two_to_one: {:?}" ,
573
- log_bytes!( self . features. encode( ) ) , log_pubkey !( self . node_one) , self . one_to_two, log_pubkey !( self . node_two) , self . two_to_one) ?;
576
+ log_bytes!( self . features. encode( ) ) , log_bytes !( self . node_one) , self . one_to_two, log_bytes !( self . node_two) , self . two_to_one) ?;
574
577
Ok ( ( ) )
575
578
}
576
579
}
@@ -725,7 +728,7 @@ impl fmt::Display for NetworkGraph {
725
728
}
726
729
writeln ! ( f, "[Nodes]" ) ?;
727
730
for ( key, val) in self . nodes . read ( ) . unwrap ( ) . iter ( ) {
728
- writeln ! ( f, " {}: {}" , log_pubkey! ( key) , val) ?;
731
+ writeln ! ( f, " {}: {}" , log_bytes! ( * key) , val) ?;
729
732
}
730
733
Ok ( ( ) )
731
734
}
@@ -780,7 +783,7 @@ impl NetworkGraph {
780
783
}
781
784
782
785
fn update_node_from_announcement_intern ( & self , msg : & msgs:: UnsignedNodeAnnouncement , full_msg : Option < & msgs:: NodeAnnouncement > ) -> Result < ( ) , LightningError > {
783
- match self . nodes . write ( ) . unwrap ( ) . get_mut ( & msg. node_id ) {
786
+ match self . nodes . write ( ) . unwrap ( ) . get_mut ( & msg. node_id . serialize ( ) ) {
784
787
None => Err ( LightningError { err : "No existing channels for node_announcement" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ,
785
788
Some ( node) => {
786
789
if let Some ( node_info) = node. announcement_info . as_ref ( ) {
@@ -886,9 +889,9 @@ impl NetworkGraph {
886
889
887
890
let chan_info = ChannelInfo {
888
891
features : msg. features . clone ( ) ,
889
- node_one : msg. node_id_1 . clone ( ) ,
892
+ node_one : msg. node_id_1 . serialize ( ) ,
890
893
one_to_two : None ,
891
- node_two : msg. node_id_2 . clone ( ) ,
894
+ node_two : msg. node_id_2 . serialize ( ) ,
892
895
two_to_one : None ,
893
896
capacity_sats : utxo_value,
894
897
announcement_message : if msg. excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY
@@ -939,8 +942,8 @@ impl NetworkGraph {
939
942
} ;
940
943
}
941
944
942
- add_channel_to_node ! ( msg. node_id_1) ;
943
- add_channel_to_node ! ( msg. node_id_2) ;
945
+ add_channel_to_node ! ( msg. node_id_1. serialize ( ) ) ;
946
+ add_channel_to_node ! ( msg. node_id_2. serialize ( ) ) ;
944
947
945
948
Ok ( ( ) )
946
949
}
@@ -969,7 +972,7 @@ impl NetworkGraph {
969
972
}
970
973
971
974
/// Marks a node in the graph as failed.
972
- pub fn fail_node ( & self , _node_id : & PublicKey , is_permanent : bool ) {
975
+ pub fn fail_node ( & self , _node_id : & NodeId , is_permanent : bool ) {
973
976
if is_permanent {
974
977
// TODO: Wholly remove the node
975
978
} else {
@@ -1050,13 +1053,19 @@ impl NetworkGraph {
1050
1053
if msg. flags & 1 == 1 {
1051
1054
dest_node_id = channel. node_one . clone ( ) ;
1052
1055
if let Some ( ( sig, ctx) ) = sig_info {
1053
- secp_verify_sig ! ( ctx, & msg_hash, & sig, & channel. node_two) ;
1056
+ secp_verify_sig ! ( ctx, & msg_hash, & sig, & PublicKey :: from_slice( & channel. node_two) . map_err( |_| LightningError {
1057
+ err: "Couldn't parse source node pubkey" . to_owned( ) ,
1058
+ action: ErrorAction :: IgnoreAndLog ( Level :: Debug )
1059
+ } ) ?) ;
1054
1060
}
1055
1061
maybe_update_channel_info ! ( channel. two_to_one, channel. node_two) ;
1056
1062
} else {
1057
1063
dest_node_id = channel. node_two . clone ( ) ;
1058
1064
if let Some ( ( sig, ctx) ) = sig_info {
1059
- secp_verify_sig ! ( ctx, & msg_hash, & sig, & channel. node_one) ;
1065
+ secp_verify_sig ! ( ctx, & msg_hash, & sig, & PublicKey :: from_slice( & channel. node_one) . map_err( |_| LightningError {
1066
+ err: "Couldn't parse destination node pubkey" . to_owned( ) ,
1067
+ action: ErrorAction :: IgnoreAndLog ( Level :: Debug )
1068
+ } ) ?) ;
1060
1069
}
1061
1070
maybe_update_channel_info ! ( channel. one_to_two, channel. node_one) ;
1062
1071
}
@@ -1104,7 +1113,7 @@ impl NetworkGraph {
1104
1113
Ok ( ( ) )
1105
1114
}
1106
1115
1107
- fn remove_channel_in_nodes ( nodes : & mut BTreeMap < PublicKey , NodeInfo > , chan : & ChannelInfo , short_channel_id : u64 ) {
1116
+ fn remove_channel_in_nodes ( nodes : & mut BTreeMap < NodeId , NodeInfo > , chan : & ChannelInfo , short_channel_id : u64 ) {
1108
1117
macro_rules! remove_from_node {
1109
1118
( $node_id: expr) => {
1110
1119
if let BtreeEntry :: Occupied ( mut entry) = nodes. entry( $node_id) {
@@ -1136,7 +1145,7 @@ impl ReadOnlyNetworkGraph<'_> {
1136
1145
/// Returns all known nodes' public keys along with announced node info.
1137
1146
///
1138
1147
/// (C-not exported) because we have no mapping for `BTreeMap`s
1139
- pub fn nodes ( & self ) -> & BTreeMap < PublicKey , NodeInfo > {
1148
+ pub fn nodes ( & self ) -> & BTreeMap < NodeId , NodeInfo > {
1140
1149
& * self . nodes
1141
1150
}
1142
1151
@@ -1146,7 +1155,7 @@ impl ReadOnlyNetworkGraph<'_> {
1146
1155
///
1147
1156
/// (C-not exported) as there is no practical way to track lifetimes of returned values.
1148
1157
pub fn get_addresses ( & self , pubkey : & PublicKey ) -> Option < & Vec < NetAddress > > {
1149
- if let Some ( node) = self . nodes . get ( pubkey) {
1158
+ if let Some ( node) = self . nodes . get ( & pubkey. serialize ( ) ) {
1150
1159
if let Some ( node_info) = node. announcement_info . as_ref ( ) {
1151
1160
return Some ( & node_info. addresses )
1152
1161
}
0 commit comments