From 456ac2f56288b6cb68c4fa41e62bcfd999e58dc2 Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Wed, 18 Dec 2019 20:29:18 +0200 Subject: [PATCH] Fix partial claim id search Signed-off-by: Anthony Fieroni --- src/claimtrie/trie.cpp | 11 +++-------- src/rpc/claimrpchelp.h | 2 +- src/rpc/claimtrie.cpp | 16 +++++++++------- src/test/claimtrierpc_tests.cpp | 8 ++++---- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/claimtrie/trie.cpp b/src/claimtrie/trie.cpp index 49d865741..5b574f36b 100644 --- a/src/claimtrie/trie.cpp +++ b/src/claimtrie/trie.cpp @@ -868,15 +868,10 @@ bool CClaimTrieCacheBase::getProofForName(const std::string& name, const uint160 bool CClaimTrieCacheBase::findNameForClaim(std::vector claim, CClaimValue& value, std::string& name) const { - if (claim.size() > 20U) // expecting RIPEMD160 -- 20 chars - return false; - // because the data coming in here is reversed we support removing chars from the left of the claimID - auto start = std::string(claim.rbegin(), claim.rend()); - auto end = start + std::string(21U - claim.size(), std::numeric_limits::max()); + std::reverse(claim.begin(), claim.end()); auto query = db << "SELECT nodeName, claimID, txID, txN, amount, activationHeight, blockHeight " - "FROM claim WHERE claimID BETWEEN ?1 AND ?2 AND activationHeight < ?3 AND expirationHeight >= ?3 " - "LIMIT 2" - << start << end << nNextHeight; + "FROM claim WHERE SUBSTR(claimID, ?1) = ?2 AND activationHeight < ?3 AND expirationHeight >= ?3" + << -int(claim.size()) << claim << nNextHeight; auto hit = false; for (auto&& row: query) { if (hit) return false; diff --git a/src/rpc/claimrpchelp.h b/src/rpc/claimrpchelp.h index f251c258b..e4e3ff108 100644 --- a/src/rpc/claimrpchelp.h +++ b/src/rpc/claimrpchelp.h @@ -192,7 +192,7 @@ S1(" ]") S1("getclaimbyid \"" T_CLAIMID R"(" Get a claim by claim id Arguments:)") -S3("1. ", T_CLAIMID, " (string) the claimId of this claim or patial id (at least 3 chars)") +S3("1. ", T_CLAIMID, " (string) the claimId of this claim or patial id (at least 6 chars)") S1("Result: [") CLAIM_OUTPUT "]", diff --git a/src/rpc/claimtrie.cpp b/src/rpc/claimtrie.cpp index dfd12daee..d9464e731 100644 --- a/src/rpc/claimtrie.cpp +++ b/src/rpc/claimtrie.cpp @@ -454,7 +454,7 @@ UniValue getclaimbyid(const JSONRPCRequest& request) ParseClaimtrieId(request.params[0], claimId, T_CLAIMID " (parameter 1)"); if (claimId.length() < 6) - throw JSONRPCError(RPC_INVALID_PARAMETER, T_CLAIMID " (parameter 1) should be at least 3 chars"); + throw JSONRPCError(RPC_INVALID_PARAMETER, T_CLAIMID " (parameter 1) should be at least 6 chars"); std::string foundName; CClaimValue foundClaim; @@ -660,12 +660,14 @@ UniValue getnameproof(const JSONRPCRequest& request) if (request.params.size() > 2) { std::string claimId; ParseClaimtrieId(request.params[2], claimId, T_CLAIMID " (optional parameter 3)"); + if (claimId.length() < 2) + throw JSONRPCError(RPC_INVALID_PARAMETER, T_CLAIMID " (optional parameter 3) should be at least 2 chars"); std::string foundClaimName; if (!trieCache.findNameForClaim(ParseHex(claimId), claim, foundClaimName) - || foundClaimName != trieCache.adjustNameForValidHeight(name, validHeight)) - throw JSONRPCError(RPC_INVALID_PARAMETER, "Failed to locate a claim with ID " + claimId); - } - else trieCache.getInfoForName(name, claim); + || foundClaimName != trieCache.adjustNameForValidHeight(name, validHeight)) + return {UniValue::VOBJ}; + } else if (!trieCache.getInfoForName(name, claim)) + return {UniValue::VOBJ}; CClaimTrieProof proof; if (!trieCache.getProofForName(name, claim.claimId, proof)) @@ -698,7 +700,7 @@ UniValue getclaimproofbybid(const JSONRPCRequest& request) auto csToName = trieCache.getClaimsForName(name); if (std::size_t(bid) >= csToName.claimsNsupports.size()) - return {UniValue::VARR}; + return {UniValue::VOBJ}; auto match = csToName.claimsNsupports[bid].claim.claimId; CClaimTrieProof proof; @@ -731,7 +733,7 @@ UniValue getclaimproofbyseq(const JSONRPCRequest& request) std::string name = request.params[0].get_str(); auto csToName = trieCache.getClaimsForName(name); if (std::size_t(seq) >= csToName.claimsNsupports.size()) - return {UniValue::VARR}; + return {UniValue::VOBJ}; auto sorted = seqSort(csToName.claimsNsupports); auto match = sorted[seq].claim.claimId; diff --git a/src/test/claimtrierpc_tests.cpp b/src/test/claimtrierpc_tests.cpp index e6a039883..4487dffc1 100644 --- a/src/test/claimtrierpc_tests.cpp +++ b/src/test/claimtrierpc_tests.cpp @@ -304,9 +304,9 @@ BOOST_AUTO_TEST_CASE(hash_bid_seq_claim_changes_test) BOOST_CHECK_EQUAL(result[T_SEQUENCE].get_int(), claim1seq); BOOST_CHECK_EQUAL(result[T_CLAIMID].get_str(), claimId1.GetHex()); - // check by partial id (at least 3 chars) + // check by partial id (at least 6 chars) req.params = UniValue(UniValue::VARR); - req.params.push_back(UniValue(claimId3.GetHex().substr(34))); + req.params.push_back(UniValue(claimId3.GetHex().substr(0, 6))); result = getclaimbyid(req); BOOST_CHECK_EQUAL(result[T_LASTTAKEOVERHEIGHT].get_int(), height); @@ -327,11 +327,11 @@ BOOST_AUTO_TEST_CASE(hash_bid_seq_claim_changes_test) auto claimHash = getValueHash(COutPoint(tx2.GetHash(), 1), result[T_LASTTAKEOVERHEIGHT].get_int()); ValidatePairs(fixture, jsonToPairs(result[T_PAIRS]), claimHash); - // check by partial id (can be even 1 char) + // check by partial id (can be even 2 chars) req.params = UniValue(UniValue::VARR); req.params.push_back(UniValue(name)); req.params.push_back(UniValue(blockhash.GetHex())); - req.params.push_back(UniValue(claimId2.GetHex().substr(14))); + req.params.push_back(UniValue(claimId2.GetHex().substr(0, 2))); result = getnameproof(req); claimHash = getValueHash(COutPoint(tx2.GetHash(), 0), result[T_LASTTAKEOVERHEIGHT].get_int());