From 7171ebc7cbd911fa7ccad732ea7f73bce30928ee Mon Sep 17 00:00:00 2001
From: Martin Zumsande <mzumsande@gmail.com>
Date: Fri, 6 May 2022 14:07:51 +0200
Subject: index: Don't commit a best block before indexing it during sync

Committing a block prior to indexing would leave the index database
in an inconsistent state until it is indexed, which could corrupt the
index in case of a unclean shutdown. Thus commit its predecessor.

Co-authored-by: Luke Dashjr <luke-jr+git@utopios.org>
---
 src/index/base.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'src')

diff --git a/src/index/base.cpp b/src/index/base.cpp
index f3c9395928..09f76adad9 100644
--- a/src/index/base.cpp
+++ b/src/index/base.cpp
@@ -168,7 +168,7 @@ void BaseIndex::ThreadSync()
             }
 
             if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL < current_time) {
-                SetBestBlockIndex(pindex);
+                SetBestBlockIndex(pindex->pprev);
                 last_locator_write_time = current_time;
                 // No need to handle errors in Commit. See rationale above.
                 Commit();
-- 
cgit v1.2.3