From 24e44c354d5b9174cb9cb7d157a985e197aa7886 Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Wed, 7 Jun 2017 13:00:11 -0400 Subject: Don't return stale data from CCoinsViewCache::Cursor() CCoinsViewCache doesn't actually support cursor iteration returning the current contents of the cache, so raise an error when the cursor method is called instead of returning a cursor that iterates over stale data. Also update the gettxoutsetinfo RPC which was relying on the old behavior to be explicit about which view it is returning data about. --- src/coins.h | 3 +++ src/init.cpp | 1 - src/rpc/blockchain.cpp | 3 ++- src/validation.cpp | 1 + src/validation.h | 4 ++++ 5 files changed, 10 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/coins.h b/src/coins.h index 476db8f37c..b10c7ea9a0 100644 --- a/src/coins.h +++ b/src/coins.h @@ -212,6 +212,9 @@ public: uint256 GetBestBlock() const; void SetBestBlock(const uint256 &hashBlock); bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock); + CCoinsViewCursor* Cursor() const { + throw std::logic_error("CCoinsViewCache cursor iteration not supported."); + } /** * Check if we have the given utxo already loaded in this cache. diff --git a/src/init.cpp b/src/init.cpp index 33023a1800..8d161004b0 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -162,7 +162,6 @@ public: // Writes do not need similar protection, as failure to write is handled by the caller. }; -static CCoinsViewDB *pcoinsdbview = NULL; static CCoinsViewErrorCatcher *pcoinscatcher = NULL; static std::unique_ptr globalVerifyHandle; diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 96871ce1dc..c9adfefa7f 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -19,6 +19,7 @@ #include "rpc/server.h" #include "streams.h" #include "sync.h" +#include "txdb.h" #include "txmempool.h" #include "util.h" #include "utilstrencodings.h" @@ -917,7 +918,7 @@ UniValue gettxoutsetinfo(const JSONRPCRequest& request) CCoinsStats stats; FlushStateToDisk(); - if (GetUTXOStats(pcoinsTip, stats)) { + if (GetUTXOStats(pcoinsdbview, stats)) { ret.push_back(Pair("height", (int64_t)stats.nHeight)); ret.push_back(Pair("bestblock", stats.hashBlock.GetHex())); ret.push_back(Pair("transactions", (int64_t)stats.nTransactions)); diff --git a/src/validation.cpp b/src/validation.cpp index de65839eef..b83c677cf4 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -175,6 +175,7 @@ CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& loc return chain.Genesis(); } +CCoinsViewDB *pcoinsdbview = NULL; CCoinsViewCache *pcoinsTip = NULL; CBlockTreeDB *pblocktree = NULL; diff --git a/src/validation.h b/src/validation.h index 096fd0a9ee..219faf0b67 100644 --- a/src/validation.h +++ b/src/validation.h @@ -36,6 +36,7 @@ class CBlockIndex; class CBlockTreeDB; class CBloomFilter; class CChainParams; +class CCoinsViewDB; class CInv; class CConnman; class CScriptCheck; @@ -482,6 +483,9 @@ bool ResetBlockFailureFlags(CBlockIndex *pindex); /** The currently-connected chain of blocks (protected by cs_main). */ extern CChain chainActive; +/** Global variable that points to the coins database (protected by cs_main) */ +extern CCoinsViewDB *pcoinsdbview; + /** Global variable that points to the active CCoinsView (protected by cs_main) */ extern CCoinsViewCache *pcoinsTip; -- cgit v1.2.3 From 3ff1fa8c4a6198ebc277bbf814b4e07bf78eb98a Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Thu, 8 Jun 2017 09:28:28 -0400 Subject: Use override keyword on CCoinsView overrides --- src/coins.h | 10 +++++----- src/test/coins_tests.cpp | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/coins.h b/src/coins.h index b10c7ea9a0..833e066c38 100644 --- a/src/coins.h +++ b/src/coins.h @@ -207,12 +207,12 @@ public: CCoinsViewCache(CCoinsView *baseIn); // Standard CCoinsView methods - bool GetCoin(const COutPoint &outpoint, Coin &coin) const; - bool HaveCoin(const COutPoint &outpoint) const; - uint256 GetBestBlock() const; + bool GetCoin(const COutPoint &outpoint, Coin &coin) const override; + bool HaveCoin(const COutPoint &outpoint) const override; + uint256 GetBestBlock() const override; void SetBestBlock(const uint256 &hashBlock); - bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock); - CCoinsViewCursor* Cursor() const { + bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override; + CCoinsViewCursor* Cursor() const override { throw std::logic_error("CCoinsViewCache cursor iteration not supported."); } diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp index 0ed71b96fc..40801ab16c 100644 --- a/src/test/coins_tests.cpp +++ b/src/test/coins_tests.cpp @@ -37,7 +37,7 @@ class CCoinsViewTest : public CCoinsView std::map map_; public: - bool GetCoin(const COutPoint& outpoint, Coin& coin) const + bool GetCoin(const COutPoint& outpoint, Coin& coin) const override { std::map::const_iterator it = map_.find(outpoint); if (it == map_.end()) { @@ -51,15 +51,15 @@ public: return true; } - bool HaveCoin(const COutPoint& outpoint) const + bool HaveCoin(const COutPoint& outpoint) const override { Coin coin; return GetCoin(outpoint, coin); } - uint256 GetBestBlock() const { return hashBestBlock_; } + uint256 GetBestBlock() const override { return hashBestBlock_; } - bool BatchWrite(CCoinsMap& mapCoins, const uint256& hashBlock) + bool BatchWrite(CCoinsMap& mapCoins, const uint256& hashBlock) override { for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); ) { if (it->second.flags & CCoinsCacheEntry::DIRTY) { -- cgit v1.2.3