Browse Source

channeld, openingd: take into account option_anchor_outputs for fees.

HTLC fees increase (larger weight), and the fee paid by the opener
has to include the anchor outputs (i.e. 660 sats).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
bump-pyln-proto
Rusty Russell 4 years ago
parent
commit
85e3b43176
  1. 6
      channeld/commit_tx.c
  2. 9
      channeld/full_channel.c
  3. 3
      channeld/test/run-commit_tx.c
  4. 9
      common/htlc_trim.c
  5. 3
      common/htlc_trim.h
  6. 4
      common/htlc_tx.c
  7. 36
      common/htlc_tx.h
  8. 1
      common/initial_channel.c
  9. 4
      common/initial_commit_tx.c
  10. 14
      common/initial_commit_tx.h
  11. 4
      devtools/mkclose.c
  12. 26
      lightningd/chaintopology.c
  13. 9
      lightningd/closing_control.c
  14. 18
      lightningd/peer_control.c
  15. 3
      lightningd/test/run-invoice-select-inchan.c
  16. 41
      onchaind/onchaind.c
  17. 3
      wallet/test/run-wallet.c

6
channeld/commit_tx.c

@ -18,7 +18,8 @@ static bool trim(const struct htlc *htlc,
enum side side)
{
return htlc_is_trimmed(htlc_owner(htlc), htlc->amount,
feerate_per_kw, dust_limit, side);
feerate_per_kw, dust_limit, side,
false /* FIXME-anchor */);
}
size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
@ -118,7 +119,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* 2. Calculate the base [commitment transaction
* fee](#fee-calculation).
*/
base_fee = commit_tx_base_fee(feerate_per_kw, untrimmed);
base_fee = commit_tx_base_fee(feerate_per_kw, untrimmed,
false /* FIXME-anchor */);
SUPERVERBOSE("# base commitment transaction fee = %s\n",
type_to_string(tmpctx, struct amount_sat, &base_fee));

9
channeld/full_channel.c

@ -390,7 +390,8 @@ static struct amount_sat fee_for_htlcs(const struct channel *channel,
untrimmed = num_untrimmed_htlcs(side, dust_limit, feerate,
committed, adding, removing);
return commit_tx_base_fee(feerate, untrimmed);
return commit_tx_base_fee(feerate, untrimmed,
false /* FIXME-anchor */);
}
/*
@ -438,7 +439,8 @@ static bool local_opener_has_fee_headroom(const struct channel *channel,
/* Now, how much would it cost us if feerate increases 100% and we added
* another HTLC? */
fee = commit_tx_base_fee(2 * feerate, untrimmed + 1);
fee = commit_tx_base_fee(2 * feerate, untrimmed + 1,
false /* FIXME-anchor */);
if (amount_msat_greater_eq_sat(remainder, fee))
return true;
@ -1034,7 +1036,8 @@ bool can_opener_afford_feerate(const struct channel *channel, u32 feerate_per_kw
- commit_tx_num_untrimmed(removing, feerate_per_kw, dust_limit,
!channel->opener);
fee = commit_tx_base_fee(feerate_per_kw, untrimmed);
fee = commit_tx_base_fee(feerate_per_kw, untrimmed,
false /* FIXME-anchor */);
/* BOLT #2:
*

3
channeld/test/run-commit_tx.c

@ -967,7 +967,8 @@ int main(int argc, const char *argv[])
* its output cannot go negative! */
for (;;) {
struct amount_sat base_fee
= commit_tx_base_fee(feerate_per_kw, 0);
= commit_tx_base_fee(feerate_per_kw, 0,
option_anchor_outputs);
if (amount_msat_greater_eq_sat(to_local, base_fee)) {
feerate_per_kw++;

9
common/htlc_trim.c

@ -6,7 +6,8 @@ bool htlc_is_trimmed(enum side htlc_owner,
struct amount_msat htlc_amount,
u32 feerate_per_kw,
struct amount_sat dust_limit,
enum side side)
enum side side,
bool option_anchor_outputs)
{
struct amount_sat htlc_fee, htlc_min;
@ -21,7 +22,8 @@ bool htlc_is_trimmed(enum side htlc_owner,
* [Offered HTLC Outputs](#offered-htlc-outputs).
*/
if (htlc_owner == side)
htlc_fee = htlc_timeout_fee(feerate_per_kw);
htlc_fee = htlc_timeout_fee(feerate_per_kw,
option_anchor_outputs);
/* BOLT #3:
*
* - for every received HTLC:
@ -32,7 +34,8 @@ bool htlc_is_trimmed(enum side htlc_owner,
* - MUST be generated as specified in
*/
else
htlc_fee = htlc_success_fee(feerate_per_kw);
htlc_fee = htlc_success_fee(feerate_per_kw,
option_anchor_outputs);
/* If these overflow, it implies htlc must be less. */
if (!amount_sat_add(&htlc_min, dust_limit, htlc_fee))

3
common/htlc_trim.h

@ -9,6 +9,7 @@ bool htlc_is_trimmed(enum side htlc_owner,
struct amount_msat htlc_amount,
u32 feerate_per_kw,
struct amount_sat dust_limit,
enum side side);
enum side side,
bool option_anchor_outputs);
#endif /* LIGHTNING_COMMON_HTLC_TRIM_H */

4
common/htlc_tx.c

@ -93,7 +93,7 @@ struct bitcoin_tx *htlc_success_tx(const tal_t *ctx,
to_self_delay,
&keyset->self_revocation_key,
&keyset->self_delayed_payment_key,
htlc_success_fee(feerate_per_kw),
htlc_success_fee(feerate_per_kw, false /* FIXME-anchor */),
0,
option_anchor_outputs);
}
@ -145,7 +145,7 @@ struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
commit_wscript, htlc_msatoshi, to_self_delay,
&keyset->self_revocation_key,
&keyset->self_delayed_payment_key,
htlc_timeout_fee(feerate_per_kw),
htlc_timeout_fee(feerate_per_kw, false /* FIXME-anchor */),
cltv_expiry,
option_anchor_outputs);
}

36
common/htlc_tx.h

@ -39,28 +39,40 @@ static inline size_t elements_add_overhead(size_t weight, size_t incount,
return weight;
}
static inline struct amount_sat htlc_timeout_fee(u32 feerate_per_kw)
static inline struct amount_sat htlc_timeout_fee(u32 feerate_per_kw,
bool option_anchor_outputs)
{
/* BOLT #3:
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
*
* The fee for an HTLC-timeout transaction:
* - MUST BE calculated to match:
* 1. Multiply `feerate_per_kw` by 663 and divide by 1000 (rounding
* down).
* - MUST BE calculated to match:
* 1. Multiply `feerate_per_kw` by 663 (666 if `option_anchor_outputs`
* applies) and divide by 1000 (rounding down).
*/
return amount_tx_fee(elements_add_overhead(663, 1, 1), feerate_per_kw);
u32 base;
if (option_anchor_outputs)
base = 666;
else
base = 663;
return amount_tx_fee(elements_add_overhead(base, 1, 1), feerate_per_kw);
}
static inline struct amount_sat htlc_success_fee(u32 feerate_per_kw)
static inline struct amount_sat htlc_success_fee(u32 feerate_per_kw,
bool option_anchor_outputs)
{
/* BOLT #3:
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
*
* The fee for an HTLC-success transaction:
* - MUST BE calculated to match:
* 1. Multiply `feerate_per_kw` by 703 and divide by 1000 (rounding
* down).
* - MUST BE calculated to match:
* 1. Multiply `feerate_per_kw` by 703 (706 if `option_anchor_outputs`
* applies) and divide by 1000 (rounding down).
*/
return amount_tx_fee(elements_add_overhead(703, 1, 1), feerate_per_kw);
u32 base;
if (option_anchor_outputs)
base = 706;
else
base = 703;
return amount_tx_fee(elements_add_overhead(base, 1, 1), feerate_per_kw);
}
/* Create HTLC-success tx to spend a received HTLC commitment tx

1
common/initial_channel.c

@ -111,6 +111,7 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx,
0 ^ channel->commitment_number_obscurer,
direct_outputs,
side,
false /* FIXME-anchor */,
err_reason);
if (init_tx) {

4
common/initial_commit_tx.c

@ -74,6 +74,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
u64 obscured_commitment_number,
struct wally_tx_output *direct_outputs[NUM_SIDES],
enum side side,
bool option_anchor_outputs,
char** err_reason)
{
struct amount_sat base_fee;
@ -101,7 +102,8 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
* 2. Calculate the base [commitment transaction
* fee](#fee-calculation).
*/
base_fee = commit_tx_base_fee(feerate_per_kw, untrimmed);
base_fee = commit_tx_base_fee(feerate_per_kw, untrimmed,
option_anchor_outputs);
/* BOLT #3:
*

14
common/initial_commit_tx.h

@ -23,17 +23,21 @@ u64 commit_number_obscurer(const struct pubkey *opener_payment_basepoint,
/* Helper to calculate the base fee if we have this many htlc outputs */
static inline struct amount_sat commit_tx_base_fee(u32 feerate_per_kw,
size_t num_untrimmed_htlcs)
size_t num_untrimmed_htlcs,
bool option_anchor_outputs)
{
u64 weight;
/* BOLT #3:
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
*
* The base fee for a commitment transaction:
* - MUST be calculated to match:
* 1. Start with `weight` = 724.
* 1. Start with `weight` = 724 (1124 if `option_anchor_outputs` applies).
*/
weight = 724;
if (option_anchor_outputs)
weight = 1124;
else
weight = 724;
/* BOLT #3:
*
@ -87,6 +91,7 @@ static inline struct amount_sat commit_tx_base_fee(u32 feerate_per_kw,
* @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).
* @side: side to generate commitment transaction for.
* @option_anchor_outputs: does option_anchor_outputs apply to this channel?
* @err_reason: When NULL is returned, this will point to a human readable reason.
*
* We need to be able to generate the remote side's tx to create signatures,
@ -109,6 +114,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
u64 obscured_commitment_number,
struct wally_tx_output *direct_outputs[NUM_SIDES],
enum side side,
bool option_anchor_outputs,
char** err_reason);
/* try_subtract_fee - take away this fee from the opener (and return true), or all if insufficient (and return false). */

4
devtools/mkclose.c

@ -70,6 +70,7 @@ int main(int argc, char *argv[])
const u8 *funding_wscript;
const struct chainparams *chainparams = chainparams_for_network("bitcoin");
const struct amount_sat dust_limit = AMOUNT_SAT(546);
bool option_anchor_outputs = false;
setup_locale();
@ -117,7 +118,8 @@ int main(int argc, char *argv[])
errx(1, "Parsing remote-close-pubkey");
argnum++;
fee = commit_tx_base_fee(feerate_per_kw, 0);
fee = commit_tx_base_fee(feerate_per_kw, 0,
option_anchor_outputs);
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));

26
lightningd/chaintopology.c

@ -16,6 +16,8 @@
#include <ccan/tal/str/str.h>
#include <common/coin_mvt.h>
#include <common/configdir.h>
#include <common/features.h>
#include <common/htlc_tx.h>
#include <common/json_command.h>
#include <common/jsonrpc_errors.h>
#include <common/memleak.h>
@ -526,6 +528,11 @@ static struct command_result *json_feerates(struct command *cmd,
json_object_end(response);
if (!missing) {
/* It actually is negotiated per-channel... */
bool anchor_outputs
= feature_offered(cmd->ld->our_features->bits[INIT_FEATURE],
OPT_ANCHOR_OUTPUTS);
json_object_start(response, "onchain_fee_estimates");
/* eg 020000000001016f51de645a47baa49a636b8ec974c28bdff0ac9151c0f4eda2dbe3b41dbe711d000000001716001401fad90abcd66697e2592164722de4a95ebee165ffffffff0240420f00000000002200205b8cd3b914cf67cdd8fa6273c930353dd36476734fbd962102c2df53b90880cdb73f890000000000160014c2ccab171c2a5be9dab52ec41b825863024c54660248304502210088f65e054dbc2d8f679de3e40150069854863efa4a45103b2bb63d060322f94702200d3ae8923924a458cffb0b7360179790830027bb6b29715ba03e12fc22365de1012103d745445c9362665f22e0d96e9e766f273f3260dea39c8a76bfa05dd2684ddccf00000000 == weight 702 */
json_add_num(response, "opening_channel_satoshis",
@ -536,20 +543,15 @@ static struct command_result *json_feerates(struct command *cmd,
/* eg. 02000000000101c4fecaae1ea940c15ec502de732c4c386d51f981317605bbe5ad2c59165690ab00000000009db0e280010a2d0f00000000002200208d290003cedb0dd00cd5004c2d565d55fc70227bf5711186f4fa9392f8f32b4a0400483045022100952fcf8c730c91cf66bcb742cd52f046c0db3694dc461e7599be330a22466d790220740738a6f9d9e1ae5c86452fa07b0d8dddc90f8bee4ded24a88fe4b7400089eb01483045022100db3002a93390fc15c193da57d6ce1020e82705e760a3aa935ebe864bd66dd8e8022062ee9c6aa7b88ff4580e2671900a339754116371d8f40eba15b798136a76cd150147522102324266de8403b3ab157a09f1f784d587af61831c998c151bcc21bb74c2b2314b2102e3bd38009866c9da8ec4aa99cc4ea9c6c0dd46df15c61ef0ce1f271291714e5752ae9a3ed620 == weight 598 */
json_add_u64(response, "unilateral_close_satoshis",
unilateral_feerate(cmd->ld->topology) * 598 / 1000);
/* BOLT #3:
*
* The *expected weight* of an HTLC transaction is calculated as follows:
* ...
* results in weights of:
*
* 663 (HTLC-timeout)
* 703 (HTLC-success)
*
*/
/* This really depends on whether we *negotiated*
* option_anchor_outputs for a particular channel! */
json_add_u64(response, "htlc_timeout_satoshis",
htlc_resolution_feerate(cmd->ld->topology) * 663 / 1000);
htlc_timeout_fee(htlc_resolution_feerate(cmd->ld->topology),
anchor_outputs).satoshis /* Raw: estimate */);
json_add_u64(response, "htlc_success_satoshis",
htlc_resolution_feerate(cmd->ld->topology) * 703 / 1000);
htlc_success_fee(htlc_resolution_feerate(cmd->ld->topology),
anchor_outputs).satoshis /* Raw: estimate */);
json_object_end(response);
}

9
lightningd/closing_control.c

@ -218,10 +218,12 @@ void peer_start_closingd(struct channel *channel,
*/
final_commit_feerate = get_feerate(channel->channel_info.fee_states,
channel->opener, LOCAL);
feelimit = commit_tx_base_fee(final_commit_feerate, 0);
feelimit = commit_tx_base_fee(final_commit_feerate, 0,
false /* FIXME-anchor */);
/* Pick some value above slow feerate (or min possible if unknown) */
minfee = commit_tx_base_fee(feerate_min(ld, NULL), 0);
minfee = commit_tx_base_fee(feerate_min(ld, NULL), 0,
false /* FIXME-anchor */);
/* If we can't determine feerate, start at half unilateral feerate. */
feerate = mutual_close_feerate(ld->topology);
@ -230,7 +232,8 @@ void peer_start_closingd(struct channel *channel,
if (feerate < feerate_floor())
feerate = feerate_floor();
}
startfee = commit_tx_base_fee(feerate, 0);
startfee = commit_tx_base_fee(feerate, 0,
false /* FIXME-anchor */);
if (amount_sat_greater(startfee, feelimit))
startfee = feelimit;

18
lightningd/peer_control.c

@ -471,7 +471,8 @@ static void json_add_htlcs(struct lightningd *ld,
json_add_string(response, "state",
htlc_state_name(hin->hstate));
if (htlc_is_trimmed(REMOTE, hin->msat, local_feerate,
channel->our_config.dust_limit, LOCAL))
channel->our_config.dust_limit, LOCAL,
false /* FIXME-anchor */))
json_add_bool(response, "local_trimmed", true);
json_object_end(response);
}
@ -492,7 +493,8 @@ static void json_add_htlcs(struct lightningd *ld,
json_add_string(response, "state",
htlc_state_name(hout->hstate));
if (htlc_is_trimmed(LOCAL, hout->msat, local_feerate,
channel->our_config.dust_limit, LOCAL))
channel->our_config.dust_limit, LOCAL,
false /* FIXME-anchor */))
json_add_bool(response, "local_trimmed", true);
json_object_end(response);
}
@ -532,7 +534,8 @@ static struct amount_sat commit_txfee(const struct channel *channel,
dust_limit = channel->channel_info.their_config.dust_limit;
/* Assume we tried to add "amount" */
if (!htlc_is_trimmed(side, amount, feerate, dust_limit, side))
if (!htlc_is_trimmed(side, amount, feerate, dust_limit, side,
false /* FIXME-anchor */))
num_untrimmed_htlcs++;
for (hin = htlc_in_map_first(&ld->htlcs_in, &ini);
@ -541,7 +544,8 @@ static struct amount_sat commit_txfee(const struct channel *channel,
if (hin->key.channel != channel)
continue;
if (!htlc_is_trimmed(!side, hin->msat, feerate, dust_limit,
side))
side,
false /* FIXME-anchor */))
num_untrimmed_htlcs++;
}
for (hout = htlc_out_map_first(&ld->htlcs_out, &outi);
@ -550,7 +554,8 @@ static struct amount_sat commit_txfee(const struct channel *channel,
if (hout->key.channel != channel)
continue;
if (!htlc_is_trimmed(side, hout->msat, feerate, dust_limit,
side))
side,
false /* FIXME-anchor */))
num_untrimmed_htlcs++;
}
@ -567,7 +572,8 @@ 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);
return commit_tx_base_fee(2 * feerate, num_untrimmed_htlcs + 1,
false /* FIXME-anchor */);
}
static void subtract_offered_htlcs(const struct channel *channel,

3
lightningd/test/run-invoice-select-inchan.c

@ -147,7 +147,8 @@ bool htlc_is_trimmed(enum side htlc_owner UNNEEDED,
struct amount_msat htlc_amount UNNEEDED,
u32 feerate_per_kw UNNEEDED,
struct amount_sat dust_limit UNNEEDED,
enum side side UNNEEDED)
enum side side UNNEEDED,
bool option_anchor_outputs UNNEEDED)
{ fprintf(stderr, "htlc_is_trimmed called!\n"); abort(); }
/* Generated stub for htlc_set_fail */
void htlc_set_fail(struct htlc_set *set UNNEEDED, const u8 *failmsg TAKES UNNEEDED)

