Skip to content

Commit

Permalink
Pass ChainstateManager to Namecoin code.
Browse files Browse the repository at this point in the history
Properly pass down a ChainstateManager to the parts of the Namecoin
code that need it (mostly validation of the coin database and the
mempool, which is partly based on block heights).

This fixes the breakages introduced by the upstream removal of the
global BlockIndex in bitcoin/bitcoin#19413.
  • Loading branch information
domob1812 committed Jul 6, 2020
1 parent 1cea402 commit 55f4edb
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/coins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bool CCoinsView::GetNamesForHeight(unsigned nHeight, std::set<valtype>& names) c
CNameIterator* CCoinsView::IterateNames() const { assert (false); }
bool CCoinsView::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, const CNameCache &names) { return false; }
CCoinsViewCursor *CCoinsView::Cursor() const { return nullptr; }
bool CCoinsView::ValidateNameDB() const { return false; }
bool CCoinsView::ValidateNameDB(ChainstateManager& chainman) const { return false; }

bool CCoinsView::HaveCoin(const COutPoint &outpoint) const
{
Expand All @@ -39,7 +39,7 @@ void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, const CNameCache &names) { return base->BatchWrite(mapCoins, hashBlock, names); }
CCoinsViewCursor *CCoinsViewBacked::Cursor() const { return base->Cursor(); }
size_t CCoinsViewBacked::EstimateSize() const { return base->EstimateSize(); }
bool CCoinsViewBacked::ValidateNameDB() const { return base->ValidateNameDB(); }
bool CCoinsViewBacked::ValidateNameDB(ChainstateManager& chainman) const { return base->ValidateNameDB(chainman); }

SaltedOutpointHasher::SaltedOutpointHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}

Expand Down
6 changes: 4 additions & 2 deletions src/coins.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <functional>
#include <unordered_map>

class ChainstateManager;

/**
* A UTXO entry.
*
Expand Down Expand Up @@ -219,7 +221,7 @@ class CCoinsView
virtual CCoinsViewCursor *Cursor() const;

// Validate the name database.
virtual bool ValidateNameDB() const;
virtual bool ValidateNameDB(ChainstateManager& chainman) const;

//! As we use CCoinsViews polymorphically, have a virtual destructor
virtual ~CCoinsView() {}
Expand Down Expand Up @@ -249,7 +251,7 @@ class CCoinsViewBacked : public CCoinsView
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, const CNameCache &names) override;
CCoinsViewCursor *Cursor() const override;
size_t EstimateSize() const override;
bool ValidateNameDB() const override;
bool ValidateNameDB(ChainstateManager& chainman) const override;
};


Expand Down
8 changes: 4 additions & 4 deletions src/names/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2014-2019 Daniel Kraft
// Copyright (c) 2014-2020 Daniel Kraft
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand Down Expand Up @@ -467,7 +467,7 @@ UnexpireNames (unsigned nHeight, CBlockUndo& undo, CCoinsViewCache& view,
}

void
CheckNameDB (bool disconnect)
CheckNameDB (ChainstateManager& chainman, bool disconnect)
{
const int option
= gArgs.GetArg ("-checknamedb", Params ().DefaultCheckNameDB ());
Expand All @@ -478,13 +478,13 @@ CheckNameDB (bool disconnect)
assert (option >= 0);
if (option != 0)
{
if (disconnect || ::ChainActive ().Height () % option != 0)
if (disconnect || chainman.ActiveChain ().Height () % option != 0)
return;
}

auto& coinsTip = ::ChainstateActive ().CoinsTip ();
coinsTip.Flush ();
const bool ok = coinsTip.ValidateNameDB ();
const bool ok = coinsTip.ValidateNameDB (chainman);

/* The DB is inconsistent (mismatch between UTXO set and names DB) between
(roughly) blocks 139,000 and 180,000. This is caused by libcoin's
Expand Down
3 changes: 2 additions & 1 deletion src/names/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class CBlockUndo;
class CCoinsView;
class CCoinsViewCache;
class ChainstateManager;
class CTxMemPool;
class TxValidationState;

Expand Down Expand Up @@ -131,6 +132,6 @@ bool UnexpireNames (unsigned nHeight, CBlockUndo& undo,
* this throws an assertion failure.
* @param disconnect Whether we are disconnecting blocks.
*/
void CheckNameDB (bool disconnect);
void CheckNameDB (ChainstateManager& chainman, bool disconnect);

#endif // H_BITCOIN_NAMES_MAIN
4 changes: 2 additions & 2 deletions src/names/mempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ CNameMemPool::removeExpireConflicts (const std::set<valtype>& expired)
}

