Merge branch 'master' of github.com:jedmccaleb/NewCoin

This commit is contained in:
Arthur Britto
2012-04-17 19:47:17 -07:00
15 changed files with 693 additions and 570 deletions

View File

@@ -1,5 +1,4 @@
#include <iostream>
#include <cstdlib>
@@ -19,9 +18,7 @@
#include "Config.h"
#include "BitcoinUtil.h"
using namespace boost::asio;
inline bool isSwitchChar(char c)
static inline bool isSwitchChar(char c)
{
#ifdef __WXMSW__
return c == '-' || c == '/';
@@ -56,13 +53,13 @@ int commandLineRPC(int argc, char *argv[])
try
{
// Skip switches
while(argc > 1 && isSwitchChar(argv[1][0]))
while ((argc > 1) && isSwitchChar(argv[1][0]))
{
argc--;
argv++;
}
if(argc < 2) return(0);
if (argc < 2) return 0;
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 error = reply.get("error", Json::Value());
if(result.isString() && result.asString()=="unknown command")
if (result.isString() && (result.asString() == "unknown command"))
nRet=1;
if (!error.isNull())
{
// Error
{ // Error
strPrint = "error: " + error.toStyledString();
int code = error["code"].asInt();
nRet = abs(code);
}
else
{
// Result
{ // Result
if (result.isNull())
strPrint = "";
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)
{
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"
"If the file does not exist, create it with owner-readable-only file permissions.");
// Connect to localhost
std::cout << "Connecting to port:" << theConfig.RPC_PORT << std::endl;
ip::tcp::endpoint endpoint(ip::address::from_string(theConfig.RPC_IP), theConfig.RPC_PORT);
ip::tcp::iostream stream;
boost::asio::ip::tcp::endpoint
endpoint(boost::asio::ip::address::from_string(theConfig.RPC_IP), theConfig.RPC_PORT);
boost::asio::ip::tcp::iostream stream;
stream.connect(endpoint);
if (stream.fail())
throw std::runtime_error("couldn't connect to server");
// HTTP basic authentication
std::string strUserPass64 = EncodeBase64(theConfig.RPC_USER + ":" + theConfig.RPC_PASSWORD);
std::map<std::string, std::string> mapRequestHeaders;
mapRequestHeaders["Authorization"] = std::string("Basic ") + strUserPass64;
// Send request
std::string strRequest = JSONRPCRequest(strMethod, params, Json::Value(1));
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);
if (nStatus == 401)
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));
else if (strReply.empty())
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;
}
// vim:ts=4

View File

