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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-10-26 16:31:26 +10:30
parent aab91557f3
commit 171006d17c
4 changed files with 16 additions and 29 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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;