9
9
10
10
//! The top-level network map tracking logic lives here.
11
11
12
+ use bitcoin:: secp256k1:: constants:: PUBLIC_KEY_SIZE ;
12
13
use bitcoin:: secp256k1:: key:: PublicKey ;
13
14
use bitcoin:: secp256k1:: Secp256k1 ;
14
15
use bitcoin:: secp256k1;
@@ -50,12 +51,73 @@ const MAX_EXCESS_BYTES_FOR_RELAY: usize = 1024;
50
51
/// This value ensures a reply fits within the 65k payload limit and is consistent with other implementations.
51
52
const MAX_SCIDS_PER_REPLY : usize = 8000 ;
52
53
54
+ /// Represents the compressed public key of a node
55
+ #[ derive( Clone , Copy ) ]
56
+ pub struct NodeId ( [ u8 ; PUBLIC_KEY_SIZE ] ) ;
57
+
58
+ impl NodeId {
59
+ pub fn from_pubkey ( pubkey : & PublicKey ) -> Self {
60
+ NodeId ( pubkey. serialize ( ) )
61
+ }
62
+
63
+ pub fn as_slice ( & self ) -> & [ u8 ] {
64
+ & self . 0
65
+ }
66
+ }
67
+
68
+ impl fmt:: Debug for NodeId {
69
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
70
+ write ! ( f, "NodeId({})" , log_bytes!( self . 0 ) )
71
+ }
72
+ }
73
+
74
+ impl core:: hash:: Hash for NodeId {
75
+ fn hash < H : core:: hash:: Hasher > ( & self , hasher : & mut H ) {
76
+ self . 0 . hash ( hasher) ;
77
+ }
78
+ }
79
+
80
+ impl Eq for NodeId { }
81
+
82
+ impl PartialEq for NodeId {
83
+ fn eq ( & self , other : & Self ) -> bool {
84
+ self . 0 [ ..] == other. 0 [ ..]
85
+ }
86
+ }
87
+
88
+ impl cmp:: PartialOrd for NodeId {
89
+ fn partial_cmp ( & self , other : & Self ) -> Option < cmp:: Ordering > {
90
+ Some ( self . cmp ( other) )
91
+ }
92
+ }
93
+
94
+ impl Ord for NodeId {
95
+ fn cmp ( & self , other : & Self ) -> cmp:: Ordering {
96
+ self . 0 [ ..] . cmp ( & other. 0 [ ..] )
97
+ }
98
+ }
99
+
100
+ impl Writeable for NodeId {
101
+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
102
+ writer. write_all ( & self . 0 ) ?;
103
+ Ok ( ( ) )
104
+ }
105
+ }
106
+
107
+ impl Readable for NodeId {
108
+ fn read < R : io:: Read > ( reader : & mut R ) -> Result < Self , DecodeError > {
109
+ let mut buf = [ 0 ; PUBLIC_KEY_SIZE ] ;
110
+ reader. read_exact ( & mut buf) ?;
111
+ Ok ( Self ( buf) )
112
+ }
113
+ }
114
+
53
115
/// Represents the network as nodes and channels between them
54
116
pub struct NetworkGraph {
55
117
genesis_hash : BlockHash ,
56
118
// Lock order: channels -> nodes
57
119
channels : RwLock < BTreeMap < u64 , ChannelInfo > > ,
58
- nodes : RwLock < BTreeMap < PublicKey , NodeInfo > > ,
120
+ nodes : RwLock < BTreeMap < NodeId , NodeInfo > > ,
59
121
}
60
122
61
123
impl Clone for NetworkGraph {
@@ -73,7 +135,7 @@ impl Clone for NetworkGraph {
73
135
/// A read-only view of [`NetworkGraph`].
74
136
pub struct ReadOnlyNetworkGraph < ' a > {
75
137
channels : RwLockReadGuard < ' a , BTreeMap < u64 , ChannelInfo > > ,
76
- nodes : RwLockReadGuard < ' a , BTreeMap < PublicKey , NodeInfo > > ,
138
+ nodes : RwLockReadGuard < ' a , BTreeMap < NodeId , NodeInfo > > ,
77
139
}
78
140
79
141
/// Update to the [`NetworkGraph`] based on payment failure information conveyed via the Onion
@@ -277,11 +339,11 @@ where C::Target: chain::Access, L::Target: Logger
277
339
let mut result = Vec :: with_capacity ( batch_amount as usize ) ;
278
340
let nodes = self . network_graph . nodes . read ( ) . unwrap ( ) ;
279
341
let mut iter = if let Some ( pubkey) = starting_point {
280
- let mut iter = nodes. range ( ( * pubkey) ..) ;
342
+ let mut iter = nodes. range ( NodeId :: from_pubkey ( pubkey) ..) ;
281
343
iter. next ( ) ;
282
344
iter
283
345
} else {
284
- nodes. range ( ..)
346
+ nodes. range :: < NodeId , _ > ( ..)
285
347
} ;
286
348
while result. len ( ) < batch_amount as usize {
287
349
if let Some ( ( _, ref node) ) = iter. next ( ) {
@@ -314,7 +376,7 @@ where C::Target: chain::Access, L::Target: Logger
314
376
}
315
377
316
378
// Check if we need to perform a full synchronization with this peer
317
- if !self . should_request_full_sync ( their_node_id) {
379
+ if !self . should_request_full_sync ( & their_node_id) {
318
380
return ( ) ;
319
381
}
320
382
@@ -551,11 +613,11 @@ pub struct ChannelInfo {
551
613
/// Protocol features of a channel communicated during its announcement
552
614
pub features : ChannelFeatures ,
553
615
/// Source node of the first direction of a channel
554
- pub node_one : PublicKey ,
616
+ pub node_one : NodeId ,
555
617
/// Details about the first direction of a channel
556
618
pub one_to_two : Option < DirectionalChannelInfo > ,
557
619
/// Source node of the second direction of a channel
558
- pub node_two : PublicKey ,
620
+ pub node_two : NodeId ,
559
621
/// Details about the second direction of a channel
560
622
pub two_to_one : Option < DirectionalChannelInfo > ,
561
623
/// The channel capacity as seen on-chain, if chain lookup is available.
@@ -570,7 +632,7 @@ pub struct ChannelInfo {
570
632
impl fmt:: Display for ChannelInfo {
571
633
fn fmt ( & self , f : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
572
634
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) ?;
635
+ log_bytes!( self . features. encode( ) ) , log_bytes !( self . node_one. as_slice ( ) ) , self . one_to_two, log_bytes !( self . node_two. as_slice ( ) ) , self . two_to_one) ?;
574
636
Ok ( ( ) )
575
637
}
576
638
}
@@ -724,8 +786,8 @@ impl fmt::Display for NetworkGraph {
724
786
writeln ! ( f, " {}: {}" , key, val) ?;
725
787
}
726
788
writeln ! ( f, "[Nodes]" ) ?;
727
- for ( key , val) in self . nodes . read ( ) . unwrap ( ) . iter ( ) {
728
- writeln ! ( f, " {}: {}" , log_pubkey! ( key ) , val) ?;
789
+ for ( & node_id , val) in self . nodes . read ( ) . unwrap ( ) . iter ( ) {
790
+ writeln ! ( f, " {}: {}" , log_bytes! ( node_id . as_slice ( ) ) , val) ?;
729
791
}
730
792
Ok ( ( ) )
731
793
}
@@ -780,7 +842,7 @@ impl NetworkGraph {
780
842
}
781
843
782
844
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 ) {
845
+ match self . nodes . write ( ) . unwrap ( ) . get_mut ( & NodeId :: from_pubkey ( & msg. node_id ) ) {
784
846
None => Err ( LightningError { err : "No existing channels for node_announcement" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ,
785
847
Some ( node) => {
786
848
if let Some ( node_info) = node. announcement_info . as_ref ( ) {
@@ -886,9 +948,9 @@ impl NetworkGraph {
886
948
887
949
let chan_info = ChannelInfo {
888
950
features : msg. features . clone ( ) ,
889
- node_one : msg. node_id_1 . clone ( ) ,
951
+ node_one : NodeId :: from_pubkey ( & msg. node_id_1 ) ,
890
952
one_to_two : None ,
891
- node_two : msg. node_id_2 . clone ( ) ,
953
+ node_two : NodeId :: from_pubkey ( & msg. node_id_2 ) ,
892
954
two_to_one : None ,
893
955
capacity_sats : utxo_value,
894
956
announcement_message : if msg. excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY
@@ -939,8 +1001,8 @@ impl NetworkGraph {
939
1001
} ;
940
1002
}
941
1003
942
- add_channel_to_node ! ( msg. node_id_1) ;
943
- add_channel_to_node ! ( msg. node_id_2) ;
1004
+ add_channel_to_node ! ( NodeId :: from_pubkey ( & msg. node_id_1) ) ;
1005
+ add_channel_to_node ! ( NodeId :: from_pubkey ( & msg. node_id_2) ) ;
944
1006
945
1007
Ok ( ( ) )
946
1008
}
@@ -1050,13 +1112,19 @@ impl NetworkGraph {
1050
1112
if msg. flags & 1 == 1 {
1051
1113
dest_node_id = channel. node_one . clone ( ) ;
1052
1114
if let Some ( ( sig, ctx) ) = sig_info {
1053
- secp_verify_sig ! ( ctx, & msg_hash, & sig, & channel. node_two) ;
1115
+ secp_verify_sig ! ( ctx, & msg_hash, & sig, & PublicKey :: from_slice( channel. node_two. as_slice( ) ) . map_err( |_| LightningError {
1116
+ err: "Couldn't parse source node pubkey" . to_owned( ) ,
1117
+ action: ErrorAction :: IgnoreAndLog ( Level :: Debug )
1118
+ } ) ?) ;
1054
1119
}
1055
1120
maybe_update_channel_info ! ( channel. two_to_one, channel. node_two) ;
1056
1121
} else {
1057
1122
dest_node_id = channel. node_two . clone ( ) ;
1058
1123
if let Some ( ( sig, ctx) ) = sig_info {
1059
- secp_verify_sig ! ( ctx, & msg_hash, & sig, & channel. node_one) ;
1124
+ secp_verify_sig ! ( ctx, & msg_hash, & sig, & PublicKey :: from_slice( channel. node_one. as_slice( ) ) . map_err( |_| LightningError {
1125
+ err: "Couldn't parse destination node pubkey" . to_owned( ) ,
1126
+ action: ErrorAction :: IgnoreAndLog ( Level :: Debug )
1127
+ } ) ?) ;
1060
1128
}
1061
1129
maybe_update_channel_info ! ( channel. one_to_two, channel. node_one) ;
1062
1130
}
@@ -1104,7 +1172,7 @@ impl NetworkGraph {
1104
1172
Ok ( ( ) )
1105
1173
}
1106
1174
1107
- fn remove_channel_in_nodes ( nodes : & mut BTreeMap < PublicKey , NodeInfo > , chan : & ChannelInfo , short_channel_id : u64 ) {
1175
+ fn remove_channel_in_nodes ( nodes : & mut BTreeMap < NodeId , NodeInfo > , chan : & ChannelInfo , short_channel_id : u64 ) {
1108
1176
macro_rules! remove_from_node {
1109
1177
( $node_id: expr) => {
1110
1178
if let BtreeEntry :: Occupied ( mut entry) = nodes. entry( $node_id) {
@@ -1136,7 +1204,7 @@ impl ReadOnlyNetworkGraph<'_> {
1136
1204
/// Returns all known nodes' public keys along with announced node info.
1137
1205
///
1138
1206
/// (C-not exported) because we have no mapping for `BTreeMap`s
1139
- pub fn nodes ( & self ) -> & BTreeMap < PublicKey , NodeInfo > {
1207
+ pub fn nodes ( & self ) -> & BTreeMap < NodeId , NodeInfo > {
1140
1208
& * self . nodes
1141
1209
}
1142
1210
@@ -1146,7 +1214,7 @@ impl ReadOnlyNetworkGraph<'_> {
1146
1214
///
1147
1215
/// (C-not exported) as there is no practical way to track lifetimes of returned values.
1148
1216
pub fn get_addresses ( & self , pubkey : & PublicKey ) -> Option < & Vec < NetAddress > > {
1149
- if let Some ( node) = self . nodes . get ( pubkey) {
1217
+ if let Some ( node) = self . nodes . get ( & NodeId :: from_pubkey ( & pubkey) ) {
1150
1218
if let Some ( node_info) = node. announcement_info . as_ref ( ) {
1151
1219
return Some ( & node_info. addresses )
1152
1220
}
0 commit comments