Skip to content

Commit 63ca72e

Browse files
committed
Skip channel_update signature checks if we have a newer state
`channel_update` messages already have their signatures checked with the network graph write lock held, so there's no reason to check the signatures before doing other quicker checks first, including checking if we're already aware of a newer update for the channel. This reduces common-case CPU usage as `channel_update`s are sent rather liberally over the p2p network to gossip them.
1 parent fcfd683 commit 63ca72e

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,8 +1376,8 @@ impl NetworkGraph {
13761376
}
13771377
}
13781378
}
1379-
macro_rules! maybe_update_channel_info {
1380-
( $target: expr, $src_node: expr) => {
1379+
macro_rules! check_update_latest {
1380+
($target: expr) => {
13811381
if let Some(existing_chan_info) = $target.as_ref() {
13821382
// The timestamp field is somewhat of a misnomer - the BOLTs use it to
13831383
// order updates to ensure you always have the latest one, only
@@ -1394,7 +1394,11 @@ impl NetworkGraph {
13941394
} else {
13951395
chan_was_enabled = false;
13961396
}
1397+
}
1398+
}
13971399

1400+
macro_rules! get_new_channel_info {
1401+
() => { {
13981402
let last_update_message = if msg.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY
13991403
{ full_msg.cloned() } else { None };
14001404

@@ -1410,29 +1414,31 @@ impl NetworkGraph {
14101414
},
14111415
last_update_message
14121416
};
1413-
$target = Some(updated_channel_update_info);
1414-
}
1417+
Some(updated_channel_update_info)
1418+
} }
14151419
}
14161420

14171421
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.encode()[..])[..]);
14181422
if msg.flags & 1 == 1 {
14191423
dest_node_id = channel.node_one.clone();
1424+
check_update_latest!(channel.two_to_one);
14201425
if let Some((sig, ctx)) = sig_info {
14211426
secp_verify_sig!(ctx, &msg_hash, &sig, &PublicKey::from_slice(channel.node_two.as_slice()).map_err(|_| LightningError{
14221427
err: "Couldn't parse source node pubkey".to_owned(),
14231428
action: ErrorAction::IgnoreAndLog(Level::Debug)
14241429
})?, "channel_update");
14251430
}
1426-
maybe_update_channel_info!(channel.two_to_one, channel.node_two);
1431+
channel.two_to_one = get_new_channel_info!();
14271432
} else {
14281433
dest_node_id = channel.node_two.clone();
1434+
check_update_latest!(channel.one_to_two);
14291435
if let Some((sig, ctx)) = sig_info {
14301436
secp_verify_sig!(ctx, &msg_hash, &sig, &PublicKey::from_slice(channel.node_one.as_slice()).map_err(|_| LightningError{
14311437
err: "Couldn't parse destination node pubkey".to_owned(),
14321438
action: ErrorAction::IgnoreAndLog(Level::Debug)
14331439
})?, "channel_update");
14341440
}
1435-
maybe_update_channel_info!(channel.one_to_two, channel.node_one);
1441+
channel.one_to_two = get_new_channel_info!();
14361442
}
14371443
}
14381444
}

0 commit comments

Comments
 (0)