Skip to content

Commit e2fc94f

Browse files
performance: reduce PoW checks on blockindex load (#36)
* performance: reduce PoW checks on blockindex load When the blockindex is constructed from disk on startup, currently each header is subjected to a PoW check. This is relatively cheap for Bitcoin's SHA256D, but less optimal when having to perform FactorN's ghash function. This changes the frequency at which the PoW check is performed to be configurable with the startup argument idxpowcheckrate, specifying a denominator at which PoW is checked during initial blockindex construction when the node starts, and sets the default value to 100 - meaning 1 in 100 blockheaders that are loaded from disk will be selected randomly to undergo a PoW check. More paranoid node operators can lower the value, at the cost of startup performance. Note: All leveldb records that are read by the iterator are still subjected to internal integrity checks. * lint: allow -idxpowcheckrate to be undocumented for now
1 parent 896a16b commit e2fc94f

4 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ void SetupServerArgs(ArgsManager& argsman)
366366

367367
// Hidden Options
368368
std::vector<std::string> hidden_args = {
369-
"-dbcrashratio", "-forcecompactdb",
369+
"-dbcrashratio", "-forcecompactdb", "-idxpowcheckrate",
370370
// GUI args. These will be overwritten by SetupUIArgs for the GUI
371371
"-choosedatadir", "-lang=<lang>", "-min", "-resetguisettings", "-splash", "-uiplatform"};
372372

src/txdb.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams,
277277

278278
pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
279279

280+
// The denominator rate at which we will check PoW when re-constructing
281+
// m_block_index from disk. Higher values reduce the amount of checks
282+
// performed, i.e. `100` means that 1 in 100 headers will be randomly
283+
// selected for a PoW check, and `1` means every header will be checked.
284+
int powcheck_rate = gArgs.GetArg("-idxpowcheckrate", nDefaultCheckPoWRate);
285+
static FastRandomContext rngPoWCheck;
286+
280287
// Load m_block_index
281288
while (pcursor->Valid()) {
282289
if (ShutdownRequested()) return false;
@@ -301,8 +308,12 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams,
301308
pindexNew->wOffset = diskindex.wOffset;
302309
pindexNew->nTx = diskindex.nTx;
303310

304-
if (!CheckProofOfWork( pindexNew->GetBlockHeader(), consensusParams))
311+
// Randomly check PoW. This does not affect integrity, as every
312+
// record is integrity-checked by leveldb; see also the
313+
// CDBWrapper::CDBWrapper implementation
314+
if (rngPoWCheck.randrange(powcheck_rate) == 0 && !CheckProofOfWork(pindexNew->GetBlockHeader(), consensusParams)) {
305315
return error("%s: CheckProofOfWork failed: %s", __func__, pindexNew->ToString());
316+
}
306317

307318
pcursor->Next();
308319
} else {

src/txdb.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ static const int64_t nMaxTxIndexCache = 1024;
3838
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;
41+
//!Default rate of checking pow on index load
42+
static const int nDefaultCheckPoWRate = 100;
4143

4244
// Actually declared in validation.cpp; can't include because of circular dependency.
4345
extern RecursiveMutex cs_main;

test/lint/check-doc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
CMD_GREP_WALLET_HIDDEN_ARGS = r"git grep --function-context 'void DummyWalletInit::AddWalletOptions' -- {}".format(CMD_ROOT_DIR)
2424
CMD_GREP_DOCS = r"git grep --perl-regexp '{}' {}".format(REGEX_DOC, CMD_ROOT_DIR)
2525
# list unsupported, deprecated and duplicate args as they need no documentation
26-
SET_DOC_OPTIONAL = set(['-h', '-help', '-dbcrashratio', '-forcecompactdb', '-zapwallettxes'])
26+
SET_DOC_OPTIONAL = set(['-h', '-help', '-dbcrashratio', '-forcecompactdb', '-zapwallettxes', '-idxpowcheckrate'])
2727

2828

2929
def lint_missing_argument_documentation():

0 commit comments

Comments
 (0)