X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Frouting%2Ftest_utils.rs;h=3c0ef85fd7e3815696a669bc34203d411535f337;hb=e64342afab9c5aed1ed3592fcc2e2f3a38fac101;hp=a5d2af43c6ccbf90d04a6274d997c34689d03247;hpb=fc9a4c22d195a75ad5942eed271757f285452214;p=rust-lightning diff --git a/lightning/src/routing/test_utils.rs b/lightning/src/routing/test_utils.rs index a5d2af43..3c0ef85f 100644 --- a/lightning/src/routing/test_utils.rs +++ b/lightning/src/routing/test_utils.rs @@ -7,37 +7,37 @@ // You may not use this file except in accordance with one or both of these // licenses. -use crate::routing::gossip::{NetworkGraph, P2PGossipSync}; +use crate::routing::gossip::{NetworkGraph, NodeAlias, P2PGossipSync}; use crate::ln::features::{ChannelFeatures, NodeFeatures}; use crate::ln::msgs::{UnsignedChannelAnnouncement, ChannelAnnouncement, RoutingMessageHandler, NodeAnnouncement, UnsignedNodeAnnouncement, ChannelUpdate, UnsignedChannelUpdate, MAX_VALUE_MSAT}; use crate::util::test_utils; use crate::util::ser::Writeable; +use bitcoin::blockdata::constants::ChainHash; use bitcoin::hashes::sha256d::Hash as Sha256dHash; use bitcoin::hashes::Hash; +use bitcoin::hashes::hex::FromHex; use bitcoin::network::constants::Network; -use bitcoin::blockdata::constants::genesis_block; - -use hex; - use bitcoin::secp256k1::{PublicKey,SecretKey}; use bitcoin::secp256k1::{Secp256k1, All}; use crate::prelude::*; use crate::sync::{self, Arc}; +use crate::routing::gossip::NodeId; + // 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 node_id_1 = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_1_privkey)); + let node_id_2 = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_2_privkey)); let unsigned_announcement = UnsignedChannelAnnouncement { features, - chain_hash: genesis_block(Network::Testnet).header.block_hash(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id, node_id_1, node_id_2, @@ -64,13 +64,13 @@ 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 node_id = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_privkey)); let unsigned_announcement = UnsignedNodeAnnouncement { features, timestamp, node_id, rgb: [0; 3], - alias: [0; 32], + alias: NodeAlias([0; 32]), addresses: Vec::new(), excess_address_data: Vec::new(), excess_data: Vec::new(), @@ -105,12 +105,12 @@ pub(super) fn update_channel( 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() + SecretKey::from_slice(&>::from_hex(&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_privkey = SecretKey::from_slice(&>::from_hex(&"01".repeat(32)).unwrap()[..]).unwrap(); let our_id = PublicKey::from_secret_key(&secp_ctx, &our_privkey); (our_privkey, our_id, privkeys, pubkeys) @@ -139,8 +139,7 @@ pub(super) fn build_line_graph() -> ( 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 network_graph = Arc::new(NetworkGraph::new(Network::Testnet, Arc::clone(&logger))); let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger)); // Build network from our_id to node 19: @@ -153,7 +152,7 @@ pub(super) fn build_line_graph() -> ( 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(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: cur_short_channel_id, timestamp: idx as u32, flags: 0, @@ -165,7 +164,7 @@ pub(super) fn build_line_graph() -> ( excess_data: Vec::new() }); update_channel(&gossip_sync, &secp_ctx, &next_privkey, UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: cur_short_channel_id, timestamp: (idx as u32)+1, flags: 1, @@ -193,8 +192,7 @@ pub(super) fn build_graph() -> ( 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 network_graph = Arc::new(NetworkGraph::new(Network::Testnet, Arc::clone(&logger))); let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger)); // Build network from our_id to node6: // @@ -259,7 +257,7 @@ pub(super) fn build_graph() -> ( 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(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 1, timestamp: 1, flags: 1, @@ -275,7 +273,7 @@ pub(super) fn build_graph() -> ( 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(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 2, timestamp: 1, flags: 0, @@ -287,7 +285,7 @@ pub(super) fn build_graph() -> ( excess_data: Vec::new() }); update_channel(&gossip_sync, &secp_ctx, &privkeys[1], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 2, timestamp: 1, flags: 1, @@ -303,7 +301,7 @@ pub(super) fn build_graph() -> ( 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(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 12, timestamp: 1, flags: 0, @@ -315,7 +313,7 @@ pub(super) fn build_graph() -> ( excess_data: Vec::new() }); update_channel(&gossip_sync, &secp_ctx, &privkeys[7], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 12, timestamp: 1, flags: 1, @@ -331,7 +329,7 @@ pub(super) fn build_graph() -> ( 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(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 3, timestamp: 1, flags: 0, @@ -343,7 +341,7 @@ pub(super) fn build_graph() -> ( excess_data: Vec::new() }); update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 3, timestamp: 1, flags: 1, @@ -357,7 +355,7 @@ pub(super) fn build_graph() -> ( 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(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 4, timestamp: 1, flags: 0, @@ -369,7 +367,7 @@ pub(super) fn build_graph() -> ( excess_data: Vec::new() }); update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 4, timestamp: 1, flags: 1, @@ -383,7 +381,7 @@ pub(super) fn build_graph() -> ( 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(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 13, timestamp: 1, flags: 0, @@ -395,7 +393,7 @@ pub(super) fn build_graph() -> ( excess_data: Vec::new() }); update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 13, timestamp: 1, flags: 1, @@ -411,7 +409,7 @@ pub(super) fn build_graph() -> ( 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(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 6, timestamp: 1, flags: 0, @@ -423,7 +421,7 @@ pub(super) fn build_graph() -> ( excess_data: Vec::new() }); update_channel(&gossip_sync, &secp_ctx, &privkeys[4], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 6, timestamp: 1, flags: 1, @@ -437,7 +435,7 @@ pub(super) fn build_graph() -> ( 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(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 11, timestamp: 1, flags: 0, @@ -449,7 +447,7 @@ pub(super) fn build_graph() -> ( excess_data: Vec::new() }); update_channel(&gossip_sync, &secp_ctx, &privkeys[3], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 11, timestamp: 1, flags: 1, @@ -467,7 +465,7 @@ pub(super) fn build_graph() -> ( 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(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 7, timestamp: 1, flags: 0, @@ -479,7 +477,7 @@ pub(super) fn build_graph() -> ( excess_data: Vec::new() }); update_channel(&gossip_sync, &secp_ctx, &privkeys[5], UnsignedChannelUpdate { - chain_hash: genesis_block(Network::Testnet).header.block_hash(), + chain_hash: ChainHash::using_genesis_block(Network::Testnet), short_channel_id: 7, timestamp: 1, flags: 1,