Fix partial claim id search
Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
This commit is contained in:
parent
54a7df886a
commit
456ac2f562
4 changed files with 17 additions and 20 deletions
|
@ -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
|
||||
{
|
||||
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<char>::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;
|
||||
|
|
|
@ -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
|
||||
"]",
|
||||
|
|
|
@ -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);
|
||||
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;
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in a new issue