Infrastructure for "tx" RPC command to track transactions.

This commit is contained in:
JoelKatz
2012-01-20 16:43:53 -08:00
parent 50e23112e3
commit ba6d040266
6 changed files with 72 additions and 2 deletions

View File

@@ -1,4 +1,6 @@
#include <boost/lexical_cast.hpp>
#include "json/writer.h"
#include "LocalTransaction.h"
@@ -36,3 +38,19 @@ void LocalTransaction::performTransaction()
{
mTransaction=theApp->getOPs().processTransaction(mTransaction);
}
Json::Value LocalTransaction::getJson() const
{
if(!mTransaction)
{ // has no corresponding transaction
Json::Value ret(Json::objectValue);
ret["Status"]="unfunded";
ret["Amount"]=boost::lexical_cast<std::string>(mAmount);
Json::Value destination(Json::objectValue);
destination["AccountID"]=NewcoinAddress(mDestAcctID).GetString();
ret["Destination"]=destination;
return ret;
}
return mTransaction->getJson(true, isPaid(), isCredited());
}

View File

@@ -7,6 +7,8 @@
#include <boost/shared_ptr.hpp>
#include "json/value.h"
#include "uint256.h"
#include "Transaction.h"
@@ -49,6 +51,8 @@ public:
void performTransaction(); // perform this transaction as if we received it from the network
bool makeTransaction(); // create a transaction object according to these rules
Json::Value getJson() const;
};
#endif

View File

@@ -383,6 +383,31 @@ Json::Value RPCServer::doSendTo(Json::Value& params)
return lt->getTransaction()->getJson(true);
}
Json::Value RPCServer::doTx(Json::Value& params)
{
// tx
// tx <txID>
// tx <family> <seq>
// tx <account>
std::string param1, param2;
if(!extractString(param1, params, 0))
{ // all local transactions
}
if(Transaction::isHexTxID(param1))
{ // transaction by ID
}
if(extractString(param2, params, 1))
{ // family seq
}
// account
return "not implemented";
}
Json::Value RPCServer::doCommand(const std::string& command, Json::Value& params)
{
std::cerr << "RPC:" << command << std::endl;
@@ -416,6 +441,7 @@ Json::Value RPCServer::doCommand(const std::string& command, Json::Value& params
if(command=="unlock") return doUnlock(params);
if(command=="sendto") return doSendTo(params);
if(command=="connect") return doConnect(params);
if(command=="tx") return doTx(params);
return "unknown command";
}

View File

@@ -41,6 +41,7 @@ class RPCServer : public boost::enable_shared_from_this<RPCServer>
Json::Value doUnlock(Json::Value& params);
Json::Value doSendTo(Json::Value& params);
Json::Value doConnect(Json::Value& params);
Json::Value doTx(Json::Value& params);
// parses a string account name into a uint160
// can be local or remote

View File

@@ -318,7 +318,23 @@ bool Transaction::convertToTransactions(uint32 firstLedgerSeq, uint32 secondLedg
return ret;
}
Json::Value Transaction::getJson(bool decorate) const
static bool isHex(char j)
{
if((j>='0') && (j<='9')) return true;
if((j>='A') && (j<='F')) return true;
if((j>='a') && (j<='f')) return true;
return false;
}
bool Transaction::isHexTxID(const std::string& txid)
{
if(txid.size()!=64) return false;
for(int i=0; i<64; i++)
if(!isHex(txid[i])) return false;
return true;
}
Json::Value Transaction::getJson(bool decorate, bool paid, bool credited) const
{
Json::Value ret(Json::objectValue);
ret["TransactionID"]=mTransactionID.GetHex();
@@ -356,7 +372,10 @@ Json::Value Transaction::getJson(bool decorate) const
lac=theApp->getWallet().getLocalAccount(mAccountTo);
if(!!lac) destination=lac->getJson();
}
if(paid) source["Paid"]=true;
if(credited) destination["Credited"]=true;
ret["Source"]=source;
ret["Destination"]=destination;
return ret;
}

View File

@@ -99,7 +99,9 @@ public:
bool operator<=(const Transaction&) const;
bool operator>=(const Transaction&) const;
Json::Value getJson(bool decorate) const;
Json::Value getJson(bool decorate, bool paid_local=false, bool credited_local=false) const;
static bool isHexTxID(const std::string&);
protected:
static Transaction::pointer transactionFromSQL(const std::string& statement);