From 112b96b74cea8395f833611af13bcce199d31445 Mon Sep 17 00:00:00 2001 From: Jianping Wu Date: Sat, 3 Nov 2018 13:42:50 -0700 Subject: [PATCH] Fixed namespace/key update checking. --- src/keva/main.cpp | 64 +++++++++++++----------------------------- src/keva/main.h | 15 +++++----- src/txmempool.h | 4 +-- src/wallet/rpckeva.cpp | 2 +- 4 files changed, 29 insertions(+), 56 deletions(-) diff --git a/src/keva/main.cpp b/src/keva/main.cpp index 594447760..8f717d711 100644 --- a/src/keva/main.cpp +++ b/src/keva/main.cpp @@ -45,28 +45,6 @@ CKevaTxUndo::apply(CCoinsViewCache& view) const /* ************************************************************************** */ /* CKevaMemPool. */ -uint256 -CKevaMemPool::getTxForNamespace(const valtype& nameSpace) const -{ -#if 0 - // JWU TODO: implement this! - NamespaceTxMap::const_iterator mi; - - mi = mapNamespaceRegs.find(nameSpace); - if (mi != mapNamespaceRegs.end()) { - assert(mapNamespaceKeyUpdates.count(name) == 0); - return mi->second; - } - - mi = mapNamespaceKeyUpdates.find(name); - if (mi != mapNameUpdates.end ()) { - assert (mapNameRegs.count (name) == 0); - return mi->second; - } -#endif - return uint256(); -} - void CKevaMemPool::addUnchecked (const uint256& hash, const CTxMemPoolEntry& entry) { @@ -80,10 +58,8 @@ CKevaMemPool::addUnchecked (const uint256& hash, const CTxMemPoolEntry& entry) if (entry.isNamespaceKeyUpdate ()) { const valtype& nameSpace = entry.getNamespace(); - const valtype& key = entry.getKey(); - NamespaceKeyTuple tuple(nameSpace, key); - assert(mapNamespaceKeyUpdates.count(tuple) == 0); - mapNamespaceKeyUpdates.insert (std::make_pair(tuple, hash)); + assert(mapNamespaceUpdates.count(nameSpace) == 0); + mapNamespaceUpdates.insert (std::make_pair(nameSpace, hash)); } } @@ -92,17 +68,16 @@ CKevaMemPool::remove (const CTxMemPoolEntry& entry) { AssertLockHeld (pool.cs); - if (entry.isNamespaceRegistration ()) { - const NamespaceTxMap::iterator mit = mapNamespaceRegs.find (entry.getNamespace()); + if (entry.isNamespaceRegistration()) { + const NamespaceTxMap::iterator mit = mapNamespaceRegs.find(entry.getNamespace()); assert (mit != mapNamespaceRegs.end()); - mapNamespaceRegs.erase (mit); + mapNamespaceRegs.erase(mit); } - if (entry.isNamespaceKeyUpdate ()) { - NamespaceKeyTuple tuple(entry.getNamespace(), entry.getKey()); - const NamespaceKeyTxMap::iterator mit = mapNamespaceKeyUpdates.find(tuple); - assert (mit != mapNamespaceKeyUpdates.end()); - mapNamespaceKeyUpdates.erase (mit); + if (entry.isNamespaceKeyUpdate()) { + const NamespaceTxMap::iterator mit = mapNamespaceUpdates.find(entry.getNamespace()); + assert (mit != mapNamespaceUpdates.end()); + mapNamespaceUpdates.erase(mit); } } @@ -143,7 +118,7 @@ CKevaMemPool::check(const CCoinsView& coins) const nHeight = mapBlockIndex.find (blockHash)->second->nHeight; std::set nameRegs; - std::set> namespaceKeyUpdates; + std::set namespaceKeyUpdates; for (const auto& entry : pool.mapTx) { const uint256 txHash = entry.GetTx ().GetHash (); if (entry.isNamespaceRegistration()) { @@ -167,14 +142,12 @@ CKevaMemPool::check(const CCoinsView& coins) const if (entry.isNamespaceKeyUpdate()) { const valtype& nameSpace = entry.getNamespace(); - const valtype& key = entry.getKey(); - std::tuple tuple(nameSpace, key); - const NamespaceKeyTxMap::const_iterator mit = mapNamespaceKeyUpdates.find(tuple); - assert (mit != mapNamespaceKeyUpdates.end ()); + const NamespaceTxMap::const_iterator mit = mapNamespaceUpdates.find(nameSpace); + assert (mit != mapNamespaceUpdates.end ()); assert (mit->second == txHash); - assert (namespaceKeyUpdates.count(tuple) == 0); - namespaceKeyUpdates.insert(tuple); + assert (namespaceKeyUpdates.count(nameSpace) == 0); + namespaceKeyUpdates.insert(nameSpace); #if 0 /* As above, use nHeight+1 for the expiration check. */ @@ -188,7 +161,7 @@ CKevaMemPool::check(const CCoinsView& coins) const } assert(nameRegs.size() == mapNamespaceRegs.size()); - assert(namespaceKeyUpdates.size() == mapNamespaceKeyUpdates.size()); + assert(namespaceKeyUpdates.size() == mapNamespaceUpdates.size()); /* Check that nameRegs and nameUpdates are disjoint. They must be since a name can only be in either category, depending on whether it exists @@ -200,8 +173,8 @@ CKevaMemPool::check(const CCoinsView& coins) const } #endif - for (const auto& namespaceKey : namespaceKeyUpdates) { - assert(nameRegs.count(std::get<0>(namespaceKey)) == 0); + for (const auto& nameSpace : namespaceKeyUpdates) { + assert(nameRegs.count(nameSpace) == 0); } } @@ -238,8 +211,9 @@ CKevaMemPool::checkTx (const CTransaction& tx) const { const valtype& nameSpace = nameOp.getOpNamespace(); const valtype& key = nameOp.getOpKey(); - if (updatesKey(nameSpace, key)) + if (updatesNamespace(nameSpace)) { return false; + } break; } diff --git a/src/keva/main.h b/src/keva/main.h index cde312764..8ba9ce187 100644 --- a/src/keva/main.h +++ b/src/keva/main.h @@ -134,7 +134,7 @@ private: * Keep track of key that are updated by transactions in the pool. * Map key to registering transaction. */ - NamespaceKeyTxMap mapNamespaceKeyUpdates; + NamespaceTxMap mapNamespaceUpdates; public: @@ -160,15 +160,14 @@ public: } /** - * Check whether a particular name has a pending update. Does not lock. - * @param name The name to check for. - * @return True iff there's a matching name update in the pool. + * Check whether a particular namespace has a pending update. Does not lock. + * @param name The namespace to check for. + * @return True iff there's a matching namespace update in the pool. */ inline bool - updatesKey (const valtype& nameSpace, const valtype& key) const + updatesNamespace(const valtype& nameSpace) const { - NamespaceKeyTuple tuple(nameSpace, key); - return mapNamespaceKeyUpdates.count(tuple) > 0; + return mapNamespaceUpdates.count(nameSpace) > 0; } /** @@ -188,7 +187,7 @@ public: clear () { mapNamespaceRegs.clear(); - mapNamespaceKeyUpdates.clear(); + mapNamespaceUpdates.clear(); } /** diff --git a/src/txmempool.h b/src/txmempool.h index 314866afa..d52e671a7 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -671,10 +671,10 @@ public: return kevaMemPool.registersNamespace(nameSpace); } - inline bool updatesKey(const valtype& nameSpace, const valtype& key) const + inline bool updatesNamespace(const valtype& nameSpace) const { AssertLockHeld(cs); - return kevaMemPool.updatesKey(nameSpace, key); + return kevaMemPool.updatesNamespace(nameSpace); } inline uint256 getTxForNamespace(const valtype& name) const diff --git a/src/wallet/rpckeva.cpp b/src/wallet/rpckeva.cpp index d428dcb81..74d65d5ee 100644 --- a/src/wallet/rpckeva.cpp +++ b/src/wallet/rpckeva.cpp @@ -232,7 +232,7 @@ UniValue keva_put(const JSONRPCRequest& request) rules, but it is necessary with the current mempool implementation. */ { LOCK (mempool.cs); - if (mempool.updatesKey(nameSpace, key)) { + if (mempool.updatesNamespace(nameSpace)) { throw JSONRPCError (RPC_TRANSACTION_ERROR, "there is already a pending update for this name"); }