Browse Source

features: Move feature-handling code to a common/features.c source.

ppa-0.6.1
ZmnSCPxj 7 years ago
committed by Christian Decker
parent
commit
83e76e3ac3
  1. 1
      common/Makefile
  2. 62
      common/features.c
  3. 14
      common/features.h
  4. 1
      lightningd/Makefile
  5. 1
      lightningd/gossip_control.c
  6. 69
      lightningd/peer_control.c
  7. 7
      lightningd/peer_control.h

1
common/Makefile

@ -11,6 +11,7 @@ COMMON_SRC := \
common/daemon_conn.c \
common/derive_basepoints.c \
common/dev_disconnect.c \
common/features.c \
common/funding_tx.c \
common/hash_u5.c \
common/htlc_state.c \

62
common/features.c

@ -0,0 +1,62 @@
#include "features.h"
#include <wire/peer_wire.h>
static const u8 supported_local_features[]
= {LOCALFEATURES_INITIAL_ROUTING_SYNC};
static const u8 supported_global_features[]
= {};
u8 *get_supported_global_features(const tal_t *ctx)
{
return tal_dup_arr(ctx, u8, supported_global_features,
sizeof(supported_global_features), 0);
}
u8 *get_supported_local_features(const tal_t *ctx)
{
return tal_dup_arr(ctx, u8, supported_local_features,
sizeof(supported_local_features), 0);
}
/**
* requires_unsupported_features - Check if we support what's being asked
*
* Given the features vector that the remote connection is expecting
* from us, we check to see if we support all even bit features, i.e.,
* the required features. We do so by subtracting our own features in
* the provided positions and see if even bits remain.
*
* @bitmap: the features bitmap the peer is asking for
* @supportmap: what do we support
* @smlen: how long is our supportmap
*/
static bool requires_unsupported_features(const u8 *bitmap,
const u8 *supportmap,
size_t smlen)
{
size_t len = tal_count(bitmap);
u8 support;
for (size_t i=0; i<len; i++) {
/* Find matching bitmap byte in supportmap, 0x00 if none */
if (len > smlen) {
support = 0x00;
} else {
support = supportmap[smlen-1];
}
/* Cancel out supported bits, check for even bits */
if ((~support & bitmap[i]) & 0x55)
return true;
}
return false;
}
bool unsupported_features(const u8 *gfeatures, const u8 *lfeatures)
{
return requires_unsupported_features(gfeatures,
supported_global_features,
sizeof(supported_global_features))
|| requires_unsupported_features(lfeatures,
supported_local_features,
sizeof(supported_local_features));
}

14
common/features.h

@ -0,0 +1,14 @@
#ifndef LIGHTNING_COMMON_FEATURES_H
#define LIGHTNING_COMMON_FEATURES_H
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
/* Returns true if these contain any unsupported features. */
bool unsupported_features(const u8 *gfeatures, const u8 *lfeatures);
/* For sending our features: tal_len() returns length. */
u8 *get_supported_global_features(const tal_t *ctx);
u8 *get_supported_local_features(const tal_t *ctx);
#endif /* LIGHTNING_COMMON_FEATURES_H */

1
lightningd/Makefile

@ -21,6 +21,7 @@ LIGHTNINGD_COMMON_OBJS := \
common/configdir.o \
common/crypto_state.o \
common/derive_basepoints.o \
common/features.o \
common/funding_tx.o \
common/hash_u5.o \
common/htlc_state.o \

1
lightningd/gossip_control.c

@ -8,6 +8,7 @@
#include <ccan/fdpass/fdpass.h>
#include <ccan/take/take.h>
#include <ccan/tal/str/str.h>
#include <common/features.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <errno.h>

69
lightningd/peer_control.c

