|
1 | 1 | use crate::builder::NodeBuilder;
|
2 | 2 | use crate::io::test_utils::TestSyncStore;
|
3 | 3 | use crate::test::utils::*;
|
4 |
| -use crate::test::utils::{expect_event, random_config, setup_two_nodes}; |
| 4 | +use crate::test::utils::{expect_channel_pending_event, expect_event, open_channel, random_config}; |
5 | 5 | use crate::{Error, Event, Node, PaymentDirection, PaymentStatus};
|
6 | 6 |
|
7 | 7 | use bitcoin::Amount;
|
@@ -63,24 +63,13 @@ fn do_channel_full_cycle<K: KVStore + Sync + Send>(
|
63 | 63 | .unwrap();
|
64 | 64 |
|
65 | 65 | assert_eq!(node_a.list_peers().first().unwrap().node_id, node_b.node_id());
|
66 |
| - expect_event!(node_a, ChannelPending); |
| 66 | + let funding_txo_a = expect_channel_pending_event!(node_a, node_b.node_id()); |
| 67 | + let funding_txo_b = expect_channel_pending_event!(node_b, node_a.node_id()); |
| 68 | + assert_eq!(funding_txo_a, funding_txo_b); |
67 | 69 |
|
68 |
| - let funding_txo = match node_b.wait_next_event() { |
69 |
| - ref e @ Event::ChannelPending { funding_txo, .. } => { |
70 |
| - println!("{} got event {:?}", std::stringify!(node_b), e); |
71 |
| - assert_eq!(node_b.next_event().as_ref(), Some(e)); |
72 |
| - node_b.event_handled(); |
73 |
| - funding_txo |
74 |
| - } |
75 |
| - ref e => { |
76 |
| - panic!("{} got unexpected event!: {:?}", std::stringify!(node_b), e); |
77 |
| - } |
78 |
| - }; |
79 |
| - |
80 |
| - wait_for_tx(&electrsd, funding_txo.txid); |
| 70 | + wait_for_tx(&electrsd, funding_txo_a.txid); |
81 | 71 |
|
82 | 72 | if !allow_0conf {
|
83 |
| - println!("\n .. generating blocks .."); |
84 | 73 | generate_blocks_and_wait(&bitcoind, &electrsd, 6);
|
85 | 74 | }
|
86 | 75 |
|
@@ -253,7 +242,7 @@ fn do_channel_full_cycle<K: KVStore + Sync + Send>(
|
253 | 242 | expect_event!(node_a, ChannelClosed);
|
254 | 243 | expect_event!(node_b, ChannelClosed);
|
255 | 244 |
|
256 |
| - wait_for_outpoint_spend(&electrsd, funding_txo); |
| 245 | + wait_for_outpoint_spend(&electrsd, funding_txo_b); |
257 | 246 |
|
258 | 247 | generate_blocks_and_wait(&bitcoind, &electrsd, 1);
|
259 | 248 | node_a.sync_wallets().unwrap();
|
@@ -318,6 +307,83 @@ fn channel_open_fails_when_funds_insufficient() {
|
318 | 307 | );
|
319 | 308 | }
|
320 | 309 |
|
| 310 | +#[test] |
| 311 | +fn multi_hop_sending() { |
| 312 | + let (bitcoind, electrsd) = setup_bitcoind_and_electrsd(); |
| 313 | + let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap()); |
| 314 | + |
| 315 | + // Setup and fund 5 nodes |
| 316 | + let mut nodes = Vec::new(); |
| 317 | + for _ in 0..5 { |
| 318 | + let config = random_config(); |
| 319 | + let mut builder = NodeBuilder::from_config(config); |
| 320 | + builder.set_esplora_server(esplora_url.clone()); |
| 321 | + let node = builder.build().unwrap(); |
| 322 | + node.start().unwrap(); |
| 323 | + nodes.push(node); |
| 324 | + } |
| 325 | + |
| 326 | + let addresses = nodes.iter().map(|n| n.new_onchain_address().unwrap()).collect(); |
| 327 | + let premine_amount_sat = 5_000_000; |
| 328 | + premine_and_distribute_funds( |
| 329 | + &bitcoind, |
| 330 | + &electrsd, |
| 331 | + addresses, |
| 332 | + Amount::from_sat(premine_amount_sat), |
| 333 | + ); |
| 334 | + |
| 335 | + for n in &nodes { |
| 336 | + n.sync_wallets().unwrap(); |
| 337 | + assert_eq!(n.spendable_onchain_balance_sats().unwrap(), premine_amount_sat); |
| 338 | + assert_eq!(n.next_event(), None); |
| 339 | + } |
| 340 | + |
| 341 | + // Setup channel topology: |
| 342 | + // (1M:0)- N2 -(1M:0) |
| 343 | + // / \ |
| 344 | + // N0 -(100k:0)-> N1 N4 |
| 345 | + // \ / |
| 346 | + // (1M:0)- N3 -(1M:0) |
| 347 | + |
| 348 | + open_channel(&nodes[0], &nodes[1], 100_000, true, &electrsd); |
| 349 | + open_channel(&nodes[1], &nodes[2], 1_000_000, true, &electrsd); |
| 350 | + // We need to sync wallets in-between back-to-back channel opens from the same node so BDK |
| 351 | + // wallet picks up on the broadcast funding tx and doesn't double-spend itself. |
| 352 | + // |
| 353 | + // TODO: Remove once fixed in BDK. |
| 354 | + nodes[1].sync_wallets().unwrap(); |
| 355 | + open_channel(&nodes[1], &nodes[3], 1_000_000, true, &electrsd); |
| 356 | + open_channel(&nodes[2], &nodes[4], 1_000_000, true, &electrsd); |
| 357 | + open_channel(&nodes[3], &nodes[4], 1_000_000, true, &electrsd); |
| 358 | + |
| 359 | + generate_blocks_and_wait(&bitcoind, &electrsd, 6); |
| 360 | + |
| 361 | + for n in &nodes { |
| 362 | + n.sync_wallets().unwrap(); |
| 363 | + } |
| 364 | + |
| 365 | + expect_event!(nodes[0], ChannelReady); |
| 366 | + expect_event!(nodes[1], ChannelReady); |
| 367 | + expect_event!(nodes[1], ChannelReady); |
| 368 | + expect_event!(nodes[1], ChannelReady); |
| 369 | + expect_event!(nodes[2], ChannelReady); |
| 370 | + expect_event!(nodes[2], ChannelReady); |
| 371 | + expect_event!(nodes[3], ChannelReady); |
| 372 | + expect_event!(nodes[3], ChannelReady); |
| 373 | + expect_event!(nodes[4], ChannelReady); |
| 374 | + expect_event!(nodes[4], ChannelReady); |
| 375 | + |
| 376 | + // Sleep a bit for gossip to propagate. |
| 377 | + // FIXME: This doesn't work anyways currently and should be removed when we have a solution. |
| 378 | + std::thread::sleep(std::time::Duration::from_secs(60)); |
| 379 | + |
| 380 | + let invoice = nodes[4].receive_payment(10000, &"asdf", 9217).unwrap(); |
| 381 | + nodes[0].send_payment(&invoice).unwrap(); |
| 382 | + |
| 383 | + expect_event!(nodes[4], PaymentReceived); |
| 384 | + expect_event!(nodes[0], PaymentSuccessful); |
| 385 | +} |
| 386 | + |
321 | 387 | #[test]
|
322 | 388 | fn connect_to_public_testnet_esplora() {
|
323 | 389 | let mut config = random_config();
|
|
0 commit comments