From 38a313af0d3a1fe581509a8fd2480c7e5653e95b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 12 Feb 2018 20:40:37 +1030 Subject: [PATCH] wallet: delete channels in state OPENINGD. Both when we forget about an opening peer, and at startup. We're going to be relying on this, and the next patch, as we refactor peer/channel handling to mirror the db. Signed-off-by: Rusty Russell --- lightningd/peer_control.c | 4 +--- wallet/wallet.c | 9 ++++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index e71f6275c..561070f54 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -181,6 +181,7 @@ static void free_peer(struct peer *peer, const char *why) command_fail(peer->opening_cmd, "%s", why); peer->opening_cmd = NULL; } + wallet_channel_delete(peer->ld->wallet, peer->channel->id); tal_free(peer); } @@ -1268,7 +1269,6 @@ static void handle_irrevocably_resolved(struct peer *peer, const u8 *msg) free_htlcs(peer->ld, peer); log_info(peer->log, "onchaind complete, forgetting peer"); - wallet_channel_delete(peer->ld->wallet, peer->channel->id); /* This will also free onchaind. */ free_peer(peer, "onchaind complete, forgetting peer"); @@ -2968,8 +2968,6 @@ static void process_dev_forget_channel(struct bitcoind *bitcoind UNUSED, json_add_txid(response, "funding_txid", forget->peer->funding_txid); json_object_end(response); - if (peer_persists(forget->peer)) - wallet_channel_delete(forget->cmd->ld->wallet, forget->peer->channel->id); free_peer(forget->peer, "dev-forget-channel called"); command_success(forget->cmd, response); diff --git a/wallet/wallet.c b/wallet/wallet.c index e942c944c..4c16c56d3 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -626,9 +626,16 @@ static const char *channel_fields = bool wallet_channels_load_active(const tal_t *ctx, struct wallet *w, struct list_head *peers) { bool ok = true; + sqlite3_stmt *stmt; + + /* Get rid of OPENINGD entries; they don't last across reconnects */ + stmt = db_prepare(w->db, "DELETE FROM channels WHERE state=?"); + sqlite3_bind_int64(stmt, 1, OPENINGD); + db_exec_prepared(w->db, stmt); + /* Channels are active if they have reached at least the * opening state and they are not marked as complete */ - sqlite3_stmt *stmt = db_query( + stmt = db_query( __func__, w->db, "SELECT %s FROM channels WHERE state > %d AND state != %d;", channel_fields, OPENINGD, CLOSINGD_COMPLETE);