features: don't assume we'll always only advertize optional features.

Generalize things a bit so OPTIONAL_FEATURE() and COMPULSORY_FEATURE()
work with either odd or even features, then explicitly use OPTIONAL_FEATURE
in our internal feature array.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2019-08-28 13:39:04 +09:30 committed by Christian Decker
parent a38131f349
commit 17edcfa24d
2 changed files with 25 additions and 15 deletions

View File

@ -4,12 +4,12 @@
#include <wire/peer_wire.h>
static const u32 our_localfeatures[] = {
LOCAL_DATA_LOSS_PROTECT,
LOCAL_INITIAL_ROUTING_SYNC,
LOCAL_UPFRONT_SHUTDOWN_SCRIPT,
LOCAL_GOSSIP_QUERIES,
OPTIONAL_FEATURE(LOCAL_DATA_LOSS_PROTECT),
OPTIONAL_FEATURE(LOCAL_INITIAL_ROUTING_SYNC),
OPTIONAL_FEATURE(LOCAL_UPFRONT_SHUTDOWN_SCRIPT),
OPTIONAL_FEATURE(LOCAL_GOSSIP_QUERIES),
#if EXPERIMENTAL_FEATURES
LOCAL_EXTENDED_GOSSIP_QUERIES
OPTIONAL_FEATURE(LOCAL_GOSSIP_QUERIES_EX)
#endif
};
@ -40,13 +40,12 @@ static bool test_bit(const u8 *features, size_t byte, unsigned int bit)
return features[tal_count(features) - 1 - byte] & (1 << (bit % 8));
}
/* We don't insist on anything, it's all optional. */
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, OPTIONAL_FEATURE(arr[i]));
set_bit(&f, arr[i]);
return f;
}
@ -74,9 +73,7 @@ static bool feature_set(const u8 *features, size_t bit)
bool feature_offered(const u8 *features, size_t f)
{
assert(f % 2 == 0);
return feature_set(features, f)
return feature_set(features, COMPULSORY_FEATURE(f))
|| feature_set(features, OPTIONAL_FEATURE(f));
}
@ -85,7 +82,8 @@ static bool feature_supported(int feature_bit,
size_t num_supported)
{
for (size_t i = 0; i < num_supported; i++) {
if (supported[i] == feature_bit)
if (OPTIONAL_FEATURE(supported[i])
== OPTIONAL_FEATURE(feature_bit))
return true;
}
return false;

View File

@ -18,8 +18,16 @@ bool feature_offered(const u8 *features, size_t f);
bool local_feature_negotiated(const u8 *lfeatures, size_t f);
bool global_feature_negotiated(const u8 *gfeatures, size_t f);
#define COMPULSORY_FEATURE(x) (x)
#define OPTIONAL_FEATURE(x) ((x)+1)
/* BOLT #9:
*
* Flags are numbered from the least-significant bit, at bit 0 (i.e. 0x1,
* an _even_ bit). They are generally assigned in pairs so that features
* can be introduced as optional (_odd_ bits) and later upgraded to be compulsory
* (_even_ bits), which will be refused by outdated nodes:
* see [BOLT #1: The `init` Message](01-messaging.md#the-init-message).
*/
#define COMPULSORY_FEATURE(x) ((x) & 0xFFFFFFFE)
#define OPTIONAL_FEATURE(x) ((x) | 1)
/* BOLT #9:
*
@ -35,8 +43,12 @@ bool global_feature_negotiated(const u8 *gfeatures, size_t f);
#define LOCAL_INITIAL_ROUTING_SYNC 2
#define LOCAL_UPFRONT_SHUTDOWN_SCRIPT 4
#define LOCAL_GOSSIP_QUERIES 6
/* FIXME: Get bolt reference! */
#define LOCAL_EXTENDED_GOSSIP_QUERIES 10
/* BOLT-927c96daab2338b716708a57cd75c84a2d169e0e #9:
* | Bits | Name |...
* | 10/11 | `gossip_queries_ex` |...
*/
#define LOCAL_GOSSIP_QUERIES_EX 10
/* BOLT #9:
*