From 60c19c1186862cfc8af81f715b966a261f26ce03 Mon Sep 17 00:00:00 2001 From: Jianping Wu Date: Mon, 19 Nov 2018 17:51:57 -0800 Subject: [PATCH] Validated unique namespace id. --- src/keva/main.cpp | 40 ++++++++++++++++++++++++---------------- src/keva/main.h | 5 +++++ src/script/keva.cpp | 1 - src/wallet/rpckeva.cpp | 2 +- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/keva/main.cpp b/src/keva/main.cpp index 8f717d711..c32b66e10 100644 --- a/src/keva/main.cpp +++ b/src/keva/main.cpp @@ -82,27 +82,26 @@ CKevaMemPool::remove (const CTxMemPoolEntry& entry) } void -CKevaMemPool::removeConflicts (const CTransaction& tx) +CKevaMemPool::removeConflicts(const CTransaction& tx) { AssertLockHeld (pool.cs); if (!tx.IsKevacoin ()) return; - for (const auto& txout : tx.vout) - { - const CKevaScript nameOp(txout.scriptPubKey); - if (nameOp.isKevaOp () && nameOp.getKevaOp () == OP_KEVA_PUT) { - const valtype& nameSpace = nameOp.getOpNamespace(); - const NamespaceTxMap::const_iterator mit = mapNamespaceRegs.find(nameSpace); - if (mit != mapNamespaceRegs.end ()) { - const CTxMemPool::txiter mit2 = pool.mapTx.find (mit->second); - assert (mit2 != pool.mapTx.end ()); - pool.removeRecursive (mit2->GetTx (), - MemPoolRemovalReason::KEVA_CONFLICT); - } + for (const auto& txout : tx.vout) { + const CKevaScript nameOp(txout.scriptPubKey); + if (nameOp.isKevaOp() && nameOp.getKevaOp() == OP_KEVA_PUT) { + const valtype& nameSpace = nameOp.getOpNamespace(); + const NamespaceTxMap::const_iterator mit = mapNamespaceRegs.find(nameSpace); + if (mit != mapNamespaceRegs.end()) { + const CTxMemPool::txiter mit2 = pool.mapTx.find(mit->second); + assert(mit2 != pool.mapTx.end()); + pool.removeRecursive (mit2->GetTx(), + MemPoolRemovalReason::KEVA_CONFLICT); } } + } } void @@ -178,6 +177,13 @@ CKevaMemPool::check(const CCoinsView& coins) const } } +bool CKevaMemPool::validateNamespace(const CTransaction& tx, const valtype& nameSpace) const +{ + valtype kevaNamespace = ToByteVector(Hash160(ToByteVector(tx.vin[0].prevout.hash))); + kevaNamespace.insert(kevaNamespace.begin(), CKevaScript::NAMESPACE_PREFIX); + return kevaNamespace == nameSpace; +} + bool CKevaMemPool::checkTx (const CTransaction& tx) const { @@ -190,20 +196,22 @@ CKevaMemPool::checkTx (const CTransaction& tx) const mempool at once (building upon each other). This is disallowed, though, since the current mempool implementation does not like it. (We keep track of only a single update tx for each name.) */ - for (const auto& txout : tx.vout) { const CKevaScript nameOp(txout.scriptPubKey); if (!nameOp.isKevaOp ()) continue; - switch (nameOp.getKevaOp ()) { case OP_KEVA_NAMESPACE: { const valtype& nameSpace = nameOp.getOpNamespace(); std::map::const_iterator mi; mi = mapNamespaceRegs.find(nameSpace); - if (mi != mapNamespaceRegs.end ()) + if (mi != mapNamespaceRegs.end ()) { return false; + } + if (!validateNamespace(tx, nameSpace)) { + return false; + } break; } diff --git a/src/keva/main.h b/src/keva/main.h index 8ba9ce187..3e13f6439 100644 --- a/src/keva/main.h +++ b/src/keva/main.h @@ -136,6 +136,11 @@ private: */ NamespaceTxMap mapNamespaceUpdates; + /** + * Validate that the namespace is the hash of the first TxIn. + */ + bool validateNamespace(const CTransaction& tx, const valtype& nameSpace) const; + public: /** diff --git a/src/script/keva.cpp b/src/script/keva.cpp index b6814535c..c3fd3cb4c 100644 --- a/src/script/keva.cpp +++ b/src/script/keva.cpp @@ -98,7 +98,6 @@ CScript CKevaScript::replaceKevaNamespace(const CScript& oldScript, const uint25 const valtype& displayName = kevaOp.getOpNamespaceDisplayName(); kaveNamespace = ToByteVector(Hash160(ToByteVector(txId))); kaveNamespace.insert(kaveNamespace.begin(), NAMESPACE_PREFIX); - //kaveNamespace = EncodeBase58(kaveNamespaceVal); return CKevaScript::buildKevaNamespace(kevaOp.getAddress(), kaveNamespace, displayName); } diff --git a/src/wallet/rpckeva.cpp b/src/wallet/rpckeva.cpp index 24a118181..c5d51b5f1 100644 --- a/src/wallet/rpckeva.cpp +++ b/src/wallet/rpckeva.cpp @@ -72,7 +72,7 @@ UniValue keva_namespace(const JSONRPCRequest& request) CKeyID keyId = pubKey.GetID(); - // The namespace name is: Hash160("first txin" || displayName) + // The namespace name is: Hash160("first txin") // For now we don't know the first txin, so use dummy name here. // It will be replaced later in CreateTransaction. valtype namespaceDummy = ToByteVector(std::string(DUMMY_NAMESPACE));