From 36316957e3f150f678df76d15a1de5c71ed379a8 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 3 Jan 2018 15:56:44 +1030 Subject: [PATCH] lightningd: set parent correctly for loaded peers. The current code makes the channel the parent, which is a cycle. Signed-off-by: Rusty Russell --- lightningd/lightningd.c | 2 +- lightningd/test/run-find_my_path.c | 3 ++- wallet/test/run-wallet.c | 2 +- wallet/wallet.c | 8 ++++---- wallet/wallet.h | 4 +++- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index d678e8845..424e9e18f 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -275,7 +275,7 @@ int main(int argc, char *argv[]) gossip_init(ld); /* Load peers from database */ - wallet_channels_load_active(ld->wallet, &ld->peers); + wallet_channels_load_active(ld, ld->wallet, &ld->peers); /* TODO(cdecker) Move this into common location for initialization */ struct peer *peer; diff --git a/lightningd/test/run-find_my_path.c b/lightningd/test/run-find_my_path.c index d1ead9a37..7630e1685 100644 --- a/lightningd/test/run-find_my_path.c +++ b/lightningd/test/run-find_my_path.c @@ -97,7 +97,8 @@ const char *version(void) u32 wallet_channels_first_blocknum(struct wallet *w UNNEEDED) { fprintf(stderr, "wallet_channels_first_blocknum called!\n"); abort(); } /* Generated stub for wallet_channels_load_active */ -bool wallet_channels_load_active(struct wallet *w UNNEEDED, struct list_head *peers UNNEEDED) +bool wallet_channels_load_active(const tal_t *ctx UNNEEDED, + struct wallet *w UNNEEDED, struct list_head *peers UNNEEDED) { fprintf(stderr, "wallet_channels_load_active called!\n"); abort(); } /* Generated stub for wallet_htlcs_load_for_channel */ bool wallet_htlcs_load_for_channel(struct wallet *wallet UNNEEDED, diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 9d2f7cd68..09d4d1998 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -263,7 +263,7 @@ static struct wallet_channel *wallet_channel_load(struct wallet *w, const u64 id list_head_init(&peers); /* We expect only one peer, but reuse same code */ - if (!wallet_channels_load_active(w, &peers)) + if (!wallet_channels_load_active(w, w, &peers)) return NULL; peer = list_top(&peers, struct peer, list); CHECK(peer); diff --git a/wallet/wallet.c b/wallet/wallet.c index c54629a4a..d98cb239c 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -441,7 +441,7 @@ bool wallet_peer_by_nodeid(struct wallet *w, const struct pubkey *nodeid, * * Returns true on success. */ -static bool wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt, +static bool wallet_stmt2channel(const tal_t *ctx, struct wallet *w, sqlite3_stmt *stmt, struct wallet_channel *chan) { bool ok = true; @@ -449,7 +449,7 @@ static bool wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt, u64 remote_config_id; if (!chan->peer) { - chan->peer = talz(chan, struct peer); + chan->peer = talz(ctx, struct peer); } chan->id = sqlite3_column_int64(stmt, 0); chan->peer->dbid = sqlite3_column_int64(stmt, 1); @@ -574,7 +574,7 @@ static const char *channel_fields = "last_sent_commit_state, last_sent_commit_id, " "last_tx, last_sig"; -bool wallet_channels_load_active(struct wallet *w, struct list_head *peers) +bool wallet_channels_load_active(const tal_t *ctx, struct wallet *w, struct list_head *peers) { bool ok = true; /* Channels are active if they have reached at least the @@ -586,7 +586,7 @@ bool wallet_channels_load_active(struct wallet *w, struct list_head *peers) int count = 0; while (ok && stmt && sqlite3_step(stmt) == SQLITE_ROW) { struct wallet_channel *c = talz(w, struct wallet_channel); - ok &= wallet_stmt2channel(w, stmt, c); + ok &= wallet_stmt2channel(ctx, w, stmt, c); list_add(peers, &c->peer->list); /* Peer owns channel. FIXME delete from db if peer freed! */ tal_steal(c->peer, c); diff --git a/wallet/wallet.h b/wallet/wallet.h index bb157ebe5..0fc382560 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -237,13 +237,15 @@ bool wallet_peer_by_nodeid(struct wallet *w, const struct pubkey *nodeid, /** * wlalet_channels_load_active -- Load persisted active channels into the peers * + * @ctx: context to allocate peers from * @w: wallet to load from * @peers: list_head to load channels/peers into * * Be sure to call this only once on startup since it'll append peers * loaded from the database to the list without checking. */ -bool wallet_channels_load_active(struct wallet *w, struct list_head *peers); +bool wallet_channels_load_active(const tal_t *ctx, + struct wallet *w, struct list_head *peers); /** * wallet_channels_first_blocknum - get first block we're interested in.