-
Notifications
You must be signed in to change notification settings - Fork 407
Check channel_announcement is on Bitcoin Mainnet or Testnet + test_invalid_channel_announcement #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Check channel_announcement is on Bitcoin Mainnet or Testnet + test_invalid_channel_announcement #134
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fa2d2bd
Add ChainWatchInterface in Router
6a12ff7
Implement get_chain_utxo and ChainError in
267a412
Check script_pubkey against expected channel_announcement
TheBlueMatt 56571ae
Add test_invalid_channel_announcemnt + test utilities
2d87bad
Add DummyChainWatcher in route_target
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2093,13 +2093,14 @@ mod tests { | |
use bitcoin::util::hash::Sha256dHash; | ||
use bitcoin::blockdata::block::{Block, BlockHeader}; | ||
use bitcoin::blockdata::transaction::{Transaction, TxOut}; | ||
use bitcoin::blockdata::constants::genesis_block; | ||
use bitcoin::network::constants::Network; | ||
use bitcoin::network::serialize::serialize; | ||
use bitcoin::network::serialize::BitcoinHash; | ||
|
||
use hex; | ||
|
||
use secp256k1::Secp256k1; | ||
use secp256k1::{Secp256k1, Message}; | ||
use secp256k1::key::{PublicKey,SecretKey}; | ||
|
||
use crypto::sha2::Sha256; | ||
|
@@ -2816,7 +2817,7 @@ mod tests { | |
|
||
for _ in 0..node_count { | ||
let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }); | ||
let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Arc::clone(&logger))); | ||
let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger))); | ||
let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())}); | ||
let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone())); | ||
let node_id = { | ||
|
@@ -2825,7 +2826,7 @@ mod tests { | |
SecretKey::from_slice(&secp_ctx, &key_slice).unwrap() | ||
}; | ||
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(); | ||
let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &node_id), Arc::clone(&logger)); | ||
let router = Router::new(PublicKey::from_secret_key(&secp_ctx, &node_id), chain_monitor.clone(), Arc::clone(&logger)); | ||
nodes.push(Node { chain_monitor, tx_broadcaster, chan_monitor, node, router }); | ||
} | ||
|
||
|
@@ -3235,4 +3236,78 @@ mod tests { | |
assert_eq!(channel_state.by_id.len(), 0); | ||
assert_eq!(channel_state.short_to_id.len(), 0); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_channel_announcement() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a little bit plumbing but I needed to separate msg content from its signatures to tweak fields freely so can't reuse create_announced_chan_between_nodes here |
||
//Test BOLT 7 channel_announcement msg requirement for final node, gather data to build customed channel_announcement msgs | ||
let secp_ctx = Secp256k1::new(); | ||
let nodes = create_network(2); | ||
|
||
let chan_announcement = create_chan_between_nodes(&nodes[0], &nodes[1]); | ||
|
||
let a_channel_lock = nodes[0].node.channel_state.lock().unwrap(); | ||
let b_channel_lock = nodes[1].node.channel_state.lock().unwrap(); | ||
let as_chan = a_channel_lock.by_id.get(&chan_announcement.3).unwrap(); | ||
let bs_chan = b_channel_lock.by_id.get(&chan_announcement.3).unwrap(); | ||
|
||
let _ = nodes[0].router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap() } ); | ||
|
||
let as_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &as_chan.get_local_keys().funding_key); | ||
let bs_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &bs_chan.get_local_keys().funding_key); | ||
|
||
let as_network_key = nodes[0].node.get_our_node_id(); | ||
let bs_network_key = nodes[1].node.get_our_node_id(); | ||
|
||
let were_node_one = as_bitcoin_key.serialize()[..] < bs_bitcoin_key.serialize()[..]; | ||
|
||
let mut chan_announcement; | ||
|
||
macro_rules! dummy_unsigned_msg { | ||
() => { | ||
msgs::UnsignedChannelAnnouncement { | ||
features: msgs::GlobalFeatures::new(), | ||
chain_hash: genesis_block(Network::Testnet).header.bitcoin_hash(), | ||
short_channel_id: as_chan.get_short_channel_id().unwrap(), | ||
node_id_1: if were_node_one { as_network_key } else { bs_network_key }, | ||
node_id_2: if !were_node_one { bs_network_key } else { as_network_key }, | ||
bitcoin_key_1: if were_node_one { as_bitcoin_key } else { bs_bitcoin_key }, | ||
bitcoin_key_2: if !were_node_one { bs_bitcoin_key } else { as_bitcoin_key }, | ||
excess_data: Vec::new(), | ||
}; | ||
} | ||
} | ||
|
||
macro_rules! sign_msg { | ||
($unsigned_msg: expr) => { | ||
let msghash = Message::from_slice(&Sha256dHash::from_data(&$unsigned_msg.encode()[..])[..]).unwrap(); | ||
let as_bitcoin_sig = secp_ctx.sign(&msghash, &as_chan.get_local_keys().funding_key); | ||
let bs_bitcoin_sig = secp_ctx.sign(&msghash, &bs_chan.get_local_keys().funding_key); | ||
let as_node_sig = secp_ctx.sign(&msghash, &nodes[0].node.our_network_key); | ||
let bs_node_sig = secp_ctx.sign(&msghash, &nodes[1].node.our_network_key); | ||
chan_announcement = msgs::ChannelAnnouncement { | ||
node_signature_1 : if were_node_one { as_node_sig } else { bs_node_sig}, | ||
node_signature_2 : if !were_node_one { bs_node_sig } else { as_node_sig}, | ||
bitcoin_signature_1: if were_node_one { as_bitcoin_sig } else { bs_bitcoin_sig }, | ||
bitcoin_signature_2 : if !were_node_one { bs_bitcoin_sig } else { as_bitcoin_sig }, | ||
contents: $unsigned_msg | ||
} | ||
} | ||
} | ||
|
||
let unsigned_msg = dummy_unsigned_msg!(); | ||
sign_msg!(unsigned_msg); | ||
assert_eq!(nodes[0].router.handle_channel_announcement(&chan_announcement).unwrap(), true); | ||
let _ = nodes[0].router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap() } ); | ||
|
||
// Configured with Network::Testnet | ||
let mut unsigned_msg = dummy_unsigned_msg!(); | ||
unsigned_msg.chain_hash = genesis_block(Network::Bitcoin).header.bitcoin_hash(); | ||
sign_msg!(unsigned_msg); | ||
assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err()); | ||
|
||
let mut unsigned_msg = dummy_unsigned_msg!(); | ||
unsigned_msg.chain_hash = Sha256dHash::from_data(&[1,2,3,4,5,6,7,8,9]); | ||
sign_msg!(unsigned_msg); | ||
assert!(nodes[0].router.handle_channel_announcement(&chan_announcement).is_err()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't generate full coverage of possible return values, best to expand the return set.