Skip to content

Commit 56571ae

Browse files
author
Antoine Riard
committed
Add test_invalid_channel_announcemnt + test utilities
Fix typo
1 parent 267a412 commit 56571ae

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

src/chain/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct OutPoint {
1313
}
1414

1515
impl OutPoint {
16-
/// Creates a new `OutPoint` from the txid an the index.
16+
/// Creates a new `OutPoint` from the txid and the index.
1717
pub fn new(txid: Sha256dHash, index: u16) -> OutPoint {
1818
OutPoint { txid, index }
1919
}

src/ln/channel.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,12 @@ impl Channel {
20022002
self.channel_value_satoshis
20032003
}
20042004

2005+
//TODO: Testing purpose only, should be changed in another way after #81
2006+
#[cfg(test)]
2007+
pub fn get_local_keys(&self) -> &ChannelKeys {
2008+
&self.local_keys
2009+
}
2010+
20052011
/// Allowed in any state (including after shutdown)
20062012
pub fn get_channel_update_count(&self) -> u32 {
20072013
self.channel_update_count

src/ln/channelmanager.rs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,13 +2093,14 @@ mod tests {
20932093
use bitcoin::util::hash::Sha256dHash;
20942094
use bitcoin::blockdata::block::{Block, BlockHeader};
20952095
use bitcoin::blockdata::transaction::{Transaction, TxOut};
2096+
use bitcoin::blockdata::constants::genesis_block;
20962097
use bitcoin::network::constants::Network;
20972098
use bitcoin::network::serialize::serialize;
20982099
use bitcoin::network::serialize::BitcoinHash;
20992100

21002101
use hex;
21012102

2102-
use secp256k1::Secp256k1;
2103+
use secp256k1::{Secp256k1, Message};
21032104
use secp256k1::key::{PublicKey,SecretKey};
21042105

21052106
use crypto::sha2::Sha256;
@@ -2826,7 +2827,7 @@ mod tests {
28262827
};
28272828
let node = ChannelManager::new(node_id.clone(), 0, true, Network::Testnet, feeest.clone(), chan_monitor.clone(), chain_monitor.clone(), tx_broadcaster.clone(), Arc::clone(&logger)).unwrap();
28282829
let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &node_id), chain_monitor.clone(), Arc::clone(&logger));
2829-
nodes.push(Node { feeest, chain_monitor, tx_broadcaster, chan_monitor, node, router });
2830+
nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router });
28302831
}
28312832

28322833
nodes
@@ -3235,4 +3236,78 @@ mod tests {
32353236
assert_eq!(channel_state.by_id.len(), 0);
32363237
assert_eq!(channel_state.short_to_id.len(), 0);
32373238
}
3239+
3240+
#[test]
3241+
fn test_invalid_channel_announcement() {
3242+
//Test BOLT 7 channel_announcement msg requirement for final node, gather data to build customed channel_announcement msgs
3243+
let secp_ctx = Secp256k1::new();
3244+
let nodes = create_network(2);
3245+
3246+
let chan_announcement = create_chan_between_nodes(&nodes[0], &nodes[1]);
3247+
3248+
let a_channel_lock = nodes[0].node.channel_state.lock().unwrap();
3249+
let b_channel_lock = nodes[1].node.channel_state.lock().unwrap();
3250+
let as_chan = a_channel_lock.by_id.get(&chan_announcement.3).unwrap();
3251+
let bs_chan = b_channel_lock.by_id.get(&chan_announcement.3).unwrap();
3252+
3253+
let _ = nodes[0].router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap() } );
3254+
3255+
let as_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &as_chan.get_local_keys().funding_key);
3256+
let bs_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &bs_chan.get_local_keys().funding_key);
3257+
3258+
let as_network_key = nodes[0].node.get_our_node_id();
3259+
let bs_network_key = nodes[1].node.get_our_node_id();
3260+
3261+
let were_node_one = as_bitcoin_key.serialize()[..] < bs_bitcoin_key.serialize()[..];
3262+
3263+
let mut chan_announcement;
3264+
3265+
macro_rules! dummy_unsigned_msg {
3266+
() => {
3267+
msgs::UnsignedChannelAnnouncement {
3268+
features: msgs::GlobalFeatures::new(),
3269+
chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(),
3270+
short_channel_id: as_chan.get_short_channel_id().unwrap(),
3271+
node_id_1: if were_node_one { as_network_key } else { bs_network_key },
3272+
node_id_2: if !were_node_one { bs_network_key } else { as_network_key },
3273+
bitcoin_key_1: if were_node_one { as_bitcoin_key } else { bs_bitcoin_key },
3274+
bitcoin_key_2: if !were_node_one { bs_bitcoin_key } else { as_bitcoin_key },
3275+
excess_data: Vec::new(),
3276+
};
3277+
}
3278+
}
3279+
3280+
macro_rules! sign_msg {
3281+
($unsigned_msg: expr) => {
3282+
let msghash = Message::from_slice(&Sha256dHash::from_data(&$unsigned_msg.encode()[..])[..]).unwrap();
3283+
let as_bitcoin_sig = secp_ctx.sign(&msghash, &as_chan.get_local_keys().funding_key);
3284+
let bs_bitcoin_sig = secp_ctx.sign(&msghash, &bs_chan.get_local_keys().funding_key);
3285+
let as_node_sig = secp_ctx.sign(&msghash, &nodes[0].node.our_network_key);
3286+
let bs_node_sig = secp_ctx.sign(&msghash, &nodes[1].node.our_network_key);
3287+
chan_announcement = msgs::ChannelAnnouncement {
3288+
node_signature_1 : if were_node_one { as_node_sig } else { bs_node_sig},
3289+
node_signature_2 : if !were_node_one { bs_node_sig } else { as_node_sig},
3290+
bitcoin_signature_1: if were_node_one { as_bitcoin_sig } else { bs_bitcoin_sig },
3291+
bitcoin_signature_2 : if !were_node_one { bs_bitcoin_sig } else { as_bitcoin_sig },
3292+
contents: $unsigned_msg
3293+
}
3294+
}
3295+
}
3296+
3297+
let unsigned_msg = dummy_unsigned_msg!();
3298+
sign_msg!(unsigned_msg);
3299+
assert_eq!(nodes[0].router.handle_channel_announcement(&chan_announcement).unwrap(), true);
3300+
let _ = nodes[0].router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap() } );
3301+
3302+
// Configured with Network::Testnet
3303+
let mut unsigned_msg = dummy_unsigned_msg!();
3304+
unsigned_msg.chain_hash = genesis_block(Network::Bitcoin).header.bitcoin_hash();
3305+
sign_msg!(unsigned_msg);
3306+
assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err());
3307+
3308+
let mut unsigned_msg = dummy_unsigned_msg!();
3309+
unsigned_msg.chain_hash = Sha256dHash::from_data(&[1,2,3,4,5,6,7,8,9]);
3310+
sign_msg!(unsigned_msg);
3311+
assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err());
3312+
}
32383313
}

src/ln/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl RoutingMessageHandler for Router {
222222
//to the new HTLC max field in channel_update
223223
},
224224
Err(ChainError::NotSupported) => {
225-
// Tenatively accept, potentially exposing us to DoS attacks
225+
// Tentatively accept, potentially exposing us to DoS attacks
226226
},
227227
Err(ChainError::NotWatched) => {
228228
return Err(HandleError{err: "Channel announced on an unknown chain", action: Some(ErrorAction::IgnoreError)});

0 commit comments

Comments
 (0)