|
9 | 9 |
|
10 | 10 | //! Further functional tests which test blockchain reorganizations.
|
11 | 11 |
|
| 12 | +#[cfg(anchors)] |
| 13 | +use crate::chain::keysinterface::BaseSign; |
| 14 | +#[cfg(anchors)] |
| 15 | +use crate::chain::channelmonitor::LATENCY_GRACE_PERIOD_BLOCKS; |
12 | 16 | use crate::chain::channelmonitor::{ANTI_REORG_DELAY, Balance};
|
13 | 17 | use crate::chain::transaction::OutPoint;
|
14 | 18 | use crate::chain::chaininterface::LowerBoundedFeeEstimator;
|
15 | 19 | use crate::ln::channel;
|
| 20 | +#[cfg(anchors)] |
| 21 | +use crate::ln::chan_utils; |
16 | 22 | use crate::ln::channelmanager::{self, BREAKDOWN_TIMEOUT};
|
17 | 23 | use crate::ln::msgs::ChannelMessageHandler;
|
| 24 | +#[cfg(anchors)] |
| 25 | +use crate::util::config::UserConfig; |
| 26 | +#[cfg(anchors)] |
| 27 | +use crate::util::events::BumpTransactionEvent; |
18 | 28 | use crate::util::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
|
19 | 29 |
|
20 | 30 | use bitcoin::blockdata::script::Builder;
|
21 | 31 | use bitcoin::blockdata::opcodes;
|
22 | 32 | use bitcoin::secp256k1::Secp256k1;
|
| 33 | +#[cfg(anchors)] |
| 34 | +use bitcoin::{Amount, Script, TxIn, TxOut, PackedLockTime}; |
23 | 35 | use bitcoin::Transaction;
|
24 | 36 |
|
25 | 37 | use crate::prelude::*;
|
@@ -1629,3 +1641,132 @@ fn test_revoked_counterparty_aggregated_claims() {
|
1629 | 1641 | test_spendable_output(&nodes[1], &claim_txn_2[0]);
|
1630 | 1642 | assert!(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances().is_empty());
|
1631 | 1643 | }
|
| 1644 | + |
| 1645 | +#[cfg(anchors)] |
| 1646 | +#[test] |
| 1647 | +fn test_yield_anchors_events() { |
| 1648 | + let secp = Secp256k1::new(); |
| 1649 | + let mut chanmon_cfgs = create_chanmon_cfgs(2); |
| 1650 | + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); |
| 1651 | + let mut anchors_config = UserConfig::default(); |
| 1652 | + anchors_config.channel_handshake_config.announced_channel = true; |
| 1653 | + anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true; |
| 1654 | + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config), Some(anchors_config)]); |
| 1655 | + let nodes = create_network(2, &node_cfgs, &node_chanmgrs); |
| 1656 | + |
| 1657 | + let features = channelmanager::provided_init_features(); |
| 1658 | + let _ = create_announced_chan_between_nodes_with_value( |
| 1659 | + &nodes, 0, 1, 1_000_000, 100_000_000, features.clone(), features.clone(), |
| 1660 | + ); |
| 1661 | + let _ = route_payment(&nodes[0], &[&nodes[1]], 1_000_000).1; |
| 1662 | + let _ = route_payment(&nodes[1], &[&nodes[0]], 1_000_000).1; |
| 1663 | + |
| 1664 | + assert!(nodes[0].node.get_and_clear_pending_events().is_empty()); |
| 1665 | + |
| 1666 | + let _ = connect_blocks(&nodes[0], TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS + 1); |
| 1667 | + assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty()); |
| 1668 | + |
| 1669 | + let mut holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events(); |
| 1670 | + assert_eq!(holder_events.len(), 1); |
| 1671 | + let (commitment_tx, anchor_tx) = match holder_events.pop().unwrap() { |
| 1672 | + Event::BumpTransaction(BumpTransactionEvent::ChannelClose { commitment_tx, anchor_descriptor, .. }) => { |
| 1673 | + assert_eq!(commitment_tx.input.len(), 1); |
| 1674 | + assert_eq!(commitment_tx.output.len(), 6); |
| 1675 | + let mut anchor_tx = Transaction { |
| 1676 | + version: 2, |
| 1677 | + lock_time: PackedLockTime::ZERO, |
| 1678 | + input: vec![ |
| 1679 | + TxIn { previous_output: anchor_descriptor.outpoint, ..Default::default() }, |
| 1680 | + TxIn { ..Default::default() }, |
| 1681 | + ], |
| 1682 | + output: vec![TxOut { |
| 1683 | + value: Amount::ONE_BTC.to_sat(), |
| 1684 | + script_pubkey: Script::new_op_return(&[]), |
| 1685 | + }], |
| 1686 | + }; |
| 1687 | + let signer = nodes[0].keys_manager.derive_channel_keys( |
| 1688 | + anchor_descriptor.channel_value_satoshis, &anchor_descriptor.channel_keys_id, |
| 1689 | + ); |
| 1690 | + let funding_sig = signer.sign_holder_anchor_input(&mut anchor_tx, 0, &secp).unwrap(); |
| 1691 | + anchor_tx.input[0].witness = chan_utils::build_anchor_input_witness( |
| 1692 | + &signer.pubkeys().funding_pubkey, &funding_sig |
| 1693 | + ); |
| 1694 | + (commitment_tx, anchor_tx) |
| 1695 | + }, |
| 1696 | + _ => panic!("Unexpected event"), |
| 1697 | + }; |
| 1698 | + |
| 1699 | + mine_transactions(&nodes[0], &[&commitment_tx, &anchor_tx]); |
| 1700 | + connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1); |
| 1701 | + check_added_monitors!(nodes[0], 1); |
| 1702 | + |
| 1703 | + let holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events(); |
| 1704 | + assert_eq!(holder_events.len(), 1); |
| 1705 | + let mut htlc_tx = Transaction { version: 2, lock_time: PackedLockTime::ZERO, input: vec![], output: vec![] }; |
| 1706 | + for event in holder_events { |
| 1707 | + match event { |
| 1708 | + Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { mut tx_template, htlc_descriptors, .. }) => { |
| 1709 | + tx_template.input.push(TxIn { ..Default::default() }); |
| 1710 | + tx_template.output.push(TxOut { |
| 1711 | + value: Amount::ONE_BTC.to_sat(), |
| 1712 | + script_pubkey: Script::new_op_return(&[]), |
| 1713 | + }); |
| 1714 | + for (idx, htlc_descriptor) in htlc_descriptors.iter().enumerate() { |
| 1715 | + let signer = nodes[0].keys_manager.derive_channel_keys( |
| 1716 | + htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id |
| 1717 | + ); |
| 1718 | + let our_sig = signer.sign_holder_htlc_transaction( |
| 1719 | + &mut tx_template, idx, htlc_descriptor.per_commitment_number, |
| 1720 | + &htlc_descriptor.htlc, &htlc_descriptor.counterparty_base_htlc_key, |
| 1721 | + &htlc_descriptor.counterparty_base_revocation_key, &secp |
| 1722 | + ).unwrap(); |
| 1723 | + let witness_script = { |
| 1724 | + let per_commitment_point = signer.get_per_commitment_point( |
| 1725 | + htlc_descriptor.per_commitment_number, &secp |
| 1726 | + ); |
| 1727 | + let our_htlc_key = chan_utils::derive_public_key( |
| 1728 | + &secp, &per_commitment_point, &signer.pubkeys().htlc_basepoint |
| 1729 | + ).unwrap(); |
| 1730 | + let counterparty_htlc_key = chan_utils::derive_public_key( |
| 1731 | + &secp, &per_commitment_point, &htlc_descriptor.counterparty_base_htlc_key |
| 1732 | + ).unwrap(); |
| 1733 | + let revocation_key = chan_utils::derive_public_revocation_key( |
| 1734 | + &secp, &per_commitment_point, &htlc_descriptor.counterparty_base_revocation_key |
| 1735 | + ).unwrap(); |
| 1736 | + chan_utils::get_htlc_redeemscript_with_explicit_keys( |
| 1737 | + &htlc_descriptor.htlc, true, &our_htlc_key, &counterparty_htlc_key, &revocation_key, |
| 1738 | + ) |
| 1739 | + }; |
| 1740 | + tx_template.input[idx].witness = chan_utils::build_htlc_input_witness( |
| 1741 | + &our_sig, &htlc_descriptor.counterparty_sig, &htlc_descriptor.preimage, |
| 1742 | + &witness_script, true |
| 1743 | + ); |
| 1744 | + } |
| 1745 | + htlc_tx = tx_template; |
| 1746 | + }, |
| 1747 | + _ => panic!("Unexpected event"), |
| 1748 | + } |
| 1749 | + } |
| 1750 | + |
| 1751 | + mine_transaction(&nodes[0], &htlc_tx); |
| 1752 | + connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1); |
| 1753 | + |
| 1754 | + assert!(nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events().is_empty()); |
| 1755 | + |
| 1756 | + connect_blocks(&nodes[0], BREAKDOWN_TIMEOUT as u32); |
| 1757 | + |
| 1758 | + let holder_events = nodes[0].chain_monitor.chain_monitor.get_and_clear_pending_events(); |
| 1759 | + assert_eq!(holder_events.len(), 2); |
| 1760 | + for event in holder_events { |
| 1761 | + match event { |
| 1762 | + Event::SpendableOutputs { .. } => {}, |
| 1763 | + _ => panic!("Unexpected event"), |
| 1764 | + } |
| 1765 | + } |
| 1766 | + |
| 1767 | + nodes[0].node.get_and_clear_pending_events(); |
| 1768 | + nodes[1].node.get_and_clear_pending_events(); |
| 1769 | + |
| 1770 | + nodes[0].node.get_and_clear_pending_msg_events(); |
| 1771 | + nodes[1].node.get_and_clear_pending_msg_events(); |
| 1772 | +} |
0 commit comments