41
onchaind/onchaind.c

@ -451,19 +451,25 @@ static bool set_htlc_timeout_fee(struct bitcoin_tx *tx,
{
static struct amount_sat amount, fee = AMOUNT_SAT_INIT(UINT64_MAX);
struct amount_asset asset = bitcoin_tx_output_get_amount(tx, 0);
size_t weight = elements_add_overhead(663, tx->wtx->num_inputs,
tx->wtx->num_outputs);
assert(amount_asset_is_main(&asset));
amount = amount_asset_to_sat(&asset);
size_t weight;
/* BOLT #3:
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
*
* The fee for an HTLC-timeout transaction:
* - MUST BE calculated to match:
* 1. Multiply `feerate_per_kw` by 663 and divide by 1000 (rounding
* down).
* 1. Multiply `feerate_per_kw` by 663 (666 if `option_anchor_outputs`
* applies) and divide by 1000 (rounding down).
*/
if (option_anchor_outputs)
weight = 666;
else
weight = 663;
weight = elements_add_overhead(weight, tx->wtx->num_inputs,
tx->wtx->num_outputs);
assert(amount_asset_is_main(&asset));
amount = amount_asset_to_sat(&asset);
if (amount_sat_eq(fee, AMOUNT_SAT(UINT64_MAX))) {
struct amount_sat grindfee;
if (grind_htlc_tx_fee(&grindfee, tx, remotesig, wscript, weight)) {
@ -492,15 +498,22 @@ static void set_htlc_success_fee(struct bitcoin_tx *tx,
{
static struct amount_sat amt, fee = AMOUNT_SAT_INIT(UINT64_MAX);
struct amount_asset asset;
size_t weight = elements_add_overhead(703, tx->wtx->num_inputs,
tx->wtx->num_outputs);
/* BOLT #3:
size_t weight;
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
*
* The fee for an HTLC-success transaction:
* - MUST BE calculated to match:
* 1. Multiply `feerate_per_kw` by 703 and divide by 1000
* (rounding down).
* - MUST BE calculated to match:
* 1. Multiply `feerate_per_kw` by 703 (706 if `option_anchor_outputs`
* applies) and divide by 1000 (rounding down).
*/
if (option_anchor_outputs)
weight = 706;
else
weight = 703;
weight = elements_add_overhead(weight, tx->wtx->num_inputs,
tx->wtx->num_outputs);
if (amount_sat_eq(fee, AMOUNT_SAT(UINT64_MAX))) {
if (!grind_htlc_tx_fee(&fee, tx, remotesig, wscript, weight))
status_failed(STATUS_FAIL_INTERNAL_ERROR,

3
wallet/test/run-wallet.c

@ -152,7 +152,8 @@ bool htlc_is_trimmed(enum side htlc_owner UNNEEDED,
struct amount_msat htlc_amount UNNEEDED,
u32 feerate_per_kw UNNEEDED,
struct amount_sat dust_limit UNNEEDED,
enum side side UNNEEDED)
enum side side UNNEEDED,
bool option_anchor_outputs UNNEEDED)
{ fprintf(stderr, "htlc_is_trimmed called!\n"); abort(); }
/* Generated stub for htlc_set_add */
void htlc_set_add(struct lightningd *ld UNNEEDED,

Loading…
Cancel
Save