Skip to content

Commit a661c92

Browse files
authored
Merge pull request #3341 from TheBlueMatt/2024-09-gossip-rustfmt-cleanup
Minor `gossip.rs` `rustfmt` cleanups
2 parents 053281f + bc5213d commit a661c92

File tree

1 file changed

+42
-112
lines changed

1 file changed

+42
-112
lines changed

lightning/src/routing/gossip.rs

Lines changed: 42 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -511,34 +511,14 @@ pub fn verify_channel_announcement<C: Verification>(
511511
msg: &ChannelAnnouncement, secp_ctx: &Secp256k1<C>,
512512
) -> Result<(), LightningError> {
513513
let msg_hash = hash_to_message!(&message_sha256d_hash(&msg.contents)[..]);
514-
secp_verify_sig!(
515-
secp_ctx,
516-
&msg_hash,
517-
&msg.node_signature_1,
518-
&get_pubkey_from_node_id!(msg.contents.node_id_1, "channel_announcement"),
519-
"channel_announcement"
520-
);
521-
secp_verify_sig!(
522-
secp_ctx,
523-
&msg_hash,
524-
&msg.node_signature_2,
525-
&get_pubkey_from_node_id!(msg.contents.node_id_2, "channel_announcement"),
526-
"channel_announcement"
527-
);
528-
secp_verify_sig!(
529-
secp_ctx,
530-
&msg_hash,
531-
&msg.bitcoin_signature_1,
532-
&get_pubkey_from_node_id!(msg.contents.bitcoin_key_1, "channel_announcement"),
533-
"channel_announcement"
534-
);
535-
secp_verify_sig!(
536-
secp_ctx,
537-
&msg_hash,
538-
&msg.bitcoin_signature_2,
539-
&get_pubkey_from_node_id!(msg.contents.bitcoin_key_2, "channel_announcement"),
540-
"channel_announcement"
541-
);
514+
let node_a = get_pubkey_from_node_id!(msg.contents.node_id_1, "channel_announcement");
515+
secp_verify_sig!(secp_ctx, &msg_hash, &msg.node_signature_1, &node_a, "channel_announcement");
516+
let node_b = get_pubkey_from_node_id!(msg.contents.node_id_2, "channel_announcement");
517+
secp_verify_sig!(secp_ctx, &msg_hash, &msg.node_signature_2, &node_b, "channel_announcement");
518+
let btc_a = get_pubkey_from_node_id!(msg.contents.bitcoin_key_1, "channel_announcement");
519+
secp_verify_sig!(secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &btc_a, "channel_announcement");
520+
let btc_b = get_pubkey_from_node_id!(msg.contents.bitcoin_key_2, "channel_announcement");
521+
secp_verify_sig!(secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &btc_b, "channel_announcement");
542522

543523
Ok(())
544524
}
@@ -2962,16 +2942,11 @@ pub(crate) mod tests {
29622942
_ => panic!(),
29632943
};
29642944

2965-
{
2966-
match network_graph
2967-
.read_only()
2968-
.channels()
2969-
.get(&valid_announcement.contents.short_channel_id)
2970-
{
2971-
None => panic!(),
2972-
Some(_) => (),
2973-
};
2974-
}
2945+
let scid = valid_announcement.contents.short_channel_id;
2946+
match network_graph.read_only().channels().get(&scid) {
2947+
None => panic!(),
2948+
Some(_) => (),
2949+
};
29752950

29762951
// If we receive announcement for the same channel (with UTXO lookups disabled),
29772952
// drop new one on the floor, since we can't see any changes.
@@ -3015,16 +2990,11 @@ pub(crate) mod tests {
30152990
_ => panic!(),
30162991
};
30172992

3018-
{
3019-
match network_graph
3020-
.read_only()
3021-
.channels()
3022-
.get(&valid_announcement.contents.short_channel_id)
3023-
{
3024-
None => panic!(),
3025-
Some(_) => (),
3026-
};
3027-
}
2993+
let scid = valid_announcement.contents.short_channel_id;
2994+
match network_graph.read_only().channels().get(&scid) {
2995+
None => panic!(),
2996+
Some(_) => (),
2997+
};
30282998

