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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-06-26 08:46:21 +09:30
parent a2039472c1
commit 73691100bf
4 changed files with 31 additions and 10 deletions

View file

@ -1636,6 +1636,7 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
channel->static_remotekey_start[LOCAL], channel->static_remotekey_start[LOCAL],
channel->static_remotekey_start[REMOTE], channel->static_remotekey_start[REMOTE],
channel_has(channel, OPT_ANCHOR_OUTPUTS), channel_has(channel, OPT_ANCHOR_OUTPUTS),
channel_has(channel, OPT_ANCHORS_ZERO_FEE_HTLC_TX),
feerate_min(ld, NULL)); feerate_min(ld, NULL));
subd_send_msg(channel->owner, take(msg)); subd_send_msg(channel->owner, take(msg));

View file

@ -79,6 +79,9 @@ static u64 static_remotekey_start[NUM_SIDES];
/* Does option_anchor_outputs apply to this commitment tx? */ /* Does option_anchor_outputs apply to this commitment tx? */
static bool option_anchor_outputs; 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. */ /* The minimum relay feerate acceptable to the fullnode. */
static u32 min_relay_feerate; 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); struct amount_asset asset = bitcoin_tx_output_get_amount(tx, 0);
size_t weight; size_t weight;
amount = amount_asset_to_sat(&asset);
assert(amount_asset_is_main(&asset));
/* BOLT #3: /* BOLT #3:
* *
* The fee for an HTLC-timeout transaction: * 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` * 1. Multiply `feerate_per_kw` by 663 (666 if `option_anchor_outputs`
* applies) and divide by 1000 (rounding down). * 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) if (option_anchor_outputs)
weight = 666; weight = 666;
else else
weight = 663; weight = 663;
weight += elements_tx_overhead(chainparams, 1, 1); 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))) { if (amount_sat_eq(fee, AMOUNT_SAT(UINT64_MAX))) {
struct amount_sat grindfee; 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 amount_sat, &fee),
type_to_string(tmpctx, struct bitcoin_tx, tx)); type_to_string(tmpctx, struct bitcoin_tx, tx));
set_amount:
bitcoin_tx_output_set_amount(tx, 0, amount); bitcoin_tx_output_set_amount(tx, 0, amount);
bitcoin_tx_finalize(tx); bitcoin_tx_finalize(tx);
return check_tx_sig(tx, 0, NULL, wscript, return check_tx_sig(tx, 0, NULL, wscript,
&keyset->other_htlc_key, remotesig); &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))) if (!amount_sat_eq(fee, AMOUNT_SAT(UINT64_MAX)))
return fee; return fee;
if (option_anchors_zero_fee_htlc_tx) {
fee = AMOUNT_SAT(0);
return fee;
}
if (!amount_sat_to_msat(&htlc_amount, out->sat)) if (!amount_sat_to_msat(&htlc_amount, out->sat))
status_failed(STATUS_FAIL_INTERNAL_ERROR, status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Overflow in get_htlc_success_fee %s", "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, htlc_scripts[i] = htlc_offered_wscript(htlc_scripts,
&htlcs[i].ripemd, &htlcs[i].ripemd,
keyset, keyset,
option_anchor_outputs); option_anchor_outputs || option_anchors_zero_fee_htlc_tx);
else { else {
/* FIXME: remove abs_locktime */ /* FIXME: remove abs_locktime */
struct abs_locktime ltime; struct abs_locktime ltime;
@ -1819,7 +1835,7 @@ static u8 **derive_htlc_scripts(const struct htlc_stub *htlcs, enum side side)
&htlcs[i].ripemd, &htlcs[i].ripemd,
&ltime, &ltime,
keyset, keyset,
option_anchor_outputs); option_anchor_outputs || option_anchors_zero_fee_htlc_tx);
} }
} }
return htlc_scripts; return htlc_scripts;
@ -1867,7 +1883,7 @@ static size_t resolve_our_htlc_ourcommit(struct tracked_output *out,
htlc_scripts[matches[i]], htlc_amount, htlc_scripts[matches[i]], htlc_amount,
htlcs[matches[i]].cltv_expiry, htlcs[matches[i]].cltv_expiry,
to_self_delay[LOCAL], 0, keyset, 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, if (set_htlc_timeout_fee(tx, out->remote_htlc_sig,
htlc_scripts[matches[i]])) htlc_scripts[matches[i]]))
@ -1894,7 +1910,7 @@ static size_t resolve_our_htlc_ourcommit(struct tracked_output *out,
" feerate %u-%u," " feerate %u-%u,"
" last tx %s, input %s, signature %s," " last tx %s, input %s, signature %s,"
" cltvs %s wscripts %s" " cltvs %s wscripts %s"
" %s", "%s%s",
tal_count(matches), tal_count(matches),
min_possible_feerate, max_possible_feerate, min_possible_feerate, max_possible_feerate,
type_to_string(tmpctx, struct bitcoin_tx, tx), 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), out->remote_htlc_sig),
cltvs, wscripts, cltvs, wscripts,
option_anchor_outputs 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? */ /* 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) 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; anchor[LOCAL] = anchor[REMOTE] = NULL;
return; return;
} }
@ -2114,7 +2132,7 @@ static u8 *scriptpubkey_to_remote(const tal_t *ctx,
*... *...
* Otherwise, this output is a simple P2WPKH to `remotepubkey`. * 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, return scriptpubkey_p2wsh(ctx,
bitcoin_wscript_to_remote_anchored(tmpctx, bitcoin_wscript_to_remote_anchored(tmpctx,
remotekey, remotekey,
@ -3471,6 +3489,7 @@ int main(int argc, char *argv[])
&static_remotekey_start[LOCAL], &static_remotekey_start[LOCAL],
&static_remotekey_start[REMOTE], &static_remotekey_start[REMOTE],
&option_anchor_outputs, &option_anchor_outputs,
&option_anchors_zero_fee_htlc_tx,
&min_relay_feerate)) { &min_relay_feerate)) {
master_badmsg(WIRE_ONCHAIND_INIT, msg); master_badmsg(WIRE_ONCHAIND_INIT, msg);
} }

View file

@ -48,6 +48,7 @@ msgdata,onchaind_init,remote_funding_pubkey,pubkey,
msgdata,onchaind_init,local_static_remotekey_start,u64, msgdata,onchaind_init,local_static_remotekey_start,u64,
msgdata,onchaind_init,remote_static_remotekey_start,u64, msgdata,onchaind_init,remote_static_remotekey_start,u64,
msgdata,onchaind_init,option_anchor_outputs,bool, msgdata,onchaind_init,option_anchor_outputs,bool,
msgdata,onchaind_init,option_anchors_zero_fee_htlc_tx,bool,
# We need this for BIP125 rule 4 # We need this for BIP125 rule 4
msgdata,onchaind_init,min_relay_feerate,u32, msgdata,onchaind_init,min_relay_feerate,u32,

1 #include <bitcoin/tx_parts.h>
48 msgdata,onchaind_init,remote_static_remotekey_start,u64,
49 msgdata,onchaind_init,option_anchor_outputs,bool,
50 # We need this for BIP125 rule 4 msgdata,onchaind_init,option_anchors_zero_fee_htlc_tx,bool,
51 # We need this for BIP125 rule 4
52 msgdata,onchaind_init,min_relay_feerate,u32,
53 # This says we're ready; give us htlcs and preimages.
54 msgtype,onchaind_init_reply,5101

View file

@ -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) 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(); } { fprintf(stderr, "fromwire_onchaind_htlcs called!\n"); abort(); }
/* Generated stub for fromwire_onchaind_init */ /* 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(); } { fprintf(stderr, "fromwire_onchaind_init called!\n"); abort(); }
/* Generated stub for fromwire_onchaind_known_preimage */ /* Generated stub for fromwire_onchaind_known_preimage */
bool fromwire_onchaind_known_preimage(const void *p UNNEEDED, struct preimage *preimage UNNEEDED) bool fromwire_onchaind_known_preimage(const void *p UNNEEDED, struct preimage *preimage UNNEEDED)