Skip to content

Commit 52aecbd

Browse files
committed
fixup!
1 parent 9fb3b71 commit 52aecbd

File tree

2 files changed

+26
-43
lines changed

2 files changed

+26
-43
lines changed

src/chain/chaininterface.rs

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ use std::sync::atomic::{AtomicUsize, Ordering};
1111

1212
/// Used to give chain error details upstream
1313
pub enum ChainError {
14-
/// Chain isn't supported
14+
/// Client doesn't support UTXO lookup (but the chain hash matches our genesis block hash)
1515
NotSupported,
1616
/// Chain isn't the one watched
1717
NotWatched,
18-
/// Tx isn't there
18+
/// Tx doesn't exist or is unconfirmed
1919
UnknownTx,
20-
/// Tx isn't confirmed
21-
UnconfirmedTx,
2220
}
2321

2422
/// An interface to request notification of certain scripts as they appear the
@@ -40,14 +38,11 @@ pub trait ChainWatchInterface: Sync + Send {
4038
fn register_listener(&self, listener: Weak<ChainListener>);
4139
//TODO: unregister
4240

43-
/// Gets the chain currently watched
44-
fn get_network(&self) -> Network;
45-
46-
/// Gets the script and value in satoshis for a given txid and outpoint index
47-
fn get_chain_txo(&self, genesis_hash: Sha256dHash, txid: Sha256dHash, output_index: u16) -> Result<(Script, u64), ChainError>;
48-
49-
/// Gets if outpoint is among UTXO
50-
fn get_spendable_outpoint(&self, genesis_hash: Sha256dHash, txid: Sha256dHash, output_index: u16) -> Result<bool, ChainError>;
41+
/// Gets the script and value in satoshis for a given unspent transaction output given a
42+
/// short_channel_id (aka unspent_tx_output_identier). For BTC/tBTC channels the top three
43+
/// bytes are the block height, the next 3 the transaction index within the block, and the
44+
/// final two the output within the transaction.
45+
fn get_chain_utxo(&self, genesis_hash: Sha256dHash, unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError>;
5146
}
5247

5348
/// An interface to send a transaction to the Bitcoin network.
@@ -100,14 +95,6 @@ pub struct ChainWatchInterfaceUtil {
10095
logger: Arc<Logger>,
10196
}
10297

103-
macro_rules! watched_chain {
104-
($self: ident, $hash: expr) => {
105-
if $hash != genesis_block($self.get_network()).header.bitcoin_hash() {
106-
return Err(ChainError::NotWatched);
107-
}
108-
}
109-
}
110-
11198
/// Register listener
11299
impl ChainWatchInterface for ChainWatchInterfaceUtil {
113100
fn install_watch_script(&self, script_pub_key: &Script) {
@@ -133,23 +120,11 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
133120
vec.push(listener);
134121
}
135122

136-
fn get_network(&self) -> Network {
137-
self.network
138-
}
139-
140-
141-
fn get_chain_txo(&self, genesis_hash: Sha256dHash, _txid: Sha256dHash, _output_index: u16) -> Result<(Script, u64), ChainError> {
142-
watched_chain!(self, genesis_hash);
143-
144-
//TODO: self.BlockchainStore.get_txo(txid, output_index)
145-
Err(ChainError::UnknownTx)
146-
}
147-
148-
fn get_spendable_outpoint(&self, genesis_hash: Sha256dHash, _txid: Sha256dHash, _output_index: u16) -> Result<bool, ChainError> {
149-
watched_chain!(self, genesis_hash);
150-
151-
//TODO: self.BlockchainStore.is_utxo(txid, output_index)
152-
Err(ChainError::UnknownTx)
123+
fn get_chain_utxo(&self, genesis_hash: Sha256dHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
124+
if genesis_hash != genesis_block(self.network).header.bitcoin_hash() {
125+
return Err(ChainError::NotWatched);
126+
}
127+
Err(ChainError::NotSupported)
153128
}
154129
}
155130

src/ln/router.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ use secp256k1::{Secp256k1,Message};
33
use secp256k1;
44

55
use bitcoin::util::hash::Sha256dHash;
6-
use bitcoin::blockdata::constants::genesis_block;
7-
use bitcoin::network::serialize::BitcoinHash;
86

9-
use chain::chaininterface::ChainWatchInterface;
7+
use chain::chaininterface::{ChainError, ChainWatchInterface};
108
use ln::channelmanager;
119
use ln::msgs::{ErrorAction,HandleError,RoutingMessageHandler,MsgEncodable,NetAddress,GlobalFeatures};
1210
use ln::msgs;
@@ -209,9 +207,19 @@ impl RoutingMessageHandler for Router {
209207
panic!("Unknown-required-features ChannelAnnouncements should never deserialize!");
210208
}
211209

212-
//TODO: Call blockchain thing to ask if the short_channel_id is valid
213-
if msg.contents.chain_hash != genesis_block(self.chain_monitor.get_network()).header.bitcoin_hash() {
214-
return Err(HandleError{err: "Channel announced on an unknown chain", action: Some(ErrorAction::IgnoreError)});
210+
match self.chain_monitor.get_chain_utxo(msg.contents.chain_hash, msg.contents.short_channel_id) {
211+
Ok((script_pubkey, _value)) => {
212+
//TODO: Check if script_pubkey matches bitcoin_key_1 and bitcoin_key_2
213+
},
214+
Err(ChainError::NotSupported) => {
215+
// Tenatively accept, potentially exposing us to DoS attacks
216+
},
217+
Err(ChainError::NotWatched) => {
218+
return Err(HandleError{err: "Channel announced on an unknown chain", action: Some(ErrorAction::IgnoreError)});
219+
},
220+
Err(ChainError::UnknownTx) => {
221+
return Err(HandleError{err: "Channel announced without corresponding UTXO entry", action: Some(ErrorAction::IgnoreError)});
222+
},
215223
}
216224

217225
let mut network = self.network_map.write().unwrap();

0 commit comments

Comments
 (0)