Skip to content

Commit dfb963e

Browse files
committed
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.
1 parent 22798b8 commit dfb963e

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

devtools/sql-rewrite.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def rewrite_single(self, q):
7575

7676
typemapping = {
7777
r'BLOB': 'BYTEA',
78+
r'_ROWID_': '(((ctid::text::point)[0]::bigint << 32) | (ctid::text::point)[1]::bigint)', # Yeah, I know...
7879
r'CURRENT_TIMESTAMP\(\)': "EXTRACT(epoch FROM now())",
7980
}
8081

tests/test_db.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@ def test_db_forward_migrate(bitcoind, node_factory):
508508
assert l1.rpc.getinfo()['fees_collected_msat'] == 4
509509
assert len(l1.rpc.listforwards()['forwards']) == 4
510510

511+
# The two null in_htlc_id are replaced with bogus entries!
512+
assert sum([f['in_htlc_id'] > 0xFFFFFFFFFFFF for f in l1.rpc.listforwards()['forwards']]) == 2
513+
511514
# Make sure autoclean can handle these!
512515
l1.stop()
513516
l1.daemon.opts['autoclean-succeededforwards-age'] = 2

wallet/db.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,10 @@ static struct migration dbmigrations[] = {
912912
", PRIMARY KEY(in_channel_scid, in_htlc_id))"), NULL},
913913
{SQL("INSERT INTO forwards SELECT"
914914
" in_channel_scid"
915-
", (SELECT channel_htlc_id FROM channel_htlcs WHERE id = forwarded_payments.in_htlc_id)"
915+
", COALESCE("
916+
" (SELECT channel_htlc_id FROM channel_htlcs WHERE id = forwarded_payments.in_htlc_id),"
917+
" -_ROWID_"
918+
" )"
916919
", out_channel_scid"
917920
", (SELECT channel_htlc_id FROM channel_htlcs WHERE id = forwarded_payments.out_htlc_id)"
918921
", in_msatoshi"

0 commit comments

Comments
 (0)