Skip to content

Commit 750d58e

Browse files
committed
Move logic to check a ChannelAnnouncement to gossip_checking
This commit is deliberately move-only, though the code being moved is somewhat crufty.
1 parent 98d626c commit 750d58e

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed

lightning/src/routing/gossip.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@ use bitcoin::secp256k1;
1616

1717
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
1818
use bitcoin::hashes::Hash;
19-
use bitcoin::blockdata::transaction::TxOut;
2019
use bitcoin::hash_types::BlockHash;
2120

22-
use crate::ln::chan_utils::make_funding_redeemscript;
2321
use crate::ln::features::{ChannelFeatures, NodeFeatures, InitFeatures};
2422
use crate::ln::msgs::{DecodeError, ErrorAction, Init, LightningError, RoutingMessageHandler, NetAddress, MAX_VALUE_MSAT};
2523
use crate::ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, GossipTimestampFilter};
2624
use crate::ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds, ReplyShortChannelIdsEnd};
2725
use crate::ln::msgs;
28-
use crate::routing::gossip_checking::{ChainAccess, ChainAccessError};
26+
use crate::routing::gossip_checking::{self, ChainAccess};
2927
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, MaybeReadable};
3028
use crate::util::logger::{Logger, Level};
3129
use crate::util::events::{MessageSendEvent, MessageSendEventsProvider};
@@ -42,7 +40,6 @@ use crate::sync::{RwLock, RwLockReadGuard};
4240
use core::sync::atomic::{AtomicUsize, Ordering};
4341
use crate::sync::Mutex;
4442
use core::ops::{Bound, Deref};
45-
use bitcoin::hashes::hex::ToHex;
4643

4744
#[cfg(feature = "std")]
4845
use std::time::{SystemTime, UNIX_EPOCH};
@@ -1481,32 +1478,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
14811478
}
14821479
}
14831480

1484-
let utxo_value = match &chain_access {
1485-
&None => {
1486-
// Tentatively accept, potentially exposing us to DoS attacks
1487-
None
1488-
},
1489-
&Some(ref chain_access) => {
1490-
match chain_access.get_utxo(&msg.chain_hash, msg.short_channel_id) {
1491-
Ok(TxOut { value, script_pubkey }) => {
1492-
let expected_script =
1493-
make_funding_redeemscript(&msg.bitcoin_key_1, &msg.bitcoin_key_2).to_v0_p2wsh();
1494-
if script_pubkey != expected_script {
1495-
return Err(LightningError{err: format!("Channel announcement key ({}) didn't match on-chain script ({})", expected_script.to_hex(), script_pubkey.to_hex()), action: ErrorAction::IgnoreError});
1496-
}
1497-
//TODO: Check if value is worth storing, use it to inform routing, and compare it
1498-
//to the new HTLC max field in channel_update
1499-
Some(value)
1500-
},
1501-
Err(ChainAccessError::UnknownChain) => {
1502-
return Err(LightningError{err: format!("Channel announced on an unknown chain ({})", msg.chain_hash.encode().to_hex()), action: ErrorAction::IgnoreError});
1503-
},
1504-
Err(ChainAccessError::UnknownTx) => {
1505-
return Err(LightningError{err: "Channel announced without corresponding UTXO entry".to_owned(), action: ErrorAction::IgnoreError});
1506-
},
1507-
}
1508-
},
1509-
};
1481+
let utxo_value = gossip_checking::check_channel_announcement(chain_access, msg)?;
15101482

15111483
#[allow(unused_mut, unused_assignments)]
15121484
let mut announcement_received_time = 0;

lightning/src/routing/gossip_checking.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
//! This module contains traits for LDK to access these UTXOs to check gossip data is correct.
66
77
use bitcoin::{BlockHash, TxOut};
8+
use bitcoin::hashes::hex::ToHex;
9+
10+
use crate::ln::chan_utils::make_funding_redeemscript;
11+
use crate::ln::msgs::{self, LightningError, ErrorAction};
12+
use crate::util::ser::Writeable;
13+
14+
use core::ops::Deref;
815

916
/// An error when accessing the chain via [`ChainAccess`].
1017
#[derive(Clone, Debug)]
@@ -26,3 +33,35 @@ pub trait ChainAccess {
2633
/// [`short_channel_id`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#definition-of-short_channel_id
2734
fn get_utxo(&self, genesis_hash: &BlockHash, short_channel_id: u64) -> Result<TxOut, ChainAccessError>;
2835
}
36+
37+
pub(crate) fn check_channel_announcement<A: Deref>(
38+
chain_access: &Option<A>, msg: &msgs::UnsignedChannelAnnouncement
39+
) -> Result<Option<u64>, msgs::LightningError> where A::Target: ChainAccess {
40+
let utxo_value = match chain_access {
41+
&None => {
42+
// Tentatively accept, potentially exposing us to DoS attacks
43+
None
44+
},
45+
&Some(ref chain_access) => {
46+
match chain_access.get_utxo(&msg.chain_hash, msg.short_channel_id) {
47+
Ok(TxOut { value, script_pubkey }) => {
48+
let expected_script =
49+
make_funding_redeemscript(&msg.bitcoin_key_1, &msg.bitcoin_key_2).to_v0_p2wsh();
50+
if script_pubkey != expected_script {
51+
return Err(LightningError{err: format!("Channel announcement key ({}) didn't match on-chain script ({})", expected_script.to_hex(), script_pubkey.to_hex()), action: ErrorAction::IgnoreError});
52+
}
53+
//TODO: Check if value is worth storing, use it to inform routing, and compare it
54+
//to the new HTLC max field in channel_update
55+
Some(value)
56+
},
57+
Err(ChainAccessError::UnknownChain) => {
58+
return Err(LightningError{err: format!("Channel announced on an unknown chain ({})", msg.chain_hash.encode().to_hex()), action: ErrorAction::IgnoreError});
59+
},
60+
Err(ChainAccessError::UnknownTx) => {
61+
return Err(LightningError{err: "Channel announced without corresponding UTXO entry".to_owned(), action: ErrorAction::IgnoreError});
62+
},
63+
}
64+
}
65+
};
66+
Ok(utxo_value)
67+
}

0 commit comments

Comments
 (0)