channel lease: set csv_lock for commitment transactions

If the channel is under lease, the lessor's (accepter, really)
funds have a CSV lock that reflects the length of the remainder of the
lease.
This commit is contained in:
niftynei 2021-07-02 14:46:20 -05:00 committed by neil saitug
parent 43ae30df21
commit 16c6ef546a
2 changed files with 19 additions and 9 deletions

View File

@ -108,6 +108,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
struct amount_msat total_pay;
struct bitcoin_tx *tx;
size_t i, n, untrimmed;
/* Is this the lessor ? */
enum side lessor = !opener;
u32 *cltvs;
bool to_local, to_remote;
struct htlc *dummy_to_local = (struct htlc *)0x01,
@ -115,6 +117,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
const u8 *funding_wscript = bitcoin_redeem_2of2(tmpctx,
local_funding_key,
remote_funding_key);
u32 csv_lock = lease_expiry > blockheight ?
lease_expiry - blockheight : 1;
if (!amount_msat_add(&total_pay, self_pay, other_pay))
abort();
@ -244,8 +248,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
*/
u8 *wscript = to_self_wscript(tmpctx,
to_self_delay,
lease_expiry > blockheight ?
lease_expiry - blockheight : 0,
side == lessor ? csv_lock : 0,
keyset);
u8 *p2wsh = scriptpubkey_p2wsh(tx, wscript);
struct amount_sat amount = amount_msat_to_sat_round_down(self_pay);
@ -287,6 +290,11 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* Otherwise, this output is a simple P2WPKH to `remotepubkey`.
*/
if (option_anchor_outputs) {
const u8 *redeem
= anchor_to_remote_redeem(tmpctx,
&keyset->other_payment_key,
(!side) == lessor ?
csv_lock : 1);
/* BOLT- #3:
* ##### Leased channel (`option_will_fund`)
*
@ -299,10 +307,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* MAX(1, lease_end - blockheight)
* OP_CHECKSEQUENCEVERIFY
*/
u32 csv_lock = lease_expiry > blockheight ?
lease_expiry - blockheight : 1;
scriptpubkey = scriptpubkey_p2wsh(tmpctx,
anchor_to_remote_redeem(tmpctx, &keyset->other_payment_key, csv_lock));
scriptpubkey = scriptpubkey_p2wsh(tmpctx, redeem);
} else {
scriptpubkey = scriptpubkey_p2wpkh(tmpctx,
&keyset->other_payment_key);

View File

@ -98,6 +98,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
bool to_local, to_remote;
struct amount_msat total_pay;
struct amount_sat amount;
enum side lessor = !opener;
u32 sequence;
void *dummy_local = (void *)LOCAL, *dummy_remote = (void *)REMOTE;
const void *output_order[NUM_SIDES];
@ -211,7 +212,8 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
*/
if (amount_msat_greater_eq_sat(self_pay, dust_limit)) {
u8 *wscript = to_self_wscript(tmpctx,
to_self_delay, csv_lock,
to_self_delay,
side == lessor ? csv_lock : 0,
keyset);
amount = amount_msat_to_sat_round_down(self_pay);
int pos = bitcoin_tx_add_output(
@ -245,8 +247,11 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
amount = amount_msat_to_sat_round_down(other_pay);
if (option_anchor_outputs) {
scriptpubkey = scriptpubkey_p2wsh(tmpctx,
anchor_to_remote_redeem(tmpctx, &keyset->other_payment_key, csv_lock));
const u8 *redeem
= anchor_to_remote_redeem(tmpctx,
&keyset->other_payment_key,
(!side) == lessor ? csv_lock : 1);
scriptpubkey = scriptpubkey_p2wsh(tmpctx, redeem);
} else {
scriptpubkey = scriptpubkey_p2wpkh(tmpctx,
&keyset->other_payment_key);