@@ -50,12 +50,63 @@ 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
+ #[ derive( Clone , Copy ) ]
55
+ pub struct NodeId ( pub [ u8 ; 33 ] ) ;
56
+
57
+ impl fmt:: Debug for NodeId {
58
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
59
+ write ! ( f, "NodeId({})" , log_bytes!( self . 0 ) )
60
+ }
61
+ }
62
+
63
+ impl core:: hash:: Hash for NodeId {
64
+ fn hash < H : core:: hash:: Hasher > ( & self , hasher : & mut H ) {
65
+ self . 0 . hash ( hasher) ;
66
+ }
67
+ }
68
+
69
+ impl Eq for NodeId { }
70
+
71
+ impl PartialEq for NodeId {
72
+ fn eq ( & self , other : & NodeId ) -> bool {
73
+ self . 0 [ ..] == other. 0 [ ..]
74
+ }
75
+ }
76
+
77
+ impl cmp:: PartialOrd for NodeId {
78
+ fn partial_cmp ( & self , other : & Self ) -> Option < cmp:: Ordering > {
79
+ Some ( self . cmp ( other) )
80
+ }
81
+ }
82
+
83
+ impl Ord for NodeId {
84
+ fn cmp ( & self , other : & NodeId ) -> cmp:: Ordering {
85
+ self . 0 [ ..] . cmp ( & other. 0 [ ..] )
86
+ }
87
+ }
88
+
89
+ impl Writeable for NodeId {
90
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
91
+ writer. write_all ( & self . 0 ) ?;
92
+ Ok ( ( ) )
93
+ }
94
+ }
95
+
96
+ impl Readable for NodeId {
97
+ fn read < R : io:: Read > ( reader : & mut R ) -> Result < NodeId , DecodeError > {
98
+ let mut buf = [ 0 ; 33 ] ;
99
+ reader. read_exact ( & mut buf) ?;
100
+ Ok ( NodeId ( buf) )
101
+ }
102
+ }
103
+
53
104
/// Represents the network as nodes and channels between them
54
105
pub struct NetworkGraph {
55
106
genesis_hash : BlockHash ,
56
107
// Lock order: channels -> nodes
57
108
channels : RwLock < BTreeMap < u64 , ChannelInfo > > ,
58
- nodes : RwLock < BTreeMap < PublicKey , NodeInfo > > ,
109
+ nodes : RwLock < BTreeMap < NodeId , NodeInfo > > ,
59
110
}
60
111
61
112
impl Clone for NetworkGraph {
@@ -73,7 +124,7 @@ impl Clone for NetworkGraph {
73
124
/// A read-only view of [`NetworkGraph`].
74
125
pub struct ReadOnlyNetworkGraph < ' a > {
75
126
channels : RwLockReadGuard < ' a , BTreeMap < u64 , ChannelInfo > > ,
76
- nodes : RwLockReadGuard < ' a , BTreeMap < PublicKey , NodeInfo > > ,
127
+ nodes : RwLockReadGuard < ' a , BTreeMap < NodeId , NodeInfo > > ,
77
128
}
78
129
79
130
/// Update to the [`NetworkGraph`] based on payment failure information conveyed via the Onion
@@ -209,7 +260,7 @@ where C::Target: chain::Access, L::Target: Logger
209
260
NetworkUpdate :: NodeFailure { ref node_id, is_permanent } => {
210
261
let action = if is_permanent { "Removing" } else { "Disabling" } ;
211
262
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) ;
263
+ self . network_graph . fail_node ( & NodeId ( node_id. serialize ( ) ) , is_permanent) ;
213
264
} ,
214
265
}
215
266
}
@@ -277,11 +328,11 @@ where C::Target: chain::Access, L::Target: Logger
277
328
let mut result = Vec :: with_capacity ( batch_amount as usize ) ;
278
329
let nodes = self . network_graph . nodes . read ( ) . unwrap ( ) ;
279
330
let mut iter = if let Some ( pubkey) = starting_point {
280
- let mut iter = nodes. range ( ( * pubkey) ..) ;
331
+ let mut iter = nodes. range ( NodeId ( pubkey. serialize ( ) ) ..) ;
281
332
iter. next ( ) ;
282
333
iter
283
334
} else {
284
- nodes. range ( ..)
335
+ nodes. range :: < NodeId , _ > ( ..)
285
336
} ;
286
337
while result. len ( ) < batch_amount as usize {
287
338
if let Some ( ( _, ref node) ) = iter. next ( ) {
@@ -314,7 +365,7 @@ where C::Target: chain::Access, L::Target: Logger
314
365
}
315
366
316
367
// Check if we need to perform a full synchronization with this peer
317
- if !self . should_request_full_sync ( their_node_id) {
368
+ if !self . should_request_full_sync ( & their_node_id) {
318
369
return ( ) ;
319
370
}
320
371
@@ -551,11 +602,11 @@ pub struct ChannelInfo {
551
602
/// Protocol features of a channel communicated during its announcement
552
603
pub features : ChannelFeatures ,
553
604
/// Source node of the first direction of a channel
554
- pub node_one : PublicKey ,
605
+ pub node_one : NodeId ,
555
606
/// Details about the first direction of a channel
556
607
pub one_to_two : Option < DirectionalChannelInfo > ,
557
608
/// Source node of the second direction of a channel
558
- pub node_two : PublicKey ,
609
+ pub node_two : NodeId ,
559
610
/// Details about the second direction of a channel
560
611
pub two_to_one : Option < DirectionalChannelInfo > ,
561
612
/// The channel capacity as seen on-chain, if chain lookup is available.
@@ -570,7 +621,7 @@ pub struct ChannelInfo {
570
621
impl fmt:: Display for ChannelInfo {
571
622
fn fmt ( & self , f : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
572
623
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) ?;
624
+ log_bytes!( self . features. encode( ) ) , log_bytes !( self . node_one. 0 ) , self . one_to_two, log_bytes !( self . node_two. 0 ) , self . two_to_one) ?;
574
625
Ok ( ( ) )
575
626
}
576
627
}
@@ -724,8 +775,8 @@ impl fmt::Display for NetworkGraph {
724
775
writeln ! ( f, " {}: {}" , key, val) ?;
725
776
}
726
777
writeln ! ( f, "[Nodes]" ) ?;
727
- for ( key , val) in self . nodes . read ( ) . unwrap ( ) . iter ( ) {
728
- writeln ! ( f, " {}: {}" , log_pubkey! ( key ) , val) ?;
778
+ for ( & node_id , val) in self . nodes . read ( ) . unwrap ( ) . iter ( ) {
779
+ writeln ! ( f, " {}: {}" , log_bytes! ( node_id . 0 ) , val) ?;
729
780
}
730
781
Ok ( ( ) )
731
782
}
@@ -780,7 +831,7 @@ impl NetworkGraph {
780
831
}
781
832
782
833
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 ) {
834
+ match self . nodes . write ( ) . unwrap ( ) . get_mut ( & NodeId ( msg. node_id . serialize ( ) ) ) {
784
835
None => Err ( LightningError { err : "No existing channels for node_announcement" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ,
785
836
Some ( node) => {
786
837
if let Some ( node_info) = node. announcement_info . as_ref ( ) {
@@ -886,9 +937,9 @@ impl NetworkGraph {
886
937
887
938
let chan_info = ChannelInfo {
888
939
features : msg. features . clone ( ) ,
889
- node_one : msg. node_id_1 . clone ( ) ,
940
+ node_one : NodeId ( msg. node_id_1 . serialize ( ) ) ,
890
941
one_to_two : None ,
891
- node_two : msg. node_id_2 . clone ( ) ,
942
+ node_two : NodeId ( msg. node_id_2 . serialize ( ) ) ,
892
943
two_to_one : None ,
893
944
capacity_sats : utxo_value,
894
945
announcement_message : if msg. excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY
@@ -939,8 +990,8 @@ impl NetworkGraph {
939
990
} ;
940
991
}
941
992
942
- add_channel_to_node ! ( msg. node_id_1) ;
943
- add_channel_to_node ! ( msg. node_id_2) ;
993
+ add_channel_to_node ! ( NodeId ( msg. node_id_1. serialize ( ) ) ) ;
994
+ add_channel_to_node ! ( NodeId ( msg. node_id_2. serialize ( ) ) ) ;
944
995
945
996
Ok ( ( ) )
946
997
}
@@ -969,7 +1020,7 @@ impl NetworkGraph {
969
1020
}
970
1021
971
1022
/// Marks a node in the graph as failed.
972
- pub fn fail_node ( & self , _node_id : & PublicKey , is_permanent : bool ) {
1023
+ pub fn fail_node ( & self , _node_id : & NodeId , is_permanent : bool ) {
973
1024
if is_permanent {
974
1025
// TODO: Wholly remove the node
975
1026
} else {
@@ -1050,13 +1101,19 @@ impl NetworkGraph {
1050
1101
if msg. flags & 1 == 1 {
1051
1102
dest_node_id = channel. node_one . clone ( ) ;
1052
1103
if let Some ( ( sig, ctx) ) = sig_info {
1053
- secp_verify_sig ! ( ctx, & msg_hash, & sig, & channel. node_two) ;
1104
+ secp_verify_sig ! ( ctx, & msg_hash, & sig, & PublicKey :: from_slice( & channel. node_two. 0 ) . map_err( |_| LightningError {
1105
+ err: "Couldn't parse source node pubkey" . to_owned( ) ,
1106
+ action: ErrorAction :: IgnoreAndLog ( Level :: Debug )
1107
+ } ) ?) ;
1054
1108
}
1055
1109
maybe_update_channel_info ! ( channel. two_to_one, channel. node_two) ;
1056
1110
} else {
1057
1111
dest_node_id = channel. node_two . clone ( ) ;
1058
1112
if let Some ( ( sig, ctx) ) = sig_info {
1059
- secp_verify_sig ! ( ctx, & msg_hash, & sig, & channel. node_one) ;
1113
+ secp_verify_sig ! ( ctx, & msg_hash, & sig, & PublicKey :: from_slice( & channel. node_one. 0 ) . map_err( |_| LightningError {
1114
+ err: "Couldn't parse destination node pubkey" . to_owned( ) ,
1115
+ action: ErrorAction :: IgnoreAndLog ( Level :: Debug )
1116
+ } ) ?) ;
1060
1117
}
1061
1118
maybe_update_channel_info ! ( channel. one_to_two, channel. node_one) ;
1062
1119
}
@@ -1104,7 +1161,7 @@ impl NetworkGraph {
1104
1161
Ok ( ( ) )
1105
1162
}
1106
1163
1107
- fn remove_channel_in_nodes ( nodes : & mut BTreeMap < PublicKey , NodeInfo > , chan : & ChannelInfo , short_channel_id : u64 ) {
1164
+ fn remove_channel_in_nodes ( nodes : & mut BTreeMap < NodeId , NodeInfo > , chan : & ChannelInfo , short_channel_id : u64 ) {
1108
1165
macro_rules! remove_from_node {
1109
1166
( $node_id: expr) => {
1110
1167
if let BtreeEntry :: Occupied ( mut entry) = nodes. entry( $node_id) {
@@ -1136,7 +1193,7 @@ impl ReadOnlyNetworkGraph<'_> {
1136
1193
/// Returns all known nodes' public keys along with announced node info.
1137
1194
///
1138
1195
/// (C-not exported) because we have no mapping for `BTreeMap`s
1139
- pub fn nodes ( & self ) -> & BTreeMap < PublicKey , NodeInfo > {
1196
+ pub fn nodes ( & self ) -> & BTreeMap < NodeId , NodeInfo > {
1140
1197
& * self . nodes
1141
1198
}
1142
1199
@@ -1146,7 +1203,7 @@ impl ReadOnlyNetworkGraph<'_> {
1146
1203
///
1147
1204
/// (C-not exported) as there is no practical way to track lifetimes of returned values.
1148
1205
pub fn get_addresses ( & self , pubkey : & PublicKey ) -> Option < & Vec < NetAddress > > {
1149
- if let Some ( node) = self . nodes . get ( pubkey) {
1206
+ if let Some ( node) = self . nodes . get ( & NodeId ( pubkey. serialize ( ) ) ) {
1150
1207
if let Some ( node_info) = node. announcement_info . as_ref ( ) {
1151
1208
return Some ( & node_info. addresses )
1152
1209
}
0 commit comments