Skip to content

Commit f0cc788

Browse files
committed
Test fixed channel reserve checks on channel open
1 parent 3f82a79 commit f0cc788

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

lightning/src/ln/functional_tests.rs

+35
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,41 @@ fn test_chan_reserve_dust_inbound_htlcs_outbound_chan() {
15011501
assert_eq!(err, "Cannot send value that would put counterparty balance under holder-announced channel reserve value"));
15021502
}
15031503

1504+
#[test]
1505+
fn test_chan_init_feerate_unaffordability() {
1506+
// Test that we will reject channel opens which do not leave enough to pay for any HTLCs due to
1507+
// channel reserve and feerate requirements.
1508+
let mut chanmon_cfgs = create_chanmon_cfgs(2);
1509+
let feerate_per_kw = *chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
1510+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1511+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1512+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1513+
1514+
// Set the push_msat amount such that nodes[0] will not be able to afford to add even a single
1515+
// HTLC.
1516+
let mut push_amt = 100_000_000;
1517+
push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64);
1518+
assert_eq!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, push_amt + 1, 42, None).unwrap_err(),
1519+
APIError::APIMisuseError { err: "Funding amount (356) can't even pay fee for initial commitment transaction fee of 357.".to_string() });
1520+
1521+
// During open, we don't have a "counterparty channel reserve" to check against, so that
1522+
// requirement only comes into play on the open_channel handling side.
1523+
push_amt -= Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(100_000) * 1000;
1524+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, push_amt, 42, None).unwrap();
1525+
let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
1526+
open_channel_msg.push_msat += 1;
1527+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel_msg);
1528+
1529+
let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
1530+
assert_eq!(msg_events.len(), 1);
1531+
match msg_events[0] {
1532+
MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id: _ } => {
1533+
assert_eq!(msg.data, "Insufficient funding amount for initial reserve");
1534+
},
1535+
_ => panic!("Unexpected event"),
1536+
}
1537+
}
1538+
15041539
#[test]
15051540
fn test_chan_reserve_dust_inbound_htlcs_inbound_chan() {
15061541
// Test that if we receive many dust HTLCs over an inbound channel, they don't count when

lightning/src/ln/script.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use io;
1717
/// A script pubkey for shutting down a channel as defined by [BOLT #2].
1818
///
1919
/// [BOLT #2]: https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md
20-
#[derive(Clone)]
20+
#[derive(Clone, PartialEq)]
2121
pub struct ShutdownScript(ShutdownScriptImpl);
2222

2323
/// An error occurring when converting from [`Script`] to [`ShutdownScript`].
@@ -29,7 +29,7 @@ pub struct InvalidShutdownScript {
2929
pub script: Script
3030
}
3131

32-
#[derive(Clone)]
32+
#[derive(Clone, PartialEq)]
3333
enum ShutdownScriptImpl {
3434
/// [`PublicKey`] used to form a P2WPKH script pubkey. Used to support backward-compatible
3535
/// serialization.

lightning/src/util/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use core::fmt;
1616

1717
/// Indicates an error on the client's part (usually some variant of attempting to use too-low or
1818
/// too-high values)
19-
#[derive(Clone)]
19+
#[derive(Clone, PartialEq)]
2020
pub enum APIError {
2121
/// Indicates the API was wholly misused (see err for more). Cases where these can be returned
2222
/// are documented, but generally indicates some precondition of a function was violated.

0 commit comments

Comments
 (0)