Refactor out a version of lookupLedger returning Status.

This commit is contained in:
Tom Ritchford
2014-12-10 14:42:13 -05:00
committed by Vinnie Falco
parent c72db5fa5f
commit a360c481c2
5 changed files with 78 additions and 71 deletions

View File

@@ -31,32 +31,23 @@ LedgerHandler::LedgerHandler (Context& context) : context_ (context)
{ {
} }
Status LedgerHandler::check (Json::Value& error) Status LedgerHandler::check ()
{ {
bool needsLedger = context_.params.isMember (jss::ledger) || auto const& params = context_.params;
context_.params.isMember (jss::ledger_hash) || bool needsLedger = params.isMember (jss::ledger) ||
context_.params.isMember (jss::ledger_index); params.isMember (jss::ledger_hash) ||
params.isMember (jss::ledger_index);
if (!needsLedger) if (!needsLedger)
return Status::OK; return Status::OK;
lookupResult_ = RPC::lookupLedger ( if (auto s = RPC::lookupLedger (params, ledger_, context_.netOps, result_))
context_.params, ledger_, context_.netOps); return s;
if (!ledger_) bool bFull = params[jss::full].asBool();
{ bool bTransactions = params[jss::transactions].asBool();
error = lookupResult_; bool bAccounts = params[jss::accounts].asBool();
auto code = error_code_i (error[jss::error_code].asInt()); bool bExpand = params[jss::expand].asBool();
return {code, {error[jss::error_message].asString()}};
}
bool bFull = context_.params.isMember (jss::full)
&& context_.params[jss::full].asBool ();
bool bTransactions = context_.params.isMember (jss::transactions)
&& context_.params[jss::transactions].asBool ();
bool bAccounts = context_.params.isMember (jss::accounts)
&& context_.params[jss::accounts].asBool ();
bool bExpand = context_.params.isMember (jss::expand)
&& context_.params[jss::expand].asBool ();
options_ = (bFull ? LEDGER_JSON_FULL : 0) options_ = (bFull ? LEDGER_JSON_FULL : 0)
| (bExpand ? LEDGER_JSON_EXPAND : 0) | (bExpand ? LEDGER_JSON_EXPAND : 0)
| (bTransactions ? LEDGER_JSON_DUMP_TXRP : 0) | (bTransactions ? LEDGER_JSON_DUMP_TXRP : 0)
@@ -64,24 +55,19 @@ Status LedgerHandler::check (Json::Value& error)
if (bFull || bAccounts) if (bFull || bAccounts)
{ {
// Until some sane way to get full ledgers has been implemented,
// disallow retrieving all state nodes.
if (context_.role != Role::ADMIN) if (context_.role != Role::ADMIN)
{
// Until some sane way to get full ledgers has been implemented,
// disallow retrieving all state nodes.
error = rpcError (rpcNO_PERMISSION);
return rpcNO_PERMISSION; return rpcNO_PERMISSION;
}
if (getApp().getFeeTrack().isLoadedLocal() && if (getApp().getFeeTrack().isLoadedLocal() &&
context_.role != Role::ADMIN) context_.role != Role::ADMIN)
{ {
error = rpcError(rpcTOO_BUSY);
return rpcTOO_BUSY; return rpcTOO_BUSY;
} }
context_.loadType = Resource::feeHighBurdenRPC; context_.loadType = Resource::feeHighBurdenRPC;
} }
std::cerr << "!!! LedgerHandler::check FIVE - true\n";
return Status::OK; return Status::OK;
} }

View File

