Skip to content

Commit

Permalink
Add options argument to getNameInfo.
Browse files Browse the repository at this point in the history
Refactor the encoding handling in the name RPCs a bit; in particular,
add an options argument to all getNameInfo variants, that allows to
override the configured default encodings for names/values that is
applied for the output.

For now, NO_OPTIONS is passed at all call sites.  This will be changed
in a follow-up, though, to ultimately implement options overrides for
read-type RPCs.  This is a still missing piece for resolving
namecoin/namecoin-core#246.
  • Loading branch information
domob1812 committed Oct 22, 2018
1 parent e4e4ad6 commit f156d78
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
3 changes: 2 additions & 1 deletion src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,8 @@ static bool rest_name(HTTPRequest* req, const std::string& strURIPart)

case RetFormat::JSON:
{
const UniValue obj = getNameInfo(plainName, data);
const UniValue NO_OPTIONS(UniValue::VOBJ);
const UniValue obj = getNameInfo(NO_OPTIONS, plainName, data);
const std::string strJSON = obj.write() + "\n";
req->WriteHeader("Content-Type", "application/json");
req->WriteReply(HTTP_OK, strJSON);
Expand Down
82 changes: 52 additions & 30 deletions src/rpc/names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,51 @@
#include <memory>
#include <stdexcept>

namespace
{

NameEncoding
EncodingFromOptionsJson (const UniValue& options, const std::string& field,
const NameEncoding defaultValue)
{
NameEncoding res = defaultValue;
RPCTypeCheckObj (options,
{
{field, UniValueType (UniValue::VSTR)},
},
true, false);
if (options.exists (field))
try
{
res = EncodingFromString (options[field].get_str ());
}
catch (const std::invalid_argument& exc)
{
LogPrintf ("Invalid value for %s in options: %s\n using default %s\n",
field, exc.what (), EncodingToString (defaultValue));
}

return res;
}

} // anonymous namespace

/**
* Utility routine to construct a "name info" object to return. This is used
* for name_show and also name_list.
*/
UniValue
getNameInfo (const valtype& name, const valtype& value,
getNameInfo (const UniValue& options,
const valtype& name, const valtype& value,
const COutPoint& outp, const CScript& addr)
{
UniValue obj(UniValue::VOBJ);
AddEncodedNameToUniv (obj, "name", name, ConfiguredNameEncoding ());
AddEncodedNameToUniv (obj, "value", value, ConfiguredValueEncoding ());
AddEncodedNameToUniv (obj, "name", name,
EncodingFromOptionsJson (options, "nameEncoding",
ConfiguredNameEncoding ()));
AddEncodedNameToUniv (obj, "value", value,
EncodingFromOptionsJson (options, "valueEncoding",
ConfiguredValueEncoding ()));
obj.pushKV ("txid", outp.hash.GetHex ());
obj.pushKV ("vout", static_cast<int> (outp.n));

Expand All @@ -60,9 +94,11 @@ getNameInfo (const valtype& name, const valtype& value,
* Return name info object for a CNameData object.
*/
UniValue
getNameInfo (const valtype& name, const CNameData& data)
getNameInfo (const UniValue& options,
const valtype& name, const CNameData& data)
{
UniValue result = getNameInfo (name, data.getValue (),
UniValue result = getNameInfo (options,
name, data.getValue (),
data.getUpdateOutpoint (),
data.getAddress ());
addExpirationInfo (data.getHeight (), result);
Expand Down Expand Up @@ -118,23 +154,7 @@ DecodeNameValueFromRPCOrThrow (const UniValue& val, const UniValue& opt,
const std::string& optKey,
const NameEncoding defaultEnc)
{
NameEncoding enc = defaultEnc;
RPCTypeCheckObj (opt,
{
{optKey, UniValueType (UniValue::VSTR)},
},
true, false);
if (opt.exists (optKey))
try
{
enc = EncodingFromString (opt[optKey].get_str ());
}
catch (const std::invalid_argument& exc)
{
LogPrintf ("Invalid value for %s in options: %s\n using default %s\n",
optKey, exc.what (), EncodingToString (defaultEnc));
}

const NameEncoding enc = EncodingFromOptionsJson (opt, optKey, defaultEnc);
try
{
return DecodeName (val.get_str (), enc);
Expand Down Expand Up @@ -238,10 +258,11 @@ addOwnershipInfo (const CScript& addr, const MaybeWalletForRequest& wallet,
* This is the most common call for methods in this file.
*/
UniValue
getNameInfo (const valtype& name, const CNameData& data,
getNameInfo (const UniValue& options,
const valtype& name, const CNameData& data,
const MaybeWalletForRequest& wallet)
{
UniValue res = getNameInfo (name, data);
UniValue res = getNameInfo (options, name, data);
addOwnershipInfo (data.getAddress (), wallet, res);
return res;
}
Expand Down Expand Up @@ -357,7 +378,7 @@ name_show (const JSONRPCRequest& request)

MaybeWalletForRequest wallet(request);
LOCK (wallet.getLock ());
return getNameInfo (name, data, wallet);
return getNameInfo (NO_OPTIONS, name, data, wallet);
}

/* ************************************************************************** */
Expand Down Expand Up @@ -418,8 +439,8 @@ name_history (const JSONRPCRequest& request)

UniValue res(UniValue::VARR);
for (const auto& entry : history.getData ())
res.push_back (getNameInfo (name, entry, wallet));
res.push_back (getNameInfo (name, data, wallet));
res.push_back (getNameInfo (NO_OPTIONS, name, entry, wallet));
res.push_back (getNameInfo (NO_OPTIONS, name, data, wallet));

return res;
}
Expand Down Expand Up @@ -475,7 +496,7 @@ name_scan (const JSONRPCRequest& request)
CNameData data;
std::unique_ptr<CNameIterator> iter(pcoinsTip->IterateNames ());
for (iter->seek (start); count > 0 && iter->next (name, data); --count)
res.push_back (getNameInfo (name, data, wallet));
res.push_back (getNameInfo (NO_OPTIONS, name, data, wallet));

return res;
}
Expand Down Expand Up @@ -599,7 +620,7 @@ name_filter (const JSONRPCRequest& request)
if (stats)
++count;
else
names.push_back (getNameInfo (name, data, wallet));
names.push_back (getNameInfo (NO_OPTIONS, name, data, wallet));

if (nb > 0)
{
Expand Down Expand Up @@ -680,7 +701,8 @@ name_pending (const JSONRPCRequest& request)
if (!op.isNameOp () || !op.isAnyUpdate ())
continue;

UniValue obj = getNameInfo (op.getOpName (), op.getOpValue (),
UniValue obj = getNameInfo (NO_OPTIONS,
op.getOpName (), op.getOpValue (),
COutPoint (tx->GetHash (), n),
op.getAddress ());
addOwnershipInfo (op.getAddress (), wallet, obj);
Expand Down
6 changes: 4 additions & 2 deletions src/rpc/names.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ class COutPoint;
class CScript;
class UniValue;

UniValue getNameInfo (const valtype& name, const valtype& value,
UniValue getNameInfo (const UniValue& options,
const valtype& name, const valtype& value,
const COutPoint& outp, const CScript& addr);
UniValue getNameInfo (const valtype& name, const CNameData& data);
UniValue getNameInfo (const UniValue& options,
const valtype& name, const CNameData& data);
void addExpirationInfo (int height, UniValue& data);

#ifdef ENABLE_WALLET
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/rpcnames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ name_list (const JSONRPCRequest& request)
continue;

UniValue obj
= getNameInfo (name, nameOp.getOpValue (),
= getNameInfo (NO_OPTIONS,
name, nameOp.getOpValue (),
COutPoint (tx.GetHash (), nOut),
nameOp.getAddress ());
addOwnershipInfo (nameOp.getAddress (), pwallet, obj);
Expand Down

0 comments on commit f156d78

Please sign in to comment.