1 #include <ccan/array_size/array_size.h>
2 #include <common/channel_type.h>
3
4 /* BOLT #2:
5 * Channel types are an explicit enumeration: for convenience of future
6 * definitions they reuse even feature bits, but they are not an
7 * arbitrary combination (they represent the persistent features which
8 * affect the channel operation).
9 *
10 * The currently defined types are:
11 * - no features (no bits set)
12 * - `option_static_remotekey` (bit 12)
13 * - `option_anchor_outputs` and `option_static_remotekey` (bits 20 and 12)
14 * - `option_anchors_zero_fee_htlc_tx` and `option_static_remotekey` (bits 22
15 * and 12)
16 */
channel_type_none(const tal_t * ctx)17 struct channel_type *channel_type_none(const tal_t *ctx)
18 {
19 struct channel_type *type = tal(ctx, struct channel_type);
20
21 type->features = tal_arr(type, u8, 0);
22 return type;
23 }
24
channel_type_static_remotekey(const tal_t * ctx)25 struct channel_type *channel_type_static_remotekey(const tal_t *ctx)
26 {
27 struct channel_type *type = channel_type_none(ctx);
28
29 set_feature_bit(&type->features,
30 COMPULSORY_FEATURE(OPT_STATIC_REMOTEKEY));
31 return type;
32 }
33
channel_type_anchor_outputs(const tal_t * ctx)34 struct channel_type *channel_type_anchor_outputs(const tal_t *ctx)
35 {
36 struct channel_type *type = channel_type_none(ctx);
37
38 set_feature_bit(&type->features,
39 COMPULSORY_FEATURE(OPT_ANCHOR_OUTPUTS));
40 set_feature_bit(&type->features,
41 COMPULSORY_FEATURE(OPT_STATIC_REMOTEKEY));
42 return type;
43 }
44
default_channel_type(const tal_t * ctx,const struct feature_set * our_features,const u8 * their_features)45 struct channel_type *default_channel_type(const tal_t *ctx,
46 const struct feature_set *our_features,
47 const u8 *their_features)
48 {
49 /* BOLT #2:
50 * Both peers:
51 * - if `channel_type` was present in both `open_channel` and `accept_channel`:
52 * - This is the `channel_type` (they must be equal, required above)
53 * - otherwise:
54 */
55 /* BOLT #2:
56 * - otherwise, if `option_anchor_outputs` was negotiated:
57 * - the `channel_type` is `option_anchor_outputs` and
58 * `option_static_remotekey` (bits 20 and 12)
59 */
60 if (feature_negotiated(our_features, their_features,
61 OPT_ANCHOR_OUTPUTS))
62 return channel_type_anchor_outputs(ctx);
63 /* BOLT #2:
64 * - otherwise, if `option_static_remotekey` was negotiated:
65 * - the `channel_type` is `option_static_remotekey` (bit 12)
66 */
67 else if (feature_negotiated(our_features, their_features,
68 OPT_STATIC_REMOTEKEY))
69 return channel_type_static_remotekey(ctx);
70 /* BOLT #2:
71 * - otherwise:
72 * - the `channel_type` is empty
73 */
74 else
75 return channel_type_none(ctx);
76 }
77
channel_type_has(const struct channel_type * type,int feature)78 bool channel_type_has(const struct channel_type *type, int feature)
79 {
80 return feature_offered(type->features, feature);
81 }
82
channel_type_eq(const struct channel_type * a,const struct channel_type * b)83 bool channel_type_eq(const struct channel_type *a,
84 const struct channel_type *b)
85 {
86 return featurebits_eq(a->features, b->features);
87 }
88
channel_type_dup(const tal_t * ctx,const struct channel_type * t)89 struct channel_type *channel_type_dup(const tal_t *ctx,
90 const struct channel_type *t)
91 {
92 struct channel_type *ret = tal(ctx, struct channel_type);
93 ret->features = tal_dup_talarr(ret, u8, t->features);
94 return ret;
95 }
96
channel_type_from(const tal_t * ctx,const u8 * features TAKES)97 struct channel_type *channel_type_from(const tal_t *ctx,
98 const u8 *features TAKES)
99 {
100 struct channel_type *ret = tal(ctx, struct channel_type);
101 ret->features = tal_dup_talarr(ret, u8, features);
102 return ret;
103 }
104
channel_type_accept(const tal_t * ctx,const u8 * t,const struct feature_set * our_features,const u8 * their_features)105 struct channel_type *channel_type_accept(const tal_t *ctx,
106 const u8 *t,
107 const struct feature_set *our_features,
108 const u8 *their_features)
109 {
110 struct channel_type *ctype;
111 static const size_t feats[] = { OPT_ANCHOR_OUTPUTS,
112 OPT_STATIC_REMOTEKEY };
113
114 for (size_t i = 0; i < ARRAY_SIZE(feats); i++) {
115 size_t f = feats[i];
116
117 if (feature_offered(t, f)) {
118 /* If we don't offer a feature, we don't allow it. */
119 if (!feature_offered(our_features->bits[INIT_FEATURE], f))
120 return NULL;
121 } else {
122 /* We assume that if we *require* a feature, we require
123 * channels have that. */
124 if (feature_is_set(our_features->bits[INIT_FEATURE],
125 COMPULSORY_FEATURE(f)))
126 return NULL;
127 }
128 }
129
130 /* Otherwise, just needs to be a known channel type. */
131 ctype = channel_type_none(tmpctx);
132 if (featurebits_eq(t, ctype->features))
133 return tal_steal(ctx, ctype);
134 ctype = channel_type_static_remotekey(tmpctx);
135 if (featurebits_eq(t, ctype->features))
136 return tal_steal(ctx, ctype);
137 ctype = channel_type_anchor_outputs(tmpctx);
138 if (featurebits_eq(t, ctype->features))
139 return tal_steal(ctx, ctype);
140 return NULL;
141 }
142