@@ -40,7 +40,7 @@ class LedgerHandler {
public: public:
explicit LedgerHandler (Context&); explicit LedgerHandler (Context&);
Status check (Json::Value& error); Status check ();
template <class Object> template <class Object>
void writeResult (Object&); void writeResult (Object&);
@@ -63,7 +63,7 @@ public:
private: private:
Context& context_; Context& context_;
Ledger::pointer ledger_; Ledger::pointer ledger_;
Json::Value lookupResult_; Json::Value result_;
int options_; int options_;
}; };
@@ -77,7 +77,7 @@ void LedgerHandler::writeResult (Object& value)
{ {
if (ledger_) if (ledger_)
{ {
RPC::copyFrom (value, lookupResult_); RPC::copyFrom (value, result_);
addJson (*ledger_, value, options_, context_.yield); addJson (*ledger_, value, options_, context_.yield);
} }
else else

View File

@@ -35,6 +35,19 @@ Handler::Method<Json::Value> byRef (Function const& f)
}; };
} }
template <class Object, class HandlerImpl>
Status handle (Context& context, Object& object)
{
HandlerImpl handler (context);
auto status = handler.check ();
if (status)
status.inject (object);
else
handler.writeResult (object);
return status;
};
class HandlerTable { class HandlerTable {
public: public:
HandlerTable (std::vector<Handler> const& entries) { HandlerTable (std::vector<Handler> const& entries) {
@@ -56,39 +69,19 @@ class HandlerTable {
private: private:
std::map<std::string, Handler> table_; std::map<std::string, Handler> table_;
template <class Impl> template <class HandlerImpl>
void addHandler() void addHandler()
{ {
assert (table_.find(Impl::name()) == table_.end()); assert (table_.find(HandlerImpl::name()) == table_.end());
auto valueMethod = [] (Context& context, Json::Value& object)
{
Impl handler (context);
auto status = handler.check (object);
if (!status)
handler.writeResult (object);
return status;
};
auto objectMethod = [] (Context& context, Object& object) Handler h;
{ h.name_ = HandlerImpl::name(),
Impl handler (context); h.valueMethod_ = &handle<Json::Value, HandlerImpl>,
Json::Value error; h.role_ = HandlerImpl::role(),
auto status = handler.check (error); h.condition_ = HandlerImpl::condition(),
if (!status) h.objectMethod_ = &handle<Object, HandlerImpl>;
handler.writeResult (object);
else
RPC::copyFrom (object, error);
return status;
};
auto handler = Handler { table_[HandlerImpl::name()] = h;
Impl::name(),
valueMethod,
Impl::role(),
Impl::condition(),
objectMethod};
table_[Impl::name()] = handler;
}; };
}; };

View File

@@ -45,10 +45,11 @@ static const int LEDGER_VALIDATED = -3;
// return value. Otherwise, the object contains the field "validated" and // return value. Otherwise, the object contains the field "validated" and
// optionally the fields "ledger_hash", "ledger_index" and // optionally the fields "ledger_hash", "ledger_index" and
// "ledger_current_index", if they are defined. // "ledger_current_index", if they are defined.
Json::Value lookupLedger ( Status lookupLedger (
Json::Value const& params, Json::Value const& params,
Ledger::pointer& ledger, Ledger::pointer& ledger,
NetworkOPs& netOps) NetworkOPs& netOps,
Json::Value& jsonResult)
{ {
using RPC::make_error; using RPC::make_error;
ledger.reset(); ledger.reset();
@@ -74,7 +75,7 @@ Json::Value lookupLedger (
uint256 ledgerHash; uint256 ledgerHash;
if (!jsonHash.isString() || !ledgerHash.SetHex (jsonHash.asString ())) if (!jsonHash.isString() || !ledgerHash.SetHex (jsonHash.asString ()))
return make_error(rpcINVALID_PARAMS, "ledgerHashMalformed"); return {rpcINVALID_PARAMS, "ledgerHashMalformed"};
std::int32_t ledgerIndex = LEDGER_CURRENT; std::int32_t ledgerIndex = LEDGER_CURRENT;
@@ -97,7 +98,7 @@ Json::Value lookupLedger (
else if (index == "validated") else if (index == "validated")
ledgerIndex = LEDGER_VALIDATED; ledgerIndex = LEDGER_VALIDATED;
else else
return make_error(rpcINVALID_PARAMS, "ledgerIndexMalformed"); return {rpcINVALID_PARAMS, "ledgerIndexMalformed"};
} }
} }
else else
@@ -105,7 +106,7 @@ Json::Value lookupLedger (
ledger = netOps.getLedgerByHash (ledgerHash); ledger = netOps.getLedgerByHash (ledgerHash);
if (!ledger) if (!ledger)
return make_error(rpcLGR_NOT_FOUND, "ledgerNotFound"); return {rpcLGR_NOT_FOUND, "ledgerNotFound"};
ledgerIndex = ledger->getLedgerSeq (); ledgerIndex = ledger->getLedgerSeq ();
} }
@@ -128,7 +129,7 @@ Json::Value lookupLedger (
break; break;
default: default:
return make_error(rpcINVALID_PARAMS, "ledgerIndexMalformed"); return {rpcINVALID_PARAMS, "ledgerIndexMalformed"};
} }
assert (ledger->isImmutable()); assert (ledger->isImmutable());
@@ -142,10 +143,9 @@ Json::Value lookupLedger (
ledger = netOps.getLedgerBySeq (ledgerIndex); ledger = netOps.getLedgerBySeq (ledgerIndex);
if (!ledger) if (!ledger)
return make_error(rpcLGR_NOT_FOUND, "ledgerNotFound"); return {rpcLGR_NOT_FOUND, "ledgerNotFound"};
} }
Json::Value jsonResult;
if (ledger->isClosed ()) if (ledger->isClosed ())
{ {
if (ledgerHash != zero) if (ledgerHash != zero)
@@ -190,7 +190,19 @@ Json::Value lookupLedger (
} }
} }
return jsonResult; return Status::OK;
}
Json::Value lookupLedger (
Json::Value const& params,
Ledger::pointer& ledger,
NetworkOPs& netOps)
{
Json::Value value (Json::objectValue);
if (auto status = lookupLedger (params, ledger, netOps, value))
status.inject (value);
return value;
} }
} // RPC } // RPC

View File

@@ -20,13 +20,29 @@
#ifndef RIPPLE_RPC_LOOKUPLEDGER_H_INCLUDED #ifndef RIPPLE_RPC_LOOKUPLEDGER_H_INCLUDED
#define RIPPLE_RPC_LOOKUPLEDGER_H_INCLUDED #define RIPPLE_RPC_LOOKUPLEDGER_H_INCLUDED
#include <ripple/rpc/Status.h>
namespace ripple { namespace ripple {
namespace RPC { namespace RPC {
/** Look up a ledger from a request and fill a Json::Result with either
an error, or data representing a ledger.
If there is no error in the return value, then the ledger pointer will have
been filled.
*/
Json::Value lookupLedger ( Json::Value lookupLedger (
Json::Value const& jvRequest, Json::Value const& request, Ledger::pointer&, NetworkOPs&);
Ledger::pointer& lpLedger,
NetworkOPs& netOps); /** Look up a ledger from a request and fill a Json::Result with the data
representing a ledger.
If the returned Status is OK, the ledger pointer will have been filled. */
Status lookupLedger (
Json::Value const& request,
Ledger::pointer&,
NetworkOPs&,
Json::Value& result);
} // RPC } // RPC
} // ripple } // ripple