Browse Source

initial_commit_tx, commit_tx: add anchor outputs if needed.

This also means we subtract 660 satoshis more everywhere we subtract
the base fee (except for mutual close, where the base fee is still
used).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
bump-pyln-proto
Rusty Russell 4 years ago
parent
commit
e7423888ba
  1. 25
      bitcoin/script.c
  2. 4
      bitcoin/script.h
  3. 84
      channeld/commit_tx.c
  4. 4
      channeld/commit_tx.h
  5. 63
      channeld/full_channel.c
  6. 19
      channeld/test/run-commit_tx.c
  7. 7
      channeld/test/run-full_channel.c
  8. 56
      common/initial_commit_tx.c
  9. 4
      common/initial_commit_tx.h
  10. 9
      devtools/mkclose.c
  11. 17
      lightningd/peer_control.c

25
bitcoin/script.c

@ -23,6 +23,7 @@
#define OP_ENDIF 0x68
#define OP_RETURN 0x6a
#define OP_2DROP 0x6d
#define OP_IFDUP 0x73
#define OP_DEPTH 0x74
#define OP_DROP 0x75
#define OP_DUP 0x76
@ -788,6 +789,30 @@ u8 *bitcoin_wscript_htlc_tx(const tal_t *ctx,
return script;
}
u8 *bitcoin_wscript_anchor(const tal_t *ctx,
const struct pubkey *funding_pubkey)
{
u8 *script = tal_arr(ctx, u8, 0);
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
* #### `to_local_anchor` and `to_remote_anchor` Output (option_anchor_outputs)
*...
* <local_funding_pubkey/remote_funding_pubkey> OP_CHECKSIG OP_IFDUP
* OP_NOTIF
* OP_16 OP_CHECKSEQUENCEVERIFY
* OP_ENDIF
*/
add_push_key(&script, funding_pubkey);
add_op(&script, OP_CHECKSIG);
add_op(&script, OP_IFDUP);
add_op(&script, OP_NOTIF);
add_number(&script, 16);
add_op(&script, OP_CHECKSEQUENCEVERIFY);
add_op(&script, OP_ENDIF);
return script;
}
bool scripteq(const u8 *s1, const u8 *s2)
{
memcheck(s1, tal_count(s1));

4
bitcoin/script.h

@ -126,6 +126,10 @@ u8 *bitcoin_wscript_htlc_tx(const tal_t *ctx,
const struct pubkey *revocation_pubkey,
const struct pubkey *local_delayedkey);
/* Anchor outputs */
u8 *bitcoin_wscript_anchor(const tal_t *ctx,
const struct pubkey *funding_pubkey);
/* Is this a pay to pubkey hash? (extract addr if not NULL) */
bool is_p2pkh(const u8 *script, struct bitcoin_address *addr);

84
channeld/commit_tx.c

@ -15,36 +15,41 @@
static bool trim(const struct htlc *htlc,
u32 feerate_per_kw,
struct amount_sat dust_limit,
bool option_anchor_outputs,
enum side side)
{
return htlc_is_trimmed(htlc_owner(htlc), htlc->amount,
feerate_per_kw, dust_limit, side,
false /* FIXME-anchor */);
option_anchor_outputs);
}
size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
u32 feerate_per_kw,
struct amount_sat dust_limit,
bool option_anchor_outputs,
enum side side)
{
size_t i, n;
for (i = n = 0; i < tal_count(htlcs); i++)
n += !trim(htlcs[i], feerate_per_kw, dust_limit, side);
n += !trim(htlcs[i], feerate_per_kw, dust_limit,
option_anchor_outputs, side);
return n;
}
static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n,
const struct htlc *htlc,
const struct keyset *keyset)
const struct keyset *keyset,
bool option_anchor_outputs)
{
struct ripemd160 ripemd;
u8 *wscript, *p2wsh;
struct amount_sat amount = amount_msat_to_sat_round_down(htlc->amount);
ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
wscript = htlc_offered_wscript(tx, &ripemd, keyset, false /* FIXME-anchor */);
wscript = htlc_offered_wscript(tx, &ripemd, keyset,
option_anchor_outputs);
p2wsh = scriptpubkey_p2wsh(tx, wscript);
bitcoin_tx_add_output(tx, p2wsh, wscript, amount);
SUPERVERBOSE("# HTLC %" PRIu64 " offered %s wscript %s\n", htlc->id,
@ -55,14 +60,16 @@ static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n,
static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
const struct htlc *htlc,
const struct keyset *keyset)
const struct keyset *keyset,
bool option_anchor_outputs)
{
struct ripemd160 ripemd;
u8 *wscript, *p2wsh;
struct amount_sat amount;
ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
wscript = htlc_received_wscript(tx, &ripemd, &htlc->expiry, keyset, false /* FIXME-anchor */);
wscript = htlc_received_wscript(tx, &ripemd, &htlc->expiry, keyset,
option_anchor_outputs);
p2wsh = scriptpubkey_p2wsh(tx, wscript);
amount = amount_msat_to_sat_round_down(htlc->amount);
@ -93,6 +100,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
const struct htlc ***htlcmap,
struct wally_tx_output *direct_outputs[NUM_SIDES],
u64 obscured_commitment_number,
bool option_anchor_outputs,
enum side side)
{
struct amount_sat base_fee;
@ -100,6 +108,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
struct bitcoin_tx *tx;
size_t i, n, untrimmed;
u32 *cltvs;
bool to_local, to_remote;
struct htlc *dummy_to_local = (struct htlc *)0x01,
*dummy_to_remote = (struct htlc *)0x02;
const u8 *funding_wscript = bitcoin_redeem_2of2(tmpctx,
@ -117,7 +126,9 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
*/
untrimmed = commit_tx_num_untrimmed(htlcs,
feerate_per_kw,
dust_limit, side);
dust_limit,
option_anchor_outputs,
side);
/* BOLT #3:
*
@ -125,11 +136,22 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* fee](#fee-calculation).
*/
base_fee = commit_tx_base_fee(feerate_per_kw, untrimmed,
false /* FIXME-anchor */);
option_anchor_outputs);
SUPERVERBOSE("# base commitment transaction fee = %s\n",
type_to_string(tmpctx, struct amount_sat, &base_fee));
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1:
* If `option_anchor_outputs` applies to the commitment
* transaction, also subtract two times the fixed anchor size
* of 330 sats from the funder (either `to_local` or
* `to_remote`).
*/
if (option_anchor_outputs
&& !amount_sat_add(&base_fee, base_fee, AMOUNT_SAT(660)))
/* Can't overflow: feerate is u32. */
abort();
/* BOLT #3:
*
* 3. Subtract this base fee from the funder (either `to_local` or
@ -142,7 +164,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
struct amount_sat out = AMOUNT_SAT(0);
bool ok = true;
for (i = 0; i < tal_count(htlcs); i++) {
if (!trim(htlcs[i], feerate_per_kw, dust_limit, side))
if (!trim(htlcs[i], feerate_per_kw, dust_limit,
option_anchor_outputs, side))
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(htlcs[i]->amount));
}
if (amount_msat_greater_sat(self_pay, dust_limit))
@ -155,8 +178,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
}
#endif
/* Worst-case sizing: both to-local and to-remote outputs. */
tx = bitcoin_tx(ctx, chainparams, 1, untrimmed + 2, 0);
/* Worst-case sizing: both to-local and to-remote outputs, and anchors. */
tx = bitcoin_tx(ctx, chainparams, 1, untrimmed + 2 + 2, 0);
/* We keep track of which outputs have which HTLCs */
*htlcmap = tal_arr(tx, const struct htlc *, tx->wtx->outputs_allocation_len);
@ -177,9 +200,11 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
for (i = 0; i < tal_count(htlcs); i++) {
if (htlc_owner(htlcs[i]) != side)
continue;
if (trim(htlcs[i], feerate_per_kw, dust_limit, side))
if (trim(htlcs[i], feerate_per_kw, dust_limit,
option_anchor_outputs, side))
continue;
add_offered_htlc_out(tx, n, htlcs[i], keyset);
add_offered_htlc_out(tx, n, htlcs[i], keyset,
option_anchor_outputs);
(*htlcmap)[n] = htlcs[i];
cltvs[n] = abs_locktime_to_blocks(&htlcs[i]->expiry);
n++;
@ -193,9 +218,11 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
for (i = 0; i < tal_count(htlcs); i++) {
if (htlc_owner(htlcs[i]) == side)
continue;
if (trim(htlcs[i], feerate_per_kw, dust_limit, side))
if (trim(htlcs[i], feerate_per_kw, dust_limit,
option_anchor_outputs, side))
continue;
add_received_htlc_out(tx, n, htlcs[i], keyset);
add_received_htlc_out(tx, n, htlcs[i], keyset,
option_anchor_outputs);
(*htlcmap)[n] = htlcs[i];
cltvs[n] = abs_locktime_to_blocks(&htlcs[i]->expiry);
n++;
@ -221,7 +248,9 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
type_to_string(tmpctx, struct amount_sat, &amount),
tal_hex(tmpctx, wscript));
n++;
}
to_local = true;
} else
to_local = false;
/* BOLT #3:
*
@ -251,6 +280,29 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
type_to_string(tmpctx, struct pubkey,
&keyset->other_payment_key));
n++;
to_remote = true;
} else
to_remote = false;
if (option_anchor_outputs) {
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
* if `to_local` exists or there are untrimmed HTLCs, add a `to_local_anchor` output
*/
if (to_local || untrimmed != 0) {
tx_add_anchor_output(tx, local_funding_key);
(*htlcmap)[n] = NULL;
n++;
}
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
* if `to_remote` exists or there are untrimmed HTLCs, add a `to_remote_anchor` output
*/
if (to_remote || untrimmed != 0) {
tx_add_anchor_output(tx, remote_funding_key);
(*htlcmap)[n] = NULL;
n++;
}
}
/* BOLT #2:

4
channeld/commit_tx.h

@ -14,6 +14,7 @@ struct keyset;
* @htlcs: tal_arr of HTLCs
* @feerate_per_kw: feerate to use
* @dust_limit: dust limit below which to trim outputs.
* @option_anchor_outputs: does option_anchor_outputs apply to this channel?
* @side: from which side's point of view
*
* We need @side because HTLC fees are different for offered and
@ -22,6 +23,7 @@ struct keyset;
size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
u32 feerate_per_kw,
struct amount_sat dust_limit,
bool option_anchor_outputs,
enum side side);
/**
@ -39,6 +41,7 @@ size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
* @htlc_map: outputed map of outnum->HTLC (NULL for direct outputs).
* @obscured_commitment_number: number to encode in commitment transaction
* @direct_outputs: If non-NULL, fill with pointers to the direct (non-HTLC) outputs (or NULL if none).
* @option_anchor_outputs: does option_anchor_outputs apply to this channel?
* @side: side to generate commitment transaction for.
*
* We need to be able to generate the remote side's tx to create signatures,
@ -62,6 +65,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
const struct htlc ***htlcmap,
struct wally_tx_output *direct_outputs[NUM_SIDES],
u64 obscured_commitment_number,
bool option_anchor_outputs,
enum side side);
#endif /* LIGHTNING_CHANNELD_COMMIT_TX_H */

