Skip to content

Commit

Permalink
lightningd: keep features arrays for connected peers.
Browse files Browse the repository at this point in the history
As a side-effect, we only print them for connected peers (which avoids
an O(n^2) traversal).

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jul 25, 2018
1 parent b5fcd54 commit 722dd05
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 30 deletions.
15 changes: 9 additions & 6 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,15 +596,16 @@ static struct uncommitted_channel *
new_uncommitted_channel(struct lightningd *ld,
struct funding_channel *fc,
const struct pubkey *peer_id,
const struct wireaddr_internal *addr)
const struct wireaddr_internal *addr,
const u8 *gfeatures, const u8 *lfeatures)
{
struct uncommitted_channel *uc = tal(ld, struct uncommitted_channel);
char *idname;

/* We make a new peer if necessary. */
uc->peer = peer_by_id(ld, peer_id);
if (!uc->peer)
uc->peer = new_peer(ld, 0, peer_id, addr);
uc->peer = new_peer(ld, 0, peer_id, addr, gfeatures, lfeatures);

if (uc->peer->uncommitted_channel)
return tal_free(uc);
Expand Down Expand Up @@ -686,7 +687,7 @@ u8 *peer_accept_channel(const tal_t *ctx,
const struct pubkey *peer_id,
const struct wireaddr_internal *addr,
const struct crypto_state *cs,
const u8 *gfeatures UNUSED, const u8 *lfeatures UNUSED,
const u8 *gfeatures, const u8 *lfeatures,
int peer_fd, int gossip_fd,
const struct channel_id *channel_id,
const u8 *open_msg)
Expand All @@ -700,7 +701,8 @@ u8 *peer_accept_channel(const tal_t *ctx,
assert(fromwire_peektype(open_msg) == WIRE_OPEN_CHANNEL);

/* Fails if there's already one */
uc = new_uncommitted_channel(ld, NULL, peer_id, addr);
uc = new_uncommitted_channel(ld, NULL, peer_id, addr,
gfeatures, lfeatures);
if (!uc)
return towire_errorfmt(ctx, channel_id,
"Multiple channels unsupported");
Expand Down Expand Up @@ -766,7 +768,7 @@ static void peer_offer_channel(struct lightningd *ld,
struct funding_channel *fc,
const struct wireaddr_internal *addr,
const struct crypto_state *cs,
const u8 *gfeatures UNUSED, const u8 *lfeatures UNUSED,
const u8 *gfeatures, const u8 *lfeatures,
int peer_fd, int gossip_fd)
{
u8 *msg;
Expand All @@ -778,7 +780,8 @@ static void peer_offer_channel(struct lightningd *ld,
list_del_from(&ld->fundchannels, &fc->list);
tal_del_destructor(fc, remove_funding_channel_from_list);

fc->uc = new_uncommitted_channel(ld, fc, &fc->peerid, addr);
fc->uc = new_uncommitted_channel(ld, fc, &fc->peerid, addr,
gfeatures, lfeatures);

/* We asked to release this peer, but another raced in? Corner case,
* close this is easiest. */
Expand Down
43 changes: 22 additions & 21 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ static void copy_to_parent_log(const char *prefix,

struct peer *new_peer(struct lightningd *ld, u64 dbid,
const struct pubkey *id,
const struct wireaddr_internal *addr)
const struct wireaddr_internal *addr,
const u8 *gfeatures, const u8 *lfeatures)
{
/* We are owned by our channels, and freed manually by destroy_channel */
struct peer *peer = tal(NULL, struct peer);
Expand All @@ -96,6 +97,10 @@ struct peer *new_peer(struct lightningd *ld, u64 dbid,
peer->addr.itype = ADDR_INTERNAL_WIREADDR;
peer->addr.u.wireaddr.type = ADDR_TYPE_PADDING;
}
peer->global_features = tal_dup_arr(peer, u8,
gfeatures, tal_count(gfeatures), 0);
peer->local_features = tal_dup_arr(peer, u8,
lfeatures, tal_count(lfeatures), 0);
list_head_init(&peer->channels);
peer->direction = get_channel_direction(&peer->ld->id, &peer->id);

Expand Down Expand Up @@ -704,18 +709,6 @@ struct getpeers_args {
struct pubkey *specific_id;
};

static void json_add_features(struct json_result *response,
const struct peer_features *pf)
{
json_add_hex(response, "global_features",
pf->global_features,
tal_len(pf->global_features));

json_add_hex(response, "local_features",
pf->local_features,
tal_len(pf->local_features));
}

static void connectd_getpeers_complete(struct subd *connectd, const u8 *msg,
const int *fds UNUSED,
struct getpeers_args *gpa)
Expand Down Expand Up @@ -756,6 +749,9 @@ static void connectd_getpeers_complete(struct subd *connectd, const u8 *msg,
}
json_add_bool(response, "connected", connected);

/* If it's not connected, features are unreliable: we don't
* store them in the database, and they would only reflect
* their features *last* time they connected. */
if (connected) {
json_array_start(response, "netaddr");
if (p->addr.itype != ADDR_INTERNAL_WIREADDR
Expand All @@ -765,14 +761,13 @@ static void connectd_getpeers_complete(struct subd *connectd, const u8 *msg,
struct wireaddr_internal,
&p->addr));
json_array_end(response);
}
json_add_hex(response, "global_features",
p->global_features,
tal_len(p->global_features));

/* Search connectd reply for this ID, to add extra info. */
for (size_t i = 0; i < tal_count(ids); i++) {
if (pubkey_eq(&ids[i], &p->id)) {
json_add_features(response, pf[i]);
break;
}
json_add_hex(response, "local_features",
p->local_features,
tal_len(p->local_features));
}

json_array_start(response, "channels");
Expand Down Expand Up @@ -905,7 +900,13 @@ static void connectd_getpeers_complete(struct subd *connectd, const u8 *msg,
/* Fake state. */
json_add_string(response, "state", "GOSSIPING");
json_add_pubkey(response, "id", ids+i);
json_add_features(response, pf[i]);
json_add_hex(response, "global_features",
pf[i]->global_features,
tal_len(pf[i]->global_features));

json_add_hex(response, "local_features",
pf[i]->local_features,
tal_len(pf[i]->local_features));
json_array_start(response, "netaddr");
if (addrs[i].itype != ADDR_INTERNAL_WIREADDR
|| addrs[i].u.wireaddr.type != ADDR_TYPE_PADDING)
Expand Down
6 changes: 5 additions & 1 deletion lightningd/peer_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ struct peer {
/* Where we connected to, or it connected from. */
struct wireaddr_internal addr;

/* We keep a copy of their feature bits */
const u8 *local_features, *global_features;

/* If we open a channel our direction will be this */
u8 direction;

Expand All @@ -54,7 +57,8 @@ struct peer *find_peer_by_dbid(struct lightningd *ld, u64 dbid);

struct peer *new_peer(struct lightningd *ld, u64 dbid,
const struct pubkey *id,
const struct wireaddr_internal *addr);
const struct wireaddr_internal *addr,
const u8 *gfeatures, const u8 *lfeatures);

/* Also removes from db. */
void delete_peer(struct peer *peer);
Expand Down
2 changes: 1 addition & 1 deletion wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ static bool test_channel_crud(struct lightningd *ld, const tal_t *ctx)
mempat(scriptpubkey, tal_len(scriptpubkey));
c1.first_blocknum = 1;
c1.final_key_idx = 1337;
p = new_peer(ld, 0, &pk, NULL);
p = new_peer(ld, 0, &pk, NULL, NULL, NULL);
c1.peer = p;
c1.dbid = wallet_get_channel_dbid(w);
c1.state = CHANNELD_NORMAL;
Expand Down
2 changes: 1 addition & 1 deletion wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid)
addrp = NULL;

peer = new_peer(w->ld, sqlite3_column_int64(stmt, 0),
&id, addrp);
&id, addrp, NULL, NULL);
db_stmt_done(stmt);

return peer;
Expand Down

0 comments on commit 722dd05

Please sign in to comment.