@@ -138,8 +138,8 @@ uint64 Ledger::getBalance(const NewcoinAddress& accountID) const
bool Ledger::updateAccountState(AccountState::pointer state)
{
assert(!mAccepted);
SHAMapItem::pointer item=boost::make_shared<SHAMapItem>(state->getAccountID().getAccountID(), state->getRaw());
return mAccountStateMap->updateGiveItem(item, false);
return mAccountStateMap->updateGiveItem(boost::make_shared<SHAMapItem>(state->getAccountID().getAccountID(),
state->getRaw()), false);
}
bool Ledger::addAccountState(AccountState::pointer state)
@@ -180,7 +180,8 @@ Transaction::pointer Ledger::getTransaction(const uint256& transID) const
if (txn) return txn;
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);
return txn;
@@ -330,7 +331,7 @@ Ledger::pointer Ledger::closeLedger(uint64 timeStamp)
// CAUTION: New ledger needs its SHAMap's connected to storage
updateHash();
setClosed();
return Ledger::pointer(new Ledger(*this, timeStamp)); // can't use make_shared
return Ledger::pointer(new Ledger(*this, timeStamp)); // can't use make_shared, constructor is protected
}
void LocalAccount::syncLedger()
@@ -363,7 +364,7 @@ bool Ledger::unitTest()
Ledger::pointer ledger=boost::make_shared<Ledger>(la1, 100000);
ledger=Ledger::pointer(new Ledger(*ledger, 0)); // can't use make_shared
ledger=make_shared<Ledger>(*ledger, 0); // can't use make_shared
AccountState::pointer as=ledger->getAccountState(la1);
assert(as);
@@ -537,8 +538,7 @@ Ledger::pointer Ledger::switchPreviousLedger(Ledger::pointer oldPrevious, Ledger
// 5) Try to add transactions from this ledger to the new ledger.
std::map<uint256, Transaction::pointer> txnMap;
for(SHAMapItem::pointer mit = peekTransactionMap()->peekFirstItem();
!!mit;
mit=peekTransactionMap()->peekNextItem(mit->getTag()))
!!mit; mit = peekTransactionMap()->peekNextItem(mit->getTag()))
{
uint256 txnID = mit->getTag();
Transaction::pointer tx = theApp->getMasterTransaction().fetch(txnID, false);

View File

@@ -15,13 +15,28 @@
#include "Hanko.h"
#include "AccountState.h"
#include "SHAMap.h"
#include "SerializedLedger.h"
enum LedgerStateParms
{
// input flags
lepCREATE, // Create if not present
// output flags
lepOKAY, // success
lepMISSING, // No node in that slot
lepWRONGTYPE, // Node of different type there
lepCREATED, // Node was created
lepERROR, // error
};
class Ledger : public boost::enable_shared_from_this<Ledger>
{ // The basic Ledger structure, can be opened, closed, or synching
public:
typedef boost::shared_ptr<Ledger> pointer;
enum TransResult
{
TR_ERROR =-1,
@@ -95,23 +110,35 @@ public:
bool isAcquiringTx(void);
bool isAcquiringAS(void);
// mid level functions
// Transaction Functions
bool hasTransaction(const uint256& TransID) const;
AccountState::pointer getAccountState(const NewcoinAddress& acctID);
Transaction::pointer getTransaction(const uint256& transID) const;
uint64 getBalance(const NewcoinAddress& acctID) const;
// high level functions
// OLD high level functions
uint64 getBalance(const NewcoinAddress& acctID) const;
AccountState::pointer getAccountState(const NewcoinAddress& acctID);
TransResult applyTransaction(Transaction::pointer trans);
TransResult removeTransaction(Transaction::pointer trans);
TransResult hasTransaction(Transaction::pointer trans);
Ledger::pointer switchPreviousLedger(Ledger::pointer oldPrevious, Ledger::pointer newPrevious, int limit);
// high-level functions
LedgerStateParms writeBack(LedgerStateParms parms, SerializedLedgerEntry::pointer);
SerializedLedgerEntry::pointer getAccountRoot(LedgerStateParms& parms, const uint160& accountID);
SerializedLedgerEntry::pointer getNickname(LedgerStateParms& parms, const std::string& nickname);
SerializedLedgerEntry::pointer getNickname(LedgerStateParms& parms, const uint256& nickHash);
// SerializedLedgerEntry::pointer getRippleState(LedgerStateParms parms, const uint160& offeror,
// const uint160& borrower, const Currency& currency);
// database functions
static void saveAcceptedLedger(Ledger::pointer);
static Ledger::pointer loadByIndex(uint32 ledgerIndex);
static Ledger::pointer loadByHash(const uint256& ledgerHash);
// index calculation functions
static uint256 getAccountRootIndex(const uint160& account);
static uint256 getRippleIndex(const uint160& account, const uint160& extendTo, const uint160& currency);
Ledger::pointer closeLedger(uint64 timestamp);
bool isCompatible(boost::shared_ptr<Ledger> other);
bool signLedger(std::vector<unsigned char> &signature, const LocalHanko &hanko);
@@ -121,4 +148,14 @@ public:
static bool unitTest();
};
inline LedgerStateParms operator|(const LedgerStateParms& l1, const LedgerStateParms& l2)
{
return static_cast<LedgerStateParms>(static_cast<int>(l1) | static_cast<int>(l2));
}
inline LedgerStateParms operator&(const LedgerStateParms& l1, const LedgerStateParms& l2)
{
return static_cast<LedgerStateParms>(static_cast<int>(l1) & static_cast<int>(l2));
}
#endif

14
src/LedgerIndex.cpp Normal file
View File

@@ -0,0 +1,14 @@
#include "Ledger.h"
uint256 Ledger::getAccountRootIndex(const uint160& accountID)
{ // Index is accountID extended to 256 bits
return accountID.to256();
}
uint256 Ledger::getRippleIndex(const uint160& accountID, const uint160& extendTo, const uint160& currency)
{ // Index is 160-bit account credit extended to, 96-bit XOR of extending account and currency
uint256 base=getAccountRootIndex(extendTo);
memcpy(base.begin() + (160/8), (accountID^currency).begin(), (256/8)-(160/8));
return base;
}

45
src/LedgerNode.cpp Normal file
View File

@@ -0,0 +1,45 @@
#include "Ledger.h"
#include "boost/make_shared.hpp"
SerializedLedgerEntry::pointer Ledger::getAccountRoot(LedgerStateParms& parms, const uint160& accountID)
{
uint256 nodeID=getAccountRootIndex(accountID);
ScopedLock l(mAccountStateMap->Lock());
SHAMapItem::pointer account = mAccountStateMap->peekItem(nodeID);
if (!account)
{
if ( (parms & lepCREATE) == 0 )
{
parms = lepMISSING;
return SerializedLedgerEntry::pointer();
}
parms = lepCREATED;
SerializedLedgerEntry::pointer sle=boost::make_shared<SerializedLedgerEntry>(ltACCOUNT_ROOT);
sle->setIndex(nodeID);
return sle;
}
try
{
SerializedLedgerEntry::pointer sle =
boost::make_shared<SerializedLedgerEntry>(account->peekSerializer(), nodeID);
if(sle->getType() != ltACCOUNT_ROOT)
{ // maybe it's a currency or something
parms = lepWRONGTYPE;
return SerializedLedgerEntry::pointer();
}
parms = lepOKAY;
return sle;
}
catch(...)
{
parms = lepERROR;
return SerializedLedgerEntry::pointer();
}
}

View File

@@ -91,7 +91,7 @@ public:
private:
uint256 mTag;
std::vector<unsigned char> mData;
Serializer mData;
public:
@@ -103,8 +103,9 @@ public:
SHAMapItem(const uint160& tag, const std::vector<unsigned char>& data);
const uint256& getTag() const { return mTag; }
std::vector<unsigned char> getData() const { return mData; }
const std::vector<unsigned char>& peekData() const { return mData; }
std::vector<unsigned char> getData() const { return mData.getData(); }
const std::vector<unsigned char>& peekData() const { return mData.peekData(); }
Serializer& peekSerializer() { return mData; }
void addRaw(Serializer &s) { s.addRaw(mData); }
void addRaw(std::vector<unsigned char>& s) { s.insert(s.end(), mData.begin(), mData.end()); }

View File

@@ -12,6 +12,19 @@ SerializedLedgerEntry::SerializedLedgerEntry(SerializerIterator& sit, const uint
mObject = STObject(mFormat->elements, sit, "Entry");
}
SerializedLedgerEntry::SerializedLedgerEntry(const Serializer& s, const uint256& index)
: STObject("LedgerEntry"), mIndex(index)
{
SerializerIterator sit(s);
uint16 type = sit.get16();
mFormat = getLgrFormat(static_cast<LedgerEntryType>(type));
if (mFormat == NULL) throw std::runtime_error("invalid ledger entry type");
mType = mFormat->t_type;
mVersion.setValue(type);
mObject = STObject(mFormat->elements, sit, "Entry");
}
SerializedLedgerEntry::SerializedLedgerEntry(LedgerEntryType type) : STObject("LedgerEntry"), mType(type)
{
mFormat = getLgrFormat(type);

View File

@@ -17,6 +17,7 @@ protected:
LedgerEntryFormat* mFormat;
public:
SerializedLedgerEntry(const Serializer& s, const uint256& index);
SerializedLedgerEntry(SerializerIterator& sit, const uint256& index);
SerializedLedgerEntry(LedgerEntryType type);

View File

@@ -79,14 +79,15 @@ STObject::STObject(SOElement* elem, const char *name) : SerializedType(name), mF
{
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);
if ( (elem->e_type == SOE_IFFLAG) || (elem->e_type == SOE_IFNFLAG) )
giveObject(new STObject(elem->e_name));
else
{
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);
}
elem++;
@@ -120,7 +121,7 @@ STObject::STObject(SOElement* elem, SerializerIterator& sit, const char *name) :
if(!done)
{
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);
}
elem++;
@@ -383,7 +384,7 @@ void STObject::setValueFieldU8(SOE_Field field, unsigned char v)
id = rf->getSType();
}
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);
}
@@ -399,7 +400,7 @@ void STObject::setValueFieldU16(SOE_Field field, uint16 v)
id = rf->getSType();
}
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);
}
@@ -415,7 +416,7 @@ void STObject::setValueFieldU32(SOE_Field field, uint32 v)
id = rf->getSType();
}
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);
}
@@ -431,7 +432,7 @@ void STObject::setValueFieldU64(SOE_Field field, uint64 v)
id = rf->getSType();
}
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);
}
@@ -447,7 +448,7 @@ void STObject::setValueFieldH160(SOE_Field field, const uint160& v)
id = rf->getSType();
}
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);
}
@@ -463,7 +464,7 @@ void STObject::setValueFieldVL(SOE_Field field, const std::vector<unsigned char>
id=rf->getSType();
}
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);
}
@@ -479,7 +480,7 @@ void STObject::setValueFieldTL(SOE_Field field, const std::vector<TaggedListItem
id=rf->getSType();
}
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);
}
@@ -487,7 +488,8 @@ Json::Value STObject::getJson(int options) const
{
Json::Value ret(Json::objectValue);
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)
{

View File

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

View File

@@ -4,7 +4,7 @@
SerializedTransaction::SerializedTransaction(TransactionType 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 STVariableLength("SigningAccount"));
@@ -20,12 +20,12 @@ SerializedTransaction::SerializedTransaction(SerializerIterator& sit, int length
if (length == -1) length=sit.getBytesLeft();
else if (length == 0) length=sit.get32();
if ( (length < TransactionMinLen) || (length > TransactionMaxLen) )
throw(std::runtime_error("Transaction length invalid"));
throw std::runtime_error("Transaction length invalid");
mSignature.setValue(sit.getVL());
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 STVariableLength("SigningAccount", sit.getVL()));
@@ -34,7 +34,7 @@ SerializedTransaction::SerializedTransaction(SerializerIterator& sit, int length
int type = sit.get32();
mMiddleTxn.giveObject(new STUInt32("Type", 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()));
mInnerTxn = STObject(mFormat->elements, sit, "InnerTransaction");
@@ -132,42 +132,42 @@ void SerializedTransaction::setSignature(const std::vector<unsigned char>& sig)
uint32 SerializedTransaction::getVersion() const
{
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();
}
void SerializedTransaction::setVersion(uint32 ver)
{
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);
}
uint64 SerializedTransaction::getTransactionFee() const
{
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();
}
void SerializedTransaction::setTransactionFee(uint64 fee)
{
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);
}
uint32 SerializedTransaction::getSequence() const
{
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();
}
void SerializedTransaction::setSequence(uint32 seq)
{
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);
}
@@ -175,7 +175,7 @@ std::vector<unsigned char> SerializedTransaction::getSigningAccount() const
{
const STVariableLength* v =
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();
}
@@ -183,21 +183,21 @@ const std::vector<unsigned char>& SerializedTransaction::peekSigningAccount() co
{
const STVariableLength* v=
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();
}
std::vector<unsigned char>& SerializedTransaction::peekSigningAccount()
{
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();
}
void SerializedTransaction::setSigningAccount(const std::vector<unsigned char>& s)
{
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);
}

