|
| 1 | +mod common; |
| 2 | +use bitcoin::Amount; |
| 3 | +use ldk_node::payment::{PaymentDirection, PaymentKind}; |
| 4 | +use ldk_node::{Event, LightningBalance, PendingSweepBalance}; |
| 5 | +use proptest::{prelude::prop, proptest}; |
| 6 | +use std::collections::HashMap; |
| 7 | + |
| 8 | +use crate::common::{ |
| 9 | + expect_event, generate_blocks_and_wait, invalidate_blocks, open_channel, |
| 10 | + premine_and_distribute_funds, random_config, setup_bitcoind_and_electrsd, setup_node, |
| 11 | + wait_for_outpoint_spend, TestChainSource, |
| 12 | +}; |
| 13 | + |
| 14 | +proptest! { |
| 15 | + #![proptest_config(proptest::test_runner::Config::with_cases(5))] |
| 16 | + #[test] |
| 17 | + fn reorg_test(reorg_depth in 1..=6usize, force_close in prop::bool::ANY) { |
| 18 | + let (bitcoind, electrsd) = setup_bitcoind_and_electrsd(); |
| 19 | + |
| 20 | + let chain_source_bitcoind = TestChainSource::BitcoindRpcSync(&bitcoind); |
| 21 | + let chain_source_electrsd = TestChainSource::Electrum(&electrsd); |
| 22 | + let chain_source_esplora = TestChainSource::Esplora(&electrsd); |
| 23 | + |
| 24 | + macro_rules! config_node { |
| 25 | + ($chain_source: expr, $anchor_channels: expr) => {{ |
| 26 | + let config_a = random_config($anchor_channels); |
| 27 | + let node = setup_node(&$chain_source, config_a, None); |
| 28 | + node |
| 29 | + }}; |
| 30 | + } |
| 31 | + let anchor_channels = true; |
| 32 | + let nodes = vec![ |
| 33 | + config_node!(chain_source_electrsd, anchor_channels), |
| 34 | + config_node!(chain_source_bitcoind, anchor_channels), |
| 35 | + config_node!(chain_source_esplora, anchor_channels), |
| 36 | + ]; |
| 37 | + |
| 38 | + let (bitcoind, electrs) = (&bitcoind.client, &electrsd.client); |
| 39 | + macro_rules! reorg { |
| 40 | + ($reorg_depth: expr) => {{ |
| 41 | + invalidate_blocks(bitcoind, $reorg_depth); |
| 42 | + generate_blocks_and_wait(bitcoind, electrs, $reorg_depth); |
| 43 | + }}; |
| 44 | + } |
| 45 | + |
| 46 | + let amount_sat = 2_100_000; |
| 47 | + let addr_nodes = |
| 48 | + nodes.iter().map(|node| node.onchain_payment().new_address().unwrap()).collect::<Vec<_>>(); |
| 49 | + premine_and_distribute_funds(bitcoind, electrs, addr_nodes, Amount::from_sat(amount_sat)); |
| 50 | + |
| 51 | + macro_rules! sync_wallets { |
| 52 | + () => { |
| 53 | + nodes.iter().for_each(|node| node.sync_wallets().unwrap()) |
| 54 | + }; |
| 55 | + } |
| 56 | + sync_wallets!(); |
| 57 | + nodes.iter().for_each(|node| { |
| 58 | + assert_eq!(node.list_balances().spendable_onchain_balance_sats, amount_sat); |
| 59 | + assert_eq!(node.list_balances().total_onchain_balance_sats, amount_sat); |
| 60 | + }); |
| 61 | + |
| 62 | + |
| 63 | + let mut nodes_funding_tx = HashMap::new(); |
| 64 | + let funding_amount_sat = 2_000_000; |
| 65 | + for (node, next_node) in nodes.iter().zip(nodes.iter().cycle().skip(1)) { |
| 66 | + let funding_txo = open_channel(node, next_node, funding_amount_sat, true, &electrsd); |
| 67 | + nodes_funding_tx.insert(node.node_id(), funding_txo); |
| 68 | + } |
| 69 | + |
| 70 | + generate_blocks_and_wait(bitcoind, electrs, 6); |
| 71 | + sync_wallets!(); |
| 72 | + |
| 73 | + reorg!(reorg_depth); |
| 74 | + sync_wallets!(); |
| 75 | + |
| 76 | + macro_rules! collect_channel_ready_events { |
| 77 | + ($node:expr, $expected:expr) => {{ |
| 78 | + let mut user_channels = HashMap::new(); |
| 79 | + for _ in 0..$expected { |
| 80 | + match $node.wait_next_event() { |
| 81 | + Event::ChannelReady { user_channel_id, counterparty_node_id, .. } => { |
| 82 | + $node.event_handled().unwrap(); |
| 83 | + user_channels.insert(counterparty_node_id, user_channel_id); |
| 84 | + }, |
| 85 | + other => panic!("Unexpected event: {:?}", other), |
| 86 | + } |
| 87 | + } |
| 88 | + user_channels |
| 89 | + }}; |
| 90 | + } |
| 91 | + |
| 92 | + let mut node_channels_id = HashMap::new(); |
| 93 | + for (i, node) in nodes.iter().enumerate() { |
| 94 | + assert_eq!( |
| 95 | + node |
| 96 | + .list_payments_with_filter(|p| p.direction == PaymentDirection::Outbound |
| 97 | + && matches!(p.kind, PaymentKind::Onchain { .. })) |
| 98 | + .len(), |
| 99 | + 1 |
| 100 | + ); |
| 101 | + |
| 102 | + let user_channels = collect_channel_ready_events!(node, 2); |
| 103 | + let next_node = nodes.get((i + 1) % nodes.len()).unwrap(); |
| 104 | + let prev_node = nodes.get((i + nodes.len() - 1) % nodes.len()).unwrap(); |
| 105 | + |
| 106 | + assert!(user_channels.get(&Some(next_node.node_id())) != None); |
| 107 | + assert!(user_channels.get(&Some(prev_node.node_id())) != None); |
| 108 | + |
| 109 | + let user_channel_id = |
| 110 | + user_channels.get(&Some(next_node.node_id())).expect("Missing user channel for node"); |
| 111 | + node_channels_id.insert(node.node_id(), *user_channel_id); |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + for (node, next_node) in nodes.iter().zip(nodes.iter().cycle().skip(1)) { |
| 116 | + let user_channel_id = node_channels_id.get(&node.node_id()).expect("user channel id not exist"); |
| 117 | + let funding = nodes_funding_tx.get(&node.node_id()).expect("Funding tx not exist"); |
| 118 | + |
| 119 | + if force_close { |
| 120 | + node.force_close_channel(&user_channel_id, next_node.node_id(), None).unwrap(); |
| 121 | + } else { |
| 122 | + node.close_channel(&user_channel_id, next_node.node_id()).unwrap(); |
| 123 | + } |
| 124 | + |
| 125 | + expect_event!(node, ChannelClosed); |
| 126 | + expect_event!(next_node, ChannelClosed); |
| 127 | + |
| 128 | + wait_for_outpoint_spend(electrs, *funding); |
| 129 | + } |
| 130 | + |
| 131 | + reorg!(reorg_depth); |
| 132 | + sync_wallets!(); |
| 133 | + |
| 134 | + generate_blocks_and_wait(bitcoind, electrs, 1); |
| 135 | + sync_wallets!(); |
| 136 | + |
| 137 | + if force_close { |
| 138 | + nodes.iter().for_each(|node| { |
| 139 | + node.sync_wallets().unwrap(); |
| 140 | + // If there is no more balance, there is nothing to process here. |
| 141 | + if node.list_balances().lightning_balances.len() < 1 { |
| 142 | + return; |
| 143 | + } |
| 144 | + match node.list_balances().lightning_balances[0] { |
| 145 | + LightningBalance::ClaimableAwaitingConfirmations { |
| 146 | + confirmation_height, |
| 147 | + .. |
| 148 | + } => { |
| 149 | + let cur_height = node.status().current_best_block.height; |
| 150 | + let blocks_to_go = confirmation_height - cur_height; |
| 151 | + generate_blocks_and_wait(bitcoind, electrs, blocks_to_go as usize); |
| 152 | + node.sync_wallets().unwrap(); |
| 153 | + }, |
| 154 | + _ => panic!("Unexpected balance state for node_hub!"), |
| 155 | + } |
| 156 | + |
| 157 | + assert!(node.list_balances().lightning_balances.len() < 2); |
| 158 | + assert!(node.list_balances().pending_balances_from_channel_closures.len() > 0); |
| 159 | + match node.list_balances().pending_balances_from_channel_closures[0] { |
| 160 | + PendingSweepBalance::BroadcastAwaitingConfirmation { .. } => {}, |
| 161 | + _ => panic!("Unexpected balance state!"), |
| 162 | + } |
| 163 | + |
| 164 | + generate_blocks_and_wait(&bitcoind, electrs, 1); |
| 165 | + node.sync_wallets().unwrap(); |
| 166 | + assert!(node.list_balances().lightning_balances.len() < 2); |
| 167 | + assert!(node.list_balances().pending_balances_from_channel_closures.len() > 0); |
| 168 | + match node.list_balances().pending_balances_from_channel_closures[0] { |
| 169 | + PendingSweepBalance::AwaitingThresholdConfirmations { .. } => {}, |
| 170 | + _ => panic!("Unexpected balance state!"), |
| 171 | + } |
| 172 | + }); |
| 173 | + } |
| 174 | + |
| 175 | + generate_blocks_and_wait(bitcoind, electrs, 6); |
| 176 | + sync_wallets!(); |
| 177 | + |
| 178 | + reorg!(reorg_depth); |
| 179 | + sync_wallets!(); |
| 180 | + |
| 181 | + let fee_sat = 7000; |
| 182 | + // Check balance after close channel |
| 183 | + nodes.iter().for_each(|node| { |
| 184 | + assert!(node.list_balances().spendable_onchain_balance_sats > amount_sat - fee_sat); |
| 185 | + assert!(node.list_balances().spendable_onchain_balance_sats < amount_sat); |
| 186 | + |
| 187 | + assert_eq!(node.list_balances().total_anchor_channels_reserve_sats, 0); |
| 188 | + assert!(node.list_balances().lightning_balances.is_empty()); |
| 189 | + |
| 190 | + assert_eq!(node.next_event(), None); |
| 191 | + }); |
| 192 | + } |
| 193 | +} |
0 commit comments