Skip to content

Commit

Permalink
db: Backfill missing HTLC IDs in the forwards table
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cdecker committed Nov 28, 2022
1 parent 22798b8 commit dfb963e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions devtools/sql-rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())",
}

Expand Down
3 changes: 3 additions & 0 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit dfb963e

Please sign in to comment.