Skip to content

Commit f553daa

Browse files
committed
Add a simple test for ChainMonitor MonitorUpdate-holding behavior
1 parent 7b71c61 commit f553daa

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

lightning/src/chain/chainmonitor.rs

+82-2
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,16 @@ impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref> even
727727

728728
#[cfg(test)]
729729
mod tests {
730-
use ::{check_added_monitors, get_local_commitment_txn};
730+
use bitcoin::BlockHeader;
731+
use ::{check_added_monitors, check_closed_broadcast, check_closed_event, expect_payment_sent};
732+
use ::{get_local_commitment_txn, get_route_and_payment_hash, unwrap_send_err};
733+
use chain::{ChannelMonitorUpdateErr, Confirm, Watch};
734+
use chain::channelmonitor::LATENCY_GRACE_PERIOD_BLOCKS;
735+
use ln::channelmanager::PaymentSendFailure;
731736
use ln::features::InitFeatures;
732737
use ln::functional_test_utils::*;
733-
use util::events::MessageSendEventsProvider;
738+
use util::errors::APIError;
739+
use util::events::{ClosureReason, MessageSendEventsProvider};
734740
use util::test_utils::{OnRegisterOutput, TxOutReference};
735741

736742
/// Tests that in-block dependent transactions are processed by `block_connected` when not
@@ -775,4 +781,78 @@ mod tests {
775781
nodes[1].node.get_and_clear_pending_msg_events();
776782
nodes[1].node.get_and_clear_pending_events();
777783
}
784+
785+
fn do_chainsync_pauses_events(block_timeout: bool) {
786+
// When a chainsync monitor update occurs, any MonitorUpdates should be held before being
787+
// passed upstream. This tests that behavior, as well as some ways it might go wrong.
788+
let chanmon_cfgs = create_chanmon_cfgs(2);
789+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
790+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
791+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
792+
let channel = create_announced_chan_between_nodes(
793+
&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
794+
795+
// Get a route for later and rebalance the channel somewhat
796+
send_payment(&nodes[0], &[&nodes[1]], 10_000_000);
797+
let (route, second_payment_hash, _, second_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100_000);
798+
799+
// First route a payment that we will claim on chain and give the recipient the preimage.
800+
let payment_preimage = route_payment(&nodes[0], &[&nodes[1]], 1_000_000).0;
801+
nodes[1].node.claim_funds(payment_preimage);
802+
nodes[1].node.get_and_clear_pending_msg_events();
803+
check_added_monitors!(nodes[1], 1);
804+
let remote_txn = get_local_commitment_txn!(nodes[1], channel.2);
805+
assert_eq!(remote_txn.len(), 2);
806+
807+
// Temp-fail the block connection which will hold the channel-closed event
808+
chanmon_cfgs[0].persister.chain_sync_monitor_persistences.lock().unwrap().clear();
809+
chanmon_cfgs[0].persister.set_update_ret(Err(ChannelMonitorUpdateErr::TemporaryFailure));
810+
811+
// Connect B's commitment transaction, but only to the ChainMonitor/ChannelMonitor. The
812+
// channel is now closed, but the ChannelManager doesn't know that yet.
813+
let new_header = BlockHeader {
814+
version: 2, time: 0, bits: 0, nonce: 0,
815+
prev_blockhash: nodes[0].best_block_info().0,
816+
merkle_root: Default::default() };
817+
nodes[0].chain_monitor.chain_monitor.transactions_confirmed(&new_header,
818+
&[(0, &remote_txn[0]), (1, &remote_txn[1])], nodes[0].best_block_info().1 + 1);
819+
assert!(nodes[0].chain_monitor.release_pending_monitor_events().is_empty());
820+
821+
// If the ChannelManager tries to update the channel, however, the ChainMonitor will pass
822+
// the update through to the ChannelMonitor which will refuse it (as the channel is closed).
823+
chanmon_cfgs[0].persister.set_update_ret(Ok(()));
824+
unwrap_send_err!(nodes[0].node.send_payment(&route, second_payment_hash, &Some(second_payment_secret)),
825+
true, APIError::ChannelUnavailable { ref err },
826+
assert!(err.contains("ChannelMonitor storage failure")));
827+
check_added_monitors!(nodes[0], 2); // After the failure we generate a close-channel monitor update
828+
check_closed_broadcast!(nodes[0], true);
829+
check_closed_event!(nodes[0], 1, ClosureReason::ProcessingError { err: "ChannelMonitor storage failure".to_string() });
830+
831+
// However, as the ChainMonitor is still waiting for the original persistence to complete,
832+
// it won't yet release the MonitorEvents.
833+
assert!(nodes[0].chain_monitor.release_pending_monitor_events().is_empty());
834+
835+
if block_timeout {
836+
// After three blocks, pending MontiorEvents should be released either way.
837+
let latest_header = BlockHeader {
838+
version: 2, time: 0, bits: 0, nonce: 0,
839+
prev_blockhash: nodes[0].best_block_info().0,
840+
merkle_root: Default::default() };
841+
nodes[0].chain_monitor.chain_monitor.best_block_updated(&latest_header, nodes[0].best_block_info().1 + LATENCY_GRACE_PERIOD_BLOCKS);
842+
} else {
843+
for (funding_outpoint, update_ids) in chanmon_cfgs[0].persister.chain_sync_monitor_persistences.lock().unwrap().iter() {
844+
for update_id in update_ids {
845+
nodes[0].chain_monitor.chain_monitor.channel_monitor_updated(*funding_outpoint, *update_id).unwrap();
846+
}
847+
}
848+
}
849+
850+
expect_payment_sent!(nodes[0], payment_preimage);
851+
}
852+
853+
#[test]
854+
fn chainsync_pauses_events() {
855+
do_chainsync_pauses_events(false);
856+
do_chainsync_pauses_events(true);
857+
}
778858
}

0 commit comments

Comments
 (0)