void
CNameMemPool::check (const CCoinsView& coins) const
CNameMemPool::check (ChainstateManager& chainman, const CCoinsView& coins) const
{
AssertLockHeld (pool.cs);

Expand All @@ -247,7 +247,7 @@ CNameMemPool::check (const CCoinsView& coins) const
if (blockHash.IsNull())
nHeight = 0;
else
nHeight = ::BlockIndex ().find (blockHash)->second->nHeight;
nHeight = chainman.BlockIndex ().find (blockHash)->second->nHeight;

std::set<valtype> nameRegs;
std::map<valtype, unsigned> nameUpdates;
Expand Down
3 changes: 2 additions & 1 deletion src/names/mempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <set>

class CCoinsView;
class ChainstateManager;
class CTxMemPool;
class CTxMemPoolEntry;

Expand Down Expand Up @@ -160,7 +161,7 @@ class CNameMemPool
/**
* Performs sanity checks. Throws if it fails.
*/
void check (const CCoinsView& coins) const;
void check (ChainstateManager& chainman, const CCoinsView& coins) const;

/**
* Checks if a tx can be added (based on name criteria) without
Expand Down
10 changes: 5 additions & 5 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1958,7 +1958,7 @@ static void ProcessHeadersMessage(CNode& pfrom, CConnman* connman, ChainstateMan
return;
}

void static ProcessOrphanTx(CConnman* connman, CTxMemPool& mempool, std::set<uint256>& orphan_work_set, std::list<CTransactionRef>& removed_txn) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans)
void static ProcessOrphanTx(ChainstateManager& chainman, CConnman* connman, CTxMemPool& mempool, std::set<uint256>& orphan_work_set, std::list<CTransactionRef>& removed_txn) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans)
{
AssertLockHeld(cs_main);
AssertLockHeld(g_cs_orphans);
Expand Down Expand Up @@ -2014,7 +2014,7 @@ void static ProcessOrphanTx(CConnman* connman, CTxMemPool& mempool, std::set<uin
EraseOrphanTx(orphanHash);
done = true;
}
mempool.check(&::ChainstateActive().CoinsTip());
mempool.check(chainman, &::ChainstateActive().CoinsTip());
}
}

Expand Down Expand Up @@ -2879,7 +2879,7 @@ void ProcessMessage(

if (!AlreadyHave(inv, mempool) &&
AcceptToMemoryPool(mempool, state, ptx, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
mempool.check(&::ChainstateActive().CoinsTip());
mempool.check(chainman, &::ChainstateActive().CoinsTip());
RelayTransaction(tx.GetHash(), *connman);
for (unsigned int i = 0; i < tx.vout.size(); i++) {
auto it_by_prev = mapOrphanTransactionsByPrev.find(COutPoint(inv.hash, i));
Expand All @@ -2898,7 +2898,7 @@ void ProcessMessage(
mempool.size(), mempool.DynamicMemoryUsage() / 1000);

// Recursively process any orphan transactions that depended on this one
ProcessOrphanTx(connman, mempool, pfrom.orphan_work_set, lRemovedTxn);
ProcessOrphanTx(chainman, connman, mempool, pfrom.orphan_work_set, lRemovedTxn);
}
else if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS)
{
Expand Down Expand Up @@ -3663,7 +3663,7 @@ bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& inter
if (!pfrom->orphan_work_set.empty()) {
std::list<CTransactionRef> removed_txn;
LOCK2(cs_main, g_cs_orphans);
ProcessOrphanTx(connman, m_mempool, pfrom->orphan_work_set, removed_txn);
ProcessOrphanTx(m_chainman, connman, m_mempool, pfrom->orphan_work_set, removed_txn);
for (const CTransactionRef& removedTx : removed_txn) {
AddToCompactExtraTransactions(removedTx);
}
Expand Down
6 changes: 4 additions & 2 deletions src/rpc/names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <names/common.h>
#include <names/main.h>
#include <primitives/transaction.h>
#include <rpc/blockchain.h>
#include <rpc/names.h>
#include <rpc/server.h>
#include <script/names.h>
Expand Down Expand Up @@ -955,10 +956,11 @@ name_checkdb (const JSONRPCRequest& request)
}
).Check (request);

ChainstateManager& chainman = EnsureChainman (request.context);
LOCK (cs_main);
auto& coinsTip = ::ChainstateActive ().CoinsTip ();
auto& coinsTip = chainman.ActiveChainstate ().CoinsTip ();
coinsTip.Flush ();
return coinsTip.ValidateNameDB ();
return coinsTip.ValidateNameDB (chainman);
}

} // namespace
Expand Down
7 changes: 5 additions & 2 deletions src/test/name_mempool_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,15 @@ BOOST_FIXTURE_TEST_CASE (mempool_sanity_check, NameMempoolTestSetup)
mempool.addUnchecked (Entry (Tx (UpdateScript (ADDR, "upd", "x"))));
mempool.addUnchecked (Entry (Tx (UpdateScript (ADDR, "upd", "y"))));

CCoinsViewCache view(&::ChainstateActive ().CoinsTip ());
ChainstateManager& chainman = g_chainman;
CCoinsViewCache view(&chainman.ActiveChainstate ().CoinsTip ());

const CNameScript nameOp(UpdateScript (ADDR, "upd", "o"));
CNameData data;
data.fromScript (100, COutPoint (uint256 (), 0), nameOp);
view.SetName (Name ("upd"), data, false);
mempool.checkNames (&view);

mempool.checkNames (chainman, &view);
}

namespace
Expand Down
4 changes: 2 additions & 2 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,14 @@ bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockF
return WriteBatch(batch, true);
}

bool CCoinsViewDB::ValidateNameDB() const
bool CCoinsViewDB::ValidateNameDB(ChainstateManager& chainman) const
{
const uint256 blockHash = GetBestBlock();
int nHeight;
if (blockHash.IsNull())
nHeight = 0;
else
nHeight = ::BlockIndex ().find(blockHash)->second->nHeight;
nHeight = chainman.BlockIndex ().find(blockHash)->second->nHeight;

/* It seems that there are no "const iterators" for LevelDB. Since we
only need read operations on it, use a const-cast to get around
Expand Down
2 changes: 1 addition & 1 deletion src/txdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CCoinsViewDB final : public CCoinsView
CNameIterator* IterateNames() const override;
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, const CNameCache &names) override;
CCoinsViewCursor *Cursor() const override;
bool ValidateNameDB() const override;
bool ValidateNameDB(ChainstateManager& chainman) const override;

//! Attempt to update from an older database format. Returns whether an error occurred.
bool Upgrade();
Expand Down
10 changes: 6 additions & 4 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,8 @@ static void CheckInputsAndUpdateCoins(const CTransaction& tx, CCoinsViewCache& m
UpdateCoins(tx, mempoolDuplicate, std::numeric_limits<int>::max());
}

void CTxMemPool::check(const CCoinsViewCache *pcoins) const
void CTxMemPool::check(ChainstateManager& chainman,
const CCoinsViewCache *pcoins) const
{
LOCK(cs);
if (nCheckFrequency == 0)
Expand Down Expand Up @@ -750,12 +751,13 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins) const
assert(totalTxSize == checkTotal);
assert(innerUsage == cachedInnerUsage);

checkNames(pcoins);
checkNames(chainman, pcoins);
}

void CTxMemPool::checkNames(const CCoinsViewCache *pcoins) const
void CTxMemPool::checkNames(ChainstateManager& chainman,
const CCoinsViewCache *pcoins) const
{
names.check (*pcoins);
names.check (chainman, *pcoins);
}

bool CTxMemPool::CompareDepthAndScore(const uint256& hasha, const uint256& hashb)
Expand Down
6 changes: 4 additions & 2 deletions src/txmempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,10 @@ class CTxMemPool
* all inputs are in the mapNextTx array). If sanity-checking is turned off,
* check does nothing.
*/
void check(const CCoinsViewCache *pcoins) const;
void checkNames(const CCoinsViewCache *pcoins) const;
void check(ChainstateManager& chainman,
const CCoinsViewCache *pcoins) const;
void checkNames(ChainstateManager& chainman,
const CCoinsViewCache *pcoins) const;

void setSanityCheck(double dFrequency = 1.0) { LOCK(cs); nCheckFrequency = static_cast<uint32_t>(dFrequency * 4294967295.0); }

Expand Down
10 changes: 5 additions & 5 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2534,7 +2534,7 @@ bool CChainState::DisconnectTip(BlockValidationState& state, const CChainParams&
{
CBlockIndex *pindexDelete = m_chain.Tip();
assert(pindexDelete);
CheckNameDB (true);
CheckNameDB (g_chainman, true);
// Read block from disk.
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
CBlock& block = *pblock;
Expand Down Expand Up @@ -2577,7 +2577,7 @@ bool CChainState::DisconnectTip(BlockValidationState& state, const CChainParams&
m_chain.SetTip(pindexDelete->pprev);

UpdateTip(pindexDelete->pprev, chainparams);
CheckNameDB (true);
CheckNameDB (g_chainman, true);
// Let wallets know transactions went from 1-confirmed to
// 0-confirmed or conflicted:
GetMainSignals().BlockDisconnected(pblock, pindexDelete);
Expand Down Expand Up @@ -2639,7 +2639,7 @@ class ConnectTrace {
bool CChainState::ConnectTip(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions &disconnectpool)
{
assert(pindexNew->pprev == m_chain.Tip());
CheckNameDB (true);
CheckNameDB (g_chainman, true);
// Read block from disk.
int64_t nTime1 = GetTimeMicros();
std::shared_ptr<const CBlock> pthisBlock;
Expand Down Expand Up @@ -2686,7 +2686,7 @@ bool CChainState::ConnectTip(BlockValidationState& state, const CChainParams& ch
// Update m_chain & related variables.
m_chain.SetTip(pindexNew);
UpdateTip(pindexNew, chainparams);
CheckNameDB (false);
CheckNameDB (g_chainman, false);

int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
LogPrint(BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime6 - nTime5) * MILLI, nTimePostConnect * MICRO, nTimePostConnect * MILLI / nBlocksTotal);
Expand Down Expand Up @@ -2850,7 +2850,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
// any disconnected transactions back to the mempool.
UpdateMempoolForReorg(disconnectpool, true);
}
mempool.check(&CoinsTip());
mempool.check(g_chainman, &CoinsTip());

// Callbacks/notifications for a new best chain.
if (fInvalidFound)
Expand Down

0 comments on commit 55f4edb

Please sign in to comment.