From 6112a443cf3bb53900f8ff2d3bc18b71b21c9bb5 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 23 Jan 2012 13:31:39 -0800 Subject: [PATCH] Add "ledger" command to get ledger information. Make the command work with no parameters. Fix a bug where the ledger's account/txn hashes didn't get properly synchronized to the SHA maps. Add function to get ledger as json object. --- Ledger.cpp | 27 ++++++++++++++++++++++++++- Ledger.h | 4 ++++ RPCServer.cpp | 23 ++++++++++++++++++++++- RPCServer.h | 1 + 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Ledger.cpp b/Ledger.cpp index ecd7b124d8..f1eba373ab 100644 --- a/Ledger.cpp +++ b/Ledger.cpp @@ -44,6 +44,11 @@ Ledger::Ledger(Ledger &prevLedger, uint64 ts) : mTimeStamp(ts), void Ledger::updateHash() { + if(mTransactionMap) mTransHash=mTransactionMap->getHash(); + else mTransHash=0; + if(mAccountStateMap) mAccountHash=mAccountStateMap->getHash(); + else mAccountHash=0; + Serializer s(116); addRaw(s); mHash=s.getSHA512Half(); @@ -265,7 +270,8 @@ Ledger::TransResult Ledger::hasTransaction(Transaction::pointer trans) Ledger::pointer Ledger::closeLedger(uint64 timeStamp) { // close this ledger, return a pointer to the next ledger - // CAUTION: New ledger needs its SHAMap's connected to storage + // CAUTION: New ledger needs its SHAMap's connected to storage + updateHash(); setClosed(); return Ledger::pointer(new Ledger(*this, timeStamp)); } @@ -408,3 +414,22 @@ Ledger::pointer Ledger::loadByHash(const uint256& ledgerHash) sql.append("';"); return getSQL(sql); } + +void Ledger::addJson(Json::Value& ret) +{ + Json::Value ledger(Json::objectValue); + + boost::recursive_mutex::scoped_lock sl(mLock); + ledger["ParentHash"]=mParentHash.GetHex(); + + if(mClosed) + { + ledger["Hash"]=mHash.GetHex(); + ledger["TransactionHash"]=mTransHash.GetHex(); + ledger["AccountHash"]=mAccountHash.GetHex(); + ledger["Closed"]=true; + ledger["Accepted"]=mAccepted; + } + else ledger["Closed"]=false; + ret[boost::lexical_cast(mLedgerSeq)]=ledger; +} diff --git a/Ledger.h b/Ledger.h index 56542a766c..955fef9c9e 100644 --- a/Ledger.h +++ b/Ledger.h @@ -7,6 +7,8 @@ #include #include +#include "json/value.h" + #include "Transaction.h" #include "types.h" #include "BitcoinUtil.h" @@ -104,6 +106,8 @@ public: bool isCompatible(boost::shared_ptr other); bool signLedger(std::vector &signature, const LocalHanko &hanko); + void addJson(Json::Value&); + static bool unitTest(); }; diff --git a/RPCServer.cpp b/RPCServer.cpp index 0f4c6288b5..c519fd7c77 100644 --- a/RPCServer.cpp +++ b/RPCServer.cpp @@ -430,6 +430,25 @@ Json::Value RPCServer::doTx(Json::Value& params) return "not implemented"; } +Json::Value RPCServer::doLedger(Json::Value& params) +{ + // ledger + // ledger + // ledger + + int paramCount=getParamCount(params); + + if(paramCount==0); + { + Json::Value ret(Json::objectValue); + theApp->getMasterLedger().getCurrentLedger()->addJson(ret); + theApp->getMasterLedger().getClosingLedger()->addJson(ret); + return ret; + } + + return "not implemented"; +} + Json::Value RPCServer::doCommand(const std::string& command, Json::Value& params) { std::cerr << "RPC:" << command << std::endl; @@ -447,7 +466,8 @@ Json::Value RPCServer::doCommand(const std::string& command, Json::Value& params humanToPK(params[1u].asString(),pubKey); theApp->getUNL().addNode(hanko,pubKey); return "adding node"; - }else return "invalid params"; + } + else return "invalid params"; } if(command=="getUNL") { @@ -464,6 +484,7 @@ Json::Value RPCServer::doCommand(const std::string& command, Json::Value& params if(command=="sendto") return doSendTo(params); if(command=="connect") return doConnect(params); if(command=="tx") return doTx(params); + if(command=="ledger") return doLedger(params); return "unknown command"; } diff --git a/RPCServer.h b/RPCServer.h index 658414d941..fa65b6dccd 100644 --- a/RPCServer.h +++ b/RPCServer.h @@ -42,6 +42,7 @@ class RPCServer : public boost::enable_shared_from_this Json::Value doSendTo(Json::Value& params); Json::Value doConnect(Json::Value& params); Json::Value doTx(Json::Value& params); + Json::Value doLedger(Json::Value& params); // parses a string account name into a uint160 // can be local or remote