added support for tips in RPC, minor cleanup

This commit is contained in:
Brannon King 2019-09-06 12:53:55 -06:00 committed by Anthony Fieroni
parent 344e839581
commit 5e7ad62f21
5 changed files with 48 additions and 27 deletions

View file

@ -348,8 +348,12 @@ struct CClaimSupportToName
const CClaimNsupports& find(const std::string& partialId) const const CClaimNsupports& find(const std::string& partialId) const
{ {
auto it = std::find_if(claimsNsupports.begin(), claimsNsupports.end(), [&partialId](const CClaimNsupports& value) { std::string lowered(partialId);
return value.claim.claimId.GetHex().find(partialId) == 0; for (auto& c: lowered)
c = std::tolower(c);
auto it = std::find_if(claimsNsupports.begin(), claimsNsupports.end(), [&lowered](const CClaimNsupports& value) {
return value.claim.claimId.GetHex().find(lowered) == 0;
}); });
return it != claimsNsupports.end() ? *it : invalid; return it != claimsNsupports.end() ? *it : invalid;
} }

View file

@ -61,7 +61,7 @@ enum {
CHECKNORMALIZATION, CHECKNORMALIZATION,
GETCLAIMBYBID, GETCLAIMBYBID,
GETCLAIMBYSEQ, GETCLAIMBYSEQ,
GETCLAIMPROOFBYID, GETCLAIMPROOFBYBID,
GETCLAIMPROOFBYSEQ, GETCLAIMPROOFBYSEQ,
GETCHANGESINBLOCK, GETCHANGESINBLOCK,
}; };
@ -90,7 +90,7 @@ S3(" ", T_HEIGHT, " (numeric) the height of the block in whic
S3(" ", T_VALIDATHEIGHT, " (numeric) the height at which the support became/becomes valid") \ S3(" ", T_VALIDATHEIGHT, " (numeric) the height at which the support became/becomes valid") \
S3(" ", T_AMOUNT, " (numeric) the amount of the claim") \ 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_EFFECTIVEAMOUNT, " (numeric) the amount plus amount from all supports associated with the claim") \
S3(" ", T_PENDINGAMOUNT, " (numeric) expected amount when claim and its support got valid") \ S3(" ", T_PENDINGAMOUNT, " (numeric) expected amount when claim and its supports are all valid") \
S3(" ", T_SUPPORTS, ": [ (array of object) supports for this claim") \ S3(" ", T_SUPPORTS, ": [ (array of object) supports for this claim") \
S3(" ", T_VALUE, " (string) the metadata of the support if any") \ S3(" ", T_VALUE, " (string) the metadata of the support if any") \
S3(" ", T_ADDRESS, " (string) the destination address of the support") \ S3(" ", T_ADDRESS, " (string) the destination address of the support") \
@ -305,7 +305,7 @@ S1("Result: [")
CLAIM_OUTPUT CLAIM_OUTPUT
"]", "]",
// GETCLAIMPROOFBYID // GETCLAIMPROOFBYBID
S1(R"(getclaimproofbyid S1(R"(getclaimproofbyid
Return the cryptographic proof that a name maps to a value or doesn't by a bid. Return the cryptographic proof that a name maps to a value or doesn't by a bid.
Arguments:)") Arguments:)")
@ -329,7 +329,8 @@ PROOF_OUTPUT
// GETCHANGESINBLOCK // GETCHANGESINBLOCK
S1(R"(getchangesinblock S1(R"(getchangesinblock
Return the list of claims added, updated, and removed in a block or doesn't." Return the list of claims added, updated, and removed as pulled from the queued work for that block."
Use this method to determine which claims or supports went live on a given block."
Arguments:)") Arguments:)")
S3("1. ", T_BLOCKHASH, BLOCKHASH_TEXT) S3("1. ", T_BLOCKHASH, BLOCKHASH_TEXT)
S1("Result: [") S1("Result: [")

View file

@ -770,7 +770,7 @@ UniValue getnameproof(const JSONRPCRequest& request)
UniValue getclaimproofbybid(const JSONRPCRequest& request) UniValue getclaimproofbybid(const JSONRPCRequest& request)
{ {
validateRequest(request, GETCLAIMPROOFBYID, 1, 2); validateRequest(request, GETCLAIMPROOFBYBID, 1, 2);
LOCK(cs_main); LOCK(cs_main);
CCoinsViewCache coinsCache(pcoinsTip.get()); CCoinsViewCache coinsCache(pcoinsTip.get());

View file

@ -172,6 +172,11 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "listnameclaims", 0, "includesupports"}, { "listnameclaims", 0, "includesupports"},
{ "listnameclaims", 1, "activeonly"}, { "listnameclaims", 1, "activeonly"},
{ "listnameclaims", 2, "minconf"}, { "listnameclaims", 2, "minconf"},
{ "getclaimbybid", 1, "bid"},
{ "getclaimbyseq", 1, "sequence"},
{ "getclaimproofbybid", 1, "bid"},
{ "getclaimproofbyseq", 1, "sequence"},
{ "supportclaim", 4, "isTip"},
{ "stop", 0, "wait" }, { "stop", 0, "wait" },
}; };
// clang-format on // clang-format on

