|
4 | 4 |
|
5 | 5 | use chain::transaction::OutPoint;
|
6 | 6 | use chain::keysinterface::{ChannelKeys, KeysInterface, SpendableOutputDescriptor};
|
| 7 | +use chain::chaininterface; |
7 | 8 | use chain::chaininterface::{ChainListener, ChainWatchInterfaceUtil, BlockNotifier};
|
8 | 9 | use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
|
9 | 10 | use ln::channelmanager::{ChannelManager,ChannelManagerReadArgs,HTLCForwardInfo,RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT};
|
10 | 11 | use ln::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ManyChannelMonitor, ANTI_REORG_DELAY};
|
| 12 | +use ln::channelmonitor; |
11 | 13 | use ln::channel::{Channel, ChannelError};
|
12 | 14 | use ln::{chan_utils, onion_utils};
|
13 | 15 | use ln::router::{Route, RouteHop};
|
@@ -7558,3 +7560,64 @@ fn test_override_0msat_htlc_minimum() {
|
7558 | 7560 | let res = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
|
7559 | 7561 | assert_eq!(res.htlc_minimum_msat, 1);
|
7560 | 7562 | }
|
| 7563 | + |
| 7564 | +#[test] |
| 7565 | +fn test_update_err_monitor_lockdown() { |
| 7566 | + // Our monitor will lock update of local commitment transaction if a broadcastion condition |
| 7567 | + // has been fulfilled (either force-close from Channel or block height requiring a HTLC- |
| 7568 | + // timeout). Trying to update monitor after lockdown should return a ChannelMonitorUpdateErr. |
| 7569 | + // |
| 7570 | + // This scenario may happen in a watchtower setup, where watchtower process a block height |
| 7571 | + // triggering a timeout while a slow-block-processing ChannelManager receives a local signed |
| 7572 | + // commitment at same time. |
| 7573 | + |
| 7574 | + let chanmon_cfgs = create_chanmon_cfgs(2); |
| 7575 | + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); |
| 7576 | + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); |
| 7577 | + let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); |
| 7578 | + |
| 7579 | + // Create some initial channel |
| 7580 | + let chan_1 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::supported(), InitFeatures::supported()); |
| 7581 | + let outpoint = OutPoint { txid: chan_1.3.txid(), index: 0 }; |
| 7582 | + |
| 7583 | + // Rebalance the network to generate htlc in the two directions |
| 7584 | + send_payment(&nodes[0], &vec!(&nodes[1])[..], 10_000_000, 10_000_000); |
| 7585 | + |
| 7586 | + // Route a HTLC from node 0 to node 1 (but don't settle) |
| 7587 | + let preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 9_000_000).0; |
| 7588 | + |
| 7589 | + // Copy SimpleManyChannelMonitor to simulate a watchtower and update block height of node 0 until its ChannelMonitor timeout HTLC onchain |
| 7590 | + let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", 0))); |
| 7591 | + let watchtower = { |
| 7592 | + let monitors = nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap(); |
| 7593 | + let monitor = monitors.get(&outpoint).unwrap(); |
| 7594 | + let mut w = test_utils::TestVecWriter(Vec::new()); |
| 7595 | + monitor.write_for_disk(&mut w).unwrap(); |
| 7596 | + let new_monitor = <(Sha256dHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read( |
| 7597 | + &mut ::std::io::Cursor::new(&w.0), Arc::new(test_utils::TestLogger::new())).unwrap().1; |
| 7598 | + assert!(new_monitor == *monitor); |
| 7599 | + let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, logger.clone() as Arc<Logger>)); |
| 7600 | + let watchtower = test_utils::TestChannelMonitor::new(chain_monitor, &chanmon_cfgs[0].tx_broadcaster, logger.clone(), &chanmon_cfgs[0].fee_estimator); |
| 7601 | + assert!(watchtower.add_monitor(outpoint, new_monitor).is_ok()); |
| 7602 | + watchtower |
| 7603 | + }; |
| 7604 | + let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 }; |
| 7605 | + watchtower.simple_monitor.block_connected(&header, 200, &vec![], &vec![]); |
| 7606 | + |
| 7607 | + // Try to update ChannelMonitor |
| 7608 | + assert!(nodes[1].node.claim_funds(preimage, 9_000_000)); |
| 7609 | + check_added_monitors!(nodes[1], 1); |
| 7610 | + let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()); |
| 7611 | + assert_eq!(updates.update_fulfill_htlcs.len(), 1); |
| 7612 | + nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]); |
| 7613 | + if let Some(ref mut channel) = nodes[0].node.channel_state.lock().unwrap().by_id.get_mut(&chan_1.2) { |
| 7614 | + if let Ok((_, _, _, update)) = channel.commitment_signed(&updates.commitment_signed, &node_cfgs[0].fee_estimator) { |
| 7615 | + if let Err(_) = watchtower.simple_monitor.update_monitor(outpoint, update.clone()) {} else { assert!(false); } |
| 7616 | + if let Ok(_) = nodes[0].chan_monitor.update_monitor(outpoint, update) {} else { assert!(false); } |
| 7617 | + } else { assert!(false); } |
| 7618 | + } else { assert!(false); }; |
| 7619 | + // Our local monitor is in-sync and hasn't processed yet timeout |
| 7620 | + check_added_monitors!(nodes[0], 1); |
| 7621 | + let events = nodes[0].node.get_and_clear_pending_events(); |
| 7622 | + assert_eq!(events.len(), 1); |
| 7623 | +} |
0 commit comments