common/features: expose feature bitmap low-level functions.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2019-09-03 13:46:03 +09:30 committed by neil saitug
parent 58f448904c
commit 2e3eadbe91
3 changed files with 17 additions and 13 deletions

View File

@ -21,7 +21,7 @@ static const u32 our_globalfeatures[] = {
*
* All data fields are unsigned big-endian unless otherwise specified.
*/
static void set_bit(u8 **ptr, u32 bit)
void set_feature_bit(u8 **ptr, u32 bit)
{
size_t len = tal_count(*ptr);
if (bit / 8 >= len) {
@ -46,7 +46,7 @@ static u8 *mkfeatures(const tal_t *ctx, const u32 *arr, size_t n)
u8 *f = tal_arr(ctx, u8, 0);
for (size_t i = 0; i < n; i++)
set_bit(&f, arr[i]);
set_feature_bit(&f, arr[i]);
return f;
}
@ -62,7 +62,7 @@ u8 *get_offered_localfeatures(const tal_t *ctx)
our_localfeatures, ARRAY_SIZE(our_localfeatures));
}
static bool feature_set(const u8 *features, size_t bit)
bool feature_is_set(const u8 *features, size_t bit)
{
size_t bytenum = bit / 8;
@ -74,8 +74,8 @@ static bool feature_set(const u8 *features, size_t bit)
bool feature_offered(const u8 *features, size_t f)
{
return feature_set(features, COMPULSORY_FEATURE(f))
|| feature_set(features, OPTIONAL_FEATURE(f));
return feature_is_set(features, COMPULSORY_FEATURE(f))
|| feature_is_set(features, OPTIONAL_FEATURE(f));
}
static bool feature_supported(int feature_bit,
@ -124,8 +124,8 @@ bool features_supported(const u8 *globalfeatures, const u8 *localfeatures)
{
/* BIT 2 would logically be "compulsory initial_routing_sync", but
* that does not exist, so we special case it. */
if (feature_set(localfeatures,
COMPULSORY_FEATURE(LOCAL_INITIAL_ROUTING_SYNC)))
if (feature_is_set(localfeatures,
COMPULSORY_FEATURE(LOCAL_INITIAL_ROUTING_SYNC)))
return false;
return all_supported_features(globalfeatures,

View File

@ -21,6 +21,10 @@ bool global_feature_negotiated(const u8 *gfeatures, size_t f);
/* Return a list of what features we advertize. */
const char **list_supported_features(const tal_t *ctx);
/* Low-level helpers to deal with big-endian bitfields. */
bool feature_is_set(const u8 *features, size_t bit);
void set_feature_bit(u8 **ptr, u32 bit);
/* BOLT #9:
*
* Flags are numbered from the least-significant bit, at bit 0 (i.e. 0x1,

View File

@ -23,12 +23,12 @@ int main(void)
bits = tal_arr(tmpctx, u8, 0);
for (size_t i = 0; i < 100; i += 3)
set_bit(&bits, i);
set_feature_bit(&bits, i);
for (size_t i = 0; i < 100; i++)
assert(test_bit(bits, i / 8, i % 8) == ((i % 3) == 0));
for (size_t i = 0; i < 100; i++)
assert(feature_set(bits, i) == ((i % 3) == 0));
assert(feature_is_set(bits, i) == ((i % 3) == 0));
/* Simple test: single byte */
bits = tal_arr(tmpctx, u8, 1);
@ -75,18 +75,18 @@ int main(void)
/* We can add random odd features, no problem. */
for (size_t i = 1; i < 16; i += 2) {
bits = tal_dup_arr(tmpctx, u8, lf, tal_count(lf), 0);
set_bit(&bits, i);
set_feature_bit(&bits, i);
assert(features_supported(gf, bits));
bits = tal_dup_arr(tmpctx, u8, gf, tal_count(gf), 0);
set_bit(&bits, i);
set_feature_bit(&bits, i);
assert(features_supported(bits, lf));
}
/* We can't add random even features. */
for (size_t i = 0; i < 16; i += 2) {
bits = tal_dup_arr(tmpctx, u8, lf, tal_count(lf), 0);
set_bit(&bits, i);
set_feature_bit(&bits, i);
/* Special case for missing compulsory feature */
if (i == 2) {
@ -98,7 +98,7 @@ int main(void)
}
bits = tal_dup_arr(tmpctx, u8, gf, tal_count(gf), 0);
set_bit(&bits, i);
set_feature_bit(&bits, i);
assert(features_supported(bits, lf)
== feature_supported(i, our_globalfeatures,
ARRAY_SIZE(our_globalfeatures)));