diff --git a/channeld/commit_tx.c b/channeld/commit_tx.c index 045f1a833..cdee2bd92 100644 --- a/channeld/commit_tx.c +++ b/channeld/commit_tx.c @@ -90,6 +90,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, const struct pubkey *remote_funding_key, enum side opener, u16 to_self_delay, + u32 lease_expiry, + u32 blockheight, const struct keyset *keyset, u32 feerate_per_kw, struct amount_sat dust_limit, @@ -234,9 +236,16 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, * output](#to_local-output). */ if (amount_msat_greater_eq_sat(self_pay, dust_limit)) { + /* BOLT- #3: + * In a leased channel, the `to_local` output that + * pays the `accepter` node is modified so that its + * CSV is equal to the greater of the + * `to_self_delay` or the `lease_end` - `blockheight`. + */ u8 *wscript = to_self_wscript(tmpctx, to_self_delay, - 1, /* FIXME: csv_lock */ + lease_expiry > blockheight ? + lease_expiry - blockheight : 0, keyset); u8 *p2wsh = scriptpubkey_p2wsh(tx, wscript); struct amount_sat amount = amount_msat_to_sat_round_down(self_pay); @@ -278,9 +287,22 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, * Otherwise, this output is a simple P2WPKH to `remotepubkey`. */ if (option_anchor_outputs) { - /* FIXME: use csv_lock */ + /* BOLT- #3: + * ##### Leased channel (`option_will_fund`) + * + * If a `lease` applies to the channel, the + * `to_remote` output of the `initiator` + * ensures the `leasor` funds are not + * spendable until the lease expires. + * + * OP_CHECKSIGVERIFY + * 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, 1)); + anchor_to_remote_redeem(tmpctx, &keyset->other_payment_key, csv_lock)); } else { scriptpubkey = scriptpubkey_p2wpkh(tmpctx, &keyset->other_payment_key); diff --git a/channeld/commit_tx.h b/channeld/commit_tx.h index 54ebcebd2..31fe5ac6e 100644 --- a/channeld/commit_tx.h +++ b/channeld/commit_tx.h @@ -58,6 +58,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, const struct pubkey *remote_funding_key, enum side opener, u16 to_self_delay, + u32 lease_expiry, + u32 blockheight, const struct keyset *keyset, u32 feerate_per_kw, struct amount_sat dust_limit, diff --git a/channeld/full_channel.c b/channeld/full_channel.c index 684859a50..f9291a8d7 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -341,8 +341,10 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx, &channel->funding_pubkey[side], &channel->funding_pubkey[!side], channel->opener, - channel->config[!side].to_self_delay, &keyset, - channel_feerate(channel, side), + channel->config[!side].to_self_delay, + channel->lease_expiry, + channel_blockheight(channel, side), + &keyset, channel_feerate(channel, side), channel->config[side].dust_limit, channel->view[side].owed[side], channel->view[side].owed[!side], committed, htlcmap, direct_outputs, commitment_number ^ channel->commitment_number_obscurer, diff --git a/channeld/test/run-commit_tx.c b/channeld/test/run-commit_tx.c index 66590b49f..2006d7194 100644 --- a/channeld/test/run-commit_tx.c +++ b/channeld/test/run-commit_tx.c @@ -800,6 +800,7 @@ int main(int argc, const char *argv[]) &local_funding_pubkey, &remote_funding_pubkey, LOCAL, to_self_delay, + 0, 0, /* No lease */ &keyset, feerate_per_kw, dust_limit, @@ -815,6 +816,7 @@ int main(int argc, const char *argv[]) &local_funding_pubkey, &remote_funding_pubkey, REMOTE, to_self_delay, + 0, 0, /* No lease */ &keyset, feerate_per_kw, dust_limit, @@ -863,6 +865,7 @@ int main(int argc, const char *argv[]) &local_funding_pubkey, &remote_funding_pubkey, LOCAL, to_self_delay, + 0, 0, /* No lease */ &keyset, feerate_per_kw, dust_limit, @@ -878,6 +881,7 @@ int main(int argc, const char *argv[]) &local_funding_pubkey, &remote_funding_pubkey, REMOTE, to_self_delay, + 0, 0, /* No lease */ &keyset, feerate_per_kw, dust_limit, @@ -914,6 +918,7 @@ int main(int argc, const char *argv[]) &local_funding_pubkey, &remote_funding_pubkey, LOCAL, to_self_delay, + 0, 0, /* No lease */ &keyset, feerate_per_kw, dust_limit, @@ -930,6 +935,7 @@ int main(int argc, const char *argv[]) &local_funding_pubkey, &remote_funding_pubkey, REMOTE, to_self_delay, + 0, 0, /* No lease */ &keyset, feerate_per_kw, dust_limit, @@ -972,6 +978,7 @@ int main(int argc, const char *argv[]) &local_funding_pubkey, &remote_funding_pubkey, LOCAL, to_self_delay, + 0, 0, /* No lease */ &keyset, feerate_per_kw-1, dust_limit, @@ -1020,6 +1027,7 @@ int main(int argc, const char *argv[]) &local_funding_pubkey, &remote_funding_pubkey, LOCAL, to_self_delay, + 0, 0, /* No lease */ &keyset, feerate_per_kw, dust_limit, @@ -1094,6 +1102,7 @@ int main(int argc, const char *argv[]) &local_funding_pubkey, &remote_funding_pubkey, LOCAL, to_self_delay, + 0, 0, /* No lease */ &keyset, feerate_per_kw, dust_limit, @@ -1148,6 +1157,7 @@ int main(int argc, const char *argv[]) &local_funding_pubkey, &remote_funding_pubkey, LOCAL, to_self_delay, + 0, 0, /* No lease */ &keyset, feerate_per_kw, dust_limit, @@ -1163,6 +1173,7 @@ int main(int argc, const char *argv[]) &local_funding_pubkey, &remote_funding_pubkey, REMOTE, to_self_delay, + 0, 0, /* No lease */ &keyset, feerate_per_kw, dust_limit, diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index db33e6ec5..3ab15b239 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -534,6 +534,7 @@ int main(int argc, const char *argv[]) &local_funding_pubkey, &remote_funding_pubkey, LOCAL, remote_config->to_self_delay, + 0, 0, /* No lease */ &keyset, feerate_per_kw[LOCAL], local_config->dust_limit, @@ -664,6 +665,7 @@ int main(int argc, const char *argv[]) &local_funding_pubkey, &remote_funding_pubkey, LOCAL, remote_config->to_self_delay, + 0, 0, /* No lease */ &keyset, feerate_per_kw[LOCAL], local_config->dust_limit, to_local, to_remote, htlcs, &htlc_map, NULL, 0x2bb038521914 ^ 42,