From d8fdf89f7f895d33c827f88977caa1a850f94f19 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 17 Oct 2026 10:38:10 +0200 Subject: [PATCH] csdb: sync the BerkeleyDB log before a checkpoint is saved BlockChain flushes the block storage before it saves a checkpoint, so that the checkpoint never gets ahead of the blocks on disk. The BerkeleyDB backend had no flush(): its commits run with DB_TXN_NOSYNC, so their log records can stay in the in-process log buffer. When the process was killed, the checkpoint survived but the last blocks did not, and the next start failed with "startSequence > last_seq_in_db + 1" and stopped the node. flush() now writes and syncs the log, once per checkpoint. Co-Authored-By: Claude Opus 5.5 --- csdb/include/csdb/database_berkeleydb.hpp | 1 + csdb/src/database_berkeleydb.cpp | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/csdb/include/csdb/database_berkeleydb.hpp b/csdb/include/csdb/database_berkeleydb.hpp index 1f6f57d6b..c04442f81 100644 --- a/csdb/include/csdb/database_berkeleydb.hpp +++ b/csdb/include/csdb/database_berkeleydb.hpp @@ -41,6 +41,7 @@ class DatabaseBerkeleyDB : public Database { bool updateContractData(const cs::Bytes& key, const cs::Bytes& data) override; bool getContractData(const cs::Bytes& key, cs::Bytes& data) override; bool compact() override; + bool flush() override; void logfile_routine(); diff --git a/csdb/src/database_berkeleydb.cpp b/csdb/src/database_berkeleydb.cpp index 6f7689e26..4bc976429 100644 --- a/csdb/src/database_berkeleydb.cpp +++ b/csdb/src/database_berkeleydb.cpp @@ -117,6 +117,21 @@ void DatabaseBerkeleyDB::logfile_routine() { } } +// Commits run with DB_TXN_NOSYNC, so their log records may still sit in the in-process log buffer. +// Write and sync the log, or a checkpoint saved after this call could name blocks that a killed +// process never put on disk, and the next start would refuse the database. +bool DatabaseBerkeleyDB::flush() { + try { + const int status = env_.log_flush(nullptr); + set_last_error_from_berkeleydb(status); + return status == 0; + } + catch (const DbException& e) { + set_last_error_from_berkeleydb(e.get_errno()); + return false; + } +} + void DatabaseBerkeleyDB::set_last_error_from_berkeleydb(int status) { Error err = UnknownError; if (status == 0) {