From 125a1ae5d343f9c6fa2d66100659e021623b6932 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Mon, 3 Dec 2012 15:55:10 -0800 Subject: [PATCH] Refactor RPC ripple_lines_get. --- src/cpp/ripple/CallRPC.cpp | 22 +++++++++++++++++- src/cpp/ripple/CallRPC.h | 1 + src/cpp/ripple/RPCHandler.cpp | 42 +++++++++++++++++------------------ src/js/remote.js | 15 ++++++++----- 4 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/cpp/ripple/CallRPC.cpp b/src/cpp/ripple/CallRPC.cpp index eb9eae3e7d..b0cbf5ffe0 100644 --- a/src/cpp/ripple/CallRPC.cpp +++ b/src/cpp/ripple/CallRPC.cpp @@ -156,6 +156,26 @@ Json::Value RPCParser::parseLedger(const Json::Value& jvParams) return jvRequest; } +// ripple_lines_get || [] +Json::Value RPCParser::parseRippleLinesGet(const Json::Value& jvParams) +{ + std::string strIdent = jvParams[0u].asString(); + bool bIndex = 2 == jvParams.size(); + int iIndex = bIndex ? lexical_cast_s(jvParams[1u].asString()) : 0; + + if (bIndex && !iIndex) // Don't send default. + bIndex = false; + + // Get info on account. + Json::Value jvRequest(Json::objectValue); + + jvRequest["account"] = strIdent; + if (bIndex) + jvRequest["index"] = iIndex; + + return jvRequest; +} + // submit any transaction to the network // submit private_key json Json::Value RPCParser::parseSubmit(const Json::Value& jvParams) @@ -248,7 +268,7 @@ Json::Value RPCParser::parseCommand(std::string strMethod, Json::Value jvParams) // { "owner_info", &RPCParser::doOwnerInfo, 1, 2, false, false, optCurrent }, { "peers", &RPCParser::parseAsIs, 0, 0 }, // { "profile", &RPCParser::doProfile, 1, 9, false, false, optCurrent }, -// { "ripple_lines_get", &RPCParser::doRippleLinesGet, 1, 2, false, false, optCurrent }, + { "ripple_lines_get", &RPCParser::parseRippleLinesGet, 1, 2 }, // { "ripple_path_find", &RPCParser::doRipplePathFind, -1, -1, false, false, optCurrent }, { "submit", &RPCParser::parseSubmit, 2, 2 }, { "server_info", &RPCParser::parseAsIs, 0, 0 }, diff --git a/src/cpp/ripple/CallRPC.h b/src/cpp/ripple/CallRPC.h index df7b4c4f07..0c8d12a773 100644 --- a/src/cpp/ripple/CallRPC.h +++ b/src/cpp/ripple/CallRPC.h @@ -17,6 +17,7 @@ protected: Json::Value parseConnect(const Json::Value& jvParams); Json::Value parseEvented(const Json::Value& jvParams); Json::Value parseLedger(const Json::Value& jvParams); + Json::Value parseRippleLinesGet(const Json::Value& jvParams); Json::Value parseSubmit(const Json::Value& jvParams); Json::Value parseUnlAdd(const Json::Value& jvParams); Json::Value parseUnlDelete(const Json::Value& jvParams); diff --git a/src/cpp/ripple/RPCHandler.cpp b/src/cpp/ripple/RPCHandler.cpp index c8715c46f0..2ee5c92b47 100644 --- a/src/cpp/ripple/RPCHandler.cpp +++ b/src/cpp/ripple/RPCHandler.cpp @@ -549,42 +549,42 @@ Json::Value RPCHandler::doProfile(Json::Value params) return obj; } -// ripple_lines_get || [] -Json::Value RPCHandler::doRippleLinesGet(Json::Value params) +// { +// account: || [] +// index: // optional, defaults to 0. +// } +Json::Value RPCHandler::doRippleLinesGet(Json::Value jvRequest) { - // uint256 uAccepted = mNetOps->getClosedLedgerHash(); + std::string strIdent = jvRequest["account"].asString(); + bool bIndex = jvRequest.isMember("index"); + int iIndex = bIndex ? jvRequest["index"].asUInt() : 0; - std::string strIdent = params[0u].asString(); - bool bIndex; - int iIndex = 2 == params.size() ? lexical_cast_s(params[1u].asString()) : 0; + RippleAddress raAccount; - RippleAddress naAccount; + Json::Value jvResult; - Json::Value ret; + jvResult = accountFromString(uint256(0), raAccount, bIndex, strIdent, iIndex); - ret = accountFromString(uint256(0), naAccount, bIndex, strIdent, iIndex); - - if (!ret.empty()) - return ret; + if (!jvResult.empty()) + return jvResult; // Get info on account. - ret = Json::Value(Json::objectValue); - ret["account"] = naAccount.humanAccountID(); + jvResult["account"] = raAccount.humanAccountID(); if (bIndex) - ret["index"] = iIndex; + jvResult["index"] = iIndex; - AccountState::pointer as = mNetOps->getAccountState(uint256(0), naAccount); + AccountState::pointer as = mNetOps->getAccountState(uint256(0), raAccount); if (as) { Json::Value jsonLines(Json::arrayValue); - ret["account"] = naAccount.humanAccountID(); + jvResult["account"] = raAccount.humanAccountID(); // XXX This is wrong, we do access the current ledger and do need to worry about changes. // We access a committed ledger and need not worry about changes. - RippleLines rippleLines(naAccount.getAccountID()); + RippleLines rippleLines(raAccount.getAccountID()); BOOST_FOREACH(RippleState::pointer line, rippleLines.getLines()) { STAmount saBalance = line->getBalance(); @@ -607,14 +607,14 @@ Json::Value RPCHandler::doRippleLinesGet(Json::Value params) jsonLines.append(jPeer); } - ret["lines"] = jsonLines; + jvResult["lines"] = jsonLines; } else { - ret = rpcError(rpcACT_NOT_FOUND); + jvResult = rpcError(rpcACT_NOT_FOUND); } - return ret; + return jvResult; } // TODO: diff --git a/src/js/remote.js b/src/js/remote.js index 0912f40dfc..836f08b9a7 100644 --- a/src/js/remote.js +++ b/src/js/remote.js @@ -698,14 +698,16 @@ Remote.prototype.request_transaction_entry = function (hash) { .tx_hash(hash); }; -Remote.prototype.request_ripple_lines_get = function (accountID) { +Remote.prototype.request_ripple_lines_get = function (accountID, index) { // XXX Does this require the server to be trusted? //assert(this.trusted); var request = new Request(this, 'ripple_lines_get'); - // XXX Convert API call to JSON - request.message.params = [accountID]; + request.message.account = accountID; + + if (index) + request.message.index = index; return request; }; @@ -1001,10 +1003,13 @@ Remote.prototype.request_unl_list = function () { return new Request(this, 'unl_list'); }; -Remote.prototype.request_unl_add = function (addr, note) { +Remote.prototype.request_unl_add = function (addr, comment) { var request = new Request(this, 'unl_add'); - request.message.params = [addr, note]; + request.message.node = addr; + + if (comment !== undefined) + request.message.comment = note; return request; };