Add 'type' param to ledger_data and ledger rpc commands (RIPD-1446):

The 'type' field allows the rpc client to specify what type of ledger
entries to retrieve. The available types are:

    "account"
    "amendments"
    "directory"
    "fee"
    "hashes"
    "offer"
    "signer_list"
    "state"
    "ticket"
This commit is contained in:
Howard Hinnant
2017-03-17 16:20:45 -04:00
committed by Nik Bougalis
parent fab3ec0b56
commit 1a7a6f22e2
13 changed files with 293 additions and 62 deletions

View File

@@ -31,17 +31,12 @@ namespace ripple {
struct LedgerFill
{
LedgerFill (ReadView const& l, int o = 0)
LedgerFill (ReadView const& l, int o = 0, std::vector<TxQ::TxDetails> q = {},
LedgerEntryType t = ltINVALID)
: ledger (l)
, options (o)
{
}
LedgerFill(ReadView const& l, int o,
std::vector<TxQ::TxDetails> q)
: ledger(l)
, options(o)
, txQueue(q)
, txQueue(std::move(q))
, type (t)
{
}
@@ -58,6 +53,7 @@ struct LedgerFill
ReadView const& ledger;
int options;
std::vector<TxQ::TxDetails> txQueue;
LedgerEntryType type;
};
/** Given a Ledger and options, fill a Json::Object or Json::Value with a

View File

@@ -170,16 +170,19 @@ void fillJsonState(Object& json, LedgerFill const& fill)
for(auto const& sle : ledger.sles)
{
if (binary)
if (fill.type == ltINVALID || sle->getType () == fill.type)
{
auto&& obj = appendObject(array);
obj[jss::hash] = to_string(sle->key());
obj[jss::tx_blob] = serializeHex(*sle);
if (binary)
{
auto&& obj = appendObject(array);
obj[jss::hash] = to_string(sle->key());
obj[jss::tx_blob] = serializeHex(*sle);
}
else if (expanded)
array.append(sle->getJson(0));
else
array.append(to_string(sle->key()));
}
else if (expanded)
array.append(sle->getJson(0));
else
array.append(to_string(sle->key()));
}
}

View File

@@ -21,6 +21,7 @@
#define RIPPLE_PROTOCOL_LEDGERFORMATS_H_INCLUDED
#include <ripple/protocol/KnownFormats.h>
#include <ripple/rpc/Status.h>
namespace ripple {
@@ -163,6 +164,9 @@ private:
void addCommonFields (Item& item);
};
std::pair<RPC::Status, LedgerEntryType>
chooseLedgerEntryType(Json::Value const& params);
} // ripple
#endif

View File

@@ -18,7 +18,12 @@
//==============================================================================
#include <BeastConfig.h>
#include <ripple/protocol/ErrorCodes.h>
#include <ripple/protocol/JsonFields.h>
#include <ripple/protocol/LedgerFormats.h>
#include <algorithm>
#include <array>
#include <utility>
namespace ripple {
@@ -165,4 +170,49 @@ LedgerFormats::getInstance ()
return instance;
}
std::pair<RPC::Status, LedgerEntryType>
chooseLedgerEntryType(Json::Value const& params)
{
std::pair<RPC::Status, LedgerEntryType> result{RPC::Status::OK, ltINVALID};
if (params.isMember(jss::type))
{
static
std::array<std::pair<char const *, LedgerEntryType>, 9> const types
{{
{ jss::account, ltACCOUNT_ROOT },
{ jss::amendments, ltAMENDMENTS },
{ jss::directory, ltDIR_NODE },
{ jss::fee, ltFEE_SETTINGS },
{ jss::hashes, ltLEDGER_HASHES },
{ jss::offer, ltOFFER },
{ jss::signer_list, ltSIGNER_LIST },
{ jss::state, ltRIPPLE_STATE },
{ jss::ticket, ltTICKET }
}};
auto const& p = params[jss::type];
if (!p.isString())
{
result.first = RPC::Status{rpcINVALID_PARAMS,
"Invalid field 'type', not string."};
assert(result.first.type() == RPC::Status::Type::error_code_i);
return result;
}
auto const filter = p.asString ();
auto iter = std::find_if (types.begin (), types.end (),
[&filter](decltype (types.front ())& t)
{ return t.first == filter; });
if (iter == types.end ())
{
result.first = RPC::Status{rpcINVALID_PARAMS,
"Invalid field 'type'."};
assert(result.first.type() == RPC::Status::Type::error_code_i);
return result;
}
result.second = iter->second;
}
return result;
}
} // ripple

View File

@@ -43,7 +43,7 @@ public:
using Code = int;
using Strings = std::vector <std::string>;
static const Code OK = 0;
static constexpr Code OK = 0;
Status () = default;

View File

@@ -25,6 +25,7 @@
#include <ripple/protocol/ErrorCodes.h>
#include <ripple/protocol/Indexes.h>
#include <ripple/protocol/JsonFields.h>
#include <ripple/protocol/LedgerFormats.h>
#include <ripple/resource/Fees.h>
#include <ripple/rpc/Context.h>
#include <ripple/rpc/impl/RPCHelpers.h>
@@ -72,36 +73,12 @@ Json::Value doAccountObjects (RPC::Context& context)
if (! ledger->exists(keylet::account (accountID)))
return rpcError (rpcACT_NOT_FOUND);
auto type = ltINVALID;
if (params.isMember (jss::type))
auto type = chooseLedgerEntryType(params);
if (type.first)
{
static
std::array<std::pair<char const *, LedgerEntryType>, 9> const
types
{{
{ jss::account, ltACCOUNT_ROOT },
{ jss::amendments, ltAMENDMENTS },
{ jss::directory, ltDIR_NODE },
{ jss::fee, ltFEE_SETTINGS },
{ jss::hashes, ltLEDGER_HASHES },
{ jss::offer, ltOFFER },
{ jss::signer_list, ltSIGNER_LIST },
{ jss::state, ltRIPPLE_STATE },
{ jss::ticket, ltTICKET }
}};
auto const& p = params[jss::type];
if (! p.isString ())
return RPC::expected_field_error (jss::type, "string");
auto const filter = p.asString ();
auto iter = std::find_if (types.begin (), types.end (),
[&filter](decltype (types.front ())& t)
{ return t.first == filter; });
if (iter == types.end ())
return RPC::invalid_field_error (jss::type);
type = iter->second;
result.clear();
type.first.inject(result);
return result;
}
unsigned int limit;
@@ -131,7 +108,7 @@ Json::Value doAccountObjects (RPC::Context& context)
return RPC::invalid_field_error (jss::marker);
}
if (! RPC::getAccountObjects (*ledger, accountID, type,
if (! RPC::getAccountObjects (*ledger, accountID, type.second,
dirIndex, entryIndex, limit, result))
{
result[jss::account_objects] = Json::arrayValue;

View File

@@ -22,6 +22,7 @@
#include <ripple/ledger/ReadView.h>
#include <ripple/protocol/ErrorCodes.h>
#include <ripple/protocol/JsonFields.h>
#include <ripple/protocol/LedgerFormats.h>
#include <ripple/rpc/impl/RPCHelpers.h>
#include <ripple/rpc/impl/Tuning.h>
#include <ripple/rpc/Context.h>
@@ -34,6 +35,7 @@ namespace ripple {
// limit: integer, maximum number of entries
// marker: opaque, resume point
// binary: boolean, format
// type: string // optional, defaults to all ledger node types
// Outputs:
// ledger_hash: chosen ledger's hash
// ledger_index: chosen ledger's index
@@ -84,6 +86,13 @@ Json::Value doLedgerData (RPC::Context& context)
LedgerFill::Options::binary : 0));
}
auto type = chooseLedgerEntryType(params);
if (type.first)
{
jvResult.clear();
type.first.inject(jvResult);
return jvResult;
}
Json::Value& nodes = jvResult[jss::state];
auto e = lpLedger->sles.end();
@@ -98,16 +107,19 @@ Json::Value doLedgerData (RPC::Context& context)
break;
}
if (isBinary)
if (type.second == ltINVALID || sle->getType () == type.second)
{
Json::Value& entry = nodes.append (Json::objectValue);
entry[jss::data] = serializeHex(*sle);
entry[jss::index] = to_string(sle->key());
}
else
{
Json::Value& entry = nodes.append (sle->getJson (0));
entry[jss::index] = to_string(sle->key());
if (isBinary)
{
Json::Value& entry = nodes.append (Json::objectValue);
entry[jss::data] = serializeHex(*sle);
entry[jss::index] = to_string(sle->key());
}
else
{
Json::Value& entry = nodes.append (sle->getJson (0));
entry[jss::index] = to_string(sle->key());
}
}
}

View File

@@ -55,6 +55,10 @@ Status LedgerHandler::check()
bool const binary = params[jss::binary].asBool();
bool const owner_funds = params[jss::owner_funds].asBool();
bool const queue = params[jss::queue].asBool();
auto type = chooseLedgerEntryType(params);
if (type.first)
return type.first;
type_ = type.second;
options_ = (full ? LedgerFill::full : 0)
| (expand ? LedgerFill::expand : 0)

View File

@@ -76,6 +76,7 @@ private:
std::vector<TxQ::TxDetails> queueTxs_;
Json::Value result_;
int options_ = 0;
LedgerEntryType type_;
};
////////////////////////////////////////////////////////////////////////////////
@@ -89,7 +90,7 @@ void LedgerHandler::writeResult (Object& value)
if (ledger_)
{
Json::copyFrom (value, result_);
addJson (value, {*ledger_, options_, queueTxs_});
addJson (value, {*ledger_, options_, queueTxs_, type_});
}
else
{

View File

@@ -139,7 +139,6 @@ error_code_i fillHandler (Context& context,
JLOG (context.j.trace()) << "COMMAND:" << strCommand;
JLOG (context.j.trace()) << "REQUEST:" << context.params;
auto handler = getHandler(strCommand);
if (!handler)

View File

@@ -23,6 +23,8 @@
namespace ripple {
namespace RPC {
constexpr Status::Code Status::OK;
std::string Status::codeString () const
{
if (!*this)