From 31b491204e79d121a6bcfe020708c528d6af35ec Mon Sep 17 00:00:00 2001 From: Aditya Sharma Date: Wed, 17 Jul 2024 12:45:05 +0530 Subject: [PATCH] functional_tests: Add test to check if the node is recovering funds from a revoked state. --- lightning/src/ln/functional_tests.rs | 104 +++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index d116ad65c29..63c24ec7092 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -4680,6 +4680,110 @@ macro_rules! check_spendable_outputs { } } +#[test] +fn test_peer_storage_on_revoked_txn() { + let chanmon_cfgs = create_chanmon_cfgs(2); + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); + let (persister, chain_monitor); + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); + let nodes_0_deserialized; + let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); + let nodes_0_serialized = nodes[0].node.encode(); + + let (_a, _b, channel_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1); + + let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0; + let revoked_local_txn = get_local_commitment_txn!(nodes[1], channel_id); + assert_eq!(revoked_local_txn[0].input.len(), 1); + assert_eq!(revoked_local_txn[0].input[0].previous_output.txid, funding_tx.txid()); + + claim_payment(&nodes[0], &vec!(&nodes[1])[..], payment_preimage); + send_payment(&nodes[0], &vec!(&nodes[1])[..], 1000); + + nodes[0].node.peer_disconnected(&nodes[1].node.get_our_node_id()); + nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id()); + + // Lets drop the monitor and clear the chain_monitor as well. + nodes[0].chain_source.remove_watched_txn_and_outputs( + OutPoint { txid: funding_tx.txid(), index: 0 }, + funding_tx.output[0].script_pubkey.clone() + ); + + reload_node_with_stubs!(nodes[0], &nodes_0_serialized, &[], &[], persister, chain_monitor, nodes_0_deserialized); + + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { + features: nodes[0].node.init_features(), networks: None, remote_network_address: None + }, true).unwrap(); + let reestablish_1 = get_chan_reestablish_msgs!(nodes[1], nodes[0]); + assert_eq!(reestablish_1.len(), 1); + + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { + features: nodes[1].node.init_features(), networks: None, remote_network_address: None + }, false).unwrap(); + let reestablish_2 = get_chan_reestablish_msgs!(nodes[0], nodes[1]); + assert_eq!(reestablish_2.len(), 0); + + nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &reestablish_1[0]); + + // Node[0] will generate bogus chennal reestablish and warning so that the peer closes the channel. + let mut closing_events = nodes[0].node.get_and_clear_pending_msg_events(); + assert_eq!(closing_events.len(), 2); + let nodes_2_event = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut closing_events); + let nodes_0_event = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut closing_events); + + match nodes_2_event { + MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } => { + assert_eq!(nodes[1].node.get_our_node_id(), *node_id); + nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), msg) + }, + _ => panic!("Unexpected event"), + } + + let mut err_msgs_0 = Vec::with_capacity(1); + if let MessageSendEvent::HandleError { ref action, .. } = nodes_0_event { + match action { + &ErrorAction::SendErrorMessage { ref msg } => { + assert_eq!(msg.data, format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", &nodes[1].node.get_our_node_id())); + err_msgs_0.push(msg.clone()); + }, + _ => panic!("Unexpected event!"), + } + } else { + panic!("Unexpected event!"); + } + assert_eq!(err_msgs_0.len(), 1); + nodes[1].node.handle_error(&nodes[0].node.get_our_node_id(), &err_msgs_0[0]); + + let block = create_dummy_block(nodes[1].best_block_hash(), 42, vec![revoked_local_txn[0].clone()]); + connect_block(&nodes[0], &block); + connect_block(&nodes[1], &block); + check_closed_broadcast!(nodes[1], true); + + let events_2 = nodes[1].node.get_and_clear_pending_events(); + assert_eq!(events_2.len(), 1); + match events_2[0] { + Event::ChannelClosed {..} => {}, // If we actually processed we'd receive the payment + _ => panic!("Unexpected event"), + } + check_added_monitors!(nodes[1], 1); + connect_blocks(&nodes[0], CHAN_CONFIRM_DEPTH); + + + // check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed, [nodes[0].node.get_our_node_id()], 100000); + + // let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); + // assert_eq!(node_txn.len(), 1); + // assert_eq!(node_txn[0].input.len(), 2); + // check_spends!(node_txn[0], revoked_local_txn[0]); + + // mine_transaction(&nodes[1], &node_txn[0]); + // connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1); + + // let spend_txn = check_spendable_outputs!(nodes[0], node_cfgs[1].keys_manager); + // assert_eq!(spend_txn.len(), 1); + // check_spends!(spend_txn[0], node_txn[0]); +} + #[test] fn test_claim_sizeable_push_msat() { // Incidentally test SpendableOutput event generation due to detection of to_local output on commitment tx