Skip to content

Commit b223111

Browse files
committed
txdb: add CCoinsViewDB::ChangeCacheSize
We'll need this to dynamically update the cache size of the existing CCoinsViewDB instance when we create a new one during snapshot activation. This requires us to keep the CDBWrapper instance as a pointer instead of a reference so that we're able to destruct it and create a new instance when the cache size changes. Also renames `db` to `m_db` since we're already modifying each usage. Includes feedback from Russ Yanofsky.
1 parent ea3e9e0 commit b223111

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

src/txdb.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <shutdown.h>
1111
#include <ui_interface.h>
1212
#include <uint256.h>
13+
#include <util/memory.h>
1314
#include <util/system.h>
1415
#include <util/translation.h>
1516
#include <util/vector.h>
@@ -41,35 +42,45 @@ struct CoinEntry {
4142

4243
}
4344

44-
CCoinsViewDB::CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe) : db(ldb_path, nCacheSize, fMemory, fWipe, true)
45+
CCoinsViewDB::CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe) :
46+
m_db(MakeUnique<CDBWrapper>(ldb_path, nCacheSize, fMemory, fWipe, true)),
47+
m_ldb_path(ldb_path),
48+
m_is_memory(fMemory) { }
49+
50+
void CCoinsViewDB::ResizeCache(size_t new_cache_size)
4551
{
52+
// Have to do a reset first to get the original `m_db` state to release its
53+
// filesystem lock.
54+
m_db.reset();
55+
m_db = MakeUnique<CDBWrapper>(
56+
m_ldb_path, new_cache_size, m_is_memory, /*fWipe*/ false, /*obfuscate*/ true);
4657
}
4758

4859
bool CCoinsViewDB::GetCoin(const COutPoint &outpoint, Coin &coin) const {
49-
return db.Read(CoinEntry(&outpoint), coin);
60+
return m_db->Read(CoinEntry(&outpoint), coin);
5061
}
5162

5263
bool CCoinsViewDB::HaveCoin(const COutPoint &outpoint) const {
53-
return db.Exists(CoinEntry(&outpoint));
64+
return m_db->Exists(CoinEntry(&outpoint));
5465
}
5566

5667
uint256 CCoinsViewDB::GetBestBlock() const {
5768
uint256 hashBestChain;
58-
if (!db.Read(DB_BEST_BLOCK, hashBestChain))
69+
if (!m_db->Read(DB_BEST_BLOCK, hashBestChain))
5970
return uint256();
6071
return hashBestChain;
6172
}
6273

