From 4e932647178fd40dd00dfdb606d66da347a324e0 Mon Sep 17 00:00:00 2001 From: Jianping Wu Date: Fri, 2 Nov 2018 14:33:50 -0700 Subject: [PATCH] Implemented keva_get. --- src/wallet/rpckeva.cpp | 56 +++++++++++++++++++++++++++++++++++++++- src/wallet/rpcwallet.cpp | 4 ++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/wallet/rpckeva.cpp b/src/wallet/rpckeva.cpp index 96699f2b8..cbe39c8bb 100644 --- a/src/wallet/rpckeva.cpp +++ b/src/wallet/rpckeva.cpp @@ -185,7 +185,7 @@ UniValue keva_put(const JSONRPCRequest& request) return NullUniValue; } - if (request.fHelp) { + if (request.fHelp || request.params.size() != 3) { throw std::runtime_error ( "keva_put \"namespace\" \"key\" \"value\" (\"create_namespace\")\n" "\nUpdate a name and possibly transfer it.\n" @@ -201,6 +201,11 @@ UniValue keva_put(const JSONRPCRequest& request) ); } + RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VSTR, UniValue::VSTR}); + RPCTypeCheckArgument(request.params[0], UniValue::VSTR); + RPCTypeCheckArgument(request.params[1], UniValue::VSTR); + RPCTypeCheckArgument(request.params[2], UniValue::VSTR); + ObserveSafeMode (); const std::string namespaceStr = request.params[0].get_str (); @@ -268,3 +273,52 @@ UniValue keva_put(const JSONRPCRequest& request) return wtx.GetHash().GetHex(); } + +UniValue keva_get(const JSONRPCRequest& request) +{ + CWallet* const pwallet = GetWalletForJSONRPCRequest(request); + if (!EnsureWalletIsAvailable (pwallet, request.fHelp)) { + return NullUniValue; + } + + if (request.fHelp || request.params.size() != 2) { + throw std::runtime_error ( + "keva_get \"namespace\" \"key\"\n" + "\nGet value of the given key.\n" + + HelpRequiringPassphrase (pwallet) + + "\nArguments:\n" + "1. \"namespace\" (string, required) the namespace to insert the key to\n" + "2. \"key\" (string, required) value for the key\n" + "\nResult:\n" + "\"value\" (string) the value associated with the key\n" + "\nExamples:\n" + + HelpExampleCli ("keva_get", "\"namespace_id\", \"key\"") + ); + } + + RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VSTR}); + RPCTypeCheckArgument(request.params[0], UniValue::VSTR); + RPCTypeCheckArgument(request.params[1], UniValue::VSTR); + + ObserveSafeMode (); + + const std::string namespaceStr = request.params[0].get_str (); + const valtype nameSpace = ValtypeFromString (namespaceStr); + if (nameSpace.size () > MAX_NAMESPACE_LENGTH) + throw JSONRPCError (RPC_INVALID_PARAMETER, "the namespace is too long"); + + const std::string keyStr = request.params[1].get_str (); + const valtype key = ValtypeFromString (keyStr); + if (key.size () > MAX_KEY_LENGTH) + throw JSONRPCError (RPC_INVALID_PARAMETER, "the key is too long"); + + CKevaData data; + { + LOCK (cs_main); + if (!pcoinsTip->GetName(nameSpace, key, data)) { + throw JSONRPCError (RPC_TRANSACTION_ERROR, "key not found"); + } + } + + return ValtypeToString(data.getValue()); +} diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index df72834b7..3709e0704 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -3585,6 +3585,7 @@ extern UniValue rescanblockchain(const JSONRPCRequest& request); // in rpckeva.cpp extern UniValue keva_namespace(const JSONRPCRequest& request); extern UniValue keva_put(const JSONRPCRequest& request); +extern UniValue keva_get(const JSONRPCRequest& request); extern UniValue keva_list_namespaces(const JSONRPCRequest& request); static const CRPCCommand commands[] = @@ -3647,7 +3648,8 @@ static const CRPCCommand commands[] = // Kevacoin-specific wallet calls. { "kevacoin", "keva_namespace", &keva_namespace, {"display_name"} }, { "kevacoin", "keva_list_namespaces", &keva_list_namespaces, {} }, - { "kevacoin", "keva_put", &keva_put, {"namespace", "key", "value", "put_value"} } + { "kevacoin", "keva_put", &keva_put, {"namespace", "key", "value"} }, + { "kevacoin", "keva_get", &keva_get, {"namespace", "key"} } }; void RegisterWalletRPCCommands(CRPCTable &t)