@ -14,6 +14,7 @@
#include <closingd/gen_closing_wire.h>
#include <common/close_tx.h>
#include <common/dev_disconnect.h>
#include <common/features.h>
#include <common/funding_tx.h>
#include <common/initial_commit_tx.h>
#include <common/key_derive.h>
@ -44,11 +45,6 @@
#include <wire/peer_wire.h>
#include <wire/wire_sync.h>
static const u8 supported_local_features[]
= {LOCALFEATURES_INITIAL_ROUTING_SYNC};
static const u8 supported_global_features[]
= {};
struct connect {
struct list_node list;
struct pubkey id;
@ -126,18 +122,6 @@ static void destroy_peer(struct peer *peer)
list_del_from(&peer->ld->peers, &peer->list);
}
u8 *get_supported_global_features(const tal_t *ctx)
{
return tal_dup_arr(ctx, u8, supported_global_features,
sizeof(supported_global_features), 0);
}
u8 *get_supported_local_features(const tal_t *ctx)
{
return tal_dup_arr(ctx, u8, supported_local_features,
sizeof(supported_local_features), 0);
}
static void sign_last_tx(struct peer *peer)
{
const tal_t *tmpctx = tal_tmpctx(peer);
@ -402,49 +386,6 @@ static struct peer *new_peer(struct lightningd *ld,
return peer;
}
/**
* requires_unsupported_features - Check if we support what's being asked
*
* Given the features vector that the remote connection is expecting
* from us, we check to see if we support all even bit features, i.e.,
* the required features. We do so by subtracting our own features in
* the provided positions and see if even bits remain.
*
* @bitmap: the features bitmap the peer is asking for
* @supportmap: what do we support
* @smlen: how long is our supportmap
*/
static bool requires_unsupported_features(const u8 *bitmap,
const u8 *supportmap,
size_t smlen)
{
size_t len = tal_count(bitmap);
u8 support;
for (size_t i=0; i<len; i++) {
/* Find matching bitmap byte in supportmap, 0x00 if none */
if (len > smlen) {
support = 0x00;
} else {
support = supportmap[smlen-1];
}
/* Cancel out supported bits, check for even bits */
if ((~support & bitmap[i]) & 0x55)
return true;
}
return false;
}
bool unsupported_features(const u8 *gfeatures, const u8 *lfeatures)
{
return requires_unsupported_features(gfeatures,
supported_global_features,
sizeof(supported_global_features))
|| requires_unsupported_features(lfeatures,
supported_local_features,
sizeof(supported_local_features));
}
/**
* peer_channel_new -- Instantiate a new channel for the given peer and save it
*
@ -524,6 +465,8 @@ void peer_connected(struct lightningd *ld, const u8 *msg,
struct crypto_state cs;
u8 *gfeatures, *lfeatures;
u8 *error;
u8 *supported_global_features;
u8 *supported_local_features;
struct peer *peer;
struct wireaddr addr;
u64 gossip_index;
@ -539,15 +482,17 @@ void peer_connected(struct lightningd *ld, const u8 *msg,
type_to_string(msg, struct pubkey, &id),
tal_hex(msg, gfeatures),
tal_hex(msg, lfeatures));
supported_global_features = get_supported_global_features(msg);
supported_local_features = get_supported_local_features(msg);
error = towire_errorfmt(msg, NULL,
"We only support globalfeatures %s"
" and localfeatures %s",
tal_hexstr(msg,
supported_global_features,
sizeof(supported_global_features)),
tal_len(supported_global_features)),
tal_hexstr(msg,
supported_local_features,
sizeof(supported_local_features)));
tal_len(supported_local_features)));
goto send_error;
}

7
lightningd/peer_control.h

@ -198,13 +198,6 @@ void peer_sent_nongossip(struct lightningd *ld,
*/
void populate_peer(struct lightningd *ld, struct peer *peer);
/* Returns true if these contain any unsupported features. */
bool unsupported_features(const u8 *gfeatures, const u8 *lfeatures);
/* For sending our features: tal_len() returns length. */
u8 *get_supported_global_features(const tal_t *ctx);
u8 *get_supported_local_features(const tal_t *ctx);
/* Could be configurable. */
#define OUR_CHANNEL_FLAGS CHANNEL_FLAGS_ANNOUNCE_CHANNEL

Loading…
Cancel
Save