Work toward RPC account_lines.

This commit is contained in:
Arthur Britto
2012-05-27 17:44:26 -07:00
parent af5c25a24d
commit d5e918e85b
5 changed files with 227 additions and 104 deletions

View File

@@ -58,6 +58,12 @@ public:
// account operations
AccountState::pointer getAccountState(const NewcoinAddress& accountID);
//
// Ripple functions
//
bool getDirLineInfo(const NewcoinAddress& naAccount, uint64& uDirLineNodeFirst, uint64& uDirLineNodeLast) { return false; }
std::vector<uint256> getDirLineNode(const uint64 uDirLineNode) { std::vector<uint256> empty; return empty; }
// raw object operations
bool findRawLedger(const uint256& ledgerHash, std::vector<unsigned char>& rawLedger);
bool findRawTransaction(const uint256& transactionHash, std::vector<unsigned char>& rawTransaction);

View File

@@ -1,4 +1,5 @@
#include "RPCDoor.h"
#include "Application.h"
#include "Config.h"
#include <boost/bind.hpp>
//#include <boost/log/trivial.hpp>
@@ -16,7 +17,7 @@ RPCDoor::RPCDoor(boost::asio::io_service& io_service) :
void RPCDoor::startListening()
{
RPCServer::pointer new_connection = RPCServer::create(mAcceptor.get_io_service());
RPCServer::pointer new_connection = RPCServer::create(mAcceptor.get_io_service(), &theApp->getOPs());
mAcceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
mAcceptor.async_accept(new_connection->getSocket(),

View File

@@ -29,8 +29,8 @@
Just read from wire until the entire request is in.
*/
RPCServer::RPCServer(boost::asio::io_service& io_service)
: mSocket(io_service)
RPCServer::RPCServer(boost::asio::io_service& io_service , NetworkOPs* nopNetwork)
: mNetOps(nopNetwork), mSocket(io_service)
{
}
@@ -173,7 +173,7 @@ bool RPCServer::extractString(std::string& param, const Json::Value& params, int
// Given a seed and a source account get the public and private key for authorizing transactions for the account.
Json::Value RPCServer::authorize(const NewcoinAddress& naSeed, const NewcoinAddress& naSrcAccountID,
NewcoinAddress& naAccountPublic, NewcoinAddress& naAccountPrivate,
SerializedLedgerEntry::pointer& sleSrc)
SLE::pointer& sleSrc)
{
Ledger::pointer ledger = theApp->getMasterLedger().getCurrentLedger();
LedgerStateParms qry = lepNONE;
@@ -198,7 +198,7 @@ Json::Value RPCServer::authorize(const NewcoinAddress& naSeed, const NewcoinAddr
na0Private.setAccountPrivate(naGenerator, naSeed, 0);
qry = lepNONE;
SerializedLedgerEntry::pointer sleGen = ledger->getGenerator(qry, na0Public.getAccountID());
SLE::pointer sleGen = ledger->getGenerator(qry, na0Public.getAccountID());
if (!sleGen)
{
@@ -246,30 +246,15 @@ Json::Value RPCServer::authorize(const NewcoinAddress& naSeed, const NewcoinAddr
return Json::Value(Json::objectValue);
}
// account_info <account>|<nickname>|<account_public_key>
// account_info <seed>|<pass_phrase>|<key> [<index>]
Json::Value RPCServer::doAccountInfo(Json::Value &params)
// <-- bIndex: true if iIndex > 0 and used the index.
Json::Value RPCServer::accountFromString(NewcoinAddress& naAccount, bool& bIndex, const std::string& strIdent, const int iIndex)
{
if (params.size() < 1 || params.size() > 2)
{
return "invalid params";
}
else if (!theApp->getOPs().available()) {
return JSONRPCError(503, "network not available");
}
else
{
std::string strIdent = params[0u].asString();
bool bIndex = 2 == params.size();
int iIndex = bIndex ? boost::lexical_cast<int>(params[1u].asString()) : 0;
NewcoinAddress naAccount;
NewcoinAddress naSeed;
if (!bIndex && (naAccount.setAccountPublic(strIdent) || naAccount.setAccountID(strIdent)))
if (naAccount.setAccountPublic(strIdent) || naAccount.setAccountID(strIdent))
{
// Got the account.
nothing();
bIndex = false;
}
// Must be a seed.
else if (!naSeed.setFamilySeedGeneric(strIdent))
@@ -293,7 +278,7 @@ Json::Value RPCServer::doAccountInfo(Json::Value &params)
Ledger::pointer ledger = theApp->getMasterLedger().getCurrentLedger();
LedgerStateParms qry = lepNONE;
SerializedLedgerEntry::pointer sleGen = ledger->getGenerator(qry, naRegular0Public.getAccountID());
SLE::pointer sleGen = ledger->getGenerator(qry, naRegular0Public.getAccountID());
if (!sleGen)
{
// Didn't find a generator map, assume it is a master generator.
@@ -312,15 +297,43 @@ Json::Value RPCServer::doAccountInfo(Json::Value &params)
naGenerator.setFamilyGenerator(vucMasterGenerator);
}
bIndex = true;
bIndex = !iIndex;
naAccount.setAccountPublic(naGenerator, iIndex);
}
// Get info on account.
Json::Value ret(Json::objectValue);
return Json::Value(Json::objectValue);
}
AccountState::pointer as=theApp->getMasterLedger().getCurrentLedger()->getAccountState(naAccount);
// account_info <account>|<nickname>|<account_public_key>
// account_info <seed>|<pass_phrase>|<key> [<index>]
Json::Value RPCServer::doAccountInfo(Json::Value &params)
{
if (params.size() < 1 || params.size() > 2)
{
return "invalid params";
}
else if (!mNetOps->available()) {
return JSONRPCError(503, "network not available");
}
else
{
std::string strIdent = params[0u].asString();
bool bIndex;
int iIndex = 2 == params.size()? boost::lexical_cast<int>(params[1u].asString()) : 0;
NewcoinAddress naAccount;
Json::Value ret;
ret = accountFromString(naAccount, bIndex, strIdent, iIndex);
if (!ret.empty())
return ret;
// Get info on account.
ret = Json::Value(Json::objectValue);
AccountState::pointer as=mNetOps->getAccountState(naAccount);
if (as)
{
as->addJson(ret);
@@ -329,7 +342,6 @@ Json::Value RPCServer::doAccountInfo(Json::Value &params)
{
ret["account"] = naAccount.humanAccountID();
ret["status"] = "NotFound";
ret["bIndex"] = bIndex;
if (bIndex)
ret["index"] = iIndex;
}
@@ -338,6 +350,100 @@ Json::Value RPCServer::doAccountInfo(Json::Value &params)
}
}
// account_lines <account>|<nickname>|<account_public_key> [<index>]
Json::Value RPCServer::doAccountLines(Json::Value &params)
{
if (params.size() < 1 || params.size() > 2)
{
return "invalid params";
}
else if (!mNetOps->available()) {
return JSONRPCError(503, "network not available");
}
else
{
std::string strIdent = params[0u].asString();
bool bIndex;
int iIndex = 2 == params.size()? boost::lexical_cast<int>(params[1u].asString()) : 0;
NewcoinAddress naAccount;
Json::Value ret;
ret = accountFromString(naAccount, bIndex, strIdent, iIndex);
if (!ret.empty())
return ret;
// SHAMap::pointer smAccounts = theApp->getMasterLedger().getCurrentLedger()->peekAccountStateMap();
// Get info on account.
ret = Json::Value(Json::objectValue);
ret["account"] = naAccount.humanAccountID();
if (bIndex)
ret["index"] = iIndex;
AccountState::pointer as = mNetOps->getAccountState(naAccount);
if (as)
{
ret["account"] = naAccount.humanAccountID();
// We access a committed ledger and need not worry about changes.
uint64 uDirLineNodeFirst;
uint64 uDirLineNodeLast;
if (!mNetOps->getDirLineInfo(naAccount, uDirLineNodeFirst, uDirLineNodeLast))
{
ret["lines"] = Json::Value(Json::objectValue);
}
else if (uDirLineNodeFirst)
{
Json::Value jsonLines = Json::Value(Json::objectValue);
for (; uDirLineNodeFirst <= uDirLineNodeLast; uDirLineNodeFirst++)
{
std::vector<uint256> vuRippleNodes = mNetOps->getDirLineNode(uDirLineNodeFirst);
BOOST_FOREACH(uint256& uNode, vuRippleNodes)
{
NewcoinAddress naAccountPeer;
uint64 uBalance = 0;
// uint160 uCurrency;
STAmount saLimit;
STAmount saLimitPeer;
#if 0
RippleState::pointer rsLine = mNetOps->getRippleState(uNode);
rsLine->setViewAccount(naAccount);
rsLine->getAccountPeer(naAccountPeer);
// ->getBalance(iBalance);
// ->getCurrency(uCurrency);
rsLine->getLimit(saLimit)
rsLine->getLimitPeer(saPeerLimit);
#endif
Json::Value jPeer = Json::Value(Json::objectValue);
jPeer["balance"] = Json::Value::UInt(uBalance); // XXX Raw number.
jPeer["limit"] = Json::Value::UInt(saLimit.getValue());
jPeer["limit_peer"] = Json::Value::UInt(saLimitPeer.getValue());
ret[naAccountPeer.humanAccountID()] = jPeer;
}
}
ret["lines"] = jsonLines;
}
}
else
{
ret["status"] = "NotFound";
}
return ret;
}
}
Json::Value RPCServer::doConnect(Json::Value& params)
{
// connect <ip> [port]
@@ -403,14 +509,14 @@ Json::Value RPCServer::doCreditSet(Json::Value& params)
{
return JSONRPCError(500, "bad src amount/currency");
}
else if (!theApp->getOPs().available()) {
else if (!mNetOps->available()) {
return JSONRPCError(503, "network not available");
}
else
{
NewcoinAddress naAccountPublic;
NewcoinAddress naAccountPrivate;
SerializedLedgerEntry::pointer sleSrc;
SLE::pointer sleSrc;
Json::Value obj = authorize(naSeed, naSrcAccountID, naAccountPublic, naAccountPrivate, sleSrc);
if (!obj.empty())
@@ -434,7 +540,7 @@ Json::Value RPCServer::doCreditSet(Json::Value& params)
saLimitAmount,
uAcceptRate);
(void) theApp->getOPs().processTransaction(trans);
(void) mNetOps->processTransaction(trans);
obj["transaction"] = trans->getSTransaction()->getJson(0);
obj["status"] = trans->getStatus();
@@ -490,14 +596,14 @@ Json::Value RPCServer::doSend(Json::Value& params)
{
return JSONRPCError(500, "bad src amount/currency");
}
else if (!theApp->getOPs().available()) {
else if (!mNetOps->available()) {
return JSONRPCError(503, "network not available");
}
else
{
NewcoinAddress naAccountPublic;
NewcoinAddress naAccountPrivate;
SerializedLedgerEntry::pointer sleSrc;
SLE::pointer sleSrc;
Json::Value obj = authorize(naSeed, naSrcAccountID, naAccountPublic, naAccountPrivate, sleSrc);
if (!obj.empty())
@@ -530,7 +636,7 @@ Json::Value RPCServer::doSend(Json::Value& params)
saSrcAmount,
spPaths);
(void) theApp->getOPs().processTransaction(trans);
(void) mNetOps->processTransaction(trans);
obj["transaction"] = trans->getSTransaction()->getJson(0);
obj["status"] = trans->getStatus();
@@ -580,14 +686,14 @@ Json::Value RPCServer::doTransitSet(Json::Value& params)
{
return JSONRPCError(500, "source account id needed");
}
else if (!theApp->getOPs().available()) {
else if (!mNetOps->available()) {
return JSONRPCError(503, "network not available");
}
else
{
NewcoinAddress naAccountPublic;
NewcoinAddress naAccountPrivate;
SerializedLedgerEntry::pointer sleSrc;
SLE::pointer sleSrc;
Json::Value obj = authorize(naSeed, naSrcAccountID, naAccountPublic, naAccountPrivate, sleSrc);
if (!obj.empty())
@@ -617,7 +723,7 @@ Json::Value RPCServer::doTransitSet(Json::Value& params)
uTransitStart,
uTransitExpire);
(void) theApp->getOPs().processTransaction(trans);
(void) mNetOps->processTransaction(trans);
obj["transaction"] = trans->getSTransaction()->getJson(0);
obj["status"] = trans->getStatus();
@@ -843,7 +949,7 @@ Json::Value RPCServer::doWalletClaim(Json::Value& params)
naRegular0Public.getAccountPublic(),
vucGeneratorSig);
(void) theApp->getOPs().processTransaction(trns);
(void) mNetOps->processTransaction(trns);
Json::Value obj(Json::objectValue);
@@ -891,11 +997,11 @@ Json::Value RPCServer::doWalletCreate(Json::Value& params)
{
return "create account id needed";
}
else if (!theApp->getOPs().available()) {
else if (!mNetOps->available()) {
// We require access to the paying account's sequence number and key information.
return JSONRPCError(503, "network not available");
}
else if (theApp->getMasterLedger().getCurrentLedger()->getAccountState(naDstAccountID))
else if (mNetOps->getAccountState(naDstAccountID))
{
return "account already exists";
}
@@ -906,7 +1012,7 @@ Json::Value RPCServer::doWalletCreate(Json::Value& params)
NewcoinAddress naAccountPublic;
NewcoinAddress naAccountPrivate;
SerializedLedgerEntry::pointer sleSrc;
SLE::pointer sleSrc;
Json::Value obj = authorize(naSeed, naSrcAccountID, naAccountPublic, naAccountPrivate, sleSrc);
STAmount saSrcBalance = sleSrc->getIValueFieldAmount(sfBalance);
@@ -931,7 +1037,7 @@ Json::Value RPCServer::doWalletCreate(Json::Value& params)
naDstAccountID,
saInitialFunds); // Initial funds in XNC.
(void) theApp->getOPs().processTransaction(trans);
(void) mNetOps->processTransaction(trans);
obj["transaction"] = trans->getSTransaction()->getJson(0);
obj["status"] = trans->getStatus();
@@ -1139,6 +1245,7 @@ Json::Value RPCServer::doCommand(const std::string& command, Json::Value& params
std::cerr << "RPC:" << command << std::endl;
if (command == "account_info") return doAccountInfo(params);
if (command == "account_lines") return doAccountLines(params);
if (command == "connect") return doConnect(params);
if (command == "credit_set") return doCreditSet(params);
if (command == "peers") return doPeers(params);

View File

@@ -9,9 +9,13 @@
#include "RequestParser.h"
#include "NewcoinAddress.h"
#include "SerializedLedger.h"
#include "NetworkOPs.h"
class RPCServer : public boost::enable_shared_from_this<RPCServer>
{
private:
NetworkOPs* mNetOps;
boost::asio::ip::tcp::socket mSocket;
boost::array<char, 8192> mReadBuffer;
std::string mReplyStr;
@@ -19,7 +23,7 @@ class RPCServer : public boost::enable_shared_from_this<RPCServer>
HttpRequest mIncomingRequest;
HttpRequestParser mRequestParser;
RPCServer(boost::asio::io_service& io_service);
RPCServer(boost::asio::io_service& io_service, NetworkOPs* nopNetwork);
void handle_write(const boost::system::error_code& error);
@@ -36,7 +40,10 @@ class RPCServer : public boost::enable_shared_from_this<RPCServer>
NewcoinAddress& naAccountPublic, NewcoinAddress& naAccountPrivate,
SerializedLedgerEntry::pointer& sleSrc);
Json::Value accountFromString(NewcoinAddress& naAccount, bool& bIndex, const std::string& strIdent, const int iIndex);
Json::Value doAccountInfo(Json::Value& params);
Json::Value doAccountLines(Json::Value &params);
Json::Value doConnect(Json::Value& params);
Json::Value doCreditSet(Json::Value& params);
Json::Value doLedger(Json::Value& params);
@@ -73,9 +80,9 @@ class RPCServer : public boost::enable_shared_from_this<RPCServer>
public:
typedef boost::shared_ptr<RPCServer> pointer;
static pointer create(boost::asio::io_service& io_service)
static pointer create(boost::asio::io_service& io_service, NetworkOPs* mNetOps)
{
return pointer(new RPCServer(io_service));
return pointer(new RPCServer(io_service, mNetOps));
}
boost::asio::ip::tcp::socket& getSocket()
@@ -85,3 +92,4 @@ public:
void connected();
};
// vim:ts=4

View File

@@ -39,6 +39,7 @@ void printHelp(const po::options_description& desc)
cout << "Commands: " << endl;
cout << " account_info <account>|<nickname>" << endl;
cout << " account_info <seed>|<pass_phrase>|<key> [<index>]" << endl;
cout << " account_lines <account>|<nickname>" << endl;
cout << " connect <ip> [<port>]" << endl;
cout << " credit_set <seed> <paying_account> <destination_account> <limit_amount> <currency> [<account_rate>]" << endl;
cout << " ledger" << endl;