View file

@ -735,9 +735,10 @@ UniValue supportclaim(const JSONRPCRequest& request)
+ HelpRequiringPassphrase(pwallet) + + HelpRequiringPassphrase(pwallet) +
"\nArguments:\n" "\nArguments:\n"
"1. \"name\" (string, required) The name claimed by the claim to support.\n" "1. \"name\" (string, required) The name claimed by the claim to support.\n"
"2. \"claimid\" (string, optional) The claimid, partialid or best claim to support.\n" "2. \"claimid\" (string, optional) The claimid or partialid of the claim to support.\n"
"3. \"amount\" (numeric, optional) The amount in LBC to use to support the claim.\n" "3. \"amount\" (numeric, optional) The amount in LBC to use to support the claim.\n"
"4. \"value\" (string, optional) The metadata of the support encoded in hexadecimal.\n" "4. \"value\" (string, optional) The metadata of the support encoded in hexadecimal.\n"
"5. \"isTip\" (boolean, optional, defaults to false) spendable by owning claim's address when true.\n"
"\nResult: [\n" "\nResult: [\n"
"\"txId\" (string) The transaction id of the support.\n" "\"txId\" (string) The transaction id of the support.\n"
"\"address\" (string) The destination address of the claim.\n" "\"address\" (string) The destination address of the claim.\n"
@ -760,19 +761,14 @@ UniValue supportclaim(const JSONRPCRequest& request)
LOCK2(cs_main, pwallet->cs_wallet); LOCK2(cs_main, pwallet->cs_wallet);
EnsureWalletIsUnlocked(pwallet); EnsureWalletIsUnlocked(pwallet);
uint160 claimId;
if (sClaimId.length() != claimLength) {
CClaimTrieCache trieCache(pclaimTrie); CClaimTrieCache trieCache(pclaimTrie);
auto csToName = trieCache.getClaimsForName(sName); auto csToName = trieCache.getClaimsForName(sName);
if (csToName.claimsNsupports.empty()) if (csToName.claimsNsupports.empty())
return NullUniValue; return NullUniValue;
auto& claimNsupports = !sClaimId.empty() ? csToName.find(sClaimId) : csToName.claimsNsupports[0]; auto& claimNsupports = !sClaimId.empty() ? csToName.find(sClaimId) : csToName.claimsNsupports[0];
if (claimNsupports.IsNull()) if (claimNsupports.IsNull())
return NullUniValue; throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Unable to find a claimid that starts with %s", sClaimId));
claimId = claimNsupports.claim.claimId; auto& claimId = claimNsupports.claim.claimId;
} else {
claimId.SetHex(sClaimId);
}
std::vector<unsigned char> vchName (sName.begin(), sName.end()); std::vector<unsigned char> vchName (sName.begin(), sName.end());
std::vector<unsigned char> vchClaimId (claimId.begin(), claimId.end()); std::vector<unsigned char> vchClaimId (claimId.begin(), claimId.end());
@ -794,13 +790,28 @@ UniValue supportclaim(const JSONRPCRequest& request)
supportScript = supportScript << OP_2DROP << lastOp; supportScript = supportScript << OP_2DROP << lastOp;
auto isTip = false;
if (request.params.size() > 4)
isTip = request.params[4].get_bool();
CTxDestination dest;
if (isTip) {
CTransactionRef ref;
uint256 block;
if (!GetTransaction(claimNsupports.claim.outPoint.hash, ref, Params().GetConsensus(), block, true))
throw JSONRPCError(RPC_INVALID_PARAMETER, "Unable to locate the TX with the claim's output.");
if (!ExtractDestination(ref->vout[claimNsupports.claim.outPoint.n].scriptPubKey, dest))
throw JSONRPCError(RPC_INVALID_PARAMETER, "Unable to extract the destination from the chosen claim.");
}
else {
CPubKey newKey; CPubKey newKey;
if (!pwallet->GetKeyFromPool(newKey)) if (!pwallet->GetKeyFromPool(newKey))
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first"); throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
OutputType output_type = pwallet->m_default_address_type; OutputType output_type = pwallet->m_default_address_type;
pwallet->LearnRelatedScripts(newKey, output_type); pwallet->LearnRelatedScripts(newKey, output_type);
CTxDestination dest = GetDestinationForKey(newKey, output_type); dest = GetDestinationForKey(newKey, output_type);
}
CCoinControl cc; CCoinControl cc;
cc.m_change_type = pwallet->m_default_change_type; cc.m_change_type = pwallet->m_default_change_type;