Files
xahaud/src/cpp/ripple/Transaction.cpp
JoelKatz 923446fb78 Fix 'tx' output format. Begin supporting a binary output format.
This adds support for binary in 'tx' and 'account_tx' commands.
https://ripple.com/wiki/FormatChange
2013-02-25 12:51:06 -08:00

357 lines
9.3 KiB
C++

#include <cassert>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/make_shared.hpp>
#include <boost/ref.hpp>
#include "Application.h"
#include "Transaction.h"
#include "Wallet.h"
#include "BitcoinUtil.h"
#include "Serializer.h"
#include "SerializedTransaction.h"
#include "Log.h"
DECLARE_INSTANCE(Transaction);
Transaction::Transaction(SerializedTransaction::ref sit, bool bValidate)
: mInLedger(0), mStatus(INVALID), mResult(temUNCERTAIN), mTransaction(sit)
{
try
{
mFromPubKey.setAccountPublic(mTransaction->getSigningPubKey());
mTransactionID = mTransaction->getTransactionID();
mAccountFrom = mTransaction->getSourceAccount();
}
catch(...)
{
return;
}
if (!bValidate || checkSign())
mStatus = NEW;
}
Transaction::pointer Transaction::sharedTransaction(const std::vector<unsigned char>&vucTransaction, bool bValidate)
{
try
{
Serializer s(vucTransaction);
SerializerIterator sit(s);
SerializedTransaction::pointer st = boost::make_shared<SerializedTransaction>(boost::ref(sit));
return boost::make_shared<Transaction>(st, bValidate);
}
catch (...)
{
Log(lsWARNING) << "Exception constructing transaction";
return boost::shared_ptr<Transaction>();
}
}
//
// Generic transaction construction
//
Transaction::Transaction(
TransactionType ttKind,
const RippleAddress& naPublicKey,
const RippleAddress& naSourceAccount,
uint32 uSeq,
const STAmount& saFee,
uint32 uSourceTag) :
mAccountFrom(naSourceAccount), mFromPubKey(naPublicKey), mStatus(NEW), mResult(temUNCERTAIN)
{
assert(mFromPubKey.isValid());
mTransaction = boost::make_shared<SerializedTransaction>(ttKind);
// Log(lsINFO) << str(boost::format("Transaction: account: %s") % naSourceAccount.humanAccountID());
// Log(lsINFO) << str(boost::format("Transaction: mAccountFrom: %s") % mAccountFrom.humanAccountID());
mTransaction->setSigningPubKey(mFromPubKey);
mTransaction->setSourceAccount(mAccountFrom);
mTransaction->setSequence(uSeq);
mTransaction->setTransactionFee(saFee);
if (uSourceTag)
{
mTransaction->makeFieldPresent(sfSourceTag);
mTransaction->setFieldU32(sfSourceTag, uSourceTag);
}
}
bool Transaction::sign(const RippleAddress& naAccountPrivate)
{
bool bResult = true;
if (!naAccountPrivate.isValid())
{
Log(lsWARNING) << "No private key for signing";
bResult = false;
}
getSTransaction()->sign(naAccountPrivate);
if (bResult)
{
updateID();
}
else
{
mStatus = INCOMPLETE;
}
return bResult;
}
//
// Misc.
//
bool Transaction::checkSign() const
{
assert(mFromPubKey.isValid());
return mTransaction->checkSign(mFromPubKey);
}
void Transaction::setStatus(TransStatus ts, uint32 lseq)
{
mStatus = ts;
mInLedger = lseq;
}
void Transaction::save()
{
if ((mStatus == INVALID) || (mStatus == REMOVED))
return;
char status;
switch (mStatus)
{
case NEW: status = TXN_SQL_NEW; break;
case INCLUDED: status = TXN_SQL_INCLUDED; break;
case CONFLICTED: status = TXN_SQL_CONFLICT; break;
case COMMITTED: status = TXN_SQL_VALIDATED; break;
case HELD: status = TXN_SQL_HELD; break;
default: status = TXN_SQL_UNKNOWN;
}
Database *db = theApp->getTxnDB()->getDB();
ScopedLock dbLock(theApp->getTxnDB()->getDBLock());
db->executeSQL(mTransaction->getSQLInsertReplaceHeader() + mTransaction->getSQL(getLedger(), status) + ";");
}
Transaction::pointer Transaction::transactionFromSQL(Database* db, bool bValidate)
{
Serializer rawTxn;
std::string status;
uint32 inLedger;
int txSize = 2048;
rawTxn.resize(txSize);
db->getStr("Status", status);
inLedger = db->getInt("LedgerSeq");
txSize = db->getBinary("RawTxn", &*rawTxn.begin(), rawTxn.getLength());
if (txSize > rawTxn.getLength())
{
rawTxn.resize(txSize);
db->getBinary("RawTxn", &*rawTxn.begin(), rawTxn.getLength());
}
rawTxn.resize(txSize);
SerializerIterator it(rawTxn);
SerializedTransaction::pointer txn = boost::make_shared<SerializedTransaction>(boost::ref(it));
Transaction::pointer tr = boost::make_shared<Transaction>(txn, bValidate);
TransStatus st(INVALID);
switch (status[0])
{
case TXN_SQL_NEW: st = NEW; break;
case TXN_SQL_CONFLICT: st = CONFLICTED; break;
case TXN_SQL_HELD: st = HELD; break;
case TXN_SQL_VALIDATED: st = COMMITTED; break;
case TXN_SQL_INCLUDED: st = INCLUDED; break;
case TXN_SQL_UNKNOWN: break;
default: assert(false);
}
tr->setStatus(st);
tr->setLedger(inLedger);
return tr;
}
// DAVID: would you rather duplicate this code or keep the lock longer?
Transaction::pointer Transaction::transactionFromSQL(const std::string& sql)
{
Serializer rawTxn;
std::string status;
uint32 inLedger;
int txSize = 2048;
rawTxn.resize(txSize);
{
ScopedLock sl(theApp->getTxnDB()->getDBLock());
Database* db = theApp->getTxnDB()->getDB();
if (!db->executeSQL(sql, true) || !db->startIterRows())
return Transaction::pointer();
db->getStr("Status", status);
inLedger = db->getInt("LedgerSeq");
txSize = db->getBinary("RawTxn", &*rawTxn.begin(), rawTxn.getLength());
if (txSize > rawTxn.getLength())
{
rawTxn.resize(txSize);
db->getBinary("RawTxn", &*rawTxn.begin(), rawTxn.getLength());
}
db->endIterRows();
}
rawTxn.resize(txSize);
SerializerIterator it(rawTxn);
SerializedTransaction::pointer txn = boost::make_shared<SerializedTransaction>(boost::ref(it));
Transaction::pointer tr = boost::make_shared<Transaction>(txn, true);
TransStatus st(INVALID);
switch (status[0])
{
case TXN_SQL_NEW: st = NEW; break;
case TXN_SQL_CONFLICT: st = CONFLICTED; break;
case TXN_SQL_HELD: st = HELD; break;
case TXN_SQL_VALIDATED: st = COMMITTED; break;
case TXN_SQL_INCLUDED: st = INCLUDED; break;
case TXN_SQL_UNKNOWN: break;
default: assert(false);
}
tr->setStatus(st);
tr->setLedger(inLedger);
return tr;
}
Transaction::pointer Transaction::load(const uint256& id)
{
std::string sql = "SELECT LedgerSeq,Status,RawTxn FROM Transactions WHERE TransID='";
sql.append(id.GetHex());
sql.append("';");
return transactionFromSQL(sql);
}
Transaction::pointer Transaction::findFrom(const RippleAddress& fromID, uint32 seq)
{
std::string sql = "SELECT LedgerSeq,Status,RawTxn FROM Transactions WHERE FromID='";
sql.append(fromID.humanAccountID());
sql.append("' AND FromSeq='");
sql.append(boost::lexical_cast<std::string>(seq));
sql.append("';");
return transactionFromSQL(sql);
}
bool Transaction::convertToTransactions(uint32 firstLedgerSeq, uint32 secondLedgerSeq,
bool checkFirstTransactions, bool checkSecondTransactions, const SHAMap::SHAMapDiff& inMap,
std::map<uint256, std::pair<Transaction::pointer, Transaction::pointer> >& outMap)
{ // convert a straight SHAMap payload difference to a transaction difference table
// return value: true=ledgers are valid, false=a ledger is invalid
SHAMap::SHAMapDiff::const_iterator it;
for(it = inMap.begin(); it != inMap.end(); ++it)
{
const uint256& id = it->first;
SHAMapItem::ref first = it->second.first;
SHAMapItem::ref second = it->second.second;
Transaction::pointer firstTrans, secondTrans;
if (!!first)
{ // transaction in our table
firstTrans = sharedTransaction(first->getData(), checkFirstTransactions);
if ((firstTrans->getStatus() == INVALID) || (firstTrans->getID() != id ))
{
firstTrans->setStatus(INVALID, firstLedgerSeq);
return false;
}
else firstTrans->setStatus(INCLUDED, firstLedgerSeq);
}
if (!!second)
{ // transaction in other table
secondTrans = sharedTransaction(second->getData(), checkSecondTransactions);
if ((secondTrans->getStatus() == INVALID) || (secondTrans->getID() != id))
{
secondTrans->setStatus(INVALID, secondLedgerSeq);
return false;
}
else secondTrans->setStatus(INCLUDED, secondLedgerSeq);
}
assert(firstTrans || secondTrans);
if(firstTrans && secondTrans && (firstTrans->getStatus() != INVALID) && (secondTrans->getStatus() != INVALID))
return false; // one or the other SHAMap is structurally invalid or a miracle has happened
outMap[id] = std::pair<Transaction::pointer, Transaction::pointer>(firstTrans, secondTrans);
}
return true;
}
// options 1 to include the date of the transaction
Json::Value Transaction::getJson(int options, bool binary) const
{
Json::Value ret(mTransaction->getJson(0, binary));
if (mInLedger)
{
ret["inLedger"] = mInLedger;
if(options == 1)
{
Ledger::pointer ledger=theApp->getLedgerMaster().getLedgerBySeq(mInLedger);
if(ledger)
ret["date"] = ledger->getCloseTimeNC();
}
}
switch (mStatus)
{
case NEW: ret["status"] = "new"; break;
case INVALID: ret["status"] = "invalid"; break;
case INCLUDED: ret["status"] = "included"; break;
case CONFLICTED: ret["status"] = "conflicted"; break;
case COMMITTED: ret["status"] = "committed"; break;
case HELD: ret["status"] = "held"; break;
case REMOVED: ret["status"] = "removed"; break;
case OBSOLETE: ret["status"] = "obsolete"; break;
case INCOMPLETE: ret["status"] = "incomplete"; break;
default: ret["status"] = "unknown"; break;
}
return ret;
}
//
// Obsolete
//
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;
}
// vim:ts=4