View File

@@ -64,6 +64,13 @@ int Serializer::addRaw(const std::vector<unsigned char> &vector)
return ret;
}
int Serializer::addRaw(const Serializer &s)
{
int ret = mData.size();
mData.insert(mData.end(), s.begin(), s.end());
return ret;
}
int Serializer::addRaw(const void *ptr, int len)
{
int ret = mData.size();
@@ -74,7 +81,7 @@ int Serializer::addRaw(const void *ptr, int len)
bool Serializer::get16(uint16& o, int offset) const
{
if ((offset + 2) > mData.size()) return false;
o=mData.at(offset++);
o = mData.at(++offset);
o <<= 8; o |= mData.at(offset);
return true;
}
@@ -82,8 +89,8 @@ bool Serializer::get16(uint16& o, int offset) const
bool Serializer::get32(uint32& o, int offset) const
{
if ((offset + 4) > mData.size()) return false;
o=mData.at(offset++);
o<<=8; o|=mData.at(offset++); o<<=8; 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);
return true;
}
@@ -91,10 +98,10 @@ bool Serializer::get32(uint32& o, int offset) const
bool Serializer::get64(uint64& o, int offset) const
{
if ((offset + 8) > mData.size()) return false;
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=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;
}
@@ -315,7 +322,7 @@ int Serializer::getTaggedListLength(const std::vector<TaggedListItem>& list)
bool Serializer::getVL(std::vector<unsigned char>& objectVL, int offset, int& length) const
{
int b1;
if(!get8(b1, offset++)) return false;
if (!get8(b1, ++offset)) return false;
int datLen, lenLen = decodeLengthLength(b1);
try
@@ -325,14 +332,14 @@ bool Serializer::getVL(std::vector<unsigned char>& objectVL, int offset, int& le
else if (lenLen == 2)
{
int b2;
if(!get8(b2, offset++)) return false;
if (!get8(b2, ++offset)) return false;
datLen=decodeVLLength(b1, b2);
}
else if (lenLen == 3)
{
int b2, b3;
if(!get8(b2, offset++)) return false;
if(!get8(b3, offset++)) return false;
if (!get8(b2, ++offset)) return false;
if (!get8(b3, ++offset)) return false;
datLen = decodeVLLength(b1, b2, b3);
}
else return false;
@@ -348,7 +355,7 @@ bool Serializer::getVL(std::vector<unsigned char>& objectVL, int offset, int& le
bool Serializer::getVLLength(int& length, int offset) const
{
int b1;
if(!get8(b1, offset++)) return false;
if (!get8(b1, ++offset)) return false;
int lenLen = decodeLengthLength(b1);
try
@@ -358,14 +365,14 @@ bool Serializer::getVLLength(int& length, int offset) const
else if (lenLen == 2)
{
int b2;
if(!get8(b2, offset++)) return false;
if (!get8(b2, ++offset)) return false;
length=decodeVLLength(b1, b2);
}
else if (lenLen == 3)
{
int b2, b3;
if(!get8(b2, offset++)) return false;
if(!get8(b3, offset++)) return false;
if (!get8(b2, ++offset)) return false;
if (!get8(b3, ++offset)) return false;
length = decodeVLLength(b1, b2, b3);
}
else return false;
@@ -382,12 +389,12 @@ bool Serializer::getTaggedList(std::list<TaggedListItem>& list, int offset, int&
list.clear();
int startOffset = offset;
int numElem;
if(!get8(numElem, offset++)) return false;
if (!get8(numElem, ++offset)) return false;
for (int i = 0; i<numElem; i++)
{
int tag, len;
std::vector<unsigned char> data;
if(!get8(tag, offset++)) return false;
if (!get8(tag, ++offset)) return false;
if (!getVL(data, offset, len)) return false;
offset += len;
list.push_back(std::make_pair(tag, data));
@@ -401,12 +408,12 @@ bool Serializer::getTaggedList(std::vector<TaggedListItem>& list, int offset, in
list.clear();
int startOffset=offset;
int numElem;
if(!get8(numElem, offset++)) return false;
if (!get8(numElem, ++offset)) return false;
for (int i=0; i<numElem; i++)
{
int tag, len;
std::vector<unsigned char> data;
if(!get8(tag, offset++)) return false;
if (!get8(tag, ++offset)) return false;
if (!getVL(data, offset, len)) return false;
offset += len;
list.push_back(std::make_pair(tag, data));
@@ -438,45 +445,45 @@ std::vector<unsigned char> Serializer::encodeVL(int length)
lenBytes[2] = static_cast<unsigned char>(length & 0xff);
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)
{
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 <= 12480) return 2;
if (length >= 918744) return 3;
throw(std::overflow_error("len>918644"));
throw std::overflow_error("len>918644");
}
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 <= 240) return 2;
if (b1 <= 254) return 3;
throw(std::overflow_error("b1>254"));
throw std::overflow_error("b1>254");
}
int Serializer::decodeVLLength(int b1)
{
if(b1<0) throw(std::overflow_error("b1<0"));
if(b1>254) throw(std::overflow_error("b1>254"));
if (b1 < 0) throw std::overflow_error("b1<0");
if (b1 > 254) throw std::overflow_error("b1>254");
return b1;
}
int Serializer::decodeVLLength(int b1, int b2)
{
if(b1<193) throw(std::overflow_error("b1<193"));
if(b1>240) throw(std::overflow_error("b1>240"));
if (b1 < 193) throw std::overflow_error("b1<193");
if (b1 > 240) throw std::overflow_error("b1>240");
return 193 + (b1 - 193) * 256 + b2;
}
int Serializer::decodeVLLength(int b1, int b2, int b3)
{
if(b1<241) throw(std::overflow_error("b1<241"));
if(b1>254) throw(std::overflow_error("b1>254"));
if (b1 < 241) throw std::overflow_error("b1<241");
if (b1 > 254) throw std::overflow_error("b1>254");
return 12481 + (b1 - 241) * 65536 + b2 * 256 + b3;
}
@@ -493,7 +500,7 @@ int SerializerIterator::getBytesLeft()
unsigned char SerializerIterator::get8()
{
int val;
if(!mSerializer.get8(val, mPos)) throw(0);
if (!mSerializer.get8(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos++;
return val;
}
@@ -501,7 +508,7 @@ unsigned char SerializerIterator::get8()
uint16 SerializerIterator::get16()
{
uint16 val;
if(!mSerializer.get16(val, mPos)) throw(0);
if (!mSerializer.get16(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos += 16/8;
return val;
}
@@ -509,7 +516,7 @@ uint16 SerializerIterator::get16()
uint32 SerializerIterator::get32()
{
uint32 val;
if(!mSerializer.get32(val, mPos)) throw(0);
if (!mSerializer.get32(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos += 32/8;
return val;
}
@@ -517,7 +524,7 @@ uint32 SerializerIterator::get32()
uint64 SerializerIterator::get64()
{
uint64 val;
if(!mSerializer.get64(val, mPos)) throw(0);
if (!mSerializer.get64(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos += 64/8;
return val;
}
@@ -525,7 +532,7 @@ uint64 SerializerIterator::get64()
uint128 SerializerIterator::get128()
{
uint128 val;
if(!mSerializer.get128(val, mPos)) throw(0);
if (!mSerializer.get128(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos += 128/8;
return val;
}
@@ -533,7 +540,7 @@ uint128 SerializerIterator::get128()
uint160 SerializerIterator::get160()
{
uint160 val;
if(!mSerializer.get160(val, mPos)) throw(0);
if (!mSerializer.get160(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos += 160/8;
return val;
}
@@ -541,7 +548,7 @@ uint160 SerializerIterator::get160()
uint256 SerializerIterator::get256()
{
uint256 val;
if(!mSerializer.get256(val, mPos)) throw(0);
if (!mSerializer.get256(val, mPos)) throw std::runtime_error("invalid serializer get");
mPos += 256/8;
return val;
}
@@ -550,7 +557,7 @@ std::vector<unsigned char> SerializerIterator::getVL()
{
int length;
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;
return vl;
}
@@ -559,7 +566,7 @@ std::vector<TaggedListItem> SerializerIterator::getTaggedList()
{
int length;
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;
return tl;
}

View File

@@ -35,6 +35,7 @@ class Serializer
int add256(const uint256&); // transaction and ledger hashes
int addRaw(const std::vector<unsigned char> &vector);
int addRaw(const void *ptr, int len);
int addRaw(const Serializer& s);
int addVL(const std::vector<unsigned char> &vector);
int addVL(const void *ptr, int len);
@@ -61,7 +62,6 @@ class Serializer
bool getTaggedList(std::list<TaggedListItem>&, int offset, int& length) const;
bool getTaggedList(std::vector<TaggedListItem>&, int offset, int& length) const;
// hash functions
uint160 getRIPEMD160(int size=-1) const;
uint256 getSHA256(int size=-1) const;
@@ -82,6 +82,13 @@ class Serializer
int removeLastByte();
bool chop(int num);
// vector-like functions
std::vector<unsigned char>::iterator begin() { return mData.begin(); }
std::vector<unsigned char>::iterator end() { return mData.end(); }
std::vector<unsigned char>::const_iterator begin() const { return mData.begin(); }
std::vector<unsigned char>::const_iterator end() const { return mData.end(); }
std::vector<unsigned char>::size_type size() const { return mData.size(); }
// signature functions
bool checkSignature(int pubkeyOffset, int signatureOffset) const;
bool checkSignature(const std::vector<unsigned char>& signature, CKey& rkey) const;

View File

@@ -52,18 +52,14 @@ public:
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getTargetSize() const
{
mLock.lock();
int ret=mTargetSize;
mLock.unlock();
return ret;
boost::recursive_mutex::scoped_lock sl(mLock);
return mTargetSize;
}
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getTargetAge() const
{
mLock.lock();
int ret=mTargetAge;
mLock.unlock();
return ret;
boost::recursive_mutex::scoped_lock sl(mLock);
return mTargetAge;
}
template<typename c_Key, typename c_Data> int TaggedCache<c_Key, c_Data>::getCacheSize()

View File

@@ -2,6 +2,8 @@
#define __TRANSACTIONENGINE__
#include "Ledger.h"
#include "Ledger.h"
#include "Currency.h"
#include "SerializedTransaction.h"
#include "SerializedLedger.h"
@@ -32,7 +34,7 @@ enum TransactionEngineParams
class TransactionEngine
{
protected:
Ledger::pointer mTargetLedger;
Ledger::pointer mLedger;
TransactionEngineResult doPayment(const SerializedTransaction&, SerializedLedgerEntry& source);
TransactionEngineResult doInvoice(const SerializedTransaction&, SerializedLedgerEntry& source);
@@ -43,10 +45,11 @@ protected:
TransactionEngineResult doDelete(const SerializedTransaction&, SerializedLedgerEntry& source);
public:
TransactionEngine(Ledger::pointer targetLedger) : mTargetLedger(targetLedger) { ; }
TransactionEngine() { ; }
TransactionEngine(Ledger::pointer ledger) : mLedger(ledger) { ; }
Ledger::pointer getTargetLedger() { return mTargetLedger; }
void setTargetLedger(Ledger::pointer targetLedger) { mTargetLedger = targetLedger; }
Ledger::pointer getLedger() { return mLedger; }
void setLedger(Ledger::pointer ledger) { mLedger = ledger; }
TransactionEngineResult applyTransaction(const SerializedTransaction&, TransactionEngineParams);
};