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];
@@ -78,19 +75,17 @@ int commandLineRPC(int argc, char *argv[])
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())
@@ -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

@@ -138,8 +138,8 @@ uint64 Ledger::getBalance(const NewcoinAddress& accountID) const
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)
@@ -180,7 +180,8 @@ Transaction::pointer Ledger::getTransaction(const uint256& transID) const
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;
@@ -537,8 +538,7 @@ Ledger::pointer Ledger::switchPreviousLedger(Ledger::pointer oldPrevious, Ledger
// 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);

View File

@@ -79,14 +79,15 @@ STObject::STObject(SOElement* elem, const char *name) : SerializedType(name), mF
{ {
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++;
@@ -120,7 +121,7 @@ STObject::STObject(SOElement* elem, SerializerIterator& sit, const char *name) :
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++;
@@ -383,7 +384,7 @@ void STObject::setValueFieldU8(SOE_Field field, unsigned char v)
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);
} }
@@ -399,7 +400,7 @@ void STObject::setValueFieldU16(SOE_Field field, uint16 v)
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);
} }
@@ -415,7 +416,7 @@ void STObject::setValueFieldU32(SOE_Field field, uint32 v)
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);
} }
@@ -431,7 +432,7 @@ void STObject::setValueFieldU64(SOE_Field field, uint64 v)
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);
} }
@@ -447,7 +448,7 @@ void STObject::setValueFieldH160(SOE_Field field, const uint160& v)
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);
} }
@@ -463,7 +464,7 @@ void STObject::setValueFieldVL(SOE_Field field, const std::vector<unsigned char>
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);
} }
@@ -479,7 +480,7 @@ void STObject::setValueFieldTL(SOE_Field field, const std::vector<TaggedListItem
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);
} }
@@ -487,7 +488,8 @@ 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)
{ {

View File

@@ -11,7 +11,11 @@
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

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"));
@@ -20,12 +20,12 @@ 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()));
@@ -34,7 +34,7 @@ SerializedTransaction::SerializedTransaction(SerializerIterator& sit, int length
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");
@@ -132,42 +132,42 @@ 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);
} }
@@ -175,7 +175,7 @@ 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);
} }

View File

@@ -74,7 +74,7 @@ int Serializer::addRaw(const void *ptr, int len)
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;
} }
@@ -82,8 +82,8 @@ bool Serializer::get16(uint16& o, int offset) const
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;
} }
@@ -91,10 +91,10 @@ bool Serializer::get32(uint32& o, int offset) const
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;
} }
@@ -315,7 +315,7 @@ int Serializer::getTaggedListLength(const std::vector<TaggedListItem>& list)
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
@@ -325,14 +325,14 @@ bool Serializer::getVL(std::vector<unsigned char>& objectVL, int offset, int& le
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;
@@ -348,7 +348,7 @@ bool Serializer::getVL(std::vector<unsigned char>& objectVL, int offset, int& le
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
@@ -358,14 +358,14 @@ bool Serializer::getVLLength(int& length, int offset) const
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;
@@ -382,12 +382,12 @@ bool Serializer::getTaggedList(std::list<TaggedListItem>& list, int offset, int&
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));
@@ -401,12 +401,12 @@ 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));
@@ -438,45 +438,45 @@ std::vector<unsigned char> Serializer::encodeVL(int length)
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;
} }
@@ -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,7 +501,7 @@ 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;
} }
@@ -509,7 +509,7 @@ uint16 SerializerIterator::get16()
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;
} }
@@ -517,7 +517,7 @@ uint32 SerializerIterator::get32()
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;
} }
@@ -525,7 +525,7 @@ uint64 SerializerIterator::get64()
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;
} }
@@ -533,7 +533,7 @@ uint128 SerializerIterator::get128()
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;
} }
@@ -541,7 +541,7 @@ uint160 SerializerIterator::get160()
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,7 +550,7 @@ 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()