From 615b401f7bfb5dbb378bf630874a148b41950837 Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Mon, 15 Aug 2022 14:09:35 -0400 Subject: [PATCH] Move router test utils into their own module Useful for testing onion message pathfinding in upcoming PRs and any future routing modules we add --- lightning/src/routing/mod.rs | 2 + lightning/src/routing/router.rs | 481 +-------------------------- lightning/src/routing/test_utils.rs | 497 ++++++++++++++++++++++++++++ 3 files changed, 504 insertions(+), 476 deletions(-) create mode 100644 lightning/src/routing/test_utils.rs diff --git a/lightning/src/routing/mod.rs b/lightning/src/routing/mod.rs index c7babbe30..9bf091066 100644 --- a/lightning/src/routing/mod.rs +++ b/lightning/src/routing/mod.rs @@ -12,3 +12,5 @@ pub mod gossip; pub mod router; pub mod scoring; +#[cfg(test)] +mod test_utils; diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 801acaa2e..523b00eb8 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -1937,19 +1937,17 @@ mod tests { PaymentParameters, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees, DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, MAX_PATH_LENGTH_ESTIMATE}; use routing::scoring::{ChannelUsage, Score, ProbabilisticScorer, ProbabilisticScoringParameters}; + use routing::test_utils::{add_channel, add_or_update_node, build_graph, build_line_graph, id_to_feature_flags, get_nodes, update_channel}; use chain::transaction::OutPoint; use chain::keysinterface::KeysInterface; use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; - use ln::msgs::{ErrorAction, LightningError, UnsignedChannelAnnouncement, ChannelAnnouncement, RoutingMessageHandler, - NodeAnnouncement, UnsignedNodeAnnouncement, ChannelUpdate, UnsignedChannelUpdate, MAX_VALUE_MSAT}; + use ln::msgs::{ErrorAction, LightningError, UnsignedChannelUpdate, MAX_VALUE_MSAT}; use ln::channelmanager; use util::test_utils; use util::chacha20::ChaCha20; - use util::ser::Writeable; #[cfg(c_bindings)] - use util::ser::Writer; + use util::ser::{Writeable, Writer}; - use bitcoin::hashes::sha256d::Hash as Sha256dHash; use bitcoin::hashes::Hash; use bitcoin::network::constants::Network; use bitcoin::blockdata::constants::genesis_block; @@ -1960,10 +1958,10 @@ mod tests { use hex; use bitcoin::secp256k1::{PublicKey,SecretKey}; - use bitcoin::secp256k1::{Secp256k1, All}; + use bitcoin::secp256k1::Secp256k1; use prelude::*; - use sync::{self, Arc}; + use sync::Arc; use core::convert::TryInto; @@ -2001,475 +1999,6 @@ mod tests { } } - // Using the same keys for LN and BTC ids - fn add_channel( - gossip_sync: &P2PGossipSync>>, Arc, Arc>, - secp_ctx: &Secp256k1, node_1_privkey: &SecretKey, node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64 - ) { - let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); - let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); - - let unsigned_announcement = UnsignedChannelAnnouncement { - features, - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id, - node_id_1, - node_id_2, - bitcoin_key_1: node_id_1, - bitcoin_key_2: node_id_2, - excess_data: Vec::new(), - }; - - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_announcement = ChannelAnnouncement { - node_signature_1: secp_ctx.sign_ecdsa(&msghash, node_1_privkey), - node_signature_2: secp_ctx.sign_ecdsa(&msghash, node_2_privkey), - bitcoin_signature_1: secp_ctx.sign_ecdsa(&msghash, node_1_privkey), - bitcoin_signature_2: secp_ctx.sign_ecdsa(&msghash, node_2_privkey), - contents: unsigned_announcement.clone(), - }; - match gossip_sync.handle_channel_announcement(&valid_announcement) { - Ok(res) => assert!(res), - _ => panic!() - }; - } - - fn update_channel( - gossip_sync: &P2PGossipSync>>, Arc, Arc>, - secp_ctx: &Secp256k1, node_privkey: &SecretKey, update: UnsignedChannelUpdate - ) { - let msghash = hash_to_message!(&Sha256dHash::hash(&update.encode()[..])[..]); - let valid_channel_update = ChannelUpdate { - signature: secp_ctx.sign_ecdsa(&msghash, node_privkey), - contents: update.clone() - }; - - match gossip_sync.handle_channel_update(&valid_channel_update) { - Ok(res) => assert!(res), - Err(_) => panic!() - }; - } - - fn add_or_update_node( - gossip_sync: &P2PGossipSync>>, Arc, Arc>, - secp_ctx: &Secp256k1, node_privkey: &SecretKey, features: NodeFeatures, timestamp: u32 - ) { - let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey); - let unsigned_announcement = UnsignedNodeAnnouncement { - features, - timestamp, - node_id, - rgb: [0; 3], - alias: [0; 32], - addresses: Vec::new(), - excess_address_data: Vec::new(), - excess_data: Vec::new(), - }; - let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); - let valid_announcement = NodeAnnouncement { - signature: secp_ctx.sign_ecdsa(&msghash, node_privkey), - contents: unsigned_announcement.clone() - }; - - match gossip_sync.handle_node_announcement(&valid_announcement) { - Ok(_) => (), - Err(_) => panic!() - }; - } - - fn get_nodes(secp_ctx: &Secp256k1) -> (SecretKey, PublicKey, Vec, Vec) { - let privkeys: Vec = (2..22).map(|i| { - SecretKey::from_slice(&hex::decode(format!("{:02x}", i).repeat(32)).unwrap()[..]).unwrap() - }).collect(); - - let pubkeys = privkeys.iter().map(|secret| PublicKey::from_secret_key(&secp_ctx, secret)).collect(); - - let our_privkey = SecretKey::from_slice(&hex::decode("01".repeat(32)).unwrap()[..]).unwrap(); - let our_id = PublicKey::from_secret_key(&secp_ctx, &our_privkey); - - (our_privkey, our_id, privkeys, pubkeys) - } - - fn id_to_feature_flags(id: u8) -> Vec { - // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can - // test for it later. - let idx = (id - 1) * 2 + 1; - if idx > 8*3 { - vec![1 << (idx - 8*3), 0, 0, 0] - } else if idx > 8*2 { - vec![1 << (idx - 8*2), 0, 0] - } else if idx > 8*1 { - vec![1 << (idx - 8*1), 0] - } else { - vec![1 << idx] - } - } - - fn build_line_graph() -> ( - Secp256k1, sync::Arc>>, - P2PGossipSync>>, sync::Arc, sync::Arc>, - sync::Arc, sync::Arc, - ) { - let secp_ctx = Secp256k1::new(); - let logger = Arc::new(test_utils::TestLogger::new()); - let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet)); - let genesis_hash = genesis_block(Network::Testnet).header.block_hash(); - let network_graph = Arc::new(NetworkGraph::new(genesis_hash, Arc::clone(&logger))); - let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger)); - - // Build network from our_id to node 19: - // our_id -1(1)2- node0 -1(2)2- node1 - ... - node19 - let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx); - - for (idx, (cur_privkey, next_privkey)) in core::iter::once(&our_privkey) - .chain(privkeys.iter()).zip(privkeys.iter()).enumerate() { - let cur_short_channel_id = (idx as u64) + 1; - add_channel(&gossip_sync, &secp_ctx, &cur_privkey, &next_privkey, - ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), cur_short_channel_id); - update_channel(&gossip_sync, &secp_ctx, &cur_privkey, UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: cur_short_channel_id, - timestamp: idx as u32, - flags: 0, - cltv_expiry_delta: 0, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 0, - excess_data: Vec::new() - }); - update_channel(&gossip_sync, &secp_ctx, &next_privkey, UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: cur_short_channel_id, - timestamp: (idx as u32)+1, - flags: 1, - cltv_expiry_delta: 0, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 0, - excess_data: Vec::new() - }); - add_or_update_node(&gossip_sync, &secp_ctx, next_privkey, - NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0); - } - - (secp_ctx, network_graph, gossip_sync, chain_monitor, logger) - } - - fn build_graph() -> ( - Secp256k1, - sync::Arc>>, - P2PGossipSync>>, sync::Arc, sync::Arc>, - sync::Arc, - sync::Arc, - ) { - let secp_ctx = Secp256k1::new(); - let logger = Arc::new(test_utils::TestLogger::new()); - let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet)); - let genesis_hash = genesis_block(Network::Testnet).header.block_hash(); - let network_graph = Arc::new(NetworkGraph::new(genesis_hash, Arc::clone(&logger))); - let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger)); - // Build network from our_id to node6: - // - // -1(1)2- node0 -1(3)2- - // / \ - // our_id -1(12)2- node7 -1(13)2--- node2 - // \ / - // -1(2)2- node1 -1(4)2- - // - // - // chan1 1-to-2: disabled - // chan1 2-to-1: enabled, 0 fee - // - // chan2 1-to-2: enabled, ignored fee - // chan2 2-to-1: enabled, 0 fee - // - // chan3 1-to-2: enabled, 0 fee - // chan3 2-to-1: enabled, 100 msat fee - // - // chan4 1-to-2: enabled, 100% fee - // chan4 2-to-1: enabled, 0 fee - // - // chan12 1-to-2: enabled, ignored fee - // chan12 2-to-1: enabled, 0 fee - // - // chan13 1-to-2: enabled, 200% fee - // chan13 2-to-1: enabled, 0 fee - // - // - // -1(5)2- node3 -1(8)2-- - // | 2 | - // | (11) | - // / 1 \ - // node2--1(6)2- node4 -1(9)2--- node6 (not in global route map) - // \ / - // -1(7)2- node5 -1(10)2- - // - // Channels 5, 8, 9 and 10 are private channels. - // - // chan5 1-to-2: enabled, 100 msat fee - // chan5 2-to-1: enabled, 0 fee - // - // chan6 1-to-2: enabled, 0 fee - // chan6 2-to-1: enabled, 0 fee - // - // chan7 1-to-2: enabled, 100% fee - // chan7 2-to-1: enabled, 0 fee - // - // chan8 1-to-2: enabled, variable fee (0 then 1000 msat) - // chan8 2-to-1: enabled, 0 fee - // - // chan9 1-to-2: enabled, 1001 msat fee - // chan9 2-to-1: enabled, 0 fee - // - // chan10 1-to-2: enabled, 0 fee - // chan10 2-to-1: enabled, 0 fee - // - // chan11 1-to-2: enabled, 0 fee - // chan11 2-to-1: enabled, 0 fee - - let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx); - - add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[0], ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), 1); - update_channel(&gossip_sync, &secp_ctx, &privkeys[0], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 1, - timestamp: 1, - flags: 1, - cltv_expiry_delta: 0, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 0, - excess_data: Vec::new() - }); - - add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[0], NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0); - - add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[1], ChannelFeatures::from_le_bytes(id_to_feature_flags(2)), 2); - update_channel(&gossip_sync, &secp_ctx, &our_privkey, UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 2, - timestamp: 1, - flags: 0, - cltv_expiry_delta: (5 << 4) | 3, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: u32::max_value(), - fee_proportional_millionths: u32::max_value(), - excess_data: Vec::new() - }); - update_channel(&gossip_sync, &secp_ctx, &privkeys[1], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 2, - timestamp: 1, - flags: 1, - cltv_expiry_delta: 0, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 0, - excess_data: Vec::new() - }); - - add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[1], NodeFeatures::from_le_bytes(id_to_feature_flags(2)), 0); - - add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[7], ChannelFeatures::from_le_bytes(id_to_feature_flags(12)), 12); - update_channel(&gossip_sync, &secp_ctx, &our_privkey, UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 12, - timestamp: 1, - flags: 0, - cltv_expiry_delta: (5 << 4) | 3, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: u32::max_value(), - fee_proportional_millionths: u32::max_value(), - excess_data: Vec::new() - }); - update_channel(&gossip_sync, &secp_ctx, &privkeys[7], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 12, - timestamp: 1, - flags: 1, - cltv_expiry_delta: 0, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 0, - excess_data: Vec::new() - }); - - add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[7], NodeFeatures::from_le_bytes(id_to_feature_flags(8)), 0); - - add_channel(&gossip_sync, &secp_ctx, &privkeys[0], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(3)), 3); - update_channel(&gossip_sync, &secp_ctx, &privkeys[0], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 3, - timestamp: 1, - flags: 0, - cltv_expiry_delta: (3 << 4) | 1, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 0, - excess_data: Vec::new() - }); - update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 3, - timestamp: 1, - flags: 1, - cltv_expiry_delta: (3 << 4) | 2, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 100, - fee_proportional_millionths: 0, - excess_data: Vec::new() - }); - - add_channel(&gossip_sync, &secp_ctx, &privkeys[1], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(4)), 4); - update_channel(&gossip_sync, &secp_ctx, &privkeys[1], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 4, - timestamp: 1, - flags: 0, - cltv_expiry_delta: (4 << 4) | 1, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 1000000, - excess_data: Vec::new() - }); - update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 4, - timestamp: 1, - flags: 1, - cltv_expiry_delta: (4 << 4) | 2, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 0, - excess_data: Vec::new() - }); - - add_channel(&gossip_sync, &secp_ctx, &privkeys[7], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(13)), 13); - update_channel(&gossip_sync, &secp_ctx, &privkeys[7], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 13, - timestamp: 1, - flags: 0, - cltv_expiry_delta: (13 << 4) | 1, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 2000000, - excess_data: Vec::new() - }); - update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 13, - timestamp: 1, - flags: 1, - cltv_expiry_delta: (13 << 4) | 2, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 0, - excess_data: Vec::new() - }); - - add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[2], NodeFeatures::from_le_bytes(id_to_feature_flags(3)), 0); - - add_channel(&gossip_sync, &secp_ctx, &privkeys[2], &privkeys[4], ChannelFeatures::from_le_bytes(id_to_feature_flags(6)), 6); - update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 6, - timestamp: 1, - flags: 0, - cltv_expiry_delta: (6 << 4) | 1, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 0, - excess_data: Vec::new() - }); - update_channel(&gossip_sync, &secp_ctx, &privkeys[4], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 6, - timestamp: 1, - flags: 1, - cltv_expiry_delta: (6 << 4) | 2, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 0, - excess_data: Vec::new(), - }); - - add_channel(&gossip_sync, &secp_ctx, &privkeys[4], &privkeys[3], ChannelFeatures::from_le_bytes(id_to_feature_flags(11)), 11); - update_channel(&gossip_sync, &secp_ctx, &privkeys[4], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 11, - timestamp: 1, - flags: 0, - cltv_expiry_delta: (11 << 4) | 1, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 0, - excess_data: Vec::new() - }); - update_channel(&gossip_sync, &secp_ctx, &privkeys[3], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 11, - timestamp: 1, - flags: 1, - cltv_expiry_delta: (11 << 4) | 2, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 0, - excess_data: Vec::new() - }); - - add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[4], NodeFeatures::from_le_bytes(id_to_feature_flags(5)), 0); - - add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[3], NodeFeatures::from_le_bytes(id_to_feature_flags(4)), 0); - - add_channel(&gossip_sync, &secp_ctx, &privkeys[2], &privkeys[5], ChannelFeatures::from_le_bytes(id_to_feature_flags(7)), 7); - update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 7, - timestamp: 1, - flags: 0, - cltv_expiry_delta: (7 << 4) | 1, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 1000000, - excess_data: Vec::new() - }); - update_channel(&gossip_sync, &secp_ctx, &privkeys[5], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), - short_channel_id: 7, - timestamp: 1, - flags: 1, - cltv_expiry_delta: (7 << 4) | 2, - htlc_minimum_msat: 0, - htlc_maximum_msat: MAX_VALUE_MSAT, - fee_base_msat: 0, - fee_proportional_millionths: 0, - excess_data: Vec::new() - }); - - add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[5], NodeFeatures::from_le_bytes(id_to_feature_flags(6)), 0); - - (secp_ctx, network_graph, gossip_sync, chain_monitor, logger) - } - #[test] fn simple_route_test() { let (secp_ctx, network_graph, _, _, logger) = build_graph(); diff --git a/lightning/src/routing/test_utils.rs b/lightning/src/routing/test_utils.rs new file mode 100644 index 000000000..f1a853f0a --- /dev/null +++ b/lightning/src/routing/test_utils.rs @@ -0,0 +1,497 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + +use routing::gossip::{NetworkGraph, P2PGossipSync}; +use ln::features::{ChannelFeatures, NodeFeatures}; +use ln::msgs::{UnsignedChannelAnnouncement, ChannelAnnouncement, RoutingMessageHandler, + NodeAnnouncement, UnsignedNodeAnnouncement, ChannelUpdate, UnsignedChannelUpdate, MAX_VALUE_MSAT}; +use util::test_utils; +use util::ser::Writeable; + +use bitcoin::hashes::sha256d::Hash as Sha256dHash; +use bitcoin::hashes::Hash; +use bitcoin::network::constants::Network; +use bitcoin::blockdata::constants::genesis_block; + +use hex; + +use bitcoin::secp256k1::{PublicKey,SecretKey}; +use bitcoin::secp256k1::{Secp256k1, All}; + +use prelude::*; +use sync::{self, Arc}; + +// Using the same keys for LN and BTC ids +pub(super) fn add_channel( + gossip_sync: &P2PGossipSync>>, Arc, Arc>, + secp_ctx: &Secp256k1, node_1_privkey: &SecretKey, node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64 +) { + let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); + let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); + + let unsigned_announcement = UnsignedChannelAnnouncement { + features, + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id, + node_id_1, + node_id_2, + bitcoin_key_1: node_id_1, + bitcoin_key_2: node_id_2, + excess_data: Vec::new(), + }; + + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = ChannelAnnouncement { + node_signature_1: secp_ctx.sign_ecdsa(&msghash, node_1_privkey), + node_signature_2: secp_ctx.sign_ecdsa(&msghash, node_2_privkey), + bitcoin_signature_1: secp_ctx.sign_ecdsa(&msghash, node_1_privkey), + bitcoin_signature_2: secp_ctx.sign_ecdsa(&msghash, node_2_privkey), + contents: unsigned_announcement.clone(), + }; + match gossip_sync.handle_channel_announcement(&valid_announcement) { + Ok(res) => assert!(res), + _ => panic!() + }; +} + +pub(super) fn add_or_update_node( + gossip_sync: &P2PGossipSync>>, Arc, Arc>, + secp_ctx: &Secp256k1, node_privkey: &SecretKey, features: NodeFeatures, timestamp: u32 +) { + let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey); + let unsigned_announcement = UnsignedNodeAnnouncement { + features, + timestamp, + node_id, + rgb: [0; 3], + alias: [0; 32], + addresses: Vec::new(), + excess_address_data: Vec::new(), + excess_data: Vec::new(), + }; + let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]); + let valid_announcement = NodeAnnouncement { + signature: secp_ctx.sign_ecdsa(&msghash, node_privkey), + contents: unsigned_announcement.clone() + }; + + match gossip_sync.handle_node_announcement(&valid_announcement) { + Ok(_) => (), + Err(_) => panic!() + }; +} + +pub(super) fn update_channel( + gossip_sync: &P2PGossipSync>>, Arc, Arc>, + secp_ctx: &Secp256k1, node_privkey: &SecretKey, update: UnsignedChannelUpdate +) { + let msghash = hash_to_message!(&Sha256dHash::hash(&update.encode()[..])[..]); + let valid_channel_update = ChannelUpdate { + signature: secp_ctx.sign_ecdsa(&msghash, node_privkey), + contents: update.clone() + }; + + match gossip_sync.handle_channel_update(&valid_channel_update) { + Ok(res) => assert!(res), + Err(_) => panic!() + }; +} + +pub(super) fn get_nodes(secp_ctx: &Secp256k1) -> (SecretKey, PublicKey, Vec, Vec) { + let privkeys: Vec = (2..22).map(|i| { + SecretKey::from_slice(&hex::decode(format!("{:02x}", i).repeat(32)).unwrap()[..]).unwrap() + }).collect(); + + let pubkeys = privkeys.iter().map(|secret| PublicKey::from_secret_key(&secp_ctx, secret)).collect(); + + let our_privkey = SecretKey::from_slice(&hex::decode("01".repeat(32)).unwrap()[..]).unwrap(); + let our_id = PublicKey::from_secret_key(&secp_ctx, &our_privkey); + + (our_privkey, our_id, privkeys, pubkeys) +} + +pub(super) fn id_to_feature_flags(id: u8) -> Vec { + // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can + // test for it later. + let idx = (id - 1) * 2 + 1; + if idx > 8*3 { + vec![1 << (idx - 8*3), 0, 0, 0] + } else if idx > 8*2 { + vec![1 << (idx - 8*2), 0, 0] + } else if idx > 8*1 { + vec![1 << (idx - 8*1), 0] + } else { + vec![1 << idx] + } +} + +pub(super) fn build_line_graph() -> ( + Secp256k1, sync::Arc>>, + P2PGossipSync>>, sync::Arc, sync::Arc>, + sync::Arc, sync::Arc, +) { + let secp_ctx = Secp256k1::new(); + let logger = Arc::new(test_utils::TestLogger::new()); + let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet)); + let genesis_hash = genesis_block(Network::Testnet).header.block_hash(); + let network_graph = Arc::new(NetworkGraph::new(genesis_hash, Arc::clone(&logger))); + let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger)); + + // Build network from our_id to node 19: + // our_id -1(1)2- node0 -1(2)2- node1 - ... - node19 + let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx); + + for (idx, (cur_privkey, next_privkey)) in core::iter::once(&our_privkey) + .chain(privkeys.iter()).zip(privkeys.iter()).enumerate() { + let cur_short_channel_id = (idx as u64) + 1; + add_channel(&gossip_sync, &secp_ctx, &cur_privkey, &next_privkey, + ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), cur_short_channel_id); + update_channel(&gossip_sync, &secp_ctx, &cur_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: cur_short_channel_id, + timestamp: idx as u32, + flags: 0, + cltv_expiry_delta: 0, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + update_channel(&gossip_sync, &secp_ctx, &next_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: cur_short_channel_id, + timestamp: (idx as u32)+1, + flags: 1, + cltv_expiry_delta: 0, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + add_or_update_node(&gossip_sync, &secp_ctx, &next_privkey, + NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0); + } + + (secp_ctx, network_graph, gossip_sync, chain_monitor, logger) +} + +pub(super) fn build_graph() -> ( + Secp256k1, + sync::Arc>>, + P2PGossipSync>>, sync::Arc, sync::Arc>, + sync::Arc, + sync::Arc, +) { + let secp_ctx = Secp256k1::new(); + let logger = Arc::new(test_utils::TestLogger::new()); + let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet)); + let genesis_hash = genesis_block(Network::Testnet).header.block_hash(); + let network_graph = Arc::new(NetworkGraph::new(genesis_hash, Arc::clone(&logger))); + let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger)); + // Build network from our_id to node6: + // + // -1(1)2- node0 -1(3)2- + // / \ + // our_id -1(12)2- node7 -1(13)2--- node2 + // \ / + // -1(2)2- node1 -1(4)2- + // + // + // chan1 1-to-2: disabled + // chan1 2-to-1: enabled, 0 fee + // + // chan2 1-to-2: enabled, ignored fee + // chan2 2-to-1: enabled, 0 fee + // + // chan3 1-to-2: enabled, 0 fee + // chan3 2-to-1: enabled, 100 msat fee + // + // chan4 1-to-2: enabled, 100% fee + // chan4 2-to-1: enabled, 0 fee + // + // chan12 1-to-2: enabled, ignored fee + // chan12 2-to-1: enabled, 0 fee + // + // chan13 1-to-2: enabled, 200% fee + // chan13 2-to-1: enabled, 0 fee + // + // + // -1(5)2- node3 -1(8)2-- + // | 2 | + // | (11) | + // / 1 \ + // node2--1(6)2- node4 -1(9)2--- node6 (not in global route map) + // \ / + // -1(7)2- node5 -1(10)2- + // + // Channels 5, 8, 9 and 10 are private channels. + // + // chan5 1-to-2: enabled, 100 msat fee + // chan5 2-to-1: enabled, 0 fee + // + // chan6 1-to-2: enabled, 0 fee + // chan6 2-to-1: enabled, 0 fee + // + // chan7 1-to-2: enabled, 100% fee + // chan7 2-to-1: enabled, 0 fee + // + // chan8 1-to-2: enabled, variable fee (0 then 1000 msat) + // chan8 2-to-1: enabled, 0 fee + // + // chan9 1-to-2: enabled, 1001 msat fee + // chan9 2-to-1: enabled, 0 fee + // + // chan10 1-to-2: enabled, 0 fee + // chan10 2-to-1: enabled, 0 fee + // + // chan11 1-to-2: enabled, 0 fee + // chan11 2-to-1: enabled, 0 fee + + let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx); + + add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[0], ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), 1); + update_channel(&gossip_sync, &secp_ctx, &privkeys[0], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 1, + timestamp: 1, + flags: 1, + cltv_expiry_delta: 0, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + + add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[0], NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0); + + add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[1], ChannelFeatures::from_le_bytes(id_to_feature_flags(2)), 2); + update_channel(&gossip_sync, &secp_ctx, &our_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 2, + timestamp: 1, + flags: 0, + cltv_expiry_delta: (5 << 4) | 3, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: u32::max_value(), + fee_proportional_millionths: u32::max_value(), + excess_data: Vec::new() + }); + update_channel(&gossip_sync, &secp_ctx, &privkeys[1], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 2, + timestamp: 1, + flags: 1, + cltv_expiry_delta: 0, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + + add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[1], NodeFeatures::from_le_bytes(id_to_feature_flags(2)), 0); + + add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[7], ChannelFeatures::from_le_bytes(id_to_feature_flags(12)), 12); + update_channel(&gossip_sync, &secp_ctx, &our_privkey, UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 12, + timestamp: 1, + flags: 0, + cltv_expiry_delta: (5 << 4) | 3, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: u32::max_value(), + fee_proportional_millionths: u32::max_value(), + excess_data: Vec::new() + }); + update_channel(&gossip_sync, &secp_ctx, &privkeys[7], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 12, + timestamp: 1, + flags: 1, + cltv_expiry_delta: 0, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + + add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[7], NodeFeatures::from_le_bytes(id_to_feature_flags(8)), 0); + + add_channel(&gossip_sync, &secp_ctx, &privkeys[0], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(3)), 3); + update_channel(&gossip_sync, &secp_ctx, &privkeys[0], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 3, + timestamp: 1, + flags: 0, + cltv_expiry_delta: (3 << 4) | 1, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 3, + timestamp: 1, + flags: 1, + cltv_expiry_delta: (3 << 4) | 2, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 100, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + + add_channel(&gossip_sync, &secp_ctx, &privkeys[1], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(4)), 4); + update_channel(&gossip_sync, &secp_ctx, &privkeys[1], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 4, + timestamp: 1, + flags: 0, + cltv_expiry_delta: (4 << 4) | 1, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 1000000, + excess_data: Vec::new() + }); + update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 4, + timestamp: 1, + flags: 1, + cltv_expiry_delta: (4 << 4) | 2, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + + add_channel(&gossip_sync, &secp_ctx, &privkeys[7], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(13)), 13); + update_channel(&gossip_sync, &secp_ctx, &privkeys[7], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 13, + timestamp: 1, + flags: 0, + cltv_expiry_delta: (13 << 4) | 1, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 2000000, + excess_data: Vec::new() + }); + update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 13, + timestamp: 1, + flags: 1, + cltv_expiry_delta: (13 << 4) | 2, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + + add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[2], NodeFeatures::from_le_bytes(id_to_feature_flags(3)), 0); + + add_channel(&gossip_sync, &secp_ctx, &privkeys[2], &privkeys[4], ChannelFeatures::from_le_bytes(id_to_feature_flags(6)), 6); + update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 6, + timestamp: 1, + flags: 0, + cltv_expiry_delta: (6 << 4) | 1, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + update_channel(&gossip_sync, &secp_ctx, &privkeys[4], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 6, + timestamp: 1, + flags: 1, + cltv_expiry_delta: (6 << 4) | 2, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new(), + }); + + add_channel(&gossip_sync, &secp_ctx, &privkeys[4], &privkeys[3], ChannelFeatures::from_le_bytes(id_to_feature_flags(11)), 11); + update_channel(&gossip_sync, &secp_ctx, &privkeys[4], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 11, + timestamp: 1, + flags: 0, + cltv_expiry_delta: (11 << 4) | 1, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + update_channel(&gossip_sync, &secp_ctx, &privkeys[3], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 11, + timestamp: 1, + flags: 1, + cltv_expiry_delta: (11 << 4) | 2, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + + add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[4], NodeFeatures::from_le_bytes(id_to_feature_flags(5)), 0); + + add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[3], NodeFeatures::from_le_bytes(id_to_feature_flags(4)), 0); + + add_channel(&gossip_sync, &secp_ctx, &privkeys[2], &privkeys[5], ChannelFeatures::from_le_bytes(id_to_feature_flags(7)), 7); + update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 7, + timestamp: 1, + flags: 0, + cltv_expiry_delta: (7 << 4) | 1, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 1000000, + excess_data: Vec::new() + }); + update_channel(&gossip_sync, &secp_ctx, &privkeys[5], UnsignedChannelUpdate { + chain_hash: genesis_block(Network::Testnet).header.block_hash(), + short_channel_id: 7, + timestamp: 1, + flags: 1, + cltv_expiry_delta: (7 << 4) | 2, + htlc_minimum_msat: 0, + htlc_maximum_msat: MAX_VALUE_MSAT, + fee_base_msat: 0, + fee_proportional_millionths: 0, + excess_data: Vec::new() + }); + + add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[5], NodeFeatures::from_le_bytes(id_to_feature_flags(6)), 0); + + (secp_ctx, network_graph, gossip_sync, chain_monitor, logger) +} -- 2.39.5