From 89ff46f1e68d6e5eb8a8bfafbb58ae2f367ed082 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 23 Apr 2018 23:53:45 +0200 Subject: [PATCH] db: Added DB migrations to get the correct sync height The no-rescan change requires us to rescan one last time from the first_blocknum of our channels (if we have any). The migrations just drop blocks that are higher, then insert a dummy with the first_blocknum, and then clean up after us. If we don't have any channels we don't go back at all. Signed-off-by: Christian Decker --- wallet/db.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/wallet/db.c b/wallet/db.c index 33667e991..f3383f833 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -318,6 +318,16 @@ char *dbmigrations[] = { ", blockheight INTEGER REFERENCES blocks(height) ON DELETE CASCADE" ", PRIMARY KEY(id)" ");", + /* -- Set the correct rescan height for PR #1398 -- */ + /* Delete blocks that are higher than our initial scan point, this is a + * no-op if we don't have a channel. */ + "DELETE FROM blocks WHERE height > (SELECT MIN(first_blocknum) FROM channels);", + /* Now make sure we have the lower bound block with the first_blocknum + * height. This may introduce a block with NULL height if we didn't have any + * blocks, remove that in the next. */ + "INSERT OR IGNORE INTO blocks (height) VALUES ((SELECT MIN(first_blocknum) FROM channels));", + "DELETE FROM blocks WHERE height IS NULL;", + /* -- End of PR #1398 -- */ NULL, };