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};
25 use crate::prelude::*;
26 use crate::sync::{self, Arc};
28 use crate::routing::gossip::NodeId;
30 // Using the same keys for LN and BTC ids
31 pub(crate) fn add_channel(
32 gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
33 secp_ctx: &Secp256k1<All>, node_1_privkey: &SecretKey, node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64
35 let node_id_1 = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_1_privkey));
36 let node_id_2 = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_2_privkey));
38 let unsigned_announcement = UnsignedChannelAnnouncement {
40 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
44 bitcoin_key_1: node_id_1,
45 bitcoin_key_2: node_id_2,
46 excess_data: Vec::new(),
49 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
50 let valid_announcement = ChannelAnnouncement {
51 node_signature_1: secp_ctx.sign_ecdsa(&msghash, node_1_privkey),
52 node_signature_2: secp_ctx.sign_ecdsa(&msghash, node_2_privkey),
53 bitcoin_signature_1: secp_ctx.sign_ecdsa(&msghash, node_1_privkey),
54 bitcoin_signature_2: secp_ctx.sign_ecdsa(&msghash, node_2_privkey),
55 contents: unsigned_announcement.clone(),
57 match gossip_sync.handle_channel_announcement(&valid_announcement) {
58 Ok(res) => assert!(res),
63 pub(crate) fn add_or_update_node(
64 gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
65 secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, features: NodeFeatures, timestamp: u32
67 let node_id = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_privkey));
68 let unsigned_announcement = UnsignedNodeAnnouncement {
73 alias: NodeAlias([0; 32]),
74 addresses: vec![SocketAddress::TcpIpV4 { addr: [127, 0, 0, 1], port: 1000 }],
75 excess_address_data: Vec::new(),
76 excess_data: Vec::new(),
78 let msghash = hash_to_message!(&Sha256dHash::hash(&unsigned_announcement.encode()[..])[..]);
79 let valid_announcement = NodeAnnouncement {
80 signature: secp_ctx.sign_ecdsa(&msghash, node_privkey),
81 contents: unsigned_announcement.clone()
84 match gossip_sync.handle_node_announcement(&valid_announcement) {
90 pub(crate) fn update_channel(
91 gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
92 secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, update: UnsignedChannelUpdate
94 let msghash = hash_to_message!(&Sha256dHash::hash(&update.encode()[..])[..]);
95 let valid_channel_update = ChannelUpdate {
96 signature: secp_ctx.sign_ecdsa(&msghash, node_privkey),
97 contents: update.clone()
100 match gossip_sync.handle_channel_update(&valid_channel_update) {
101 Ok(res) => assert!(res),
106 pub(super) fn get_nodes(secp_ctx: &Secp256k1<All>) -> (SecretKey, PublicKey, Vec<SecretKey>, Vec<PublicKey>) {
107 let privkeys: Vec<SecretKey> = (2..22).map(|i| {
108 SecretKey::from_slice(&<Vec<u8>>::from_hex(&format!("{:02x}", i).repeat(32)).unwrap()[..]).unwrap()
111 let pubkeys = privkeys.iter().map(|secret| PublicKey::from_secret_key(&secp_ctx, secret)).collect();
113 let our_privkey = SecretKey::from_slice(&<Vec<u8>>::from_hex(&"01".repeat(32)).unwrap()[..]).unwrap();
114 let our_id = PublicKey::from_secret_key(&secp_ctx, &our_privkey);
116 (our_privkey, our_id, privkeys, pubkeys)
119 pub(super) fn id_to_feature_flags(id: u8) -> Vec<u8> {
120 // Set the feature flags to the id'th odd (ie non-required) feature bit so that we can
121 // test for it later.
122 let idx = (id - 1) * 2 + 1;
124 vec![1 << (idx - 8*3), 0, 0, 0]
125 } else if idx > 8*2 {
126 vec![1 << (idx - 8*2), 0, 0]
127 } else if idx > 8*1 {
128 vec![1 << (idx - 8*1), 0]
134 pub(super) fn build_line_graph() -> (
135 Secp256k1<All>, sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
136 P2PGossipSync<sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>>,
137 sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>,
139 let secp_ctx = Secp256k1::new();
140 let logger = Arc::new(test_utils::TestLogger::new());
141 let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
142 let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, Arc::clone(&logger)));
143 let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger));
145 // Build network from our_id to node 19:
146 // our_id -1(1)2- node0 -1(2)2- node1 - ... - node19
147 let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx);
149 for (idx, (cur_privkey, next_privkey)) in core::iter::once(&our_privkey)
150 .chain(privkeys.iter()).zip(privkeys.iter()).enumerate() {
151 let cur_short_channel_id = (idx as u64) + 1;
152 add_channel(&gossip_sync, &secp_ctx, &cur_privkey, &next_privkey,
153 ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), cur_short_channel_id);
154 update_channel(&gossip_sync, &secp_ctx, &cur_privkey, UnsignedChannelUpdate {
155 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
156 short_channel_id: cur_short_channel_id,
157 timestamp: idx as u32,
159 cltv_expiry_delta: 0,
160 htlc_minimum_msat: 0,
161 htlc_maximum_msat: MAX_VALUE_MSAT,
163 fee_proportional_millionths: 0,
164 excess_data: Vec::new()
166 update_channel(&gossip_sync, &secp_ctx, &next_privkey, UnsignedChannelUpdate {
167 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
168 short_channel_id: cur_short_channel_id,
169 timestamp: (idx as u32)+1,
171 cltv_expiry_delta: 0,
172 htlc_minimum_msat: 0,
173 htlc_maximum_msat: MAX_VALUE_MSAT,
175 fee_proportional_millionths: 0,
176 excess_data: Vec::new()
178 add_or_update_node(&gossip_sync, &secp_ctx, &next_privkey,
179 NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0);
182 (secp_ctx, network_graph, gossip_sync, chain_monitor, logger)
185 pub(super) fn build_graph() -> (
187 sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
188 P2PGossipSync<sync::Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, sync::Arc<test_utils::TestChainSource>, sync::Arc<test_utils::TestLogger>>,
189 sync::Arc<test_utils::TestChainSource>,
190 sync::Arc<test_utils::TestLogger>,
192 let secp_ctx = Secp256k1::new();
193 let logger = Arc::new(test_utils::TestLogger::new());
194 let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
195 let network_graph = Arc::new(NetworkGraph::new(Network::Testnet, Arc::clone(&logger)));
196 let gossip_sync = P2PGossipSync::new(Arc::clone(&network_graph), None, Arc::clone(&logger));
197 // Build network from our_id to node6:
199 // -1(1)2- node0 -1(3)2-
201 // our_id -1(12)2- node7 -1(13)2--- node2
203 // -1(2)2- node1 -1(4)2-
206 // chan1 1-to-2: disabled
207 // chan1 2-to-1: enabled, 0 fee
209 // chan2 1-to-2: enabled, ignored fee
210 // chan2 2-to-1: enabled, 0 fee
212 // chan3 1-to-2: enabled, 0 fee
213 // chan3 2-to-1: enabled, 100 msat fee
215 // chan4 1-to-2: enabled, 100% fee
216 // chan4 2-to-1: enabled, 0 fee
218 // chan12 1-to-2: enabled, ignored fee
219 // chan12 2-to-1: enabled, 0 fee
221 // chan13 1-to-2: enabled, 200% fee
222 // chan13 2-to-1: enabled, 0 fee
225 // -1(5)2- node3 -1(8)2--
229 // node2--1(6)2- node4 -1(9)2--- node6 (not in global route map)
231 // -1(7)2- node5 -1(10)2-
233 // Channels 5, 8, 9 and 10 are private channels.
235 // chan5 1-to-2: enabled, 100 msat fee
236 // chan5 2-to-1: enabled, 0 fee
238 // chan6 1-to-2: enabled, 0 fee
239 // chan6 2-to-1: enabled, 0 fee
241 // chan7 1-to-2: enabled, 100% fee
242 // chan7 2-to-1: enabled, 0 fee
244 // chan8 1-to-2: enabled, variable fee (0 then 1000 msat)
245 // chan8 2-to-1: enabled, 0 fee
247 // chan9 1-to-2: enabled, 1001 msat fee
248 // chan9 2-to-1: enabled, 0 fee
250 // chan10 1-to-2: enabled, 0 fee
251 // chan10 2-to-1: enabled, 0 fee
253 // chan11 1-to-2: enabled, 0 fee
254 // chan11 2-to-1: enabled, 0 fee
256 let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx);
258 add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[0], ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), 1);
259 update_channel(&gossip_sync, &secp_ctx, &privkeys[0], UnsignedChannelUpdate {
260 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
264 cltv_expiry_delta: 0,
265 htlc_minimum_msat: 0,
266 htlc_maximum_msat: MAX_VALUE_MSAT,
268 fee_proportional_millionths: 0,
269 excess_data: Vec::new()
272 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[0], NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0);
274 add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[1], ChannelFeatures::from_le_bytes(id_to_feature_flags(2)), 2);
275 update_channel(&gossip_sync, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
276 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
280 cltv_expiry_delta: (5 << 4) | 3,
281 htlc_minimum_msat: 0,
282 htlc_maximum_msat: MAX_VALUE_MSAT,
283 fee_base_msat: u32::max_value(),
284 fee_proportional_millionths: u32::max_value(),
285 excess_data: Vec::new()
287 update_channel(&gossip_sync, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
288 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
292 cltv_expiry_delta: 0,
293 htlc_minimum_msat: 0,
294 htlc_maximum_msat: MAX_VALUE_MSAT,
296 fee_proportional_millionths: 0,
297 excess_data: Vec::new()
300 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[1], NodeFeatures::from_le_bytes(id_to_feature_flags(2)), 0);
302 add_channel(&gossip_sync, &secp_ctx, &our_privkey, &privkeys[7], ChannelFeatures::from_le_bytes(id_to_feature_flags(12)), 12);
303 update_channel(&gossip_sync, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
304 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
305 short_channel_id: 12,
308 cltv_expiry_delta: (5 << 4) | 3,
309 htlc_minimum_msat: 0,
310 htlc_maximum_msat: MAX_VALUE_MSAT,
311 fee_base_msat: u32::max_value(),
312 fee_proportional_millionths: u32::max_value(),
313 excess_data: Vec::new()
315 update_channel(&gossip_sync, &secp_ctx, &privkeys[7], UnsignedChannelUpdate {
316 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
317 short_channel_id: 12,
320 cltv_expiry_delta: 0,
321 htlc_minimum_msat: 0,
322 htlc_maximum_msat: MAX_VALUE_MSAT,
324 fee_proportional_millionths: 0,
325 excess_data: Vec::new()
328 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[7], NodeFeatures::from_le_bytes(id_to_feature_flags(8)), 0);
330 add_channel(&gossip_sync, &secp_ctx, &privkeys[0], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(3)), 3);
331 update_channel(&gossip_sync, &secp_ctx, &privkeys[0], UnsignedChannelUpdate {
332 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
336 cltv_expiry_delta: (3 << 4) | 1,
337 htlc_minimum_msat: 0,
338 htlc_maximum_msat: MAX_VALUE_MSAT,
340 fee_proportional_millionths: 0,
341 excess_data: Vec::new()
343 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
344 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
348 cltv_expiry_delta: (3 << 4) | 2,
349 htlc_minimum_msat: 0,
350 htlc_maximum_msat: MAX_VALUE_MSAT,
352 fee_proportional_millionths: 0,
353 excess_data: Vec::new()
356 add_channel(&gossip_sync, &secp_ctx, &privkeys[1], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(4)), 4);
357 update_channel(&gossip_sync, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
358 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
362 cltv_expiry_delta: (4 << 4) | 1,
363 htlc_minimum_msat: 0,
364 htlc_maximum_msat: MAX_VALUE_MSAT,
366 fee_proportional_millionths: 1000000,
367 excess_data: Vec::new()
369 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
370 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
374 cltv_expiry_delta: (4 << 4) | 2,
375 htlc_minimum_msat: 0,
376 htlc_maximum_msat: MAX_VALUE_MSAT,
378 fee_proportional_millionths: 0,
379 excess_data: Vec::new()
382 add_channel(&gossip_sync, &secp_ctx, &privkeys[7], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(13)), 13);
383 update_channel(&gossip_sync, &secp_ctx, &privkeys[7], UnsignedChannelUpdate {
384 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
385 short_channel_id: 13,
388 cltv_expiry_delta: (13 << 4) | 1,
389 htlc_minimum_msat: 0,
390 htlc_maximum_msat: MAX_VALUE_MSAT,
392 fee_proportional_millionths: 2000000,
393 excess_data: Vec::new()
395 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
396 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
397 short_channel_id: 13,
400 cltv_expiry_delta: (13 << 4) | 2,
401 htlc_minimum_msat: 0,
402 htlc_maximum_msat: MAX_VALUE_MSAT,
404 fee_proportional_millionths: 0,
405 excess_data: Vec::new()
408 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[2], NodeFeatures::from_le_bytes(id_to_feature_flags(3)), 0);
410 add_channel(&gossip_sync, &secp_ctx, &privkeys[2], &privkeys[4], ChannelFeatures::from_le_bytes(id_to_feature_flags(6)), 6);
411 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
412 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
416 cltv_expiry_delta: (6 << 4) | 1,
417 htlc_minimum_msat: 0,
418 htlc_maximum_msat: MAX_VALUE_MSAT,
420 fee_proportional_millionths: 0,
421 excess_data: Vec::new()
423 update_channel(&gossip_sync, &secp_ctx, &privkeys[4], UnsignedChannelUpdate {
424 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
428 cltv_expiry_delta: (6 << 4) | 2,
429 htlc_minimum_msat: 0,
430 htlc_maximum_msat: MAX_VALUE_MSAT,
432 fee_proportional_millionths: 0,
433 excess_data: Vec::new(),
436 add_channel(&gossip_sync, &secp_ctx, &privkeys[4], &privkeys[3], ChannelFeatures::from_le_bytes(id_to_feature_flags(11)), 11);
437 update_channel(&gossip_sync, &secp_ctx, &privkeys[4], UnsignedChannelUpdate {
438 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
439 short_channel_id: 11,
442 cltv_expiry_delta: (11 << 4) | 1,
443 htlc_minimum_msat: 0,
444 htlc_maximum_msat: MAX_VALUE_MSAT,
446 fee_proportional_millionths: 0,
447 excess_data: Vec::new()
449 update_channel(&gossip_sync, &secp_ctx, &privkeys[3], UnsignedChannelUpdate {
450 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
451 short_channel_id: 11,
454 cltv_expiry_delta: (11 << 4) | 2,
455 htlc_minimum_msat: 0,
456 htlc_maximum_msat: MAX_VALUE_MSAT,
458 fee_proportional_millionths: 0,
459 excess_data: Vec::new()
462 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[4], NodeFeatures::from_le_bytes(id_to_feature_flags(5)), 0);
464 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[3], NodeFeatures::from_le_bytes(id_to_feature_flags(4)), 0);
466 add_channel(&gossip_sync, &secp_ctx, &privkeys[2], &privkeys[5], ChannelFeatures::from_le_bytes(id_to_feature_flags(7)), 7);
467 update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
468 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
472 cltv_expiry_delta: (7 << 4) | 1,
473 htlc_minimum_msat: 0,
474 htlc_maximum_msat: MAX_VALUE_MSAT,
476 fee_proportional_millionths: 1000000,
477 excess_data: Vec::new()
479 update_channel(&gossip_sync, &secp_ctx, &privkeys[5], UnsignedChannelUpdate {
480 chain_hash: ChainHash::using_genesis_block(Network::Testnet),
484 cltv_expiry_delta: (7 << 4) | 2,
485 htlc_minimum_msat: 0,
486 htlc_maximum_msat: MAX_VALUE_MSAT,
488 fee_proportional_millionths: 0,
489 excess_data: Vec::new()
492 add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[5], NodeFeatures::from_le_bytes(id_to_feature_flags(6)), 0);
494 (secp_ctx, network_graph, gossip_sync, chain_monitor, logger)