diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 02b457844..e73dbde18 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -542,6 +542,23 @@ static struct amount_sat commit_txfee(const struct channel *channel, return commit_tx_base_fee(local_feerate, num_untrimmed_htlcs); } +static void subtract_offered_htlcs(const struct channel *channel, + struct amount_msat *amount) +{ + const struct htlc_out *hout; + struct htlc_out_map_iter outi; + struct lightningd *ld = channel->peer->ld; + + for (hout = htlc_out_map_first(&ld->htlcs_out, &outi); + hout; + hout = htlc_out_map_next(&ld->htlcs_out, &outi)) { + if (hout->key.channel != channel) + continue; + if (!amount_msat_sub(amount, *amount, hout->msat)) + *amount = AMOUNT_MSAT(0); + } +} + static void json_add_channel(struct lightningd *ld, struct json_stream *response, const char *key, const struct channel *channel) @@ -656,6 +673,9 @@ static void json_add_channel(struct lightningd *ld, channel->channel_info.their_config.channel_reserve)) spendable = AMOUNT_MSAT(0); + /* Take away any currently-offered HTLCs. */ + subtract_offered_htlcs(channel, &spendable); + /* If we're funder, subtract txfees we'll need to spend this */ if (channel->funder == LOCAL) { if (!amount_msat_sub_sat(&spendable, spendable, diff --git a/tests/test_pay.py b/tests/test_pay.py index 06841797a..586fe6d57 100644 --- a/tests/test_pay.py +++ b/tests/test_pay.py @@ -2047,7 +2047,6 @@ def test_setchannelfee_all(node_factory, bitcoind): assert result['channels'][1]['short_channel_id'] == scid3 -@pytest.mark.xfail(strict=True) def test_channel_spendable(node_factory, bitcoind): """Test that spendable_msat is accurate""" sats = 10**6