From a006d08559177f6caadeb94edcc868210a3019f9 Mon Sep 17 00:00:00 2001 From: Amperstrand Date: Sat, 5 Sep 2026 22:35:09 +0200 Subject: [PATCH 1/4] openingd: fail open_channel at receipt when our dust limit exceeds their reserve BOLT #2 requires the accept_channel sender to set dust_limit_satoshis less than or equal to channel_reserve_satoshis from the open_channel message. fundee_channel() quotes this exact MUST in its comment block but implements no check for it: with the chainparams dust limit (546) and an opener reserve below that, CLN replies accept_channel with dust_limit_satoshis=546, violating the sender MUST. Spec-strict peers (e.g. an LDK opener at the 354-sat spec-minimum dust limit) then fail the channel, with nothing above DEBUG on our side recording why. Mirror the existing opener-side check (openingd.c:446-455) in the accepter path, respecting --dev-allowdustreserve like both neighboring checks. The in-suite test sends a true sub-546 reserve by running the opener under --dev-allowdustreserve (a stock fundchannel self-bumps to its dust limit, mirroring test_zeroreserve's construction); it fails on master and passes with this change. Changelog-Fixes: #9439 Fixes: #9439 --- openingd/openingd.c | 18 ++++++++++++++++++ tests/test_opening.py | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/openingd/openingd.c b/openingd/openingd.c index a682594fc93a..7285306e6959 100644 --- a/openingd/openingd.c +++ b/openingd/openingd.c @@ -996,6 +996,24 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg) return NULL; } + /* BOLT #2: + * + * The sender: + *... + * - MUST set `dust_limit_satoshis` less than or equal to + * `channel_reserve_satoshis` from the `open_channel` message. + */ + if (!state->allowdustreserve && + amount_sat_greater(state->localconf.dust_limit, + state->remoteconf.channel_reserve)) { + negotiation_failed(state, + "Our dust limit %s" + " would be above their reserve %s", + fmt_amount_sat(tmpctx, state->localconf.dust_limit), + fmt_amount_sat(tmpctx, state->remoteconf.channel_reserve)); + return NULL; + } + /* These checks are the same whether we're opener or accepter... */ if (!check_config_bounds(tmpctx, state->funding_sats, state->feerate_per_kw, diff --git a/tests/test_opening.py b/tests/test_opening.py index 6d4f40a01dd8..1e4a01a18e9b 100644 --- a/tests/test_opening.py +++ b/tests/test_opening.py @@ -2812,6 +2812,27 @@ def test_zeroconf_multichan_forward(node_factory): .format(normal_scid, zeroconf_scid)) +def test_dust_limit_above_reserve(node_factory, bitcoind): + """BOLT #2: the accept_channel sender MUST set dust_limit_satoshis + to less than or equal to channel_reserve_satoshis from the + open_channel message (ElementsProject/lightning#9439). + + A stock opener self-bumps its reserve up to its own dust limit, so + the violating open is only reachable when the opener runs + --dev-allowdustreserve and sends its true sub-dust reserve: the + accepter's chainparams dust limit (546) then exceeds it, and the + accepter must fail the channel instead of replying accept_channel. + """ + l1 = node_factory.get_node(options={'dev-allowdustreserve': True}) + l2 = node_factory.get_node() + + l1.fundwallet(10**7) + l1.connect(l2) + + with pytest.raises(RpcError, match='would be above their reserve'): + l1.rpc.fundchannel(l2.info['id'], 10**5, reserve='354sat') + + def test_zeroreserve(node_factory, bitcoind): """Ensure we can set the reserves. From 4d48f2fa76f933667eb9775df50faa7ff675f864 Mon Sep 17 00:00:00 2001 From: Amperstrand Date: Tue, 15 Sep 2026 17:42:12 +0200 Subject: [PATCH 2/4] tests: allow l2 a sub-dust reserve in test_zeroreserve_mixed The new accepter-side check (dust_limit <= their reserve) rejects l1's 0sat-reserve open at l2, whose dust limit is the chainparams 546 -- and a stock accepter has no legal dust_limit against a 0sat reserve at all (the spec floors dust at 354). l2 sets --dev-allowdustreserve, the same crutch its sibling tests already use on every node; mixed/all zeroreserve tests pass on both networks. Changelog-None: test-only Signed-off-by: Amperstrand --- tests/test_opening.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_opening.py b/tests/test_opening.py index 1e4a01a18e9b..d91272334851 100644 --- a/tests/test_opening.py +++ b/tests/test_opening.py @@ -2925,7 +2925,9 @@ def test_zeroreserve_mixed(node_factory, bitcoind): 'reserve': '0sat', 'dev-allowdustreserve': True, }, { - 'dev-allowdustreserve': False, + # l2 accepts l1's zero reserve, which is below l2's dust + # limit: spec-illegal unless allowed for testing. + 'dev-allowdustreserve': True, }, { 'dev-allowdustreserve': False, } From 81b4443fffb550a73f66751b4305e4d4e9a47c5f Mon Sep 17 00:00:00 2001 From: Andezion Date: Thu, 17 Sep 2026 13:52:41 +0200 Subject: [PATCH 3/4] openingd: floor accept_channel reserve at our own dust limit too Changelog-Fixed: 'openingd' no longer sends an 'accept_channel' whose 'channel_reserve_satoshis' is below its own 'dust_limit_satoshis' --- openingd/openingd.c | 19 ++++++++--- tests/test_connection.py | 74 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 7 deletions(-) diff --git a/openingd/openingd.c b/openingd/openingd.c index 7285306e6959..ec1300ac2707 100644 --- a/openingd/openingd.c +++ b/openingd/openingd.c @@ -145,6 +145,8 @@ static void NORETURN negotiation_failed(struct state *state, static void set_reserve_absolute(struct state * state, const struct amount_sat dust_limit, struct amount_sat reserve_sat) { + struct amount_sat floor; + status_debug("Setting their reserve to %s", fmt_amount_sat(tmpctx, reserve_sat)); if (state->allowdustreserve) { @@ -156,13 +158,22 @@ static void set_reserve_absolute(struct state * state, const struct amount_sat d * ... * - MUST set `channel_reserve_satoshis` greater than or equal * to `dust_limit_satoshis` from the `open_channel` message. + * - MUST set `dust_limit_satoshis` less than or equal to + * `channel_reserve_satoshis` from the `open_channel` message. */ - if (amount_sat_greater(dust_limit, reserve_sat)) { - status_debug("Their reserve is too small, bumping to " + /* So floor against whichever dust limit (ours or the + * caller-supplied one) is larger. */ + if (amount_sat_greater(state->localconf.dust_limit, dust_limit)) + floor = state->localconf.dust_limit; + else + floor = dust_limit; + + if (amount_sat_greater(floor, reserve_sat)) { + status_debug("Reserve is too small, bumping to " "dust_limit: %s < %s", fmt_amount_sat(tmpctx, reserve_sat), - fmt_amount_sat(tmpctx, dust_limit)); - state->localconf.channel_reserve = dust_limit; + fmt_amount_sat(tmpctx, floor)); + state->localconf.channel_reserve = floor; } else { state->localconf.channel_reserve = reserve_sat; } diff --git a/tests/test_connection.py b/tests/test_connection.py index c1dd24f779c0..c59b3ea26584 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -5314,16 +5314,50 @@ def send_open_channel(lconn, chain_hash, temp_chan_id, funding_sat, push_msat, lconn.send_message(msg) -def read_channel_reply(lconn): - """Read past gossip chatter to openingd's answer to our open_channel.""" +def read_channel_reply_payload(lconn): + """ + Read past gossip chatter to openingd's answer to our open_channel + Returns (mtype, payload), so callers can inspect accept_channel's fields + """ for _ in range(20): msg = lconn.read_message() mtype = int.from_bytes(msg[0:2], 'big') if mtype in (WIRE_ACCEPT_CHANNEL, WIRE_WARNING, WIRE_ERROR): - return mtype + return mtype, msg[2:] raise AssertionError("no reply to open_channel") +def read_channel_reply(lconn): + mtype, _ = read_channel_reply_payload(lconn) + return mtype + + +def send_open_channel_reserve(lconn, chain_hash, temp_chan_id, funding_sat, push_msat, + dust_limit, channel_reserve, feerate_per_kw, channel_type): + """Like send_open_channel, but with a caller-chosen dust_limit / channel_reserve""" + keys = [wire.PrivateKey(bytes([i + 1] * 32)).public_key().serializeCompressed() + for i in range(6)] + + msg = struct.pack('>H', WIRE_OPEN_CHANNEL) + msg += chain_hash + msg += temp_chan_id + msg += struct.pack('>Q', funding_sat) # funding_satoshis + msg += struct.pack('>Q', push_msat) # push_msat + msg += struct.pack('>Q', dust_limit) # dust_limit_satoshis + msg += struct.pack('>Q', 0xFFFFFFFFFFFF) # max_htlc_value_in_flight_msat + msg += struct.pack('>Q', channel_reserve) # channel_reserve_satoshis + msg += struct.pack('>Q', 0) # htlc_minimum_msat + msg += struct.pack('>I', feerate_per_kw) # feerate_per_kw + msg += struct.pack('>H', 144) # to_self_delay + msg += struct.pack('>H', 483) # max_accepted_htlcs + for k in keys: + msg += k + msg += struct.pack('>B', 0) # channel_flags + msg += bytes([1, len(channel_type)]) + channel_type # TLV 1: channel_type + + lconn.send_message(msg) + + def send_funding_created(lconn, temp_chan_id): """Drive the open to the point where we build the commitment transaction. @@ -5826,3 +5860,37 @@ def test_open_channel2_tx_add_output_sum_above_max_supply(node_factory, bitcoind # node is still up assert l1.rpc.getinfo()["id"] == l1.info["id"] + + +@pytest.mark.openchannel('v1') +def test_accept_channel_reserve_below_our_dust_limit(node_factory, bitcoind): + """Our accept_channel must not set channel_reserve below our dust limit. + + As fundee, openingd used to floor the 1% channel reserve at the peer's + dust_limit_satoshis only, never at our own. A peer opening a small + channel with a low dust limit could get an accept_channel whose + dust_limit_satoshis (546, hardcoded) exceeded its own + channel_reserve_satoshis, violating BOLT #2. + """ + l1 = node_factory.get_node() + + chain_hash = bytes.fromhex(bitcoind.rpc.getblockhash(0))[::-1] + feerate = l1.rpc.feerates('perkw')['perkw']['opening'] + + lconn, channel_type = raw_peer_connect(l1) + # 1% of 30000 is 300sat, floored to our peer's 354sat dust limit -- still + # under our own hardcoded 546sat dust limit. channel_reserve_satoshis=546 + # keeps us clear of the separate issue #9439, and funding_sat is well + # clear of lightningd's default 10,000sat minimum effective capacity. + send_open_channel_reserve(lconn, chain_hash, os.urandom(32), + funding_sat=30000, push_msat=0, + dust_limit=354, channel_reserve=546, + feerate_per_kw=feerate, channel_type=channel_type) + mtype, payload = read_channel_reply_payload(lconn) + assert mtype == WIRE_ACCEPT_CHANNEL + + dust_limit = struct.unpack('>Q', payload[32:40])[0] + reserve = struct.unpack('>Q', payload[48:56])[0] + assert dust_limit <= reserve, \ + "accept_channel dust_limit_satoshis {} exceeds its channel_reserve_satoshis {}".format( + dust_limit, reserve) From 54c55b97e90a85a2a03fb3b1d23492f33ae253b9 Mon Sep 17 00:00:00 2001 From: Amperstrand Date: Thu, 12 Nov 2026 08:17:36 +0100 Subject: [PATCH 4/4] openingd: mention our own dust limit in the fundee set_reserve comment Master's comment above the fundee set_reserve() call predates the folded floor in set_reserve_absolute(), which now bumps our reserve to the higher of the two dust limits, not just the peer's. Say so. Changelog-None: comment-only Signed-off-by: Amperstrand --- openingd/openingd.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/openingd/openingd.c b/openingd/openingd.c index ec1300ac2707..37347d69dd3b 100644 --- a/openingd/openingd.c +++ b/openingd/openingd.c @@ -986,9 +986,10 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg) return NULL; } - /* This reserves 1% of the channel (rounded up), or their dust - * limit if that's higher: so check_dust_limit()'s ceiling is what - * actually bounds their dust limit here. */ + /* This reserves 1% of the channel (rounded up), or whichever dust + * limit (theirs or ours) is higher: so check_dust_limit()'s ceiling + * is what actually bounds their dust limit here, and our reserve + * stays at or above our own dust limit too. */ set_reserve(state, state->remoteconf.dust_limit); /* Pending proposal to remove these limits. */