Skip to content

Commit a8d4cfa

Browse files
committed
Avoid allocating when checking gossip message signatures
When we check gossip message signatures, there's no reason to serialize out the full gossip message before hashing, and it generates a lot of allocations during the initial startup when we fetch the full gossip from peers.
1 parent 18dc7f2 commit a8d4cfa

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

lightning/src/routing/gossip.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,17 @@ macro_rules! get_pubkey_from_node_id {
412412
}
413413
}
414414

415+
fn message_sha256d_hash<M: Writeable>(msg: &M) -> Sha256dHash {
416+
let mut engine = Sha256dHash::engine();
417+
msg.write(&mut engine).expect("In-memory structs should not fail to serialize");
418+
Sha256dHash::from_engine(engine)
419+
}
420+
415421
/// Verifies the signature of a [`NodeAnnouncement`].
416422
///
417423
/// Returns an error if it is invalid.
418424
pub fn verify_node_announcement<C: Verification>(msg: &NodeAnnouncement, secp_ctx: &Secp256k1<C>) -> Result<(), LightningError> {
419-
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
425+
let msg_hash = hash_to_message!(&message_sha256d_hash(&msg.contents)[..]);
420426
secp_verify_sig!(secp_ctx, &msg_hash, &msg.signature, &get_pubkey_from_node_id!(msg.contents.node_id, "node_announcement"), "node_announcement");
421427

422428
Ok(())
@@ -426,7 +432,7 @@ pub fn verify_node_announcement<C: Verification>(msg: &NodeAnnouncement, secp_ct
426432
///
427433
/// Returns an error if one of the signatures is invalid.
428434
pub fn verify_channel_announcement<C: Verification>(msg: &ChannelAnnouncement, secp_ctx: &Secp256k1<C>) -> Result<(), LightningError> {
429-
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
435+
let msg_hash = hash_to_message!(&message_sha256d_hash(&msg.contents)[..]);
430436
secp_verify_sig!(secp_ctx, &msg_hash, &msg.node_signature_1, &get_pubkey_from_node_id!(msg.contents.node_id_1, "channel_announcement"), "channel_announcement");
431437
secp_verify_sig!(secp_ctx, &msg_hash, &msg.node_signature_2, &get_pubkey_from_node_id!(msg.contents.node_id_2, "channel_announcement"), "channel_announcement");
432438
secp_verify_sig!(secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &get_pubkey_from_node_id!(msg.contents.bitcoin_key_1, "channel_announcement"), "channel_announcement");
@@ -1969,7 +1975,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
19691975
} }
19701976
}
19711977

1972-
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);
1978+
let msg_hash = hash_to_message!(&message_sha256d_hash(&msg)[..]);
19731979
if msg.flags & 1 == 1 {
19741980
check_update_latest!(channel.two_to_one);
19751981
if let Some(sig) = sig {

0 commit comments

Comments
 (0)