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, P2PGossipSync};
11 use crate::ln::features::{ChannelFeatures, NodeFeatures};
12 use crate::ln::msgs::{UnsignedChannelAnnouncement, ChannelAnnouncement, RoutingMessageHandler,
13 NodeAnnouncement, UnsignedNodeAnnouncement, ChannelUpdate, UnsignedChannelUpdate, MAX_VALUE_MSAT};
14 use crate::util::test_utils;
15 use crate::util::ser::Writeable;
17 use bitcoin::hashes::sha256d::Hash as Sha256dHash;
18 use bitcoin::hashes::Hash;
19 use bitcoin::network::constants::Network;
20 use bitcoin::blockdata::constants::genesis_block;
24 use bitcoin::secp256k1::{PublicKey,SecretKey};
25 use bitcoin::secp256k1::{Secp256k1, All};
27 use crate::prelude::*;
28 use crate::sync::{self, Arc};
30 use crate::routing::gossip::NodeId;
32 // Using the same keys for LN and BTC ids
33 pub(super) fn add_channel(
34 gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
35 secp_ctx: &Secp256k1<All>, node_1_privkey: &SecretKey, node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64
37 let node_id_1 = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_1_privkey));
38 let node_id_2 = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_2_privkey));
40 let unsigned_announcement = UnsignedChannelAnnouncement {
42 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
46 bitcoin_key_1: node_id_1,
47 bitcoin_key_2: node_id_2,
48 excess_data: Vec::new(),
51 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
52 let valid_announcement = ChannelAnnouncement {
53 node_signature_1: secp_ctx.sign_ecdsa(&msghash, node_1_privkey),
54 node_signature_2: secp_ctx.sign_ecdsa(&msghash, node_2_privkey),
55 bitcoin_signature_1: secp_ctx.sign_ecdsa(&msghash, node_1_privkey),
56 bitcoin_signature_2: secp_ctx.sign_ecdsa(&msghash, node_2_privkey),
57 contents: unsigned_announcement.clone(),
59 match gossip_sync.handle_channel_announcement(&valid_announcement) {
60 Ok(res) => assert!(res),
65 pub(super) fn add_or_update_node(
66 gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
67 secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, features: NodeFeatures, timestamp: u32
69 let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
70 let unsigned_announcement = UnsignedNodeAnnouncement {
76 addresses: Vec::new(),
77 excess_address_data: Vec::new(),
78 excess_data: Vec::new(),
80 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
81 let valid_announcement = NodeAnnouncement {
82 signature: secp_ctx.sign_ecdsa(&msghash, node_privkey),
83 contents: unsigned_announcement.clone()
86 match gossip_sync.handle_node_announcement(&valid_announcement) {
92 pub(super) fn update_channel(
93 gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
94 secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, update: UnsignedChannelUpdate
96 let msghash = hash_to_message!(&Sha256dHash::hash(&update.encode()[..])[..]);
97 let valid_channel_update = ChannelUpdate {
98 signature: secp_ctx.sign_ecdsa(&msghash, node_privkey),
99 contents: update.clone()
102 match gossip_sync.handle_channel_update(&valid_channel_update) {
103 Ok(res) => assert!(res),
108 pub(super) fn get_nodes(secp_ctx: &Secp256k1<All>) -> (SecretKey, PublicKey, Vec<SecretKey>, Vec<PublicKey>) {
109 let privkeys: Vec<SecretKey> = (2..22).map(|i| {
110 SecretKey::from_slice(&hex::decode(format!("{:02x}", i).repeat(32)).unwrap()[..]).unwrap()
113 let pubkeys = privkeys.iter().map(|secret| PublicKey::from_secret_key(&secp_ctx, secret)).collect();
115 let our_privkey = SecretKey::from_slice(&hex::decode("01".repeat(32)).unwrap()[..]).unwrap();
116 let our_id = PublicKey::from_secret_key(&secp_ctx, &our_privkey);
118 (our_privkey, our_id, privkeys, pubkeys)
121 pub(super) fn id_to_feature_flags(id: u8) -> Vec<u8> {
122 // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can
123 // test for it later.
124 let idx = (id - 1) * 2 + 1;
126 vec![1 << (idx - 8*3), 0, 0, 0]
127 } else if idx > 8*2 {
128 vec![1 << (idx - 8*2), 0, 0]
129 } else if idx > 8*1 {
130 vec![1 << (idx - 8*1), 0]
136 pub(super) fn build_line_graph() -> (
137 Secp256k1<All>, sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
138 P2PGossipSync<sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>>,
139 sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>,
141 let secp_ctx = Secp256k1::new();
142 let logger = Arc::new(test_utils::TestLogger::new());
143 let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
144 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
145 let network_graph = Arc::new(NetworkGraph::new(genesis_hash, Arc::clone(&logger)));
146 let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger));
148 // Build network from our_id to node 19:
149 // our_id -1(1)2- node0 -1(2)2- node1 - ... - node19
150 let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx);
152 for (idx, (cur_privkey, next_privkey)) in core::iter::once(&our_privkey)
153 .chain(privkeys.iter()).zip(privkeys.iter()).enumerate() {
154 let cur_short_channel_id = (idx as u64) + 1;
155 add_channel(&gossip_sync, &secp_ctx, &cur_privkey, &next_privkey,
156 ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), cur_short_channel_id);
157 update_channel(&gossip_sync, &secp_ctx, &cur_privkey, UnsignedChannelUpdate {
158 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
159 short_channel_id: cur_short_channel_id,
160 timestamp: idx as u32,
162 cltv_expiry_delta: 0,
163 htlc_minimum_msat: 0,
164 htlc_maximum_msat: MAX_VALUE_MSAT,
166 fee_proportional_millionths: 0,
167 excess_data: Vec::new()
169 update_channel(&gossip_sync, &secp_ctx, &next_privkey, UnsignedChannelUpdate {
170 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
171 short_channel_id: cur_short_channel_id,
172 timestamp: (idx as u32)+1,
174 cltv_expiry_delta: 0,
175 htlc_minimum_msat: 0,
176 htlc_maximum_msat: MAX_VALUE_MSAT,
178 fee_proportional_millionths: 0,
179 excess_data: Vec::new()
181 add_or_update_node(&gossip_sync, &secp_ctx, &next_privkey,
182 NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0);
185 (secp_ctx, network_graph, gossip_sync, chain_monitor, logger)
188 pub(super) fn build_graph() -> (
190 sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
191 P2PGossipSync<sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>>,
192 sync::Arc<test_utils::TestChainSource>,
193 sync::Arc<test_utils::TestLogger>,
195 let secp_ctx = Secp256k1::new();
196 let logger = Arc::new(test_utils::TestLogger::new());
197 let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
198 let genesis_hash = genesis_block(Network::Testnet).header.block_hash();
199 let network_graph = Arc::new(NetworkGraph::new(genesis_hash, Arc::clone(&logger)));
200 let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger));
201 // Build network from our_id to node6:
203 // -1(1)2- node0 -1(3)2-
205 // our_id -1(12)2- node7 -1(13)2--- node2
207 // -1(2)2- node1 -1(4)2-
210 // chan1 1-to-2: disabled
211 // chan1 2-to-1: enabled, 0 fee
213 // chan2 1-to-2: enabled, ignored fee
214 // chan2 2-to-1: enabled, 0 fee
216 // chan3 1-to-2: enabled, 0 fee
217 // chan3 2-to-1: enabled, 100 msat fee
219 // chan4 1-to-2: enabled, 100% fee
220 // chan4 2-to-1: enabled, 0 fee
222 // chan12 1-to-2: enabled, ignored fee
223 // chan12 2-to-1: enabled, 0 fee
225 // chan13 1-to-2: enabled, 200% fee
226 // chan13 2-to-1: enabled, 0 fee
229 // -1(5)2- node3 -1(8)2--
233 // node2--1(6)2- node4 -1(9)2--- node6 (not in global route map)
235 // -1(7)2- node5 -1(10)2-
237 // Channels 5, 8, 9 and 10 are private channels.
239 // chan5 1-to-2: enabled, 100 msat fee
240 // chan5 2-to-1: enabled, 0 fee
242 // chan6 1-to-2: enabled, 0 fee
243 // chan6 2-to-1: enabled, 0 fee
245 // chan7 1-to-2: enabled, 100% fee
246 // chan7 2-to-1: enabled, 0 fee
248 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
249 // chan8 2-to-1: enabled, 0 fee
251 // chan9 1-to-2: enabled, 1001 msat fee
252 // chan9 2-to-1: enabled, 0 fee
254 // chan10 1-to-2: enabled, 0 fee
255 // chan10 2-to-1: enabled, 0 fee
257 // chan11 1-to-2: enabled, 0 fee
258 // chan11 2-to-1: enabled, 0 fee
260 let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx);
262 add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[0], ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), 1);
263 update_channel(&gossip_sync, &secp_ctx, &privkeys[0], UnsignedChannelUpdate {
264 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
268 cltv_expiry_delta: 0,
269 htlc_minimum_msat: 0,
270 htlc_maximum_msat: MAX_VALUE_MSAT,
272 fee_proportional_millionths: 0,
273 excess_data: Vec::new()
276 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[0], NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0);
278 add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[1], ChannelFeatures::from_le_bytes(id_to_feature_flags(2)), 2);
279 update_channel(&gossip_sync, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
280 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
284 cltv_expiry_delta: (5 << 4) | 3,
285 htlc_minimum_msat: 0,
286 htlc_maximum_msat: MAX_VALUE_MSAT,
287 fee_base_msat: u32::max_value(),
288 fee_proportional_millionths: u32::max_value(),
289 excess_data: Vec::new()
291 update_channel(&gossip_sync, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
292 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
296 cltv_expiry_delta: 0,
297 htlc_minimum_msat: 0,
298 htlc_maximum_msat: MAX_VALUE_MSAT,
300 fee_proportional_millionths: 0,
301 excess_data: Vec::new()
304 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[1], NodeFeatures::from_le_bytes(id_to_feature_flags(2)), 0);
306 add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[7], ChannelFeatures::from_le_bytes(id_to_feature_flags(12)), 12);
307 update_channel(&gossip_sync, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
308 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
309 short_channel_id: 12,
312 cltv_expiry_delta: (5 << 4) | 3,
313 htlc_minimum_msat: 0,
314 htlc_maximum_msat: MAX_VALUE_MSAT,
315 fee_base_msat: u32::max_value(),
316 fee_proportional_millionths: u32::max_value(),
317 excess_data: Vec::new()
319 update_channel(&gossip_sync, &secp_ctx, &privkeys[7], UnsignedChannelUpdate {
320 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
321 short_channel_id: 12,
324 cltv_expiry_delta: 0,
325 htlc_minimum_msat: 0,
326 htlc_maximum_msat: MAX_VALUE_MSAT,
328 fee_proportional_millionths: 0,
329 excess_data: Vec::new()
332 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[7], NodeFeatures::from_le_bytes(id_to_feature_flags(8)), 0);
334 add_channel(&gossip_sync, &secp_ctx, &privkeys[0], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(3)), 3);
335 update_channel(&gossip_sync, &secp_ctx, &privkeys[0], UnsignedChannelUpdate {
336 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
340 cltv_expiry_delta: (3 << 4) | 1,
341 htlc_minimum_msat: 0,
342 htlc_maximum_msat: MAX_VALUE_MSAT,
344 fee_proportional_millionths: 0,
345 excess_data: Vec::new()
347 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
348 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
352 cltv_expiry_delta: (3 << 4) | 2,
353 htlc_minimum_msat: 0,
354 htlc_maximum_msat: MAX_VALUE_MSAT,
356 fee_proportional_millionths: 0,
357 excess_data: Vec::new()
360 add_channel(&gossip_sync, &secp_ctx, &privkeys[1], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(4)), 4);
361 update_channel(&gossip_sync, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
362 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
366 cltv_expiry_delta: (4 << 4) | 1,
367 htlc_minimum_msat: 0,
368 htlc_maximum_msat: MAX_VALUE_MSAT,
370 fee_proportional_millionths: 1000000,
371 excess_data: Vec::new()
373 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
374 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
378 cltv_expiry_delta: (4 << 4) | 2,
379 htlc_minimum_msat: 0,
380 htlc_maximum_msat: MAX_VALUE_MSAT,
382 fee_proportional_millionths: 0,
383 excess_data: Vec::new()
386 add_channel(&gossip_sync, &secp_ctx, &privkeys[7], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(13)), 13);
387 update_channel(&gossip_sync, &secp_ctx, &privkeys[7], UnsignedChannelUpdate {
388 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
389 short_channel_id: 13,
392 cltv_expiry_delta: (13 << 4) | 1,
393 htlc_minimum_msat: 0,
394 htlc_maximum_msat: MAX_VALUE_MSAT,
396 fee_proportional_millionths: 2000000,
397 excess_data: Vec::new()
399 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
400 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
401 short_channel_id: 13,
404 cltv_expiry_delta: (13 << 4) | 2,
405 htlc_minimum_msat: 0,
406 htlc_maximum_msat: MAX_VALUE_MSAT,
408 fee_proportional_millionths: 0,
409 excess_data: Vec::new()
412 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[2], NodeFeatures::from_le_bytes(id_to_feature_flags(3)), 0);
414 add_channel(&gossip_sync, &secp_ctx, &privkeys[2], &privkeys[4], ChannelFeatures::from_le_bytes(id_to_feature_flags(6)), 6);
415 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
416 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
420 cltv_expiry_delta: (6 << 4) | 1,
421 htlc_minimum_msat: 0,
422 htlc_maximum_msat: MAX_VALUE_MSAT,
424 fee_proportional_millionths: 0,
425 excess_data: Vec::new()
427 update_channel(&gossip_sync, &secp_ctx, &privkeys[4], UnsignedChannelUpdate {
428 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
432 cltv_expiry_delta: (6 << 4) | 2,
433 htlc_minimum_msat: 0,
434 htlc_maximum_msat: MAX_VALUE_MSAT,
436 fee_proportional_millionths: 0,
437 excess_data: Vec::new(),
440 add_channel(&gossip_sync, &secp_ctx, &privkeys[4], &privkeys[3], ChannelFeatures::from_le_bytes(id_to_feature_flags(11)), 11);
441 update_channel(&gossip_sync, &secp_ctx, &privkeys[4], UnsignedChannelUpdate {
442 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
443 short_channel_id: 11,
446 cltv_expiry_delta: (11 << 4) | 1,
447 htlc_minimum_msat: 0,
448 htlc_maximum_msat: MAX_VALUE_MSAT,
450 fee_proportional_millionths: 0,
451 excess_data: Vec::new()
453 update_channel(&gossip_sync, &secp_ctx, &privkeys[3], UnsignedChannelUpdate {
454 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
455 short_channel_id: 11,
458 cltv_expiry_delta: (11 << 4) | 2,
459 htlc_minimum_msat: 0,
460 htlc_maximum_msat: MAX_VALUE_MSAT,
462 fee_proportional_millionths: 0,
463 excess_data: Vec::new()
466 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[4], NodeFeatures::from_le_bytes(id_to_feature_flags(5)), 0);
468 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[3], NodeFeatures::from_le_bytes(id_to_feature_flags(4)), 0);
470 add_channel(&gossip_sync, &secp_ctx, &privkeys[2], &privkeys[5], ChannelFeatures::from_le_bytes(id_to_feature_flags(7)), 7);
471 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
472 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
476 cltv_expiry_delta: (7 << 4) | 1,
477 htlc_minimum_msat: 0,
478 htlc_maximum_msat: MAX_VALUE_MSAT,
480 fee_proportional_millionths: 1000000,
481 excess_data: Vec::new()
483 update_channel(&gossip_sync, &secp_ctx, &privkeys[5], UnsignedChannelUpdate {
484 chain_hash: genesis_block(Network::Testnet).header.block_hash(),
488 cltv_expiry_delta: (7 << 4) | 2,
489 htlc_minimum_msat: 0,
490 htlc_maximum_msat: MAX_VALUE_MSAT,
492 fee_proportional_millionths: 0,
493 excess_data: Vec::new()
496 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[5], NodeFeatures::from_le_bytes(id_to_feature_flags(6)), 0);
498 (secp_ctx, network_graph, gossip_sync, chain_monitor, logger)