FlushStateToDisk writes the wallet's SetBestChain at most once an hour,
the shutdown flush included, and its timer starts at "now" on the first
call. A session shorter than an hour therefore never saves: the next
start rescans every block of the session ("Rescanning last N blocks"),
re-extends every note witness across it, and then rewrites the whole
wallet inside init before RPC answers. Measured on regtest with a
throwaway prototype: restarts of 8.3 s -> 2.0 s (150 empty blocks),
29 s -> 2.5 s (150 blocks, 639 commitments), 66 s -> 8.5 s (8,215 notes,
631 MB wallet). The cost moves to the stop, at about 41 MB/s of
wallet.dat.
Add CWallet::PersistStateAtShutdown(), called from Shutdown() inside
the existing cs_main block right after the chainstate flush and before
the coins view and the wallet are torn down. It calls
SetBestChain(chainActive.GetLocator()), which on this tree stamps
pindexWalletTip, the block the wallet's state reflects, in the same
atomic transaction as the witnesses. It logs "Saving wallet state ..."
first, so a GUI can show that the stop is busy, and then the height,
the txs written, the elapsed ms and whether it committed. It skips the
write, and logs why, when:
- AppInit2 did not run to the end (new fInitCompleted): threads were
interrupted but not joined, and with no wallet tip SetBestChain would
stamp the chain tip over an interrupted rescan's checkpoint;
- the shutdown came from the wallet failing on a block
(NotifyWalletsOrShutdown now sets fWalletNotifyFailed), or the BDB
environment has panicked or a checkpoint/lsn_reset failed (new
CDBEnv::IsHealthy()): a full-wallet write into that database fails
or deepens the damage, and the logs are what recovery needs;
- nothing changed since the last committed save. SetBestChain now
returns SetBestChainINTERNAL's result (the CValidationInterface hook
returns bool) and, when it committed, records the block it stamped
and a generation counter that BuildWitnessCache (when it extended),
DecrementNoteWitnesses, VerifyAndSetInitialWitness reseeds,
ClearNoteWitnessCache and AddToWallet (a note-bearing tx inserted or
changed) bump. Init records the same when it finds the locator at
the tip and does not rescan. This also stops a second full write
when the hourly SetBestChain happens to fire in the shutdown flush.
FlushStateToDisk and DATABASE_WRITE_INTERVAL are unchanged: dropping
the throttle for FLUSH_STATE_ALWAYS would also rewrite the whole wallet
on every gettxoutsetinfo and on the init flush after a replay.
witnessRootValidated stays in memory only, so every witness is still
root-checked against hashFinalSaplingRoot on the next start, and a kill
during the write rolls back to the previous save (DB_RECOVER).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
79 lines
2.7 KiB
C++
79 lines
2.7 KiB
C++
// Copyright (c) 2016-2024 The Hush developers
|
|
// Distributed under the GPLv3 software license, see the accompanying
|
|
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include "wallet/db.h"
|
|
|
|
namespace {
|
|
|
|
// A fresh environment in a temp dir with one committed record in "wallet.dat", so database/ holds a log file.
|
|
class WalletDbEnv : public ::testing::Test {
|
|
protected:
|
|
boost::filesystem::path dir;
|
|
CDBEnv env;
|
|
|
|
void SetUp() override {
|
|
dir = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path("dbenv-%%%%%%%%");
|
|
boost::filesystem::create_directories(dir);
|
|
ASSERT_TRUE(env.Open(dir));
|
|
Db db(env.dbenv, DB_CXX_NO_EXCEPTIONS);
|
|
ASSERT_EQ(0, db.open(NULL, "wallet.dat", "main", DB_BTREE, DB_CREATE | DB_AUTO_COMMIT, 0));
|
|
char k[] = "key", v[] = "value";
|
|
Dbt key(k, sizeof k), value(v, sizeof v);
|
|
ASSERT_EQ(0, db.put(NULL, &key, &value, 0));
|
|
ASSERT_EQ(0, db.close(0));
|
|
ASSERT_TRUE(boost::filesystem::exists(Log()));
|
|
}
|
|
void TearDown() override {
|
|
env.Close();
|
|
boost::system::error_code ec;
|
|
boost::filesystem::remove_all(dir, ec);
|
|
}
|
|
boost::filesystem::path Log() const { return dir / "database" / "log.0000000001"; }
|
|
};
|
|
|
|
} // namespace
|
|
|
|
TEST_F(WalletDbEnv, CleanShutdownRemovesTheLogs) {
|
|
env.Flush(true);
|
|
EXPECT_FALSE(boost::filesystem::exists(dir / "database"));
|
|
}
|
|
|
|
// BDB0060 PANIC (a page write failed, e.g. through an fd closed behind BDB's back): the logs may hold committed
|
|
// changes wallet.dat never got, and the next start's DB_RECOVER needs them.
|
|
TEST_F(WalletDbEnv, PanickedEnvironmentKeepsTheLogs) {
|
|
ASSERT_EQ(0, env.dbenv->set_flags(DB_PANIC_ENVIRONMENT, 1));
|
|
env.Flush(true);
|
|
EXPECT_TRUE(boost::filesystem::exists(Log()));
|
|
}
|
|
|
|
TEST_F(WalletDbEnv, FailedCheckpointKeepsTheLogs) {
|
|
env.CheckpointLSN("no-such-file.dat"); // lsn_reset fails
|
|
env.Flush(true);
|
|
EXPECT_TRUE(boost::filesystem::exists(Log()));
|
|
}
|
|
|
|
// The shutdown wallet save asks before writing: a failed flush or a panicked environment means the logs are what
|
|
// recovery needs, and a large write into it would fail or make things worse.
|
|
TEST_F(WalletDbEnv, HealthyUntilAFlushFailsOrTheEnvironmentPanics) {
|
|
EXPECT_TRUE(env.IsHealthy());
|
|
env.Flush(false);
|
|
EXPECT_TRUE(env.IsHealthy());
|
|
ASSERT_EQ(0, env.dbenv->set_flags(DB_PANIC_ENVIRONMENT, 1));
|
|
EXPECT_FALSE(env.IsHealthy());
|
|
}
|
|
|
|
TEST_F(WalletDbEnv, UnhealthyAfterAFailedCheckpoint) {
|
|
env.CheckpointLSN("no-such-file.dat"); // lsn_reset fails
|
|
EXPECT_FALSE(env.IsHealthy());
|
|
}
|
|
|
|
TEST(WalletDbEnvNotOpen, NotOpenIsHealthy) {
|
|
CDBEnv env;
|
|
EXPECT_TRUE(env.IsHealthy());
|
|
}
|