Skip to content

Commit 7e86255

Browse files
committed
Avoid parsing PublicKeys when applying an unsigned chan update
`PublicKey` parsing is relatively expensive as we have to check if the point is actually on the curve. To avoid it, our `NetworkGraph` uses `NodeId`s which don't have the validity requirement. Sadly, we were always parsing the broadcasting node's `PublicKey` from the `node_id` in the network graph whenever we see an update for that channel, whether we have a corresponding signature or not. Here we fix this, only parsing the public key (and hashing the message) if we're going to check a signature.
1 parent 1434e9c commit 7e86255

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

lightning/src/routing/gossip.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,7 +2537,7 @@ where
25372537
}
25382538
};
25392539

2540-
let node_pubkey;
2540+
let mut node_pubkey = None;
25412541
{
25422542
let channels = self.channels.read().unwrap();
25432543
match channels.get(&msg.short_channel_id) {
@@ -2556,17 +2556,26 @@ where
25562556
} else {
25572557
channel.node_one.as_slice()
25582558
};
2559-
node_pubkey = PublicKey::from_slice(node_id).map_err(|_| LightningError {
2560-
err: "Couldn't parse source node pubkey".to_owned(),
2561-
action: ErrorAction::IgnoreAndLog(Level::Debug),
2562-
})?;
2559+
if sig.is_some() {
2560+
node_pubkey = Some(PublicKey::from_slice(node_id).map_err(|_| LightningError {
2561+
err: "Couldn't parse source node pubkey".to_owned(),
2562+
action: ErrorAction::IgnoreAndLog(Level::Debug),
2563+
})?);
2564+
}
25632565
},
25642566
}
25652567
}
25662568

2567-
let msg_hash = hash_to_message!(&message_sha256d_hash(&msg)[..]);
25682569
if let Some(sig) = sig {
2569-
secp_verify_sig!(self.secp_ctx, &msg_hash, &sig, &node_pubkey, "channel_update");
2570+
let msg_hash = hash_to_message!(&message_sha256d_hash(&msg)[..]);
2571+
if node_pubkey.is_none() {
2572+
debug_assert!(false);
2573+
let err = "Reached unreachable code".to_owned();
2574+
return Err(LightningError {
2575+
err, action: ErrorAction::IgnoreAndLog(Level::Error),
2576+
});
2577+
}
2578+
secp_verify_sig!(self.secp_ctx, &msg_hash, &sig, &node_pubkey.unwrap(), "channel_update");
25702579
}
25712580

25722581
if only_verify {

0 commit comments

Comments
 (0)