2021-12-04 21:53:56 +10:30
|
|
|
#include "config.h"
|
2021-09-09 12:20:52 +09:30
|
|
|
#include <ccan/array_size/array_size.h>
|
|
|
|
|
#include <common/channel_type.h>
|
2025-10-22 19:44:27 +10:30
|
|
|
#include <common/utils.h>
|
2021-09-09 12:20:52 +09:30
|
|
|
|
2024-06-19 09:30:01 +09:30
|
|
|
static struct channel_type *new_channel_type(const tal_t *ctx)
|
|
|
|
|
{
|
|
|
|
|
struct channel_type *type = tal(ctx, struct channel_type);
|
|
|
|
|
|
|
|
|
|
type->features = tal_arr(type, u8, 0);
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* These can still exist in the database */
|
|
|
|
|
struct channel_type *channel_type_none_obsolete(const tal_t *ctx)
|
|
|
|
|
{
|
|
|
|
|
return new_channel_type(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-09-10 11:44:24 +09:30
|
|
|
/* BOLT #2:
|
2021-09-09 12:20:52 +09:30
|
|
|
* Channel types are an explicit enumeration: for convenience of future
|
|
|
|
|
* definitions they reuse even feature bits, but they are not an
|
|
|
|
|
* arbitrary combination (they represent the persistent features which
|
|
|
|
|
* affect the channel operation).
|
|
|
|
|
*
|
2022-09-10 11:40:31 +09:30
|
|
|
* The currently defined basic types are:
|
2021-09-09 12:20:52 +09:30
|
|
|
* - `option_static_remotekey` (bit 12)
|
2024-06-19 10:06:41 +09:30
|
|
|
* - `option_anchors` and `option_static_remotekey` (bits 22 and 12)
|
2024-06-19 09:30:01 +09:30
|
|
|
*
|
|
|
|
|
* Each basic type has the following variations allowed:
|
2021-09-09 12:20:52 +09:30
|
|
|
*/
|
|
|
|
|
struct channel_type *channel_type_static_remotekey(const tal_t *ctx)
|
|
|
|
|
{
|
2024-06-19 09:30:01 +09:30
|
|
|
struct channel_type *type = new_channel_type(ctx);
|
2021-09-09 12:20:52 +09:30
|
|
|
|
|
|
|
|
set_feature_bit(&type->features,
|
|
|
|
|
COMPULSORY_FEATURE(OPT_STATIC_REMOTEKEY));
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-19 09:31:01 +09:30
|
|
|
struct channel_type *channel_type_anchor_outputs_obsolete(const tal_t *ctx)
|
2021-09-09 12:20:52 +09:30
|
|
|
{
|
2024-06-19 09:30:01 +09:30
|
|
|
struct channel_type *type = new_channel_type(ctx);
|
2021-09-09 12:20:52 +09:30
|
|
|
|
|
|
|
|
set_feature_bit(&type->features,
|
2024-06-19 09:31:01 +09:30
|
|
|
COMPULSORY_FEATURE(OPT_ANCHOR_OUTPUTS_DEPRECATED));
|
2021-09-09 12:20:52 +09:30
|
|
|
set_feature_bit(&type->features,
|
|
|
|
|
COMPULSORY_FEATURE(OPT_STATIC_REMOTEKEY));
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 09:50:56 +09:30
|
|
|
void channel_type_set_zeroconf(struct channel_type *type)
|
|
|
|
|
{
|
|
|
|
|
set_feature_bit(&type->features,
|
|
|
|
|
COMPULSORY_FEATURE(OPT_ZEROCONF));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void channel_type_set_scid_alias(struct channel_type *type)
|
|
|
|
|
{
|
|
|
|
|
set_feature_bit(&type->features,
|
|
|
|
|
COMPULSORY_FEATURE(OPT_SCID_ALIAS));
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-26 08:47:21 +09:30
|
|
|
struct channel_type *channel_type_anchors_zero_fee_htlc(const tal_t *ctx)
|
|
|
|
|
{
|
2024-06-19 09:30:01 +09:30
|
|
|
struct channel_type *type = new_channel_type(ctx);
|
2023-06-26 08:47:21 +09:30
|
|
|
|
|
|
|
|
set_feature_bit(&type->features,
|
|
|
|
|
COMPULSORY_FEATURE(OPT_ANCHORS_ZERO_FEE_HTLC_TX));
|
|
|
|
|
set_feature_bit(&type->features,
|
|
|
|
|
COMPULSORY_FEATURE(OPT_STATIC_REMOTEKEY));
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 12:20:52 +09:30
|
|
|
bool channel_type_has(const struct channel_type *type, int feature)
|
|
|
|
|
{
|
|
|
|
|
return feature_offered(type->features, feature);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-26 08:37:21 +09:30
|
|
|
bool channel_type_has_anchors(const struct channel_type *type)
|
|
|
|
|
{
|
2024-06-19 09:31:01 +09:30
|
|
|
return feature_offered(type->features, OPT_ANCHOR_OUTPUTS_DEPRECATED)
|
2023-06-26 08:37:21 +09:30
|
|
|
|| feature_offered(type->features, OPT_ANCHORS_ZERO_FEE_HTLC_TX);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 12:20:52 +09:30
|
|
|
bool channel_type_eq(const struct channel_type *a,
|
|
|
|
|
const struct channel_type *b)
|
|
|
|
|
{
|
|
|
|
|
return featurebits_eq(a->features, b->features);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct channel_type *channel_type_dup(const tal_t *ctx,
|
|
|
|
|
const struct channel_type *t)
|
|
|
|
|
{
|
|
|
|
|
struct channel_type *ret = tal(ctx, struct channel_type);
|
|
|
|
|
ret->features = tal_dup_talarr(ret, u8, t->features);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-08 10:10:30 +10:30
|
|
|
struct channel_type *channel_type_from(const tal_t *ctx,
|
|
|
|
|
const u8 *features TAKES)
|
|
|
|
|
{
|
|
|
|
|
struct channel_type *ret = tal(ctx, struct channel_type);
|
|
|
|
|
ret->features = tal_dup_talarr(ret, u8, features);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 12:20:52 +09:30
|
|
|
struct channel_type *channel_type_accept(const tal_t *ctx,
|
|
|
|
|
const u8 *t,
|
2024-01-29 10:04:16 +10:30
|
|
|
const struct feature_set *our_features)
|
2021-09-09 12:20:52 +09:30
|
|
|
{
|
2022-04-19 12:49:32 +02:00
|
|
|
struct channel_type *ctype, proposed;
|
|
|
|
|
/* Need to copy since we're going to blank variant bits for equality. */
|
|
|
|
|
proposed.features = tal_dup_talarr(tmpctx, u8, t);
|
|
|
|
|
|
|
|
|
|
static const size_t feats[] = {
|
2023-06-26 08:47:21 +09:30
|
|
|
OPT_ANCHORS_ZERO_FEE_HTLC_TX,
|
2022-04-19 12:49:32 +02:00
|
|
|
OPT_STATIC_REMOTEKEY,
|
2023-04-10 09:50:56 +09:30
|
|
|
OPT_SCID_ALIAS,
|
2022-04-19 12:49:32 +02:00
|
|
|
OPT_ZEROCONF,
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-10 11:40:31 +09:30
|
|
|
/* BOLT #2:
|
|
|
|
|
* Each basic type has the following variations allowed:
|
|
|
|
|
* - `option_scid_alias` (bit 46)
|
|
|
|
|
* - `option_zeroconf` (bit 50)
|
|
|
|
|
*/
|
2022-04-19 12:49:32 +02:00
|
|
|
static const size_t variants[] = {
|
2023-04-10 09:50:56 +09:30
|
|
|
OPT_SCID_ALIAS,
|
2022-04-19 12:49:32 +02:00
|
|
|
OPT_ZEROCONF,
|
|
|
|
|
};
|
2021-09-09 12:20:52 +09:30
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(feats); i++) {
|
|
|
|
|
size_t f = feats[i];
|
|
|
|
|
|
|
|
|
|
if (feature_offered(t, f)) {
|
|
|
|
|
/* If we don't offer a feature, we don't allow it. */
|
|
|
|
|
if (!feature_offered(our_features->bits[INIT_FEATURE], f))
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
/* We assume that if we *require* a feature, we require
|
|
|
|
|
* channels have that. */
|
|
|
|
|
if (feature_is_set(our_features->bits[INIT_FEATURE],
|
|
|
|
|
COMPULSORY_FEATURE(f)))
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 12:49:32 +02:00
|
|
|
/* Blank variants so we can just check for equality. */
|
|
|
|
|
for (size_t i = 0; i< ARRAY_SIZE(variants); i++)
|
|
|
|
|
featurebits_unset(&proposed.features, variants[i]);
|
|
|
|
|
|
2021-09-09 12:20:52 +09:30
|
|
|
/* Otherwise, just needs to be a known channel type. */
|
2024-06-19 09:30:01 +09:30
|
|
|
if (channel_type_eq(&proposed,
|
2022-04-19 12:49:32 +02:00
|
|
|
channel_type_static_remotekey(tmpctx)) ||
|
2023-06-26 08:47:21 +09:30
|
|
|
channel_type_eq(&proposed,
|
2024-06-19 09:31:01 +09:30
|
|
|
channel_type_anchors_zero_fee_htlc(tmpctx))) {
|
2022-04-19 12:49:32 +02:00
|
|
|
/* At this point we know it matches, and maybe has
|
|
|
|
|
* a couple of extra options. So let's just reply
|
|
|
|
|
* with their proposal. */
|
|
|
|
|
ctype = tal(ctx, struct channel_type);
|
|
|
|
|
ctype->features = tal_dup_talarr(ctx, u8, t);
|
|
|
|
|
return ctype;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 12:20:52 +09:30
|
|
|
return NULL;
|
|
|
|
|
}
|
2023-03-20 10:49:27 +10:30
|
|
|
|
|
|
|
|
/* Return an array of feature strings indicating channel type. */
|
|
|
|
|
const char **channel_type_name(const tal_t *ctx, const struct channel_type *t)
|
|
|
|
|
{
|
|
|
|
|
const char **names = tal_arr(ctx, const char *, 0);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < tal_bytelen(t->features) * CHAR_BIT; i++) {
|
|
|
|
|
if (!feature_is_set(t->features, i))
|
|
|
|
|
continue;
|
|
|
|
|
tal_arr_expand(&names,
|
|
|
|
|
feature_name(names, i) + strlen("option_"));
|
|
|
|
|
}
|
|
|
|
|
return names;
|
|
|
|
|
}
|