common/channel_type: wrapper for generated 'struct channel_type'.

We make it a first-class citizen internally, even though we won't use
it over the wire (at least, non-experimental builds).  This scheme
follows the latest draft, in which features are flagged compulsory.

We also add several helper functions.

Since uses the *even* bits (as per latest spec), not the *odd* bits,
we have some other fixups.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2021-09-09 12:20:52 +09:30
committed by Christian Decker
parent 1b8551923d
commit cb22015b2a
74 changed files with 404 additions and 122 deletions

View File

@@ -7,7 +7,7 @@
#include <ccan/cast/cast.h>
#include <ccan/tal/str/str.h>
#include <common/blockheight_states.h>
#include <common/features.h>
#include <common/channel_type.h>
#include <common/fee_states.h>
#include <common/initial_channel.h>
#include <common/initial_commit_tx.h>
@@ -169,53 +169,15 @@ u32 channel_blockheight(const struct channel *channel, enum side side)
channel->opener, side);
}
#if EXPERIMENTAL_FEATURES
/* BOLT-upgrade_protocol #2:
* Channel features are explicitly enumerated as `channel_type` bitfields,
* using odd features bits. The currently defined types are:
* - no features (no bits set)
* - `option_static_remotekey` (bit 13)
* - `option_anchor_outputs` and `option_static_remotekey` (bits 21 and 13)
* - `option_anchors_zero_fee_htlc_tx` and `option_static_remotekey` (bits 23
* and 13)
*/
static struct channel_type *new_channel_type(const tal_t *ctx)
struct channel_type *current_channel_type(const tal_t *ctx,
const struct channel *channel)
{
struct channel_type *type = tal(ctx, struct channel_type);
if (channel->option_anchor_outputs)
return channel_type_anchor_outputs(ctx);
if (channel->option_static_remotekey)
return channel_type_static_remotekey(ctx);
type->features = tal_arr(type, u8, 0);
return type;
}
static struct channel_type *type_static_remotekey(const tal_t *ctx)
{
struct channel_type *type = new_channel_type(ctx);
set_feature_bit(&type->features,
OPTIONAL_FEATURE(OPT_STATIC_REMOTEKEY));
return type;
}
static struct channel_type *type_anchor_outputs(const tal_t *ctx)
{
struct channel_type *type = new_channel_type(ctx);
set_feature_bit(&type->features,
OPTIONAL_FEATURE(OPT_ANCHOR_OUTPUTS));
set_feature_bit(&type->features,
OPTIONAL_FEATURE(OPT_STATIC_REMOTEKEY));
return type;
}
struct channel_type *channel_type(const tal_t *ctx,
const struct channel *channel)
{
if (channel->option_anchor_outputs)
return type_anchor_outputs(ctx);
if (channel->option_static_remotekey)
return type_static_remotekey(ctx);
return new_channel_type(ctx);
return channel_type_none(ctx);
}
struct channel_type **channel_upgradable_types(const tal_t *ctx,
@@ -224,7 +186,7 @@ struct channel_type **channel_upgradable_types(const tal_t *ctx,
struct channel_type **arr = tal_arr(ctx, struct channel_type *, 0);
if (!channel->option_static_remotekey)
tal_arr_expand(&arr, type_static_remotekey(arr));
tal_arr_expand(&arr, channel_type_static_remotekey(arr));
return arr;
}
@@ -234,12 +196,11 @@ struct channel_type *channel_desired_type(const tal_t *ctx,
{
/* We don't actually want to downgrade anchors! */
if (channel->option_anchor_outputs)
return type_anchor_outputs(ctx);
return channel_type_anchor_outputs(ctx);
/* For now, we just want option_static_remotekey */
return type_static_remotekey(ctx);
return channel_type_static_remotekey(ctx);
}
#endif /* EXPERIMENTAL_FEATURES */
static char *fmt_channel_view(const tal_t *ctx, const struct channel_view *view)
{