Skip to content

Commit

Permalink
onchaind: tell lightningd correct nSequence value for local leases.
Browse files Browse the repository at this point in the history
If the nSequence in the tx it produces is not at least the value we
test in the script, the tx will always fail:

```
error code: -26\nerror message:\nmandatory-script-verify-flag-failed (Locktime requirement not satisfied)
```

If we have a lease, the nSequence is max(lease-time-remaining,
to-self-delay), so have onchaind tell lightningd the correct nSequence.

Fixes: #7460
Reported-by: https://github.com/pabpas
Signed-off-by: Rusty Russell <[email protected]>
Changelog-EXPERIMENTAL: Correctly collect our own (delayed) funds if we have a unilateral close when we are still offering a lease.
  • Loading branch information
rustyrussell committed Feb 24, 2025
1 parent 5078e79 commit 79b28eb
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lightningd/onchain_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,7 @@ static void handle_onchaind_spend_to_us(struct channel *channel,
struct onchain_signing_info *info;
struct bitcoin_outpoint out;
struct amount_sat out_sats;
u32 sequence;

info = new_signing_info(msg, channel, WIRE_ONCHAIND_SPEND_TO_US);

Expand All @@ -1293,6 +1294,7 @@ static void handle_onchaind_spend_to_us(struct channel *channel,

if (!fromwire_onchaind_spend_to_us(info, msg,
&out, &out_sats,
&sequence,
&info->minblock,
&info->u.htlc_timedout.commit_num,
&info->wscript)) {
Expand All @@ -1304,8 +1306,11 @@ static void handle_onchaind_spend_to_us(struct channel *channel,
/* No real deadline on this, it's just returning to our wallet. */
info->deadline_block =
slow_sweep_deadline(channel->peer->ld->topology, channel);

/* sequence is usually channel->channel_info.their_config.to_self_delay,
* but for leases it can be greater. */
create_onchain_tx(channel, &out, out_sats,
channel->channel_info.their_config.to_self_delay, 0,
sequence, 0,
sign_tx_to_us, info,
__func__);
}
Expand Down
2 changes: 2 additions & 0 deletions onchaind/onchaind.c
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,7 @@ static void resolve_htlc_tx(struct tracked_output ***outs,

msg = towire_onchaind_spend_to_us(NULL,
&outpoint, amt,
to_self_delay[LOCAL],
rel_blockheight(out, to_self_delay[LOCAL]),
commit_num,
wscript);
Expand Down Expand Up @@ -2156,6 +2157,7 @@ static void our_unilateral_to_us(struct tracked_output ***outs,

msg = towire_onchaind_spend_to_us(NULL,
outpoint, amt,
sequence,
rel_blockheight(out, sequence),
commit_num,
local_wscript);
Expand Down
1 change: 1 addition & 0 deletions onchaind/onchaind_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ msgdata,onchaind_notify_coin_mvt,mvt,chain_coin_mvt,
msgtype,onchaind_spend_to_us,5040
msgdata,onchaind_spend_to_us,outpoint,bitcoin_outpoint,
msgdata,onchaind_spend_to_us,outpoint_amount,amount_sat,
msgdata,onchaind_spend_to_us,sequence,u32,
msgdata,onchaind_spend_to_us,minblock,u32,
msgdata,onchaind_spend_to_us,commit_num,u64,
msgdata,onchaind_spend_to_us,wscript_len,u32,
Expand Down
2 changes: 1 addition & 1 deletion onchaind/test/run-grind_feerate.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ u8 *towire_onchaind_spend_htlc_timeout(const tal_t *ctx UNNEEDED, const struct b
u8 *towire_onchaind_spend_penalty(const tal_t *ctx UNNEEDED, const struct bitcoin_outpoint *outpoint UNNEEDED, struct amount_sat outpoint_amount UNNEEDED, const struct secret *remote_per_commitment_secret UNNEEDED, const u8 *stack_elem UNNEEDED, const u8 *wscript UNNEEDED)
{ fprintf(stderr, "towire_onchaind_spend_penalty called!\n"); abort(); }
/* Generated stub for towire_onchaind_spend_to_us */
u8 *towire_onchaind_spend_to_us(const tal_t *ctx UNNEEDED, const struct bitcoin_outpoint *outpoint UNNEEDED, struct amount_sat outpoint_amount UNNEEDED, u32 minblock UNNEEDED, u64 commit_num UNNEEDED, const u8 *wscript UNNEEDED)
u8 *towire_onchaind_spend_to_us(const tal_t *ctx UNNEEDED, const struct bitcoin_outpoint *outpoint UNNEEDED, struct amount_sat outpoint_amount UNNEEDED, u32 sequence UNNEEDED, u32 minblock UNNEEDED, u64 commit_num UNNEEDED, const u8 *wscript UNNEEDED)
{ fprintf(stderr, "towire_onchaind_spend_to_us called!\n"); abort(); }
/* Generated stub for towire_onchaind_spent_reply */
u8 *towire_onchaind_spent_reply(const tal_t *ctx UNNEEDED, bool interested UNNEEDED)
Expand Down
1 change: 0 additions & 1 deletion tests/test_closing.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,6 @@ def test_channel_lease_post_expiry(node_factory, bitcoind, chainparams):
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
@pytest.mark.openchannel('v2')
@pytest.mark.slow_test
@pytest.mark.xfail(strict=True)
def test_channel_lease_unilat_closes(node_factory, bitcoind):
'''
Check that channel leases work
Expand Down

0 comments on commit 79b28eb

Please sign in to comment.