Skip to content

Commit c00fa1a

Browse files
committed
refactor, txdb: Add CoinsViewOptions struct
Add CoinsViewOptions struct to remove ArgsManager uses from txdb. To reduce size of this commit, this moves references to gArgs variable out of txdb.cpp to calling code in validation.cpp. But these moves are temporary. The gArgs references in validation.cpp are moved out to calling code in init.cpp in later commits. This commit does not change behavior.
1 parent 2eaeded commit c00fa1a

File tree

8 files changed

+72
-40
lines changed

8 files changed

+72
-40
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ BITCOIN_CORE_H = \
204204
node/chainstate.h \
205205
node/chainstatemanager_args.h \
206206
node/coin.h \
207+
node/coins_view_args.h \
207208
node/connection_types.h \
208209
node/context.h \
209210
node/database_args.h \
@@ -389,6 +390,7 @@ libbitcoin_node_a_SOURCES = \
389390
node/chainstate.cpp \
390391
node/chainstatemanager_args.cpp \
391392
node/coin.cpp \
393+
node/coins_view_args.cpp \
392394
node/connection_types.cpp \
393395
node/context.cpp \
394396
node/database_args.cpp \

src/node/coins_view_args.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <node/coins_view_args.h>
6+
7+
#include <txdb.h>
8+
#include <util/system.h>
9+
10+
namespace node {
11+
void ReadCoinsViewArgs(const ArgsManager& args, CoinsViewOptions& options)
12+
{
13+
if (auto value = args.GetIntArg("-dbbatchsize")) options.batch_write_bytes = *value;
14+
if (auto value = args.GetIntArg("-dbcrashratio")) options.simulate_crash_ratio = *value;
15+
}
16+
} // namespace node

src/node/coins_view_args.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_NODE_COINS_VIEW_ARGS_H
6+
#define BITCOIN_NODE_COINS_VIEW_ARGS_H
7+
8+
class ArgsManager;
9+
struct CoinsViewOptions;
10+
11+
namespace node {
12+
void ReadCoinsViewArgs(const ArgsManager& args, CoinsViewOptions& options);
13+
} // namespace node
14+
15+
#endif // BITCOIN_NODE_COINS_VIEW_ARGS_H

src/test/coins_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
278278
CCoinsViewTest base;
279279
SimulationTest(&base, false);
280280

281-
CCoinsViewDB db_base{"test", /*nCacheSize=*/1 << 23, /*fMemory=*/true, /*fWipe=*/false};
281+
CCoinsViewDB db_base{{.path = "test", .cache_bytes = 1 << 23, .memory_only = true}, {}};
282282
SimulationTest(&db_base, true);
283283
}
284284

