Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix out-of-sync sequence for psql and wrap destructor in optional DB transaction #4894

Merged
merged 3 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions devtools/sql-rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def rewrite(self, queries):

class Sqlite3Rewriter(Rewriter):
def rewrite_single(self, query):
# Replace DB specific queries with a no-op
if "/*PSQL*/" in query:
return "UPDATE vars SET intval=1 WHERE name='doesnotexist'" # Return a no-op

typemapping = {
r'BIGINT': 'INTEGER',
r'BIGINTEGER': 'INTEGER',
Expand All @@ -51,6 +55,10 @@ def rewrite_single(self, query):

class PostgresRewriter(Rewriter):
def rewrite_single(self, q):
# Replace DB specific queries with a no-op
if "/*SQLITE*/" in q:
return "UPDATE vars SET intval=1 WHERE name='doesnotexist'" # Return a no-op

# Let's start by replacing any eventual '?' placeholders
q2 = ""
count = 1
Expand Down
12 changes: 12 additions & 0 deletions lightningd/peer_htlcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,12 @@ static void handle_localpay(struct htlc_in *hin,
*/
static void destroy_hout_subd_died(struct htlc_out *hout)
{
struct db *db = hout->key.channel->peer->ld->wallet->db;
/* Under some circumstances we may need to start a DB
* transaction and commit it here again. This is the case when
* we're getting called from the destructor chain. */
bool have_tx =
db_in_transaction(db);
log_debug(hout->key.channel->log,
"Failing HTLC %"PRIu64" due to peer death",
hout->key.id);
Expand All @@ -531,8 +537,14 @@ static void destroy_hout_subd_died(struct htlc_out *hout)
assert(hout->hstate == SENT_ADD_HTLC);
hout->hstate = RCVD_REMOVE_HTLC;

if (!have_tx)
db_begin_transaction(db);

fail_out_htlc(hout, "Outgoing subdaemon died",
take(towire_temporary_channel_failure(NULL, NULL)));

if (!have_tx)
db_commit_transaction(db);
}

/* This is where channeld gives us the HTLC id, and also reports if it
Expand Down
3 changes: 3 additions & 0 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,9 @@ static struct migration dbmigrations[] = {
/* We default to 50k sats */
{SQL("ALTER TABLE channel_configs ADD max_dust_htlc_exposure_msat BIGINT DEFAULT 50000000"), NULL},
{SQL("ALTER TABLE channel_htlcs ADD fail_immediate INTEGER DEFAULT 0"), NULL},

/* Issue #4887: reset the payments.id sequence after the migration above. Since this is a SELECT statement that would otherwise fail, make it an INSERT into the `vars` table.*/
{SQL("/*PSQL*/INSERT INTO vars (name, intval) VALUES ('payment_id_reset', setval(pg_get_serial_sequence('payments', 'id'), COALESCE((SELECT MAX(id)+1 FROM payments), 1)))"), NULL},
};

/* Leak tracking. */
Expand Down