Skip to content

Commit 629b66c

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 629b66c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lightning/src/routing/gossip.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,22 @@ 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 mut 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{err: "".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
1444+
} else if chain_access.is_none() {
1445+
// Similarly, if we can't check the chain right now anyway, ignore the
1446+
// duplicate announcement without bothering to take the channels write lock.
1447+
return Err(LightningError{err: "".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
1448+
}
1449+
}
1450+
}
1451+
14361452
let utxo_value = match &chain_access {
14371453
&None => {
14381454
// Tentatively accept, potentially exposing us to DoS attacks

0 commit comments

Comments
 (0)