63
channeld/full_channel.c

@ -313,6 +313,7 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx,
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,
false /* FIXME-anchor */,
side);
/* Set the remote/local pubkeys on the commitment tx psbt */
@ -371,13 +372,17 @@ static bool get_room_above_reserve(const struct channel *channel,
static size_t num_untrimmed_htlcs(enum side side,
struct amount_sat dust_limit,
u32 feerate,
bool option_static_remotekey,
const struct htlc **committed,
const struct htlc **adding,
const struct htlc **removing)
{
return commit_tx_num_untrimmed(committed, feerate, dust_limit, side)
+ commit_tx_num_untrimmed(adding, feerate, dust_limit, side)
- commit_tx_num_untrimmed(removing, feerate, dust_limit, side);
return commit_tx_num_untrimmed(committed, feerate, dust_limit,
option_static_remotekey, side)
+ commit_tx_num_untrimmed(adding, feerate, dust_limit,
option_static_remotekey, side)
- commit_tx_num_untrimmed(removing, feerate, dust_limit,
option_static_remotekey, side);
}
static struct amount_sat fee_for_htlcs(const struct channel *channel,
@ -391,6 +396,7 @@ static struct amount_sat fee_for_htlcs(const struct channel *channel,
size_t untrimmed;
untrimmed = num_untrimmed_htlcs(side, dust_limit, feerate,
false /* FIXME-anchor */,
committed, adding, removing);
return commit_tx_base_fee(feerate, untrimmed,
@ -438,6 +444,7 @@ static bool local_opener_has_fee_headroom(const struct channel *channel,
* only *reduce* this number, so use current feerate here! */
untrimmed = num_untrimmed_htlcs(LOCAL, channel->config[LOCAL].dust_limit,
feerate,
false /* FIXME-anchor */,
committed, adding, removing);
/* Now, how much would it cost us if feerate increases 100% and we added
@ -629,6 +636,17 @@ static enum channel_add_err add_htlc(struct channel *channel,
&remainder))
return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED;
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1:
* If `option_anchor_outputs` applies to the commitment
* transaction, also subtract two times the fixed anchor size
* of 330 sats from the funder (either `to_local` or
* `to_remote`).
*/
if (false /* FIXME-anchor */
&& channel->opener == sender
&& !amount_msat_sub_sat(&remainder, remainder, AMOUNT_SAT(660)))
return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED;
if (channel->opener== sender) {
if (amount_msat_less_sat(remainder, fee)) {
status_debug("Cannot afford fee %s with %s above reserve",
@ -657,6 +675,12 @@ static enum channel_add_err add_htlc(struct channel *channel,
channel->opener,
&remainder))
return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED;
if (false /* FIXME-anchor */
&& channel->opener != sender
&& !amount_msat_sub_sat(&remainder, remainder, AMOUNT_SAT(660)))
return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED;
/* Should be able to afford both their own commit tx
* fee, and other's commit tx fee, which are subtly
* different! */
@ -1014,11 +1038,24 @@ u32 approx_max_feerate(const struct channel *channel)
weight = 724 + 172 * num;
weight = elements_add_overhead(weight, 1, num + 2);
/* Available is their view */
avail = amount_msat_to_sat_round_down(channel->view[!channel->opener].owed[channel->opener]);
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1:
* If `option_anchor_outputs` applies to the commitment
* transaction, also subtract two times the fixed anchor size
* of 330 sats from the funder (either `to_local` or
* `to_remote`).
*/
if (false /* FIXME-anchor */
&& !amount_sat_sub(&avail, avail, AMOUNT_SAT(660))) {
avail = AMOUNT_SAT(0);
} else {
/* We should never go below reserve. */
if (!amount_sat_sub(&avail,
amount_msat_to_sat_round_down(channel->view[!channel->opener].owed[channel->opener]),
if (!amount_sat_sub(&avail, avail,
channel->config[!channel->opener].channel_reserve))
avail = AMOUNT_SAT(0);
}
return avail.satoshis / weight * 1000; /* Raw: once-off reverse feerate*/
}
@ -1033,15 +1070,31 @@ bool can_opener_afford_feerate(const struct channel *channel, u32 feerate_per_kw
&committed, &removing, &adding);
untrimmed = commit_tx_num_untrimmed(committed, feerate_per_kw, dust_limit,
false /* FIXME-anchor */,
!channel->opener)
+ commit_tx_num_untrimmed(adding, feerate_per_kw, dust_limit,
false /* FIXME-anchor */,
!channel->opener)
- commit_tx_num_untrimmed(removing, feerate_per_kw, dust_limit,
false /* FIXME-anchor */,
!channel->opener);
fee = commit_tx_base_fee(feerate_per_kw, untrimmed,
false /* FIXME-anchor */);
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1:
* If `option_anchor_outputs` applies to the commitment
* transaction, also subtract two times the fixed anchor size
* of 330 sats from the funder (either `to_local` or
* `to_remote`).
*/
if (false /* FIXME-anchor */
&& !amount_sat_add(&fee, fee, AMOUNT_SAT(660)))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Cannot add 660 sats to %s for anchor",
type_to_string(tmpctx, struct amount_sat,
&fee));
/* BOLT #2:
*
* - if the sender cannot afford the new fee rate on the receiving

19
channeld/test/run-commit_tx.c

@ -756,6 +756,7 @@ int main(int argc, const char *argv[])
to_local,
to_remote,
NULL, &htlc_map, NULL, commitment_number ^ cn_obscurer,
option_anchor_outputs,
LOCAL);
print_superverbose = false;
tx2 = commit_tx(tmpctx,
@ -770,6 +771,7 @@ int main(int argc, const char *argv[])
to_local,
to_remote,
NULL, &htlc_map2, NULL, commitment_number ^ cn_obscurer,
option_anchor_outputs,
REMOTE);
tx_must_be_eq(tx, tx2);
report(tx, wscript, &x_remote_funding_privkey, &remote_funding_pubkey,
@ -817,6 +819,7 @@ int main(int argc, const char *argv[])
to_local,
to_remote,
htlcs, &htlc_map, NULL, commitment_number ^ cn_obscurer,
option_anchor_outputs,
LOCAL);
print_superverbose = false;
tx2 = commit_tx(tmpctx,
@ -832,6 +835,7 @@ int main(int argc, const char *argv[])
to_remote,
inv_htlcs, &htlc_map2, NULL,
commitment_number ^ cn_obscurer,
option_anchor_outputs,
REMOTE);
tx_must_be_eq(tx, tx2);
report(tx, wscript, &x_remote_funding_privkey, &remote_funding_pubkey,
@ -867,6 +871,7 @@ int main(int argc, const char *argv[])
to_remote,
htlcs, &htlc_map, NULL,
commitment_number ^ cn_obscurer,
option_anchor_outputs,
LOCAL);
/* This is what it would look like for peer generating it! */
tx2 = commit_tx(tmpctx,
@ -882,6 +887,7 @@ int main(int argc, const char *argv[])
to_remote,
inv_htlcs, &htlc_map2, NULL,
commitment_number ^ cn_obscurer,
option_anchor_outputs,
REMOTE);
tx_must_be_eq(newtx, tx2);
#ifdef DEBUG
@ -916,6 +922,7 @@ int main(int argc, const char *argv[])
to_remote,
htlcs, &htlc_map, NULL,
commitment_number ^ cn_obscurer,
option_anchor_outputs,
LOCAL);
report(tx, wscript,
&x_remote_funding_privkey, &remote_funding_pubkey,
@ -956,6 +963,7 @@ int main(int argc, const char *argv[])
to_remote,
htlcs, &htlc_map, NULL,
commitment_number ^ cn_obscurer,
option_anchor_outputs,
LOCAL);
report(newtx, wscript,
&x_remote_funding_privkey, &remote_funding_pubkey,
@ -986,6 +994,16 @@ int main(int argc, const char *argv[])
= commit_tx_base_fee(feerate_per_kw, 0,
option_anchor_outputs);
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1:
* If `option_anchor_outputs` applies to the commitment
* transaction, also subtract two times the fixed anchor size
* of 330 sats from the funder (either `to_local` or
* `to_remote`).
*/
if (option_anchor_outputs
&& !amount_sat_add(&base_fee, base_fee, AMOUNT_SAT(660)))
abort();
if (amount_msat_greater_eq_sat(to_local, base_fee)) {
feerate_per_kw++;
continue;
@ -1019,6 +1037,7 @@ int main(int argc, const char *argv[])
to_remote,
htlcs, &htlc_map, NULL,
commitment_number ^ cn_obscurer,
option_anchor_outputs,
LOCAL);
report(tx, wscript,
&x_remote_funding_privkey, &remote_funding_pubkey,

7
channeld/test/run-full_channel.c

@ -372,6 +372,7 @@ int main(int argc, const char *argv[])
struct amount_msat to_local, to_remote;
const struct htlc **htlc_map, **htlcs;
const u8 *funding_wscript, *funding_wscript_alt;
bool option_anchor_outputs = false;
size_t i;
chainparams = chainparams_for_network("bitcoin");
@ -533,7 +534,8 @@ int main(int argc, const char *argv[])
local_config->dust_limit,
to_local,
to_remote,
NULL, &htlc_map, NULL, 0x2bb038521914 ^ 42, LOCAL);
NULL, &htlc_map, NULL, 0x2bb038521914 ^ 42,
option_anchor_outputs, LOCAL);
txs = channel_txs(tmpctx,
&htlc_map, NULL, &funding_wscript_alt,
@ -659,7 +661,8 @@ int main(int argc, const char *argv[])
LOCAL, remote_config->to_self_delay,
&keyset, feerate_per_kw[LOCAL], local_config->dust_limit,
to_local, to_remote, htlcs, &htlc_map, NULL,
0x2bb038521914 ^ 42, LOCAL);
0x2bb038521914 ^ 42,
option_anchor_outputs, LOCAL);
txs = channel_txs(tmpctx, &htlc_map, NULL, &funding_wscript,
lchannel, &local_per_commitment_point, 42,

56
common/initial_commit_tx.c

@ -58,6 +58,19 @@ u8 *to_self_wscript(const tal_t *ctx,
&keyset->self_delayed_payment_key);
}
void tx_add_anchor_output(struct bitcoin_tx *tx,
const struct pubkey *funding_key)
{
u8 *wscript = bitcoin_wscript_anchor(tmpctx, funding_key);
u8 *p2wsh = scriptpubkey_p2wsh(tmpctx, wscript);
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
* The amount of the output is fixed at 330 sats, the default
* dust limit for P2WSH.
*/
bitcoin_tx_add_output(tx, p2wsh, wscript, AMOUNT_SAT(330));
}
struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
@ -80,6 +93,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
struct amount_sat base_fee;
struct bitcoin_tx *tx;
size_t n, untrimmed;
bool to_local, to_remote;
struct amount_msat total_pay;
struct amount_sat amount;
u32 sequence;
@ -108,6 +122,18 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
base_fee = commit_tx_base_fee(feerate_per_kw, untrimmed,
option_anchor_outputs);
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1:
* If `option_anchor_outputs` applies to the commitment
* transaction, also subtract two times the fixed anchor size
* of 330 sats from the funder (either `to_local` or
* `to_remote`).
*/
if (option_anchor_outputs
&& !amount_sat_add(&base_fee, base_fee, AMOUNT_SAT(660))) {
*err_reason = "Funder cannot afford anchor outputs";
return NULL;
}
/* BOLT #3:
*
* 3. Subtract this base fee from the funder (either `to_local` or
@ -153,8 +179,8 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
}
/* Worst-case sizing: both to-local and to-remote outputs. */
tx = bitcoin_tx(ctx, chainparams, 1, untrimmed + 2, 0);
/* Worst-case sizing: both to-local and to-remote outputs + anchors. */
tx = bitcoin_tx(ctx, chainparams, 1, untrimmed + 4, 0);
/* This could be done in a single loop, but we follow the BOLT
* literally to make comments in test vectors clearer. */
@ -186,7 +212,9 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
assert(pos == n);
output_order[n] = dummy_local;
n++;
}
to_local = true;
} else
to_local = false;
/* BOLT #3:
*
@ -209,6 +237,28 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
assert(pos == n);
output_order[n] = dummy_remote;
n++;
to_remote = true;
} else
to_remote = false;
if (option_anchor_outputs) {
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
* if `to_local` exists or there are untrimmed HTLCs, add a `to_local_anchor` output
*/
if (to_local || untrimmed != 0) {
tx_add_anchor_output(tx, &funding_key[side]);
output_order[n] = NULL;
n++;
}
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
* if `to_remote` exists or there are untrimmed HTLCs, add a `to_remote_anchor` output
*/
if (to_remote || untrimmed != 0) {
tx_add_anchor_output(tx, &funding_key[!side]);
output_order[n] = NULL;
n++;
}
}
assert(n <= tx->wtx->num_outputs);

4
common/initial_commit_tx.h

@ -132,4 +132,8 @@ u8 *to_self_wscript(const tal_t *ctx,
/* To-other is simply: scriptpubkey_p2wpkh(tx, keyset->other_payment_key) */
/* If we determine we need one, append this anchor output */
void tx_add_anchor_output(struct bitcoin_tx *tx,
const struct pubkey *funding_key);
#endif /* LIGHTNING_COMMON_INITIAL_COMMIT_TX_H */

9
devtools/mkclose.c

@ -120,6 +120,15 @@ int main(int argc, char *argv[])
fee = commit_tx_base_fee(feerate_per_kw, 0,
option_anchor_outputs);
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1:
* If `option_anchor_outputs` applies to the commitment
* transaction, also subtract two times the fixed anchor size
* of 330 sats from the funder (either `to_local` or
* `to_remote`).
*/
if (option_anchor_outputs && !amount_sat_add(&fee, fee, AMOUNT_SAT(660)))
errx(1, "Can't afford anchors");
if (!amount_msat_sub_sat(&local_msat, local_msat, fee))
errx(1, "Can't afford fee %s",
type_to_string(NULL, struct amount_sat, &fee));

17
lightningd/peer_control.c

@ -528,6 +528,8 @@ static struct amount_sat commit_txfee(const struct channel *channel,
u32 feerate = get_feerate(channel->channel_info.fee_states,
channel->opener, side);
struct amount_sat dust_limit;
struct amount_sat fee;
if (side == LOCAL)
dust_limit = channel->our_config.dust_limit;
if (side == REMOTE)
@ -572,8 +574,21 @@ static struct amount_sat commit_txfee(const struct channel *channel,
* ("fee spike buffer"). A buffer of 2*feerate_per_kw is
* recommended to ensure predictability.
*/
return commit_tx_base_fee(2 * feerate, num_untrimmed_htlcs + 1,
fee = commit_tx_base_fee(2 * feerate, num_untrimmed_htlcs + 1,
false /* FIXME-anchor */);
if (false /* FIXME-anchor */) {
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1:
* If `option_anchor_outputs` applies to the commitment
* transaction, also subtract two times the fixed anchor size
* of 330 sats from the funder (either `to_local` or
* `to_remote`).
*/
if (!amount_sat_add(&fee, fee, AMOUNT_SAT(660)))
; /* fee is somehow astronomical already.... */
}
return fee;
}
static void subtract_offered_htlcs(const struct channel *channel,

Loading…
Cancel
Save