Skip to content

Commit 0d2450e

Browse files
committed
Add a test for shutdown negotiaion funder restart and timeout
1 parent a295c2a commit 0d2450e

File tree

3 files changed

+127
-3
lines changed

3 files changed

+127
-3
lines changed

lightning/src/ln/channel.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,14 +452,20 @@ pub(super) struct Channel<Signer: Sign> {
452452
counterparty_max_commitment_tx_output: Mutex<(u64, u64)>,
453453

454454
last_sent_closing_fee: Option<(u64, Signature)>, // (fee, holder_sig)
455-
closing_fee_limits: Option<(u64, u64)>,
456455
target_closing_feerate_sats_per_kw: Option<u32>,
457456

458457
/// If our counterparty sent us a closing_signed while we were waiting for a channel monitor
459458
/// update, we need to delay processing it until later. We do that here by simply storing the
460459
/// closing_signed message and handling it in `maybe_propose_closing_signed`.
461460
pending_counterparty_closing_signed: Option<msgs::ClosingSigned>,
462461

462+
/// The minimum and maximum absolute fee we are willing to place on the closing transaction.
463+
/// These are set once we reach `closing_negotiation_ready`.
464+
#[cfg(test)]
465+
pub(crate) closing_fee_limits: Option<(u64, u64)>,
466+
#[cfg(not(test))]
467+
closing_fee_limits: Option<(u64, u64)>,
468+
463469
/// The hash of the block in which the funding transaction was included.
464470
funding_tx_confirmed_in: Option<BlockHash>,
465471
funding_tx_confirmation_height: u32,

lightning/src/ln/functional_test_utils.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,22 @@ macro_rules! get_htlc_update_msgs {
416416
}
417417
}
418418