@@ -1064,7 +1064,7 @@ void TestFlushBehavior(
10641064
BOOST_AUTO_TEST_CASE(ccoins_flush_behavior)
10651065
{
10661066
// Create two in-memory caches atop a leveldb view.
1067-
CCoinsViewDB base{"test", /*nCacheSize=*/ 1 << 23, /*fMemory=*/ true, /*fWipe=*/ false};
1067+
CCoinsViewDB base{{.path = "test", .cache_bytes = 1 << 23, .memory_only = true}, {}};
10681068
std::vector<std::unique_ptr<CCoinsViewCacheTest>> caches;
10691069
caches.push_back(std::make_unique<CCoinsViewCacheTest>(&base));
10701070
caches.push_back(std::make_unique<CCoinsViewCacheTest>(caches.back().get()));

src/txdb.cpp

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,22 @@ struct CoinEntry {
7171

7272
} // namespace
7373

74-
CCoinsViewDB::CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe) :
75-
m_db{std::make_unique<CDBWrapper>(DBParams{
76-
.path = ldb_path,
77-
.cache_bytes = nCacheSize,
78-
.memory_only = fMemory,
79-
.wipe_data = fWipe,
80-
.obfuscate = true,
81-
.options = [] { DBOptions options; node::ReadDatabaseArgs(gArgs, options); return options; }()})},
82-
m_ldb_path(ldb_path),
83-
m_is_memory(fMemory) { }
74+
CCoinsViewDB::CCoinsViewDB(DBParams db_params, CoinsViewOptions options) :
75+
m_db_params{std::move(db_params)},
76+
m_options{std::move(options)},
77+
m_db{std::make_unique<CDBWrapper>(m_db_params)} { }
8478

8579
void CCoinsViewDB::ResizeCache(size_t new_cache_size)
8680
{
8781
// We can't do this operation with an in-memory DB since we'll lose all the coins upon
8882
// reset.
89-
if (!m_is_memory) {
83+
if (!m_db_params.memory_only) {
9084
// Have to do a reset first to get the original `m_db` state to release its
9185
// filesystem lock.
9286
m_db.reset();
93-
m_db = std::make_unique<CDBWrapper>(DBParams{
94-
.path = m_ldb_path,
95-
.cache_bytes = new_cache_size,
96-
.memory_only = m_is_memory,
97-
.wipe_data = false,
98-
.obfuscate = true,
99-
.options = [] { DBOptions options; node::ReadDatabaseArgs(gArgs, options); return options; }()});
87+
m_db_params.cache_bytes = new_cache_size;
88+
m_db_params.wipe_data = false;
89+
m_db = std::make_unique<CDBWrapper>(m_db_params);
10090
}
10191
}
10292

@@ -127,8 +117,6 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, boo
127117
CDBBatch batch(*m_db);
128118
size_t count = 0;
129119
size_t changed = 0;
130-
size_t batch_size = (size_t)gArgs.GetIntArg("-dbbatchsize", nDefaultDbBatchSize);
131-
int crash_simulate = gArgs.GetIntArg("-dbcrashratio", 0);
132120
assert(!hashBlock.IsNull());
133121

134122
uint256 old_tip = GetBestBlock();
@@ -159,13 +147,13 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, boo
159147
}
160148
count++;
161149
it = erase ? mapCoins.erase(it) : std::next(it);
162-
if (batch.SizeEstimate() > batch_size) {
150+
if (batch.SizeEstimate() > m_options.batch_write_bytes) {
163151
LogPrint(BCLog::COINDB, "Writing partial batch of %.2f MiB\n", batch.SizeEstimate() * (1.0 / 1048576.0));
164152
m_db->WriteBatch(batch);
165153
batch.Clear();
166-
if (crash_simulate) {
154+
if (m_options.simulate_crash_ratio) {
167155
static FastRandomContext rng;
168-
if (rng.randrange(crash_simulate) == 0) {
156+
if (rng.randrange(m_options.simulate_crash_ratio) == 0) {
169157
LogPrintf("Simulating a crash. Goodbye.\n");
170158
_Exit(0);
171159
}

src/txdb.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,24 @@ static const int64_t max_filter_index_cache = 1024;
4545
//! Max memory allocated to coin DB specific cache (MiB)
4646
static const int64_t nMaxCoinsDBCache = 8;
4747

48+
//! User-controlled performance and debug options.
49+
struct CoinsViewOptions {
50+
//! Maximum database write batch size in bytes.
51+
size_t batch_write_bytes = nDefaultDbBatchSize;
52+
//! If non-zero, randomly exit when the database is flushed with (1/ratio)
53+
//! probability.
54+
int simulate_crash_ratio = 0;
55+
};
56+
4857
/** CCoinsView backed by the coin database (chainstate/) */
4958
class CCoinsViewDB final : public CCoinsView
5059
{
5160
protected:
61+
DBParams m_db_params;
62+
CoinsViewOptions m_options;
5263
std::unique_ptr<CDBWrapper> m_db;
53-
fs::path m_ldb_path;
54-
bool m_is_memory;
5564
public:
56-
/**
57-
* @param[in] ldb_path Location in the filesystem where leveldb data will be stored.
58-
*/
59-
explicit CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe);
65+
explicit CCoinsViewDB(DBParams db_params, CoinsViewOptions options);
6066

6167
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
6268
bool HaveCoin(const COutPoint &outpoint) const override;

src/validation.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <node/blockstorage.h>
2929
#include <node/interface_ui.h>
3030
#include <node/utxo_snapshot.h>
31+
#include <node/coins_view_args.h>
32+
#include <node/database_args.h>
3133
#include <policy/policy.h>
3234
#include <policy/rbf.h>
3335
#include <policy/settings.h>
@@ -1511,13 +1513,9 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
15111513
return nSubsidy;
15121514
}
15131515

1514-
CoinsViews::CoinsViews(
1515-
fs::path ldb_name,
1516-
size_t cache_size_bytes,
1517-
bool in_memory,
1518-
bool should_wipe) : m_dbview(
1519-
gArgs.GetDataDirNet() / ldb_name, cache_size_bytes, in_memory, should_wipe),
1520-
m_catcherview(&m_dbview) {}
1516+
CoinsViews::CoinsViews(DBParams db_params, CoinsViewOptions options)
1517+
: m_dbview{std::move(db_params), std::move(options)},
1518+
m_catcherview(&m_dbview) {}
15211519

15221520
void CoinsViews::InitCache()
15231521
{
@@ -1546,7 +1544,14 @@ void Chainstate::InitCoinsDB(
15461544
}
15471545

15481546
m_coins_views = std::make_unique<CoinsViews>(
1549-
leveldb_name, cache_size_bytes, in_memory, should_wipe);
1547+
DBParams{
1548+
.path = gArgs.GetDataDirNet() / leveldb_name,
1549+
.cache_bytes = cache_size_bytes,
1550+
.memory_only = in_memory,
1551+
.wipe_data = should_wipe,
1552+
.obfuscate = true,
1553+
.options = [] { DBOptions options; node::ReadDatabaseArgs(gArgs, options); return options; }()},
1554+
[] { CoinsViewOptions options; node::ReadCoinsViewArgs(gArgs, options); return options; }());
15501555
}
15511556

15521557
void Chainstate::InitCoinsCache(size_t cache_size_bytes)

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ class CoinsViews {
408408
//! state to disk, which should not be done until the health of the database is verified.
409409
//!
410410
//! All arguments forwarded onto CCoinsViewDB.
411-
CoinsViews(fs::path ldb_name, size_t cache_size_bytes, bool in_memory, bool should_wipe);
411+
CoinsViews(DBParams db_params, CoinsViewOptions options);
412412

413413
//! Initialize the CCoinsViewCache member.
414414
void InitCache() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);

0 commit comments

Comments
 (0)