30292999
// If we receive announcement for the same channel, once we've validated it against the
30303000
// chain, we simply ignore all new (duplicate) announcements.
@@ -3044,9 +3014,8 @@ pub(crate) mod tests {
30443014
.expect("Time must be > 1970")
30453015
.as_secs();
30463016
// Mark a node as permanently failed so it's tracked as removed.
3047-
gossip_sync
3048-
.network_graph()
3049-
.node_failed_permanent(&PublicKey::from_secret_key(&secp_ctx, node_1_privkey));
3017+
let node_1_pubkey = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
3018+
gossip_sync.network_graph().node_failed_permanent(&node_1_pubkey);
30503019

30513020
// Return error and ignore valid channel announcement if one of the nodes has been tracked as removed.
30523021
let valid_announcement = get_signed_channel_announcement(
@@ -3291,59 +3260,48 @@ pub(crate) mod tests {
32913260

32923261
let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
32933262
let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
3294-
let node_2_id = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
3263+
let node_2_pk = PublicKey::from_secret_key(&secp_ctx, node_2_privkey);
3264+
let node_2_id = NodeId::from_pubkey(&node_2_pk);
32953265

32963266
{
32973267
// There is no nodes in the table at the beginning.
32983268
assert_eq!(network_graph.read_only().nodes().len(), 0);
32993269
}
33003270

3301-
let short_channel_id;
3271+
let scid;
33023272
{
33033273
// Check that we can manually apply a channel update.
33043274
let valid_channel_announcement =
33053275
get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx);
3306-
short_channel_id = valid_channel_announcement.contents.short_channel_id;
3276+
scid = valid_channel_announcement.contents.short_channel_id;
33073277
let chain_source: Option<&test_utils::TestChainSource> = None;
33083278
assert!(network_graph
33093279
.update_channel_from_announcement(&valid_channel_announcement, &chain_source)
33103280
.is_ok());
3311-
assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
3281+
assert!(network_graph.read_only().channels().get(&scid).is_some());
33123282

33133283
let valid_channel_update = get_signed_channel_update(|_| {}, node_1_privkey, &secp_ctx);
33143284

3315-
assert!(network_graph
3316-
.read_only()
3317-
.channels()
3318-
.get(&short_channel_id)
3319-
.unwrap()
3320-
.one_to_two
3321-
.is_none());
3285+
assert!(network_graph.read_only().channels().get(&scid).unwrap().one_to_two.is_none());
33223286
network_graph.update_channel(&valid_channel_update).unwrap();
3323-
assert!(network_graph
3324-
.read_only()
3325-
.channels()
3326-
.get(&short_channel_id)
3327-
.unwrap()
3328-
.one_to_two
3329-
.is_some());
3287+
assert!(network_graph.read_only().channels().get(&scid).unwrap().one_to_two.is_some());
33303288
}
33313289

33323290
// Non-permanent failure doesn't touch the channel at all
33333291
{
3334-
match network_graph.read_only().channels().get(&short_channel_id) {
3292+
match network_graph.read_only().channels().get(&scid) {
33353293
None => panic!(),
33363294
Some(channel_info) => {
33373295
assert!(channel_info.one_to_two.as_ref().unwrap().enabled);
33383296
},
33393297
};
33403298

33413299
network_graph.handle_network_update(&NetworkUpdate::ChannelFailure {
3342-
short_channel_id,
3300+
short_channel_id: scid,
33433301
is_permanent: false,
33443302
});
33453303

3346-
match network_graph.read_only().channels().get(&short_channel_id) {
3304+
match network_graph.read_only().channels().get(&scid) {
33473305
None => panic!(),
33483306
Some(channel_info) => {
33493307
assert!(channel_info.one_to_two.as_ref().unwrap().enabled);
@@ -3353,7 +3311,7 @@ pub(crate) mod tests {
33533311

33543312
// Permanent closing deletes a channel
33553313
network_graph.handle_network_update(&NetworkUpdate::ChannelFailure {
3356-
short_channel_id,
3314+
short_channel_id: scid,
33573315
is_permanent: true,
33583316
});
33593317

@@ -3377,20 +3335,16 @@ pub(crate) mod tests {
33773335

33783336
// Non-permanent node failure does not delete any nodes or channels
33793337
network_graph.handle_network_update(&NetworkUpdate::NodeFailure {
3380-
node_id: node_2_id,
3338+
node_id: node_2_pk,
33813339
is_permanent: false,
33823340
});
33833341

33843342
assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
3385-
assert!(network_graph
3386-
.read_only()
3387-
.nodes()
3388-
.get(&NodeId::from_pubkey(&node_2_id))
3389-
.is_some());
3343+
assert!(network_graph.read_only().nodes().get(&node_2_id).is_some());
33903344

33913345
// Permanent node failure deletes node and its channels
33923346
network_graph.handle_network_update(&NetworkUpdate::NodeFailure {
3393-
node_id: node_2_id,
3347+
node_id: node_2_pk,
33943348
is_permanent: true,
33953349
});
33963350

@@ -3415,25 +3369,19 @@ pub(crate) mod tests {
34153369

34163370
let valid_channel_announcement =
34173371
get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx);
3418-
let short_channel_id = valid_channel_announcement.contents.short_channel_id;
3372+
let scid = valid_channel_announcement.contents.short_channel_id;
34193373
let chain_source: Option<&test_utils::TestChainSource> = None;
34203374
assert!(network_graph
34213375
.update_channel_from_announcement(&valid_channel_announcement, &chain_source)
34223376
.is_ok());
3423-
assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
3377+
assert!(network_graph.read_only().channels().get(&scid).is_some());
34243378

34253379
// Submit two channel updates for each channel direction (update.flags bit).
34263380
let valid_channel_update = get_signed_channel_update(|_| {}, node_1_privkey, &secp_ctx);
34273381
assert!(gossip_sync
34283382
.handle_channel_update(Some(node_1_pubkey), &valid_channel_update)
34293383
.is_ok());
3430-
assert!(network_graph
3431-
.read_only()
3432-
.channels()
3433-
.get(&short_channel_id)
3434-
.unwrap()
3435-
.one_to_two
3436-
.is_some());
3384+
assert!(network_graph.read_only().channels().get(&scid).unwrap().one_to_two.is_some());
34373385

34383386
let valid_channel_update_2 = get_signed_channel_update(
34393387
|update| {
@@ -3443,13 +3391,7 @@ pub(crate) mod tests {
34433391
&secp_ctx,
34443392
);
34453393
gossip_sync.handle_channel_update(Some(node_1_pubkey), &valid_channel_update_2).unwrap();
3446-
assert!(network_graph
3447-
.read_only()
3448-
.channels()
3449-
.get(&short_channel_id)
3450-
.unwrap()
3451-
.two_to_one
3452-
.is_some());
3394+
assert!(network_graph.read_only().channels().get(&scid).unwrap().two_to_one.is_some());
34533395

34543396
network_graph.remove_stale_channels_and_tracking_with_time(
34553397
100 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS,
@@ -3480,13 +3422,7 @@ pub(crate) mod tests {
34803422
// Note that the directional channel information will have been removed already..
34813423
// We want to check that this will work even if *one* of the channel updates is recent,
34823424
// so we should add it with a recent timestamp.
3483-
assert!(network_graph
3484-
.read_only()
3485-
.channels()
3486-
.get(&short_channel_id)
3487-
.unwrap()
3488-
.one_to_two
3489-
.is_none());
3425+
assert!(network_graph.read_only().channels().get(&scid).unwrap().one_to_two.is_none());
34903426
use std::time::{SystemTime, UNIX_EPOCH};
34913427
let announcement_time = SystemTime::now()
34923428
.duration_since(UNIX_EPOCH)
@@ -3503,13 +3439,7 @@ pub(crate) mod tests {
35033439
assert!(gossip_sync
35043440
.handle_channel_update(Some(node_1_pubkey), &valid_channel_update)
35053441
.is_ok());
3506-
assert!(network_graph
3507-
.read_only()
3508-
.channels()
3509-
.get(&short_channel_id)
3510-
.unwrap()
3511-
.one_to_two
3512-
.is_some());
3442+
assert!(network_graph.read_only().channels().get(&scid).unwrap().one_to_two.is_some());
35133443
network_graph.remove_stale_channels_and_tracking_with_time(
35143444
announcement_time + 1 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS,
35153445
);
@@ -3548,7 +3478,7 @@ pub(crate) mod tests {
35483478

35493479
// Mark the channel as permanently failed. This will also remove the two nodes
35503480
// and all of the entries will be tracked as removed.
3551-
network_graph.channel_failed_permanent_with_time(short_channel_id, Some(tracking_time));
3481+
network_graph.channel_failed_permanent_with_time(scid, Some(tracking_time));
35523482

35533483
// Should not remove from tracking if insufficient time has passed
35543484
network_graph.remove_stale_channels_and_tracking_with_time(
@@ -3597,7 +3527,7 @@ pub(crate) mod tests {
35973527

35983528
// Mark the channel as permanently failed. This will also remove the two nodes
35993529
// and all of the entries will be tracked as removed.
3600-
network_graph.channel_failed_permanent(short_channel_id);
3530+
network_graph.channel_failed_permanent(scid);
36013531

36023532
// The first time we call the following, the channel will have a removal time assigned.
36033533
network_graph.remove_stale_channels_and_tracking_with_time(removal_time);

0 commit comments

Comments
 (0)