From 740afb822cc7a57d14d0075bf80a746b3f6da5b3 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 9 Sep 2021 14:55:23 +0930 Subject: [PATCH] common/initial_channel: use channel_type instead of individual option bools. Signed-off-by: Rusty Russell --- channeld/channeld.c | 42 ++++++++++++++++-------- channeld/full_channel.c | 55 +++++++++++++++++--------------- channeld/full_channel.h | 8 ++--- channeld/test/Makefile | 1 + channeld/test/run-commit_tx.c | 16 ---------- channeld/test/run-full_channel.c | 20 ++---------- channeld/watchtower.c | 3 +- common/initial_channel.c | 34 ++++++++------------ common/initial_channel.h | 18 ++++------- devtools/mkcommit.c | 11 +++++-- openingd/dualopend.c | 21 +++++++++--- openingd/openingd.c | 29 +++++++++-------- 12 files changed, 126 insertions(+), 132 deletions(-) diff --git a/channeld/channeld.c b/channeld/channeld.c index 66a322fcf..70feccca6 100644 --- a/channeld/channeld.c +++ b/channeld/channeld.c @@ -377,17 +377,18 @@ static bool match_type(const struct channel_type *desired, static void set_channel_type(struct channel *channel, const struct channel_type *type) { - const struct channel_type *cur = current_channel_type(tmpctx, channel); + const struct channel_type *cur = channel->type; if (channel_type_eq(cur, type)) return; /* We only allow one upgrade at the moment, so that's it. */ - assert(!channel->option_static_remotekey); + assert(!channel_has(channel, OPT_STATIC_REMOTEKEY)); assert(feature_offered(type->features, OPT_STATIC_REMOTEKEY)); /* Do upgrade, tell master. */ - channel->option_static_remotekey = true; + tal_free(channel->type); + channel->type = channel_type_dup(channel, type); status_unusual("Upgraded channel to [%s]", fmt_featurebits(tmpctx, type->features)); wire_sync_write(MASTER_FD, take(towire_channeld_upgraded(NULL, true))); @@ -1053,7 +1054,8 @@ static struct bitcoin_signature *calc_commitsigs(const tal_t *ctx, msg = towire_hsmd_sign_remote_commitment_tx(NULL, txs[0], &peer->channel->funding_pubkey[REMOTE], &peer->remote_per_commit, - peer->channel->option_static_remotekey); + channel_has(peer->channel, + OPT_STATIC_REMOTEKEY)); msg = hsm_req(tmpctx, take(msg)); if (!fromwire_hsmd_sign_tx_reply(msg, commit_sig)) @@ -1093,7 +1095,8 @@ static struct bitcoin_signature *calc_commitsigs(const tal_t *ctx, txs[i+1]->wtx->inputs[0].index); msg = towire_hsmd_sign_remote_htlc_tx(NULL, txs[i + 1], wscript, &peer->remote_per_commit, - peer->channel->option_anchor_outputs); + channel_has(peer->channel, + OPT_ANCHOR_OUTPUTS)); msg = hsm_req(tmpctx, take(msg)); if (!fromwire_hsmd_sign_tx_reply(msg, &htlc_sigs[i])) @@ -1621,7 +1624,7 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg) /* SIGHASH_ALL is implied. */ commit_sig.sighash_type = SIGHASH_ALL; htlc_sigs = unraw_sigs(tmpctx, raw_sigs, - peer->channel->option_anchor_outputs); + channel_has(peer->channel, OPT_ANCHOR_OUTPUTS)); txs = channel_txs(tmpctx, &htlc_map, NULL, @@ -2699,7 +2702,7 @@ static void peer_reconnect(struct peer *peer, /* Both these options give us extra fields to check. */ check_extra_fields - = dataloss_protect || peer->channel->option_static_remotekey; + = dataloss_protect || channel_has(peer->channel, OPT_STATIC_REMOTEKEY); /* Our current per-commitment point is the commitment point in the last * received signed commitment */ @@ -2733,8 +2736,8 @@ static void peer_reconnect(struct peer *peer, * to. * - MAY not set `upgradable` if it would be empty. */ - send_tlvs->current_type - = current_channel_type(send_tlvs, peer->channel); + send_tlvs->current_type = tal_dup(send_tlvs, struct channel_type, + peer->channel->type); send_tlvs->upgradable = channel_upgradable_types(send_tlvs, peer->channel); } @@ -2770,7 +2773,7 @@ static void peer_reconnect(struct peer *peer, * - MUST set `your_last_per_commitment_secret` to the last * `per_commitment_secret` it received */ - if (peer->channel->option_static_remotekey) { + if (channel_has(peer->channel, OPT_STATIC_REMOTEKEY)) { msg = towire_channel_reestablish (NULL, &peer->channel_id, peer->next_index[LOCAL], @@ -2933,7 +2936,9 @@ got_reestablish: check_future_dataloss_fields(peer, next_revocation_number, &last_local_per_commitment_secret, - peer->channel->option_static_remotekey ? NULL : + channel_has(peer->channel, + OPT_STATIC_REMOTEKEY) + ? NULL : &remote_current_per_commitment_point); } else retransmit_revoke_and_ack = false; @@ -2981,7 +2986,8 @@ got_reestablish: next_revocation_number, next_commitment_number, &last_local_per_commitment_secret, - peer->channel->option_static_remotekey + channel_has(peer->channel, + OPT_STATIC_REMOTEKEY) ? NULL : &remote_current_per_commitment_point); @@ -3641,6 +3647,7 @@ static void init_channel(struct peer *peer) bool option_static_remotekey, option_anchor_outputs; struct penalty_base *pbases; u8 *reestablish_only; + struct channel_type *channel_type; #if !DEVELOPER bool dev_fail_process_onionpacket; /* Ignored */ #endif @@ -3766,6 +3773,14 @@ static void init_channel(struct peer *peer) get_per_commitment_point(peer->next_index[LOCAL], &peer->next_local_per_commit, NULL); + /* FIXME: hand type directly from lightningd! */ + if (option_anchor_outputs) + channel_type = channel_type_anchor_outputs(NULL); + else if (option_static_remotekey) + channel_type = channel_type_static_remotekey(NULL); + else + channel_type = channel_type_none(NULL); + peer->channel = new_full_channel(peer, &peer->channel_id, &funding_txid, funding_txout, @@ -3779,8 +3794,7 @@ static void init_channel(struct peer *peer) &points[LOCAL], &points[REMOTE], &funding_pubkey[LOCAL], &funding_pubkey[REMOTE], - option_static_remotekey, - option_anchor_outputs, + take(channel_type), feature_offered(peer->their_features, OPT_LARGE_CHANNELS), opener); diff --git a/channeld/full_channel.c b/channeld/full_channel.c index 39fb3dacb..4dcd66688 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -100,15 +101,14 @@ struct channel *new_full_channel(const tal_t *ctx, u32 lease_expiry, struct amount_sat funding, struct amount_msat local_msat, - const struct fee_states *fee_states, + const struct fee_states *fee_states TAKES, const struct channel_config *local, const struct channel_config *remote, const struct basepoints *local_basepoints, const struct basepoints *remote_basepoints, const struct pubkey *local_funding_pubkey, const struct pubkey *remote_funding_pubkey, - bool option_static_remotekey, - bool option_anchor_outputs, + const struct channel_type *type TAKES, bool option_wumbo, enum side opener) { @@ -127,8 +127,7 @@ struct channel *new_full_channel(const tal_t *ctx, remote_basepoints, local_funding_pubkey, remote_funding_pubkey, - option_static_remotekey, - option_anchor_outputs, + type, option_wumbo, opener); @@ -262,6 +261,7 @@ static void add_htlcs(struct bitcoin_tx ***txs, size_t i; struct bitcoin_txid txid; u32 feerate_per_kw = channel_feerate(channel, side); + bool option_anchor_outputs = channel_has(channel, OPT_ANCHOR_OUTPUTS); /* Get txid of commitment transaction */ bitcoin_txid((*txs)[0], &txid); @@ -278,7 +278,7 @@ static void add_htlcs(struct bitcoin_tx ***txs, if (htlc_owner(htlc) == side) { ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8)); wscript = htlc_offered_wscript(tmpctx, &ripemd, keyset, - channel->option_anchor_outputs); + option_anchor_outputs); tx = htlc_timeout_tx(*txs, chainparams, &txid, i, wscript, htlc->amount, @@ -286,19 +286,19 @@ static void add_htlcs(struct bitcoin_tx ***txs, channel->config[!side].to_self_delay, feerate_per_kw, keyset, - channel->option_anchor_outputs); + option_anchor_outputs); } else { ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8)); wscript = htlc_received_wscript(tmpctx, &ripemd, &htlc->expiry, keyset, - channel->option_anchor_outputs); + option_anchor_outputs); tx = htlc_success_tx(*txs, chainparams, &txid, i, wscript, htlc->amount, channel->config[!side].to_self_delay, feerate_per_kw, keyset, - channel->option_anchor_outputs); + option_anchor_outputs); } /* Append to array. */ @@ -323,7 +323,7 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx, if (!derive_keyset(per_commitment_point, &channel->basepoints[side], &channel->basepoints[!side], - channel->option_static_remotekey, + channel_has(channel, OPT_STATIC_REMOTEKEY), &keyset)) return NULL; @@ -350,7 +350,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, - channel->option_anchor_outputs, + channel_has(channel, OPT_ANCHOR_OUTPUTS), side); /* Set the remote/local pubkeys on the commitment tx psbt */ @@ -431,13 +431,13 @@ static struct amount_sat fee_for_htlcs(const struct channel *channel, u32 feerate = channel_feerate(channel, side); struct amount_sat dust_limit = channel->config[side].dust_limit; size_t untrimmed; + bool option_anchor_outputs = channel_has(channel, OPT_ANCHOR_OUTPUTS); untrimmed = num_untrimmed_htlcs(side, dust_limit, feerate, - channel->option_anchor_outputs, + option_anchor_outputs, committed, adding, removing); - return commit_tx_base_fee(feerate, untrimmed, - channel->option_anchor_outputs); + return commit_tx_base_fee(feerate, untrimmed, option_anchor_outputs); } /* @@ -474,6 +474,7 @@ static bool local_opener_has_fee_headroom(const struct channel *channel, u32 feerate = channel_feerate(channel, LOCAL); size_t untrimmed; struct amount_sat fee; + bool option_anchor_outputs = channel_has(channel, OPT_ANCHOR_OUTPUTS); assert(channel->opener == LOCAL); @@ -481,13 +482,13 @@ 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, - channel->option_anchor_outputs, + option_anchor_outputs, committed, adding, removing); /* 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, - channel->option_anchor_outputs); + option_anchor_outputs); if (amount_msat_greater_eq_sat(remainder, fee)) return true; @@ -517,6 +518,7 @@ static enum channel_add_err add_htlc(struct channel *channel, const struct htlc **committed, **adding, **removing; const struct channel_view *view; size_t htlc_count; + bool option_anchor_outputs = channel_has(channel, OPT_ANCHOR_OUTPUTS); htlc = tal(tmpctx, struct htlc); @@ -685,7 +687,7 @@ static enum channel_add_err add_htlc(struct channel *channel, * of 330 sats from the funder (either `to_local` or * `to_remote`). */ - if (channel->option_anchor_outputs + if (option_anchor_outputs && channel->opener == sender && !amount_msat_sub_sat(&remainder, remainder, AMOUNT_SAT(660))) return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED; @@ -719,7 +721,7 @@ static enum channel_add_err add_htlc(struct channel *channel, &remainder)) return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED; - if (channel->option_anchor_outputs + if (option_anchor_outputs && channel->opener != sender && !amount_msat_sub_sat(&remainder, remainder, AMOUNT_SAT(660))) return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED; @@ -1089,6 +1091,7 @@ u32 approx_max_feerate(const struct channel *channel) u64 weight; struct amount_sat avail; const struct htlc **committed, **adding, **removing; + bool option_anchor_outputs = channel_has(channel, OPT_ANCHOR_OUTPUTS); gather_htlcs(tmpctx, channel, !channel->opener, &committed, &removing, &adding); @@ -1096,7 +1099,7 @@ u32 approx_max_feerate(const struct channel *channel) /* Assume none are trimmed; this gives lower bound on feerate. */ num = tal_count(committed) + tal_count(adding) - tal_count(removing); - weight = commit_tx_base_weight(num, channel->option_anchor_outputs); + weight = commit_tx_base_weight(num, option_anchor_outputs); /* Available is their view */ avail = amount_msat_to_sat_round_down(channel->view[!channel->opener].owed[channel->opener]); @@ -1107,7 +1110,7 @@ u32 approx_max_feerate(const struct channel *channel) * of 330 sats from the funder (either `to_local` or * `to_remote`). */ - if (channel->option_anchor_outputs + if (option_anchor_outputs && !amount_sat_sub(&avail, avail, AMOUNT_SAT(660))) { avail = AMOUNT_SAT(0); } else { @@ -1126,21 +1129,23 @@ bool can_opener_afford_feerate(const struct channel *channel, u32 feerate_per_kw struct amount_sat dust_limit = channel->config[!channel->opener].dust_limit; size_t untrimmed; const struct htlc **committed, **adding, **removing; + bool option_anchor_outputs = channel_has(channel, OPT_ANCHOR_OUTPUTS); + gather_htlcs(tmpctx, channel, !channel->opener, &committed, &removing, &adding); untrimmed = commit_tx_num_untrimmed(committed, feerate_per_kw, dust_limit, - channel->option_anchor_outputs, + option_anchor_outputs, !channel->opener) + commit_tx_num_untrimmed(adding, feerate_per_kw, dust_limit, - channel->option_anchor_outputs, + option_anchor_outputs, !channel->opener) - commit_tx_num_untrimmed(removing, feerate_per_kw, dust_limit, - channel->option_anchor_outputs, + option_anchor_outputs, !channel->opener); fee = commit_tx_base_fee(feerate_per_kw, untrimmed, - channel->option_anchor_outputs); + option_anchor_outputs); /* BOLT #3: * If `option_anchors` applies to the commitment @@ -1148,7 +1153,7 @@ bool can_opener_afford_feerate(const struct channel *channel, u32 feerate_per_kw * of 330 sats from the funder (either `to_local` or * `to_remote`). */ - if (channel->option_anchor_outputs + if (option_anchor_outputs && !amount_sat_add(&fee, fee, AMOUNT_SAT(660))) status_failed(STATUS_FAIL_INTERNAL_ERROR, "Cannot add 660 sats to %s for anchor", diff --git a/channeld/full_channel.h b/channeld/full_channel.h index 689a873c3..8351de8de 100644 --- a/channeld/full_channel.h +++ b/channeld/full_channel.h @@ -28,8 +28,7 @@ struct existing_htlc; * @remote_basepoints: remote basepoints. * @local_fundingkey: local funding key * @remote_fundingkey: remote funding key - * @option_static_remotekey: use `option_static_remotekey`. - * @option_anchor_outputs: use `option_anchor_outputs`. + * @type: type for this channel * @option_wumbo: large channel negotiated. * @opener: which side initiated it. * @@ -44,15 +43,14 @@ struct channel *new_full_channel(const tal_t *ctx, u32 lease_expiry, struct amount_sat funding, struct amount_msat local_msat, - const struct fee_states *fee_states, + const struct fee_states *fee_states TAKES, const struct channel_config *local, const struct channel_config *remote, const struct basepoints *local_basepoints, const struct basepoints *remote_basepoints, const struct pubkey *local_funding_pubkey, const struct pubkey *remote_funding_pubkey, - bool option_static_remotekey, - bool option_anchor_outputs, + const struct channel_type *type TAKES, bool option_wumbo, enum side opener); diff --git a/channeld/test/Makefile b/channeld/test/Makefile index 7342fad61..73faebf12 100644 --- a/channeld/test/Makefile +++ b/channeld/test/Makefile @@ -11,6 +11,7 @@ CHANNELD_TEST_COMMON_OBJS := \ common/amount.o \ common/channel_type.o \ common/daemon_conn.o \ + common/features.o \ common/htlc_state.o \ common/htlc_trim.o \ common/htlc_tx.o \ diff --git a/channeld/test/run-commit_tx.c b/channeld/test/run-commit_tx.c index 3324298d9..03bce950f 100644 --- a/channeld/test/run-commit_tx.c +++ b/channeld/test/run-commit_tx.c @@ -23,19 +23,6 @@ static bool print_superverbose; /*#define DEBUG */ /* AUTOGENERATED MOCKS START */ -/* Generated stub for feature_is_set */ -bool feature_is_set(const u8 *features UNNEEDED, size_t bit UNNEEDED) -{ fprintf(stderr, "feature_is_set called!\n"); abort(); } -/* Generated stub for feature_negotiated */ -bool feature_negotiated(const struct feature_set *our_features UNNEEDED, - const u8 *their_features UNNEEDED, size_t f UNNEEDED) -{ fprintf(stderr, "feature_negotiated called!\n"); abort(); } -/* Generated stub for feature_offered */ -bool feature_offered(const u8 *features UNNEEDED, size_t f UNNEEDED) -{ fprintf(stderr, "feature_offered called!\n"); abort(); } -/* Generated stub for featurebits_eq */ -bool featurebits_eq(const u8 *f1 UNNEEDED, const u8 *f2 UNNEEDED) -{ fprintf(stderr, "featurebits_eq called!\n"); abort(); } /* Generated stub for fromwire_bigsize */ bigsize_t fromwire_bigsize(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) { fprintf(stderr, "fromwire_bigsize called!\n"); abort(); } @@ -46,9 +33,6 @@ void fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, /* Generated stub for fromwire_node_id */ void fromwire_node_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct node_id *id UNNEEDED) { fprintf(stderr, "fromwire_node_id called!\n"); abort(); } -/* Generated stub for set_feature_bit */ -void set_feature_bit(u8 **ptr UNNEEDED, u32 bit UNNEEDED) -{ fprintf(stderr, "set_feature_bit called!\n"); abort(); } /* Generated stub for status_fmt */ void status_fmt(enum log_level level UNNEEDED, const struct node_id *peer UNNEEDED, diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index 24293d3ce..037bda5e0 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -18,19 +18,6 @@ #include /* AUTOGENERATED MOCKS START */ -/* Generated stub for feature_is_set */ -bool feature_is_set(const u8 *features UNNEEDED, size_t bit UNNEEDED) -{ fprintf(stderr, "feature_is_set called!\n"); abort(); } -/* Generated stub for feature_negotiated */ -bool feature_negotiated(const struct feature_set *our_features UNNEEDED, - const u8 *their_features UNNEEDED, size_t f UNNEEDED) -{ fprintf(stderr, "feature_negotiated called!\n"); abort(); } -/* Generated stub for feature_offered */ -bool feature_offered(const u8 *features UNNEEDED, size_t f UNNEEDED) -{ fprintf(stderr, "feature_offered called!\n"); abort(); } -/* Generated stub for featurebits_eq */ -bool featurebits_eq(const u8 *f1 UNNEEDED, const u8 *f2 UNNEEDED) -{ fprintf(stderr, "featurebits_eq called!\n"); abort(); } /* Generated stub for fromwire_bigsize */ bigsize_t fromwire_bigsize(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) { fprintf(stderr, "fromwire_bigsize called!\n"); abort(); } @@ -43,9 +30,6 @@ void memleak_add_helper_(const tal_t *p UNNEEDED, void (*cb)(struct htable *memt /* Generated stub for memleak_remove_htable */ void memleak_remove_htable(struct htable *memtable UNNEEDED, const struct htable *ht UNNEEDED) { fprintf(stderr, "memleak_remove_htable called!\n"); abort(); } -/* Generated stub for set_feature_bit */ -void set_feature_bit(u8 **ptr UNNEEDED, u32 bit UNNEEDED) -{ fprintf(stderr, "set_feature_bit called!\n"); abort(); } /* Generated stub for status_failed */ void status_failed(enum status_failreason code UNNEEDED, const char *fmt UNNEEDED, ...) @@ -506,7 +490,7 @@ int main(int argc, const char *argv[]) &localbase, &remotebase, &local_funding_pubkey, &remote_funding_pubkey, - false, false, false, LOCAL); + take(channel_type_none(NULL)), false, LOCAL); rchannel = new_full_channel(tmpctx, &cid, &funding_txid, funding_output_index, 0, take(new_height_states(NULL, REMOTE, &blockheight)), @@ -519,7 +503,7 @@ int main(int argc, const char *argv[]) &remotebase, &localbase, &remote_funding_pubkey, &local_funding_pubkey, - false, false, false, REMOTE); + take(channel_type_none(NULL)), false, REMOTE); /* BOLT #3: * diff --git a/channeld/watchtower.c b/channeld/watchtower.c index 994f643d0..efb13468e 100644 --- a/channeld/watchtower.c +++ b/channeld/watchtower.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -32,7 +33,7 @@ penalty_tx_create(const tal_t *ctx, struct amount_sat fee, min_out, amt; struct bitcoin_signature sig; u32 locktime = 0; - bool option_static_remotekey = channel->option_static_remotekey; + bool option_static_remotekey = channel_has(channel, OPT_STATIC_REMOTEKEY); u8 **witness; u32 remote_to_self_delay = channel->config[REMOTE].to_self_delay; const struct amount_sat dust_limit = channel->config[LOCAL].dust_limit; diff --git a/common/initial_channel.c b/common/initial_channel.c index 56599c5ec..cb6ec9100 100644 --- a/common/initial_channel.c +++ b/common/initial_channel.c @@ -33,8 +33,7 @@ struct channel *new_initial_channel(const tal_t *ctx, const struct basepoints *remote_basepoints, const struct pubkey *local_funding_pubkey, const struct pubkey *remote_funding_pubkey, - bool option_static_remotekey, - bool option_anchor_outputs, + const struct channel_type *type TAKES, bool option_wumbo, enum side opener) { @@ -82,11 +81,10 @@ struct channel *new_initial_channel(const tal_t *ctx, = commit_number_obscurer(&channel->basepoints[opener].payment, &channel->basepoints[!opener].payment); - channel->option_static_remotekey = option_static_remotekey; - channel->option_anchor_outputs = option_anchor_outputs; channel->option_wumbo = option_wumbo; - if (option_anchor_outputs) - assert(option_static_remotekey); + /* takes() if necessary */ + channel->type = tal_dup(channel, struct channel_type, type); + return channel; } @@ -109,7 +107,7 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx, if (!derive_keyset(per_commitment_point, &channel->basepoints[side], &channel->basepoints[!side], - channel->option_static_remotekey, + channel_has(channel, OPT_STATIC_REMOTEKEY), &keyset)) { *err_reason = "Cannot derive keyset"; return NULL; @@ -145,7 +143,7 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx, 0 ^ channel->commitment_number_obscurer, direct_outputs, side, csv_lock, - channel->option_anchor_outputs, + channel_has(channel, OPT_ANCHOR_OUTPUTS), err_reason); if (init_tx) { @@ -169,23 +167,12 @@ u32 channel_blockheight(const struct channel *channel, enum side side) channel->opener, side); } -struct channel_type *current_channel_type(const tal_t *ctx, - const struct channel *channel) -{ - if (channel->option_anchor_outputs) - return channel_type_anchor_outputs(ctx); - if (channel->option_static_remotekey) - return channel_type_static_remotekey(ctx); - - return channel_type_none(ctx); -} - struct channel_type **channel_upgradable_types(const tal_t *ctx, const struct channel *channel) { struct channel_type **arr = tal_arr(ctx, struct channel_type *, 0); - if (!channel->option_static_remotekey) + if (!channel_has(channel, OPT_STATIC_REMOTEKEY)) tal_arr_expand(&arr, channel_type_static_remotekey(arr)); return arr; @@ -195,13 +182,18 @@ struct channel_type *channel_desired_type(const tal_t *ctx, const struct channel *channel) { /* We don't actually want to downgrade anchors! */ - if (channel->option_anchor_outputs) + if (channel_has(channel, OPT_ANCHOR_OUTPUTS)) return channel_type_anchor_outputs(ctx); /* For now, we just want option_static_remotekey */ return channel_type_static_remotekey(ctx); } +bool channel_has(const struct channel *channel, int feature) +{ + return channel_type_has(channel->type, feature); +} + static char *fmt_channel_view(const tal_t *ctx, const struct channel_view *view) { return tal_fmt(ctx, "{ owed_local=%s," diff --git a/common/initial_channel.h b/common/initial_channel.h index fcafbb2b5..a084cf3b6 100644 --- a/common/initial_channel.h +++ b/common/initial_channel.h @@ -69,11 +69,8 @@ struct channel { /* What it looks like to each side. */ struct channel_view view[NUM_SIDES]; - /* Is this using option_static_remotekey? */ - bool option_static_remotekey; - - /* Is this using option_anchor_outputs? */ - bool option_anchor_outputs; + /* Features which apply to this channel. */ + struct channel_type *type; /* Are we using big channels? */ bool option_wumbo; @@ -100,8 +97,7 @@ struct channel { * @remote_basepoints: remote basepoints. * @local_fundingkey: local funding key * @remote_fundingkey: remote funding key - * @option_static_remotekey: was this created with option_static_remotekey? - * @option_anchor_outputs: was this created with option_anchor_outputs? + * @type: type for this channel * @option_wumbo: has peer currently negotiated wumbo? * @opener: which side initiated it. * @@ -123,8 +119,7 @@ struct channel *new_initial_channel(const tal_t *ctx, const struct basepoints *remote_basepoints, const struct pubkey *local_funding_pubkey, const struct pubkey *remote_funding_pubkey, - bool option_static_remotekey, - bool option_anchor_outputs, + const struct channel_type *type TAKES, bool option_wumbo, enum side opener); @@ -167,9 +162,6 @@ u32 channel_blockheight(const struct channel *channel, enum side side); * Channel features are explicitly enumerated as `channel_type` bitfields, * using odd features bits. */ -struct channel_type *current_channel_type(const tal_t *ctx, - const struct channel *channel); - /* What features can we upgrade? (Returns NULL if none). */ struct channel_type **channel_upgradable_types(const tal_t *ctx, const struct channel *channel); @@ -178,4 +170,6 @@ struct channel_type **channel_upgradable_types(const tal_t *ctx, struct channel_type *channel_desired_type(const tal_t *ctx, const struct channel *channel); +/* Convenience for querying channel->type */ +bool channel_has(const struct channel *channel, int feature); #endif /* LIGHTNING_COMMON_INITIAL_CHANNEL_H */ diff --git a/devtools/mkcommit.c b/devtools/mkcommit.c index eaba96080..221bebffd 100644 --- a/devtools/mkcommit.c +++ b/devtools/mkcommit.c @@ -272,6 +272,7 @@ int main(int argc, char *argv[]) struct privkey local_htlc_privkey, remote_htlc_privkey; struct pubkey local_htlc_pubkey, remote_htlc_pubkey; bool option_static_remotekey = false, option_anchor_outputs = false; + const struct channel_type *channel_type; struct sha256_double hash; u32 blockheight = 0; @@ -394,6 +395,13 @@ int main(int argc, char *argv[]) /* FIXME: option for v2? */ derive_channel_id(&cid, &funding_txid, funding_outnum); + if (option_anchor_outputs) + channel_type = channel_type_anchor_outputs(NULL); + else if (option_static_remotekey) + channel_type = channel_type_static_remotekey(NULL); + else + channel_type = channel_type_none(NULL); + channel = new_full_channel(NULL, &cid, &funding_txid, funding_outnum, 1, @@ -407,8 +415,7 @@ int main(int argc, char *argv[]) &localconfig, &remoteconfig, &localbase, &remotebase, &funding_localkey, &funding_remotekey, - option_static_remotekey, - option_anchor_outputs, + channel_type, false, fee_payer); diff --git a/openingd/dualopend.c b/openingd/dualopend.c index 14f0f38ce..f939cd44e 100644 --- a/openingd/dualopend.c +++ b/openingd/dualopend.c @@ -1692,6 +1692,7 @@ static void revert_channel_state(struct state *state) struct amount_sat total; struct amount_msat our_msats; enum side opener = state->our_role == TX_INITIATOR ? LOCAL : REMOTE; + const struct channel_type *type; /* We've already checked this */ if (!amount_sat_add(&total, tx_state->opener_funding, @@ -1706,6 +1707,8 @@ static void revert_channel_state(struct state *state) abort(); tal_free(state->channel); + type = default_channel_type(NULL, + state->our_features, state->their_features); state->channel = new_initial_channel(state, &state->channel_id, &tx_state->funding_txid, @@ -1725,7 +1728,7 @@ static void revert_channel_state(struct state *state) &state->their_points, &state->our_funding_pubkey, &state->their_funding_pubkey, - true, true, + take(type), feature_offered(state->their_features, OPT_LARGE_CHANNELS), opener); @@ -1749,6 +1752,7 @@ static u8 *accepter_commits(struct state *state, const u8 *wscript; u8 *msg; char *error; + const struct channel_type *type; /* Find the funding transaction txid */ psbt_txid(NULL, tx_state->psbt, &tx_state->funding_txid, NULL); @@ -1809,6 +1813,8 @@ static u8 *accepter_commits(struct state *state, if (state->channel) state->channel = tal_free(state->channel); + type = default_channel_type(NULL, + state->our_features, state->their_features); state->channel = new_initial_channel(state, &state->channel_id, &tx_state->funding_txid, @@ -1829,7 +1835,7 @@ static u8 *accepter_commits(struct state *state, &state->their_points, &state->our_funding_pubkey, &state->their_funding_pubkey, - true, true, + take(type), feature_offered(state->their_features, OPT_LARGE_CHANNELS), REMOTE); @@ -2379,6 +2385,7 @@ static u8 *opener_commits(struct state *state, const u8 *wscript; u8 *msg; char *error; + const struct channel_type *type; wscript = bitcoin_redeem_2of2(tmpctx, &state->our_funding_pubkey, &state->their_funding_pubkey); @@ -2421,6 +2428,8 @@ static u8 *opener_commits(struct state *state, } /* Ok, we're mostly good now? Let's do this */ + type = default_channel_type(NULL, + state->our_features, state->their_features); state->channel = new_initial_channel(state, &cid, &tx_state->funding_txid, @@ -2439,7 +2448,7 @@ static u8 *opener_commits(struct state *state, &state->their_points, &state->our_funding_pubkey, &state->their_funding_pubkey, - true, true, + take(type), feature_offered(state->their_features, OPT_LARGE_CHANNELS), /* Opener is local */ @@ -3765,6 +3774,7 @@ int main(int argc, char *argv[]) u8 *msg; struct amount_sat total_funding; struct amount_msat our_msat; + const struct channel_type *type; subdaemon_setup(argc, argv); @@ -3847,6 +3857,9 @@ int main(int argc, char *argv[]) /*~ We only reconnect on channels that the * saved the the database (exchanged commitment sigs) */ + type = default_channel_type(NULL, + state->our_features, + state->their_features); state->channel = new_initial_channel(state, &state->channel_id, &state->tx_state->funding_txid, @@ -3864,7 +3877,7 @@ int main(int argc, char *argv[]) &state->their_points, &state->our_funding_pubkey, &state->their_funding_pubkey, - true, true, + take(type), feature_offered(state->their_features, OPT_LARGE_CHANNELS), opener); diff --git a/openingd/openingd.c b/openingd/openingd.c index 9c686ed94..437113187 100644 --- a/openingd/openingd.c +++ b/openingd/openingd.c @@ -502,6 +502,7 @@ static bool funder_finalize_channel_setup(struct state *state, struct channel_id cid; char *err_reason; struct wally_tx_output *direct_outputs[NUM_SIDES]; + struct channel_type *type; /*~ Now we can initialize the `struct channel`. This represents * the current channel state and is how we can generate the current @@ -514,6 +515,10 @@ static bool funder_finalize_channel_setup(struct state *state, derive_channel_id(&cid, &state->funding_txid, state->funding_txout); + type = default_channel_type(NULL, + state->our_features, + state->their_features); + state->channel = new_initial_channel(state, &cid, &state->funding_txid, @@ -530,12 +535,7 @@ static bool funder_finalize_channel_setup(struct state *state, &state->their_points, &state->our_funding_pubkey, &state->their_funding_pubkey, - feature_negotiated(state->our_features, - state->their_features, - OPT_STATIC_REMOTEKEY), - feature_negotiated(state->our_features, - state->their_features, - OPT_ANCHOR_OUTPUTS), + take(type), feature_offered(state->their_features, OPT_LARGE_CHANNELS), /* Opener is local */ @@ -582,7 +582,8 @@ static bool funder_finalize_channel_setup(struct state *state, *tx, &state->channel->funding_pubkey[REMOTE], &state->first_per_commitment_point[REMOTE], - state->channel->option_static_remotekey); + channel_has(state->channel, + OPT_STATIC_REMOTEKEY)); wire_sync_write(HSM_FD, take(msg)); msg = wire_sync_read(tmpctx, HSM_FD); @@ -764,6 +765,7 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg) char* err_reason; struct wally_tx_output *direct_outputs[NUM_SIDES]; struct penalty_base *pbase; + struct channel_type *type; struct tlv_accept_channel_tlvs *accept_tlvs; struct tlv_open_channel_tlvs *open_tlvs = tlv_open_channel_tlvs_new(tmpctx); @@ -1012,6 +1014,9 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg) &state->channel_id), type_to_string(msg, struct channel_id, &id_in)); + type = default_channel_type(NULL, + state->our_features, state->their_features); + /* Now we can create the channel structure. */ state->channel = new_initial_channel(state, &state->channel_id, @@ -1028,12 +1033,7 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg) &state->our_points, &theirs, &state->our_funding_pubkey, &their_funding_pubkey, - feature_negotiated(state->our_features, - state->their_features, - OPT_STATIC_REMOTEKEY), - feature_negotiated(state->our_features, - state->their_features, - OPT_ANCHOR_OUTPUTS), + take(type), feature_offered(state->their_features, OPT_LARGE_CHANNELS), REMOTE); @@ -1123,7 +1123,8 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg) remote_commit, &state->channel->funding_pubkey[REMOTE], &state->first_per_commitment_point[REMOTE], - state->channel->option_static_remotekey); + channel_has(state->channel, + OPT_STATIC_REMOTEKEY)); wire_sync_write(HSM_FD, take(msg)); msg = wire_sync_read(tmpctx, HSM_FD);