From dfb963e2494bb1003b268193ad79129112739f0d Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 24 Nov 2022 17:55:04 +0100 Subject: [PATCH] db: Backfill missing HTLC IDs in the forwards table We have a primary key that is spanning the `in_channel_id` and the `in_htcl_id`. The latter gets set to NULL when the HTLC and channel gets deleted, so we coalesce with a random large number that is unlikely to collide for the primary key. --- devtools/sql-rewrite.py | 1 + tests/test_db.py | 3 +++ wallet/db.c | 5 ++++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/devtools/sql-rewrite.py b/devtools/sql-rewrite.py index 7ae209403c3b..03c358a643c7 100755 --- a/devtools/sql-rewrite.py +++ b/devtools/sql-rewrite.py @@ -75,6 +75,7 @@ def rewrite_single(self, q): typemapping = { r'BLOB': 'BYTEA', + r'_ROWID_': '(((ctid::text::point)[0]::bigint << 32) | (ctid::text::point)[1]::bigint)', # Yeah, I know... r'CURRENT_TIMESTAMP\(\)': "EXTRACT(epoch FROM now())", } diff --git a/tests/test_db.py b/tests/test_db.py index 8b1ac2969d4b..eccb492add99 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -508,6 +508,9 @@ def test_db_forward_migrate(bitcoind, node_factory): assert l1.rpc.getinfo()['fees_collected_msat'] == 4 assert len(l1.rpc.listforwards()['forwards']) == 4 + # The two null in_htlc_id are replaced with bogus entries! + assert sum([f['in_htlc_id'] > 0xFFFFFFFFFFFF for f in l1.rpc.listforwards()['forwards']]) == 2 + # Make sure autoclean can handle these! l1.stop() l1.daemon.opts['autoclean-succeededforwards-age'] = 2 diff --git a/wallet/db.c b/wallet/db.c index 6418a1924f36..6b39a44050de 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -912,7 +912,10 @@ static struct migration dbmigrations[] = { ", PRIMARY KEY(in_channel_scid, in_htlc_id))"), NULL}, {SQL("INSERT INTO forwards SELECT" " in_channel_scid" - ", (SELECT channel_htlc_id FROM channel_htlcs WHERE id = forwarded_payments.in_htlc_id)" + ", COALESCE(" + " (SELECT channel_htlc_id FROM channel_htlcs WHERE id = forwarded_payments.in_htlc_id)," + " -_ROWID_" + " )" ", out_channel_scid" ", (SELECT channel_htlc_id FROM channel_htlcs WHERE id = forwarded_payments.out_htlc_id)" ", in_msatoshi"