1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 use crate::routing::gossip::{NetworkGraph, NodeAlias, P2PGossipSync};
11 use crate::ln::features::{ChannelFeatures, NodeFeatures};
12 use crate::ln::msgs::{ChannelAnnouncement, ChannelUpdate, MAX_VALUE_MSAT, NodeAnnouncement, RoutingMessageHandler, SocketAddress, UnsignedChannelAnnouncement, UnsignedChannelUpdate, UnsignedNodeAnnouncement};
13 use crate::util::test_utils;
14 use crate::util::ser::Writeable;
16 use bitcoin::blockdata::constants::ChainHash;
17 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
18 use bitcoin::hashes::Hash;
19 use bitcoin::hashes::hex::FromHex;
20 use bitcoin::network::constants::Network;
21 use bitcoin::secp256k1::{PublicKey,SecretKey};
22 use bitcoin::secp256k1::{Secp256k1, All};
24 use crate::prelude::*;
25 use crate::sync::{self, Arc};
27 use crate::routing::gossip::NodeId;
29 // Using the same keys for LN and BTC ids
30 pub(crate) fn add_channel(
31 gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
32 secp_ctx: &Secp256k1<All>, node_1_privkey: &SecretKey, node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64
34 let node_id_1 = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_1_privkey));
35 let node_id_2 = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_2_privkey));
37 let unsigned_announcement = UnsignedChannelAnnouncement {
39 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
43 bitcoin_key_1: node_id_1,
44 bitcoin_key_2: node_id_2,
45 excess_data: Vec::new(),
48 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
49 let valid_announcement = ChannelAnnouncement {
50 node_signature_1: secp_ctx.sign_ecdsa(&msghash, node_1_privkey),
51 node_signature_2: secp_ctx.sign_ecdsa(&msghash, node_2_privkey),
52 bitcoin_signature_1: secp_ctx.sign_ecdsa(&msghash, node_1_privkey),
53 bitcoin_signature_2: secp_ctx.sign_ecdsa(&msghash, node_2_privkey),
54 contents: unsigned_announcement.clone(),
56 match gossip_sync.handle_channel_announcement(&valid_announcement) {
57 Ok(res) => assert!(res),
62 pub(crate) fn add_or_update_node(
63 gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
64 secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, features: NodeFeatures, timestamp: u32
66 let node_id = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_privkey));
67 let unsigned_announcement = UnsignedNodeAnnouncement {
72 alias: NodeAlias([0; 32]),
73 addresses: vec![SocketAddress::TcpIpV4 { addr: [127, 0, 0, 1], port: 1000 }],
74 excess_address_data: Vec::new(),
75 excess_data: Vec::new(),
77 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
78 let valid_announcement = NodeAnnouncement {
79 signature: secp_ctx.sign_ecdsa(&msghash, node_privkey),
80 contents: unsigned_announcement.clone()
83 match gossip_sync.handle_node_announcement(&valid_announcement) {
89 pub(crate) fn update_channel(
90 gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
91 secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, update: UnsignedChannelUpdate
93 let msghash = hash_to_message!(&Sha256dHash::hash(&update.encode()[..])[..]);
94 let valid_channel_update = ChannelUpdate {
95 signature: secp_ctx.sign_ecdsa(&msghash, node_privkey),
96 contents: update.clone()
99 match gossip_sync.handle_channel_update(&valid_channel_update) {
100 Ok(res) => assert!(res),
105 pub(super) fn get_nodes(secp_ctx: &Secp256k1<All>) -> (SecretKey, PublicKey, Vec<SecretKey>, Vec<PublicKey>) {
106 let privkeys: Vec<SecretKey> = (2..22).map(|i| {
107 SecretKey::from_slice(&<Vec<u8>>::from_hex(&format!("{:02x}", i).repeat(32)).unwrap()[..]).unwrap()
110 let pubkeys = privkeys.iter().map(|secret| PublicKey::from_secret_key(&secp_ctx, secret)).collect();
112 let our_privkey = SecretKey::from_slice(&<Vec<u8>>::from_hex(&"01".repeat(32)).unwrap()[..]).unwrap();
113 let our_id = PublicKey::from_secret_key(&secp_ctx, &our_privkey);
115 (our_privkey, our_id, privkeys, pubkeys)
118 pub(super) fn id_to_feature_flags(id: u8) -> Vec<u8> {
119 // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can
120 // test for it later.
121 let idx = (id - 1) * 2 + 1;
123 vec![1 << (idx - 8*3), 0, 0, 0]
124 } else if idx > 8*2 {
125 vec![1 << (idx - 8*2), 0, 0]
126 } else if idx > 8*1 {
127 vec![1 << (idx - 8*1), 0]
133 pub(super) fn build_line_graph() -> (
134 Secp256k1<All>, sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
135 P2PGossipSync<sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>>,
136 sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>,
138 let secp_ctx = Secp256k1::new();
139 let logger = Arc::new(test_utils::TestLogger::new());
140 let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
141 let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, Arc::clone(&logger)));
142 let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger));
144 // Build network from our_id to node 19:
145 // our_id -1(1)2- node0 -1(2)2- node1 - ... - node19
146 let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx);
148 for (idx, (cur_privkey, next_privkey)) in core::iter::once(&our_privkey)
149 .chain(privkeys.iter()).zip(privkeys.iter()).enumerate() {
150 let cur_short_channel_id = (idx as u64) + 1;
151 add_channel(&gossip_sync, &secp_ctx, &cur_privkey, &next_privkey,
152 ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), cur_short_channel_id);
153 update_channel(&gossip_sync, &secp_ctx, &cur_privkey, UnsignedChannelUpdate {
154 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
155 short_channel_id: cur_short_channel_id,
156 timestamp: idx as u32,
158 cltv_expiry_delta: 0,
159 htlc_minimum_msat: 0,
160 htlc_maximum_msat: MAX_VALUE_MSAT,
162 fee_proportional_millionths: 0,
163 excess_data: Vec::new()
165 update_channel(&gossip_sync, &secp_ctx, &next_privkey, UnsignedChannelUpdate {
166 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
167 short_channel_id: cur_short_channel_id,
168 timestamp: (idx as u32)+1,
170 cltv_expiry_delta: 0,
171 htlc_minimum_msat: 0,
172 htlc_maximum_msat: MAX_VALUE_MSAT,
174 fee_proportional_millionths: 0,
175 excess_data: Vec::new()
177 add_or_update_node(&gossip_sync, &secp_ctx, &next_privkey,
178 NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0);
181 (secp_ctx, network_graph, gossip_sync, chain_monitor, logger)
184 pub(super) fn build_graph() -> (
186 sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
187 P2PGossipSync<sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>>,
188 sync::Arc<test_utils::TestChainSource>,
189 sync::Arc<test_utils::TestLogger>,
191 let secp_ctx = Secp256k1::new();
192 let logger = Arc::new(test_utils::TestLogger::new());
193 let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
194 let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, Arc::clone(&logger)));
195 let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger));
196 // Build network from our_id to node6:
198 // -1(1)2- node0 -1(3)2-
200 // our_id -1(12)2- node7 -1(13)2--- node2
202 // -1(2)2- node1 -1(4)2-
205 // chan1 1-to-2: disabled
206 // chan1 2-to-1: enabled, 0 fee
208 // chan2 1-to-2: enabled, ignored fee
209 // chan2 2-to-1: enabled, 0 fee
211 // chan3 1-to-2: enabled, 0 fee
212 // chan3 2-to-1: enabled, 100 msat fee
214 // chan4 1-to-2: enabled, 100% fee
215 // chan4 2-to-1: enabled, 0 fee
217 // chan12 1-to-2: enabled, ignored fee
218 // chan12 2-to-1: enabled, 0 fee
220 // chan13 1-to-2: enabled, 200% fee
221 // chan13 2-to-1: enabled, 0 fee
224 // -1(5)2- node3 -1(8)2--
228 // node2--1(6)2- node4 -1(9)2--- node6 (not in global route map)
230 // -1(7)2- node5 -1(10)2-
232 // Channels 5, 8, 9 and 10 are private channels.
234 // chan5 1-to-2: enabled, 100 msat fee
235 // chan5 2-to-1: enabled, 0 fee
237 // chan6 1-to-2: enabled, 0 fee
238 // chan6 2-to-1: enabled, 0 fee
240 // chan7 1-to-2: enabled, 100% fee
241 // chan7 2-to-1: enabled, 0 fee
243 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
244 // chan8 2-to-1: enabled, 0 fee
246 // chan9 1-to-2: enabled, 1001 msat fee
247 // chan9 2-to-1: enabled, 0 fee
249 // chan10 1-to-2: enabled, 0 fee
250 // chan10 2-to-1: enabled, 0 fee
252 // chan11 1-to-2: enabled, 0 fee
253 // chan11 2-to-1: enabled, 0 fee
255 let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx);
257 add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[0], ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), 1);
258 update_channel(&gossip_sync, &secp_ctx, &privkeys[0], UnsignedChannelUpdate {
259 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
263 cltv_expiry_delta: 0,
264 htlc_minimum_msat: 0,
265 htlc_maximum_msat: MAX_VALUE_MSAT,
267 fee_proportional_millionths: 0,
268 excess_data: Vec::new()
271 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[0], NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0);
273 add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[1], ChannelFeatures::from_le_bytes(id_to_feature_flags(2)), 2);
274 update_channel(&gossip_sync, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
275 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
279 cltv_expiry_delta: (5 << 4) | 3,
280 htlc_minimum_msat: 0,
281 htlc_maximum_msat: MAX_VALUE_MSAT,
282 fee_base_msat: u32::max_value(),
283 fee_proportional_millionths: u32::max_value(),
284 excess_data: Vec::new()
286 update_channel(&gossip_sync, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
287 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
291 cltv_expiry_delta: 0,
292 htlc_minimum_msat: 0,
293 htlc_maximum_msat: MAX_VALUE_MSAT,
295 fee_proportional_millionths: 0,
296 excess_data: Vec::new()
299 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[1], NodeFeatures::from_le_bytes(id_to_feature_flags(2)), 0);
301 add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[7], ChannelFeatures::from_le_bytes(id_to_feature_flags(12)), 12);
302 update_channel(&gossip_sync, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
303 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
304 short_channel_id: 12,
307 cltv_expiry_delta: (5 << 4) | 3,
308 htlc_minimum_msat: 0,
309 htlc_maximum_msat: MAX_VALUE_MSAT,
310 fee_base_msat: u32::max_value(),
311 fee_proportional_millionths: u32::max_value(),
312 excess_data: Vec::new()
314 update_channel(&gossip_sync, &secp_ctx, &privkeys[7], UnsignedChannelUpdate {
315 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
316 short_channel_id: 12,
319 cltv_expiry_delta: 0,
320 htlc_minimum_msat: 0,
321 htlc_maximum_msat: MAX_VALUE_MSAT,
323 fee_proportional_millionths: 0,
324 excess_data: Vec::new()
327 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[7], NodeFeatures::from_le_bytes(id_to_feature_flags(8)), 0);
329 add_channel(&gossip_sync, &secp_ctx, &privkeys[0], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(3)), 3);
330 update_channel(&gossip_sync, &secp_ctx, &privkeys[0], UnsignedChannelUpdate {
331 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
335 cltv_expiry_delta: (3 << 4) | 1,
336 htlc_minimum_msat: 0,
337 htlc_maximum_msat: MAX_VALUE_MSAT,
339 fee_proportional_millionths: 0,
340 excess_data: Vec::new()
342 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
343 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
347 cltv_expiry_delta: (3 << 4) | 2,
348 htlc_minimum_msat: 0,
349 htlc_maximum_msat: MAX_VALUE_MSAT,
351 fee_proportional_millionths: 0,
352 excess_data: Vec::new()
355 add_channel(&gossip_sync, &secp_ctx, &privkeys[1], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(4)), 4);
356 update_channel(&gossip_sync, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
357 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
361 cltv_expiry_delta: (4 << 4) | 1,
362 htlc_minimum_msat: 0,
363 htlc_maximum_msat: MAX_VALUE_MSAT,
365 fee_proportional_millionths: 1000000,
366 excess_data: Vec::new()
368 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
369 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
373 cltv_expiry_delta: (4 << 4) | 2,
374 htlc_minimum_msat: 0,
375 htlc_maximum_msat: MAX_VALUE_MSAT,
377 fee_proportional_millionths: 0,
378 excess_data: Vec::new()
381 add_channel(&gossip_sync, &secp_ctx, &privkeys[7], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(13)), 13);
382 update_channel(&gossip_sync, &secp_ctx, &privkeys[7], UnsignedChannelUpdate {
383 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
384 short_channel_id: 13,
387 cltv_expiry_delta: (13 << 4) | 1,
388 htlc_minimum_msat: 0,
389 htlc_maximum_msat: MAX_VALUE_MSAT,
391 fee_proportional_millionths: 2000000,
392 excess_data: Vec::new()
394 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
395 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
396 short_channel_id: 13,
399 cltv_expiry_delta: (13 << 4) | 2,
400 htlc_minimum_msat: 0,
401 htlc_maximum_msat: MAX_VALUE_MSAT,
403 fee_proportional_millionths: 0,
404 excess_data: Vec::new()
407 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[2], NodeFeatures::from_le_bytes(id_to_feature_flags(3)), 0);
409 add_channel(&gossip_sync, &secp_ctx, &privkeys[2], &privkeys[4], ChannelFeatures::from_le_bytes(id_to_feature_flags(6)), 6);
410 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
411 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
415 cltv_expiry_delta: (6 << 4) | 1,
416 htlc_minimum_msat: 0,
417 htlc_maximum_msat: MAX_VALUE_MSAT,
419 fee_proportional_millionths: 0,
420 excess_data: Vec::new()
422 update_channel(&gossip_sync, &secp_ctx, &privkeys[4], UnsignedChannelUpdate {
423 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
427 cltv_expiry_delta: (6 << 4) | 2,
428 htlc_minimum_msat: 0,
429 htlc_maximum_msat: MAX_VALUE_MSAT,
431 fee_proportional_millionths: 0,
432 excess_data: Vec::new(),
435 add_channel(&gossip_sync, &secp_ctx, &privkeys[4], &privkeys[3], ChannelFeatures::from_le_bytes(id_to_feature_flags(11)), 11);
436 update_channel(&gossip_sync, &secp_ctx, &privkeys[4], UnsignedChannelUpdate {
437 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
438 short_channel_id: 11,
441 cltv_expiry_delta: (11 << 4) | 1,
442 htlc_minimum_msat: 0,
443 htlc_maximum_msat: MAX_VALUE_MSAT,
445 fee_proportional_millionths: 0,
446 excess_data: Vec::new()
448 update_channel(&gossip_sync, &secp_ctx, &privkeys[3], UnsignedChannelUpdate {
449 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
450 short_channel_id: 11,
453 cltv_expiry_delta: (11 << 4) | 2,
454 htlc_minimum_msat: 0,
455 htlc_maximum_msat: MAX_VALUE_MSAT,
457 fee_proportional_millionths: 0,
458 excess_data: Vec::new()
461 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[4], NodeFeatures::from_le_bytes(id_to_feature_flags(5)), 0);
463 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[3], NodeFeatures::from_le_bytes(id_to_feature_flags(4)), 0);
465 add_channel(&gossip_sync, &secp_ctx, &privkeys[2], &privkeys[5], ChannelFeatures::from_le_bytes(id_to_feature_flags(7)), 7);
466 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
467 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
471 cltv_expiry_delta: (7 << 4) | 1,
472 htlc_minimum_msat: 0,
473 htlc_maximum_msat: MAX_VALUE_MSAT,
475 fee_proportional_millionths: 1000000,
476 excess_data: Vec::new()
478 update_channel(&gossip_sync, &secp_ctx, &privkeys[5], UnsignedChannelUpdate {
479 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
483 cltv_expiry_delta: (7 << 4) | 2,
484 htlc_minimum_msat: 0,
485 htlc_maximum_msat: MAX_VALUE_MSAT,
487 fee_proportional_millionths: 0,
488 excess_data: Vec::new()
491 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[5], NodeFeatures::from_le_bytes(id_to_feature_flags(6)), 0);
493 (secp_ctx, network_graph, gossip_sync, chain_monitor, logger)