Skip to content

Commit 843d25d

Browse files
authored
Merge pull request #1107 from dunxen/2021-10-swap-pubkey-for-bytearray
Replace PublicKey with [u8; 33] in NetworkGraph
2 parents e635db0 + fce631c commit 843d25d

File tree

2 files changed

+177
-103
lines changed

2 files changed

+177
-103
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 90 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
//! The top-level network map tracking logic lives here.
1111
12+
use bitcoin::secp256k1::constants::PUBLIC_KEY_SIZE;
1213
use bitcoin::secp256k1::key::PublicKey;
1314
use bitcoin::secp256k1::Secp256k1;
1415
use bitcoin::secp256k1;
@@ -50,12 +51,75 @@ const MAX_EXCESS_BYTES_FOR_RELAY: usize = 1024;
5051
/// This value ensures a reply fits within the 65k payload limit and is consistent with other implementations.
5152
const MAX_SCIDS_PER_REPLY: usize = 8000;
5253

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+
/// Create a new NodeId from a public key
60+
pub fn from_pubkey(pubkey: &PublicKey) -> Self {
61+
NodeId(pubkey.serialize())
62+
}
63+
64+
/// Get the public key slice from this NodeId
65+
pub fn as_slice(&self) -> &[u8] {
66+
&self.0
67+
}
68+
}
69+
70+
impl fmt::Debug for NodeId {
71+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72+
write!(f, "NodeId({})", log_bytes!(self.0))
73+
}
74+
}
75+
76+
impl core::hash::Hash for NodeId {
77+
fn hash<H: core::hash::Hasher>(&self, hasher: &mut H) {
78+
self.0.hash(hasher);
79+
}
80+
}
81+
82+
impl Eq for NodeId {}
83+
84+
impl PartialEq for NodeId {
85+
fn eq(&self, other: &Self) -> bool {
86+
self.0[..] == other.0[..]
87+
}
88+
}
89+
90+
impl cmp::PartialOrd for NodeId {
91+
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
92+
Some(self.cmp(other))
93+
}
94+
}
95+
96+
impl Ord for NodeId {
97+
fn cmp(&self, other: &Self) -> cmp::Ordering {
98+
self.0[..].cmp(&other.0[..])
99+
}
100+
}
101+
102+
impl Writeable for NodeId {
103+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
104+
writer.write_all(&self.0)?;
105+
Ok(())
106+
}
107+
}
108+
109+
impl Readable for NodeId {
110+
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
111+
let mut buf = [0; PUBLIC_KEY_SIZE];
112+
reader.read_exact(&mut buf)?;
113+
Ok(Self(buf))
114+
}
115+
}
116+
53117
/// Represents the network as nodes and channels between them
54118
pub struct NetworkGraph {
55119
genesis_hash: BlockHash,
56120
// Lock order: channels -> nodes
57121
channels: RwLock<BTreeMap<u64, ChannelInfo>>,
58-
nodes: RwLock<BTreeMap<PublicKey, NodeInfo>>,
122+
nodes: RwLock<BTreeMap<NodeId, NodeInfo>>,
59123
}
60124

