mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
common/features: expose feature bitmap low-level functions.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
committed by
neil saitug
parent
58f448904c
commit
2e3eadbe91
@@ -21,7 +21,7 @@ static const u32 our_globalfeatures[] = {
|
|||||||
*
|
*
|
||||||
* All data fields are unsigned big-endian unless otherwise specified.
|
* 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);
|
size_t len = tal_count(*ptr);
|
||||||
if (bit / 8 >= len) {
|
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);
|
u8 *f = tal_arr(ctx, u8, 0);
|
||||||
|
|
||||||
for (size_t i = 0; i < n; i++)
|
for (size_t i = 0; i < n; i++)
|
||||||
set_bit(&f, arr[i]);
|
set_feature_bit(&f, arr[i]);
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ u8 *get_offered_localfeatures(const tal_t *ctx)
|
|||||||
our_localfeatures, ARRAY_SIZE(our_localfeatures));
|
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;
|
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)
|
bool feature_offered(const u8 *features, size_t f)
|
||||||
{
|
{
|
||||||
return feature_set(features, COMPULSORY_FEATURE(f))
|
return feature_is_set(features, COMPULSORY_FEATURE(f))
|
||||||
|| feature_set(features, OPTIONAL_FEATURE(f));
|
|| feature_is_set(features, OPTIONAL_FEATURE(f));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool feature_supported(int feature_bit,
|
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
|
/* BIT 2 would logically be "compulsory initial_routing_sync", but
|
||||||
* that does not exist, so we special case it. */
|
* that does not exist, so we special case it. */
|
||||||
if (feature_set(localfeatures,
|
if (feature_is_set(localfeatures,
|
||||||
COMPULSORY_FEATURE(LOCAL_INITIAL_ROUTING_SYNC)))
|
COMPULSORY_FEATURE(LOCAL_INITIAL_ROUTING_SYNC)))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return all_supported_features(globalfeatures,
|
return all_supported_features(globalfeatures,
|
||||||
|
|||||||
@@ -21,6 +21,10 @@ bool global_feature_negotiated(const u8 *gfeatures, size_t f);
|
|||||||
/* Return a list of what features we advertize. */
|
/* Return a list of what features we advertize. */
|
||||||
const char **list_supported_features(const tal_t *ctx);
|
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:
|
/* BOLT #9:
|
||||||
*
|
*
|
||||||
* Flags are numbered from the least-significant bit, at bit 0 (i.e. 0x1,
|
* Flags are numbered from the least-significant bit, at bit 0 (i.e. 0x1,
|
||||||
|
|||||||
@@ -23,12 +23,12 @@ int main(void)
|
|||||||
|
|
||||||
bits = tal_arr(tmpctx, u8, 0);
|
bits = tal_arr(tmpctx, u8, 0);
|
||||||
for (size_t i = 0; i < 100; i += 3)
|
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++)
|
for (size_t i = 0; i < 100; i++)
|
||||||
assert(test_bit(bits, i / 8, i % 8) == ((i % 3) == 0));
|
assert(test_bit(bits, i / 8, i % 8) == ((i % 3) == 0));
|
||||||
|
|
||||||
for (size_t i = 0; i < 100; i++)
|
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 */
|
/* Simple test: single byte */
|
||||||
bits = tal_arr(tmpctx, u8, 1);
|
bits = tal_arr(tmpctx, u8, 1);
|
||||||
@@ -75,18 +75,18 @@ int main(void)
|
|||||||
/* We can add random odd features, no problem. */
|
/* We can add random odd features, no problem. */
|
||||||
for (size_t i = 1; i < 16; i += 2) {
|
for (size_t i = 1; i < 16; i += 2) {
|
||||||
bits = tal_dup_arr(tmpctx, u8, lf, tal_count(lf), 0);
|
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));
|
assert(features_supported(gf, bits));
|
||||||
|
|
||||||
bits = tal_dup_arr(tmpctx, u8, gf, tal_count(gf), 0);
|
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));
|
assert(features_supported(bits, lf));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We can't add random even features. */
|
/* We can't add random even features. */
|
||||||
for (size_t i = 0; i < 16; i += 2) {
|
for (size_t i = 0; i < 16; i += 2) {
|
||||||
bits = tal_dup_arr(tmpctx, u8, lf, tal_count(lf), 0);
|
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 */
|
/* Special case for missing compulsory feature */
|
||||||
if (i == 2) {
|
if (i == 2) {
|
||||||
@@ -98,7 +98,7 @@ int main(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
bits = tal_dup_arr(tmpctx, u8, gf, tal_count(gf), 0);
|
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)
|
assert(features_supported(bits, lf)
|
||||||
== feature_supported(i, our_globalfeatures,
|
== feature_supported(i, our_globalfeatures,
|
||||||
ARRAY_SIZE(our_globalfeatures)));
|
ARRAY_SIZE(our_globalfeatures)));
|
||||||
|
|||||||
Reference in New Issue
Block a user