From 5e7ad62f2184863d7d353a8f8bce57f12054127e Mon Sep 17 00:00:00 2001 From: Brannon King Date: Fri, 6 Sep 2019 12:53:55 -0600 Subject: [PATCH] added support for tips in RPC, minor cleanup --- src/claimtrie.h | 8 +++++-- src/rpc/claimrpchelp.h | 9 +++---- src/rpc/claimtrie.cpp | 2 +- src/rpc/client.cpp | 5 ++++ src/wallet/rpcwallet.cpp | 51 ++++++++++++++++++++++++---------------- 5 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/claimtrie.h b/src/claimtrie.h index 81eadf76c..f4c443ce9 100644 --- a/src/claimtrie.h +++ b/src/claimtrie.h @@ -348,8 +348,12 @@ struct CClaimSupportToName 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; + std::string lowered(partialId); + 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; } diff --git a/src/rpc/claimrpchelp.h b/src/rpc/claimrpchelp.h index 2933d1ba1..d308bd870 100644 --- a/src/rpc/claimrpchelp.h +++ b/src/rpc/claimrpchelp.h @@ -61,7 +61,7 @@ enum { CHECKNORMALIZATION, GETCLAIMBYBID, GETCLAIMBYSEQ, - GETCLAIMPROOFBYID, + GETCLAIMPROOFBYBID, GETCLAIMPROOFBYSEQ, 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_AMOUNT, " (numeric) the amount of 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_VALUE, " (string) the metadata of the support if any") \ S3(" ", T_ADDRESS, " (string) the destination address of the support") \ @@ -305,7 +305,7 @@ S1("Result: [") CLAIM_OUTPUT "]", -// GETCLAIMPROOFBYID +// GETCLAIMPROOFBYBID S1(R"(getclaimproofbyid Return the cryptographic proof that a name maps to a value or doesn't by a bid. Arguments:)") @@ -329,7 +329,8 @@ PROOF_OUTPUT // 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:)") S3("1. ", T_BLOCKHASH, BLOCKHASH_TEXT) S1("Result: [") diff --git a/src/rpc/claimtrie.cpp b/src/rpc/claimtrie.cpp index 3a69591e9..12e9b5e74 100644 --- a/src/rpc/claimtrie.cpp +++ b/src/rpc/claimtrie.cpp @@ -770,7 +770,7 @@ UniValue getnameproof(const JSONRPCRequest& request) UniValue getclaimproofbybid(const JSONRPCRequest& request) { - validateRequest(request, GETCLAIMPROOFBYID, 1, 2); + validateRequest(request, GETCLAIMPROOFBYBID, 1, 2); LOCK(cs_main); CCoinsViewCache coinsCache(pcoinsTip.get()); diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index 086d2328a..1ff7eb504 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -172,6 +172,11 @@ static const CRPCConvertParam vRPCConvertParams[] = { "listnameclaims", 0, "includesupports"}, { "listnameclaims", 1, "activeonly"}, { "listnameclaims", 2, "minconf"}, + { "getclaimbybid", 1, "bid"}, + { "getclaimbyseq", 1, "sequence"}, + { "getclaimproofbybid", 1, "bid"}, + { "getclaimproofbyseq", 1, "sequence"}, + { "supportclaim", 4, "isTip"}, { "stop", 0, "wait" }, }; // clang-format on diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 262231e19..9a0ebc5c2 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -735,9 +735,10 @@ UniValue supportclaim(const JSONRPCRequest& request) + HelpRequiringPassphrase(pwallet) + "\nArguments:\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" "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" "\"txId\" (string) The transaction id of the support.\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); EnsureWalletIsUnlocked(pwallet); - uint160 claimId; - if (sClaimId.length() != claimLength) { - CClaimTrieCache trieCache(pclaimTrie); - auto csToName = trieCache.getClaimsForName(sName); - if (csToName.claimsNsupports.empty()) - return NullUniValue; - auto& claimNsupports = !sClaimId.empty() ? csToName.find(sClaimId) : csToName.claimsNsupports[0]; - if (claimNsupports.IsNull()) - return NullUniValue; - claimId = claimNsupports.claim.claimId; - } else { - claimId.SetHex(sClaimId); - } + CClaimTrieCache trieCache(pclaimTrie); + auto csToName = trieCache.getClaimsForName(sName); + if (csToName.claimsNsupports.empty()) + return NullUniValue; + auto& claimNsupports = !sClaimId.empty() ? csToName.find(sClaimId) : csToName.claimsNsupports[0]; + if (claimNsupports.IsNull()) + throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Unable to find a claimid that starts with %s", sClaimId)); + auto& claimId = claimNsupports.claim.claimId; std::vector vchName (sName.begin(), sName.end()); std::vector vchClaimId (claimId.begin(), claimId.end()); @@ -794,13 +790,28 @@ UniValue supportclaim(const JSONRPCRequest& request) supportScript = supportScript << OP_2DROP << lastOp; - CPubKey newKey; - if (!pwallet->GetKeyFromPool(newKey)) - throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first"); + auto isTip = false; + if (request.params.size() > 4) + isTip = request.params[4].get_bool(); - OutputType output_type = pwallet->m_default_address_type; - pwallet->LearnRelatedScripts(newKey, output_type); - CTxDestination dest = GetDestinationForKey(newKey, output_type); + 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; + if (!pwallet->GetKeyFromPool(newKey)) + throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first"); + + OutputType output_type = pwallet->m_default_address_type; + pwallet->LearnRelatedScripts(newKey, output_type); + dest = GetDestinationForKey(newKey, output_type); + } CCoinControl cc; cc.m_change_type = pwallet->m_default_change_type;