419+
#[cfg(test)]
420+
macro_rules! get_channel_ref {
421+
($node: expr, $lock: ident, $channel_id: expr) => {
422+
{
423+
$lock = $node.node.channel_state.lock().unwrap();
424+
$lock.by_id.get_mut(&$channel_id).unwrap()
425+
}
426+
}
427+
}
428+
419429
#[cfg(test)]
420430
macro_rules! get_feerate {
421431
($node: expr, $channel_id: expr) => {
422432
{
423-
let chan_lock = $node.node.channel_state.lock().unwrap();
424-
let chan = chan_lock.by_id.get(&$channel_id).unwrap();
433+
let mut lock;
434+
let chan = get_channel_ref!($node, lock, $channel_id);
425435
chan.get_feerate()
426436
}
427437
}

lightning/src/ln/shutdown_tests.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,3 +726,111 @@ fn test_invalid_shutdown_script() {
726726
}
727727
check_added_monitors!(nodes[0], 1);
728728
}
729+
730+
#[derive(PartialEq)]
731+
enum TimeoutStep {
732+
AfterShutdown,
733+
AfterClosingSigned,
734+
NoTimeout,
735+
}
736+
737+
fn do_test_closing_signed_reinit_timeout(timeout_step: TimeoutStep) {
738+
// The range-based closing signed negotiation allows the funder to restart the process with a
739+
// new range if the previous range did not overlap. This allows implementations to request user
740+
// intervention allowing users to enter a new fee range. We do not implement the sending side
741+
// of this, instead opting to allow users to enter an explicit "willing to pay up to X to avoid
742+
// force-closing" value and relying on that instead.
743+
//
744+
// Here we run test the fundee side of that restart mechanism, implementing the funder side of
745+
// it manually.
746+
let chanmon_cfgs = create_chanmon_cfgs(2);
747+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
748+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
749+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
750+
let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
751+
752+
send_payment(&nodes[0], &[&nodes[1]], 8_000_000);
753+
754+
nodes[0].node.close_channel(&chan_id).unwrap();
755+
let node_0_shutdown = get_event_msg!(nodes[0], MessageSendEvent::SendShutdown, nodes[1].node.get_our_node_id());
756+
nodes[1].node.handle_shutdown(&nodes[0].node.get_our_node_id(), &InitFeatures::known(), &node_0_shutdown);
757+
let node_1_shutdown = get_event_msg!(nodes[1], MessageSendEvent::SendShutdown, nodes[0].node.get_our_node_id());
758+
nodes[0].node.handle_shutdown(&nodes[1].node.get_our_node_id(), &InitFeatures::known(), &node_1_shutdown);
759+
760+
{
761+
// Now we set nodes[1] to require a relatively high feerate for closing. This should result
762+
// in it rejecting nodes[0]'s initial closing_signed, giving nodes[0] a chance to try
763+
// again.
764+
let mut feerate_lock = chanmon_cfgs[1].fee_estimator.sat_per_kw.lock().unwrap();
765+
*feerate_lock *= 10;
766+
}
767+
768+
let mut node_0_closing_signed = get_event_msg!(nodes[0], MessageSendEvent::SendClosingSigned, nodes[1].node.get_our_node_id());
769+
// nodes[0] should use a "reasonable" feerate, well under the 10 sat/vByte that nodes[1] thinks
770+
// is the current prevailing feerate.
771+
assert!(node_0_closing_signed.fee_satoshis <= 500);
772+
773+
if timeout_step != TimeoutStep::AfterShutdown {
774+
nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
775+
// At this point nodes[1] should send back a warning message indicating it disagrees with the
776+
// given channel-closing fee. Currently we do not implement warning messages so instead we
777+
// remain silent here.
778+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
779+
780+
// Now deliver a mutated closing_signed indicating a higher acceptable fee range, which
781+
// nodes[1] should happily accept and respond to.
782+
node_0_closing_signed.fee_range.as_mut().unwrap().max_fee_satoshis *= 10;
783+
{
784+
let mut lock;
785+
get_channel_ref!(nodes[0], lock, chan_id).closing_fee_limits.as_mut().unwrap().1 *= 10;
786+
}
787+
nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
788+
let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
789+
nodes[0].node.handle_closing_signed(&nodes[1].node.get_our_node_id(), &node_1_closing_signed);
790+
let node_0_2nd_closing_signed = get_closing_signed_broadcast!(nodes[0].node, nodes[1].node.get_our_node_id());
791+
if timeout_step == TimeoutStep::NoTimeout {
792+
nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_2nd_closing_signed.1.unwrap());
793+
}
794+
}
795+
796+
if timeout_step != TimeoutStep::NoTimeout {
797+
assert!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty());
798+
} else {
799+
assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
800+
}
801+
802+
nodes[1].node.timer_tick_occurred();
803+
nodes[1].node.timer_tick_occurred();
804+
805+
let txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
806+
assert_eq!(txn.len(), 1);
807+
assert_eq!(txn[0].output.len(), 2);
808+
809+
if timeout_step != TimeoutStep::NoTimeout {
810+
assert!((txn[0].output[0].script_pubkey.is_v0_p2wpkh() &&
811+
txn[0].output[1].script_pubkey.is_v0_p2wsh()) ||
812+
(txn[0].output[1].script_pubkey.is_v0_p2wpkh() &&
813+
txn[0].output[0].script_pubkey.is_v0_p2wsh()));
814+
check_closed_broadcast!(nodes[1], true);
815+
check_added_monitors!(nodes[1], 1);
816+
} else {
817+
assert!(txn[0].output[0].script_pubkey.is_v0_p2wpkh());
818+
assert!(txn[0].output[1].script_pubkey.is_v0_p2wpkh());
819+
820+
let events = nodes[1].node.get_and_clear_pending_msg_events();
821+
assert_eq!(events.len(), 1);
822+
match events[0] {
823+
MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
824+
assert_eq!(msg.contents.flags & 2, 2);
825+
},
826+
_ => panic!("Unexpected event"),
827+
}
828+
}
829+
}
830+
831+
#[test]
832+
fn test_closing_signed_reinit_timeout() {
833+
do_test_closing_signed_reinit_timeout(TimeoutStep::AfterShutdown);
834+
do_test_closing_signed_reinit_timeout(TimeoutStep::AfterClosingSigned);
835+
do_test_closing_signed_reinit_timeout(TimeoutStep::NoTimeout);
836+
}

0 commit comments

Comments
 (0)