From f1e658a26664abcdc8b24616931a458394c89f9f Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Fri, 19 Jul 2019 18:36:53 +0300 Subject: [PATCH 1/8] Implement binary tree hash algorithm Signed-off-by: Anthony Fieroni -- 2.45.3 From 155b6818b8dd354310589c6fc556f308a11d2f31 Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Thu, 8 Aug 2019 16:39:57 +0300 Subject: [PATCH 2/8] Unify claimtrie rpc methods Signed-off-by: Anthony Fieroni --- src/claimtrie.cpp | 53 ++-- src/claimtrie.h | 90 +++++-- src/claimtrieforks.cpp | 11 +- src/rpc/claimtrie.cpp | 359 +++++++++++++------------- src/test/claimtriebranching_tests.cpp | 142 +++++----- src/test/claimtriecache_tests.cpp | 18 +- 6 files changed, 348 insertions(+), 325 deletions(-) diff --git a/src/claimtrie.cpp b/src/claimtrie.cpp index 4c977e7ba..9963b3731 100644 --- a/src/claimtrie.cpp +++ b/src/claimtrie.cpp @@ -391,7 +391,7 @@ bool CClaimTrieCacheBase::getInfoForName(const std::string& name, CClaimValue& c return base->find(name, claims) && claims.getBestClaim(claim); } -CClaimsForNameType CClaimTrieCacheBase::getClaimsForName(const std::string& name) const +CClaimSupportToName CClaimTrieCacheBase::getClaimsForName(const std::string& name) const { claimEntryType claims; int nLastTakeoverHeight = 0; @@ -406,30 +406,24 @@ CClaimsForNameType CClaimTrieCacheBase::getClaimsForName(const std::string& name claims = data.claims; nLastTakeoverHeight = data.nHeightOfLastTakeover; } - return {std::move(claims), std::move(supports), nLastTakeoverHeight, name}; -} - -CAmount CClaimTrieCacheBase::getEffectiveAmountForClaim(const std::string& name, const uint160& claimId, std::vector* supports) const -{ - return getEffectiveAmountForClaim(getClaimsForName(name), claimId, supports); -} - -CAmount CClaimTrieCacheBase::getEffectiveAmountForClaim(const CClaimsForNameType& claims, const uint160& claimId, std::vector* supports) const -{ - CAmount effectiveAmount = 0; - for (const auto& claim : claims.claims) { - if (claim.claimId == claimId && claim.nValidAtHeight < nNextHeight) { - effectiveAmount += claim.nAmount; - for (const auto& support : claims.supports) { - if (support.supportedClaimId == claimId && support.nValidAtHeight < nNextHeight) { - effectiveAmount += support.nAmount; - if (supports) supports->push_back(support); - } - } - break; + auto find = [&supports](decltype(supports)::iterator& it, const CClaimValue& claim) { + it = std::find_if(it, supports.end(), [&claim](const CSupportValue& support) { + return claim.claimId == support.supportedClaimId; + }); + return it != supports.end(); + }; + // match support to claim + std::vector claimsNsupports; + for (const auto& claim : claims) { + CAmount nAmount = claim.nValidAtHeight < nNextHeight ? claim.nAmount : 0; + auto ic = claimsNsupports.emplace(claimsNsupports.end(), claim, nAmount); + for (auto it = supports.begin(); find(it, claim); it = supports.erase(it)) { + if (it->nValidAtHeight < nNextHeight) + ic->effectiveAmount += it->nAmount; + ic->supports.emplace_back(std::move(*it)); } } - return effectiveAmount; + return {name, nLastTakeoverHeight, std::move(claimsNsupports), std::move(supports)}; } void completeHash(uint256& partialHash, const std::string& key, std::size_t to) @@ -532,19 +526,6 @@ bool CClaimTrie::find(const std::string& key, CClaimTrieData &data) const { return db->Read(std::make_pair(TRIE_NODE_CLAIMS, key), data); } -bool CClaimTrieCacheBase::getClaimById(const uint160& claimId, std::string& name, CClaimValue& claim) const -{ - CClaimIndexElement element; - if (!base->db->Read(std::make_pair(CLAIM_BY_ID, claimId), element)) - return false; - if (element.claim.claimId == claimId) { - name = element.name; - claim = element.claim; - return true; - } - return false; -} - template void BatchWrite(CDBBatch& batch, uint8_t dbkey, const K& key, const std::vector& value) { diff --git a/src/claimtrie.h b/src/claimtrie.h index 6e360fc64..532582cd5 100644 --- a/src/claimtrie.h +++ b/src/claimtrie.h @@ -36,10 +36,10 @@ struct CClaimValue { COutPoint outPoint; uint160 claimId; - CAmount nAmount; - CAmount nEffectiveAmount; - int nHeight; - int nValidAtHeight; + CAmount nAmount = 0; + CAmount nEffectiveAmount = 0; + int nHeight = 0; + int nValidAtHeight = 0; CClaimValue() = default; @@ -94,9 +94,9 @@ struct CSupportValue { COutPoint outPoint; uint160 supportedClaimId; - CAmount nAmount; - int nHeight; - int nValidAtHeight; + CAmount nAmount = 0; + int nHeight = 0; + int nValidAtHeight = 0; CSupportValue() = default; @@ -213,7 +213,7 @@ struct CClaimTrieDataNode { struct COutPointHeightType { COutPoint outPoint; - int nHeight; + int nHeight = 0; COutPointHeightType() = default; @@ -236,7 +236,7 @@ struct CNameOutPointHeightType { std::string name; COutPoint outPoint; - int nHeight; + int nHeight = 0; CNameOutPointHeightType() = default; @@ -305,22 +305,59 @@ struct CClaimIndexElement CClaimValue claim; }; -struct CClaimsForNameType +struct CClaimNsupports { - claimEntryType claims; - supportEntryType supports; - int nLastTakeoverHeight; - std::string name; + CClaimNsupports() = default; + CClaimNsupports(CClaimNsupports&&) = default; + CClaimNsupports(const CClaimNsupports&) = default; - CClaimsForNameType(claimEntryType claims, supportEntryType supports, int nLastTakeoverHeight, std::string name) - : claims(std::move(claims)), supports(std::move(supports)), nLastTakeoverHeight(nLastTakeoverHeight), name(std::move(name)) + CClaimNsupports& operator=(CClaimNsupports&&) = default; + CClaimNsupports& operator=(const CClaimNsupports&) = default; + + CClaimNsupports(const CClaimValue& claim, CAmount effectiveAmount, const std::vector& supports = {}) + : claim(claim), effectiveAmount(effectiveAmount), supports(supports) { } - CClaimsForNameType(CClaimsForNameType&&) = default; - CClaimsForNameType(const CClaimsForNameType&) = default; - CClaimsForNameType& operator=(CClaimsForNameType&&) = default; - CClaimsForNameType& operator=(const CClaimsForNameType&) = default; + bool IsNull() const + { + return claim.claimId.IsNull(); + } + + CClaimValue claim; + CAmount effectiveAmount = 0; + std::vector supports; +}; + +static const CClaimNsupports invalid; + +struct CClaimSupportToName +{ + CClaimSupportToName(const std::string& name, int nLastTakeoverHeight, std::vector claimsNsupports, std::vector unmatchedSupports) + : name(name), nLastTakeoverHeight(nLastTakeoverHeight), claimsNsupports(std::move(claimsNsupports)), unmatchedSupports(std::move(unmatchedSupports)) + { + } + + const CClaimNsupports& find(const uint160& claimId) const + { + auto it = std::find_if(claimsNsupports.begin(), claimsNsupports.end(), [&claimId](const CClaimNsupports& value) { + return claimId == value.claim.claimId; + }); + return it != claimsNsupports.end() ? *it : invalid; + } + + const CClaimNsupports& find(const std::string& partialId) const + { + auto it = std::find_if(claimsNsupports.begin(), claimsNsupports.end(), [&partialId](const CClaimNsupports& value) { + return value.claim.claimId.GetHex().find(partialId) == 0; + }); + return it != claimsNsupports.end() ? *it : invalid; + } + + const std::string name; + const int nLastTakeoverHeight; + const std::vector claimsNsupports; + const std::vector unmatchedSupports; }; class CClaimTrie @@ -341,6 +378,8 @@ public: friend struct ClaimTrieChainFixture; friend class CClaimTrieCacheExpirationFork; friend class CClaimTrieCacheNormalizationFork; + friend bool getClaimById(const uint160&, std::string&, CClaimValue*); + friend bool getClaimById(const std::string&, std::string&, CClaimValue*); std::size_t getTotalNamesInTrie() const; std::size_t getTotalClaimsInTrie() const; @@ -475,8 +514,6 @@ public: uint256 getMerkleHash(); - bool getClaimById(const uint160& claimId, std::string& name, CClaimValue& claim) const; - bool flush(); bool empty() const; bool ReadFromDisk(const CBlockIndex* tip); @@ -517,10 +554,7 @@ public: virtual bool finalizeDecrement(std::vector>& takeoverHeightUndo); - virtual CClaimsForNameType getClaimsForName(const std::string& name) const; - - CAmount getEffectiveAmountForClaim(const std::string& name, const uint160& claimId, std::vector* supports = nullptr) const; - CAmount getEffectiveAmountForClaim(const CClaimsForNameType& claims, const uint160& claimId, std::vector* supports = nullptr) const; + virtual CClaimSupportToName getClaimsForName(const std::string& name) const; CClaimPrefixTrie::const_iterator begin() const; CClaimPrefixTrie::const_iterator end() const; @@ -704,7 +738,7 @@ public: bool getProofForName(const std::string& name, CClaimTrieProof& proof) override; bool getInfoForName(const std::string& name, CClaimValue& claim) const override; - CClaimsForNameType getClaimsForName(const std::string& name) const override; + CClaimSupportToName getClaimsForName(const std::string& name) const override; std::string adjustNameForValidHeight(const std::string& name, int validHeight) const override; protected: @@ -733,7 +767,7 @@ public: explicit CClaimTrieCacheHashFork(CClaimTrie* base); bool getProofForName(const std::string& name, CClaimTrieProof& proof) override; - bool getProofForName(const std::string& name, CClaimTrieProof& proof, const uint160& claimId); + bool getProofForName(const std::string& name, CClaimTrieProof& proof, const std::function& comp); void initializeIncrement() override; bool finalizeDecrement(std::vector>& takeoverHeightUndo) override; diff --git a/src/claimtrieforks.cpp b/src/claimtrieforks.cpp index 4c79a885a..def94092f 100644 --- a/src/claimtrieforks.cpp +++ b/src/claimtrieforks.cpp @@ -246,7 +246,7 @@ bool CClaimTrieCacheNormalizationFork::getInfoForName(const std::string& name, C return CClaimTrieCacheExpirationFork::getInfoForName(normalizeClaimName(name), claim); } -CClaimsForNameType CClaimTrieCacheNormalizationFork::getClaimsForName(const std::string& name) const +CClaimSupportToName CClaimTrieCacheNormalizationFork::getClaimsForName(const std::string& name) const { return CClaimTrieCacheExpirationFork::getClaimsForName(normalizeClaimName(name)); } @@ -408,10 +408,10 @@ std::vector ComputeMerklePath(const std::vector& hashes, uint3 bool CClaimTrieCacheHashFork::getProofForName(const std::string& name, CClaimTrieProof& proof) { - return getProofForName(name, proof, uint160()); + return getProofForName(name, proof, nullptr); } -bool CClaimTrieCacheHashFork::getProofForName(const std::string& name, CClaimTrieProof& proof, const uint160& claimId) +bool CClaimTrieCacheHashFork::getProofForName(const std::string& name, CClaimTrieProof& proof, const std::function& comp) { if (nNextHeight < Params().GetConsensus().nAllClaimsInMerkleForkHeight) return CClaimTrieCacheNormalizationFork::getProofForName(name, proof); @@ -445,10 +445,7 @@ bool CClaimTrieCacheHashFork::getProofForName(const std::string& name, CClaimTri if (it.key() == name) { uint32_t nClaimIndex = 0; auto& claims = it->claims; - auto itClaim = claimId.IsNull() ? claims.begin() : - std::find_if(claims.begin(), claims.end(), [&claimId](const CClaimValue& claim) { - return claim.claimId == claimId; - }); + auto itClaim = !comp ? claims.begin() : std::find_if(claims.begin(), claims.end(), comp); if (itClaim != claims.end()) { proof.hasValue = true; proof.outPoint = itClaim->outPoint; diff --git a/src/rpc/claimtrie.cpp b/src/rpc/claimtrie.cpp index defb31bb9..479c192ae 100644 --- a/src/rpc/claimtrie.cpp +++ b/src/rpc/claimtrie.cpp @@ -15,21 +15,19 @@ #include #include -uint160 ParseClaimtrieId(const UniValue& v, const std::string& strName) +void ParseClaimtrieId(const UniValue& v, std::string& partialId, uint160& claimId, const std::string& strName) { static constexpr size_t claimIdHexLength = 40; std::string strHex; if (v.isStr()) strHex = v.get_str(); - if (!IsHex(strHex)) // Note: IsHex("") is false - throw JSONRPCError(RPC_INVALID_PARAMETER, strName + " must be a 20-character hexadecimal string (not '" + strHex + "')"); - if (strHex.length() != claimIdHexLength) - throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be of length %d (not %d)", strName, claimIdHexLength, strHex.length())); - - uint160 result; - result.SetHex(strHex); - return result; + if (!IsHex(strHex)) + throw JSONRPCError(RPC_INVALID_PARAMETER, strName + " must be a hexadecimal string"); + if (strHex.length() == claimIdHexLength) + claimId.SetHex(strHex); + else + partialId = strHex; } static CBlockIndex* BlockHashIndex(const uint256& blockHash) @@ -107,29 +105,128 @@ static bool getValueForOutPoint(const CCoinsViewCache& coinsCache, const COutPoi { const Coin& coin = coinsCache.AccessCoin(out); if (coin.IsSpent()) - { return false; - } int op; std::vector > vvchParams; if (!DecodeClaimScript(coin.out.scriptPubKey, op, vvchParams)) - { return false; - } + if (op == OP_CLAIM_NAME) - { sValue = HexStr(vvchParams[1].begin(), vvchParams[1].end()); - return true; - } - if (vvchParams.size() > 2) // both UPDATE and SUPPORT - { + else if (vvchParams.size() > 2) // both UPDATE and SUPPORT sValue = HexStr(vvchParams[2].begin(), vvchParams[2].end()); + else + return false; + return true; +} + +bool getClaimById(const uint160& claimId, std::string& name, CClaimValue* claim = nullptr) +{ + if (claimId.IsNull()) + return false; + + CClaimIndexElement element; + if (!pclaimTrie->db->Read(std::make_pair(CLAIM_BY_ID, claimId), element)) + return false; + if (element.claim.claimId == claimId) { + name = element.name; + if (claim) + *claim = element.claim; return true; } return false; } +// name can be setted explicitly +bool getClaimById(const std::string& partialId, std::string& name, CClaimValue* claim = nullptr) +{ + if (partialId.empty()) + return false; + + std::unique_ptr pcursor(pclaimTrie->db->NewIterator()); + + for (pcursor->SeekToFirst(); pcursor->Valid(); pcursor->Next()) { + std::pair key; + if (!pcursor->GetKey(key) || key.first != CLAIM_BY_ID) + continue; + + if (key.second.GetHex().find(partialId) != 0) + continue; + + CClaimIndexElement element; + if (pcursor->GetValue(element)) { + if (!name.empty() && name != element.name) + continue; + name = element.name; + if (claim) + *claim = element.claim; + return true; + } + } + return false; +} + +UniValue claimToJSON(const CCoinsViewCache& coinsCache, const CClaimValue& claim) +{ + UniValue result(UniValue::VOBJ); + + std::string targetName; + if (getClaimById(claim.claimId, targetName)) + result.pushKV("name", escapeNonUtf8(targetName)); + + std::string sValue; + if (getValueForOutPoint(coinsCache, claim.outPoint, sValue)) + result.pushKV("value", sValue); + + result.pushKV("claimId", claim.claimId.GetHex()); + result.pushKV("txId", claim.outPoint.hash.GetHex()); + result.pushKV("n", (int)claim.outPoint.n); + result.pushKV("height", claim.nHeight); + result.pushKV("validAtHeight", claim.nValidAtHeight); + result.pushKV("amount", claim.nAmount); + + return result; +} + +UniValue supportToJSON(const CCoinsViewCache& coinsCache, const CSupportValue& support) +{ + UniValue ret(UniValue::VOBJ); + + std::string value; + if (getValueForOutPoint(coinsCache, support.outPoint, value)) + ret.pushKV("value", value); + + ret.pushKV("txId", support.outPoint.hash.GetHex()); + ret.pushKV("n", (int)support.outPoint.n); + ret.pushKV("height", support.nHeight); + ret.pushKV("validAtHeight", support.nValidAtHeight); + ret.pushKV("amount", support.nAmount); + + return ret; +} + +UniValue claimAndSupportsToJSON(const CCoinsViewCache& coinsCache, const CClaimNsupports& claimNsupports) +{ + auto& claim = claimNsupports.claim; + auto& supports = claimNsupports.supports; + + auto result = claimToJSON(coinsCache, claim); + result.pushKV("effectiveAmount", claimNsupports.effectiveAmount); + + if (claim.nEffectiveAmount > claimNsupports.effectiveAmount) + result.pushKV("pendingEffectiveAmount", claim.nEffectiveAmount); + + UniValue supportObjs(UniValue::VARR); + for (auto& support : supports) + supportObjs.push_back(supportToJSON(coinsCache, support)); + + if (!supportObjs.empty()) + result.pushKV("supports", supportObjs); + + return result; +} + bool validParams(const UniValue& params, uint8_t required, uint8_t optional) { auto count = params.size(); @@ -186,7 +283,7 @@ static UniValue getclaimsintrie(const JSONRPCRequest& request) RollBackTo(blockIndex, coinsCache, trieCache); } - trieCache.recurseNodes("", [&ret, &trieCache, &coinsCache] (const std::string& name, const CClaimTrieData& data) { + trieCache.recurseNodes({}, [&ret, &trieCache, &coinsCache] (const std::string& name, const CClaimTrieData& data) { if (ShutdownRequested()) throw JSONRPCError(RPC_INTERNAL_ERROR, "Shutdown requested"); @@ -196,34 +293,8 @@ static UniValue getclaimsintrie(const JSONRPCRequest& request) return; UniValue claims(UniValue::VARR); - for (auto itClaims = data.claims.cbegin(); itClaims != data.claims.cend(); ++itClaims) { - UniValue claim(UniValue::VOBJ); - claim.pushKV("claimId", itClaims->claimId.GetHex()); - claim.pushKV("txid", itClaims->outPoint.hash.GetHex()); - claim.pushKV("n", (int) itClaims->outPoint.n); - claim.pushKV("amount", ValueFromAmount(itClaims->nAmount)); - claim.pushKV("height", itClaims->nHeight); - const Coin &coin = coinsCache.AccessCoin(itClaims->outPoint); - if (coin.IsSpent()) { - LogPrintf("%s: the specified txout of %s appears to have been spent\n", __func__, - itClaims->outPoint.hash.GetHex()); - claim.pushKV("error", "Txout spent"); - } else { - int op; - std::vector > vvchParams; - if (!DecodeClaimScript(coin.out.scriptPubKey, op, vvchParams)) { - LogPrintf("%s: the specified txout of %s does not have an claim command\n", __func__, - itClaims->outPoint.hash.GetHex()); - } - claim.pushKV("value", HexStr(vvchParams[1].begin(), vvchParams[1].end())); - } - std::string targetName; - CClaimValue targetClaim; - if (trieCache.getClaimById(itClaims->claimId, targetName, targetClaim)) - claim.push_back(Pair("name", escapeNonUtf8(targetName))); - - claims.push_back(claim); - } + for (auto& claim : data.claims) + claims.push_back(claimToJSON(coinsCache, claim)); UniValue nodeObj(UniValue::VOBJ); nodeObj.pushKV("normalized_name", escapeNonUtf8(name)); @@ -266,7 +337,7 @@ static UniValue getnamesintrie(const JSONRPCRequest& request) } UniValue ret(UniValue::VARR); - trieCache.recurseNodes("", [&ret](const std::string &name, const CClaimTrieData &data) { + trieCache.recurseNodes({}, [&ret](const std::string &name, const CClaimTrieData &data) { if (!data.empty()) ret.push_back(escapeNonUtf8(name)); if (ShutdownRequested()) @@ -280,10 +351,10 @@ static UniValue getnamesintrie(const JSONRPCRequest& request) static UniValue getvalueforname(const JSONRPCRequest& request) { - if (request.fHelp || !validParams(request.params, 1, 1)) + if (request.fHelp || !validParams(request.params, 1, 2)) throw std::runtime_error( "getvalueforname \"name\"\n" - "Return the winning value associated with a name, if one exists\n" + "Return the winning or specified by claimId value associated with a name\n" "Arguments:\n" "1. \"name\" (string) the name to look up\n" "2. \"blockhash\" (string, optional) get the value\n" @@ -293,6 +364,7 @@ static UniValue getvalueforname(const JSONRPCRequest& request) " If none is given,\n" " the latest active\n" " block will be used.\n" + "3. \"claimId\" (string, optional) can be partial one\n" "Result: \n" "\"value\" (string) the value of the name, if it exists\n" "\"claimId\" (string) the claimId for this name claim\n" @@ -313,82 +385,32 @@ static UniValue getvalueforname(const JSONRPCRequest& request) RollBackTo(blockIndex, coinsCache, trieCache); } - const auto& name = request.params[0].get_str(); + uint160 claimId; + std::string partialId; + if (request.params.size() > 2) + ParseClaimtrieId(request.params[2], partialId, claimId, "claimId (optional parameter 3)"); + + const auto name = request.params[0].get_str(); UniValue ret(UniValue::VOBJ); - CClaimValue claim; - if (!trieCache.getInfoForName(name, claim)) - return ret; // they may have asked for a name that doesn't exist (which is not an error) - - std::string sValue; - if (!getValueForOutPoint(coinsCache, claim.outPoint, sValue)) + auto res = trieCache.getClaimsForName(name); + if (res.claimsNsupports.empty()) return ret; - const auto nEffectiveAmount = trieCache.getEffectiveAmountForClaim(name, claim.claimId); + auto& claimNsupports = + !claimId.IsNull() ? res.find(claimId) : + !partialId.empty() ? res.find(partialId) : res.claimsNsupports[0]; - ret.pushKV("value", sValue); - ret.pushKV("claimId", claim.claimId.GetHex()); - ret.pushKV("txid", claim.outPoint.hash.GetHex()); - ret.pushKV("n", (int)claim.outPoint.n); - ret.pushKV("amount", claim.nAmount); - ret.pushKV("effective amount", nEffectiveAmount); - ret.pushKV("height", claim.nHeight); + if (claimNsupports.IsNull()) + return ret; - std::string targetName; - CClaimValue targetClaim; - if (trieCache.getClaimById(claim.claimId, targetName, targetClaim)) - ret.pushKV("name", escapeNonUtf8(targetName)); + ret.pushKV("normalizedName", escapeNonUtf8(res.name)); + ret.pushKVs(claimAndSupportsToJSON(coinsCache, claimNsupports)); + ret.pushKV("lastTakeoverHeight", res.nLastTakeoverHeight); return ret; } -typedef std::pair > claimAndSupportsType; -typedef std::map claimSupportMapType; - -UniValue supportToJSON(const CCoinsViewCache& coinsCache, const CSupportValue& support) -{ - UniValue ret(UniValue::VOBJ); - ret.pushKV("txid", support.outPoint.hash.GetHex()); - ret.pushKV("n", (int)support.outPoint.n); - ret.pushKV("nHeight", support.nHeight); - ret.pushKV("nValidAtHeight", support.nValidAtHeight); - ret.pushKV("nAmount", support.nAmount); - std::string value; - if (getValueForOutPoint(coinsCache, support.outPoint, value)) - ret.pushKV("value", value); - return ret; -} - -UniValue claimAndSupportsToJSON(const CClaimTrieCache& trieCache, const CCoinsViewCache& coinsCache, CAmount nEffectiveAmount, claimSupportMapType::const_iterator itClaimsAndSupports) -{ - const CClaimValue& claim = itClaimsAndSupports->second.first; - const std::vector& supports = itClaimsAndSupports->second.second; - - UniValue supportObjs(UniValue::VARR); - for (const auto& support: supports) - supportObjs.push_back(supportToJSON(coinsCache, support)); - - UniValue result(UniValue::VOBJ); - result.pushKV("claimId", itClaimsAndSupports->first.GetHex()); - result.pushKV("txid", claim.outPoint.hash.GetHex()); - result.pushKV("n", (int)claim.outPoint.n); - result.pushKV("nHeight", claim.nHeight); - result.pushKV("nValidAtHeight", claim.nValidAtHeight); - result.pushKV("nAmount", claim.nAmount); - std::string sValue; - if (getValueForOutPoint(coinsCache, claim.outPoint, sValue)) - result.pushKV("value", sValue); - result.pushKV("nEffectiveAmount", nEffectiveAmount); - result.pushKV("supports", supportObjs); - - std::string targetName; - CClaimValue targetClaim; - if (trieCache.getClaimById(claim.claimId, targetName, targetClaim)) - result.pushKV("name", escapeNonUtf8(targetName)); - - return result; -} - UniValue getclaimsforname(const JSONRPCRequest& request) { if (request.fHelp || !validParams(request.params, 1, 1)) @@ -452,39 +474,20 @@ UniValue getclaimsforname(const JSONRPCRequest& request) std::string name = request.params[0].get_str(); auto claimsForName = trieCache.getClaimsForName(name); - UniValue claimObjs(UniValue::VARR); - claimSupportMapType claimSupportMap; - UniValue unmatchedSupports(UniValue::VARR); - - for (auto itClaims = claimsForName.claims.begin(); itClaims != claimsForName.claims.end(); ++itClaims) - { - claimAndSupportsType claimAndSupports = std::make_pair(*itClaims, std::vector()); - claimSupportMap.emplace(itClaims->claimId, claimAndSupports); - } - - for (auto itSupports = claimsForName.supports.begin(); itSupports != claimsForName.supports.end(); ++itSupports) - { - auto itClaimAndSupports = claimSupportMap.find(itSupports->supportedClaimId); - if (itClaimAndSupports == claimSupportMap.end()) - unmatchedSupports.push_back(supportToJSON(coinsCache, *itSupports)); - else - itClaimAndSupports->second.second.push_back(*itSupports); - } - UniValue result(UniValue::VOBJ); - result.pushKV("nLastTakeoverHeight", claimsForName.nLastTakeoverHeight); result.pushKV("normalized_name", escapeNonUtf8(claimsForName.name)); - for (auto itClaims = claimsForName.claims.begin(); itClaims != claimsForName.claims.end(); ++itClaims) - { - auto itClaimsAndSupports = claimSupportMap.find(itClaims->claimId); - const auto nEffectiveAmount = trieCache.getEffectiveAmountForClaim(claimsForName, itClaimsAndSupports->first); - UniValue claimObj = claimAndSupportsToJSON(trieCache, coinsCache, nEffectiveAmount, itClaimsAndSupports); - claimObjs.push_back(claimObj); - } + UniValue claimObjs(UniValue::VARR); + for (auto& claim : claimsForName.claimsNsupports) + claimObjs.push_back(claimAndSupportsToJSON(coinsCache, claim)); + + UniValue unmatchedSupports(UniValue::VARR); + for (auto& support : claimsForName.unmatchedSupports) + unmatchedSupports.push_back(supportToJSON(coinsCache, support)); result.pushKV("claims", claimObjs); - result.pushKV("supports without claims", unmatchedSupports); + result.pushKV("lastTakeoverHeight", claimsForName.nLastTakeoverHeight); + result.pushKV("supportsWithoutClaims", unmatchedSupports); return result; } @@ -495,7 +498,7 @@ UniValue getclaimbyid(const JSONRPCRequest& request) "getclaimbyid\n" "Get a claim by claim id\n" "Arguments: \n" - "1. \"claimId\" (string) the claimId of this claim\n" + "1. \"claimId\" (string) the claimId of this claim or patial id (at least 3 chars)\n" "Result:\n" "{\n" " \"name\" (string) the original name of the claim (before normalization)\n" @@ -521,43 +524,26 @@ UniValue getclaimbyid(const JSONRPCRequest& request) LOCK(cs_main); CClaimTrieCache trieCache(pclaimTrie); - uint160 claimId = ParseClaimtrieId(request.params[0], "Claim-id (parameter 1)"); + CCoinsViewCache coinsCache(pcoinsTip.get()); + + uint160 claimId; + std::string partialId; + ParseClaimtrieId(request.params[0], partialId, claimId, "Claim-id (parameter 1)"); + + if (claimId.IsNull() && partialId.length() < 3) + throw JSONRPCError(RPC_INVALID_PARAMETER, "Claim-id (parameter 1) should be at least 3 chars"); + UniValue claim(UniValue::VOBJ); std::string name; CClaimValue claimValue; - trieCache.getClaimById(claimId, name, claimValue); - if (claimValue.claimId == claimId) - { - std::vector supports; - CAmount effectiveAmount = trieCache.getEffectiveAmountForClaim(name, claimValue.claimId, &supports); - - std::string sValue; - claim.pushKV("name", escapeNonUtf8(name)); - if (trieCache.shouldNormalize()) - claim.pushKV("normalized_name", escapeNonUtf8(trieCache.normalizeClaimName(name, true))); - CCoinsViewCache coinsCache(pcoinsTip.get()); - if (getValueForOutPoint(coinsCache, claimValue.outPoint, sValue)) - claim.pushKV("value", sValue); - claim.pushKV("claimId", claimValue.claimId.GetHex()); - claim.pushKV("txid", claimValue.outPoint.hash.GetHex()); - claim.pushKV("n", (int) claimValue.outPoint.n); - claim.pushKV("amount", claimValue.nAmount); - claim.pushKV("effective amount", effectiveAmount); - UniValue supportList(UniValue::VARR); - for(const CSupportValue& support: supports) { - UniValue supportEntry(UniValue::VOBJ); - supportEntry.pushKV("txid", support.outPoint.hash.GetHex()); - supportEntry.pushKV("n", (int)support.outPoint.n); - supportEntry.pushKV("height", support.nHeight); - supportEntry.pushKV("valid at height", support.nValidAtHeight); - supportEntry.pushKV("amount", support.nAmount); - if (getValueForOutPoint(coinsCache, support.outPoint, sValue)) - claim.pushKV("value", sValue); - supportList.pushKVs(supportEntry); + if (getClaimById(claimId, name, &claimValue) || getClaimById(partialId, name, &claimValue)) { + auto res = trieCache.getClaimsForName(name); + auto& claimNsupports = !claimId.IsNull() ? res.find(claimId) : res.find(partialId); + if (!claimNsupports.IsNull()) { + claim.pushKV("normalizedName", escapeNonUtf8(res.name)); + claim.pushKVs(claimAndSupportsToJSON(coinsCache, claimNsupports)); + claim.pushKV("lastTakeoverHeight", res.nLastTakeoverHeight); } - claim.pushKV("supports", supportList); - claim.pushKV("height", claimValue.nHeight); - claim.pushKV("valid at height", claimValue.nValidAtHeight); } return claim; } @@ -882,12 +868,23 @@ UniValue getnameproof(const JSONRPCRequest& request) } uint160 claimId; + std::string partialId; if (request.params.size() > 2) - claimId = ParseClaimtrieId(request.params[2], "claimId (optional parameter 3)"); + ParseClaimtrieId(request.params[2], partialId, claimId, "claimId (optional parameter 3)"); + + std::function comp; + if (!claimId.IsNull()) + comp = [&claimId](const CClaimValue& claim) { + return claim.claimId == claimId; + }; + else + comp = [&partialId](const CClaimValue& claim) { + return claim.claimId.GetHex().find(partialId) == 0; + }; CClaimTrieProof proof; std::string name = request.params[0].get_str(); - if (!trieCache.getProofForName(name, proof, claimId)) + if (!trieCache.getProofForName(name, proof, comp)) throw JSONRPCError(RPC_INTERNAL_ERROR, "Failed to generate proof"); return proofToJSON(proof); @@ -917,7 +914,7 @@ static const CRPCCommand commands[] = { "Claimtrie", "getclaimsintrie", &getclaimsintrie, { "blockhash" } }, { "Claimtrie", "getnamesintrie", &getnamesintrie, { "blockhash" } }, { "hidden", "getclaimtrie", &getclaimtrie, { } }, - { "Claimtrie", "getvalueforname", &getvalueforname, { "name","blockhash" } }, + { "Claimtrie", "getvalueforname", &getvalueforname, { "name","blockhash","claimId" } }, { "Claimtrie", "getclaimsforname", &getclaimsforname, { "name","blockhash" } }, { "Claimtrie", "gettotalclaimednames", &gettotalclaimednames, { "" } }, { "Claimtrie", "gettotalclaims", &gettotalclaims, { "" } }, diff --git a/src/test/claimtriebranching_tests.cpp b/src/test/claimtriebranching_tests.cpp index 385ce52f3..cb22e554e 100644 --- a/src/test/claimtriebranching_tests.cpp +++ b/src/test/claimtriebranching_tests.cpp @@ -25,6 +25,7 @@ extern ::CChainState g_chainstate; extern ::ArgsManager gArgs; extern std::vector random_strings(std::size_t count); +extern bool getClaimById(const uint160&, std::string&, CClaimValue*); using namespace std; @@ -434,7 +435,7 @@ struct ClaimTrieChainFixture: public CClaimTrieCache res.message() << "No claim found"; return res; } else { - CAmount effective_amount = getEffectiveAmountForClaim(name, val.claimId); + CAmount effective_amount = getClaimsForName(name).find(val.claimId).effectiveAmount; if (effective_amount != amount) { boost::test_tools::predicate_result res(false); res.message() << amount << " != " << effective_amount; @@ -566,12 +567,12 @@ BOOST_AUTO_TEST_CASE(claim_test) CMutableTransaction tx3 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",2); fixture.IncrementBlocks(1); BOOST_CHECK(fixture.is_best_claim("test",tx3)); - BOOST_CHECK_EQUAL(2U, fixture.getClaimsForName("test").claims.size()); + BOOST_CHECK_EQUAL(2U, fixture.getClaimsForName("test").claimsNsupports.size()); fixture.DecrementBlocks(1); BOOST_CHECK(!fixture.is_best_claim("test",tx2)); BOOST_CHECK(!fixture.is_best_claim("test",tx3)); - BOOST_CHECK_EQUAL(0U, fixture.getClaimsForName("test").claims.size()); + BOOST_CHECK_EQUAL(0U, fixture.getClaimsForName("test").claimsNsupports.size()); // make two claims , one older CMutableTransaction tx4 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",1); @@ -583,7 +584,7 @@ BOOST_AUTO_TEST_CASE(claim_test) BOOST_CHECK(fixture.is_best_claim("test", tx4)); fixture.IncrementBlocks(1); BOOST_CHECK(fixture.is_best_claim("test",tx4)); - BOOST_CHECK_EQUAL(2U, fixture.getClaimsForName("test").claims.size()); + BOOST_CHECK_EQUAL(2U, fixture.getClaimsForName("test").claimsNsupports.size()); fixture.DecrementBlocks(1); BOOST_CHECK(fixture.is_best_claim("test", tx4)); @@ -603,7 +604,7 @@ BOOST_AUTO_TEST_CASE(claim_test) BOOST_CHECK(fixture.is_best_claim("test", tx6)); fixture.IncrementBlocks(10); BOOST_CHECK(fixture.is_best_claim("test",tx7)); - BOOST_CHECK_EQUAL(2U, fixture.getClaimsForName("test").claims.size()); + BOOST_CHECK_EQUAL(2U, fixture.getClaimsForName("test").claimsNsupports.size()); fixture.DecrementBlocks(10); BOOST_CHECK(fixture.is_claim_in_queue("test",tx7)); @@ -873,7 +874,7 @@ BOOST_AUTO_TEST_CASE(support_spend_test) CMutableTransaction s2 = fixture.MakeSupport(fixture.GetCoinbase(),tx5,"test",2); fixture.IncrementBlocks(1); BOOST_CHECK(fixture.is_best_claim("test",tx5)); - BOOST_CHECK_EQUAL(2U, fixture.getClaimsForName("test").claims.size()); + BOOST_CHECK_EQUAL(2U, fixture.getClaimsForName("test").claimsNsupports.size()); // build the spend where s2 is sppent on txin[1] and tx3 is spent on txin[0] uint32_t prevout = 0; @@ -938,7 +939,7 @@ BOOST_AUTO_TEST_CASE(claimtrie_update_test) CMutableTransaction u3 = fixture.MakeUpdate(tx3, "test", "one", ClaimIdHash(tx3.GetHash(), 0), 2); fixture.IncrementBlocks(1); BOOST_CHECK(fixture.is_best_claim("test",u3)); - BOOST_CHECK_EQUAL(2U, fixture.getClaimsForName("test").claims.size()); + BOOST_CHECK_EQUAL(2U, fixture.getClaimsForName("test").claimsNsupports.size()); fixture.DecrementBlocks(11); // losing update on winning claim happens without delay @@ -946,7 +947,7 @@ BOOST_AUTO_TEST_CASE(claimtrie_update_test) CMutableTransaction tx6 = fixture.MakeClaim(fixture.GetCoinbase(), "test", "one", 2); fixture.IncrementBlocks(10); BOOST_CHECK(fixture.is_best_claim("test", tx5)); - BOOST_CHECK_EQUAL(2U, fixture.getClaimsForName("test").claims.size()); + BOOST_CHECK_EQUAL(2U, fixture.getClaimsForName("test").claimsNsupports.size()); CMutableTransaction u4 = fixture.MakeUpdate(tx5, "test", "one", ClaimIdHash(tx5.GetHash(), 0), 1); fixture.IncrementBlocks(1); BOOST_CHECK(fixture.is_best_claim("test",tx6)); @@ -1054,7 +1055,7 @@ BOOST_AUTO_TEST_CASE(claimtrie_expire_test) BOOST_CHECK(fixture.is_best_claim("test", tx6)); } /* - * tests for CClaimPrefixTrie::getEffectiveAmountForClaim + * tests for effectiveAmount */ BOOST_AUTO_TEST_CASE(claimtriebranching_get_effective_amount_for_claim) { @@ -1064,42 +1065,42 @@ BOOST_AUTO_TEST_CASE(claimtriebranching_get_effective_amount_for_claim) uint160 claimId = ClaimIdHash(claimtx.GetHash(), 0); fixture.IncrementBlocks(1); - BOOST_CHECK_EQUAL(fixture.getEffectiveAmountForClaim("test", claimId), 2); - BOOST_CHECK_EQUAL(fixture.getEffectiveAmountForClaim("inexistent", claimId), 0); //not found returns 0 + BOOST_CHECK_EQUAL(fixture.getClaimsForName("test").find(claimId).effectiveAmount, 2); + BOOST_CHECK_EQUAL(fixture.getClaimsForName("inexistent").find(claimId).effectiveAmount, 0); //not found returns 0 // one claim, one support fixture.MakeSupport(fixture.GetCoinbase(), claimtx, "test", 40); fixture.IncrementBlocks(1); - BOOST_CHECK_EQUAL(fixture.getEffectiveAmountForClaim("test", claimId), 42); + BOOST_CHECK_EQUAL(fixture.getClaimsForName("test").find(claimId).effectiveAmount, 42); // Two claims, first one with supports CMutableTransaction claimtx2 = fixture.MakeClaim(fixture.GetCoinbase(), "test", "two", 1); uint160 claimId2 = ClaimIdHash(claimtx2.GetHash(), 0); fixture.IncrementBlocks(10); - BOOST_CHECK_EQUAL(fixture.getEffectiveAmountForClaim("test", claimId), 42); - BOOST_CHECK_EQUAL(fixture.getEffectiveAmountForClaim("test", claimId2), 1); - BOOST_CHECK_EQUAL(fixture.getEffectiveAmountForClaim("inexistent", claimId), 0); - BOOST_CHECK_EQUAL(fixture.getEffectiveAmountForClaim("inexistent", claimId2), 0); + BOOST_CHECK_EQUAL(fixture.getClaimsForName("test").find(claimId).effectiveAmount, 42); + BOOST_CHECK_EQUAL(fixture.getClaimsForName("test").find(claimId2).effectiveAmount, 1); + BOOST_CHECK_EQUAL(fixture.getClaimsForName("inexistent").find(claimId).effectiveAmount, 0); + BOOST_CHECK_EQUAL(fixture.getClaimsForName("inexistent").find(claimId2).effectiveAmount, 0); // Two claims, both with supports, second claim effective amount being less than first claim fixture.MakeSupport(fixture.GetCoinbase(), claimtx2, "test", 6); fixture.IncrementBlocks(13); //delay - BOOST_CHECK_EQUAL(fixture.getEffectiveAmountForClaim("test", claimId), 42); - BOOST_CHECK_EQUAL(fixture.getEffectiveAmountForClaim("test", claimId2), 7); + BOOST_CHECK_EQUAL(fixture.getClaimsForName("test").find(claimId).effectiveAmount, 42); + BOOST_CHECK_EQUAL(fixture.getClaimsForName("test").find(claimId2).effectiveAmount, 7); // Two claims, both with supports, second one taking over fixture.MakeSupport(fixture.GetCoinbase(), claimtx2, "test", 1330); fixture.IncrementBlocks(26); //delay - BOOST_CHECK_EQUAL(fixture.getEffectiveAmountForClaim("test", claimId), 42); - BOOST_CHECK_EQUAL(fixture.getEffectiveAmountForClaim("test", claimId2), 1337); + BOOST_CHECK_EQUAL(fixture.getClaimsForName("test").find(claimId).effectiveAmount, 42); + BOOST_CHECK_EQUAL(fixture.getClaimsForName("test").find(claimId2).effectiveAmount, 1337); } /* - * tests for CClaimPrefixTrie::getClaimById basic consistency checks + * tests for getClaimById basic consistency checks */ BOOST_AUTO_TEST_CASE(get_claim_by_id_test) { @@ -1111,7 +1112,7 @@ BOOST_AUTO_TEST_CASE(get_claim_by_id_test) CClaimValue claimValue; std::string claimName; - fixture.getClaimById(claimId, claimName, claimValue); + BOOST_CHECK(getClaimById(claimId, claimName, &claimValue)); BOOST_CHECK_EQUAL(claimName, name); BOOST_CHECK_EQUAL(claimValue.claimId, claimId); @@ -1122,14 +1123,14 @@ BOOST_AUTO_TEST_CASE(get_claim_by_id_test) uint160 claimId2 = ClaimIdHash(tx2.GetHash(), 0); fixture.IncrementBlocks(1); - fixture.getClaimById(claimId2, claimName, claimValue); + BOOST_CHECK(getClaimById(claimId2, claimName, &claimValue)); BOOST_CHECK_EQUAL(claimName, name); BOOST_CHECK_EQUAL(claimValue.claimId, claimId2); CMutableTransaction u1 = fixture.MakeUpdate(tx1, name, "updated one", claimId, 1); fixture.IncrementBlocks(1); - fixture.getClaimById(claimId, claimName, claimValue); + BOOST_CHECK(getClaimById(claimId, claimName, &claimValue)); BOOST_CHECK_EQUAL(claimName, name); BOOST_CHECK_EQUAL(claimValue.claimId, claimId); BOOST_CHECK_EQUAL(claimValue.nAmount, 1); @@ -1137,24 +1138,24 @@ BOOST_AUTO_TEST_CASE(get_claim_by_id_test) fixture.Spend(u1); fixture.IncrementBlocks(1); - BOOST_CHECK(!fixture.getClaimById(claimId, claimName, claimValue)); + BOOST_CHECK(!getClaimById(claimId, claimName, &claimValue)); fixture.DecrementBlocks(3); CClaimValue claimValue2; claimName = ""; - fixture.getClaimById(claimId2, claimName, claimValue2); + BOOST_CHECK(!getClaimById(claimId2, claimName, &claimValue2)); BOOST_CHECK(claimName != name); BOOST_CHECK(claimValue2.claimId != claimId2); - fixture.getClaimById(claimId, claimName, claimValue); + BOOST_CHECK(getClaimById(claimId, claimName, &claimValue)); BOOST_CHECK_EQUAL(claimName, name); BOOST_CHECK_EQUAL(claimValue.claimId, claimId); fixture.DecrementBlocks(2); claimName = ""; - fixture.getClaimById(claimId, claimName, claimValue2); + BOOST_CHECK(!getClaimById(claimId, claimName, &claimValue2)); BOOST_CHECK(claimName != name); BOOST_CHECK(claimValue2.claimId != claimId); } @@ -1535,7 +1536,7 @@ BOOST_AUTO_TEST_CASE(claimtriecache_normalization) CClaimValue lookupClaim; std::string lookupName; - BOOST_CHECK(fixture.getClaimById(ClaimIdHash(tx2.GetHash(), 0), lookupName, lookupClaim)); + BOOST_CHECK(getClaimById(ClaimIdHash(tx2.GetHash(), 0), lookupName, &lookupClaim)); CClaimValue nval1; BOOST_CHECK(fixture.getInfoForName("amelie1", nval1)); // ameĢlie is not found cause normalization still not appear @@ -1614,13 +1615,13 @@ BOOST_AUTO_TEST_CASE(normalized_activations_fall_through) BOOST_CHECK(fixture.is_best_claim("AB", tx1)); fixture.IncrementBlocks(3); BOOST_CHECK(fixture.is_best_claim("ab", tx2)); - BOOST_CHECK(fixture.getClaimsForName("ab").claims.size() == 4U); + BOOST_CHECK(fixture.getClaimsForName("ab").claimsNsupports.size() == 4U); fixture.DecrementBlocks(3); fixture.Spend(tx1); fixture.Spend(tx2); fixture.IncrementBlocks(1); BOOST_CHECK(fixture.is_best_claim("ab", tx3)); - BOOST_CHECK(fixture.getClaimsForName("ab").claims.size() == 2U); + BOOST_CHECK(fixture.getClaimsForName("ab").claimsNsupports.size() == 2U); fixture.DecrementBlocks(1); BOOST_CHECK(fixture.is_best_claim("AB", tx1)); fixture.Spend(tx1); @@ -1666,8 +1667,10 @@ BOOST_AUTO_TEST_CASE(normalization_removal_test) supportQueueRowType expireSupportUndo; std::vector > takeoverHeightUndo; BOOST_CHECK(cache.incrementBlock(insertUndo, expireUndo, insertSupportUndo, expireSupportUndo, takeoverHeightUndo)); - BOOST_CHECK(cache.getClaimsForName("ab").claims.size() == 3U); - BOOST_CHECK(cache.getClaimsForName("ab").supports.size() == 2U); + BOOST_CHECK(cache.getClaimsForName("ab").claimsNsupports.size() == 3U); + BOOST_CHECK(cache.getClaimsForName("ab").claimsNsupports[0].supports.size() == 1U); + BOOST_CHECK(cache.getClaimsForName("ab").claimsNsupports[1].supports.size() == 0U); + BOOST_CHECK(cache.getClaimsForName("ab").claimsNsupports[2].supports.size() == 1U); BOOST_CHECK(cache.decrementBlock(insertUndo, expireUndo, insertSupportUndo, expireSupportUndo)); BOOST_CHECK(cache.finalizeDecrement(takeoverHeightUndo)); BOOST_CHECK(cache.undoAddSupport("AB", COutPoint(sx1.GetHash(), 0), height)); @@ -1675,7 +1678,7 @@ BOOST_AUTO_TEST_CASE(normalization_removal_test) BOOST_CHECK(cache.undoAddClaim("AB", COutPoint(tx1.GetHash(), 0), height)); BOOST_CHECK(cache.undoAddClaim("Ab", COutPoint(tx2.GetHash(), 0), height)); BOOST_CHECK(cache.undoAddClaim("aB", COutPoint(tx3.GetHash(), 0), height)); - BOOST_CHECK(cache.getClaimsForName("ab").claims.size() == 0U); + BOOST_CHECK(cache.getClaimsForName("ab").claimsNsupports.size() == 0U); } BOOST_AUTO_TEST_CASE(normalization_does_not_kill_supports) @@ -1745,7 +1748,7 @@ BOOST_AUTO_TEST_CASE(normalization_does_not_kill_sort_order) fixture.IncrementBlocks(1); BOOST_CHECK(!fixture.is_best_claim("A", tx2)); BOOST_CHECK(fixture.is_best_claim("a", tx3)); - BOOST_CHECK(fixture.getClaimsForName("a").claims.size() == 3U); + BOOST_CHECK(fixture.getClaimsForName("a").claimsNsupports.size() == 3U); fixture.DecrementBlocks(1); BOOST_CHECK(fixture.is_best_claim("A", tx2)); @@ -3837,7 +3840,7 @@ BOOST_AUTO_TEST_CASE(bogus_claimtrie_hash_test) } /* - * tests for CClaimPrefixTrie::getClaimById basic consistency checks + * tests for getClaimById basic consistency checks */ BOOST_AUTO_TEST_CASE(get_claim_by_id_test_2) { @@ -3852,7 +3855,7 @@ BOOST_AUTO_TEST_CASE(get_claim_by_id_test_2) CClaimValue claimValue; std::string claimName; - fixture.getClaimById(claimId, claimName, claimValue); + BOOST_CHECK(getClaimById(claimId, claimName, &claimValue)); BOOST_CHECK_EQUAL(claimName, name); BOOST_CHECK_EQUAL(claimValue.claimId, claimId); @@ -3860,16 +3863,16 @@ BOOST_AUTO_TEST_CASE(get_claim_by_id_test_2) CMutableTransaction txb = fixture.Spend(txx); fixture.IncrementBlocks(1); - BOOST_CHECK(!fixture.getClaimById(claimId, claimName, claimValue)); - BOOST_CHECK(!fixture.getClaimById(claimIdx, claimName, claimValue)); + BOOST_CHECK(!getClaimById(claimId, claimName, &claimValue)); + BOOST_CHECK(!getClaimById(claimIdx, claimName, &claimValue)); fixture.DecrementBlocks(1); - fixture.getClaimById(claimId, claimName, claimValue); + BOOST_CHECK(getClaimById(claimId, claimName, &claimValue)); BOOST_CHECK_EQUAL(claimName, name); BOOST_CHECK_EQUAL(claimValue.claimId, claimId); // this test fails - fixture.getClaimById(claimIdx, claimName, claimValue); + BOOST_CHECK(getClaimById(claimIdx, claimName, &claimValue)); BOOST_CHECK_EQUAL(claimName, name); BOOST_CHECK_EQUAL(claimValue.claimId, claimIdx); } @@ -3886,7 +3889,7 @@ BOOST_AUTO_TEST_CASE(get_claim_by_id_test_3) CClaimValue claimValue; std::string claimName; - fixture.getClaimById(claimId, claimName, claimValue); + BOOST_CHECK(getClaimById(claimId, claimName, &claimValue)); BOOST_CHECK_EQUAL(claimName, name); BOOST_CHECK_EQUAL(claimValue.claimId, claimId); // make second claim with activation delay 1 @@ -3896,14 +3899,14 @@ BOOST_AUTO_TEST_CASE(get_claim_by_id_test_3) fixture.IncrementBlocks(1); // second claim is not activated yet, but can still access by claim id BOOST_CHECK(fixture.is_best_claim(name, tx1)); - BOOST_CHECK(fixture.getClaimById(claimId2, claimName, claimValue)); + BOOST_CHECK(getClaimById(claimId2, claimName, &claimValue)); BOOST_CHECK_EQUAL(claimName, name); BOOST_CHECK_EQUAL(claimValue.claimId, claimId2); fixture.IncrementBlocks(1); // second claim has activated BOOST_CHECK(fixture.is_best_claim(name, tx2)); - BOOST_CHECK(fixture.getClaimById(claimId2, claimName, claimValue)); + BOOST_CHECK(getClaimById(claimId2, claimName, &claimValue)); BOOST_CHECK_EQUAL(claimName, name); BOOST_CHECK_EQUAL(claimValue.claimId, claimId2); @@ -3912,14 +3915,14 @@ BOOST_AUTO_TEST_CASE(get_claim_by_id_test_3) // second claim has been deactivated via decrement // should still be accesible via claim id BOOST_CHECK(fixture.is_best_claim(name, tx1)); - BOOST_CHECK(fixture.getClaimById(claimId2, claimName, claimValue)); + BOOST_CHECK(getClaimById(claimId2, claimName, &claimValue)); BOOST_CHECK_EQUAL(claimName, name); BOOST_CHECK_EQUAL(claimValue.claimId, claimId2); fixture.IncrementBlocks(1); // second claim should have been re activated via increment BOOST_CHECK(fixture.is_best_claim(name, tx2)); - BOOST_CHECK(fixture.getClaimById(claimId2, claimName, claimValue)); + BOOST_CHECK(getClaimById(claimId2, claimName, &claimValue)); BOOST_CHECK_EQUAL(claimName, name); BOOST_CHECK_EQUAL(claimValue.claimId, claimId2); } @@ -3970,13 +3973,13 @@ BOOST_AUTO_TEST_CASE(getvalueforname_test) UniValue results = getvalueforname(req); BOOST_CHECK_EQUAL(results["value"].get_str(), HexStr(sValue1)); BOOST_CHECK_EQUAL(results["amount"].get_int(), 2); - BOOST_CHECK_EQUAL(results["effective amount"].get_int(), 5); + BOOST_CHECK_EQUAL(results["effectiveAmount"].get_int(), 5); req.params.push_back(blockHash.GetHex()); results = getvalueforname(req); BOOST_CHECK_EQUAL(results["amount"].get_int(), 2); - BOOST_CHECK_EQUAL(results["effective amount"].get_int(), 2); + BOOST_CHECK_EQUAL(results["effectiveAmount"].get_int(), 2); } BOOST_AUTO_TEST_CASE(getclaimsforname_test) @@ -4004,8 +4007,8 @@ BOOST_AUTO_TEST_CASE(getclaimsforname_test) UniValue results = getclaimsforname(req); UniValue claims = results["claims"]; BOOST_CHECK_EQUAL(claims.size(), 1U); - BOOST_CHECK_EQUAL(results["nLastTakeoverHeight"].get_int(), height + 1); - BOOST_CHECK_EQUAL(claims[0]["nEffectiveAmount"].get_int(), 2); + BOOST_CHECK_EQUAL(results["lastTakeoverHeight"].get_int(), height + 1); + BOOST_CHECK_EQUAL(claims[0]["effectiveAmount"].get_int(), 2); BOOST_CHECK_EQUAL(claims[0]["supports"].size(), 0U); fixture.IncrementBlocks(1); @@ -4013,9 +4016,9 @@ BOOST_AUTO_TEST_CASE(getclaimsforname_test) results = getclaimsforname(req); claims = results["claims"]; BOOST_CHECK_EQUAL(claims.size(), 2U); - BOOST_CHECK_EQUAL(results["nLastTakeoverHeight"].get_int(), height + 3); - BOOST_CHECK_EQUAL(claims[0]["nEffectiveAmount"].get_int(), 3); - BOOST_CHECK_EQUAL(claims[1]["nEffectiveAmount"].get_int(), 2); + BOOST_CHECK_EQUAL(results["lastTakeoverHeight"].get_int(), height + 3); + BOOST_CHECK_EQUAL(claims[0]["effectiveAmount"].get_int(), 3); + BOOST_CHECK_EQUAL(claims[1]["effectiveAmount"].get_int(), 2); BOOST_CHECK_EQUAL(claims[0]["supports"].size(), 0U); BOOST_CHECK_EQUAL(claims[1]["supports"].size(), 0U); @@ -4024,8 +4027,8 @@ BOOST_AUTO_TEST_CASE(getclaimsforname_test) results = getclaimsforname(req); claims = results["claims"]; BOOST_CHECK_EQUAL(claims.size(), 1U); - BOOST_CHECK_EQUAL(results["nLastTakeoverHeight"].get_int(), height + 1); - BOOST_CHECK_EQUAL(claims[0]["nEffectiveAmount"].get_int(), 2); + BOOST_CHECK_EQUAL(results["lastTakeoverHeight"].get_int(), height + 1); + BOOST_CHECK_EQUAL(claims[0]["effectiveAmount"].get_int(), 2); BOOST_CHECK_EQUAL(claims[0]["supports"].size(), 0U); } @@ -4056,7 +4059,7 @@ BOOST_AUTO_TEST_CASE(claim_rpcs_rollback2_test) req.params.push_back(blockHash.GetHex()); UniValue claimsResults = getclaimsforname(req); - BOOST_CHECK_EQUAL(claimsResults["nLastTakeoverHeight"].get_int(), height + 5); + BOOST_CHECK_EQUAL(claimsResults["lastTakeoverHeight"].get_int(), height + 5); BOOST_CHECK_EQUAL(claimsResults["claims"][0]["supports"].size(), 0U); BOOST_CHECK_EQUAL(claimsResults["claims"][1]["supports"].size(), 0U); @@ -4097,7 +4100,7 @@ BOOST_AUTO_TEST_CASE(claim_rpcs_rollback3_test) req.params.push_back(blockHash.GetHex()); UniValue claimsResults = getclaimsforname(req); - BOOST_CHECK_EQUAL(claimsResults["nLastTakeoverHeight"].get_int(), height + 1); + BOOST_CHECK_EQUAL(claimsResults["lastTakeoverHeight"].get_int(), height + 1); UniValue valueResults = getvalueforname(req); BOOST_CHECK_EQUAL(valueResults["value"].get_str(), HexStr(sValue1)); @@ -4172,7 +4175,9 @@ BOOST_AUTO_TEST_CASE(hash_includes_all_claims_single_test) uint160 claimId = ClaimIdHash(tx1.GetHash(), 0); CClaimTrieProof proof; - BOOST_CHECK(fixture.getProofForName("test", proof, claimId)); + BOOST_CHECK(fixture.getProofForName("test", proof, [&claimId](const CClaimValue& claim) { + return claim.claimId == claimId; + })); BOOST_CHECK(proof.hasValue); BOOST_CHECK_EQUAL(proof.outPoint, outPoint); auto claimHash = getValueHash(outPoint, proof.nHeightOfLastTakeover); @@ -4197,9 +4202,12 @@ BOOST_AUTO_TEST_CASE(hash_includes_all_claims_triple_test) fixture.IncrementBlocks(1); for (const auto& name : names) { - for (auto& claim : fixture.getClaimsForName(name).claims) { + for (auto& claimSupports : fixture.getClaimsForName(name).claimsNsupports) { CClaimTrieProof proof; - BOOST_CHECK(fixture.getProofForName(name, proof, claim.claimId)); + auto& claim = claimSupports.claim; + BOOST_CHECK(fixture.getProofForName(name, proof, [&claim](const CClaimValue& value) { + return claim.claimId == value.claimId; + })); BOOST_CHECK(proof.hasValue); BOOST_CHECK_EQUAL(proof.outPoint, claim.outPoint); uint256 claimHash = getValueHash(claim.outPoint, proof.nHeightOfLastTakeover); @@ -4225,9 +4233,12 @@ BOOST_AUTO_TEST_CASE(hash_includes_all_claims_branched_test) fixture.IncrementBlocks(1); for (const auto& name : names) { - for (auto& claim : fixture.getClaimsForName(name).claims) { + for (auto& claimSupports : fixture.getClaimsForName(name).claimsNsupports) { CClaimTrieProof proof; - BOOST_CHECK(fixture.getProofForName(name, proof, claim.claimId)); + auto& claim = claimSupports.claim; + BOOST_CHECK(fixture.getProofForName(name, proof, [&claim](const CClaimValue& value) { + return claim.claimId == value.claimId; + })); BOOST_CHECK(proof.hasValue); BOOST_CHECK_EQUAL(proof.outPoint, claim.outPoint); uint256 claimHash = getValueHash(claim.outPoint, proof.nHeightOfLastTakeover); @@ -4257,9 +4268,12 @@ BOOST_AUTO_TEST_CASE(hash_claims_children_fuzzer_test) } for (const auto& name : names) { - for (auto& claim : fixture.getClaimsForName(name).claims) { + for (auto& claimSupports : fixture.getClaimsForName(name).claimsNsupports) { CClaimTrieProof proof; - BOOST_CHECK(fixture.getProofForName(name, proof, claim.claimId)); + auto& claim = claimSupports.claim; + BOOST_CHECK(fixture.getProofForName(name, proof, [&claim](const CClaimValue& value) { + return claim.claimId == value.claimId; + })); BOOST_CHECK(proof.hasValue); BOOST_CHECK_EQUAL(proof.outPoint, claim.outPoint); uint256 claimHash = getValueHash(claim.outPoint, proof.nHeightOfLastTakeover); diff --git a/src/test/claimtriecache_tests.cpp b/src/test/claimtriecache_tests.cpp index 38906a9b8..697b6e0af 100644 --- a/src/test/claimtriecache_tests.cpp +++ b/src/test/claimtriecache_tests.cpp @@ -210,13 +210,13 @@ BOOST_AUTO_TEST_CASE(basic_insertion_info_test) CClaimValue claimVal(claimOutPoint, claimId, amount, height, validHeight); ctc.insertClaimIntoTrie("test", claimVal, true); - // try getClaimsForName, getEffectiveAmountForClaim, getInfoForName + // try getClaimsForName, effectiveAmount, getInfoForName auto res = ctc.getClaimsForName("test"); - BOOST_CHECK_EQUAL(res.claims.size(), 1); - BOOST_CHECK_EQUAL(res.claims[0], claimVal); - BOOST_CHECK_EQUAL(res.supports.size(), 0); + BOOST_CHECK_EQUAL(res.claimsNsupports.size(), 1); + BOOST_CHECK_EQUAL(res.claimsNsupports[0].claim, claimVal); + BOOST_CHECK_EQUAL(res.claimsNsupports[0].supports.size(), 0); - BOOST_CHECK_EQUAL(10, ctc.getEffectiveAmountForClaim("test", claimId)); + BOOST_CHECK_EQUAL(10, res.claimsNsupports[0].effectiveAmount); CClaimValue claim; BOOST_CHECK(ctc.getInfoForName("test", claim)); @@ -231,12 +231,12 @@ BOOST_AUTO_TEST_CASE(basic_insertion_info_test) CSupportValue support(supportOutPoint, claimId, supportAmount, height, validHeight); ctc.insertSupportIntoMap("test", support, false); - res = ctc.getClaimsForName("test"); - BOOST_CHECK_EQUAL(res.claims.size(), 1); - BOOST_CHECK_EQUAL(res.supports.size(), 1); + auto res1 = ctc.getClaimsForName("test"); + BOOST_CHECK_EQUAL(res1.claimsNsupports.size(), 1); + BOOST_CHECK_EQUAL(res1.claimsNsupports[0].supports.size(), 1); // try getEffectiveAmount - BOOST_CHECK_EQUAL(20, ctc.getEffectiveAmountForClaim("test", claimId)); + BOOST_CHECK_EQUAL(20, res1.claimsNsupports[0].effectiveAmount); } BOOST_AUTO_TEST_CASE(recursive_prune_test) -- 2.45.3 From 09f6103649aa33a8ba53a05fea6767780edaf530 Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Fri, 9 Aug 2019 14:43:03 +0300 Subject: [PATCH 3/8] Split help and rpc methods Use constants for field names Signed-off-by: Anthony Fieroni --- src/Makefile.am | 1 + src/rpc/claimrpchelp.h | 290 ++++++++++++++ src/rpc/claimtrie.cpp | 547 ++++++-------------------- src/test/claimtriebranching_tests.cpp | 55 +-- 4 files changed, 448 insertions(+), 445 deletions(-) create mode 100644 src/rpc/claimrpchelp.h diff --git a/src/Makefile.am b/src/Makefile.am index e88e21a60..a030b6dc4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -155,6 +155,7 @@ BITCOIN_CORE_H = \ reverse_iterator.h \ reverselock.h \ rpc/blockchain.h \ + rpc/claimrpchelp.h \ rpc/client.h \ rpc/mining.h \ rpc/protocol.h \ diff --git a/src/rpc/claimrpchelp.h b/src/rpc/claimrpchelp.h new file mode 100644 index 000000000..28202f8bb --- /dev/null +++ b/src/rpc/claimrpchelp.h @@ -0,0 +1,290 @@ + +#include + +// always keep defines T_ + value in upper case +#define T_NORMALIZEDNAME "normalizedName" +#define T_BLOCKHASH "blockhash" +#define T_CLAIMS "claims" +#define T_CLAIMID "claimId" +#define T_TXID "txId" +#define T_N "n" +#define T_AMOUNT "amount" +#define T_HEIGHT "height" +#define T_VALUE "value" +#define T_NAME "name" +#define T_VALIDATHEIGHT "validAtHeight" +#define T_NAMES "names" +#define T_EFFECTIVEAMOUNT "effectiveAmount" +#define T_LASTTAKEOVERHEIGHT "lastTakeoverHeight" +#define T_SUPPORTS "supports" +#define T_SUPPORTSWITHOUTCLAIM "supportsWithoutClaim" +#define T_TOTALNAMES "totalNames" +#define T_TOTALCLAIMS "totalClaims" +#define T_TOTALVALUE "totalValue" +#define T_CONTROLLINGONLY "controllingOnly" +#define T_CLAIMTYPE "claimType" +#define T_DEPTH "depth" +#define T_INCLAIMTRIE "inClaimTrie" +#define T_ISCONTROLLING "isControlling" +#define T_INSUPPORTMAP "inSupportMap" +#define T_INQUEUE "inQueue" +#define T_BLOCKSTOVALID "blocksToValid" +#define T_NODES "nodes" +#define T_CHILDREN "children" +#define T_CHARACTER "character" +#define T_NODEHASH "nodeHash" +#define T_VALUEHASH "valueHash" +#define T_PAIRS "pairs" +#define T_ODD "odd" +#define T_HASH "hash" + +enum { + GETCLAIMSINTRIE = 0, + GETNAMESINTRIE, + GETVALUEFORNAME, + GETCLAIMSFORNAME, + GETCLAIMBYID, + GETTOTALCLAIMEDNAMES, + GETTOTALCLAIMS, + GETTOTALVALUEOFCLAIMS, + GETCLAIMSFORTX, + GETNAMEPROOF, + CHECKNORMALIZATION, +}; + +static const char* const rpc_help[] = { + +// GETCLAIMSINTRIE +R"(getclaimsintrie +Return all claims in the name trie. Deprecated +Arguments: +1. ")" T_BLOCKHASH R"(" (string, optional) get claims in the trie + at the block specified + by this block hash. + If none is given, + the latest active + block will be used. +Result: [ + ")" T_NORMALIZEDNAME R"(" (string) the name of the claim(s) (after normalization) + ")" T_CLAIMS R"(": [ (array of object) the claims for this name + ")" T_NAME R"(" (string) the original name of this claim (before normalization) + ")" T_VALUE R"(" (string) the value of this claim + ")" T_CLAIMID R"(" (string) the claimId of the claim + ")" T_TXID R"(" (string) the txid of the claim + ")" T_N R"(" (numeric) the index of the claim in the transaction's list of outputs + ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located + ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the claim became/becomes valid + ")" T_AMOUNT R"(" (numeric) the amount of the claim + ] +])", + +// GETNAMESINTRIE +R"(getnamesintrie +Return all claim names in the trie. +Arguments: +1. ")" T_BLOCKHASH R"(" (string, optional) get claims in the trie + at the block specified + by this block hash. + If none is given, + the latest active + block will be used. +Result: [ + ")" T_NAMES R"(" all names in the trie that have claims +])", + +// GETVALUEFORNAME +R"(getvalueforname +Return the winning or specified by claimId value associated with a name +Arguments: +1. ")" T_NAME R"(" (string) the name to look up +2. ")" T_BLOCKHASH R"(" (string, optional) get the value + associated with the name + at the block specified + by this block hash. + If none is given, + the latest active + block will be used. +3. ")" T_CLAIMID R"(" (string, optional) can be partial one +Result: [ + ")" T_NORMALIZEDNAME R"(" (string) the name of the claim(s) (after normalization) + ")" T_NAME R"(" (string) the original name of this claim (before normalization) + ")" T_VALUE R"(" (string) the value of this claim + ")" T_CLAIMID R"(" (string) the claimId of the claim + ")" T_TXID R"(" (string) the txid of the claim + ")" T_N R"(" (numeric) the index of the claim in the transaction's list of outputs + ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located + ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the support became/becomes valid + ")" T_AMOUNT R"(" (numeric) the amount of the claim + ")" T_EFFECTIVEAMOUNT R"(" (numeric) the amount plus amount from all supports associated with the claim + ")" T_SUPPORTS R"(": [ (array of object) supports for this claim + ")" T_VALUE R"(" (string) the metadata of the support if any + ")" T_TXID R"(" (string) the txid of the support + ")" T_N R"(" (numeric) the index of the support in the transaction's list of outputs + ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located + ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the support became/becomes valid + ")" T_AMOUNT R"(" (numeric) the amount of the support + ] + ")" T_LASTTAKEOVERHEIGHT R"(" (numeric) the last height at which ownership of the name changed +])", + +// GETCLAIMSFORNAME +R"(getclaimsforname +Return all claims and supports for a name +Arguments: +1. ")" T_NAME R"(" (string) the name to look up +2. ")" T_BLOCKHASH R"(" (string, optional) get claims in the trie + at the block specified + by this block hash. + If none is given, + the latest active + block will be used. +Result: [ + ")" T_NORMALIZEDNAME R"(" (string) the name of the claim(s) (after normalization) + ")" T_CLAIMS R"(": [ (array of object) the claims for this name + ")" T_NAME R"(" (string) the original name of this claim (before normalization) + ")" T_VALUE R"(" (string) the value of this claim + ")" T_CLAIMID R"(" (string) the claimId of the claim + ")" T_TXID R"(" (string) the txid of the claim + ")" T_N R"(" (numeric) the index of the claim in the transaction's list of outputs + ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located + ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the claim became/becomes valid + ")" T_AMOUNT R"(" (numeric) the amount of the claim + ")" T_EFFECTIVEAMOUNT R"(" (numeric) the amount plus amount from all supports associated with the claim + ")" T_SUPPORTS R"(": [ (array of object) supports for this claim + ")" T_VALUE R"(" (string) the metadata of the support if any + ")" T_TXID R"(" (string) the txid of the support + ")" T_N R"(" (numeric) the index of the support in the transaction's list of outputs + ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located + ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the support became/becomes valid + ")" T_AMOUNT R"(" (numeric) the amount of the support + ] + ] + ")" T_LASTTAKEOVERHEIGHT R"(" (numeric) the last height at which ownership of the name changed + ")" T_SUPPORTSWITHOUTCLAIM R"(": [ + ")" T_TXID R"(" (string) the txid of the support + ")" T_N R"(" (numeric) the index of the support in the transaction's list of outputs + ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located + ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the support became/becomes valid + ")" T_AMOUNT R"(" (numeric) the amount of the support + ] +])", + +// GETCLAIMBYID +R"(getclaimbyid +Get a claim by claim id +Arguments: +1. ")" T_CLAIMID R"(" (string) the claimId of this claim or patial id (at least 3 chars) +Result: [ + ")" T_NORMALIZEDNAME R"(" (string) the name of the claim(s) (after normalization) + ")" T_NAME R"(" (string) the original name of this claim (before normalization) + ")" T_VALUE R"(" (string) the value of this claim + ")" T_CLAIMID R"(" (string) the claimId of the claim + ")" T_TXID R"(" (string) the txid of the claim + ")" T_N R"(" (numeric) the index of the claim in the transaction's list of outputs + ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located + ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the support became/becomes valid + ")" T_AMOUNT R"(" (numeric) the amount of the claim + ")" T_EFFECTIVEAMOUNT R"(" (numeric) the amount plus amount from all supports associated with the claim + ")" T_SUPPORTS R"(": [ (array of object) supports for this claim + ")" T_VALUE R"(" (string) the metadata of the support if any + ")" T_TXID R"(" (string) the txid of the support + ")" T_N R"(" (numeric) the index of the support in the transaction's list of outputs + ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located + ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the support became/becomes valid + ")" T_AMOUNT R"(" (numeric) the amount of the support + ] + ")" T_LASTTAKEOVERHEIGHT R"(" (numeric) the last height at which ownership of the name changed +])", + +// GETTOTALCLAIMEDNAMES + +R"(gettotalclaimednames +Return the total number of names that have been +Arguments: +Result: + ")" T_TOTALNAMES R"(" (numeric) the total number of names in the trie +)", + +// GETTOTALCLAIMS +R"(gettotalclaims +Return the total number of active claims in the trie +Arguments: +Result: + ")" T_TOTALCLAIMS R"(" (numeric) the total number of active claims +)", + +// GETTOTALVALUEOFCLAIMS +R"(gettotalvalueofclaims +Return the total value of the claims in the trie +Arguments: +1. ")" T_CONTROLLINGONLY R"(" (boolean) only include the value of controlling claims +Result: + ")" T_TOTALVALUE R"(" (numeric) the total value of the claims in the trie +)", + +// GETCLAIMSFORTX +R"(getclaimsfortx +Return any claims or supports found in a transaction +Arguments: +1. ")" T_TXID R"(" (string) the txid of the transaction to check for unspent claims +Result: [ + ")" T_N R"(" (numeric) the index of the claim in the transaction's list of outputs + ")" T_CLAIMTYPE R"(" (string) claim or support + ")" T_NAME R"(" (string) the name claimed or supported + ")" T_CLAIMID R"(" (string) if a claim, its ID + ")" T_VALUE R"(" (string) if a claim, its value + ")" T_DEPTH R"(" (numeric) the depth of the transaction in the main chain + ")" T_INCLAIMTRIE R"(" (boolean) if a name claim, whether the claim is active, i.e. has made it into the trie + ")" T_ISCONTROLLING R"(" (boolean) if a name claim, whether the claim is the current controlling claim for the name + ")" T_INSUPPORTMAP R"(" (boolean) if a support, whether the support is active, i.e. has made it into the support map + ")" T_INQUEUE R"(" (boolean) whether the claim is in a queue waiting to be inserted into the trie or support map + ")" T_BLOCKSTOVALID R"(" (numeric) if in a queue, the number of blocks until it's inserted into the trie or support map +])", + +// GETNAMEPROOF +R"(getnameproof +Return the cryptographic proof that a name maps to a value or doesn't. +Arguments: +1. ")" T_NAME R"(" (string) the name to look up +2. ")" T_BLOCKHASH R"(" (string, optional) get the value + associated with the name + at the block specified + by this block hash. + If none is given, + the latest active + block will be used. +3. ")" T_CLAIMID R"(" (string, optional, post-fork) for validating a specific claim + can be partial one +Result: [ + ")" T_NODES R"(": [ (array of object, pre-fork) full nodes + (i.e. those which lead to the requested name) + ")" T_CHILDREN R"(": [ (array of object) the children of the node + ")" T_CHARACTER R"(" (string) the character which leads from the parent + to this child node + ")" T_NODEHASH R"(" (string, if exists) the hash of the node if this is a leaf node + ] + ")" T_VALUEHASH R"(" (string, if exists) the hash of this node's value, if + it has one. If this is the requested name this + will not exist whether the node has a value or not + ] + ")" T_PAIRS R"(": [ (array of pairs, post-fork) hash can be validated by + hashing claim from the bottom up + ")" T_ODD R"(" (boolean) this value goes on the right of hash + ")" T_HASH R"(" (string) the hash to be mixed in + ] + ")" T_TXID R"(" (string, if exists) the txid of the claim which controls + this name, if there is one. + ")" T_N R"(" (numeric) the index of the claim in the transaction's list of outputs + ")" T_LASTTAKEOVERHEIGHT R"(" (numeric) the last height at which ownership of the name changed +])", + +// CHECKNORMALIZATION +R"(checknormalization +Given an unnormalized name of a claim, return normalized version of it +Arguments: +1. ")" T_NAME R"(" (string) the name to normalize +Result: +")" T_NORMALIZEDNAME R"(" (string) normalized name +)", + +}; diff --git a/src/rpc/claimtrie.cpp b/src/rpc/claimtrie.cpp index 479c192ae..d9ca050cd 100644 --- a/src/rpc/claimtrie.cpp +++ b/src/rpc/claimtrie.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -24,6 +25,8 @@ void ParseClaimtrieId(const UniValue& v, std::string& partialId, uint160& claimI strHex = v.get_str(); if (!IsHex(strHex)) throw JSONRPCError(RPC_INVALID_PARAMETER, strName + " must be a hexadecimal string"); + if (strHex.length() > claimIdHexLength) + throw JSONRPCError(RPC_INVALID_PARAMETER, strName + " must be max 20-character hexadecimal string"); if (strHex.length() == claimIdHexLength) claimId.SetHex(strHex); else @@ -173,18 +176,18 @@ UniValue claimToJSON(const CCoinsViewCache& coinsCache, const CClaimValue& claim std::string targetName; if (getClaimById(claim.claimId, targetName)) - result.pushKV("name", escapeNonUtf8(targetName)); + result.pushKV(T_NAME, escapeNonUtf8(targetName)); std::string sValue; if (getValueForOutPoint(coinsCache, claim.outPoint, sValue)) - result.pushKV("value", sValue); + result.pushKV(T_VALUE, sValue); - result.pushKV("claimId", claim.claimId.GetHex()); - result.pushKV("txId", claim.outPoint.hash.GetHex()); - result.pushKV("n", (int)claim.outPoint.n); - result.pushKV("height", claim.nHeight); - result.pushKV("validAtHeight", claim.nValidAtHeight); - result.pushKV("amount", claim.nAmount); + result.pushKV(T_CLAIMID, claim.claimId.GetHex()); + result.pushKV(T_TXID, claim.outPoint.hash.GetHex()); + result.pushKV(T_N, (int)claim.outPoint.n); + result.pushKV(T_HEIGHT, claim.nHeight); + result.pushKV(T_VALIDATHEIGHT, claim.nValidAtHeight); + result.pushKV(T_AMOUNT, claim.nAmount); return result; } @@ -195,13 +198,13 @@ UniValue supportToJSON(const CCoinsViewCache& coinsCache, const CSupportValue& s std::string value; if (getValueForOutPoint(coinsCache, support.outPoint, value)) - ret.pushKV("value", value); + ret.pushKV(T_VALUE, value); - ret.pushKV("txId", support.outPoint.hash.GetHex()); - ret.pushKV("n", (int)support.outPoint.n); - ret.pushKV("height", support.nHeight); - ret.pushKV("validAtHeight", support.nValidAtHeight); - ret.pushKV("amount", support.nAmount); + ret.pushKV(T_TXID, support.outPoint.hash.GetHex()); + ret.pushKV(T_N, (int)support.outPoint.n); + ret.pushKV(T_HEIGHT, support.nHeight); + ret.pushKV(T_VALIDATHEIGHT, support.nValidAtHeight); + ret.pushKV(T_AMOUNT, support.nAmount); return ret; } @@ -212,17 +215,14 @@ UniValue claimAndSupportsToJSON(const CCoinsViewCache& coinsCache, const CClaimN auto& supports = claimNsupports.supports; auto result = claimToJSON(coinsCache, claim); - result.pushKV("effectiveAmount", claimNsupports.effectiveAmount); - - if (claim.nEffectiveAmount > claimNsupports.effectiveAmount) - result.pushKV("pendingEffectiveAmount", claim.nEffectiveAmount); + result.pushKV(T_EFFECTIVEAMOUNT, claimNsupports.effectiveAmount); UniValue supportObjs(UniValue::VARR); for (auto& support : supports) supportObjs.push_back(supportToJSON(coinsCache, support)); if (!supportObjs.empty()) - result.pushKV("supports", supportObjs); + result.pushKV(T_SUPPORTS, supportObjs); return result; } @@ -233,48 +233,25 @@ bool validParams(const UniValue& params, uint8_t required, uint8_t optional) return count >= required && count <= required + optional; } +void validateRequest(const JSONRPCRequest& request, int findex, uint8_t required, uint8_t optional) +{ + if (request.fHelp || !validParams(request.params, required, optional)) + throw std::runtime_error(rpc_help[findex]); +} + static UniValue getclaimsintrie(const JSONRPCRequest& request) { - if (request.fHelp || request.params.size() > 1) - throw std::runtime_error( - "getclaimsintrie\n" - "Return all claims in the name trie. Deprecated.\n" - "Arguments:\n" - "1. \"blockhash\" (string, optional) get claims in the trie\n" - " at the block specified\n" - " by this block hash.\n" - " If none is given,\n" - " the latest active\n" - " block will be used.\n" - "Result: \n" - "[\n" - " {\n" - " \"normalized_name\" (string) the name of these claims (after normalization)\n" - " \"claims\": [ (array of object) the claims for this name\n" - " {\n" - " \"claimId\" (string) the claimId of the claim\n" - " \"txid\" (string) the txid of the claim\n" - " \"n\" (numeric) the vout value of the claim\n" - " \"amount\" (numeric) txout amount\n" - " \"height\" (numeric) the height of the block in which this transaction is located\n" - " \"value\" (string) the value of this claim\n" - " \"name\" (string) the original name of this claim (before normalization)\n" - " }\n" - " ]\n" - " }\n" - "]\n"); + validateRequest(request, GETCLAIMSINTRIE, 0, 1); if (!IsDeprecatedRPCEnabled("getclaimsintrie")) { const auto msg = "getclaimsintrie is deprecated and will be removed in v0.18. To use this command, start with -deprecatedrpc=getclaimsintrie"; - if (request.fHelp) { + if (request.fHelp) throw std::runtime_error(msg); - } throw JSONRPCError(RPC_METHOD_DEPRECATED, msg); } UniValue ret(UniValue::VARR); LOCK(cs_main); - CCoinsViewCache coinsCache(pcoinsTip.get()); CClaimTrieCache trieCache(pclaimTrie); @@ -297,8 +274,8 @@ static UniValue getclaimsintrie(const JSONRPCRequest& request) claims.push_back(claimToJSON(coinsCache, claim)); UniValue nodeObj(UniValue::VOBJ); - nodeObj.pushKV("normalized_name", escapeNonUtf8(name)); - nodeObj.pushKV("claims", claims); + nodeObj.pushKV(T_NORMALIZEDNAME, escapeNonUtf8(name)); + nodeObj.pushKV(T_CLAIMS, claims); ret.push_back(nodeObj); }); return ret; @@ -312,22 +289,9 @@ static UniValue getclaimtrie(const JSONRPCRequest& request) static UniValue getnamesintrie(const JSONRPCRequest& request) { - if (request.fHelp || !validParams(request.params, 0, 1)) - throw std::runtime_error( - "getnamesintrie\n" - "Return all claim names in the trie.\n" - "Arguments:\n" - "1. \"blockhash\" (string, optional) get claims in the trie\n" - " at the block specified\n" - " by this block hash.\n" - " If none is given,\n" - " the latest active\n" - " block will be used.\n" - "Result: \n" - "\"names\" (array) all names in the trie that have claims\n"); + validateRequest(request, GETNAMESINTRIE, 0, 1); LOCK(cs_main); - CCoinsViewCache coinsCache(pcoinsTip.get()); CClaimTrieCache trieCache(pclaimTrie); @@ -351,32 +315,9 @@ static UniValue getnamesintrie(const JSONRPCRequest& request) static UniValue getvalueforname(const JSONRPCRequest& request) { - if (request.fHelp || !validParams(request.params, 1, 2)) - throw std::runtime_error( - "getvalueforname \"name\"\n" - "Return the winning or specified by claimId value associated with a name\n" - "Arguments:\n" - "1. \"name\" (string) the name to look up\n" - "2. \"blockhash\" (string, optional) get the value\n" - " associated with the name\n" - " at the block specified\n" - " by this block hash.\n" - " If none is given,\n" - " the latest active\n" - " block will be used.\n" - "3. \"claimId\" (string, optional) can be partial one\n" - "Result: \n" - "\"value\" (string) the value of the name, if it exists\n" - "\"claimId\" (string) the claimId for this name claim\n" - "\"txid\" (string) the hash of the transaction which successfully claimed the name\n" - "\"n\" (numeric) vout value\n" - "\"amount\" (numeric) txout amount\n" - "\"effective amount\" (numeric) txout amount plus amount from all supports associated with the claim\n" - "\"height\" (numeric) the height of the block in which this transaction is located\n" - "\"name\" (string) the original name of this claim (before normalization)\n"); + validateRequest(request, GETVALUEFORNAME, 1, 2); LOCK(cs_main); - CCoinsViewCache coinsCache(pcoinsTip.get()); CClaimTrieCache trieCache(pclaimTrie); @@ -404,65 +345,18 @@ static UniValue getvalueforname(const JSONRPCRequest& request) if (claimNsupports.IsNull()) return ret; - ret.pushKV("normalizedName", escapeNonUtf8(res.name)); + ret.pushKV(T_NORMALIZEDNAME, escapeNonUtf8(res.name)); ret.pushKVs(claimAndSupportsToJSON(coinsCache, claimNsupports)); - ret.pushKV("lastTakeoverHeight", res.nLastTakeoverHeight); + ret.pushKV(T_LASTTAKEOVERHEIGHT, res.nLastTakeoverHeight); return ret; } UniValue getclaimsforname(const JSONRPCRequest& request) { - if (request.fHelp || !validParams(request.params, 1, 1)) - throw std::runtime_error( - "getclaimsforname\n" - "Return all claims and supports for a name\n" - "Arguments: \n" - "1. \"name\" (string) the name for which to get claims and supports\n" - "2. \"blockhash\" (string, optional) get claims for name\n" - " at the block specified\n" - " by this block hash.\n" - " If none is given,\n" - " the latest active\n" - " block will be used.\n" - "Result:\n" - "{\n" - " \"nLastTakeoverHeight\" (numeric) the last height at which ownership of the name changed\n" - " \"normalized_name\" (string) the name of these claims after normalization\n" - " \"claims\": [ (array of object) claims for this name\n" - " {\n" - " \"claimId\" (string) the claimId of this claim\n" - " \"txid\" (string) the txid of this claim\n" - " \"n\" (numeric) the index of the claim in the transaction's list of outputs\n" - " \"nHeight\" (numeric) the height at which the claim was included in the blockchain\n" - " \"nValidAtHeight\" (numeric) the height at which the claim became/becomes valid\n" - " \"nAmount\" (numeric) the amount of the claim\n" - " \"value\" (string) the metadata of the claim\n" - " \"nEffectiveAmount\" (numeric) the total effective amount of the claim, taking into effect whether the claim or support has reached its nValidAtHeight\n" - " \"supports\" : [ (array of object) supports for this claim\n" - " \"txid\" (string) the txid of the support\n" - " \"n\" (numeric) the index of the support in the transaction's list of outputs\n" - " \"nHeight\" (numeric) the height at which the support was included in the blockchain\n" - " \"nValidAtHeight\" (numeric) the height at which the support became/becomes valid\n" - " \"nAmount\" (numeric) the amount of the support\n" - " \"value\" (string) the metadata of the support if any\n" - " ]\n" - " \"name\" (string) the original name of this claim before normalization\n" - " }\n" - " ],\n" - " \"supports without claims\": [ (array of object) supports that did not match a claim for this name\n" - " {\n" - " \"txid\" (string) the txid of the support\n" - " \"n\" (numeric) the index of the support in the transaction's list of outputs\n" - " \"nHeight\" (numeric) the height at which the support was included in the blockchain\n" - " \"nValidAtHeight\" (numeric) the height at which the support became/becomes valid\n" - " \"nAmount\" (numeric) the amount of the support\n" - " }\n" - " ]\n" - "}\n"); + validateRequest(request, GETCLAIMSFORNAME, 1, 1); LOCK(cs_main); - CCoinsViewCache coinsCache(pcoinsTip.get()); CClaimTrieCache trieCache(pclaimTrie); @@ -475,7 +369,7 @@ UniValue getclaimsforname(const JSONRPCRequest& request) auto claimsForName = trieCache.getClaimsForName(name); UniValue result(UniValue::VOBJ); - result.pushKV("normalized_name", escapeNonUtf8(claimsForName.name)); + result.pushKV(T_NORMALIZEDNAME, escapeNonUtf8(claimsForName.name)); UniValue claimObjs(UniValue::VARR); for (auto& claim : claimsForName.claimsNsupports) @@ -485,42 +379,15 @@ UniValue getclaimsforname(const JSONRPCRequest& request) for (auto& support : claimsForName.unmatchedSupports) unmatchedSupports.push_back(supportToJSON(coinsCache, support)); - result.pushKV("claims", claimObjs); - result.pushKV("lastTakeoverHeight", claimsForName.nLastTakeoverHeight); - result.pushKV("supportsWithoutClaims", unmatchedSupports); + result.pushKV(T_CLAIMS, claimObjs); + result.pushKV(T_LASTTAKEOVERHEIGHT, claimsForName.nLastTakeoverHeight); + result.pushKV(T_SUPPORTSWITHOUTCLAIM, unmatchedSupports); return result; } UniValue getclaimbyid(const JSONRPCRequest& request) { - if (request.fHelp || !validParams(request.params, 1, 0)) - throw std::runtime_error( - "getclaimbyid\n" - "Get a claim by claim id\n" - "Arguments: \n" - "1. \"claimId\" (string) the claimId of this claim or patial id (at least 3 chars)\n" - "Result:\n" - "{\n" - " \"name\" (string) the original name of the claim (before normalization)\n" - " \"normalized_name\" (string) the name of this claim (after normalization)\n" - " \"value\" (string) metadata of the claim\n" - " \"claimId\" (string) the claimId of this claim\n" - " \"txid\" (string) the hash of the transaction which has successfully claimed this name\n" - " \"n\" (numeric) vout value\n" - " \"amount\" (numeric) txout value\n" - " \"effective amount\" (numeric) txout amount plus amount from all supports associated with the claim\n" - " \"supports\" (array of object) supports for this claim\n" - " [\n" - " \"txid\" (string) the txid of the support\n" - " \"n\" (numeric) the index of the support in the transaction's list of outputs\n" - " \"height\" (numeric) the height at which the support was included in the blockchain\n" - " \"valid at height\" (numeric) the height at which the support is valid\n" - " \"amount\" (numeric) the amount of the support\n" - " \"value\" (string) the metadata of the support if any\n" - " ]\n" - " \"height\" (numeric) the height of the block in which this claim transaction is located\n" - " \"valid at height\" (numeric) the height at which the claim is valid\n" - "}\n"); + validateRequest(request, GETCLAIMBYID, 1, 0); LOCK(cs_main); CClaimTrieCache trieCache(pclaimTrie); @@ -540,9 +407,9 @@ UniValue getclaimbyid(const JSONRPCRequest& request) auto res = trieCache.getClaimsForName(name); auto& claimNsupports = !claimId.IsNull() ? res.find(claimId) : res.find(partialId); if (!claimNsupports.IsNull()) { - claim.pushKV("normalizedName", escapeNonUtf8(res.name)); + claim.pushKV(T_NORMALIZEDNAME, escapeNonUtf8(res.name)); claim.pushKVs(claimAndSupportsToJSON(coinsCache, claimNsupports)); - claim.pushKV("lastTakeoverHeight", res.nLastTakeoverHeight); + claim.pushKV(T_LASTTAKEOVERHEIGHT, res.nLastTakeoverHeight); } } return claim; @@ -550,16 +417,8 @@ UniValue getclaimbyid(const JSONRPCRequest& request) UniValue gettotalclaimednames(const JSONRPCRequest& request) { - if (request.fHelp || !validParams(request.params, 0, 0)) - throw std::runtime_error( - "gettotalclaimednames\n" - "Return the total number of names that have been\n" - "successfully claimed, and therefore exist in the trie\n" - "Arguments:\n" - "Result:\n" - "\"total names\" (numeric) the total number of\n" - " names in the trie\n" - ); + validateRequest(request, GETTOTALCLAIMEDNAMES, 0, 0); + LOCK(cs_main); auto num_names = pclaimTrie->getTotalNamesInTrie(); return int(num_names); @@ -567,15 +426,8 @@ UniValue gettotalclaimednames(const JSONRPCRequest& request) UniValue gettotalclaims(const JSONRPCRequest& request) { - if (request.fHelp || !validParams(request.params, 0, 0)) - throw std::runtime_error( - "gettotalclaims\n" - "Return the total number of active claims in the trie\n" - "Arguments:\n" - "Result:\n" - "\"total claims\" (numeric) the total number\n" - " of active claims\n" - ); + validateRequest(request, GETTOTALCLAIMS, 0, 0); + LOCK(cs_main); auto num_claims = pclaimTrie->getTotalClaimsInTrie(); return int(num_claims); @@ -583,17 +435,8 @@ UniValue gettotalclaims(const JSONRPCRequest& request) UniValue gettotalvalueofclaims(const JSONRPCRequest& request) { - if (request.fHelp || !validParams(request.params, 0, 1)) - throw std::runtime_error( - "gettotalvalueofclaims\n" - "Return the total value of the claims in the trie\n" - "Arguments:\n" - "1. \"controlling_only\" (boolean) only include the value\n" - " of controlling claims\n" - "Result:\n" - "\"total value\" (numeric) the total value of the\n" - " claims in the trie\n" - ); + validateRequest(request, GETTOTALVALUEOFCLAIMS, 0, 1); + LOCK(cs_main); bool controlling_only = false; if (request.params.size() == 1) @@ -604,31 +447,7 @@ UniValue gettotalvalueofclaims(const JSONRPCRequest& request) UniValue getclaimsfortx(const JSONRPCRequest& request) { - if (request.fHelp || !validParams(request.params, 1, 0)) - throw std::runtime_error( - "getclaimsfortx\n" - "Return any claims or supports found in a transaction\n" - "Arguments:\n" - "1. \"txid\" (string) the txid of the transaction to check for unspent claims\n" - "Result:\n" - "[\n" - " {\n" - " \"nOut\" (numeric) the index of the claim or support in the transaction's list of outputs\n" - " \"claim type\" (string) 'claim' or 'support'\n" - " \"name\" (string) the name claimed or supported\n" - " \"claimId\" (string) if a claim, its ID\n" - " \"value\" (string) if a name claim, the value of the claim\n" - " \"supported txid\" (string) if a support, the txid of the supported claim\n" - " \"supported nout\" (numeric) if a support, the index of the supported claim in its transaction\n" - " \"depth\" (numeric) the depth of the transaction in the main chain\n" - " \"in claim trie\" (boolean) if a name claim, whether the claim is active, i.e. has made it into the trie\n" - " \"is controlling\" (boolean) if a name claim, whether the claim is the current controlling claim for the name\n" - " \"in support map\" (boolean) if a support, whether the support is active, i.e. has made it into the support map\n" - " \"in queue\" (boolean) whether the claim is in a queue waiting to be inserted into the trie or support map\n" - " \"blocks to valid\" (numeric) if in a queue, the number of blocks until it's inserted into the trie or support map\n" - " }\n" - "]\n" - ); + validateRequest(request, GETCLAIMSFORTX, 1, 0); LOCK(cs_main); uint256 hash = ParseHashV(request.params[0], "txid (parameter 1)"); @@ -643,102 +462,66 @@ UniValue getclaimsfortx(const JSONRPCRequest& request) std::vector txouts{ coin.out }; int nHeight = coin.nHeight; - for (unsigned int i = 0; i < txouts.size(); ++i) - { - if (!txouts[i].IsNull()) - { - vvchParams.clear(); - const CTxOut& txout = txouts[i]; - UniValue o(UniValue::VOBJ); - if (DecodeClaimScript(txout.scriptPubKey, op, vvchParams)) - { - o.pushKV("nOut", static_cast(i)); - std::string sName(vvchParams[0].begin(), vvchParams[0].end()); - o.pushKV("name", escapeNonUtf8(sName)); - if (op == OP_CLAIM_NAME) - { - uint160 claimId = ClaimIdHash(hash, i); - o.pushKV("claimId", claimId.GetHex()); - o.pushKV("value", HexStr(vvchParams[1].begin(), vvchParams[1].end())); - } - else if (op == OP_UPDATE_CLAIM) - { - uint160 claimId(vvchParams[1]); - o.pushKV("claimId", claimId.GetHex()); - o.pushKV("value", HexStr(vvchParams[2].begin(), vvchParams[2].end())); - } - else if (op == OP_SUPPORT_CLAIM) - { - uint160 supportedClaimId(vvchParams[1]); - o.pushKV("supported claimId", supportedClaimId.GetHex()); - if (vvchParams.size() > 2) - o.pushKV("supported value", HexStr(vvchParams[2].begin(), vvchParams[2].end())); - } - if (nHeight > 0) - { - o.pushKV("depth", chainActive.Height() - nHeight); - if (op == OP_CLAIM_NAME || op == OP_UPDATE_CLAIM) - { - bool inClaimTrie = trieCache.haveClaim(sName, COutPoint(hash, i)); - o.pushKV("in claim trie", inClaimTrie); - if (inClaimTrie) - { - CClaimValue claim; - if (!trieCache.getInfoForName(sName, claim)) - { - LogPrintf("HaveClaim was true but getInfoForName returned false."); - } - o.pushKV("is controlling", (claim.outPoint.hash == hash && claim.outPoint.n == i)); - } - else - { - int nValidAtHeight; - if (trieCache.haveClaimInQueue(sName, COutPoint(hash, i), nValidAtHeight)) - { - o.pushKV("in queue", true); - o.pushKV("blocks to valid", nValidAtHeight - chainActive.Height()); - } - else - { - o.pushKV("in queue", false); - } - } - } - else if (op == OP_SUPPORT_CLAIM) - { - bool inSupportMap = trieCache.haveSupport(sName, COutPoint(hash, i)); - o.pushKV("in support map", inSupportMap); - if (!inSupportMap) - { - int nValidAtHeight; - if (trieCache.haveSupportInQueue(sName, COutPoint(hash, i), nValidAtHeight)) - { - o.pushKV("in queue", true); - o.pushKV("blocks to valid", nValidAtHeight - chainActive.Height()); - } - else - { - o.pushKV("in queue", false); - } - } - } - } - else - { - o.pushKV("depth", 0); - if (op == OP_CLAIM_NAME || op == OP_UPDATE_CLAIM) - { - o.pushKV("in claim trie", false); - } - else if (op == OP_SUPPORT_CLAIM) - { - o.pushKV("in support map", false); - } - o.pushKV("in queue", false); - } - ret.push_back(o); - } + for (unsigned int i = 0; i < txouts.size(); ++i) { + if (txouts[i].IsNull()) + continue; + vvchParams.clear(); + const CTxOut& txout = txouts[i]; + if (!DecodeClaimScript(txout.scriptPubKey, op, vvchParams)) + continue; + UniValue o(UniValue::VOBJ); + o.pushKV(T_N, static_cast(i)); + std::string sName(vvchParams[0].begin(), vvchParams[0].end()); + o.pushKV(T_NAME, escapeNonUtf8(sName)); + if (op == OP_CLAIM_NAME) { + uint160 claimId = ClaimIdHash(hash, i); + o.pushKV(T_CLAIMID, claimId.GetHex()); + o.pushKV(T_VALUE, HexStr(vvchParams[1].begin(), vvchParams[1].end())); + } else if (op == OP_UPDATE_CLAIM || op == OP_SUPPORT_CLAIM) { + uint160 claimId(vvchParams[1]); + o.pushKV(T_CLAIMID, claimId.GetHex()); + if (vvchParams.size() > 2) + o.pushKV(T_VALUE, HexStr(vvchParams[2].begin(), vvchParams[2].end())); } + if (nHeight > 0) { + o.pushKV(T_DEPTH, chainActive.Height() - nHeight); + if (op == OP_CLAIM_NAME || op == OP_UPDATE_CLAIM) { + bool inClaimTrie = trieCache.haveClaim(sName, COutPoint(hash, i)); + o.pushKV(T_INCLAIMTRIE, inClaimTrie); + if (inClaimTrie) { + CClaimValue claim; + if (!trieCache.getInfoForName(sName, claim)) + LogPrintf("HaveClaim was true but getInfoForName returned false."); + o.pushKV(T_ISCONTROLLING, (claim.outPoint.hash == hash && claim.outPoint.n == i)); + } else { + int nValidAtHeight; + if (trieCache.haveClaimInQueue(sName, COutPoint(hash, i), nValidAtHeight)) { + o.pushKV(T_INQUEUE, true); + o.pushKV(T_BLOCKSTOVALID, nValidAtHeight - chainActive.Height()); + } else + o.pushKV(T_INQUEUE, false); + } + } else if (op == OP_SUPPORT_CLAIM) { + bool inSupportMap = trieCache.haveSupport(sName, COutPoint(hash, i)); + o.pushKV(T_INSUPPORTMAP, inSupportMap); + if (!inSupportMap) { + int nValidAtHeight; + if (trieCache.haveSupportInQueue(sName, COutPoint(hash, i), nValidAtHeight)) { + o.pushKV(T_INQUEUE, true); + o.pushKV(T_BLOCKSTOVALID, nValidAtHeight - chainActive.Height()); + } else + o.pushKV(T_INQUEUE, false); + } + } + } else { + o.pushKV(T_DEPTH, 0); + if (op == OP_CLAIM_NAME || op == OP_UPDATE_CLAIM) + o.pushKV(T_INCLAIMTRIE, false); + else if (op == OP_SUPPORT_CLAIM) + o.pushKV(T_INSUPPORTMAP, false); + o.pushKV(T_INQUEUE, false); + } + ret.push_back(o); } return ret; } @@ -754,111 +537,46 @@ UniValue proofToJSON(const CClaimTrieProof& proof) for (const auto& itChildren : itNode.children) { UniValue child(UniValue::VOBJ); - child.pushKV("character", itChildren.first); + child.pushKV(T_CHARACTER, itChildren.first); if (!itChildren.second.IsNull()) - child.pushKV("nodeHash", itChildren.second.GetHex()); + child.pushKV(T_NODEHASH, itChildren.second.GetHex()); children.push_back(child); } - - node.pushKV("children", children); + node.pushKV(T_CHILDREN, children); if (itNode.hasValue && !itNode.valHash.IsNull()) - node.pushKV("valueHash", itNode.valHash.GetHex()); - + node.pushKV(T_VALUEHASH, itNode.valHash.GetHex()); nodes.push_back(node); } if (!nodes.empty()) - result.push_back(Pair("nodes", nodes)); + result.push_back(Pair(T_NODES, nodes)); UniValue pairs(UniValue::VARR); for (const auto& itPair : proof.pairs) { UniValue child(UniValue::VOBJ); - child.push_back(Pair("odd", itPair.first)); - child.push_back(Pair("hash", itPair.second.GetHex())); + child.push_back(Pair(T_ODD, itPair.first)); + child.push_back(Pair(T_HASH, itPair.second.GetHex())); pairs.push_back(child); } if (!pairs.empty()) - result.push_back(Pair("pairs", pairs)); + result.push_back(Pair(T_PAIRS, pairs)); if (proof.hasValue) { - result.pushKV("txhash", proof.outPoint.hash.GetHex()); - result.pushKV("nOut", (int)proof.outPoint.n); - result.pushKV("last takeover height", (int)proof.nHeightOfLastTakeover); + result.pushKV(T_TXID, proof.outPoint.hash.GetHex()); + result.pushKV(T_N, (int)proof.outPoint.n); + result.pushKV(T_LASTTAKEOVERHEIGHT, (int)proof.nHeightOfLastTakeover); } return result; } UniValue getnameproof(const JSONRPCRequest& request) { - if (request.fHelp || !validParams(request.params, 1, 2)) - throw std::runtime_error( - "getnameproof\n" - "Return the cryptographic proof that a name maps to a value\n" - "or doesn't.\n" - "Arguments:\n" - "1. \"name\" (string) the name to get a proof for\n" - "2. \"blockhash\" (string, optional) the hash of the block\n" - " which is the basis\n" - " of the proof. If\n" - " none is given, \n" - " the latest block\n" - " will be used.\n" - "3. \"claimId\" (string, optional, post-fork) for validating a specific claim\n" - "Result: \n" - "{\n" - " \"nodes\" : [ (array of object, pre-fork) full nodes (i.e.\n" - " those which lead to\n" - " the requested name)\n" - " \"children\" : [ (array of object) the children of\n" - " this node\n" - " \"child\" : { (object) a child node, either leaf or\n" - " reference to a full node\n" - " \"character\" : \"char\" (string) the character which\n" - " leads from the parent\n" - " to this child node\n" - " \"nodeHash\" : \"hash\" (string, if exists) the hash of\n" - " the node if\n" - " this is a \n" - " leaf node\n" - " }\n" - " ]\n" - " \"valueHash\" (string, if exists) the hash of this\n" - " node's value, if\n" - " it has one. If \n" - " this is the\n" - " requested name\n" - " this will not\n" - " exist whether\n" - " the node has a\n" - " value or not\n" - " ]\n" - " \"pairs\" : [ (array of pairs, post-fork) hash can be validated by \n" - " hashing claim from the bottom up\n" - " {\n" - " \"odd\" (boolean) this value goes on the right of hash\n" - " \"hash\" (boolean) the hash to be mixed in\n" - " }\n" - " ]\n" - " \"txhash\" : \"hash\" (string, if exists) the txid of the\n" - " claim which controls\n" - " this name, if there\n" - " is one.\n" - " \"nOut\" : n, (numeric) the nOut of the claim which\n" - " controls this name, if there\n" - " is one.\n" - " \"last takeover height\" (numeric) the most recent height at\n" - " which the value of a name\n" - " changed other than through\n" - " an update to the winning\n" - " bid\n" - " }\n" - "}\n"); + validateRequest(request, GETNAMEPROOF, 1, 2); LOCK(cs_main); - CCoinsViewCache coinsCache(pcoinsTip.get()); CClaimTrieCache trieCache(pclaimTrie); @@ -892,14 +610,7 @@ UniValue getnameproof(const JSONRPCRequest& request) UniValue checknormalization(const JSONRPCRequest& request) { - if (request.fHelp || !validParams(request.params, 1, 0)) - throw std::runtime_error( - "checknormalization\n" - "Given an unnormalized name of a claim, return normalized version of it\n" - "Arguments:\n" - "1. \"name\" (string) the name to normalize\n" - "Result: \n" - "\"normalized\" (string) fully normalized name\n"); + validateRequest(request, CHECKNORMALIZATION, 1, 0); const bool force = true; const std::string name = request.params[0].get_str(); @@ -911,18 +622,18 @@ UniValue checknormalization(const JSONRPCRequest& request) static const CRPCCommand commands[] = { // category name actor (function) argNames // --------------------- ------------------------ ----------------------- ---------- - { "Claimtrie", "getclaimsintrie", &getclaimsintrie, { "blockhash" } }, - { "Claimtrie", "getnamesintrie", &getnamesintrie, { "blockhash" } }, + { "Claimtrie", "getclaimsintrie", &getclaimsintrie, { T_BLOCKHASH } }, + { "Claimtrie", "getnamesintrie", &getnamesintrie, { T_BLOCKHASH } }, { "hidden", "getclaimtrie", &getclaimtrie, { } }, - { "Claimtrie", "getvalueforname", &getvalueforname, { "name","blockhash","claimId" } }, - { "Claimtrie", "getclaimsforname", &getclaimsforname, { "name","blockhash" } }, + { "Claimtrie", "getvalueforname", &getvalueforname, { T_NAME,T_BLOCKHASH,T_CLAIMID } }, + { "Claimtrie", "getclaimsforname", &getclaimsforname, { T_NAME,T_BLOCKHASH } }, { "Claimtrie", "gettotalclaimednames", &gettotalclaimednames, { "" } }, { "Claimtrie", "gettotalclaims", &gettotalclaims, { "" } }, - { "Claimtrie", "gettotalvalueofclaims", &gettotalvalueofclaims, { "controlling_only" } }, - { "Claimtrie", "getclaimsfortx", &getclaimsfortx, { "txid" } }, - { "Claimtrie", "getnameproof", &getnameproof, { "name","blockhash","claimId"} }, - { "Claimtrie", "getclaimbyid", &getclaimbyid, { "claimId" } }, - { "Claimtrie", "checknormalization", &checknormalization, { "name" }}, + { "Claimtrie", "gettotalvalueofclaims", &gettotalvalueofclaims, { T_CONTROLLINGONLY } }, + { "Claimtrie", "getclaimsfortx", &getclaimsfortx, { T_TXID } }, + { "Claimtrie", "getnameproof", &getnameproof, { T_NAME,T_BLOCKHASH,T_CLAIMID} }, + { "Claimtrie", "getclaimbyid", &getclaimbyid, { T_CLAIMID } }, + { "Claimtrie", "checknormalization", &checknormalization, { T_NAME }}, }; void RegisterClaimTrieRPCCommands(CRPCTable &tableRPC) diff --git a/src/test/claimtriebranching_tests.cpp b/src/test/claimtriebranching_tests.cpp index cb22e554e..454162d0a 100644 --- a/src/test/claimtriebranching_tests.cpp +++ b/src/test/claimtriebranching_tests.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -3971,15 +3972,15 @@ BOOST_AUTO_TEST_CASE(getvalueforname_test) req.params.push_back(UniValue(sName1)); UniValue results = getvalueforname(req); - BOOST_CHECK_EQUAL(results["value"].get_str(), HexStr(sValue1)); - BOOST_CHECK_EQUAL(results["amount"].get_int(), 2); - BOOST_CHECK_EQUAL(results["effectiveAmount"].get_int(), 5); + BOOST_CHECK_EQUAL(results[T_VALUE].get_str(), HexStr(sValue1)); + BOOST_CHECK_EQUAL(results[T_AMOUNT].get_int(), 2); + BOOST_CHECK_EQUAL(results[T_EFFECTIVEAMOUNT].get_int(), 5); req.params.push_back(blockHash.GetHex()); results = getvalueforname(req); - BOOST_CHECK_EQUAL(results["amount"].get_int(), 2); - BOOST_CHECK_EQUAL(results["effectiveAmount"].get_int(), 2); + BOOST_CHECK_EQUAL(results[T_AMOUNT].get_int(), 2); + BOOST_CHECK_EQUAL(results[T_EFFECTIVEAMOUNT].get_int(), 2); } BOOST_AUTO_TEST_CASE(getclaimsforname_test) @@ -4005,31 +4006,31 @@ BOOST_AUTO_TEST_CASE(getclaimsforname_test) req.params.push_back(UniValue(sName1)); UniValue results = getclaimsforname(req); - UniValue claims = results["claims"]; + UniValue claims = results[T_CLAIMS]; BOOST_CHECK_EQUAL(claims.size(), 1U); - BOOST_CHECK_EQUAL(results["lastTakeoverHeight"].get_int(), height + 1); - BOOST_CHECK_EQUAL(claims[0]["effectiveAmount"].get_int(), 2); - BOOST_CHECK_EQUAL(claims[0]["supports"].size(), 0U); + BOOST_CHECK_EQUAL(results[T_LASTTAKEOVERHEIGHT].get_int(), height + 1); + BOOST_CHECK_EQUAL(claims[0][T_EFFECTIVEAMOUNT].get_int(), 2); + BOOST_CHECK_EQUAL(claims[0][T_SUPPORTS].size(), 0U); fixture.IncrementBlocks(1); results = getclaimsforname(req); - claims = results["claims"]; + claims = results[T_CLAIMS]; BOOST_CHECK_EQUAL(claims.size(), 2U); - BOOST_CHECK_EQUAL(results["lastTakeoverHeight"].get_int(), height + 3); - BOOST_CHECK_EQUAL(claims[0]["effectiveAmount"].get_int(), 3); - BOOST_CHECK_EQUAL(claims[1]["effectiveAmount"].get_int(), 2); - BOOST_CHECK_EQUAL(claims[0]["supports"].size(), 0U); - BOOST_CHECK_EQUAL(claims[1]["supports"].size(), 0U); + BOOST_CHECK_EQUAL(results[T_LASTTAKEOVERHEIGHT].get_int(), height + 3); + BOOST_CHECK_EQUAL(claims[0][T_EFFECTIVEAMOUNT].get_int(), 3); + BOOST_CHECK_EQUAL(claims[1][T_EFFECTIVEAMOUNT].get_int(), 2); + BOOST_CHECK_EQUAL(claims[0][T_SUPPORTS].size(), 0U); + BOOST_CHECK_EQUAL(claims[1][T_SUPPORTS].size(), 0U); req.params.push_back(blockHash.GetHex()); results = getclaimsforname(req); - claims = results["claims"]; + claims = results[T_CLAIMS]; BOOST_CHECK_EQUAL(claims.size(), 1U); - BOOST_CHECK_EQUAL(results["lastTakeoverHeight"].get_int(), height + 1); - BOOST_CHECK_EQUAL(claims[0]["effectiveAmount"].get_int(), 2); - BOOST_CHECK_EQUAL(claims[0]["supports"].size(), 0U); + BOOST_CHECK_EQUAL(results[T_LASTTAKEOVERHEIGHT].get_int(), height + 1); + BOOST_CHECK_EQUAL(claims[0][T_EFFECTIVEAMOUNT].get_int(), 2); + BOOST_CHECK_EQUAL(claims[0][T_SUPPORTS].size(), 0U); } BOOST_AUTO_TEST_CASE(claim_rpcs_rollback2_test) @@ -4059,13 +4060,13 @@ BOOST_AUTO_TEST_CASE(claim_rpcs_rollback2_test) req.params.push_back(blockHash.GetHex()); UniValue claimsResults = getclaimsforname(req); - BOOST_CHECK_EQUAL(claimsResults["lastTakeoverHeight"].get_int(), height + 5); - BOOST_CHECK_EQUAL(claimsResults["claims"][0]["supports"].size(), 0U); - BOOST_CHECK_EQUAL(claimsResults["claims"][1]["supports"].size(), 0U); + BOOST_CHECK_EQUAL(claimsResults[T_LASTTAKEOVERHEIGHT].get_int(), height + 5); + BOOST_CHECK_EQUAL(claimsResults[T_CLAIMS][0][T_SUPPORTS].size(), 0U); + BOOST_CHECK_EQUAL(claimsResults[T_CLAIMS][1][T_SUPPORTS].size(), 0U); UniValue valueResults = getvalueforname(req); - BOOST_CHECK_EQUAL(valueResults["value"].get_str(), HexStr(sValue2)); - BOOST_CHECK_EQUAL(valueResults["amount"].get_int(), 2); + BOOST_CHECK_EQUAL(valueResults[T_VALUE].get_str(), HexStr(sValue2)); + BOOST_CHECK_EQUAL(valueResults[T_AMOUNT].get_int(), 2); } BOOST_AUTO_TEST_CASE(claim_rpcs_rollback3_test) @@ -4100,11 +4101,11 @@ BOOST_AUTO_TEST_CASE(claim_rpcs_rollback3_test) req.params.push_back(blockHash.GetHex()); UniValue claimsResults = getclaimsforname(req); - BOOST_CHECK_EQUAL(claimsResults["lastTakeoverHeight"].get_int(), height + 1); + BOOST_CHECK_EQUAL(claimsResults[T_LASTTAKEOVERHEIGHT].get_int(), height + 1); UniValue valueResults = getvalueforname(req); - BOOST_CHECK_EQUAL(valueResults["value"].get_str(), HexStr(sValue1)); - BOOST_CHECK_EQUAL(valueResults["amount"].get_int(), 3); + BOOST_CHECK_EQUAL(valueResults[T_VALUE].get_str(), HexStr(sValue1)); + BOOST_CHECK_EQUAL(valueResults[T_AMOUNT].get_int(), 3); } BOOST_AUTO_TEST_CASE(update_on_support2_test) -- 2.45.3 From 2f872ce720f25b5752a1002ceb4b08a1eb149080 Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Tue, 13 Aug 2019 16:38:34 +0300 Subject: [PATCH 4/8] Add bid, sequence like rpc methods Reuse a bunch of rpc help texts --- src/rpc/claimrpchelp.h | 453 +++++++++++++++++++++++------------------ src/rpc/claimtrie.cpp | 364 +++++++++++++++++++++++++++++---- src/validation.cpp | 60 +++--- 3 files changed, 604 insertions(+), 273 deletions(-) diff --git a/src/rpc/claimrpchelp.h b/src/rpc/claimrpchelp.h index 28202f8bb..69abc2cac 100644 --- a/src/rpc/claimrpchelp.h +++ b/src/rpc/claimrpchelp.h @@ -1,5 +1,6 @@ -#include +#ifndef CLAIMRPCHELP_H +#define CLAIMRPCHELP_H // always keep defines T_ + value in upper case #define T_NORMALIZEDNAME "normalizedName" @@ -37,6 +38,13 @@ #define T_PAIRS "pairs" #define T_ODD "odd" #define T_HASH "hash" +#define T_BID "bid" +#define T_SEQUENCE "sequence" +#define T_CLAIMSADDEDORUPDATED "claimsAddedOrUpdated" +#define T_SUPPORTSADDEDORUPDATED "supportsAddedOrUpdated" +#define T_CLAIMSREMOVED "claimsRemoved" +#define T_SUPPORTSREMOVED "supportsRemoved" +#define T_ADDRESS "address" enum { GETCLAIMSINTRIE = 0, @@ -50,241 +58,284 @@ enum { GETCLAIMSFORTX, GETNAMEPROOF, CHECKNORMALIZATION, + GETCLAIMBYBID, + GETCLAIMBYSEQ, + GETCLAIMPROOFBYID, + GETCLAIMPROOFBYSEQ, + GETCHANGESINBLOCK, }; +#define S3_(pre, name, def) pre "\"" name "\"" def "\n" +#define S3(pre, name, def) S3_(pre, name, def) +#define S1(str) str "\n" + +#define NAME_TEXT " (string) the name to look up" +#define BLOCKHASH_TEXT " (string, optional) get claims in the trie\n" \ +" at the block specified\n" \ +" by this block hash.\n" \ +" If none is given,\n" \ +" the latest active\n" \ +" block will be used." + +#define CLAIM_OUTPUT \ +S3(" ", T_NORMALIZEDNAME, " (string) the name of the claim (after normalization)") \ +S3(" ", T_NAME, " (string) the original name of this claim (before normalization)") \ +S3(" ", T_VALUE, " (string) the value of this claim") \ +S3(" ", T_ADDRESS, " (string) the destination address of this claim") \ +S3(" ", T_CLAIMID, " (string) the claimId of the claim") \ +S3(" ", T_TXID, " (string) the txid of the claim") \ +S3(" ", T_N, " (numeric) the index of the claim in the transaction's list of outputs") \ +S3(" ", T_HEIGHT, " (numeric) the height of the block in which this transaction is located") \ +S3(" ", T_VALIDATHEIGHT, " (numeric) the height at which the support became/becomes valid") \ +S3(" ", T_AMOUNT, " (numeric) the amount of the claim") \ +S3(" ", T_EFFECTIVEAMOUNT, " (numeric) the amount plus amount from all supports associated with the claim") \ +S3(" ", T_SUPPORTS, ": [ (array of object) supports for this claim") \ +S3(" ", T_VALUE, " (string) the metadata of the support if any") \ +S3(" ", T_ADDRESS, " (string) the destination address of the support") \ +S3(" ", T_TXID, " (string) the txid of the support") \ +S3(" ", T_N, " (numeric) the index of the support in the transaction's list of outputs") \ +S3(" ", T_HEIGHT, " (numeric) the height of the block in which this transaction is located") \ +S3(" ", T_VALIDATHEIGHT, " (numeric) the height at which the support became/becomes valid") \ +S3(" ", T_AMOUNT, " (numeric) the amount of the support") \ +S1(" ]") \ +S3(" ", T_LASTTAKEOVERHEIGHT, " (numeric) the last height at which ownership of the name changed") \ +S3(" ", T_BID, " (numeric) lower value means a higher bid rate, ordered by effective amount") \ +S3(" ", T_SEQUENCE, " (numeric) lower value means latest in sequence, ordered by height of insertion") + +#define PROOF_OUTPUT \ +S3(" ", T_NODES, ": [ (array of object, pre-fork) full nodes\n" \ +" (i.e. those which lead to the requested name)") \ +S3(" ", T_CHILDREN, ": [ (array of object) the children of the node") \ +S3(" ", T_CHARACTER, " (string) the character which leads from the parent to this child node") \ +S3(" ", T_NODEHASH, " (string, if exists) the hash of the node if this is a leaf node") \ +S1(" ]") \ +S3(" ", T_VALUEHASH, " (string, if exists) the hash of this node's value, if" \ +" it has one. If this is the requested name this\n" \ +" will not exist whether the node has a value or not") \ +S1(" ]") \ +S3(" ", T_PAIRS, ": [ (array of pairs, post-fork) hash can be validated by" \ +" hashing claim from the bottom up") \ +S3(" ", T_ODD, " (boolean) this value goes on the right of hash") \ +S3(" ", T_HASH, " (string) the hash to be mixed in") \ +S1(" ]") \ +S3(" ", T_TXID, " (string, if exists) the txid of the claim which controls" \ +" this name, if there is one.") \ +S3(" ", T_N, " (numeric) the index of the claim in the transaction's list of outputs") \ +S3(" ", T_LASTTAKEOVERHEIGHT, " (numeric) the last height at which ownership of the name changed") + static const char* const rpc_help[] = { // GETCLAIMSINTRIE -R"(getclaimsintrie +S1(R"(getclaimsintrie Return all claims in the name trie. Deprecated -Arguments: -1. ")" T_BLOCKHASH R"(" (string, optional) get claims in the trie - at the block specified - by this block hash. - If none is given, - the latest active - block will be used. -Result: [ - ")" T_NORMALIZEDNAME R"(" (string) the name of the claim(s) (after normalization) - ")" T_CLAIMS R"(": [ (array of object) the claims for this name - ")" T_NAME R"(" (string) the original name of this claim (before normalization) - ")" T_VALUE R"(" (string) the value of this claim - ")" T_CLAIMID R"(" (string) the claimId of the claim - ")" T_TXID R"(" (string) the txid of the claim - ")" T_N R"(" (numeric) the index of the claim in the transaction's list of outputs - ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located - ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the claim became/becomes valid - ")" T_AMOUNT R"(" (numeric) the amount of the claim - ] -])", +Arguments:)") +S3("1. ", T_BLOCKHASH, BLOCKHASH_TEXT) +S1("Result: [") +S3(" ", T_NORMALIZEDNAME, " (string) the name of the claim(s) (after normalization)") +S3(" ", T_CLAIMS, ": [ (array of object) the claims for this name") +S3(" ", T_NAME, " (string) the original name of this claim (before normalization)") +S3(" ", T_VALUE, " (string) the value of this claim") +S3(" ", T_ADDRESS, " (string) the destination address of this claim") +S3(" ", T_CLAIMID, " (string) the claimId of the claim") +S3(" ", T_TXID, " (string) the txid of the claim") +S3(" ", T_N, " (numeric) the index of the claim in the transaction's list of outputs") +S3(" ", T_HEIGHT, " (numeric) the height of the block in which this transaction is located") +S3(" ", T_VALIDATHEIGHT, " (numeric) the height at which the claim became/becomes valid") +S3(" ", T_AMOUNT, " (numeric) the amount of the claim") +S1(" ]") +"]", // GETNAMESINTRIE -R"(getnamesintrie +S1(R"(getnamesintrie Return all claim names in the trie. -Arguments: -1. ")" T_BLOCKHASH R"(" (string, optional) get claims in the trie - at the block specified - by this block hash. - If none is given, - the latest active - block will be used. -Result: [ - ")" T_NAMES R"(" all names in the trie that have claims -])", +Arguments:)") +S3("1. ", T_BLOCKHASH, BLOCKHASH_TEXT) +S1("Result: [") +S3(" ", T_NAMES, " all names in the trie that have claims") +"]", // GETVALUEFORNAME -R"(getvalueforname +S1(R"(getvalueforname Return the winning or specified by claimId value associated with a name -Arguments: -1. ")" T_NAME R"(" (string) the name to look up -2. ")" T_BLOCKHASH R"(" (string, optional) get the value - associated with the name - at the block specified - by this block hash. - If none is given, - the latest active - block will be used. -3. ")" T_CLAIMID R"(" (string, optional) can be partial one -Result: [ - ")" T_NORMALIZEDNAME R"(" (string) the name of the claim(s) (after normalization) - ")" T_NAME R"(" (string) the original name of this claim (before normalization) - ")" T_VALUE R"(" (string) the value of this claim - ")" T_CLAIMID R"(" (string) the claimId of the claim - ")" T_TXID R"(" (string) the txid of the claim - ")" T_N R"(" (numeric) the index of the claim in the transaction's list of outputs - ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located - ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the support became/becomes valid - ")" T_AMOUNT R"(" (numeric) the amount of the claim - ")" T_EFFECTIVEAMOUNT R"(" (numeric) the amount plus amount from all supports associated with the claim - ")" T_SUPPORTS R"(": [ (array of object) supports for this claim - ")" T_VALUE R"(" (string) the metadata of the support if any - ")" T_TXID R"(" (string) the txid of the support - ")" T_N R"(" (numeric) the index of the support in the transaction's list of outputs - ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located - ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the support became/becomes valid - ")" T_AMOUNT R"(" (numeric) the amount of the support - ] - ")" T_LASTTAKEOVERHEIGHT R"(" (numeric) the last height at which ownership of the name changed -])", +Arguments:)") +S3("1. ", T_NAME, NAME_TEXT) +S3("2. ", T_BLOCKHASH, BLOCKHASH_TEXT) +S3("3. ", T_CLAIMID, " (string, optional) can be partial one") +S1("Result: [") +CLAIM_OUTPUT +"]", // GETCLAIMSFORNAME -R"(getclaimsforname +S1(R"(getclaimsforname Return all claims and supports for a name -Arguments: -1. ")" T_NAME R"(" (string) the name to look up -2. ")" T_BLOCKHASH R"(" (string, optional) get claims in the trie - at the block specified - by this block hash. - If none is given, - the latest active - block will be used. -Result: [ - ")" T_NORMALIZEDNAME R"(" (string) the name of the claim(s) (after normalization) - ")" T_CLAIMS R"(": [ (array of object) the claims for this name - ")" T_NAME R"(" (string) the original name of this claim (before normalization) - ")" T_VALUE R"(" (string) the value of this claim - ")" T_CLAIMID R"(" (string) the claimId of the claim - ")" T_TXID R"(" (string) the txid of the claim - ")" T_N R"(" (numeric) the index of the claim in the transaction's list of outputs - ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located - ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the claim became/becomes valid - ")" T_AMOUNT R"(" (numeric) the amount of the claim - ")" T_EFFECTIVEAMOUNT R"(" (numeric) the amount plus amount from all supports associated with the claim - ")" T_SUPPORTS R"(": [ (array of object) supports for this claim - ")" T_VALUE R"(" (string) the metadata of the support if any - ")" T_TXID R"(" (string) the txid of the support - ")" T_N R"(" (numeric) the index of the support in the transaction's list of outputs - ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located - ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the support became/becomes valid - ")" T_AMOUNT R"(" (numeric) the amount of the support - ] - ] - ")" T_LASTTAKEOVERHEIGHT R"(" (numeric) the last height at which ownership of the name changed - ")" T_SUPPORTSWITHOUTCLAIM R"(": [ - ")" T_TXID R"(" (string) the txid of the support - ")" T_N R"(" (numeric) the index of the support in the transaction's list of outputs - ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located - ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the support became/becomes valid - ")" T_AMOUNT R"(" (numeric) the amount of the support - ] -])", +Arguments:)") +S3("1. ", T_NAME, NAME_TEXT) +S3("2. ", T_BLOCKHASH, BLOCKHASH_TEXT) +S1("Result: [") +S3(" ", T_NORMALIZEDNAME, " (string) the name of the claim(s) (after normalization)") +S3(" ", T_CLAIMS, ": [ (array of object) the claims for this name") +S3(" ", T_NAME, " (string) the original name of this claim (before normalization)") +S3(" ", T_VALUE, " (string) the value of this claim") +S3(" ", T_ADDRESS, " (string) the destination address of this claim") +S3(" ", T_CLAIMID, " (string) the claimId of the claim") +S3(" ", T_TXID, " (string) the txid of the claim") +S3(" ", T_N, " (numeric) the index of the claim in the transaction's list of outputs") +S3(" ", T_HEIGHT, " (numeric) the height of the block in which this transaction is located") +S3(" ", T_VALIDATHEIGHT, " (numeric) the height at which the claim became/becomes valid") +S3(" ", T_AMOUNT, " (numeric) the amount of the claim") +S3(" ", T_EFFECTIVEAMOUNT, " (numeric) the amount plus amount from all supports associated with the claim") +S3(" ", T_SUPPORTS, ": [ (array of object) supports for this claim") +S3(" ", T_VALUE, " (string) the metadata of the support if any") +S3(" ", T_ADDRESS, " (string) the destination address of the support") +S3(" ", T_TXID, " (string) the txid of the support") +S3(" ", T_N, " (numeric) the index of the support in the transaction's list of outputs") +S3(" ", T_HEIGHT, " (numeric) the height of the block in which this transaction is located") +S3(" ", T_VALIDATHEIGHT, " (numeric) the height at which the support became/becomes valid") +S3(" ", T_AMOUNT, " (numeric) the amount of the support") +S1(" ]") +S3(" ", T_BID, " (numeric) lower value means a higher bid rate, ordered by effective amount") +S3(" ", T_SEQUENCE, " (numeric) lower value means latest in sequence, ordered by height of insertion") +S1(" ]") +S3(" ", T_LASTTAKEOVERHEIGHT, " (numeric) the last height at which ownership of the name changed") +S3(" ", T_SUPPORTSWITHOUTCLAIM, ": [") +S3(" ", T_TXID, " (string) the txid of the support") +S3(" ", T_N, " (numeric) the index of the support in the transaction's list of outputs") +S3(" ", T_HEIGHT, " (numeric) the height of the block in which this transaction is located") +S3(" ", T_VALIDATHEIGHT, " (numeric) the height at which the support became/becomes valid") +S3(" ", T_AMOUNT, " (numeric) the amount of the support") +S1(" ]") +"]", // GETCLAIMBYID -R"(getclaimbyid +S1(R"(getclaimbyid Get a claim by claim id -Arguments: -1. ")" T_CLAIMID R"(" (string) the claimId of this claim or patial id (at least 3 chars) -Result: [ - ")" T_NORMALIZEDNAME R"(" (string) the name of the claim(s) (after normalization) - ")" T_NAME R"(" (string) the original name of this claim (before normalization) - ")" T_VALUE R"(" (string) the value of this claim - ")" T_CLAIMID R"(" (string) the claimId of the claim - ")" T_TXID R"(" (string) the txid of the claim - ")" T_N R"(" (numeric) the index of the claim in the transaction's list of outputs - ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located - ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the support became/becomes valid - ")" T_AMOUNT R"(" (numeric) the amount of the claim - ")" T_EFFECTIVEAMOUNT R"(" (numeric) the amount plus amount from all supports associated with the claim - ")" T_SUPPORTS R"(": [ (array of object) supports for this claim - ")" T_VALUE R"(" (string) the metadata of the support if any - ")" T_TXID R"(" (string) the txid of the support - ")" T_N R"(" (numeric) the index of the support in the transaction's list of outputs - ")" T_HEIGHT R"(" (numeric) the height of the block in which this transaction is located - ")" T_VALIDATHEIGHT R"(" (numeric) the height at which the support became/becomes valid - ")" T_AMOUNT R"(" (numeric) the amount of the support - ] - ")" T_LASTTAKEOVERHEIGHT R"(" (numeric) the last height at which ownership of the name changed -])", +Arguments:)") +S3("1. ", T_CLAIMID, " (string) the claimId of this claim or patial id (at least 3 chars)") +S1("Result: [") +CLAIM_OUTPUT +"]", // GETTOTALCLAIMEDNAMES - -R"(gettotalclaimednames +S1(R"(gettotalclaimednames Return the total number of names that have been -Arguments: -Result: - ")" T_TOTALNAMES R"(" (numeric) the total number of names in the trie -)", +Arguments:)") +S1("Result:") +S3(" ", T_TOTALNAMES, " (numeric) the total number of names in the trie") +, // GETTOTALCLAIMS -R"(gettotalclaims +S1(R"(gettotalclaims Return the total number of active claims in the trie -Arguments: -Result: - ")" T_TOTALCLAIMS R"(" (numeric) the total number of active claims -)", +Arguments:)") +S1("Result:") +S3(" ", T_TOTALCLAIMS, " (numeric) the total number of active claims") +, // GETTOTALVALUEOFCLAIMS -R"(gettotalvalueofclaims +S1(R"(gettotalvalueofclaims Return the total value of the claims in the trie -Arguments: -1. ")" T_CONTROLLINGONLY R"(" (boolean) only include the value of controlling claims -Result: - ")" T_TOTALVALUE R"(" (numeric) the total value of the claims in the trie -)", +Arguments:)") +S3("1. ", T_CONTROLLINGONLY, " (boolean) only include the value of controlling claims") +S1("Result:") +S3(" ", T_TOTALVALUE, " (numeric) the total value of the claims in the trie") +, // GETCLAIMSFORTX -R"(getclaimsfortx +S1(R"(getclaimsfortx Return any claims or supports found in a transaction -Arguments: -1. ")" T_TXID R"(" (string) the txid of the transaction to check for unspent claims -Result: [ - ")" T_N R"(" (numeric) the index of the claim in the transaction's list of outputs - ")" T_CLAIMTYPE R"(" (string) claim or support - ")" T_NAME R"(" (string) the name claimed or supported - ")" T_CLAIMID R"(" (string) if a claim, its ID - ")" T_VALUE R"(" (string) if a claim, its value - ")" T_DEPTH R"(" (numeric) the depth of the transaction in the main chain - ")" T_INCLAIMTRIE R"(" (boolean) if a name claim, whether the claim is active, i.e. has made it into the trie - ")" T_ISCONTROLLING R"(" (boolean) if a name claim, whether the claim is the current controlling claim for the name - ")" T_INSUPPORTMAP R"(" (boolean) if a support, whether the support is active, i.e. has made it into the support map - ")" T_INQUEUE R"(" (boolean) whether the claim is in a queue waiting to be inserted into the trie or support map - ")" T_BLOCKSTOVALID R"(" (numeric) if in a queue, the number of blocks until it's inserted into the trie or support map -])", +Arguments:)") +S3("1. ", T_TXID, " (string) the txid of the transaction to check for unspent claims") +S1("Result: [") +S3(" ", T_N, " (numeric) the index of the claim in the transaction's list of outputs") +S3(" ", T_CLAIMTYPE, " (string) claim or support") +S3(" ", T_NAME, " (string) the name claimed or supported") +S3(" ", T_CLAIMID, " (string) if a claim, its ID") +S3(" ", T_VALUE, " (string) if a claim, its value") +S3(" ", T_DEPTH, " (numeric) the depth of the transaction in the main chain") +S3(" ", T_INCLAIMTRIE, " (boolean) if a name claim, whether the claim is active, i.e. has made it into the trie") +S3(" ", T_ISCONTROLLING, " (boolean) if a name claim, whether the claim is the current controlling claim for the name") +S3(" ", T_INSUPPORTMAP, " (boolean) if a support, whether the support is active, i.e. has made it into the support map") +S3(" ", T_INQUEUE, " (boolean) whether the claim is in a queue waiting to be inserted into the trie or support map") +S3(" ", T_BLOCKSTOVALID, " (numeric) if in a queue, the number of blocks until it's inserted into the trie or support map") +"]", // GETNAMEPROOF -R"(getnameproof +S1(R"(getnameproof Return the cryptographic proof that a name maps to a value or doesn't. -Arguments: -1. ")" T_NAME R"(" (string) the name to look up -2. ")" T_BLOCKHASH R"(" (string, optional) get the value - associated with the name - at the block specified - by this block hash. - If none is given, - the latest active - block will be used. -3. ")" T_CLAIMID R"(" (string, optional, post-fork) for validating a specific claim - can be partial one -Result: [ - ")" T_NODES R"(": [ (array of object, pre-fork) full nodes - (i.e. those which lead to the requested name) - ")" T_CHILDREN R"(": [ (array of object) the children of the node - ")" T_CHARACTER R"(" (string) the character which leads from the parent - to this child node - ")" T_NODEHASH R"(" (string, if exists) the hash of the node if this is a leaf node - ] - ")" T_VALUEHASH R"(" (string, if exists) the hash of this node's value, if - it has one. If this is the requested name this - will not exist whether the node has a value or not - ] - ")" T_PAIRS R"(": [ (array of pairs, post-fork) hash can be validated by - hashing claim from the bottom up - ")" T_ODD R"(" (boolean) this value goes on the right of hash - ")" T_HASH R"(" (string) the hash to be mixed in - ] - ")" T_TXID R"(" (string, if exists) the txid of the claim which controls - this name, if there is one. - ")" T_N R"(" (numeric) the index of the claim in the transaction's list of outputs - ")" T_LASTTAKEOVERHEIGHT R"(" (numeric) the last height at which ownership of the name changed -])", +Arguments:)") +S3("1. ", T_NAME, NAME_TEXT) +S3("2. ", T_BLOCKHASH, BLOCKHASH_TEXT) +S3("3. ", T_CLAIMID, R"( (string, optional, post-fork) for validating a specific claim + can be partial one)") +S1("Result: [") +PROOF_OUTPUT +"]", // CHECKNORMALIZATION -R"(checknormalization +S1(R"(checknormalization Given an unnormalized name of a claim, return normalized version of it -Arguments: -1. ")" T_NAME R"(" (string) the name to normalize -Result: -")" T_NORMALIZEDNAME R"(" (string) normalized name -)", +Arguments:)") +S3("1. ", T_NAME, " (string) the name to normalize") +S1("Result:") +S3(" ", T_NORMALIZEDNAME, " (string) normalized name") +, + +// GETCLAIMBYBID +S1(R"(getclaimbybid +Get a claim by bid +Arguments:)") +S3("1. ", T_NAME, NAME_TEXT) +S3("2. ", T_BID, " (numeric) bid number") +S3("3. ", T_BLOCKHASH, BLOCKHASH_TEXT) +S1("Result: [") +CLAIM_OUTPUT +"]", + +// GETCLAIMBYSEQ +S1(R"(getclaimbyseq +Get a claim by sequence +Arguments:)") +S3("1. ", T_NAME, NAME_TEXT) +S3("2. ", T_SEQUENCE, " (numeric) sequence number") +S3("3. ", T_BLOCKHASH, BLOCKHASH_TEXT) +S1("Result: [") +CLAIM_OUTPUT +"]", + +// GETCLAIMPROOFBYID +S1(R"(getclaimproofbyid +Return the cryptographic proof that a name maps to a value or doesn't by a bid. +Arguments:)") +S3("1. ", T_NAME, NAME_TEXT) +S3("2. ", T_BID, " (numeric) bid number") +S3("3. ", T_BLOCKHASH, BLOCKHASH_TEXT) +S1("Result: [") +PROOF_OUTPUT +"]", + +// GETCLAIMPROOFBYSEQ +S1(R"(getclaimproofbyseq +Return the cryptographic proof that a name maps to a value or doesn't by a sequence. +Arguments:)") +S3("1. ", T_NAME, NAME_TEXT) +S3("2. ", T_SEQUENCE, " (numeric) sequence number") +S3("3. ", T_BLOCKHASH, BLOCKHASH_TEXT) +S1("Result: [") +PROOF_OUTPUT +"]", + +// GETCHANGESINBLOCK +S1(R"(getchangesinblock +Return the list of claims added, updated, and removed in a block or doesn't." +Arguments:)") +S3("1. ", T_BLOCKHASH, BLOCKHASH_TEXT) +S1("Result: [") +S3(" ", T_CLAIMSADDEDORUPDATED, " (array of string) claimIDs added or updated in the trie") +S3(" ", T_CLAIMSREMOVED, " (array of string) claimIDs that were removed from the trie") +S3(" ", T_SUPPORTSADDEDORUPDATED, " (array of string) IDs of supports added or updated") +S3(" ", T_SUPPORTSREMOVED, " (array of string) IDs that were removed from the trie") +"]", }; + +#endif // CLAIMRPCHELP_H diff --git a/src/rpc/claimtrie.cpp b/src/rpc/claimtrie.cpp index d9ca050cd..70c49fa4b 100644 --- a/src/rpc/claimtrie.cpp +++ b/src/rpc/claimtrie.cpp @@ -1,13 +1,16 @@ #include #include #include +#include #include #include #include #include +#include