Skip to content

Commit

Permalink
Merge #1203: [Backport][Performance] Cache + guard best block hash.
Browse files Browse the repository at this point in the history
4976c0a [Backport][Performance] Cache best block hash for miner thread usage + refactor. (furszy)

Pull request description:

  This comes from upstream PR 12743.

   * Best block hash cached for miner thread usage (preventing one of the many many cs_main locks..).
   * csBestBlock, cvBlockChange names updated to latest upstream naming convention.

ACKs for top commit:
  random-zebra:
    ACK 4976c0a
  Fuzzbawls:
    utACK 4976c0a

Tree-SHA512: 9d3281bd4b01ef250af9136136582af99f04b1d81b7c6b6c1b8e41aa208f8ebff7a25c0bca823bc5b74d4802d29d10e58b8ebf43764fb9208bdf4a4f6140d822
  • Loading branch information
Liquid369 committed Jan 5, 2020
1 parent 823ba7e commit e32c7c8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ void OnRPCStopped()
{
uiInterface.NotifyBlockTip.disconnect(RPCNotifyBlockChange);
//RPCNotifyBlockChange(0);
cvBlockChange.notify_all();
g_best_block_cv.notify_all();
LogPrint("rpc", "RPC stopped.\n");
}

Expand Down
30 changes: 16 additions & 14 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ map<unsigned int, unsigned int> mapHashedBlocks;
CChain chainActive;
CBlockIndex* pindexBestHeader = nullptr;
int64_t nTimeBestReceived = 0;
CWaitableCriticalSection csBestBlock;
CConditionVariable cvBlockChange;

// Best block section
CWaitableCriticalSection g_best_block_mutex;
std::condition_variable g_best_block_cv;
uint256 g_best_block;

int nScriptCheckThreads = 0;
bool fImporting = false;
bool fReindex = false;
Expand Down Expand Up @@ -3475,23 +3479,21 @@ void FlushStateToDisk()
void static UpdateTip(CBlockIndex* pindexNew)
{
chainActive.SetTip(pindexNew);
/*
#ifdef ENABLE_WALLET
// If turned on AutoZeromint will automatically convert DOGEC to zdogec
if (pwalletMain && pwalletMain->isZeromintEnabled())
pwalletMain->AutoZeromint();
#endif // ENABLE_WALLET
*/

// New best block
nTimeBestReceived = GetTime();
mempool.AddTransactionsUpdated(1);

LogPrintf("UpdateTip: new best=%s height=%d version=%d log2_work=%.8g tx=%lu date=%s progress=%f cache=%u\n",
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), chainActive.Tip()->nVersion, log(chainActive.Tip()->nChainWork.getdouble()) / log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
Checkpoints::GuessVerificationProgress(chainActive.Tip()), (unsigned int)pcoinsTip->GetCacheSize());
{
WaitableLock lock(g_best_block_mutex);
g_best_block = pindexNew->GetBlockHash();
g_best_block_cv.notify_all();
}

cvBlockChange.notify_all();
LogPrintf("UpdateTip: new best=%s height=%d version=%d log2_work=%.8g tx=%lu date=%s progress=%f cache=%u\n",
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), chainActive.Tip()->nVersion, log(chainActive.Tip()->nChainWork.getdouble()) / log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
Checkpoints::GuessVerificationProgress(chainActive.Tip()), (unsigned int)pcoinsTip->GetCacheSize());

// Check the version of the last 100 blocks to see if we need to upgrade:
static bool fWarned = false;
Expand Down
8 changes: 6 additions & 2 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,12 @@ extern uint64_t nLastBlockTx;
extern uint64_t nLastBlockSize;
extern const std::string strMessageMagic;
extern int64_t nTimeBestReceived;
extern CWaitableCriticalSection csBestBlock;
extern CConditionVariable cvBlockChange;

// Best block section
extern CWaitableCriticalSection g_best_block_mutex;
extern std::condition_variable g_best_block_cv;
extern uint256 g_best_block;

extern bool fImporting;
extern bool fReindex;
extern int nScriptCheckThreads;
Expand Down
6 changes: 3 additions & 3 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,9 @@ bool ProcessBlockFound(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey)

// Found a solution
{
LOCK(cs_main);
if (pblock->hashPrevBlock != chainActive.Tip()->GetBlockHash())
return error("DogeCashMiner : generated block is stale");
WaitableLock lock(g_best_block_mutex);
if (pblock->hashPrevBlock != g_best_block)
return error("PIVXMiner : generated block is stale");
}

// Remove key from key pool
Expand Down
6 changes: 3 additions & 3 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,9 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
{
checktxtime = std::chrono::steady_clock::now() + std::chrono::minutes(1);

WaitableLock lock(csBestBlock);
while (chainActive.Tip()->GetBlockHash() == hashWatchedChain && IsRPCRunning()) {
if (cvBlockChange.wait_until(lock, checktxtime) == std::cv_status::timeout)
WaitableLock lock(g_best_block_mutex);
while (g_best_block == hashWatchedChain && IsRPCRunning()) {
if (g_best_block_cv.wait_until(lock, checktxtime) == std::cv_status::timeout)
{
// Timeout: Check transactions for update
if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP)
Expand Down

0 comments on commit e32c7c8

Please sign in to comment.