Skip to content

Assume option_channel_type #8389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions common/channel_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,6 @@ struct channel_type *channel_type_anchors_zero_fee_htlc(const tal_t *ctx)
return type;
}

struct channel_type *default_channel_type(const tal_t *ctx,
const struct feature_set *our_features,
const u8 *their_features)
{
/* BOLT #2:
* Both peers:
* - if `channel_type` was present in both `open_channel` and `accept_channel`:
* - This is the `channel_type` (they must be equal, required above)
* - otherwise:
* - if `option_anchors` was negotiated:
* - the `channel_type` is `option_anchors` and `option_static_remotekey` (bits 22 and 12)
* - otherwise:
* - the `channel_type` is `option_static_remotekey` (bit 12)
*/
if (feature_negotiated(our_features, their_features,
OPT_ANCHORS_ZERO_FEE_HTLC_TX))
return channel_type_anchors_zero_fee_htlc(ctx);
return channel_type_static_remotekey(ctx);
}

bool channel_type_has(const struct channel_type *type, int feature)
{
return feature_offered(type->features, feature);
Expand Down
5 changes: 0 additions & 5 deletions common/channel_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ struct channel_type *channel_type_dup(const tal_t *ctx,
struct channel_type *channel_type_from(const tal_t *ctx,
const u8 *features TAKES);

/* Derive channel type from feature negotiation */
struct channel_type *default_channel_type(const tal_t *ctx,
const struct feature_set *our_features,
const u8 *their_features);

/* Does this type include this feature? */
bool channel_type_has(const struct channel_type *type, int feature);

Expand Down
12 changes: 11 additions & 1 deletion lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,16 @@ struct open_attempt *new_channel_open_attempt(struct channel *channel)
return oa;
}

struct channel_type *desired_channel_type(const tal_t *ctx,
const struct feature_set *our_features,
const u8 *their_features)
{
if (feature_negotiated(our_features, their_features,
OPT_ANCHORS_ZERO_FEE_HTLC_TX))
return channel_type_anchors_zero_fee_htlc(ctx);
return channel_type_static_remotekey(ctx);
}

struct channel *new_unsaved_channel(struct peer *peer,
u32 feerate_base,
u32 feerate_ppm)
Expand Down Expand Up @@ -305,7 +315,7 @@ struct channel *new_unsaved_channel(struct peer *peer,
channel->close_blockheight = NULL;
/* In case someone looks at channels before open negotiation,
* initialize this with default */
channel->type = default_channel_type(channel,
channel->type = desired_channel_type(channel,
ld->our_features,
peer->their_features);

Expand Down
4 changes: 4 additions & 0 deletions lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -899,5 +899,9 @@ const u8 *channel_update_for_error(const tal_t *ctx,

struct amount_msat htlc_max_possible_send(const struct channel *channel);

/* Given features, what channel_type do we want? */
struct channel_type *desired_channel_type(const tal_t *ctx,
const struct feature_set *our_features,
const u8 *their_features);

#endif /* LIGHTNING_LIGHTNINGD_CHANNEL_H */
7 changes: 5 additions & 2 deletions lightningd/dual_open_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -3249,8 +3249,11 @@ static struct command_result *json_openchannel_init(struct command *cmd,
"by peer");
}

if (info->ctype &&
!cmd->ld->dev_any_channel_type &&
if (!info->ctype)
info->ctype = desired_channel_type(info, cmd->ld->our_features,
peer->their_features);

if (!cmd->ld->dev_any_channel_type &&
!channel_type_accept(tmpctx,
info->ctype->features,
cmd->ld->our_features)) {
Expand Down
2 changes: 1 addition & 1 deletion lightningd/lightningd.c
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ static struct feature_set *default_features(const tal_t *ctx)
OPTIONAL_FEATURE(OPT_ZEROCONF),
OPTIONAL_FEATURE(OPT_QUIESCE),
OPTIONAL_FEATURE(OPT_ONION_MESSAGES),
OPTIONAL_FEATURE(OPT_CHANNEL_TYPE),
COMPULSORY_FEATURE(OPT_CHANNEL_TYPE),
OPTIONAL_FEATURE(OPT_ROUTE_BLINDING),
OPTIONAL_FEATURE(OPT_PROVIDE_STORAGE),
/* Removed later for elements */
Expand Down
16 changes: 12 additions & 4 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ void json_add_uncommitted_channel(struct command *cmd,
json_object_start(response, NULL);
json_add_node_id(response, "peer_id", &peer->id);
json_add_bool(response, "peer_connected", peer->connected == PEER_CONNECTED);
if (uc->fc->channel_type)
json_add_channel_type(response, "channel_type", uc->fc->channel_type);
json_add_channel_type(response, "channel_type", uc->fc->channel_type);
json_add_string(response, "state", "OPENINGD");
json_add_string(response, "owner", "lightning_openingd");
json_add_string(response, "opener", "local");
Expand Down Expand Up @@ -332,6 +331,10 @@ static void opening_funder_start_replied(struct subd *openingd, const u8 *resp,
{
bool supports_shutdown_script;

/* It will tell us the resulting channel type (which can vary
* by ZEROCONF and SCID_ALIAS), so free old one */
tal_free(fc->channel_type);

if (!fromwire_openingd_funder_start_reply(fc, resp,
&fc->funding_scriptpubkey,
&supports_shutdown_script,
Expand Down Expand Up @@ -1297,7 +1300,7 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
}
}
} else {
fc->channel_type = NULL;
fc->channel_type = NULL; /* set later */
}

if (!mindepth)
Expand Down Expand Up @@ -1389,6 +1392,11 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
if (command_check_only(cmd))
return command_check_done(cmd);

/* Now we know the peer, we can derive the channel type to ask for */
if (!fc->channel_type)
fc->channel_type = desired_channel_type(fc, cmd->ld->our_features,
peer->their_features);

fc->push = push_msat ? *push_msat : AMOUNT_MSAT(0);
fc->channel_flags = OUR_CHANNEL_FLAGS;
if (!*announce_channel) {
Expand Down Expand Up @@ -1426,7 +1434,7 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
&tmp_channel_id,
fc->channel_flags,
reserve,
ctype);
fc->channel_type);

if (!topology_synced(cmd->ld->topology)) {
struct fundchannel_start_info *info
Expand Down
75 changes: 31 additions & 44 deletions openingd/dualopend.c
Original file line number Diff line number Diff line change
Expand Up @@ -2434,30 +2434,30 @@ static void accepter_start(struct state *state, const u8 *oc2_msg)
* - if `type` is not suitable.
* - if `type` includes `option_zeroconf` and it does not trust the sender to open an unconfirmed channel.
*/
if (open_tlv->channel_type) {
state->channel_type =
channel_type_accept(state,
open_tlv->channel_type,
state->our_features);
if (!state->channel_type) {
if (state->dev_accept_any_channel_type) {
status_unusual("dev-any-channel-type: accepting %s",
fmt_featurebits(tmpctx,
open_tlv->channel_type));
state->channel_type = channel_type_from(state, open_tlv->channel_type);
} else {
negotiation_failed(state,
"Did not support channel_type [%s]",
fmt_featurebits(tmpctx,
open_tlv->channel_type));
return;
}
if (!open_tlv->channel_type) {
negotiation_failed(state,
"open_channel2 missing channel_type");
return;
}

state->channel_type =
channel_type_accept(state,
open_tlv->channel_type,
state->our_features);
if (!state->channel_type) {
if (state->dev_accept_any_channel_type) {
status_unusual("dev-any-channel-type: accepting %s",
fmt_featurebits(tmpctx,
open_tlv->channel_type));
state->channel_type = channel_type_from(state, open_tlv->channel_type);
} else {
negotiation_failed(state,
"Did not support channel_type [%s]",
fmt_featurebits(tmpctx,
open_tlv->channel_type));
return;
}
} else
state->channel_type
= default_channel_type(state,
state->our_features,
state->their_features);
}

/* Since anchor outputs are optional, we
* only support liquidity ads if those are enabled. */
Expand Down Expand Up @@ -2961,7 +2961,6 @@ static void opener_start(struct state *state, u8 *msg)
struct amount_sat *requested_lease;
size_t locktime;
u32 nonanchor_feerate, anchor_feerate;
struct channel_type *ctype;

if (!fromwire_dualopend_opener_init(state, msg,
&tx_state->psbt,
Expand All @@ -2975,30 +2974,14 @@ static void opener_start(struct state *state, u8 *msg)
&requested_lease,
&tx_state->blockheight,
&dry_run,
&ctype,
&state->channel_type,
&expected_rates))
master_badmsg(WIRE_DUALOPEND_OPENER_INIT, msg);

state->our_role = TX_INITIATOR;
wally_psbt_get_locktime(tx_state->psbt, &locktime);
tx_state->tx_locktime = locktime;
open_tlv = tlv_opening_tlvs_new(tmpctx);

/* BOLT #2:
* - if it includes `channel_type`:
* - MUST set it to a defined type representing the type it wants.
* - MUST use the smallest bitmap possible to represent the channel
* type.
* - SHOULD NOT set it to a type containing a feature which was not
* negotiated.
*/
if (ctype) {
state->channel_type = ctype;
} else {
state->channel_type = default_channel_type(state,
state->our_features,
state->their_features);
}
open_tlv->channel_type = state->channel_type->features;

/* Given channel type, which feerate do we use? */
Expand Down Expand Up @@ -3164,9 +3147,13 @@ static void opener_start(struct state *state, u8 *msg)
* `open_channel`, and they are not equal types:
* - MUST fail the channel.
*/
if (a_tlv->channel_type
&& !featurebits_eq(a_tlv->channel_type,
state->channel_type->features)) {
if (!a_tlv->channel_type) {
negotiation_failed(state,
"Missing channel_type in accept_channel2");
return;
}
if (!featurebits_eq(a_tlv->channel_type,
state->channel_type->features)) {
negotiation_failed(state,
"Return unoffered channel_type: %s",
fmt_featurebits(tmpctx,
Expand Down
2 changes: 1 addition & 1 deletion openingd/dualopend_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ msgdata,dualopend_opener_init,channel_flags,u8,
msgdata,dualopend_opener_init,requested_sats,?amount_sat,
msgdata,dualopend_opener_init,blockheight,u32,
msgdata,dualopend_opener_init,dry_run,bool,
msgdata,dualopend_opener_init,channel_type,?channel_type,
msgdata,dualopend_opener_init,channel_type,channel_type,
# must go last because embedded tu32
msgdata,dualopend_opener_init,expected_rates,?lease_rates,

Expand Down
Loading
Loading