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