Skip to content

Commit 6f10d58

Browse files
committed
lightningd: always tell openingd/dualopend what channel type we want.
Prior to it being compulsory, these daemons would need a default value. Now it's always required, it's clearer if it's always told. There's no "default_channel_type" now everyone has to specify channel_type either, so rename it to "desired_channel_type" and put it in lightningd specifically. Note that the channel_type can have options added: either option_scid_alias or option_zeroconf. This results in a slight behavior change: we will get type zeroconf even if we didn't ask for it, if they gave it to us. Signed-off-by: Rusty Russell <[email protected]> Changelog-Changed: JSON-RPC: fundchannel / fundchannel_start returned `channel_type` will include option_zeroconf if it was implied by a 0 minimum_depth, even if we didn't explicitly ask for a zero conf channel.
1 parent 8b95d17 commit 6f10d58

File tree

11 files changed

+53
-83
lines changed

11 files changed

+53
-83
lines changed

common/channel_type.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,6 @@ struct channel_type *channel_type_anchors_zero_fee_htlc(const tal_t *ctx)
7272
return type;
7373
}
7474

75-
struct channel_type *default_channel_type(const tal_t *ctx,
76-
const struct feature_set *our_features,
77-
const u8 *their_features)
78-
{
79-
/* BOLT #2:
80-
* Both peers:
81-
* - if `channel_type` was present in both `open_channel` and `accept_channel`:
82-
* - This is the `channel_type` (they must be equal, required above)
83-
* - otherwise:
84-
* - if `option_anchors` was negotiated:
85-
* - the `channel_type` is `option_anchors` and `option_static_remotekey` (bits 22 and 12)
86-
* - otherwise:
87-
* - the `channel_type` is `option_static_remotekey` (bit 12)
88-
*/
89-
if (feature_negotiated(our_features, their_features,
90-
OPT_ANCHORS_ZERO_FEE_HTLC_TX))
91-
return channel_type_anchors_zero_fee_htlc(ctx);
92-
return channel_type_static_remotekey(ctx);
93-
}
94-
9575
bool channel_type_has(const struct channel_type *type, int feature)
9676
{
9777
return feature_offered(type->features, feature);

common/channel_type.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ struct channel_type *channel_type_dup(const tal_t *ctx,
2121
struct channel_type *channel_type_from(const tal_t *ctx,
2222
const u8 *features TAKES);
2323

24-
/* Derive channel type from feature negotiation */
25-
struct channel_type *default_channel_type(const tal_t *ctx,
26-
const struct feature_set *our_features,
27-
const u8 *their_features);
28-
2924
/* Does this type include this feature? */
3025
bool channel_type_has(const struct channel_type *type, int feature);
3126

lightningd/channel.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,16 @@ struct open_attempt *new_channel_open_attempt(struct channel *channel)
242242
return oa;
243243
}
244244

245+
struct channel_type *desired_channel_type(const tal_t *ctx,
246+
const struct feature_set *our_features,
247+
const u8 *their_features)
248+
{
249+
if (feature_negotiated(our_features, their_features,
250+
OPT_ANCHORS_ZERO_FEE_HTLC_TX))
251+
return channel_type_anchors_zero_fee_htlc(ctx);
252+
return channel_type_static_remotekey(ctx);
253+
}
254+
245255
struct channel *new_unsaved_channel(struct peer *peer,
246256
u32 feerate_base,
247257
u32 feerate_ppm)
@@ -305,7 +315,7 @@ struct channel *new_unsaved_channel(struct peer *peer,
305315
channel->close_blockheight = NULL;
306316
/* In case someone looks at channels before open negotiation,
307317
* initialize this with default */
308-
channel->type = default_channel_type(channel,
318+
channel->type = desired_channel_type(channel,
309319
ld->our_features,
310320
peer->their_features);
311321

lightningd/channel.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,5 +899,9 @@ const u8 *channel_update_for_error(const tal_t *ctx,
899899

900900
struct amount_msat htlc_max_possible_send(const struct channel *channel);
901901

902+
/* Given features, what channel_type do we want? */
903+
struct channel_type *desired_channel_type(const tal_t *ctx,
904+
const struct feature_set *our_features,
905+
const u8 *their_features);
902906

903907
#endif /* LIGHTNING_LIGHTNINGD_CHANNEL_H */

lightningd/dual_open_control.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,8 +3249,11 @@ static struct command_result *json_openchannel_init(struct command *cmd,
32493249
"by peer");
32503250
}
32513251

3252-
if (info->ctype &&
3253-
!cmd->ld->dev_any_channel_type &&
3252+
if (!info->ctype)
3253+
info->ctype = desired_channel_type(info, cmd->ld->our_features,
3254+
peer->their_features);
3255+
3256+
if (!cmd->ld->dev_any_channel_type &&
32543257
!channel_type_accept(tmpctx,
32553258
info->ctype->features,
32563259
cmd->ld->our_features)) {
@@ -3882,7 +3885,9 @@ static struct command_result *json_queryrates(struct command *cmd,
38823885
NULL : request_amt,
38833886
get_block_height(cmd->ld->topology),
38843887
true,
3885-
NULL, NULL);
3888+
desired_channel_type(tmpctx, cmd->ld->our_features,
3889+
peer->their_features),
3890+
NULL);
38863891

38873892
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) != 0) {
38883893
return command_fail(cmd, FUND_MAX_EXCEEDED,

lightningd/opening_control.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ void json_add_uncommitted_channel(struct command *cmd,
4747
json_object_start(response, NULL);
4848
json_add_node_id(response, "peer_id", &peer->id);
4949
json_add_bool(response, "peer_connected", peer->connected == PEER_CONNECTED);
50-
if (uc->fc->channel_type)
51-
json_add_channel_type(response, "channel_type", uc->fc->channel_type);
50+
json_add_channel_type(response, "channel_type", uc->fc->channel_type);
5251
json_add_string(response, "state", "OPENINGD");
5352
json_add_string(response, "owner", "lightning_openingd");
5453
json_add_string(response, "opener", "local");
@@ -332,6 +331,10 @@ static void opening_funder_start_replied(struct subd *openingd, const u8 *resp,
332331
{
333332
bool supports_shutdown_script;
334333

334+
/* It will tell us the resulting channel type (which can vary
335+
* by ZEROCONF and SCID_ALIAS), so free old one */
336+
tal_free(fc->channel_type);
337+
335338
if (!fromwire_openingd_funder_start_reply(fc, resp,
336339
&fc->funding_scriptpubkey,
337340
&supports_shutdown_script,
@@ -1297,7 +1300,7 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
12971300
}
12981301
}
12991302
} else {
1300-
fc->channel_type = NULL;
1303+
fc->channel_type = NULL; /* set later */
13011304
}
13021305

13031306
if (!mindepth)
@@ -1389,6 +1392,11 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
13891392
if (command_check_only(cmd))
13901393
return command_check_done(cmd);
13911394

1395+
/* Now we know the peer, we can derive the channel type to ask for */
1396+
if (!fc->channel_type)
1397+
fc->channel_type = desired_channel_type(fc, cmd->ld->our_features,
1398+
peer->their_features);
1399+
13921400
fc->push = push_msat ? *push_msat : AMOUNT_MSAT(0);
13931401
fc->channel_flags = OUR_CHANNEL_FLAGS;
13941402
if (!*announce_channel) {
@@ -1426,7 +1434,7 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
14261434
&tmp_channel_id,
14271435
fc->channel_flags,
14281436
reserve,
1429-
ctype);
1437+
fc->channel_type);
14301438

14311439
if (!topology_synced(cmd->ld->topology)) {
14321440
struct fundchannel_start_info *info

openingd/dualopend.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,7 +2961,6 @@ static void opener_start(struct state *state, u8 *msg)
29612961
struct amount_sat *requested_lease;
29622962
size_t locktime;
29632963
u32 nonanchor_feerate, anchor_feerate;
2964-
struct channel_type *ctype;
29652964

29662965
if (!fromwire_dualopend_opener_init(state, msg,
29672966
&tx_state->psbt,
@@ -2975,30 +2974,14 @@ static void opener_start(struct state *state, u8 *msg)
29752974
&requested_lease,
29762975
&tx_state->blockheight,
29772976
&dry_run,
2978-
&ctype,
2977+
&state->channel_type,
29792978
&expected_rates))
29802979
master_badmsg(WIRE_DUALOPEND_OPENER_INIT, msg);
29812980

29822981
state->our_role = TX_INITIATOR;
29832982
wally_psbt_get_locktime(tx_state->psbt, &locktime);
29842983
tx_state->tx_locktime = locktime;
29852984
open_tlv = tlv_opening_tlvs_new(tmpctx);
2986-
2987-
/* BOLT #2:
2988-
* - if it includes `channel_type`:
2989-
* - MUST set it to a defined type representing the type it wants.
2990-
* - MUST use the smallest bitmap possible to represent the channel
2991-
* type.
2992-
* - SHOULD NOT set it to a type containing a feature which was not
2993-
* negotiated.
2994-
*/
2995-
if (ctype) {
2996-
state->channel_type = ctype;
2997-
} else {
2998-
state->channel_type = default_channel_type(state,
2999-
state->our_features,
3000-
state->their_features);
3001-
}
30022985
open_tlv->channel_type = state->channel_type->features;
30032986

30042987
/* Given channel type, which feerate do we use? */

openingd/dualopend_wire.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ msgdata,dualopend_opener_init,channel_flags,u8,
211211
msgdata,dualopend_opener_init,requested_sats,?amount_sat,
212212
msgdata,dualopend_opener_init,blockheight,u32,
213213
msgdata,dualopend_opener_init,dry_run,bool,
214-
msgdata,dualopend_opener_init,channel_type,?channel_type,
214+
msgdata,dualopend_opener_init,channel_type,channel_type,
215215
# must go last because embedded tu32
216216
msgdata,dualopend_opener_init,expected_rates,?lease_rates,
217217

openingd/openingd.c

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ static bool intuit_scid_alias_type(struct state *state, u8 channel_flags)
294294
* stop when we get to the part where we need the funding txid */
295295
static u8 *funder_channel_start(struct state *state, u8 channel_flags,
296296
u32 nonanchor_feerate, u32 anchor_feerate,
297-
const struct channel_type *ctype)
297+
const struct channel_type *ctype TAKES)
298298
{
299299
u8 *msg;
300300
u8 *funding_output_script;
@@ -323,28 +323,12 @@ static u8 *funder_channel_start(struct state *state, u8 channel_flags,
323323
state->our_features,
324324
state->their_features);
325325

326-
if (ctype) {
327-
state->channel_type = channel_type_dup(state, ctype);
328-
} else {
329-
state->channel_type = default_channel_type(state,
330-
state->our_features,
331-
state->their_features);
332-
333-
/* Spec says we should use the option_scid_alias variation if we
334-
* want them to *only* use the scid_alias (which we do for unannounced
335-
* channels!).
336-
*
337-
* But:
338-
* 1. We didn't accept this in CLN prior to v23.05.
339-
* 2. LND won't accept that without OPT_ANCHORS_ZERO_FEE_HTLC_TX.
340-
* 3. LND <= 18 won't accept OPT_SCID_ALIAS unless it advertizes it,
341-
* which it does not by default.
342-
*/
343-
if (channel_type_has(state->channel_type, OPT_ANCHORS_ZERO_FEE_HTLC_TX)
344-
&& feature_offered(state->their_features, OPT_SCID_ALIAS)) {
345-
if (!(channel_flags & CHANNEL_FLAGS_ANNOUNCE_CHANNEL))
346-
channel_type_set_scid_alias(state->channel_type);
347-
}
326+
state->channel_type = channel_type_dup(state, ctype);
327+
/* We set scid alias if we're not announcing */
328+
if (feature_negotiated(state->our_features, state->their_features,
329+
OPT_SCID_ALIAS)
330+
&& !(channel_flags & CHANNEL_FLAGS_ANNOUNCE_CHANNEL)) {
331+
channel_type_set_scid_alias(state->channel_type);
348332
}
349333

350334
/* Which feerate do we use? (We can lowball fees if using anchors!) */
@@ -569,7 +553,7 @@ static u8 *funder_channel_start(struct state *state, u8 channel_flags,
569553
"We negotiated option_zeroconf, using our minimum_depth=%d",
570554
state->minimum_depth);
571555
/* We set this now to show we're zeroconf */
572-
if (their_mindepth == 0 && !ctype)
556+
if (their_mindepth == 0)
573557
channel_type_set_zeroconf(state->channel_type);
574558
} else {
575559
state->minimum_depth = their_mindepth;

openingd/openingd_wire.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ msgdata,openingd_funder_start,anchor_feerate_per_kw,u32,
8888
msgdata,openingd_funder_start,temporary_channel_id,channel_id,
8989
msgdata,openingd_funder_start,channel_flags,u8,
9090
msgdata,openingd_funder_start,reserve,?amount_sat,
91-
msgdata,openingd_funder_start,channel_type,?channel_type,
91+
msgdata,openingd_funder_start,channel_type,channel_type,
9292

9393
# openingd->master: send back output script for 2-of-2 funding output
9494
msgtype,openingd_funder_start_reply,6102

0 commit comments

Comments
 (0)