From 141e583ccec779b73a1608e1f54049b0c607a963 Mon Sep 17 00:00:00 2001 From: Brannon King Date: Tue, 7 May 2019 16:32:22 -0600 Subject: [PATCH] code reuse between miner & validator originally from BvbFan --- CMakeLists.txt | 2 +- src/Makefile.am | 2 + src/claimscriptop.cpp | 211 +++++++++++++++++++++++++++++++++++ src/claimscriptop.h | 250 ++++++++++++++++++++++++++++++++++++++++++ src/claimtrie.cpp | 47 +++----- src/miner.cpp | 93 +--------------- src/validation.cpp | 214 ++++-------------------------------- 7 files changed, 509 insertions(+), 310 deletions(-) create mode 100644 src/claimscriptop.cpp create mode 100644 src/claimscriptop.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 52a2c7d91..e42d7ef13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,12 +16,12 @@ file(GLOB sources src/script/*.h src/script/*.cpp src/index/*.h src/index/*.cpp src/interfaces/*.h src/interfaces/*.cpp - src/rpc/*.h src/rpc/*.cpp src/primitives/*.h src/primitives/*.cpp src/policy/*.h src/policy/*.cpp src/crypto/*.h src/crypto/*.cpp src/consensus/*.h src/consensus/*.cpp src/compat/*.h src/compat/*.cpp + src/rpc/*.h src/rpc/*.cpp ) list(FILTER sources EXCLUDE REGEX "src/bitcoin*.cpp$") diff --git a/src/Makefile.am b/src/Makefile.am index 7981d5926..d7e2890b5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -102,6 +102,7 @@ BITCOIN_CORE_H = \ chainparamsseeds.h \ checkpoints.h \ checkqueue.h \ + claimscriptop.h \ claimtrie.h \ clientversion.h \ coins.h \ @@ -226,6 +227,7 @@ libbitcoin_server_a_SOURCES = \ blockencodings.cpp \ chain.cpp \ checkpoints.cpp \ + claimscriptop.cpp \ claimtrie.cpp \ claimtrieforks.cpp \ consensus/tx_verify.cpp \ diff --git a/src/claimscriptop.cpp b/src/claimscriptop.cpp new file mode 100644 index 000000000..3af804fe2 --- /dev/null +++ b/src/claimscriptop.cpp @@ -0,0 +1,211 @@ +// Copyright (c) 2018 The LBRY developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "claimscriptop.h" +#include "nameclaim.h" + +CClaimScriptAddOp::CClaimScriptAddOp(const COutPoint& point, CAmount nValue, int nHeight) + : point(point), nValue(nValue), nHeight(nHeight) +{ +} + +bool CClaimScriptAddOp::claimName(CClaimTrieCache& trieCache, const std::string& name) +{ + return addClaim(trieCache, name, ClaimIdHash(point.hash, point.n)); +} + +bool CClaimScriptAddOp::updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) +{ + return addClaim(trieCache, name, claimId); +} + +bool CClaimScriptAddOp::addClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) +{ + return trieCache.addClaim(name, point, claimId, nValue, nHeight); +} + +bool CClaimScriptAddOp::supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) +{ + return trieCache.addSupport(name, point, nValue, claimId, nHeight); +} + +CClaimScriptUndoAddOp::CClaimScriptUndoAddOp(const COutPoint& point, int nHeight) : point(point), nHeight(nHeight) +{ +} + +bool CClaimScriptUndoAddOp::claimName(CClaimTrieCache& trieCache, const std::string& name) +{ + auto claimId = ClaimIdHash(point.hash, point.n); + LogPrintf("--- [%lu]: OP_CLAIM_NAME \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); + return undoAddClaim(trieCache, name, claimId); +} + +bool CClaimScriptUndoAddOp::updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) +{ + LogPrintf("--- [%lu]: OP_UPDATE_CLAIM \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); + return undoAddClaim(trieCache, name, claimId); +} + +bool CClaimScriptUndoAddOp::undoAddClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) +{ + LogPrintf("%s: (txid: %s, nOut: %d) Removing %s, claimId: %s, from the claim trie due to block disconnect\n", __func__, point.hash.ToString(), point.n, name, claimId.ToString()); + bool res = trieCache.undoAddClaim(name, point, nHeight); + if (!res) + LogPrintf("%s: Removing fails\n", __func__); + return res; +} + +bool CClaimScriptUndoAddOp::supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) +{ + LogPrintf("--- [%lu]: OP_SUPPORT_CLAIM \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); + LogPrintf("%s: (txid: %s, nOut: %d) Removing support for %s, claimId: %s, from the claim trie due to block disconnect\n", __func__, point.hash.ToString(), point.n, name, claimId.ToString()); + bool res = trieCache.undoAddSupport(name, point, nHeight); + if (!res) + LogPrintf("%s: Removing support fails\n", __func__); + return res; +} + +CClaimScriptSpendOp::CClaimScriptSpendOp(const COutPoint& point, int nHeight, int& nValidHeight) + : point(point), nHeight(nHeight), nValidHeight(nValidHeight) +{ +} + +bool CClaimScriptSpendOp::claimName(CClaimTrieCache& trieCache, const std::string& name) +{ + auto claimId = ClaimIdHash(point.hash, point.n); + LogPrintf("+++ [%lu]: OP_CLAIM_NAME \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); + return spendClaim(trieCache, name, claimId); +} + +bool CClaimScriptSpendOp::updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) +{ + LogPrintf("+++ [%lu]: OP_UPDATE_CLAIM \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); + return spendClaim(trieCache, name, claimId); +} + +bool CClaimScriptSpendOp::spendClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) +{ + LogPrintf("%s: (txid: %s, nOut: %d) Removing %s, claimId: %s, from the claim trie\n", __func__, point.hash.ToString(), point.n, name, claimId.ToString()); + bool res = trieCache.spendClaim(name, point, nHeight, nValidHeight); + if (!res) + LogPrintf("%s: Removing fails\n", __func__); + return res; +} + +bool CClaimScriptSpendOp::supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) +{ + LogPrintf("+++ [%lu]: OP_SUPPORT_CLAIM \"%s\" with claimId %s and tx prevout %s at index %d\n", nHeight, name, claimId.GetHex(), point.hash.ToString(), point.n); + LogPrintf("%s: (txid: %s, nOut: %d) Restoring support for %s, claimId: %s, to the claim trie\n", __func__, point.hash.ToString(), point.n, name, claimId.ToString()); + bool res = trieCache.spendSupport(name, point, nHeight, nValidHeight); + if (!res) + LogPrintf("%s: Removing support fails\n", __func__); + return res; +} + +CClaimScriptUndoSpendOp::CClaimScriptUndoSpendOp(const COutPoint& point, CAmount nValue, int nHeight, int nValidHeight) + : point(point), nValue(nValue), nHeight(nHeight), nValidHeight(nValidHeight) +{ +} + +bool CClaimScriptUndoSpendOp::claimName(CClaimTrieCache& trieCache, const std::string& name) +{ + return undoSpendClaim(trieCache, name, ClaimIdHash(point.hash, point.n)); +} + +bool CClaimScriptUndoSpendOp::updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) +{ + return undoSpendClaim(trieCache, name, claimId); +} + +bool CClaimScriptUndoSpendOp::undoSpendClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) +{ + LogPrintf("%s: (txid: %s, nOut: %d) Restoring %s, claimId: %s, to the claim trie due to block disconnect\n", __func__, point.hash.ToString(), point.n, name, claimId.ToString()); + return trieCache.undoSpendClaim(name, point, claimId, nValue, nHeight, nValidHeight); +} + +bool CClaimScriptUndoSpendOp::supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) +{ + LogPrintf("%s: (txid: %s, nOut: %d) Restoring support for %s, claimId: %s, to the claim trie due to block disconnect\n", __func__, point.hash.ToString(), point.n, name, claimId.ToString()); + return trieCache.undoSpendSupport(name, point, claimId, nValue, nHeight, nValidHeight); +} + +static std::string vchToString(const std::vector& name) +{ + return std::string(name.begin(), name.end()); +} + +bool ProcessClaim(CClaimScriptOp& claimOp, CClaimTrieCache& trieCache, const CScript& scriptPubKey) +{ + int op; + std::vector > vvchParams; + if (!DecodeClaimScript(scriptPubKey, op, vvchParams)) + return false; + + switch (op) { + case OP_CLAIM_NAME: + return claimOp.claimName(trieCache, vchToString(vvchParams[0])); + case OP_SUPPORT_CLAIM: + return claimOp.supportClaim(trieCache, vchToString(vvchParams[0]), uint160(vvchParams[1])); + case OP_UPDATE_CLAIM: + return claimOp.updateClaim(trieCache, vchToString(vvchParams[0]), uint160(vvchParams[1])); + } + throw std::runtime_error("Unimplemented OP handler."); +} + +bool SpendClaim(CClaimTrieCache& trieCache, const CScript& scriptPubKey, const COutPoint& point, int nHeight, int& nValidHeight, spentClaimsType& spentClaims) +{ + class CSpendClaimHistory : public CClaimScriptSpendOp + { + public: + CSpendClaimHistory(spentClaimsType& spentClaims, const COutPoint& point, int nHeight, int& nValidHeight) + : CClaimScriptSpendOp(point, nHeight, nValidHeight), spentClaims(spentClaims) + { + } + + bool spendClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) override + { + if (CClaimScriptSpendOp::spendClaim(trieCache, name, claimId)) { + spentClaims.emplace_back(name, claimId); + return true; + } + return false; + } + + private: + spentClaimsType& spentClaims; + }; + + CSpendClaimHistory spendClaim(spentClaims, point, nHeight, nValidHeight); + return ProcessClaim(spendClaim, trieCache, scriptPubKey); +} + +bool AddSpendClaim(CClaimTrieCache& trieCache, const CScript& scriptPubKey, const COutPoint& point, CAmount nValue, int nHeight, spentClaimsType& spentClaims) +{ + class CAddSpendClaim : public CClaimScriptAddOp + { + public: + CAddSpendClaim(spentClaimsType& spentClaims, const COutPoint& point, CAmount nValue, int nHeight) + : CClaimScriptAddOp(point, nValue, nHeight), spentClaims(spentClaims) + { + } + + bool updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) override + { + spentClaimsType::iterator itSpent = spentClaims.begin(); + for (; itSpent != spentClaims.end(); ++itSpent) { + if (itSpent->second == claimId && trieCache.normalizeClaimName(name) == trieCache.normalizeClaimName(itSpent->first)) { + spentClaims.erase(itSpent); + return CClaimScriptAddOp::updateClaim(trieCache, name, claimId); + } + } + return false; + } + + private: + spentClaimsType& spentClaims; + }; + + CAddSpendClaim addClaim(spentClaims, point, nValue, nHeight); + return ProcessClaim(addClaim, trieCache, scriptPubKey); +} \ No newline at end of file diff --git a/src/claimscriptop.h b/src/claimscriptop.h new file mode 100644 index 000000000..0502093dc --- /dev/null +++ b/src/claimscriptop.h @@ -0,0 +1,250 @@ +// Copyright (c) 2018 The LBRY developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef CLAIMSCRIPTOP_H +#define CLAIMSCRIPTOP_H + +#include "amount.h" +#include "claimtrie.h" +#include "hash.h" +#include "primitives/transaction.h" +#include "script/script.h" +#include "uint256.h" +#include "util.h" + +#include +#include + +/** + * Claim script operation base class + */ +class CClaimScriptOp +{ +public: + virtual ~CClaimScriptOp() {} + /** + * Pure virtual, OP_CLAIM_NAME handler + * @param[in] trieCache trie to operate on + * @param[in] name name of the claim + */ + virtual bool claimName(CClaimTrieCache& trieCache, const std::string& name) = 0; + /** + * Pure virtual, OP_UPDATE_CLAIM handler + * @param[in] trieCache trie to operate on + * @param[in] name name of the claim + * @param[in] claimId id of the claim + */ + virtual bool updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) = 0; + /** + * Pure virtual, OP_SUPPORT_CLAIM handler + * @param[in] trieCache trie to operate on + * @param[in] name name of the claim + * @param[in] claimId id of the claim + */ + virtual bool supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) = 0; +}; + +/** + * Class to add claim in trie + */ +class CClaimScriptAddOp : public CClaimScriptOp +{ +public: + /** + * Constructor + * @param[in] point pair of transaction hash and its index + * @param[in] nValue value of the claim + * @param[in] nHeight entry height of the claim + */ + CClaimScriptAddOp(const COutPoint& point, CAmount nValue, int nHeight); + /** + * Implamention of OP_CLAIM_NAME handler + * @see CClaimScriptOp::claimName + */ + bool claimName(CClaimTrieCache& trieCache, const std::string& name) override; + /** + * Implamention of OP_UPDATE_CLAIM handler + * @see CClaimScriptOp::updateClaim + */ + bool updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) override; + /** + * Implamention of OP_SUPPORT_CLAIM handler + * @see CClaimScriptOp::supportClaim + */ + bool supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) override; + +protected: + /** + * Reimplement to handle OP_CLAIM_NAME and OP_UPDATE_CLAIM at once + * @param[in] trieCache trie to operate on + * @param[in] name name of the claim + * @param[in] claimId id of the claim + */ + virtual bool addClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId); + const COutPoint point; + const CAmount nValue; + const int nHeight; +}; + +/** + * Class to undo added claim in trie + */ +class CClaimScriptUndoAddOp : public CClaimScriptOp +{ +public: + /** + * Constructor + * @param[in] point pair of transaction hash and its index + * @param[in] nHeight entry height of the claim + */ + CClaimScriptUndoAddOp(const COutPoint& point, int nHeight); + /** + * Implamention of OP_CLAIM_NAME handler + * @see CClaimScriptOp::claimName + */ + bool claimName(CClaimTrieCache& trieCache, const std::string& name) override; + /** + * Implamention of OP_UPDATE_CLAIM handler + * @see CClaimScriptOp::updateClaim + */ + bool updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) override; + /** + * Implamention of OP_SUPPORT_CLAIM handler + * @see CClaimScriptOp::supportClaim + */ + bool supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) override; + +protected: + /** + * Reimplement to handle OP_CLAIM_NAME and OP_UPDATE_CLAIM at once + * @param[in] trieCache trie to operate on + * @param[in] name name of the claim + * @param[in] claimId id of the claim + */ + virtual bool undoAddClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId); + const COutPoint point; + const int nHeight; +}; + +/** + * Class to spend claim from trie + */ +class CClaimScriptSpendOp : public CClaimScriptOp +{ +public: + /** + * Constructor + * @param[in] point pair of transaction hash and its index + * @param[in] nHeight entry height of the claim + * @param[out] nValidHeight valid height of the claim + */ + CClaimScriptSpendOp(const COutPoint& point, int nHeight, int& nValidHeight); + /** + * Implamention of OP_CLAIM_NAME handler + * @see CClaimScriptOp::claimName + */ + bool claimName(CClaimTrieCache& trieCache, const std::string& name) override; + /** + * Implamention of OP_UPDATE_CLAIM handler + * @see CClaimScriptOp::updateClaim + */ + bool updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) override; + /** + * Implamention of OP_SUPPORT_CLAIM handler + * @see CClaimScriptOp::supportClaim + */ + bool supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) override; + +protected: + /** + * Reimplement to handle OP_CLAIM_NAME and OP_UPDATE_CLAIM at once + * @param[in] trieCache trie to operate on + * @param[in] name name of the claim + * @param[in] claimId id of the claim + */ + virtual bool spendClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId); + const COutPoint point; + const int nHeight; + int& nValidHeight; +}; + +/** + * Class to undo spent claim from trie + */ +class CClaimScriptUndoSpendOp : public CClaimScriptOp +{ +public: + /** + * Constructor + * @param[in] point pair of transaction hash and its index + * @param[in] nValue value of the claim + * @param[in] nHeight entry height of the claim + * @param[in] nValidHeight valid height of the claim + */ + CClaimScriptUndoSpendOp(const COutPoint& point, CAmount nValue, int nHeight, int nValidHeight); + /** + * Implamention of OP_CLAIM_NAME handler + * @see CClaimScriptOp::claimName + */ + bool claimName(CClaimTrieCache& trieCache, const std::string& name) override; + /** + * Implamention of OP_UPDATE_CLAIM handler + * @see CClaimScriptOp::updateClaim + */ + bool updateClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) override; + /** + * Implamention of OP_SUPPORT_CLAIM handler + * @see CClaimScriptOp::supportClaim + */ + bool supportClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId) override; + +protected: + /** + * Reimplement to handle OP_CLAIM_NAME and OP_UPDATE_CLAIM at once + * @param[in] trieCache trie to operate on + * @param[in] name name of the claim + * @param[in] claimId id of the claim + */ + virtual bool undoSpendClaim(CClaimTrieCache& trieCache, const std::string& name, const uint160& claimId); + const COutPoint point; + const CAmount nValue; + const int nHeight; + const int nValidHeight; +}; + +/** + * Function to process operation on claim + * @param[in] claimOp operation to be performed + * @param[in] trieCache trie to operate on + * @param[in] scriptPubKey claim script to be decoded + */ +bool ProcessClaim(CClaimScriptOp& claimOp, CClaimTrieCache& trieCache, const CScript& scriptPubKey); + +typedef std::pair spentClaimType; + +typedef std::vector spentClaimsType; + +/** + * Function to spend claim from tie, keeping the successful list on + * @param[in] trieCache trie to operate on + * @param[in] scriptPubKey claim script to be decoded + * @param[in] point pair of transaction hash and its index + * @param[in] nHeight entry height of the claim + * @param[out] nValidHeight valid height of the claim + * @param[out] spentClaims inserts successfully spent claim + */ +bool SpendClaim(CClaimTrieCache& trieCache, const CScript& scriptPubKey, const COutPoint& point, int nHeight, int& nValidHeight, spentClaimsType& spentClaims); + +/** + * Function to add / update (that present in spent list) claim in trie + * @param[in] trieCache trie to operate on + * @param[in] scriptPubKey claim script to be decoded + * @param[in] point pair of transaction hash and its index + * @param[in] nValue ` value of the claim + * @param[in] nHeight entry height of the claim + * @param[out] spentClaims erases successfully added claim + */ +bool AddSpendClaim(CClaimTrieCache& trieCache, const CScript& scriptPubKey, const COutPoint& point, CAmount nValue, int nHeight, spentClaimsType& spentClaims); + +#endif // CLAIMSCRIPTOP_H diff --git a/src/claimtrie.cpp b/src/claimtrie.cpp index b63c00296..328ed62b9 100644 --- a/src/claimtrie.cpp +++ b/src/claimtrie.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include #include @@ -27,34 +29,21 @@ std::vector heightToVch(int n) uint256 getValueHash(COutPoint outPoint, int nHeightOfLastTakeover) { - CHash256 txHasher; - txHasher.Write(outPoint.hash.begin(), outPoint.hash.size()); - std::vector vchtxHash(txHasher.OUTPUT_SIZE); - txHasher.Finalize(&(vchtxHash[0])); - - CHash256 nOutHasher; - std::stringstream ss; - ss << outPoint.n; - std::string snOut = ss.str(); - nOutHasher.Write((unsigned char*) snOut.data(), snOut.size()); - std::vector vchnOutHash(nOutHasher.OUTPUT_SIZE); - nOutHasher.Finalize(&(vchnOutHash[0])); - - CHash256 takeoverHasher; - std::vector vchTakeoverHeightToHash = heightToVch(nHeightOfLastTakeover); - takeoverHasher.Write(vchTakeoverHeightToHash.data(), vchTakeoverHeightToHash.size()); - std::vector vchTakeoverHash(takeoverHasher.OUTPUT_SIZE); - takeoverHasher.Finalize(&(vchTakeoverHash[0])); - CHash256 hasher; - hasher.Write(vchtxHash.data(), vchtxHash.size()); - hasher.Write(vchnOutHash.data(), vchnOutHash.size()); - hasher.Write(vchTakeoverHash.data(), vchTakeoverHash.size()); - std::vector vchHash(hasher.OUTPUT_SIZE); - hasher.Finalize(&(vchHash[0])); + auto hash = Hash(outPoint.hash.begin(), outPoint.hash.end()); + hasher.Write(hash.begin(), hash.size()); - uint256 valueHash(vchHash); - return valueHash; + auto snOut = std::to_string(outPoint.n); + hash = Hash(snOut.begin(), snOut.end()); + hasher.Write(hash.begin(), hash.size()); + + auto vchHash = heightToVch(nHeightOfLastTakeover); + hash = Hash(vchHash.begin(), vchHash.end()); + hasher.Write(hash.begin(), hash.size()); + + uint256 result; + hasher.Finalize(result.begin()); + return result; } bool CClaimTrieNode::insertClaim(CClaimValue claim) @@ -453,11 +442,7 @@ bool CClaimTrie::recursiveCheckConsistency(const CClaimTrieNode* node) const vchToHash.insert(vchToHash.end(), valueHash.begin(), valueHash.end()); } - CHash256 hasher; - std::vector vchHash(hasher.OUTPUT_SIZE); - hasher.Write(vchToHash.data(), vchToHash.size()); - hasher.Finalize(&(vchHash[0])); - uint256 calculatedHash(vchHash); + auto calculatedHash = Hash(vchToHash.begin(), vchToHash.end()); return calculatedHash == node->hash; } diff --git a/src/miner.cpp b/src/miner.cpp index 3e317ce27..35104934c 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -429,7 +430,6 @@ void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpda continue; } - typedef std::vector > spentClaimsType; spentClaimsType spentClaims; const CTransaction& tx = iter->GetTx(); @@ -449,45 +449,9 @@ void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpda scriptPubKey = coin.out.scriptPubKey; } - std::vector > vvchParams; - int op; - - if (DecodeClaimScript(scriptPubKey, op, vvchParams)) - { - std::string name(vvchParams[0].begin(), vvchParams[0].end()); - if (op == OP_CLAIM_NAME || op == OP_UPDATE_CLAIM) - { - uint160 claimId; - if (op == OP_CLAIM_NAME) - { - assert(vvchParams.size() == 2); - claimId = ClaimIdHash(txin.prevout.hash, txin.prevout.n); - } - else if (op == OP_UPDATE_CLAIM) - { - assert(vvchParams.size() == 3); - claimId = uint160(vvchParams[1]); - } - int throwaway; - if (trieCache.spendClaim(name, COutPoint(txin.prevout.hash, txin.prevout.n), coin.nHeight, throwaway)) - { - std::pair entry(name, claimId); - spentClaims.push_back(entry); - } - else - { - LogPrintf("%s(): The claim was not found in the trie or queue and therefore can't be updated\n", __func__); - } - } - else if (op == OP_SUPPORT_CLAIM) - { - assert(vvchParams.size() == 2); - int throwaway; - if (!trieCache.spendSupport(name, COutPoint(txin.prevout.hash, txin.prevout.n), coin.nHeight, throwaway)) - { - LogPrintf("%s(): The support was not found in the trie or queue\n", __func__); - } - } + if (!scriptPubKey.empty()) { + int throwaway; + SpendClaim(trieCache, scriptPubKey, COutPoint(txin.prevout.hash, txin.prevout.n), coin.nHeight, throwaway, spentClaims); } } @@ -495,53 +459,8 @@ void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpda { const CTxOut& txout = tx.vout[i]; - std::vector > vvchParams; - int op; - if (DecodeClaimScript(txout.scriptPubKey, op, vvchParams)) - { - std::string name(vvchParams[0].begin(), vvchParams[0].end()); - if (op == OP_CLAIM_NAME) - { - assert(vvchParams.size() == 2); - if (!trieCache.addClaim(name, COutPoint(tx.GetHash(), i), ClaimIdHash(tx.GetHash(), i), txout.nValue, nHeight)) - { - LogPrintf("%s: Something went wrong inserting the name\n", __func__); - } - } - else if (op == OP_UPDATE_CLAIM) - { - assert(vvchParams.size() == 3); - uint160 claimId(vvchParams[1]); - spentClaimsType::iterator itSpent; - for (itSpent = spentClaims.begin(); itSpent != spentClaims.end(); ++itSpent) - { - if (itSpent->second == claimId && - trieCache.normalizeClaimName(name) == trieCache.normalizeClaimName(itSpent->first)) - break; - } - if (itSpent != spentClaims.end()) - { - spentClaims.erase(itSpent); - if (!trieCache.addClaim(name, COutPoint(tx.GetHash(), i), claimId, txout.nValue, nHeight)) - { - LogPrintf("%s: Something went wrong updating a claim\n", __func__); - } - } - else - { - LogPrintf("%s(): This update refers to a claim that was not found in the trie or queue, and therefore cannot be updated. The claim may have expired or it may have never existed.\n", __func__); - } - } - else if (op == OP_SUPPORT_CLAIM) - { - assert(vvchParams.size() == 2); - uint160 supportedClaimId(vvchParams[1]); - if (!trieCache.addSupport(name, COutPoint(tx.GetHash(), i), txout.nValue, supportedClaimId, nHeight)) - { - LogPrintf("%s: Something went wrong inserting the claim support\n", __func__); - } - } - } + if (!txout.scriptPubKey.empty()) + AddSpendClaim(trieCache, txout.scriptPubKey, COutPoint(tx.GetHash(), i), txout.nValue, nHeight, spentClaims); } txs.emplace_back(MakeTransactionRef(tx)); diff --git a/src/validation.cpp b/src/validation.cpp index f53f21ff3..acd57c108 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -24,6 +25,7 @@ #include #include #include +#include #include #include #include