6374
std::vector<uint256> CCoinsViewDB::GetHeadBlocks() const {
6475
std::vector<uint256> vhashHeadBlocks;
65-
if (!db.Read(DB_HEAD_BLOCKS, vhashHeadBlocks)) {
76+
if (!m_db->Read(DB_HEAD_BLOCKS, vhashHeadBlocks)) {
6677
return std::vector<uint256>();
6778
}
6879
return vhashHeadBlocks;
6980
}
7081

7182
bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
72-
CDBBatch batch(db);
83+
CDBBatch batch(*m_db);
7384
size_t count = 0;
7485
size_t changed = 0;
7586
size_t batch_size = (size_t)gArgs.GetArg("-dbbatchsize", nDefaultDbBatchSize);
@@ -107,7 +118,7 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
107118
mapCoins.erase(itOld);
108119
if (batch.SizeEstimate() > batch_size) {
109120
LogPrint(BCLog::COINDB, "Writing partial batch of %.2f MiB\n", batch.SizeEstimate() * (1.0 / 1048576.0));
110-
db.WriteBatch(batch);
121+
m_db->WriteBatch(batch);
111122
batch.Clear();
112123
if (crash_simulate) {
113124
static FastRandomContext rng;
@@ -124,14 +135,14 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
124135
batch.Write(DB_BEST_BLOCK, hashBlock);
125136

126137
LogPrint(BCLog::COINDB, "Writing final batch of %.2f MiB\n", batch.SizeEstimate() * (1.0 / 1048576.0));
127-
bool ret = db.WriteBatch(batch);
138+
bool ret = m_db->WriteBatch(batch);
128139
LogPrint(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count);
129140
return ret;
130141
}
131142

132143
size_t CCoinsViewDB::EstimateSize() const
133144
{
134-
return db.EstimateSize(DB_COIN, (char)(DB_COIN+1));
145+
return m_db->EstimateSize(DB_COIN, (char)(DB_COIN+1));
135146
}
136147

137148
CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory, fWipe) {
@@ -158,7 +169,7 @@ bool CBlockTreeDB::ReadLastBlockFile(int &nFile) {
158169

159170
CCoinsViewCursor *CCoinsViewDB::Cursor() const
160171
{
161-
CCoinsViewDBCursor *i = new CCoinsViewDBCursor(const_cast<CDBWrapper&>(db).NewIterator(), GetBestBlock());
172+
CCoinsViewDBCursor *i = new CCoinsViewDBCursor(const_cast<CDBWrapper&>(*m_db).NewIterator(), GetBestBlock());
162173
/* It seems that there are no "const iterators" for LevelDB. Since we
163174
only need read operations on it, use a const-cast to get around
164175
that restriction. */
@@ -338,7 +349,7 @@ class CCoins
338349
* Currently implemented: from the per-tx utxo model (0.8..0.14.x) to per-txout.
339350
*/
340351
bool CCoinsViewDB::Upgrade() {
341-
std::unique_ptr<CDBIterator> pcursor(db.NewIterator());
352+
std::unique_ptr<CDBIterator> pcursor(m_db->NewIterator());
342353
pcursor->Seek(std::make_pair(DB_COINS, uint256()));
343354
if (!pcursor->Valid()) {
344355
return true;
@@ -349,7 +360,7 @@ bool CCoinsViewDB::Upgrade() {
349360
LogPrintf("[0%%]..."); /* Continued */
350361
uiInterface.ShowProgress(_("Upgrading UTXO database").translated, 0, true);
351362
size_t batch_size = 1 << 24;
352-
CDBBatch batch(db);
363+
CDBBatch batch(*m_db);
353364
int reportDone = 0;
354365
std::pair<unsigned char, uint256> key;
355366
std::pair<unsigned char, uint256> prev_key = {DB_COINS, uint256()};
@@ -384,18 +395,18 @@ bool CCoinsViewDB::Upgrade() {
384395
}
385396
batch.Erase(key);
386397
if (batch.SizeEstimate() > batch_size) {
387-
db.WriteBatch(batch);
398+
m_db->WriteBatch(batch);
388399
batch.Clear();
389-
db.CompactRange(prev_key, key);
400+
m_db->CompactRange(prev_key, key);
390401
prev_key = key;
391402
}
392403
pcursor->Next();
393404
} else {
394405
break;
395406
}
396407
}
397-
db.WriteBatch(batch);
398-
db.CompactRange({DB_COINS, uint256()}, key);
408+
m_db->WriteBatch(batch);
409+
m_db->CompactRange({DB_COINS, uint256()}, key);
399410
uiInterface.ShowProgress("", 100, false);
400411
LogPrintf("[%s].\n", ShutdownRequested() ? "CANCELLED" : "DONE");
401412
return !ShutdownRequested();

src/txdb.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,16 @@ static const int64_t max_filter_index_cache = 1024;
3939
//! Max memory allocated to coin DB specific cache (MiB)
4040
static const int64_t nMaxCoinsDBCache = 8;
4141

42+
// Actually declared in validation.cpp; can't include because of circular dependency.
43+
extern RecursiveMutex cs_main;
44+
4245
/** CCoinsView backed by the coin database (chainstate/) */
4346
class CCoinsViewDB final : public CCoinsView
4447
{
4548
protected:
46-
CDBWrapper db;
49+
std::unique_ptr<CDBWrapper> m_db;
50+
fs::path m_ldb_path;
51+
bool m_is_memory;
4752
public:
4853
/**
4954
* @param[in] ldb_path Location in the filesystem where leveldb data will be stored.
@@ -60,6 +65,9 @@ class CCoinsViewDB final : public CCoinsView
6065
//! Attempt to update from an older database format. Returns whether an error occurred.
6166
bool Upgrade();
6267
size_t EstimateSize() const override;
68+
69+
//! Dynamically alter the underlying leveldb cache size.
70+
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
6371
};
6472

6573
/** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */

0 commit comments

Comments
 (0)