From 171006d17cc3b8d273960590d6b1d38f9950c280 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 26 Oct 2018 16:31:26 +1030 Subject: [PATCH] lightnignd: peer addr is never NULL, adjust new_peer. We always have an addr entry in the db (though it may be an ephemeral socket the peer connected in from). We don't have to handle a NULL address. While we're there, simplify new_peer not to take the features args; the caller who cares immediately updates the features anyway. Signed-off-by: Rusty Russell --- lightningd/peer_control.c | 20 +++++--------------- lightningd/peer_control.h | 4 +--- wallet/test/run-wallet.c | 5 ++++- wallet/wallet.c | 16 ++++++---------- 4 files changed, 16 insertions(+), 29 deletions(-) diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 4cc9f7595..2ca9a36b2 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -93,9 +93,7 @@ static void peer_update_features(struct peer *peer, struct peer *new_peer(struct lightningd *ld, u64 dbid, const struct pubkey *id, - const struct wireaddr_internal *addr, - const u8 *globalfeatures TAKES, - const u8 *localfeatures TAKES) + const struct wireaddr_internal *addr) { /* We are owned by our channels, and freed manually by destroy_channel */ struct peer *peer = tal(NULL, struct peer); @@ -104,15 +102,8 @@ struct peer *new_peer(struct lightningd *ld, u64 dbid, peer->dbid = dbid; peer->id = *id; peer->uncommitted_channel = NULL; - /* FIXME: This is always set, right? */ - if (addr) - peer->addr = *addr; - else { - peer->addr.itype = ADDR_INTERNAL_WIREADDR; - peer->addr.u.wireaddr.type = ADDR_TYPE_PADDING; - } + peer->addr = *addr; peer->globalfeatures = peer->localfeatures = NULL; - peer_update_features(peer, globalfeatures, localfeatures); list_head_init(&peer->channels); peer->direction = get_channel_direction(&peer->ld->id, &peer->id); @@ -440,10 +431,9 @@ void peer_connected(struct lightningd *ld, const u8 *msg, * subdaemon. Otherwise, we'll hand to openingd to wait there. */ peer = peer_by_id(ld, &id); if (!peer) - peer = new_peer(ld, 0, &id, &addr, - globalfeatures, localfeatures); - else - peer_update_features(peer, globalfeatures, localfeatures); + peer = new_peer(ld, 0, &id, &addr); + + peer_update_features(peer, globalfeatures, localfeatures); /* Can't be opening, since we wouldn't have sent peer_disconnected. */ assert(!peer->uncommitted_channel); diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index bf4533e6c..5dc06d62d 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -57,9 +57,7 @@ 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 u8 *globalfeatures TAKES, - const u8 *localfeatures TAKES); + const struct wireaddr_internal *addr); /* Last one out deletes peer. Also removes from db. */ void maybe_delete_peer(struct peer *peer); diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 354999449..d21bd838a 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -796,6 +796,7 @@ static bool test_channel_crud(struct lightningd *ld, const tal_t *ctx) { struct wallet *w = create_test_wallet(ld, ctx); struct channel c1, *c2 = tal(w, struct channel); + struct wireaddr_internal addr; struct peer *p; struct channel_info *ci = &c1.channel_info; struct bitcoin_txid *hash = tal(w, struct bitcoin_txid); @@ -815,8 +816,10 @@ static bool test_channel_crud(struct lightningd *ld, const tal_t *ctx) ci->feerate_per_kw[LOCAL] = ci->feerate_per_kw[REMOTE] = 31337; mempat(scriptpubkey, tal_count(scriptpubkey)); c1.first_blocknum = 1; + parse_wireaddr_internal("localhost:1234", &addr, 0, false, false, false, + NULL); c1.final_key_idx = 1337; - p = new_peer(ld, 0, &pk, NULL, NULL, NULL); + p = new_peer(ld, 0, &pk, &addr); c1.peer = p; c1.dbid = wallet_get_channel_dbid(w); c1.state = CHANNELD_NORMAL; diff --git a/wallet/wallet.c b/wallet/wallet.c index 0048b4766..d9bce0752 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -514,7 +514,7 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid) const unsigned char *addrstr; struct peer *peer; struct pubkey id; - struct wireaddr_internal *addrp, addr; + struct wireaddr_internal addr; sqlite3_stmt *stmt = db_query(w->db, @@ -529,17 +529,13 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid) return NULL; } addrstr = sqlite3_column_text(stmt, 2); - if (addrstr) { - addrp = &addr; - if (!parse_wireaddr_internal((const char*)addrstr, addrp, DEFAULT_PORT, false, false, true, NULL)) { - db_stmt_done(stmt); - return NULL; - } - } else - addrp = NULL; + if (!parse_wireaddr_internal((const char*)addrstr, &addr, DEFAULT_PORT, false, false, true, NULL)) { + db_stmt_done(stmt); + return NULL; + } peer = new_peer(w->ld, sqlite3_column_int64(stmt, 0), - &id, addrp, NULL, NULL); + &id, &addr); db_stmt_done(stmt); return peer;