61125
impl Clone for NetworkGraph {
@@ -73,7 +137,7 @@ impl Clone for NetworkGraph {
73137
/// A read-only view of [`NetworkGraph`].
74138
pub struct ReadOnlyNetworkGraph<'a> {
75139
channels: RwLockReadGuard<'a, BTreeMap<u64, ChannelInfo>>,
76-
nodes: RwLockReadGuard<'a, BTreeMap<PublicKey, NodeInfo>>,
140+
nodes: RwLockReadGuard<'a, BTreeMap<NodeId, NodeInfo>>,
77141
}
78142

79143
/// Update to the [`NetworkGraph`] based on payment failure information conveyed via the Onion
@@ -277,11 +341,11 @@ where C::Target: chain::Access, L::Target: Logger
277341
let mut result = Vec::with_capacity(batch_amount as usize);
278342
let nodes = self.network_graph.nodes.read().unwrap();
279343
let mut iter = if let Some(pubkey) = starting_point {
280-
let mut iter = nodes.range((*pubkey)..);
344+
let mut iter = nodes.range(NodeId::from_pubkey(pubkey)..);
281345
iter.next();
282346
iter
283347
} else {
284-
nodes.range(..)
348+
nodes.range::<NodeId, _>(..)
285349
};
286350
while result.len() < batch_amount as usize {
287351
if let Some((_, ref node)) = iter.next() {
@@ -314,7 +378,7 @@ where C::Target: chain::Access, L::Target: Logger
314378
}
315379

316380
// Check if we need to perform a full synchronization with this peer
317-
if !self.should_request_full_sync(their_node_id) {
381+
if !self.should_request_full_sync(&their_node_id) {
318382
return ();
319383
}
320384

@@ -551,11 +615,11 @@ pub struct ChannelInfo {
551615
/// Protocol features of a channel communicated during its announcement
552616
pub features: ChannelFeatures,
553617
/// Source node of the first direction of a channel
554-
pub node_one: PublicKey,
618+
pub node_one: NodeId,
555619
/// Details about the first direction of a channel
556620
pub one_to_two: Option<DirectionalChannelInfo>,
557621
/// Source node of the second direction of a channel
558-
pub node_two: PublicKey,
622+
pub node_two: NodeId,
559623
/// Details about the second direction of a channel
560624
pub two_to_one: Option<DirectionalChannelInfo>,
561625
/// The channel capacity as seen on-chain, if chain lookup is available.
@@ -570,7 +634,7 @@ pub struct ChannelInfo {
570634
impl fmt::Display for ChannelInfo {
571635
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
572636
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)?;
637+
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)?;
574638
Ok(())
575639
}
576640
}
@@ -724,8 +788,8 @@ impl fmt::Display for NetworkGraph {
724788
writeln!(f, " {}: {}", key, val)?;
725789
}
726790
writeln!(f, "[Nodes]")?;
727-
for (key, val) in self.nodes.read().unwrap().iter() {
728-
writeln!(f, " {}: {}", log_pubkey!(key), val)?;
791+
for (&node_id, val) in self.nodes.read().unwrap().iter() {
792+
writeln!(f, " {}: {}", log_bytes!(node_id.as_slice()), val)?;
729793
}
730794
Ok(())
731795
}
@@ -780,7 +844,7 @@ impl NetworkGraph {
780844
}
781845

782846
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) {
847+
match self.nodes.write().unwrap().get_mut(&NodeId::from_pubkey(&msg.node_id)) {
784848
None => Err(LightningError{err: "No existing channels for node_announcement".to_owned(), action: ErrorAction::IgnoreError}),
785849
Some(node) => {
786850
if let Some(node_info) = node.announcement_info.as_ref() {
@@ -886,9 +950,9 @@ impl NetworkGraph {
886950

887951
let chan_info = ChannelInfo {
888952
features: msg.features.clone(),
889-
node_one: msg.node_id_1.clone(),
953+
node_one: NodeId::from_pubkey(&msg.node_id_1),
890954
one_to_two: None,
891-
node_two: msg.node_id_2.clone(),
955+
node_two: NodeId::from_pubkey(&msg.node_id_2),
892956
two_to_one: None,
893957
capacity_sats: utxo_value,
894958
announcement_message: if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
@@ -939,8 +1003,8 @@ impl NetworkGraph {
9391003
};
9401004
}
9411005

942-
add_channel_to_node!(msg.node_id_1);
943-
add_channel_to_node!(msg.node_id_2);
1006+
add_channel_to_node!(NodeId::from_pubkey(&msg.node_id_1));
1007+
add_channel_to_node!(NodeId::from_pubkey(&msg.node_id_2));
9441008

9451009
Ok(())
9461010
}
@@ -1050,13 +1114,19 @@ impl NetworkGraph {
10501114
if msg.flags & 1 == 1 {
10511115
dest_node_id = channel.node_one.clone();
10521116
if let Some((sig, ctx)) = sig_info {
1053-
secp_verify_sig!(ctx, &msg_hash, &sig, &channel.node_two);
1117+
secp_verify_sig!(ctx, &msg_hash, &sig, &PublicKey::from_slice(channel.node_two.as_slice()).map_err(|_| LightningError{
1118+
err: "Couldn't parse source node pubkey".to_owned(),
1119+
action: ErrorAction::IgnoreAndLog(Level::Debug)
1120+
})?);
10541121
}
10551122
maybe_update_channel_info!(channel.two_to_one, channel.node_two);
10561123
} else {
10571124
dest_node_id = channel.node_two.clone();
10581125
if let Some((sig, ctx)) = sig_info {
1059-
secp_verify_sig!(ctx, &msg_hash, &sig, &channel.node_one);
1126+
secp_verify_sig!(ctx, &msg_hash, &sig, &PublicKey::from_slice(channel.node_one.as_slice()).map_err(|_| LightningError{
1127+
err: "Couldn't parse destination node pubkey".to_owned(),
1128+
action: ErrorAction::IgnoreAndLog(Level::Debug)
1129+
})?);
10601130
}
10611131
maybe_update_channel_info!(channel.one_to_two, channel.node_one);
10621132
}
@@ -1104,7 +1174,7 @@ impl NetworkGraph {
11041174
Ok(())
11051175
}
11061176

1107-
fn remove_channel_in_nodes(nodes: &mut BTreeMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
1177+
fn remove_channel_in_nodes(nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
11081178
macro_rules! remove_from_node {
11091179
($node_id: expr) => {
11101180
if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
@@ -1136,7 +1206,7 @@ impl ReadOnlyNetworkGraph<'_> {
11361206
/// Returns all known nodes' public keys along with announced node info.
11371207
///
11381208
/// (C-not exported) because we have no mapping for `BTreeMap`s
1139-
pub fn nodes(&self) -> &BTreeMap<PublicKey, NodeInfo> {
1209+
pub fn nodes(&self) -> &BTreeMap<NodeId, NodeInfo> {
11401210
&*self.nodes
11411211
}
11421212

@@ -1146,7 +1216,7 @@ impl ReadOnlyNetworkGraph<'_> {
11461216
///
11471217
/// (C-not exported) as there is no practical way to track lifetimes of returned values.
11481218
pub fn get_addresses(&self, pubkey: &PublicKey) -> Option<&Vec<NetAddress>> {
1149-
if let Some(node) = self.nodes.get(pubkey) {
1219+
if let Some(node) = self.nodes.get(&NodeId::from_pubkey(&pubkey)) {
11501220
if let Some(node_info) = node.announcement_info.as_ref() {
11511221
return Some(&node_info.addresses)
11521222
}

0 commit comments

Comments
 (0)