Fix partial claim id search

Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
This commit is contained in:
Anthony Fieroni 2019-12-18 20:29:18 +02:00 committed by Brannon King
parent 54a7df886a
commit 456ac2f562
4 changed files with 17 additions and 20 deletions

View file

@ -868,15 +868,10 @@ bool CClaimTrieCacheBase::getProofForName(const std::string& name, const uint160
bool CClaimTrieCacheBase::findNameForClaim(std::vector<unsigned char> claim, CClaimValue& value, std::string& name) const bool CClaimTrieCacheBase::findNameForClaim(std::vector<unsigned char> claim, CClaimValue& value, std::string& name) const
{ {
if (claim.size() > 20U) // expecting RIPEMD160 -- 20 chars std::reverse(claim.begin(), claim.end());
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<char>::max());
auto query = db << "SELECT nodeName, claimID, txID, txN, amount, activationHeight, blockHeight " 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 " "FROM claim WHERE SUBSTR(claimID, ?1) = ?2 AND activationHeight < ?3 AND expirationHeight >= ?3"
"LIMIT 2" << -int(claim.size()) << claim << nNextHeight;
<< start << end << nNextHeight;
auto hit = false; auto hit = false;
for (auto&& row: query) { for (auto&& row: query) {
if (hit) return false; if (hit) return false;

View file

@ -192,7 +192,7 @@ S1(" ]")
S1("getclaimbyid \"" T_CLAIMID R"(" S1("getclaimbyid \"" T_CLAIMID R"("
Get a claim by claim id Get a claim by claim id
Arguments:)") 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: [") S1("Result: [")
CLAIM_OUTPUT CLAIM_OUTPUT
"]", "]",

View file

@ -454,7 +454,7 @@ UniValue getclaimbyid(const JSONRPCRequest& request)
ParseClaimtrieId(request.params[0], claimId, T_CLAIMID " (parameter 1)"); ParseClaimtrieId(request.params[0], claimId, T_CLAIMID " (parameter 1)");
if (claimId.length() < 6) 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; std::string foundName;
CClaimValue foundClaim; CClaimValue foundClaim;
@ -660,12 +660,14 @@ UniValue getnameproof(const JSONRPCRequest& request)
if (request.params.size() > 2) { if (request.params.size() > 2) {
std::string claimId; std::string claimId;
ParseClaimtrieId(request.params[2], claimId, T_CLAIMID " (optional parameter 3)"); 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; std::string foundClaimName;
if (!trieCache.findNameForClaim(ParseHex(claimId), claim, foundClaimName) if (!trieCache.findNameForClaim(ParseHex(claimId), claim, foundClaimName)
|| foundClaimName != trieCache.adjustNameForValidHeight(name, validHeight)) || foundClaimName != trieCache.adjustNameForValidHeight(name, validHeight))
throw JSONRPCError(RPC_INVALID_PARAMETER, "Failed to locate a claim with ID " + claimId); return {UniValue::VOBJ};
} } else if (!trieCache.getInfoForName(name, claim))
else trieCache.getInfoForName(name, claim); return {UniValue::VOBJ};
CClaimTrieProof proof; CClaimTrieProof proof;
if (!trieCache.getProofForName(name, claim.claimId, proof)) if (!trieCache.getProofForName(name, claim.claimId, proof))
@ -698,7 +700,7 @@ UniValue getclaimproofbybid(const JSONRPCRequest& request)
auto csToName = trieCache.getClaimsForName(name); auto csToName = trieCache.getClaimsForName(name);
if (std::size_t(bid) >= csToName.claimsNsupports.size()) if (std::size_t(bid) >= csToName.claimsNsupports.size())
return {UniValue::VARR}; return {UniValue::VOBJ};
auto match = csToName.claimsNsupports[bid].claim.claimId; auto match = csToName.claimsNsupports[bid].claim.claimId;
CClaimTrieProof proof; CClaimTrieProof proof;
@ -731,7 +733,7 @@ UniValue getclaimproofbyseq(const JSONRPCRequest& request)
std::string name = request.params[0].get_str(); std::string name = request.params[0].get_str();
auto csToName = trieCache.getClaimsForName(name); auto csToName = trieCache.getClaimsForName(name);
if (std::size_t(seq) >= csToName.claimsNsupports.size()) if (std::size_t(seq) >= csToName.claimsNsupports.size())
return {UniValue::VARR}; return {UniValue::VOBJ};
auto sorted = seqSort(csToName.claimsNsupports); auto sorted = seqSort(csToName.claimsNsupports);
auto match = sorted[seq].claim.claimId; auto match = sorted[seq].claim.claimId;

View file

@ -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_SEQUENCE].get_int(), claim1seq);
BOOST_CHECK_EQUAL(result[T_CLAIMID].get_str(), claimId1.GetHex()); 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 = 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); result = getclaimbyid(req);
BOOST_CHECK_EQUAL(result[T_LASTTAKEOVERHEIGHT].get_int(), height); 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()); auto claimHash = getValueHash(COutPoint(tx2.GetHash(), 1), result[T_LASTTAKEOVERHEIGHT].get_int());
ValidatePairs(fixture, jsonToPairs(result[T_PAIRS]), claimHash); 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 = UniValue(UniValue::VARR);
req.params.push_back(UniValue(name)); req.params.push_back(UniValue(name));
req.params.push_back(UniValue(blockhash.GetHex())); 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); result = getnameproof(req);
claimHash = getValueHash(COutPoint(tx2.GetHash(), 0), result[T_LASTTAKEOVERHEIGHT].get_int()); claimHash = getValueHash(COutPoint(tx2.GetHash(), 0), result[T_LASTTAKEOVERHEIGHT].get_int());