Skip to content

Commit d3105d7

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 96b9cf2 commit d3105d7

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-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_from_slices;
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::utxo::{UtxoLookup, UtxoLookupError};
26+
use crate::routing::utxo::{self, UtxoLookup};
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};
@@ -1497,32 +1494,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
14971494
}
14981495
}
14991496

1500-
let utxo_value = match &utxo_lookup {
1501-
&None => {
1502-
// Tentatively accept, potentially exposing us to DoS attacks
1503-
None
1504-
},
1505-
&Some(ref utxo_lookup) => {
1506-
match utxo_lookup.get_utxo(&msg.chain_hash, msg.short_channel_id) {
1507-
Ok(TxOut { value, script_pubkey }) => {
1508-
let expected_script =
1509-
make_funding_redeemscript_from_slices(msg.bitcoin_key_1.as_slice(), msg.bitcoin_key_2.as_slice()).to_v0_p2wsh();
1510-
if script_pubkey != expected_script {
1511-
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});
1512-
}
1513-
//TODO: Check if value is worth storing, use it to inform routing, and compare it
1514-
//to the new HTLC max field in channel_update
1515-
Some(value)
1516-
},
1517-
Err(UtxoLookupError::UnknownChain) => {
1518-
return Err(LightningError{err: format!("Channel announced on an unknown chain ({})", msg.chain_hash.encode().to_hex()), action: ErrorAction::IgnoreError});
1519-
},
1520-
Err(UtxoLookupError::UnknownTx) => {
1521-
return Err(LightningError{err: "Channel announced without corresponding UTXO entry".to_owned(), action: ErrorAction::IgnoreError});
1522-
},
1523-
}
1524-
},
1525-
};
1497+
let utxo_value = utxo::check_channel_announcement(utxo_lookup, msg)?;
15261498

15271499
#[allow(unused_mut, unused_assignments)]
15281500
let mut announcement_received_time = 0;

lightning/src/routing/utxo.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
//! order to announce a channel. This module handles that checking.
1515
1616
use bitcoin::{BlockHash, TxOut};
17+
use bitcoin::hashes::hex::ToHex;
18+
19+
use crate::ln::chan_utils::make_funding_redeemscript_from_slices;
20+
use crate::ln::msgs::{self, LightningError, ErrorAction};
21+
use crate::util::ser::Writeable;
22+
23+
use crate::prelude::*;
24+
25+
use core::ops::Deref;
1726

1827
/// An error when accessing the chain via [`UtxoLookup`].
1928
#[derive(Clone, Debug)]
@@ -34,3 +43,35 @@ pub trait UtxoLookup {
3443
/// [`short_channel_id`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#definition-of-short_channel_id
3544
fn get_utxo(&self, genesis_hash: &BlockHash, short_channel_id: u64) -> Result<TxOut, UtxoLookupError>;
3645
}
46+
47+
pub(crate) fn check_channel_announcement<U: Deref>(
48+
utxo_lookup: &Option<U>, msg: &msgs::UnsignedChannelAnnouncement
49+
) -> Result<Option<u64>, msgs::LightningError> where U::Target: UtxoLookup {
50+
let utxo_value = match utxo_lookup {
51+
&None => {
52+
// Tentatively accept, potentially exposing us to DoS attacks
53+
None
54+
},
55+
&Some(ref utxo_lookup) => {
56+
match utxo_lookup.get_utxo(&msg.chain_hash, msg.short_channel_id) {
57+
Ok(TxOut { value, script_pubkey }) => {
58+
let expected_script =
59+
make_funding_redeemscript_from_slices(msg.bitcoin_key_1.as_slice(), msg.bitcoin_key_2.as_slice()).to_v0_p2wsh();
60+
if script_pubkey != expected_script {
61+
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});
62+
}
63+
//TODO: Check if value is worth storing, use it to inform routing, and compare it
64+
//to the new HTLC max field in channel_update
65+
Some(value)
66+
},
67+
Err(UtxoLookupError::UnknownChain) => {
68+
return Err(LightningError{err: format!("Channel announced on an unknown chain ({})", msg.chain_hash.encode().to_hex()), action: ErrorAction::IgnoreError});
69+
},
70+
Err(UtxoLookupError::UnknownTx) => {
71+
return Err(LightningError{err: "Channel announced without corresponding UTXO entry".to_owned(), action: ErrorAction::IgnoreError});
72+
},
73+
}
74+
}
75+
};
76+
Ok(utxo_value)
77+
}

0 commit comments

Comments
 (0)