From 36b66871a25e65798ac4f6d7d21983c033d2d10b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 13 Oct 2021 14:13:13 +1030 Subject: [PATCH] db: add min/max commitnum fields to channel_htlcs. And initialize max to current height max when htlcs are already dead. Turns out (thanks CI!) that MAX() of multiple columns is GREATEST() in Postgres. That's clearer (MAX is used elsewhere for single columns), so translate on the sqlite3 side. Signed-off-by: Rusty Russell --- devtools/sql-rewrite.py | 2 ++ wallet/db.c | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/devtools/sql-rewrite.py b/devtools/sql-rewrite.py index f49a23871..53a861992 100755 --- a/devtools/sql-rewrite.py +++ b/devtools/sql-rewrite.py @@ -43,6 +43,8 @@ class Sqlite3Rewriter(Rewriter): r'INSERT INTO[ \t]+(.*)[ \t]+ON CONFLICT.*DO NOTHING;': 'INSERT OR IGNORE INTO \\1;', # Rewrite "decode('abcd', 'hex')" to become "x'abcd'" r'decode\((.*),\s*[\'\"]hex[\'\"]\)': 'x\\1', + # GREATEST() of multiple columns is simple MAX in sqlite3. + r'GREATEST\(([^)]*)\)': "MAX(\\1)", } return self.rewrite_types(query, typemapping) diff --git a/wallet/db.c b/wallet/db.c index e08c62d8a..54f78bbdd 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -835,6 +835,15 @@ static struct migration dbmigrations[] = { /* HTLCs also need to carry the groupid around so we can * selectively update them. */ {SQL("ALTER TABLE channel_htlcs ADD groupid BIGINT;"), NULL}, + {SQL("ALTER TABLE channel_htlcs ADD COLUMN" + " min_commit_num BIGINT default 0;"), NULL}, + {SQL("ALTER TABLE channel_htlcs ADD COLUMN" + " max_commit_num BIGINT default NULL;"), NULL}, + /* Set max_commit_num for dead (RCVD_REMOVE_ACK_REVOCATION or SENT_REMOVE_ACK_REVOCATION) HTLCs based on latest indexes */ + {SQL("UPDATE channel_htlcs SET max_commit_num =" + " (SELECT GREATEST(next_index_local, next_index_remote)" + " FROM channels WHERE id=channel_id)" + " WHERE (hstate=9 OR hstate=19);"), NULL}, }; /* Leak tracking. */