From 73691100bf4b5e2003bea6c42340754b96437abd Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 26 Jun 2023 08:46:21 +0930 Subject: [PATCH] onchaind: don't grind for htlc fees if option_anchors_zero_fee_htlc_tx set. The answer, it's right in the name of the option! Signed-off-by: Rusty Russell --- lightningd/onchain_control.c | 1 + onchaind/onchaind.c | 37 +++++++++++++++++++++++-------- onchaind/onchaind_wire.csv | 1 + onchaind/test/run-grind_feerate.c | 2 +- 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index 3d37b0c2e..6ab1d7751 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -1636,6 +1636,7 @@ enum watch_result onchaind_funding_spent(struct channel *channel, channel->static_remotekey_start[LOCAL], channel->static_remotekey_start[REMOTE], channel_has(channel, OPT_ANCHOR_OUTPUTS), + channel_has(channel, OPT_ANCHORS_ZERO_FEE_HTLC_TX), feerate_min(ld, NULL)); subd_send_msg(channel->owner, take(msg)); diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index 9669d6d6c..ef6e7b99e 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -79,6 +79,9 @@ static u64 static_remotekey_start[NUM_SIDES]; /* Does option_anchor_outputs apply to this commitment tx? */ static bool option_anchor_outputs; +/* Does option_anchors_zero_fee_htlc_tx apply to this commitment tx? */ +static bool option_anchors_zero_fee_htlc_tx; + /* The minimum relay feerate acceptable to the fullnode. */ static u32 min_relay_feerate; @@ -465,6 +468,9 @@ static bool set_htlc_timeout_fee(struct bitcoin_tx *tx, struct amount_asset asset = bitcoin_tx_output_get_amount(tx, 0); size_t weight; + amount = amount_asset_to_sat(&asset); + assert(amount_asset_is_main(&asset)); + /* BOLT #3: * * The fee for an HTLC-timeout transaction: @@ -474,14 +480,17 @@ static bool set_htlc_timeout_fee(struct bitcoin_tx *tx, * 1. Multiply `feerate_per_kw` by 663 (666 if `option_anchor_outputs` * applies) and divide by 1000 (rounding down). */ + if (option_anchors_zero_fee_htlc_tx) { + fee = AMOUNT_SAT(0); + goto set_amount; + } + if (option_anchor_outputs) weight = 666; else weight = 663; weight += elements_tx_overhead(chainparams, 1, 1); - 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; @@ -499,8 +508,10 @@ static bool set_htlc_timeout_fee(struct bitcoin_tx *tx, type_to_string(tmpctx, struct amount_sat, &fee), type_to_string(tmpctx, struct bitcoin_tx, tx)); +set_amount: bitcoin_tx_output_set_amount(tx, 0, amount); bitcoin_tx_finalize(tx); + return check_tx_sig(tx, 0, NULL, wscript, &keyset->other_htlc_key, remotesig); } @@ -516,6 +527,11 @@ static struct amount_sat get_htlc_success_fee(struct tracked_output *out) if (!amount_sat_eq(fee, AMOUNT_SAT(UINT64_MAX))) return fee; + if (option_anchors_zero_fee_htlc_tx) { + fee = AMOUNT_SAT(0); + return fee; + } + if (!amount_sat_to_msat(&htlc_amount, out->sat)) status_failed(STATUS_FAIL_INTERNAL_ERROR, "Overflow in get_htlc_success_fee %s", @@ -1806,7 +1822,7 @@ static u8 **derive_htlc_scripts(const struct htlc_stub *htlcs, enum side side) htlc_scripts[i] = htlc_offered_wscript(htlc_scripts, &htlcs[i].ripemd, keyset, - option_anchor_outputs); + option_anchor_outputs || option_anchors_zero_fee_htlc_tx); else { /* FIXME: remove abs_locktime */ struct abs_locktime ltime; @@ -1819,7 +1835,7 @@ static u8 **derive_htlc_scripts(const struct htlc_stub *htlcs, enum side side) &htlcs[i].ripemd, <ime, keyset, - option_anchor_outputs); + option_anchor_outputs || option_anchors_zero_fee_htlc_tx); } } return htlc_scripts; @@ -1867,7 +1883,7 @@ static size_t resolve_our_htlc_ourcommit(struct tracked_output *out, htlc_scripts[matches[i]], htlc_amount, htlcs[matches[i]].cltv_expiry, to_self_delay[LOCAL], 0, keyset, - option_anchor_outputs); + option_anchor_outputs || option_anchors_zero_fee_htlc_tx); if (set_htlc_timeout_fee(tx, out->remote_htlc_sig, htlc_scripts[matches[i]])) @@ -1894,7 +1910,7 @@ static size_t resolve_our_htlc_ourcommit(struct tracked_output *out, " feerate %u-%u," " last tx %s, input %s, signature %s," " cltvs %s wscripts %s" - " %s", + "%s%s", tal_count(matches), min_possible_feerate, max_possible_feerate, type_to_string(tmpctx, struct bitcoin_tx, tx), @@ -1904,7 +1920,9 @@ static size_t resolve_our_htlc_ourcommit(struct tracked_output *out, out->remote_htlc_sig), cltvs, wscripts, option_anchor_outputs - ? "option_anchor_outputs" : ""); + ? " option_anchor_outputs" : "", + option_anchors_zero_fee_htlc_tx + ? " option_anchors_zero_fee_htlc_tx" : ""); } /* FIXME: lightningd could derive this itself? */ @@ -2086,7 +2104,7 @@ static void note_missing_htlcs(u8 **htlc_scripts, static void get_anchor_scriptpubkeys(const tal_t *ctx, u8 **anchor) { - if (!option_anchor_outputs) { + if (!option_anchor_outputs && !option_anchors_zero_fee_htlc_tx) { anchor[LOCAL] = anchor[REMOTE] = NULL; return; } @@ -2114,7 +2132,7 @@ static u8 *scriptpubkey_to_remote(const tal_t *ctx, *... * Otherwise, this output is a simple P2WPKH to `remotepubkey`. */ - if (option_anchor_outputs) { + if (option_anchor_outputs || option_anchors_zero_fee_htlc_tx) { return scriptpubkey_p2wsh(ctx, bitcoin_wscript_to_remote_anchored(tmpctx, remotekey, @@ -3471,6 +3489,7 @@ int main(int argc, char *argv[]) &static_remotekey_start[LOCAL], &static_remotekey_start[REMOTE], &option_anchor_outputs, + &option_anchors_zero_fee_htlc_tx, &min_relay_feerate)) { master_badmsg(WIRE_ONCHAIND_INIT, msg); } diff --git a/onchaind/onchaind_wire.csv b/onchaind/onchaind_wire.csv index f8a513d78..a320c6381 100644 --- a/onchaind/onchaind_wire.csv +++ b/onchaind/onchaind_wire.csv @@ -48,6 +48,7 @@ msgdata,onchaind_init,remote_funding_pubkey,pubkey, msgdata,onchaind_init,local_static_remotekey_start,u64, msgdata,onchaind_init,remote_static_remotekey_start,u64, msgdata,onchaind_init,option_anchor_outputs,bool, +msgdata,onchaind_init,option_anchors_zero_fee_htlc_tx,bool, # We need this for BIP125 rule 4 msgdata,onchaind_init,min_relay_feerate,u32, diff --git a/onchaind/test/run-grind_feerate.c b/onchaind/test/run-grind_feerate.c index 3f225adbc..b604d9b6e 100644 --- a/onchaind/test/run-grind_feerate.c +++ b/onchaind/test/run-grind_feerate.c @@ -51,7 +51,7 @@ bool fromwire_onchaind_dev_memleak(const void *p UNNEEDED) bool fromwire_onchaind_htlcs(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct htlc_stub **htlc UNNEEDED, bool **tell_if_missing UNNEEDED, bool **tell_immediately UNNEEDED) { fprintf(stderr, "fromwire_onchaind_htlcs called!\n"); abort(); } /* Generated stub for fromwire_onchaind_init */ -bool fromwire_onchaind_init(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct shachain *shachain UNNEEDED, const struct chainparams **chainparams UNNEEDED, struct amount_sat *funding_amount_satoshi UNNEEDED, struct amount_msat *our_msat UNNEEDED, struct pubkey *old_remote_per_commitment_point UNNEEDED, struct pubkey *remote_per_commitment_point UNNEEDED, u32 *local_to_self_delay UNNEEDED, u32 *remote_to_self_delay UNNEEDED, struct amount_sat *local_dust_limit_satoshi UNNEEDED, struct bitcoin_txid *our_broadcast_txid UNNEEDED, u8 **local_scriptpubkey UNNEEDED, u8 **remote_scriptpubkey UNNEEDED, u32 *ourwallet_index UNNEEDED, struct ext_key *ourwallet_ext_key UNNEEDED, struct pubkey *ourwallet_pubkey UNNEEDED, enum side *opener UNNEEDED, struct basepoints *local_basepoints UNNEEDED, struct basepoints *remote_basepoints UNNEEDED, struct tx_parts **tx_parts UNNEEDED, u32 *locktime UNNEEDED, u32 *tx_blockheight UNNEEDED, u32 *reasonable_depth UNNEEDED, struct bitcoin_signature **htlc_signature UNNEEDED, u32 *min_possible_feerate UNNEEDED, u32 *max_possible_feerate UNNEEDED, struct pubkey **possible_remote_per_commit_point UNNEEDED, struct pubkey *local_funding_pubkey UNNEEDED, struct pubkey *remote_funding_pubkey UNNEEDED, u64 *local_static_remotekey_start UNNEEDED, u64 *remote_static_remotekey_start UNNEEDED, bool *option_anchor_outputs UNNEEDED, u32 *min_relay_feerate UNNEEDED) +bool fromwire_onchaind_init(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct shachain *shachain UNNEEDED, const struct chainparams **chainparams UNNEEDED, struct amount_sat *funding_amount_satoshi UNNEEDED, struct amount_msat *our_msat UNNEEDED, struct pubkey *old_remote_per_commitment_point UNNEEDED, struct pubkey *remote_per_commitment_point UNNEEDED, u32 *local_to_self_delay UNNEEDED, u32 *remote_to_self_delay UNNEEDED, struct amount_sat *local_dust_limit_satoshi UNNEEDED, struct bitcoin_txid *our_broadcast_txid UNNEEDED, u8 **local_scriptpubkey UNNEEDED, u8 **remote_scriptpubkey UNNEEDED, u32 *ourwallet_index UNNEEDED, struct ext_key *ourwallet_ext_key UNNEEDED, struct pubkey *ourwallet_pubkey UNNEEDED, enum side *opener UNNEEDED, struct basepoints *local_basepoints UNNEEDED, struct basepoints *remote_basepoints UNNEEDED, struct tx_parts **tx_parts UNNEEDED, u32 *locktime UNNEEDED, u32 *tx_blockheight UNNEEDED, u32 *reasonable_depth UNNEEDED, struct bitcoin_signature **htlc_signature UNNEEDED, u32 *min_possible_feerate UNNEEDED, u32 *max_possible_feerate UNNEEDED, struct pubkey **possible_remote_per_commit_point UNNEEDED, struct pubkey *local_funding_pubkey UNNEEDED, struct pubkey *remote_funding_pubkey UNNEEDED, u64 *local_static_remotekey_start UNNEEDED, u64 *remote_static_remotekey_start UNNEEDED, bool *option_anchor_outputs UNNEEDED, bool *option_anchors_zero_fee_htlc_tx UNNEEDED, u32 *min_relay_feerate UNNEEDED) { fprintf(stderr, "fromwire_onchaind_init called!\n"); abort(); } /* Generated stub for fromwire_onchaind_known_preimage */ bool fromwire_onchaind_known_preimage(const void *p UNNEEDED, struct preimage *preimage UNNEEDED)