Various cleanups, mostly style and whitespace.

This commit is contained in:
JoelKatz
2012-04-17 02:34:24 -07:00
parent cc4b350eb7
commit 1ea7b56fa8
7 changed files with 523 additions and 528 deletions

View File

@@ -1,5 +1,4 @@
#include <iostream> #include <iostream>
#include <cstdlib> #include <cstdlib>
@@ -19,9 +18,7 @@
#include "Config.h" #include "Config.h"
#include "BitcoinUtil.h" #include "BitcoinUtil.h"
using namespace boost::asio; static inline bool isSwitchChar(char c)
inline bool isSwitchChar(char c)
{ {
#ifdef __WXMSW__ #ifdef __WXMSW__
return c == '-' || c == '/'; return c == '-' || c == '/';
@@ -56,13 +53,13 @@ int commandLineRPC(int argc, char *argv[])
try try
{ {
// Skip switches // Skip switches
while(argc > 1 && isSwitchChar(argv[1][0])) while ((argc > 1) && isSwitchChar(argv[1][0]))
{ {
argc--; argc--;
argv++; argv++;
} }
if(argc < 2) return(0); if (argc < 2) return 0;
std::string strMethod = argv[1]; std::string strMethod = argv[1];
@@ -75,22 +72,20 @@ int commandLineRPC(int argc, char *argv[])
Json::Value reply = callRPC(strMethod, params); Json::Value reply = callRPC(strMethod, params);
// Parse reply // Parse reply
Json::Value result=reply.get("result", Json::Value()); Json::Value result = reply.get("result", Json::Value());
Json::Value error=reply.get("error", Json::Value()); Json::Value error = reply.get("error", Json::Value());
if(result.isString() && result.asString()=="unknown command") if (result.isString() && (result.asString() == "unknown command"))
nRet=1; nRet=1;
if(!error.isNull()) if (!error.isNull())
{ { // Error
// Error
strPrint = "error: " + error.toStyledString(); strPrint = "error: " + error.toStyledString();
int code = error["code"].asInt(); int code = error["code"].asInt();
nRet = abs(code); nRet = abs(code);
} }
else else
{ { // Result
// Result
if (result.isNull()) if (result.isNull())
strPrint = ""; strPrint = "";
else if (result.isString()) else if (result.isString())
@@ -109,7 +104,7 @@ int commandLineRPC(int argc, char *argv[])
std::cout << "Exception CommandLineRPC()" << std::endl; std::cout << "Exception CommandLineRPC()" << std::endl;
} }
if(strPrint != "") if (strPrint != "")
{ {
std::cout << strPrint << std::endl; std::cout << strPrint << std::endl;
} }
@@ -119,27 +114,24 @@ int commandLineRPC(int argc, char *argv[])
Json::Value callRPC(const std::string& strMethod, const Json::Value& params) Json::Value callRPC(const std::string& strMethod, const Json::Value& params)
{ {
if(theConfig.RPC_USER == "" && theConfig.RPC_PASSWORD == "") if (theConfig.RPC_USER.empty() && theConfig.RPC_PASSWORD.empty())
throw std::runtime_error("You must set rpcpassword=<password> in the configuration file" throw std::runtime_error("You must set rpcpassword=<password> in the configuration file"
"If the file does not exist, create it with owner-readable-only file permissions."); "If the file does not exist, create it with owner-readable-only file permissions.");
// Connect to localhost // Connect to localhost
std::cout << "Connecting to port:" << theConfig.RPC_PORT << std::endl; std::cout << "Connecting to port:" << theConfig.RPC_PORT << std::endl;
ip::tcp::endpoint endpoint(ip::address::from_string(theConfig.RPC_IP), theConfig.RPC_PORT); boost::asio::ip::tcp::endpoint
ip::tcp::iostream stream; endpoint(boost::asio::ip::address::from_string(theConfig.RPC_IP), theConfig.RPC_PORT);
boost::asio::ip::tcp::iostream stream;
stream.connect(endpoint); stream.connect(endpoint);
if(stream.fail()) if (stream.fail())
throw std::runtime_error("couldn't connect to server"); throw std::runtime_error("couldn't connect to server");
// HTTP basic authentication // HTTP basic authentication
std::string strUserPass64 = EncodeBase64(theConfig.RPC_USER + ":" + theConfig.RPC_PASSWORD); std::string strUserPass64 = EncodeBase64(theConfig.RPC_USER + ":" + theConfig.RPC_PASSWORD);
std::map<std::string, std::string> mapRequestHeaders; std::map<std::string, std::string> mapRequestHeaders;
mapRequestHeaders["Authorization"] = std::string("Basic ") + strUserPass64; mapRequestHeaders["Authorization"] = std::string("Basic ") + strUserPass64;
// Send request // Send request
std::string strRequest = JSONRPCRequest(strMethod, params, Json::Value(1)); std::string strRequest = JSONRPCRequest(strMethod, params, Json::Value(1));
std::cout << "send request " << strMethod << " : " << strRequest << std::endl; std::cout << "send request " << strMethod << " : " << strRequest << std::endl;
@@ -154,7 +146,7 @@ Json::Value callRPC(const std::string& strMethod, const Json::Value& params)
int nStatus = ReadHTTP(stream, mapHeaders, strReply); int nStatus = ReadHTTP(stream, mapHeaders, strReply);
if (nStatus == 401) if (nStatus == 401)
throw std::runtime_error("incorrect rpcuser or rpcpassword (authorization failed)"); throw std::runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");
else if (nStatus >= 400 && nStatus != 400 && nStatus != 404 && nStatus != 500) else if ((nStatus >= 400) && (nStatus != 400) && (nStatus != 404) && (nStatus != 500)) // ?
throw std::runtime_error(strprintf("server returned HTTP error %d", nStatus)); throw std::runtime_error(strprintf("server returned HTTP error %d", nStatus));
else if (strReply.empty()) else if (strReply.empty())
throw std::runtime_error("no response from server"); throw std::runtime_error("no response from server");
@@ -170,3 +162,4 @@ Json::Value callRPC(const std::string& strMethod, const Json::Value& params)
return valReply; return valReply;
} }
// vim:ts=4

View File

@@ -19,12 +19,12 @@ Ledger::Ledger(const NewcoinAddress& masterID, uint64 startAmount) :
mFeeHeld(0), mTimeStamp(0), mLedgerSeq(0), mFeeHeld(0), mTimeStamp(0), mLedgerSeq(0),
mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false) mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false)
{ {
mTransactionMap=boost::make_shared<SHAMap>(); mTransactionMap = boost::make_shared<SHAMap>();
mAccountStateMap=boost::make_shared<SHAMap>(); mAccountStateMap = boost::make_shared<SHAMap>();
AccountState::pointer startAccount=boost::make_shared<AccountState>(masterID); AccountState::pointer startAccount = boost::make_shared<AccountState>(masterID);
startAccount->credit(startAmount); startAccount->credit(startAmount);
if(!addAccountState(startAccount)) if (!addAccountState(startAccount))
assert(false); assert(false);
} }
@@ -41,8 +41,8 @@ Ledger::Ledger(Ledger &prevLedger, uint64 ts) : mTimeStamp(ts),
mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false), mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false),
mTransactionMap(new SHAMap()), mAccountStateMap(prevLedger.mAccountStateMap) mTransactionMap(new SHAMap()), mAccountStateMap(prevLedger.mAccountStateMap)
{ {
mParentHash=prevLedger.getHash(); mParentHash = prevLedger.getHash();
mLedgerSeq=prevLedger.mLedgerSeq+1; mLedgerSeq = prevLedger.mLedgerSeq+1;
mAccountStateMap->setSeq(mLedgerSeq); mAccountStateMap->setSeq(mLedgerSeq);
} }
@@ -51,17 +51,17 @@ Ledger::Ledger(const std::vector<unsigned char>& rawLedger) : mFeeHeld(0), mTime
{ {
Serializer s(rawLedger); Serializer s(rawLedger);
// 32seq, 64fee, 256phash, 256thash, 256ahash, 64ts // 32seq, 64fee, 256phash, 256thash, 256ahash, 64ts
if(!s.get32(mLedgerSeq, BLgPIndex)) return; if (!s.get32(mLedgerSeq, BLgPIndex)) return;
if(!s.get64(mFeeHeld, BLgPFeeHeld)) return; if (!s.get64(mFeeHeld, BLgPFeeHeld)) return;
if(!s.get256(mParentHash, BLgPPrevLg)) return; if (!s.get256(mParentHash, BLgPPrevLg)) return;
if(!s.get256(mTransHash, BLgPTxT)) return; if (!s.get256(mTransHash, BLgPTxT)) return;
if(!s.get256(mAccountHash, BLgPAcT)) return; if (!s.get256(mAccountHash, BLgPAcT)) return;
if(!s.get64(mTimeStamp, BLgPClTs)) return; if (!s.get64(mTimeStamp, BLgPClTs)) return;
updateHash(); updateHash();
if(mValidHash) if(mValidHash)
{ {
mTransactionMap=boost::make_shared<SHAMap>(); mTransactionMap = boost::make_shared<SHAMap>();
mAccountStateMap=boost::make_shared<SHAMap>(); mAccountStateMap = boost::make_shared<SHAMap>();
} }
} }
@@ -70,17 +70,17 @@ Ledger::Ledger(const std::string& rawLedger) : mFeeHeld(0), mTimeStamp(0),
{ {
Serializer s(rawLedger); Serializer s(rawLedger);
// 32seq, 64fee, 256phash, 256thash, 256ahash, 64ts // 32seq, 64fee, 256phash, 256thash, 256ahash, 64ts
if(!s.get32(mLedgerSeq, BLgPIndex)) return; if (!s.get32(mLedgerSeq, BLgPIndex)) return;
if(!s.get64(mFeeHeld, BLgPFeeHeld)) return; if (!s.get64(mFeeHeld, BLgPFeeHeld)) return;
if(!s.get256(mParentHash, BLgPPrevLg)) return; if (!s.get256(mParentHash, BLgPPrevLg)) return;
if(!s.get256(mTransHash, BLgPTxT)) return; if (!s.get256(mTransHash, BLgPTxT)) return;
if(!s.get256(mAccountHash, BLgPAcT)) return; if (!s.get256(mAccountHash, BLgPAcT)) return;
if(!s.get64(mTimeStamp, BLgPClTs)) return; if (!s.get64(mTimeStamp, BLgPClTs)) return;
updateHash(); updateHash();
if(mValidHash) if(mValidHash)
{ {
mTransactionMap=boost::make_shared<SHAMap>(); mTransactionMap = boost::make_shared<SHAMap>();
mAccountStateMap=boost::make_shared<SHAMap>(); mAccountStateMap = boost::make_shared<SHAMap>();
} }
} }
@@ -88,16 +88,16 @@ void Ledger::updateHash()
{ {
if(!mImmutable) if(!mImmutable)
{ {
if(mTransactionMap) mTransHash=mTransactionMap->getHash(); if (mTransactionMap) mTransHash = mTransactionMap->getHash();
else mTransHash.zero(); else mTransHash.zero();
if(mAccountStateMap) mAccountHash=mAccountStateMap->getHash(); if (mAccountStateMap) mAccountHash = mAccountStateMap->getHash();
else mAccountHash.zero(); else mAccountHash.zero();
} }
Serializer s(116); Serializer s(116);
addRaw(s); addRaw(s);
mHash=s.getSHA512Half(); mHash =s.getSHA512Half();
mValidHash=true; mValidHash = true;
} }
void Ledger::addRaw(Serializer &s) void Ledger::addRaw(Serializer &s)
@@ -116,8 +116,8 @@ AccountState::pointer Ledger::getAccountState(const NewcoinAddress& accountID)
std::cerr << "Ledger:getAccountState(" << accountID.humanAccountID() << ")" << std::endl; std::cerr << "Ledger:getAccountState(" << accountID.humanAccountID() << ")" << std::endl;
#endif #endif
ScopedLock l(mTransactionMap->Lock()); ScopedLock l(mTransactionMap->Lock());
SHAMapItem::pointer item=mAccountStateMap->peekItem(accountID.getAccountID().to256()); SHAMapItem::pointer item = mAccountStateMap->peekItem(accountID.getAccountID().to256());
if(!item) if (!item)
{ {
#ifdef DEBUG #ifdef DEBUG
std::cerr << " notfound" << std::endl; std::cerr << " notfound" << std::endl;
@@ -130,23 +130,23 @@ AccountState::pointer Ledger::getAccountState(const NewcoinAddress& accountID)
uint64 Ledger::getBalance(const NewcoinAddress& accountID) const uint64 Ledger::getBalance(const NewcoinAddress& accountID) const
{ {
ScopedLock l(mTransactionMap->Lock()); ScopedLock l(mTransactionMap->Lock());
SHAMapItem::pointer item=mAccountStateMap->peekItem(accountID.getAccountID().to256()); SHAMapItem::pointer item = mAccountStateMap->peekItem(accountID.getAccountID().to256());
if(!item) return 0; if (!item) return 0;
return AccountState(item->getData()).getBalance(); return AccountState(item->getData()).getBalance();
} }
bool Ledger::updateAccountState(AccountState::pointer state) bool Ledger::updateAccountState(AccountState::pointer state)
{ {
assert(!mAccepted); assert(!mAccepted);
SHAMapItem::pointer item=boost::make_shared<SHAMapItem>(state->getAccountID().getAccountID(), state->getRaw()); return mAccountStateMap->updateGiveItem(boost::make_shared<SHAMapItem>(state->getAccountID().getAccountID(),
return mAccountStateMap->updateGiveItem(item, false); state->getRaw()), false);
} }
bool Ledger::addAccountState(AccountState::pointer state) bool Ledger::addAccountState(AccountState::pointer state)
{ {
assert(!mAccepted); assert(!mAccepted);
assert( (state->getBalance()==0) || (state->getSeq()>0) ); assert( (state->getBalance()==0) || (state->getSeq()>0) );
SHAMapItem::pointer item=boost::make_shared<SHAMapItem>(state->getAccountID().getAccountID(), state->getRaw()); SHAMapItem::pointer item = boost::make_shared<SHAMapItem>(state->getAccountID().getAccountID(), state->getRaw());
return mAccountStateMap->addGiveItem(item, false); return mAccountStateMap->addGiveItem(item, false);
} }
@@ -156,7 +156,7 @@ bool Ledger::addTransaction(Transaction::pointer trans)
assert(!!trans->getID()); assert(!!trans->getID());
Serializer s; Serializer s;
trans->getSTransaction()->getTransaction(s, false); trans->getSTransaction()->getTransaction(s, false);
SHAMapItem::pointer item=boost::make_shared<SHAMapItem>(trans->getID(), s.peekData()); SHAMapItem::pointer item = boost::make_shared<SHAMapItem>(trans->getID(), s.peekData());
return mTransactionMap->addGiveItem(item, true); return mTransactionMap->addGiveItem(item, true);
} }
@@ -173,14 +173,15 @@ bool Ledger::hasTransaction(const uint256& transID) const
Transaction::pointer Ledger::getTransaction(const uint256& transID) const Transaction::pointer Ledger::getTransaction(const uint256& transID) const
{ {
SHAMapItem::pointer item=mTransactionMap->peekItem(transID); SHAMapItem::pointer item = mTransactionMap->peekItem(transID);
if(!item) return Transaction::pointer(); if (!item) return Transaction::pointer();
Transaction::pointer txn=theApp->getMasterTransaction().fetch(transID, false); Transaction::pointer txn = theApp->getMasterTransaction().fetch(transID, false);
if(txn) return txn; if (txn) return txn;
txn=boost::make_shared<Transaction>(item->getData(), true); txn = boost::make_shared<Transaction>(item->getData(), true);
if(txn->getStatus()==NEW) txn->setStatus(mClosed ? COMMITTED : INCLUDED, mLedgerSeq); if (txn->getStatus() == NEW)
txn->setStatus(mClosed ? COMMITTED : INCLUDED, mLedgerSeq);
theApp->getMasterTransaction().canonicalize(txn, false); theApp->getMasterTransaction().canonicalize(txn, false);
return txn; return txn;
@@ -190,9 +191,9 @@ Ledger::TransResult Ledger::applyTransaction(Transaction::pointer trans)
{ {
assert(!mAccepted); assert(!mAccepted);
boost::recursive_mutex::scoped_lock sl(mLock); boost::recursive_mutex::scoped_lock sl(mLock);
if(trans->getSourceLedger()>mLedgerSeq) return TR_BADLSEQ; if (trans->getSourceLedger() > mLedgerSeq) return TR_BADLSEQ;
if(trans->getAmount()<trans->getFee()) if (trans->getAmount()<trans->getFee())
{ {
#ifdef DEBUG #ifdef DEBUG
std::cerr << "Transaction for " << trans->getAmount() << ", but fee is " << std::cerr << "Transaction for " << trans->getAmount() << ", but fee is " <<
@@ -519,38 +520,37 @@ Ledger::pointer Ledger::switchPreviousLedger(Ledger::pointer oldPrevious, Ledger
do do
{ {
count=0; count=0;
std::map<uint256, std::pair<Transaction::pointer, Transaction::pointer> >::iterator it=TxnDiff.begin(); std::map<uint256, std::pair<Transaction::pointer, Transaction::pointer> >::iterator it = TxnDiff.begin();
while(it!=TxnDiff.end()) while (it != TxnDiff.end())
{ {
Transaction::pointer& tx=it->second.second; Transaction::pointer& tx = it->second.second;
if(!tx || newLedger->addTransaction(tx)) if (!tx || newLedger->addTransaction(tx))
{ {
count++; count++;
TxnDiff.erase(it++); TxnDiff.erase(it++);
} }
else ++it; else ++it;
} }
} while(count!=0); } while (count!=0);
// WRITEME: Handle rejected transactions left in TxnDiff // WRITEME: Handle rejected transactions left in TxnDiff
// 5) Try to add transactions from this ledger to the new ledger. // 5) Try to add transactions from this ledger to the new ledger.
std::map<uint256, Transaction::pointer> txnMap; std::map<uint256, Transaction::pointer> txnMap;
for(SHAMapItem::pointer mit=peekTransactionMap()->peekFirstItem(); for(SHAMapItem::pointer mit = peekTransactionMap()->peekFirstItem();
!!mit; !!mit; mit = peekTransactionMap()->peekNextItem(mit->getTag()))
mit=peekTransactionMap()->peekNextItem(mit->getTag()))
{ {
uint256 txnID=mit->getTag(); uint256 txnID = mit->getTag();
Transaction::pointer tx=theApp->getMasterTransaction().fetch(txnID, false); Transaction::pointer tx = theApp->getMasterTransaction().fetch(txnID, false);
if(!tx) tx=boost::make_shared<Transaction>(mit->peekData(), false); if(!tx) tx = boost::make_shared<Transaction>(mit->peekData(), false);
txnMap.insert(std::make_pair(txnID, tx)); txnMap.insert(std::make_pair(txnID, tx));
} }
do do
{ {
count=0; count=0;
std::map<uint256, Transaction::pointer>::iterator it=txnMap.begin(); std::map<uint256, Transaction::pointer>::iterator it = txnMap.begin();
while(it!=txnMap.end()) while (it != txnMap.end())
{ {
if(newLedger->addTransaction(it->second)) if(newLedger->addTransaction(it->second))
{ {

View File

@@ -77,16 +77,17 @@ SerializedType* STObject::makeDeserializedObject(SerializedTypeID id, const char
STObject::STObject(SOElement* elem, const char *name) : SerializedType(name), mFlagIdx(-1) STObject::STObject(SOElement* elem, const char *name) : SerializedType(name), mFlagIdx(-1)
{ {
while(elem->e_id!=STI_DONE) while (elem->e_id != STI_DONE)
{ {
if(elem->e_type==SOE_FLAGS) mFlagIdx=mType.size(); if (elem->e_type == SOE_FLAGS)
mFlagIdx = mType.size();
mType.push_back(elem); mType.push_back(elem);
if( (elem->e_type==SOE_IFFLAG) || (elem->e_type==SOE_IFNFLAG) ) if ( (elem->e_type == SOE_IFFLAG) || (elem->e_type == SOE_IFNFLAG) )
giveObject(new STObject(elem->e_name)); giveObject(new STObject(elem->e_name));
else else
{ {
SerializedType* t=makeDefaultObject(elem->e_id, elem->e_name); SerializedType* t = makeDefaultObject(elem->e_id, elem->e_name);
if(!t) throw(std::runtime_error("invalid transaction element")); if (!t) throw std::runtime_error("invalid transaction element");
giveObject(t); giveObject(t);
} }
elem++; elem++;
@@ -95,32 +96,32 @@ STObject::STObject(SOElement* elem, const char *name) : SerializedType(name), mF
STObject::STObject(SOElement* elem, SerializerIterator& sit, const char *name) : SerializedType(name), mFlagIdx(-1) STObject::STObject(SOElement* elem, SerializerIterator& sit, const char *name) : SerializedType(name), mFlagIdx(-1)
{ {
int flags=-1; int flags = -1;
while(elem->e_id!=STI_DONE) while (elem->e_id != STI_DONE)
{ {
mType.push_back(elem); mType.push_back(elem);
bool done=false; bool done = false;
if(elem->e_type==SOE_IFFLAG) if (elem->e_type == SOE_IFFLAG)
{ {
assert(flags>=0); assert(flags >= 0);
if((flags&elem->e_flags)==0) done=true; if ((flags&elem->e_flags) == 0) done = true;
} }
else if(elem->e_type==SOE_IFNFLAG) else if (elem->e_type == SOE_IFNFLAG)
{ {
assert(flags>=0); assert(flags >= 0);
if((flags&elem->e_flags)!=0) done=true; if ((flags&elem->e_flags) != 0) done = true;
} }
else if(elem->e_type==SOE_FLAGS) else if (elem->e_type == SOE_FLAGS)
{ {
assert(elem->e_id==STI_UINT32); assert(elem->e_id == STI_UINT32);
flags=sit.get32(); flags = sit.get32();
mFlagIdx=giveObject(new STUInt32(elem->e_name, flags)); mFlagIdx = giveObject(new STUInt32(elem->e_name, flags));
done=true; done = true;
} }
if(!done) if(!done)
{ {
SerializedType* t=makeDeserializedObject(elem->e_id, elem->e_name, sit); SerializedType* t = makeDeserializedObject(elem->e_id, elem->e_name, sit);
if(!t) throw(std::runtime_error("invalid transaction element")); if (!t) throw std::runtime_error("invalid transaction element");
giveObject(t); giveObject(t);
} }
elem++; elem++;
@@ -130,59 +131,59 @@ STObject::STObject(SOElement* elem, SerializerIterator& sit, const char *name) :
std::string STObject::getFullText() const std::string STObject::getFullText() const
{ {
std::string ret; std::string ret;
if(name!=NULL) if (name != NULL)
{ {
ret=name; ret = name;
ret+=" = {"; ret += " = {";
} }
else ret="{"; else ret = "{";
for(boost::ptr_vector<SerializedType>::const_iterator it=mData.begin(), end=mData.end(); it!=end; ++it) for (boost::ptr_vector<SerializedType>::const_iterator it = mData.begin(), end = mData.end(); it != end; ++it)
ret+=it->getFullText(); ret += it->getFullText();
ret+="}"; ret += "}";
return ret; return ret;
} }
int STObject::getLength() const int STObject::getLength() const
{ {
int ret=0; int ret = 0;
for(boost::ptr_vector<SerializedType>::const_iterator it=mData.begin(), end=mData.end(); it!=end; ++it) for (boost::ptr_vector<SerializedType>::const_iterator it = mData.begin(), end = mData.end(); it != end; ++it)
ret+=it->getLength(); ret += it->getLength();
return ret; return ret;
} }
void STObject::add(Serializer& s) const void STObject::add(Serializer& s) const
{ {
for(boost::ptr_vector<SerializedType>::const_iterator it=mData.begin(), end=mData.end(); it!=end; ++it) for (boost::ptr_vector<SerializedType>::const_iterator it = mData.begin(), end = mData.end(); it != end; ++it)
it->add(s); it->add(s);
} }
std::string STObject::getText() const std::string STObject::getText() const
{ {
std::string ret="{"; std::string ret = "{";
bool first=false; bool first = false;
for(boost::ptr_vector<SerializedType>::const_iterator it=mData.begin(), end=mData.end(); it!=end; ++it) for (boost::ptr_vector<SerializedType>::const_iterator it = mData.begin(), end = mData.end(); it != end; ++it)
{ {
if(!first) if (!first)
{ {
ret+=", "; ret += ", ";
first=false; first = false;
} }
ret+=it->getText(); ret += it->getText();
} }
ret+="}"; ret += "}";
return ret; return ret;
} }
bool STObject::isEquivalent(const SerializedType& t) const bool STObject::isEquivalent(const SerializedType& t) const
{ {
const STObject* v=dynamic_cast<const STObject*>(&t); const STObject* v = dynamic_cast<const STObject*>(&t);
if(!v) return false; if (!v) return false;
boost::ptr_vector<SerializedType>::const_iterator it1=mData.begin(), end1=mData.end(); boost::ptr_vector<SerializedType>::const_iterator it1 = mData.begin(), end1 = mData.end();
boost::ptr_vector<SerializedType>::const_iterator it2=v->mData.begin(), end2=v->mData.end(); boost::ptr_vector<SerializedType>::const_iterator it2 = v->mData.begin(), end2 = v->mData.end();
while((it1!=end1) && (it2!=end2)) while ((it1 != end1) && (it2 != end2))
{ {
if(it1->getSType() != it2->getSType()) return false; if (it1->getSType() != it2->getSType()) return false;
if(!it1->isEquivalent(*it2)) return false; if (!it1->isEquivalent(*it2)) return false;
++it1; ++it1;
++it2; ++it2;
} }
@@ -191,51 +192,51 @@ bool STObject::isEquivalent(const SerializedType& t) const
int STObject::getFieldIndex(SOE_Field field) const int STObject::getFieldIndex(SOE_Field field) const
{ {
int i=0; int i = 0;
for(std::vector<SOElement*>::const_iterator it=mType.begin(), end=mType.end(); it!=end; ++it, ++i) for (std::vector<SOElement*>::const_iterator it = mType.begin(), end = mType.end(); it != end; ++it, ++i)
if((*it)->e_field==field) return i; if( (*it)->e_field == field) return i;
return -1; return -1;
} }
const SerializedType& STObject::peekAtField(SOE_Field field) const const SerializedType& STObject::peekAtField(SOE_Field field) const
{ {
int index=getFieldIndex(field); int index = getFieldIndex(field);
if(index==-1) throw std::runtime_error("Field not found"); if (index == -1) throw std::runtime_error("Field not found");
return peekAtIndex(index); return peekAtIndex(index);
} }
SerializedType& STObject::getField(SOE_Field field) SerializedType& STObject::getField(SOE_Field field)
{ {
int index=getFieldIndex(field); int index = getFieldIndex(field);
if(index==-1) throw std::runtime_error("Field not found"); if (index==-1) throw std::runtime_error("Field not found");
return getIndex(index); return getIndex(index);
} }
const SerializedType* STObject::peekAtPField(SOE_Field field) const const SerializedType* STObject::peekAtPField(SOE_Field field) const
{ {
int index=getFieldIndex(field); int index = getFieldIndex(field);
if(index==-1) return NULL; if (index == -1) return NULL;
return peekAtPIndex(index); return peekAtPIndex(index);
} }
SerializedType* STObject::getPField(SOE_Field field) SerializedType* STObject::getPField(SOE_Field field)
{ {
int index=getFieldIndex(field); int index = getFieldIndex(field);
if(index==-1) return NULL; if (index == -1) return NULL;
return getPIndex(index); return getPIndex(index);
} }
bool STObject::isFieldPresent(SOE_Field field) const bool STObject::isFieldPresent(SOE_Field field) const
{ {
int index=getFieldIndex(field); int index = getFieldIndex(field);
if(index==-1) return false; if (index == -1) return false;
return peekAtIndex(field).getSType()==STI_OBJECT; return peekAtIndex(field).getSType() == STI_OBJECT;
} }
bool STObject::setFlag(uint32 f) bool STObject::setFlag(uint32 f)
{ {
if(mFlagIdx<0) return false; if (mFlagIdx<0) return false;
STUInt32* t=dynamic_cast<STUInt32*>(getPIndex(mFlagIdx)); STUInt32* t = dynamic_cast<STUInt32*>(getPIndex(mFlagIdx));
assert(t); assert(t);
t->setValue(t->getValue() | f); t->setValue(t->getValue() | f);
return true; return true;
@@ -243,8 +244,8 @@ bool STObject::setFlag(uint32 f)
bool STObject::clearFlag(uint32 f) bool STObject::clearFlag(uint32 f)
{ {
if(mFlagIdx<0) return false; if (mFlagIdx<0) return false;
STUInt32* t=dynamic_cast<STUInt32*>(getPIndex(mFlagIdx)); STUInt32* t = dynamic_cast<STUInt32*>(getPIndex(mFlagIdx));
assert(t); assert(t);
t->setValue(t->getValue() & ~f); t->setValue(t->getValue() & ~f);
return true; return true;
@@ -252,247 +253,248 @@ bool STObject::clearFlag(uint32 f)
uint32 STObject::getFlags(void) const uint32 STObject::getFlags(void) const
{ {
if(mFlagIdx<0) return 0; if (mFlagIdx<0) return 0;
const STUInt32* t=dynamic_cast<const STUInt32*>(peekAtPIndex(mFlagIdx)); const STUInt32* t = dynamic_cast<const STUInt32*>(peekAtPIndex(mFlagIdx));
assert(t); assert(t);
return t->getValue(); return t->getValue();
} }
void STObject::makeFieldPresent(SOE_Field field) void STObject::makeFieldPresent(SOE_Field field)
{ {
int index=getFieldIndex(field); int index = getFieldIndex(field);
if(index==-1) throw std::runtime_error("Field not found"); if (index == -1) throw std::runtime_error("Field not found");
if(peekAtIndex(field).getSType()!=STI_OBJECT) return; if (peekAtIndex(field).getSType() != STI_OBJECT) return;
mData.replace(index, makeDefaultObject(mType[index]->e_id, mType[index]->e_name)); mData.replace(index, makeDefaultObject(mType[index]->e_id, mType[index]->e_name));
setFlag(mType[index]->e_flags); setFlag(mType[index]->e_flags);
} }
void STObject::makeFieldAbsent(SOE_Field field) void STObject::makeFieldAbsent(SOE_Field field)
{ {
int index=getFieldIndex(field); int index = getFieldIndex(field);
if(index==-1) throw std::runtime_error("Field not found"); if (index == -1) throw std::runtime_error("Field not found");
if(peekAtIndex(field).getSType()==STI_OBJECT) return; if (peekAtIndex(field).getSType() == STI_OBJECT) return;
mData.replace(index, new STObject(mType[index]->e_name)); mData.replace(index, new STObject(mType[index]->e_name));
clearFlag(mType[index]->e_flags); clearFlag(mType[index]->e_flags);
} }
std::string STObject::getFieldString(SOE_Field field) const std::string STObject::getFieldString(SOE_Field field) const
{ {
const SerializedType* rf=peekAtPField(field); const SerializedType* rf = peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
return rf->getText(); return rf->getText();
} }
unsigned char STObject::getValueFieldU8(SOE_Field field) const unsigned char STObject::getValueFieldU8(SOE_Field field) const
{ {
const SerializedType* rf=peekAtPField(field); const SerializedType* rf = peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) return 0; // optional field not present if (id == STI_OBJECT) return 0; // optional field not present
const STUInt8 *cf=dynamic_cast<const STUInt8 *>(rf); const STUInt8 *cf = dynamic_cast<const STUInt8 *>(rf);
if(!cf) throw std::runtime_error("Wrong field type"); if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue(); return cf->getValue();
} }
uint16 STObject::getValueFieldU16(SOE_Field field) const uint16 STObject::getValueFieldU16(SOE_Field field) const
{ {
const SerializedType* rf=peekAtPField(field); const SerializedType* rf = peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) return 0; // optional field not present if (id == STI_OBJECT) return 0; // optional field not present
const STUInt16 *cf=dynamic_cast<const STUInt16 *>(rf); const STUInt16 *cf = dynamic_cast<const STUInt16 *>(rf);
if(!cf) throw std::runtime_error("Wrong field type"); if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue(); return cf->getValue();
} }
uint32 STObject::getValueFieldU32(SOE_Field field) const uint32 STObject::getValueFieldU32(SOE_Field field) const
{ {
const SerializedType* rf=peekAtPField(field); const SerializedType* rf = peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) return 0; // optional field not present if (id == STI_OBJECT) return 0; // optional field not present
const STUInt32 *cf=dynamic_cast<const STUInt32 *>(rf); const STUInt32 *cf = dynamic_cast<const STUInt32 *>(rf);
if(!cf) throw std::runtime_error("Wrong field type"); if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue(); return cf->getValue();
} }
uint64 STObject::getValueFieldU64(SOE_Field field) const uint64 STObject::getValueFieldU64(SOE_Field field) const
{ {
const SerializedType* rf=peekAtPField(field); const SerializedType* rf = peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) return 0; // optional field not present if (id == STI_OBJECT) return 0; // optional field not present
const STUInt64 *cf=dynamic_cast<const STUInt64 *>(rf); const STUInt64 *cf = dynamic_cast<const STUInt64 *>(rf);
if(!cf) throw std::runtime_error("Wrong field type"); if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue(); return cf->getValue();
} }
uint160 STObject::getValueFieldH160(SOE_Field field) const uint160 STObject::getValueFieldH160(SOE_Field field) const
{ {
const SerializedType* rf=peekAtPField(field); const SerializedType* rf = peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) return uint160(); // optional field not present if (id == STI_OBJECT) return uint160(); // optional field not present
const STHash160 *cf=dynamic_cast<const STHash160 *>(rf); const STHash160 *cf = dynamic_cast<const STHash160 *>(rf);
if(!cf) throw std::runtime_error("Wrong field type"); if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue(); return cf->getValue();
} }
uint256 STObject::getValueFieldH256(SOE_Field field) const uint256 STObject::getValueFieldH256(SOE_Field field) const
{ {
const SerializedType* rf=peekAtPField(field); const SerializedType* rf = peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) return uint256(); // optional field not present if (id == STI_OBJECT) return uint256(); // optional field not present
const STHash256 *cf=dynamic_cast<const STHash256 *>(rf); const STHash256 *cf = dynamic_cast<const STHash256 *>(rf);
if(!cf) throw std::runtime_error("Wrong field type"); if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue(); return cf->getValue();
} }
std::vector<unsigned char> STObject::getValueFieldVL(SOE_Field field) const std::vector<unsigned char> STObject::getValueFieldVL(SOE_Field field) const
{ {
const SerializedType* rf=peekAtPField(field); const SerializedType* rf = peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) return std::vector<unsigned char>(); // optional field not present if (id == STI_OBJECT) return std::vector<unsigned char>(); // optional field not present
const STVariableLength *cf=dynamic_cast<const STVariableLength *>(rf); const STVariableLength *cf = dynamic_cast<const STVariableLength *>(rf);
if(!cf) throw std::runtime_error("Wrong field type"); if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue(); return cf->getValue();
} }
std::vector<TaggedListItem> STObject::getValueFieldTL(SOE_Field field) const std::vector<TaggedListItem> STObject::getValueFieldTL(SOE_Field field) const
{ {
const SerializedType* rf=peekAtPField(field); const SerializedType* rf = peekAtPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) return std::vector<TaggedListItem>(); // optional field not present if (id == STI_OBJECT) return std::vector<TaggedListItem>(); // optional field not present
const STTaggedList *cf=dynamic_cast<const STTaggedList *>(rf); const STTaggedList *cf = dynamic_cast<const STTaggedList *>(rf);
if(!cf) throw std::runtime_error("Wrong field type"); if (!cf) throw std::runtime_error("Wrong field type");
return cf->getValue(); return cf->getValue();
} }
void STObject::setValueFieldU8(SOE_Field field, unsigned char v) void STObject::setValueFieldU8(SOE_Field field, unsigned char v)
{ {
SerializedType* rf=getPField(field); SerializedType* rf = getPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) if (id == STI_OBJECT)
{ {
makeFieldPresent(field); makeFieldPresent(field);
rf=getPField(field); rf = getPField(field);
id=rf->getSType(); id = rf->getSType();
} }
STUInt8* cf=dynamic_cast<STUInt8*>(rf); STUInt8* cf = dynamic_cast<STUInt8*>(rf);
if(!cf) throw(std::runtime_error("Wrong field type")); if (!cf) throw std::runtime_error("Wrong field type");
cf->setValue(v); cf->setValue(v);
} }
void STObject::setValueFieldU16(SOE_Field field, uint16 v) void STObject::setValueFieldU16(SOE_Field field, uint16 v)
{ {
SerializedType* rf=getPField(field); SerializedType* rf = getPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) if (id == STI_OBJECT)
{ {
makeFieldPresent(field); makeFieldPresent(field);
rf=getPField(field); rf = getPField(field);
id=rf->getSType(); id = rf->getSType();
} }
STUInt16* cf=dynamic_cast<STUInt16*>(rf); STUInt16* cf = dynamic_cast<STUInt16*>(rf);
if(!cf) throw(std::runtime_error("Wrong field type")); if (!cf) throw std::runtime_error("Wrong field type");
cf->setValue(v); cf->setValue(v);
} }
void STObject::setValueFieldU32(SOE_Field field, uint32 v) void STObject::setValueFieldU32(SOE_Field field, uint32 v)
{ {
SerializedType* rf=getPField(field); SerializedType* rf = getPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) if (id == STI_OBJECT)
{ {
makeFieldPresent(field); makeFieldPresent(field);
rf=getPField(field); rf = getPField(field);
id=rf->getSType(); id = rf->getSType();
} }
STUInt32* cf=dynamic_cast<STUInt32*>(rf); STUInt32* cf = dynamic_cast<STUInt32*>(rf);
if(!cf) throw(std::runtime_error("Wrong field type")); if (!cf) throw std::runtime_error("Wrong field type");
cf->setValue(v); cf->setValue(v);
} }
void STObject::setValueFieldU64(SOE_Field field, uint64 v) void STObject::setValueFieldU64(SOE_Field field, uint64 v)
{ {
SerializedType* rf=getPField(field); SerializedType* rf = getPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) if (id == STI_OBJECT)
{ {
makeFieldPresent(field); makeFieldPresent(field);
rf=getPField(field); rf = getPField(field);
id=rf->getSType(); id = rf->getSType();
} }
STUInt64* cf=dynamic_cast<STUInt64*>(rf); STUInt64* cf = dynamic_cast<STUInt64*>(rf);
if(!cf) throw(std::runtime_error("Wrong field type")); if (!cf) throw std::runtime_error("Wrong field type");
cf->setValue(v); cf->setValue(v);
} }
void STObject::setValueFieldH160(SOE_Field field, const uint160& v) void STObject::setValueFieldH160(SOE_Field field, const uint160& v)
{ {
SerializedType* rf=getPField(field); SerializedType* rf = getPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) if (id == STI_OBJECT)
{ {
makeFieldPresent(field); makeFieldPresent(field);
rf=getPField(field); rf = getPField(field);
id=rf->getSType(); id = rf->getSType();
} }
STHash160* cf=dynamic_cast<STHash160*>(rf); STHash160* cf = dynamic_cast<STHash160*>(rf);
if(!cf) throw(std::runtime_error("Wrong field type")); if (!cf) throw std::runtime_error("Wrong field type");
cf->setValue(v); cf->setValue(v);
} }
void STObject::setValueFieldVL(SOE_Field field, const std::vector<unsigned char>& v) void STObject::setValueFieldVL(SOE_Field field, const std::vector<unsigned char>& v)
{ {
SerializedType* rf=getPField(field); SerializedType* rf = getPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) if (id == STI_OBJECT)
{ {
makeFieldPresent(field); makeFieldPresent(field);
rf=getPField(field); rf=getPField(field);
id=rf->getSType(); id=rf->getSType();
} }
STVariableLength* cf=dynamic_cast<STVariableLength*>(rf); STVariableLength* cf = dynamic_cast<STVariableLength*>(rf);
if(!cf) throw(std::runtime_error("Wrong field type")); if (!cf) throw std::runtime_error("Wrong field type");
cf->setValue(v); cf->setValue(v);
} }
void STObject::setValueFieldTL(SOE_Field field, const std::vector<TaggedListItem>& v) void STObject::setValueFieldTL(SOE_Field field, const std::vector<TaggedListItem>& v)
{ {
SerializedType* rf=getPField(field); SerializedType* rf = getPField(field);
if(!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id=rf->getSType(); SerializedTypeID id = rf->getSType();
if(id==STI_OBJECT) if (id == STI_OBJECT)
{ {
makeFieldPresent(field); makeFieldPresent(field);
rf=getPField(field); rf=getPField(field);
id=rf->getSType(); id=rf->getSType();
} }
STTaggedList* cf=dynamic_cast<STTaggedList*>(rf); STTaggedList* cf = dynamic_cast<STTaggedList*>(rf);
if(!cf) throw(std::runtime_error("Wrong field type")); if (!cf) throw std::runtime_error("Wrong field type");
cf->setValue(v); cf->setValue(v);
} }
Json::Value STObject::getJson(int options) const Json::Value STObject::getJson(int options) const
{ {
Json::Value ret(Json::objectValue); Json::Value ret(Json::objectValue);
int index=1; int index = 1;
for(boost::ptr_vector<SerializedType>::const_iterator it=mData.begin(), end=mData.end(); it!=end; ++it, ++index) for(boost::ptr_vector<SerializedType>::const_iterator it = mData.begin(), end = mData.end(); it != end;
++it, ++index)
{ {
if(it->getSType()!=STI_NOTPRESENT) if (it->getSType() != STI_NOTPRESENT)
{ {
if(it->getName()==NULL) if (it->getName() == NULL)
ret[boost::lexical_cast<std::string>(index)]=it->getText(); ret[boost::lexical_cast<std::string>(index)] = it->getText();
else ret[it->getName()]=it->getText(); else ret[it->getName()]=it->getText();
} }
} }

View File

@@ -11,13 +11,17 @@
enum SOE_Type enum SOE_Type
{ {
SOE_NEVER=-1, SOE_REQUIRED=0, SOE_FLAGS, SOE_IFFLAG=1, SOE_IFNFLAG=2 SOE_NEVER = -1, // never occurs (marks end of object)
SOE_REQUIRED = 0, // required
SOE_FLAGS = 1, // flags field
SOE_IFFLAG = 2, // present if flag set
SOE_IFNFLAG = 3 // present if flag not set
}; };
enum SOE_Field enum SOE_Field
{ {
sfInvalid=-1, sfInvalid = -1,
sfGeneric=0, sfGeneric = 0,
// common fields // common fields
sfFlags, sfExtensions, sfTargetLedger, sfSourceTag, sfIdentifier, sfFlags, sfExtensions, sfTargetLedger, sfSourceTag, sfIdentifier,
@@ -50,9 +54,9 @@ protected:
static SerializedType* makeDeserializedObject(SerializedTypeID id, const char *name, SerializerIterator&); static SerializedType* makeDeserializedObject(SerializedTypeID id, const char *name, SerializerIterator&);
public: public:
STObject(const char *n=NULL) : SerializedType(n), mFlagIdx(-1) { ; } STObject(const char *n = NULL) : SerializedType(n), mFlagIdx(-1) { ; }
STObject(SOElement *t, const char *n=NULL); STObject(SOElement *t, const char *n = NULL);
STObject(SOElement *t, SerializerIterator& u, const char *n=NULL); STObject(SOElement *t, SerializerIterator& u, const char *n = NULL);
virtual ~STObject() { ; } virtual ~STObject() { ; }
int getLength() const; int getLength() const;

View File

@@ -4,7 +4,7 @@
SerializedTransaction::SerializedTransaction(TransactionType type) SerializedTransaction::SerializedTransaction(TransactionType type)
{ {
mFormat=getTxnFormat(type); mFormat=getTxnFormat(type);
if(mFormat==NULL) throw(std::runtime_error("invalid transaction type")); if (mFormat == NULL) throw std::runtime_error("invalid transaction type");
mMiddleTxn.giveObject(new STUInt32("Magic", TransactionMagic)); mMiddleTxn.giveObject(new STUInt32("Magic", TransactionMagic));
mMiddleTxn.giveObject(new STVariableLength("SigningAccount")); mMiddleTxn.giveObject(new STVariableLength("SigningAccount"));
@@ -17,27 +17,27 @@ SerializedTransaction::SerializedTransaction(TransactionType type)
SerializedTransaction::SerializedTransaction(SerializerIterator& sit, int length) SerializedTransaction::SerializedTransaction(SerializerIterator& sit, int length)
{ {
if(length==-1) length=sit.getBytesLeft(); if (length == -1) length=sit.getBytesLeft();
else if(length==0) length=sit.get32(); else if (length == 0) length=sit.get32();
if( (length<TransactionMinLen) || (length>TransactionMaxLen) ) if ( (length < TransactionMinLen) || (length > TransactionMaxLen) )
throw(std::runtime_error("Transaction length invalid")); throw std::runtime_error("Transaction length invalid");
mSignature.setValue(sit.getVL()); mSignature.setValue(sit.getVL());
if(sit.get32()!=TransactionMagic) if (sit.get32() != TransactionMagic)
throw(std::runtime_error("Transaction has invalid magic")); throw std::runtime_error("Transaction has invalid magic");
mMiddleTxn.giveObject(new STUInt32("Magic", TransactionMagic)); mMiddleTxn.giveObject(new STUInt32("Magic", TransactionMagic));
mMiddleTxn.giveObject(new STVariableLength("SigningAccount", sit.getVL())); mMiddleTxn.giveObject(new STVariableLength("SigningAccount", sit.getVL()));
mMiddleTxn.giveObject(new STUInt32("Sequence", sit.get32())); mMiddleTxn.giveObject(new STUInt32("Sequence", sit.get32()));
int type=sit.get32(); int type = sit.get32();
mMiddleTxn.giveObject(new STUInt32("Type", type)); mMiddleTxn.giveObject(new STUInt32("Type", type));
mFormat=getTxnFormat(static_cast<TransactionType>(type)); mFormat = getTxnFormat(static_cast<TransactionType>(type));
if(!mFormat) throw(std::runtime_error("Transaction has invalid type")); if (!mFormat) throw std::runtime_error("Transaction has invalid type");
mMiddleTxn.giveObject(new STUInt64("Fee", sit.get64())); mMiddleTxn.giveObject(new STUInt64("Fee", sit.get64()));
mInnerTxn=STObject(mFormat->elements, sit, "InnerTransaction"); mInnerTxn = STObject(mFormat->elements, sit, "InnerTransaction");
} }
int SerializedTransaction::getLength() const int SerializedTransaction::getLength() const
@@ -47,30 +47,30 @@ int SerializedTransaction::getLength() const
std::string SerializedTransaction::getFullText() const std::string SerializedTransaction::getFullText() const
{ {
std::string ret="\""; std::string ret = "\"";
ret+=getTransactionID().GetHex(); ret += getTransactionID().GetHex();
ret+="\" = {"; ret += "\" = {";
ret+=mSignature.getFullText(); ret += mSignature.getFullText();
ret+=mMiddleTxn.getFullText(); ret += mMiddleTxn.getFullText();
ret+=mInnerTxn.getFullText(); ret += mInnerTxn.getFullText();
ret+="}"; ret += "}";
return ret; return ret;
} }
std::string SerializedTransaction::getText() const std::string SerializedTransaction::getText() const
{ {
std::string ret="{"; std::string ret = "{";
ret+=mSignature.getText(); ret += mSignature.getText();
ret+=mMiddleTxn.getText(); ret += mMiddleTxn.getText();
ret+=mInnerTxn.getText(); ret += mInnerTxn.getText();
ret+="}"; ret += "}";
return ret; return ret;
} }
int SerializedTransaction::getTransaction(Serializer& s, bool include_length) const int SerializedTransaction::getTransaction(Serializer& s, bool include_length) const
{ {
int l=getLength(); int l = getLength();
if(include_length) s.add32(l); if (include_length) s.add32(l);
mSignature.add(s); mSignature.add(s);
mMiddleTxn.add(s); mMiddleTxn.add(s);
mInnerTxn.add(s); mInnerTxn.add(s);
@@ -79,11 +79,11 @@ int SerializedTransaction::getTransaction(Serializer& s, bool include_length) co
bool SerializedTransaction::isEquivalent(const SerializedType& t) const bool SerializedTransaction::isEquivalent(const SerializedType& t) const
{ // Signatures are not compared { // Signatures are not compared
const SerializedTransaction* v=dynamic_cast<const SerializedTransaction*>(&t); const SerializedTransaction* v = dynamic_cast<const SerializedTransaction*>(&t);
if(!v) return false; if (!v) return false;
if(type != v->type) return false; if (type != v->type) return false;
if(mMiddleTxn != v->mMiddleTxn) return false; if (mMiddleTxn != v->mMiddleTxn) return false;
if(mInnerTxn != v->mInnerTxn) return false; if (mInnerTxn != v->mInnerTxn) return false;
return true; return true;
} }
@@ -131,51 +131,51 @@ void SerializedTransaction::setSignature(const std::vector<unsigned char>& sig)
uint32 SerializedTransaction::getVersion() const uint32 SerializedTransaction::getVersion() const
{ {
const STUInt32* v=dynamic_cast<const STUInt32*>(mMiddleTxn.peekAtPIndex(TransactionIVersion)); const STUInt32* v = dynamic_cast<const STUInt32*>(mMiddleTxn.peekAtPIndex(TransactionIVersion));
if(!v) throw(std::runtime_error("corrupt transaction")); if (!v) throw std::runtime_error("corrupt transaction");
return v->getValue(); return v->getValue();
} }
void SerializedTransaction::setVersion(uint32 ver) void SerializedTransaction::setVersion(uint32 ver)
{ {
STUInt32* v=dynamic_cast<STUInt32*>(mMiddleTxn.getPIndex(TransactionIVersion)); STUInt32* v = dynamic_cast<STUInt32*>(mMiddleTxn.getPIndex(TransactionIVersion));
if(!v) throw(std::runtime_error("corrupt transaction")); if (!v) throw std::runtime_error("corrupt transaction");
v->setValue(ver); v->setValue(ver);
} }
uint64 SerializedTransaction::getTransactionFee() const uint64 SerializedTransaction::getTransactionFee() const
{ {
const STUInt64* v=dynamic_cast<const STUInt64*>(mMiddleTxn.peekAtPIndex(TransactionIFee)); const STUInt64* v = dynamic_cast<const STUInt64*>(mMiddleTxn.peekAtPIndex(TransactionIFee));
if(!v) throw(std::runtime_error("corrupt transaction")); if (!v) throw std::runtime_error("corrupt transaction");
return v->getValue(); return v->getValue();
} }
void SerializedTransaction::setTransactionFee(uint64 fee) void SerializedTransaction::setTransactionFee(uint64 fee)
{ {
STUInt64* v=dynamic_cast<STUInt64*>(mMiddleTxn.getPIndex(TransactionIFee)); STUInt64* v = dynamic_cast<STUInt64*>(mMiddleTxn.getPIndex(TransactionIFee));
if(!v) throw(std::runtime_error("corrupt transaction")); if (!v) throw std::runtime_error("corrupt transaction");
v->setValue(fee); v->setValue(fee);
} }
uint32 SerializedTransaction::getSequence() const uint32 SerializedTransaction::getSequence() const
{ {
const STUInt32* v=dynamic_cast<const STUInt32*>(mMiddleTxn.peekAtPIndex(TransactionISequence)); const STUInt32* v = dynamic_cast<const STUInt32*>(mMiddleTxn.peekAtPIndex(TransactionISequence));
if(!v) throw(std::runtime_error("corrupt transaction")); if (!v) throw std::runtime_error("corrupt transaction");
return v->getValue(); return v->getValue();
} }
void SerializedTransaction::setSequence(uint32 seq) void SerializedTransaction::setSequence(uint32 seq)
{ {
STUInt32* v=dynamic_cast<STUInt32*>(mMiddleTxn.getPIndex(TransactionISequence)); STUInt32* v = dynamic_cast<STUInt32*>(mMiddleTxn.getPIndex(TransactionISequence));
if(!v) throw(std::runtime_error("corrupt transaction")); if (!v) throw std::runtime_error("corrupt transaction");
v->setValue(seq); v->setValue(seq);
} }
std::vector<unsigned char> SerializedTransaction::getSigningAccount() const std::vector<unsigned char> SerializedTransaction::getSigningAccount() const
{ {
const STVariableLength* v= const STVariableLength* v =
dynamic_cast<const STVariableLength*>(mMiddleTxn.peekAtPIndex(TransactionISigningAccount)); dynamic_cast<const STVariableLength*>(mMiddleTxn.peekAtPIndex(TransactionISigningAccount));
if(!v) throw(std::runtime_error("corrupt transaction")); if (!v) throw std::runtime_error("corrupt transaction");
return v->getValue(); return v->getValue();
} }
@@ -183,21 +183,21 @@ const std::vector<unsigned char>& SerializedTransaction::peekSigningAccount() co
{ {
const STVariableLength* v= const STVariableLength* v=
dynamic_cast<const STVariableLength*>(mMiddleTxn.peekAtPIndex(TransactionISigningAccount)); dynamic_cast<const STVariableLength*>(mMiddleTxn.peekAtPIndex(TransactionISigningAccount));
if(!v) throw(std::runtime_error("corrupt transaction")); if (!v) throw std::runtime_error("corrupt transaction");
return v->peekValue(); return v->peekValue();
} }
std::vector<unsigned char>& SerializedTransaction::peekSigningAccount() std::vector<unsigned char>& SerializedTransaction::peekSigningAccount()
{ {
STVariableLength* v=dynamic_cast<STVariableLength*>(mMiddleTxn.getPIndex(TransactionISigningAccount)); STVariableLength* v = dynamic_cast<STVariableLength*>(mMiddleTxn.getPIndex(TransactionISigningAccount));
if(!v) throw(std::runtime_error("corrupt transaction")); if (!v) throw std::runtime_error("corrupt transaction");
return v->peekValue(); return v->peekValue();
} }
void SerializedTransaction::setSigningAccount(const std::vector<unsigned char>& s) void SerializedTransaction::setSigningAccount(const std::vector<unsigned char>& s)
{ {
STVariableLength* v=dynamic_cast<STVariableLength*>(mMiddleTxn.getPIndex(TransactionISigningAccount)); STVariableLength* v = dynamic_cast<STVariableLength*>(mMiddleTxn.getPIndex(TransactionISigningAccount));
if(!v) throw(std::runtime_error("corrupt transaction")); if (!v) throw std::runtime_error("corrupt transaction");
v->setValue(s); v->setValue(s);
} }
@@ -239,10 +239,10 @@ void SerializedTransaction::makeITFieldAbsent(SOE_Field field)
Json::Value SerializedTransaction::getJson(int options) const Json::Value SerializedTransaction::getJson(int options) const
{ {
Json::Value ret(Json::objectValue); Json::Value ret(Json::objectValue);
ret["Type"]=mFormat->t_name; ret["Type"] = mFormat->t_name;
ret["ID"]=getTransactionID().GetHex(); ret["ID"] = getTransactionID().GetHex();
ret["Signature"]=mSignature.getText(); ret["Signature"] = mSignature.getText();
ret["Middle"]=mMiddleTxn.getJson(options); ret["Middle"] = mMiddleTxn.getJson(options);
ret["Inner"]=mInnerTxn.getJson(options); ret["Inner"] = mInnerTxn.getJson(options);
return ret; return ret;
} }

View File

@@ -6,181 +6,181 @@
int Serializer::add16(uint16 i) int Serializer::add16(uint16 i)
{ {
int ret=mData.size(); int ret = mData.size();
mData.push_back(static_cast<unsigned char>(i>>8)); mData.push_back(static_cast<unsigned char>(i >> 8));
mData.push_back(static_cast<unsigned char>(i&0xff)); mData.push_back(static_cast<unsigned char>(i & 0xff));
return ret; return ret;
} }
int Serializer::add32(uint32 i) int Serializer::add32(uint32 i)
{ {
int ret=mData.size(); int ret = mData.size();
mData.push_back(static_cast<unsigned char>(i>>24)); mData.push_back(static_cast<unsigned char>(i >> 24));
mData.push_back(static_cast<unsigned char>((i>>16)&0xff)); mData.push_back(static_cast<unsigned char>((i >> 16) & 0xff));
mData.push_back(static_cast<unsigned char>((i>>8)&0xff)); mData.push_back(static_cast<unsigned char>((i >> 8) & 0xff));
mData.push_back(static_cast<unsigned char>(i&0xff)); mData.push_back(static_cast<unsigned char>(i & 0xff));
return ret; return ret;
} }
int Serializer::add64(uint64 i) int Serializer::add64(uint64 i)
{ {
int ret=mData.size(); int ret = mData.size();
mData.push_back(static_cast<unsigned char>(i>>56)); mData.push_back(static_cast<unsigned char>(i >> 56));
mData.push_back(static_cast<unsigned char>((i>>48)&0xff)); mData.push_back(static_cast<unsigned char>((i >> 48) & 0xff));
mData.push_back(static_cast<unsigned char>((i>>40)&0xff)); mData.push_back(static_cast<unsigned char>((i >> 40) & 0xff));
mData.push_back(static_cast<unsigned char>((i>>32)&0xff)); mData.push_back(static_cast<unsigned char>((i >> 32) & 0xff));
mData.push_back(static_cast<unsigned char>((i>>24)&0xff)); mData.push_back(static_cast<unsigned char>((i >> 24) & 0xff));
mData.push_back(static_cast<unsigned char>((i>>16)&0xff)); mData.push_back(static_cast<unsigned char>((i >> 16) & 0xff));
mData.push_back(static_cast<unsigned char>((i>>8)&0xff)); mData.push_back(static_cast<unsigned char>((i >> 8) & 0xff));
mData.push_back(static_cast<unsigned char>(i&0xff)); mData.push_back(static_cast<unsigned char>(i & 0xff));
return ret; return ret;
} }
int Serializer::add128(const uint128& i) int Serializer::add128(const uint128& i)
{ {
int ret=mData.size(); int ret = mData.size();
mData.insert(mData.end(), i.begin(), i.end()); mData.insert(mData.end(), i.begin(), i.end());
return ret; return ret;
} }
int Serializer::add160(const uint160& i) int Serializer::add160(const uint160& i)
{ {
int ret=mData.size(); int ret = mData.size();
mData.insert(mData.end(), i.begin(), i.end()); mData.insert(mData.end(), i.begin(), i.end());
return ret; return ret;
} }
int Serializer::add256(const uint256& i) int Serializer::add256(const uint256& i)
{ {
int ret=mData.size(); int ret = mData.size();
mData.insert(mData.end(), i.begin(), i.end()); mData.insert(mData.end(), i.begin(), i.end());
return ret; return ret;
} }
int Serializer::addRaw(const std::vector<unsigned char> &vector) int Serializer::addRaw(const std::vector<unsigned char> &vector)
{ {
int ret=mData.size(); int ret = mData.size();
mData.insert(mData.end(), vector.begin(), vector.end()); mData.insert(mData.end(), vector.begin(), vector.end());
return ret; return ret;
} }
int Serializer::addRaw(const void *ptr, int len) int Serializer::addRaw(const void *ptr, int len)
{ {
int ret=mData.size(); int ret = mData.size();
mData.insert(mData.end(), (const char *) ptr, ((const char *)ptr)+len); mData.insert(mData.end(), (const char *) ptr, ((const char *)ptr)+len);
return ret; return ret;
} }
bool Serializer::get16(uint16& o, int offset) const bool Serializer::get16(uint16& o, int offset) const
{ {
if((offset+2)>mData.size()) return false; if ((offset + 2) > mData.size()) return false;
o=mData.at(offset++); o = mData.at(++offset);
o<<=8; o|=mData.at(offset); o <<= 8; o |= mData.at(offset);
return true; return true;
} }
bool Serializer::get32(uint32& o, int offset) const bool Serializer::get32(uint32& o, int offset) const
{ {
if((offset+4)>mData.size()) return false; if ((offset + 4) > mData.size()) return false;
o=mData.at(offset++); o=mData.at(++offset);
o<<=8; o|=mData.at(offset++); o<<=8; o|=mData.at(offset++); o<<=8; o |= mData.at(++offset); o <<= 8; o |= mData.at(++offset);
o<<=8; o|=mData.at(offset); o<<=8; o |= mData.at(offset);
return true; return true;
} }
bool Serializer::get64(uint64& o, int offset) const bool Serializer::get64(uint64& o, int offset) const
{ {
if((offset+8)>mData.size()) return false; if ((offset + 8) > mData.size()) return false;
o=mData.at(offset++); o=mData.at(++offset);
o<<=8; o|=mData.at(offset++); o<<=8; o|=mData.at(offset++); o<<=8; o|= mData.at(++offset); o <<= 8; o |= mData.at(++offset);
o<<=8; o|=mData.at(offset++); o<<=8; o|=mData.at(offset++); o<<=8; o|= mData.at(++offset); o <<= 8; o |= mData.at(++offset);
o<<=8; o|=mData.at(offset++); o<<=8; o|=mData.at(offset++); o<<=8; o|= mData.at(++offset); o <<= 8; o |= mData.at(++offset);
o<<=8; o|=mData.at(offset); o<<=8; o|= mData.at(offset);
return true; return true;
} }
bool Serializer::get128(uint128& o, int offset) const bool Serializer::get128(uint128& o, int offset) const
{ {
if((offset+(128/8))>mData.size()) return false; if ((offset + (128 / 8)) > mData.size()) return false;
memcpy(o.begin(), &(mData.front())+offset, (128/8)); memcpy(o.begin(), &(mData.front()) + offset, (128 / 8));
return true; return true;
} }
bool Serializer::get160(uint160& o, int offset) const bool Serializer::get160(uint160& o, int offset) const
{ {
if((offset+(160/8))>mData.size()) return false; if ((offset + (160 / 8)) > mData.size()) return false;
memcpy(o.begin(), &(mData.front())+offset, (160/8)); memcpy(o.begin(), &(mData.front()) + offset, (160 / 8));
return true; return true;
} }
bool Serializer::get256(uint256& o, int offset) const bool Serializer::get256(uint256& o, int offset) const
{ {
if((offset+(256/8))>mData.size()) return false; if ((offset + (256 / 8)) > mData.size()) return false;
memcpy(o.begin(), &(mData.front())+offset, (256/8)); memcpy(o.begin(), &(mData.front()) + offset, (256 / 8));
return true; return true;
} }
uint256 Serializer::get256(int offset) const uint256 Serializer::get256(int offset) const
{ {
uint256 ret; uint256 ret;
if((offset+(256/8))>mData.size()) return ret; if ((offset + (256 / 8)) > mData.size()) return ret;
memcpy(&ret, &(mData.front())+offset, (256/8)); memcpy(&ret, &(mData.front()) + offset, (256 / 8));
return ret; return ret;
} }
int Serializer::add8(unsigned char byte) int Serializer::add8(unsigned char byte)
{ {
int ret=mData.size(); int ret = mData.size();
mData.push_back(byte); mData.push_back(byte);
return ret; return ret;
} }
bool Serializer::get8(int& byte, int offset) const bool Serializer::get8(int& byte, int offset) const
{ {
if(offset>=mData.size()) return false; if (offset >= mData.size()) return false;
byte=mData[offset]; byte = mData[offset];
return true; return true;
} }
bool Serializer::chop(int bytes) bool Serializer::chop(int bytes)
{ {
if(bytes>mData.size()) return false; if (bytes > mData.size()) return false;
mData.resize(mData.size()-bytes); mData.resize(mData.size() - bytes);
return true; return true;
} }
int Serializer::removeLastByte() int Serializer::removeLastByte()
{ {
int size=mData.size()-1; int size = mData.size()-1;
if(size<0) if (size < 0)
{ {
assert(false); assert(false);
return -1; return -1;
} }
int ret=mData[size]; int ret = mData[size];
mData.resize(size); mData.resize(size);
return ret; return ret;
} }
bool Serializer::getRaw(std::vector<unsigned char>& o, int offset, int length) const bool Serializer::getRaw(std::vector<unsigned char>& o, int offset, int length) const
{ {
if((offset+length)>mData.size()) return false; if ((offset + length) > mData.size()) return false;
o.assign(mData.begin()+offset, mData.begin()+offset+length); o.assign(mData.begin() + offset, mData.begin() + offset+length);
return true; return true;
} }
std::vector<unsigned char> Serializer::getRaw(int offset, int length) const std::vector<unsigned char> Serializer::getRaw(int offset, int length) const
{ {
std::vector<unsigned char> o; std::vector<unsigned char> o;
if((offset+length)>mData.size()) return o; if ((offset + length) > mData.size()) return o;
o.assign(mData.begin()+offset, mData.begin()+offset+length); o.assign(mData.begin() + offset, mData.begin() + offset + length);
return o; return o;
} }
uint160 Serializer::getRIPEMD160(int size) const uint160 Serializer::getRIPEMD160(int size) const
{ {
uint160 ret; uint160 ret;
if((size<0)||(size>mData.size())) size=mData.size(); if ((size < 0) || (size>mData.size())) size = mData.size();
RIPEMD160(&(mData.front()), size, (unsigned char *) &ret); RIPEMD160(&(mData.front()), size, (unsigned char *) &ret);
return ret; return ret;
} }
@@ -188,7 +188,7 @@ uint160 Serializer::getRIPEMD160(int size) const
uint256 Serializer::getSHA256(int size) const uint256 Serializer::getSHA256(int size) const
{ {
uint256 ret; uint256 ret;
if((size<0)||(size>mData.size())) size=mData.size(); if ((size < 0) || (size > mData.size())) size = mData.size();
SHA256(&(mData.front()), size, (unsigned char *) &ret); SHA256(&(mData.front()), size, (unsigned char *) &ret);
return ret; return ret;
} }
@@ -201,7 +201,7 @@ uint256 Serializer::getSHA512Half(int size) const
uint256 Serializer::getSHA512Half(const std::vector<unsigned char>& data, int size) uint256 Serializer::getSHA512Half(const std::vector<unsigned char>& data, int size)
{ {
uint256 j[2]; uint256 j[2];
if((size<0)||(size>data.size())) size=data.size(); if ((size < 0) || (size > data.size())) size = data.size();
SHA512(&(data.front()), size, (unsigned char *) j); SHA512(&(data.front()), size, (unsigned char *) j);
return j[0]; return j[0];
} }
@@ -221,11 +221,11 @@ uint256 Serializer::getSHA512Half(const std::string& strData)
bool Serializer::checkSignature(int pubkeyOffset, int signatureOffset) const bool Serializer::checkSignature(int pubkeyOffset, int signatureOffset) const
{ {
std::vector<unsigned char> pubkey, signature; std::vector<unsigned char> pubkey, signature;
if(!getRaw(pubkey, pubkeyOffset, 65)) return false; if (!getRaw(pubkey, pubkeyOffset, 65)) return false;
if(!getRaw(signature, signatureOffset, 72)) return false; if (!getRaw(signature, signatureOffset, 72)) return false;
CKey pubCKey; CKey pubCKey;
if(!pubCKey.SetPubKey(pubkey)) return false; if (!pubCKey.SetPubKey(pubkey)) return false;
return pubCKey.Verify(getSHA512Half(signatureOffset), signature); return pubCKey.Verify(getSHA512Half(signatureOffset), signature);
} }
@@ -242,33 +242,33 @@ bool Serializer::makeSignature(std::vector<unsigned char> &signature, CKey& key)
bool Serializer::addSignature(CKey& key) bool Serializer::addSignature(CKey& key)
{ {
std::vector<unsigned char> signature; std::vector<unsigned char> signature;
if(!key.Sign(getSHA512Half(), signature)) return false; if (!key.Sign(getSHA512Half(), signature)) return false;
assert(signature.size()==72); assert(signature.size() == 72);
addRaw(signature); addRaw(signature);
return true; return true;
} }
int Serializer::addVL(const std::vector<unsigned char>& vector) int Serializer::addVL(const std::vector<unsigned char>& vector)
{ {
int ret=addRaw(encodeVL(vector.size())); int ret = addRaw(encodeVL(vector.size()));
addRaw(vector); addRaw(vector);
return ret; return ret;
} }
int Serializer::addVL(const void *ptr, int len) int Serializer::addVL(const void *ptr, int len)
{ {
int ret=addRaw(encodeVL(len)); int ret = addRaw(encodeVL(len));
addRaw(ptr, len); addRaw(ptr, len);
return ret; return ret;
} }
int Serializer::addTaggedList(const std::list<TaggedListItem>& list) int Serializer::addTaggedList(const std::list<TaggedListItem>& list)
{ {
int size=list.size(); int size = list.size();
if(size>255) return -1; if (size > 255) return -1;
int ret=add8(size); int ret = add8(size);
if(size!=0) if (size != 0)
for(std::list<TaggedListItem>::const_iterator it=list.begin(); it!=list.end(); ++it) for (std::list<TaggedListItem>::const_iterator it = list.begin(); it != list.end(); ++it)
{ {
add8(it->first); add8(it->first);
addVL(it->second); addVL(it->second);
@@ -278,11 +278,11 @@ int Serializer::addTaggedList(const std::list<TaggedListItem>& list)
int Serializer::addTaggedList(const std::vector<TaggedListItem>& list) int Serializer::addTaggedList(const std::vector<TaggedListItem>& list)
{ {
int size=list.size(); int size = list.size();
if(size>255) return -1; if (size > 255) return -1;
int ret=add8(size); int ret = add8(size);
if(size!=0) if (size != 0)
for(std::vector<TaggedListItem>::const_iterator it=list.begin(); it!=list.end(); ++it) for (std::vector<TaggedListItem>::const_iterator it = list.begin(); it != list.end(); ++it)
{ {
add8(it->first); add8(it->first);
addVL(it->second); addVL(it->second);
@@ -292,48 +292,48 @@ int Serializer::addTaggedList(const std::vector<TaggedListItem>& list)
int Serializer::getTaggedListLength(const std::list<TaggedListItem>& list) int Serializer::getTaggedListLength(const std::list<TaggedListItem>& list)
{ {
int size=list.size(); int size = list.size();
if(size>255) return -1; if (size > 255) return -1;
int ret=1; int ret = 1;
if(size!=0) if (size != 0)
for(std::list<TaggedListItem>::const_iterator it=list.begin(); it!=list.end(); ++it) for (std::list<TaggedListItem>::const_iterator it = list.begin(); it != list.end(); ++it)
ret+=1 + it->second.size() + Serializer::encodeLengthLength(it->second.size()); ret += 1 + it->second.size() + Serializer::encodeLengthLength(it->second.size());
return ret; return ret;
} }
int Serializer::getTaggedListLength(const std::vector<TaggedListItem>& list) int Serializer::getTaggedListLength(const std::vector<TaggedListItem>& list)
{ {
int size=list.size(); int size = list.size();
if(size>255) return -1; if (size > 255) return -1;
int ret=1; int ret = 1;
if(size!=0) if (size != 0)
for(std::vector<TaggedListItem>::const_iterator it=list.begin(); it!=list.end(); ++it) for (std::vector<TaggedListItem>::const_iterator it = list.begin(); it != list.end(); ++it)
ret+=1 + it->second.size() + Serializer::encodeLengthLength(it->second.size()); ret += 1 + it->second.size() + Serializer::encodeLengthLength(it->second.size());
return ret; return ret;
} }
bool Serializer::getVL(std::vector<unsigned char>& objectVL, int offset, int& length) const bool Serializer::getVL(std::vector<unsigned char>& objectVL, int offset, int& length) const
{ {
int b1; int b1;
if(!get8(b1, offset++)) return false; if (!get8(b1, ++offset)) return false;
int datLen, lenLen=decodeLengthLength(b1); int datLen, lenLen = decodeLengthLength(b1);
try try
{ {
if(lenLen==1) if (lenLen == 1)
datLen=decodeVLLength(b1); datLen = decodeVLLength(b1);
else if(lenLen==2) else if (lenLen == 2)
{ {
int b2; int b2;
if(!get8(b2, offset++)) return false; if (!get8(b2, ++offset)) return false;
datLen=decodeVLLength(b1, b2); datLen=decodeVLLength(b1, b2);
} }
else if(lenLen==3) else if (lenLen == 3)
{ {
int b2, b3; int b2, b3;
if(!get8(b2, offset++)) return false; if (!get8(b2, ++offset)) return false;
if(!get8(b3, offset++)) return false; if (!get8(b3, ++offset)) return false;
datLen=decodeVLLength(b1, b2, b3); datLen = decodeVLLength(b1, b2, b3);
} }
else return false; else return false;
} }
@@ -341,32 +341,32 @@ bool Serializer::getVL(std::vector<unsigned char>& objectVL, int offset, int& le
{ {
return false; return false;
} }
length=lenLen+datLen; length = lenLen + datLen;
return getRaw(objectVL, offset, datLen); return getRaw(objectVL, offset, datLen);
} }
bool Serializer::getVLLength(int& length, int offset) const bool Serializer::getVLLength(int& length, int offset) const
{ {
int b1; int b1;
if(!get8(b1, offset++)) return false; if (!get8(b1, ++offset)) return false;
int lenLen=decodeLengthLength(b1); int lenLen = decodeLengthLength(b1);
try try
{ {
if(lenLen==1) if (lenLen == 1)
length=decodeVLLength(b1); length=decodeVLLength(b1);
else if(lenLen==2) else if (lenLen == 2)
{ {
int b2; int b2;
if(!get8(b2, offset++)) return false; if (!get8(b2, ++offset)) return false;
length=decodeVLLength(b1, b2); length=decodeVLLength(b1, b2);
} }
else if(lenLen==3) else if (lenLen == 3)
{ {
int b2, b3; int b2, b3;
if(!get8(b2, offset++)) return false; if (!get8(b2, ++offset)) return false;
if(!get8(b3, offset++)) return false; if (!get8(b3, ++offset)) return false;
length=decodeVLLength(b1, b2, b3); length = decodeVLLength(b1, b2, b3);
} }
else return false; else return false;
} }
@@ -380,19 +380,19 @@ bool Serializer::getVLLength(int& length, int offset) const
bool Serializer::getTaggedList(std::list<TaggedListItem>& list, int offset, int& length) const bool Serializer::getTaggedList(std::list<TaggedListItem>& list, int offset, int& length) const
{ {
list.clear(); list.clear();
int startOffset=offset; int startOffset = offset;
int numElem; int numElem;
if(!get8(numElem, offset++)) return false; if (!get8(numElem, ++offset)) return false;
for(int i=0; i<numElem; i++) for (int i = 0; i<numElem; i++)
{ {
int tag, len; int tag, len;
std::vector<unsigned char> data; std::vector<unsigned char> data;
if(!get8(tag, offset++)) return false; if (!get8(tag, ++offset)) return false;
if(!getVL(data, offset, len)) return false; if (!getVL(data, offset, len)) return false;
offset+=len; offset += len;
list.push_back(std::make_pair(tag, data)); list.push_back(std::make_pair(tag, data));
} }
length=offset-startOffset; length = offset - startOffset;
return true; return true;
} }
@@ -401,83 +401,83 @@ bool Serializer::getTaggedList(std::vector<TaggedListItem>& list, int offset, in
list.clear(); list.clear();
int startOffset=offset; int startOffset=offset;
int numElem; int numElem;
if(!get8(numElem, offset++)) return false; if (!get8(numElem, ++offset)) return false;
for(int i=0; i<numElem; i++) for (int i=0; i<numElem; i++)
{ {
int tag, len; int tag, len;
std::vector<unsigned char> data; std::vector<unsigned char> data;
if(!get8(tag, offset++)) return false; if (!get8(tag, ++offset)) return false;
if(!getVL(data, offset, len)) return false; if (!getVL(data, offset, len)) return false;
offset+=len; offset += len;
list.push_back(std::make_pair(tag, data)); list.push_back(std::make_pair(tag, data));
} }
length=offset-startOffset; length = offset - startOffset;
return true; return true;
} }
std::vector<unsigned char> Serializer::encodeVL(int length) std::vector<unsigned char> Serializer::encodeVL(int length)
{ {
unsigned char lenBytes[4]; unsigned char lenBytes[4];
if(length<=192) if (length <= 192)
{ {
lenBytes[0]=static_cast<unsigned char>(length); lenBytes[0] = static_cast<unsigned char>(length);
return std::vector<unsigned char>(&lenBytes[0], &lenBytes[1]); return std::vector<unsigned char>(&lenBytes[0], &lenBytes[1]);
} }
else if(length<=12480) else if (length <= 12480)
{ {
length-=193; length -= 193;
lenBytes[0]=static_cast<unsigned char>(length>>8); lenBytes[0] = static_cast<unsigned char>(length >> 8);
lenBytes[1]=static_cast<unsigned char>(length&0xff); lenBytes[1] = static_cast<unsigned char>(length & 0xff);
return std::vector<unsigned char>(&lenBytes[0], &lenBytes[2]); return std::vector<unsigned char>(&lenBytes[0], &lenBytes[2]);
} }
else if(length<=918744) else if (length <= 918744)
{ {
length-=12481; length -= 12481;
lenBytes[0]=static_cast<unsigned char>(length>>16); lenBytes[0] = static_cast<unsigned char>(length >> 16);
lenBytes[1]=static_cast<unsigned char>((length>>8)&0xff); lenBytes[1] = static_cast<unsigned char>((length >> 8) & 0xff);
lenBytes[2]=static_cast<unsigned char>(length&0xff); lenBytes[2] = static_cast<unsigned char>(length & 0xff);
return std::vector<unsigned char>(&lenBytes[0], &lenBytes[3]); return std::vector<unsigned char>(&lenBytes[0], &lenBytes[3]);
} }
else throw(std::overflow_error("lenlen")); else throw std::overflow_error("lenlen");
} }
int Serializer::encodeLengthLength(int length) int Serializer::encodeLengthLength(int length)
{ {
if(length<0) throw(std::overflow_error("len<0")); if (length < 0) throw std::overflow_error("len<0");
if(length<=192) return 1; if (length <= 192) return 1;
if(length<=12480) return 2; if (length <= 12480) return 2;
if(length>=918744) return 3; if (length >= 918744) return 3;
throw(std::overflow_error("len>918644")); throw std::overflow_error("len>918644");
} }
int Serializer::decodeLengthLength(int b1) int Serializer::decodeLengthLength(int b1)
{ {
if(b1<0) throw(std::overflow_error("b1<0")); if (b1 < 0) throw std::overflow_error("b1<0");
if(b1<=192) return 1; if (b1 <= 192) return 1;
if(b1<=240) return 2; if (b1 <= 240) return 2;
if(b1<=254) return 3; if (b1 <= 254) return 3;
throw(std::overflow_error("b1>254")); throw std::overflow_error("b1>254");
} }
int Serializer::decodeVLLength(int b1) int Serializer::decodeVLLength(int b1)
{ {
if(b1<0) throw(std::overflow_error("b1<0")); if (b1 < 0) throw std::overflow_error("b1<0");
if(b1>254) throw(std::overflow_error("b1>254")); if (b1 > 254) throw std::overflow_error("b1>254");
return b1; return b1;
} }
int Serializer::decodeVLLength(int b1, int b2) int Serializer::decodeVLLength(int b1, int b2)
{ {
if(b1<193) throw(std::overflow_error("b1<193")); if (b1 < 193) throw std::overflow_error("b1<193");
if(b1>240) throw(std::overflow_error("b1>240")); if (b1 > 240) throw std::overflow_error("b1>240");
return 193+(b1-193)*256+b2; return 193 + (b1 - 193) * 256 + b2;
} }
int Serializer::decodeVLLength(int b1, int b2, int b3) int Serializer::decodeVLLength(int b1, int b2, int b3)
{ {
if(b1<241) throw(std::overflow_error("b1<241")); if (b1 < 241) throw std::overflow_error("b1<241");
if(b1>254) throw(std::overflow_error("b1>254")); if (b1 > 254) throw std::overflow_error("b1>254");
return 12481+(b1-241)*65536+b2*256+b3; return 12481 + (b1 - 241) * 65536 + b2 * 256 + b3;
} }
void Serializer::TestSerializer() void Serializer::TestSerializer()
@@ -493,7 +493,7 @@ int SerializerIterator::getBytesLeft()
unsigned char SerializerIterator::get8() unsigned char SerializerIterator::get8()
{ {
int val; int val;
if(!mSerializer.get8(val, mPos)) throw(0); if (!mSerializer.get8(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos++; mPos++;
return val; return val;
} }
@@ -501,48 +501,48 @@ unsigned char SerializerIterator::get8()
uint16 SerializerIterator::get16() uint16 SerializerIterator::get16()
{ {
uint16 val; uint16 val;
if(!mSerializer.get16(val, mPos)) throw(0); if (!mSerializer.get16(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos+=16/8; mPos += 16/8;
return val; return val;
} }
uint32 SerializerIterator::get32() uint32 SerializerIterator::get32()
{ {
uint32 val; uint32 val;
if(!mSerializer.get32(val, mPos)) throw(0); if (!mSerializer.get32(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos+=32/8; mPos += 32/8;
return val; return val;
} }
uint64 SerializerIterator::get64() uint64 SerializerIterator::get64()
{ {
uint64 val; uint64 val;
if(!mSerializer.get64(val, mPos)) throw(0); if (!mSerializer.get64(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos+=64/8; mPos += 64/8;
return val; return val;
} }
uint128 SerializerIterator::get128() uint128 SerializerIterator::get128()
{ {
uint128 val; uint128 val;
if(!mSerializer.get128(val, mPos)) throw(0); if (!mSerializer.get128(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos+=128/8; mPos += 128/8;
return val; return val;
} }
uint160 SerializerIterator::get160() uint160 SerializerIterator::get160()
{ {
uint160 val; uint160 val;
if(!mSerializer.get160(val, mPos)) throw(0); if (!mSerializer.get160(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos+=160/8; mPos += 160/8;
return val; return val;
} }
uint256 SerializerIterator::get256() uint256 SerializerIterator::get256()
{ {
uint256 val; uint256 val;
if(!mSerializer.get256(val, mPos)) throw(0); if (!mSerializer.get256(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos+=256/8; mPos += 256/8;
return val; return val;
} }
@@ -550,8 +550,8 @@ std::vector<unsigned char> SerializerIterator::getVL()
{ {
int length; int length;
std::vector<unsigned char> vl; std::vector<unsigned char> vl;
if(!mSerializer.getVL(vl, mPos, length)) throw(0); if (!mSerializer.getVL(vl, mPos, length)) throw std::runtime_error("invalid serializer get");
mPos+=length; mPos += length;
return vl; return vl;
} }
@@ -559,7 +559,7 @@ std::vector<TaggedListItem> SerializerIterator::getTaggedList()
{ {
int length; int length;
std::vector<TaggedListItem> tl; std::vector<TaggedListItem> tl;
if(!mSerializer.getTaggedList(tl, mPos, length)) throw(0); if (!mSerializer.getTaggedList(tl, mPos, length)) throw std::runtime_error("invalid serializer get");
mPos+=length; mPos += length;
return tl; return tl;
} }

View File

@@ -52,18 +52,14 @@ public:
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getTargetSize() const template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getTargetSize() const
{ {
mLock.lock(); boost::recursive_mutex::scoped_lock sl(mLock);
int ret=mTargetSize; return mTargetSize;
mLock.unlock();
return ret;
} }
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getTargetAge() const template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getTargetAge() const
{ {
mLock.lock(); boost::recursive_mutex::scoped_lock sl(mLock);
int ret=mTargetAge; return mTargetAge;
mLock.unlock();
return ret;
} }
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getCacheSize() template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getCacheSize()
@@ -76,28 +72,28 @@ template<typename c_Key, typename c_Data> void TaggedCache<c_Key, c_Data>::sweep
{ {
boost::recursive_mutex::scoped_lock sl(mLock); boost::recursive_mutex::scoped_lock sl(mLock);
if(mCache.size()<mTargetSize) return; if (mCache.size() < mTargetSize) return;
time_t now=time(NULL); time_t now = time(NULL);
if((mLastSweep+10)<now) return; if ((mLastSweep + 10) < now) return;
mLastSweep=now; mLastSweep = now;
time_t target=now-mTargetAge; time_t target = now - mTargetAge;
// Pass 1, remove old objects from cache // Pass 1, remove old objects from cache
typename std::map<key_type, cache_entry>::iterator cit=mCache.begin(); typename std::map<key_type, cache_entry>::iterator cit = mCache.begin();
while(cit!=mCache.end()) while (cit != mCache.end())
{ {
if(cit->second->second.first<target) if (cit->second->second.first < target)
mCache.erase(cit++); mCache.erase(cit++);
else ++cit; else ++cit;
} }
// Pass 2, remove dead objects from map // Pass 2, remove dead objects from map
typename std::map<key_type, weak_data_ptr>::iterator mit=mMap.begin(); typename std::map<key_type, weak_data_ptr>::iterator mit = mMap.begin();
while(mit!=mMap.end()) while (mit != mMap.end())
{ {
if(mit->second->expired()) if (mit->second->expired())
mMap.erase(mit++); mMap.erase(mit++);
else ++mit; else ++mit;
} }
@@ -108,19 +104,19 @@ template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::touch
boost::recursive_mutex::scoped_lock sl(mLock); boost::recursive_mutex::scoped_lock sl(mLock);
// Is the object in the map? // Is the object in the map?
typename std::map<key_type, weak_data_ptr>::iterator mit=mMap.find(key); typename std::map<key_type, weak_data_ptr>::iterator mit = mMap.find(key);
if(mit==mMap.end()) return false; if (mit == mMap.end()) return false;
if(mit->second->expired()) if (mit->second->expired())
{ // in map, but expired { // in map, but expired
mMap.erase(mit); mMap.erase(mit);
return false; return false;
} }
// Is the object in the cache? // Is the object in the cache?
typename std::map<key_type, cache_entry>::iterator cit=mCache.find(key); typename std::map<key_type, cache_entry>::iterator cit = mCache.find(key);
if(cit!=mCache.end()) if (cit != mCache.end())
{ // in both map and cache { // in both map and cache
cit->second->first=time(NULL); cit->second->first = time(NULL);
return true; return true;
} }
@@ -133,8 +129,8 @@ template<typename c_Key, typename c_Data> bool TaggedCache<c_Key, c_Data>::del(c
{ // Remove from cache, map unaffected { // Remove from cache, map unaffected
boost::recursive_mutex::scoped_lock sl(mLock); boost::recursive_mutex::scoped_lock sl(mLock);
typename std::map<key_type, cache_entry>::iterator cit=mCache.find(key); typename std::map<key_type, cache_entry>::iterator cit = mCache.find(key);
if(cit==mCache.end()) return false; if (cit == mCache.end()) return false;
mCache.erase(cit); mCache.erase(cit);
return true; return true;
} }
@@ -145,27 +141,27 @@ bool TaggedCache<c_Key, c_Data>::canonicalize(const key_type& key, boost::shared
// Return values: true=we had the data already // Return values: true=we had the data already
boost::recursive_mutex::scoped_lock sl(mLock); boost::recursive_mutex::scoped_lock sl(mLock);
typename std::map<key_type, weak_data_ptr>::iterator mit=mMap.find(key); typename std::map<key_type, weak_data_ptr>::iterator mit = mMap.find(key);
if(mit==mMap.end()) if (mit == mMap.end())
{ // not in map { // not in map
mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data))); mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data)));
mMap.insert(std::make_pair(key, data)); mMap.insert(std::make_pair(key, data));
return false; return false;
} }
if(mit->second.expired()) if (mit->second.expired())
{ // in map, but expired { // in map, but expired
mit->second=data; mit->second = data;
mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data))); mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data)));
return false; return false;
} }
data=mit->second.lock(); data = mit->second.lock();
assert(!!data); assert(!!data);
// Valid in map, is it in cache? // Valid in map, is it in cache?
typename std::map<key_type, cache_entry>::iterator cit=mCache.find(key); typename std::map<key_type, cache_entry>::iterator cit = mCache.find(key);
if(cit!=mCache.end()) if (cit != mCache.end())
cit->second.first=time(NULL); // Yes, refesh cit->second.first = time(NULL); // Yes, refesh
else // no, add to cache else // no, add to cache
mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data))); mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data)));
@@ -178,20 +174,20 @@ boost::shared_ptr<c_Data> TaggedCache<c_Key, c_Data>::fetch(const key_type& key)
boost::recursive_mutex::scoped_lock sl(mLock); boost::recursive_mutex::scoped_lock sl(mLock);
// Is it in the map? // Is it in the map?
typename std::map<key_type, weak_data_ptr>::iterator mit=mMap.find(key); typename std::map<key_type, weak_data_ptr>::iterator mit = mMap.find(key);
if(mit==mMap.end()) return data_ptr(); // No, we're done if (mit == mMap.end()) return data_ptr(); // No, we're done
if(mit->second.expired()) if (mit->second.expired())
{ // in map, but expired { // in map, but expired
mMap.erase(mit); mMap.erase(mit);
return data_ptr(); return data_ptr();
} }
boost::shared_ptr<c_Data> data=mit->second.lock(); boost::shared_ptr<c_Data> data = mit->second.lock();
// Valid in map, is it in the cache? // Valid in map, is it in the cache?
typename std::map<key_type, cache_entry>::iterator cit=mCache.find(key); typename std::map<key_type, cache_entry>::iterator cit = mCache.find(key);
if(cit!=mCache.end()) if (cit != mCache.end())
cit->second.first=time(NULL); // Yes, refresh cit->second.first = time(NULL); // Yes, refresh
else // No, add to cache else // No, add to cache
mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data))); mCache.insert(std::make_pair(key, std::make_pair(time(NULL), data)));