X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Frouting%2Ftest_utils.rs;h=3c0ef85fd7e3815696a669bc34203d411535f337;hb=e64342afab9c5aed1ed3592fcc2e2f3a38fac101;hp=b737bab352441a1b2a9289cc143e078ba6384c97;hpb=33720b07a06e70cef099639d054aada6bb96a3d5;p=rust-lightning diff --git a/lightning/src/routing/test_utils.rs b/lightning/src/routing/test_utils.rs index b737bab3..3c0ef85f 100644 --- a/lightning/src/routing/test_utils.rs +++ b/lightning/src/routing/test_utils.rs @@ -7,20 +7,18 @@ // 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}; @@ -39,7 +37,7 @@ pub(super) fn add_channel( 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, @@ -72,7 +70,7 @@ pub(super) fn add_or_update_node( 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(), @@ -107,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) @@ -141,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: @@ -155,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, @@ -167,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, @@ -195,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: // @@ -261,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, @@ -277,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, @@ -289,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, @@ -305,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, @@ -317,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, @@ -333,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, @@ -345,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, @@ -359,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, @@ -371,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, @@ -385,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, @@ -397,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, @@ -413,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, @@ -425,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, @@ -439,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, @@ -451,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, @@ -469,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, @@ -481,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,