partial Merge #15855: [refactor] interfaces: Add missing LockAnnotation for cs_main

fa3c651143 [refactor] interfaces: Add missing LockAnnotation for cs_main (MarcoFalke)

Pull request description:

  This adds missing `LockAnnotation lock(::cs_main);` to `src/interfaces/chain.cpp` (as well as tests and  benchmarks)

ACKs for commit fa3c65:
  practicalswift:
    utACK fa3c6511435149782545ac0d09d4722dc115d709
  ryanofsky:
    utACK fa3c6511435149782545ac0d09d4722dc115d709

Tree-SHA512: b67082fe3718c94b4addf7f2530593915225c25080f20c3ffa4ff7e08f1f49548f255fb285f89a8feff84be3f6c91e1792495ced9f6bf396732396d1356d597a
This commit is contained in:
MarcoFalke 2019-05-14 08:22:08 -04:00 committed by Pasta
parent dfa040e9b9
commit 0bc782dfcb
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
2 changed files with 18 additions and 4 deletions

View File

@ -22,6 +22,7 @@ class LockImpl : public Chain::Lock
{ {
Optional<int> getHeight() override Optional<int> getHeight() override
{ {
LockAnnotation lock(::cs_main);
int height = ::ChainActive().Height(); int height = ::ChainActive().Height();
if (height >= 0) { if (height >= 0) {
return height; return height;
@ -30,6 +31,7 @@ class LockImpl : public Chain::Lock
} }
Optional<int> getBlockHeight(const uint256& hash) override Optional<int> getBlockHeight(const uint256& hash) override
{ {
LockAnnotation lock(::cs_main);
CBlockIndex* block = LookupBlockIndex(hash); CBlockIndex* block = LookupBlockIndex(hash);
if (block && ::ChainActive().Contains(block)) { if (block && ::ChainActive().Contains(block)) {
return block->nHeight; return block->nHeight;
@ -44,29 +46,34 @@ class LockImpl : public Chain::Lock
} }
uint256 getBlockHash(int height) override uint256 getBlockHash(int height) override
{ {
LockAnnotation lock(::cs_main);
CBlockIndex* block = ::ChainActive()[height]; CBlockIndex* block = ::ChainActive()[height];
assert(block != nullptr); assert(block != nullptr);
return block->GetBlockHash(); return block->GetBlockHash();
} }
int64_t getBlockTime(int height) override int64_t getBlockTime(int height) override
{ {
LockAnnotation lock(::cs_main);
CBlockIndex* block = ::ChainActive()[height]; CBlockIndex* block = ::ChainActive()[height];
assert(block != nullptr); assert(block != nullptr);
return block->GetBlockTime(); return block->GetBlockTime();
} }
int64_t getBlockMedianTimePast(int height) override int64_t getBlockMedianTimePast(int height) override
{ {
LockAnnotation lock(::cs_main);
CBlockIndex* block = ::ChainActive()[height]; CBlockIndex* block = ::ChainActive()[height];
assert(block != nullptr); assert(block != nullptr);
return block->GetMedianTimePast(); return block->GetMedianTimePast();
} }
bool haveBlockOnDisk(int height) override bool haveBlockOnDisk(int height) override
{ {
LockAnnotation lock(::cs_main);
CBlockIndex* block = ::ChainActive()[height]; CBlockIndex* block = ::ChainActive()[height];
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0; return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0;
} }
Optional<int> findFirstBlockWithTime(int64_t time, uint256* hash) override Optional<int> findFirstBlockWithTime(int64_t time, uint256* hash) override
{ {
LockAnnotation lock(::cs_main);
CBlockIndex* block = ::ChainActive().FindEarliestAtLeast(time); CBlockIndex* block = ::ChainActive().FindEarliestAtLeast(time);
if (block) { if (block) {
if (hash) *hash = block->GetBlockHash(); if (hash) *hash = block->GetBlockHash();
@ -90,6 +97,7 @@ class LockImpl : public Chain::Lock
} }
Optional<int> findPruned(int start_height, Optional<int> stop_height) override Optional<int> findPruned(int start_height, Optional<int> stop_height) override
{ {
LockAnnotation lock(::cs_main);
if (::fPruneMode) { if (::fPruneMode) {
CBlockIndex* block = stop_height ? ::ChainActive()[*stop_height] : ::ChainActive().Tip(); CBlockIndex* block = stop_height ? ::ChainActive()[*stop_height] : ::ChainActive().Tip();
while (block && block->nHeight >= start_height) { while (block && block->nHeight >= start_height) {
@ -103,6 +111,7 @@ class LockImpl : public Chain::Lock
} }
Optional<int> findFork(const uint256& hash, Optional<int>* height) override Optional<int> findFork(const uint256& hash, Optional<int>* height) override
{ {
LockAnnotation lock(::cs_main);
const CBlockIndex* block = LookupBlockIndex(hash); const CBlockIndex* block = LookupBlockIndex(hash);
const CBlockIndex* fork = block ? ::ChainActive().FindFork(block) : nullptr; const CBlockIndex* fork = block ? ::ChainActive().FindFork(block) : nullptr;
if (height) { if (height) {
@ -119,11 +128,16 @@ class LockImpl : public Chain::Lock
} }
bool isPotentialTip(const uint256& hash) override bool isPotentialTip(const uint256& hash) override
{ {
LockAnnotation lock(::cs_main);
if (::ChainActive().Tip()->GetBlockHash() == hash) return true; if (::ChainActive().Tip()->GetBlockHash() == hash) return true;
CBlockIndex* block = LookupBlockIndex(hash); CBlockIndex* block = LookupBlockIndex(hash);
return block && block->GetAncestor(::ChainActive().Height()) == ::ChainActive().Tip(); return block && block->GetAncestor(::ChainActive().Height()) == ::ChainActive().Tip();
} }
CBlockLocator getTipLocator() override { return ::ChainActive().GetLocator(); } CBlockLocator getTipLocator() override
{
LockAnnotation lock(::cs_main);
return ::ChainActive().GetLocator();
}
Optional<int> findLocatorFork(const CBlockLocator& locator) override Optional<int> findLocatorFork(const CBlockLocator& locator) override
{ {
LockAnnotation lock(::cs_main); LockAnnotation lock(::cs_main);

View File

@ -14,6 +14,7 @@
#include <consensus/validation.h> #include <consensus/validation.h>
#include <interfaces/chain.h> #include <interfaces/chain.h>
#include <key_io.h> #include <key_io.h>
#include <policy/policy.h>
#include <rpc/server.h> #include <rpc/server.h>
#include <test/setup_common.h> #include <test/setup_common.h>
#include <validation.h> #include <validation.h>
@ -46,7 +47,7 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
auto chain = interfaces::MakeChain(); auto chain = interfaces::MakeChain();
auto locked_chain = chain->lock(); auto locked_chain = chain->lock();
LockAnnotation lock(::cs_main); // for PruneOneBlockFile LockAnnotation lock(::cs_main);
// Verify ScanForWalletTransactions accommodates a null start block. // Verify ScanForWalletTransactions accommodates a null start block.
{ {
@ -117,14 +118,13 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup) BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
{ {
auto chain = interfaces::MakeChain();
// Cap last block file size, and mine new block in a new block file. // Cap last block file size, and mine new block in a new block file.
CBlockIndex* oldTip = ::ChainActive().Tip(); CBlockIndex* oldTip = ::ChainActive().Tip();
GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE; GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE;
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
CBlockIndex* newTip = ::ChainActive().Tip(); CBlockIndex* newTip = ::ChainActive().Tip();
auto chain = interfaces::MakeChain();
auto locked_chain = chain->lock(); auto locked_chain = chain->lock();
LockAnnotation lock(::cs_main); LockAnnotation lock(::cs_main);