Skip to content

Commit d5cdf7a

Browse files
committed
Avoid querying the chain for outputs for channels we already have
If we receive a ChannelAnnouncement message but we already have the channel, there's no reason to do a chain lookup. Instead of immediately calling the user-provided `chain::Access` when handling a ChannelAnnouncement, we first check if we have the corresponding channel in the graph. Note that if we do have the corresponding channel but it was not previously checked against the blockchain, we should still check with the `chain::Access` and update if necessary.
1 parent 19536c6 commit d5cdf7a

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

lightning/src/routing/gossip.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,28 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
14331433
return Err(LightningError{err: "Channel announcement node had a channel with itself".to_owned(), action: ErrorAction::IgnoreError});
14341434
}
14351435

1436+
{
1437+
let channels = self.channels.read().unwrap();
1438+
1439+
if let Some(chan) = channels.get(&msg.short_channel_id) {
1440+
if chan.capacity_sats.is_some() {
1441+
// If we'd previously looked up the channel on-chain and checked the script
1442+
// against what appears on-chain, ignore the duplicate announcement.
1443+
return Err(LightningError {
1444+
err: "Already have chain-validated channel".to_owned(),
1445+
action: ErrorAction::IgnoreDuplicateGossip
1446+
});
1447+
} else if chain_access.is_none() {
1448+
// Similarly, if we can't check the chain right now anyway, ignore the
1449+
// duplicate announcement without bothering to take the channels write lock.
1450+
return Err(LightningError {
1451+
err: "Already have non-chain-validated channel".to_owned(),
1452+
action: ErrorAction::IgnoreDuplicateGossip
1453+
});
1454+
}
1455+
}
1456+
}
1457+
14361458
let utxo_value = match &chain_access {
14371459
&None => {
14381460
// Tentatively accept, potentially exposing us to DoS attacks
@@ -2046,7 +2068,7 @@ mod tests {
20462068
// drop new one on the floor, since we can't see any changes.
20472069
match gossip_sync.handle_channel_announcement(&valid_announcement) {
20482070
Ok(_) => panic!(),
2049-
Err(e) => assert_eq!(e.err, "Already have knowledge of channel")
2071+
Err(e) => assert_eq!(e.err, "Already have non-chain-validated channel")
20502072
};
20512073

20522074
// Test if an associated transaction were not on-chain (or not confirmed).
@@ -2080,32 +2102,13 @@ mod tests {
20802102
};
20812103
}
20822104

2083-
// If we receive announcement for the same channel (but TX is not confirmed),
2084-
// drop new one on the floor, since we can't see any changes.
2085-
*chain_source.utxo_ret.lock().unwrap() = Err(chain::AccessError::UnknownTx);
2086-
match gossip_sync.handle_channel_announcement(&valid_announcement) {
2087-
Ok(_) => panic!(),
2088-
Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry")
2089-
};
2090-
2091-
// But if it is confirmed, replace the channel
2105+
// If we receive announcement for the same channel, once we've validated it against the
2106+
// chain, we simply ignore all new (duplicate) announcements.
20922107
*chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script });
2093-
let valid_announcement = get_signed_channel_announcement(|unsigned_announcement| {
2094-
unsigned_announcement.features = ChannelFeatures::empty();
2095-
unsigned_announcement.short_channel_id += 2;
2096-
}, node_1_privkey, node_2_privkey, &secp_ctx);
20972108
match gossip_sync.handle_channel_announcement(&valid_announcement) {
2098-
Ok(res) => assert!(res),
2099-
_ => panic!()
2109+
Ok(_) => panic!(),
2110+
Err(e) => assert_eq!(e.err, "Already have chain-validated channel")
21002111
};
2101-
{
2102-
match network_graph.read_only().channels().get(&valid_announcement.contents.short_channel_id) {
2103-
Some(channel_entry) => {
2104-
assert_eq!(channel_entry.features, ChannelFeatures::empty());
2105-
},
2106-
_ => panic!()
2107-
};
2108-
}
21092112

21102113
// Don't relay valid channels with excess data
21112114
let valid_announcement = get_signed_channel_announcement(|unsigned_announcement| {

0 commit comments

Comments
 (0)