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

This commit is contained in:
JoelKatz
2012-08-13 02:19:42 -07:00
6 changed files with 1712 additions and 508 deletions

View File

@@ -15,7 +15,7 @@ void LedgerEntrySet::clear()
mSet.clear();
}
LedgerEntrySet LedgerEntrySet::duplicate()
LedgerEntrySet LedgerEntrySet::duplicate() const
{
return LedgerEntrySet(mEntries, mSet, mSeq + 1);
}
@@ -189,6 +189,11 @@ void LedgerEntrySet::entryDelete(SLE::pointer& sle, bool unfunded)
}
}
bool LedgerEntrySet::intersect(const LedgerEntrySet& lesLeft, const LedgerEntrySet& lesRight)
{
return true; // XXX Needs implementation
}
Json::Value LedgerEntrySet::getJson(int) const
{
Json::Value ret(Json::objectValue);

View File

@@ -34,14 +34,14 @@ protected:
TransactionMetaSet mSet;
int mSeq;
LedgerEntrySet(const boost::unordered_map<uint256, LedgerEntrySetEntry> &e, TransactionMetaSet& s, int m) :
LedgerEntrySet(const boost::unordered_map<uint256, LedgerEntrySetEntry> &e, const TransactionMetaSet& s, int m) :
mEntries(e), mSet(s), mSeq(m) { ; }
public:
LedgerEntrySet() : mSeq(0) { ; }
// set functions
LedgerEntrySet duplicate(); // Make a duplicate of this set
LedgerEntrySet duplicate() const; // Make a duplicate of this set
void setTo(LedgerEntrySet&); // Set this set to have the same contents as another
void swapWith(LedgerEntrySet&); // Swap the contents of two sets
@@ -67,6 +67,8 @@ public:
boost::unordered_map<uint256, LedgerEntrySetEntry>::const_iterator end() const { return mEntries.end(); }
boost::unordered_map<uint256, LedgerEntrySetEntry>::iterator begin() { return mEntries.begin(); }
boost::unordered_map<uint256, LedgerEntrySetEntry>::iterator end() { return mEntries.end(); }
static bool intersect(const LedgerEntrySet& lesLeft, const LedgerEntrySet& lesRight);
};
#endif

View File

@@ -300,34 +300,48 @@ STPathSet* STPathSet::construct(SerializerIterator& s, const char *name)
{
std::vector<STPath> paths;
std::vector<STPathElement> path;
do
{
switch(s.get8())
int iType = s.get8();
if (iType == STPathElement::typeEnd || iType == STPathElement::typeBoundary)
{
case STPathElement::typeEnd:
if (path.empty())
{
if (!paths.empty())
throw std::runtime_error("empty last path");
}
else paths.push_back(path);
if (path.empty())
throw std::runtime_error("empty path");
paths.push_back(path);
path.clear();
if (iType == STPathElement::typeEnd)
{
return new STPathSet(name, paths);
}
}
else if (iType & ~STPathElement::typeValidBits)
{
throw std::runtime_error("bad path element");
}
else
{
bool bAccount = !!(iType & STPathElement::typeAccount);
bool bCurrency = !!(iType & STPathElement::typeCurrency);
bool bIssuer = !!(iType & STPathElement::typeIssuer);
case STPathElement::typeBoundary:
if (path.empty())
throw std::runtime_error("empty path");
paths.push_back(path);
path.clear();
uint160 uAccountID;
uint160 uCurrency;
uint160 uIssuerID;
case STPathElement::typeAccount:
path.push_back(STPathElement(STPathElement::typeAccount, s.get160()));
break;
if (bAccount)
uAccountID = s.get160();
case STPathElement::typeOffer:
path.push_back(STPathElement(STPathElement::typeOffer, s.get160()));
break;
if (bCurrency)
uCurrency = s.get160();
default: throw std::runtime_error("Unknown path element");
if (bIssuer)
uIssuerID = s.get160();
path.push_back(STPathElement(uAccountID, uCurrency, uIssuerID));
}
} while(1);
}
@@ -335,8 +349,10 @@ STPathSet* STPathSet::construct(SerializerIterator& s, const char *name)
int STPathSet::getLength() const
{
int ret = 0;
for (std::vector<STPath>::const_iterator it = value.begin(), end = value.end(); it != end; ++it)
ret += it->getSerializeSize();
return (ret != 0) ? ret : (ret + 1);
}
@@ -346,30 +362,22 @@ Json::Value STPath::getJson(int) const
BOOST_FOREACH(std::vector<STPathElement>::const_iterator::value_type it, mPath)
{
switch (it.getNodeType())
{
case STPathElement::typeAccount:
{
Json::Value elem(Json::objectValue);
Json::Value elem(Json::objectValue);
int iType = it.getNodeType();
elem["account"] = NewcoinAddress::createHumanAccountID(it.getNode());
elem["type"] = it.getNodeType();
elem["type_hex"] = strHex(it.getNodeType());
ret.append(elem);
break;
}
if (iType & STPathElement::typeAccount)
elem["account"] = NewcoinAddress::createHumanAccountID(it.getAccountID());
case STPathElement::typeOffer:
{
Json::Value elem(Json::objectValue);
if (iType & STPathElement::typeCurrency)
elem["currency"] = STAmount::createHumanCurrency(it.getCurrency());
elem["offer"] = it.getNode().GetHex();
if (iType & STPathElement::typeIssuer)
elem["issuer"] = NewcoinAddress::createHumanAccountID(it.getIssuerID());
ret.append(elem);
break;
}
default: throw std::runtime_error("Unknown path element");
}
ret.append(elem);
}
return ret;
@@ -385,12 +393,13 @@ Json::Value STPathSet::getJson(int options) const
return ret;
}
#if 0
std::string STPath::getText() const
{
std::string ret("[");
bool first = true;
BOOST_FOREACH(std::vector<STPathElement>::const_iterator::value_type it, mPath)
BOOST_FOREACH(const STPathElement& it, mPath)
{
if (!first) ret += ", ";
switch (it.getNodeType())
@@ -416,7 +425,9 @@ std::string STPath::getText() const
return ret + "]";
}
#endif
#if 0
std::string STPathSet::getText() const
{
std::string ret("{");
@@ -433,23 +444,34 @@ std::string STPathSet::getText() const
}
return ret + "}";
}
#endif
void STPathSet::add(Serializer& s) const
{
bool firstPath = true;
bool bFirst = true;
BOOST_FOREACH(std::vector<STPath>::const_iterator::value_type pit, value)
BOOST_FOREACH(const STPath& spPath, value)
{
if (!firstPath)
if (!bFirst)
{
s.add8(STPathElement::typeBoundary);
firstPath = false;
bFirst = false;
}
for (std::vector<STPathElement>::const_iterator eit = pit.begin(), eend = pit.end(); eit != eend; ++eit)
BOOST_FOREACH(const STPathElement& speElement, spPath)
{
s.add8(eit->getNodeType());
s.add160(eit->getNode());
int iType = speElement.getNodeType();
s.add8(iType);
if (iType & STPathElement::typeAccount)
s.add160(speElement.getAccountID());
if (iType & STPathElement::typeCurrency)
s.add160(speElement.getCurrency());
if (iType & STPathElement::typeIssuer)
s.add160(speElement.getIssuerID());
}
}
s.add8(STPathElement::typeEnd);

View File

@@ -520,26 +520,41 @@ public:
class STPathElement
{
public:
static const int typeEnd = 0x00;
static const int typeAccount = 0x01; // Rippling through an account
static const int typeOffer = 0x02; // Claiming an offer
static const int typeBoundary = 0xFF; // boundary between alternate paths
enum {
typeEnd = 0x00,
typeAccount = 0x01, // Rippling through an account (vs taking an offer).
typeRedeem = 0x04, // Redeem IOUs.
typeIssue = 0x08, // Issue IOUs.
typeCurrency = 0x10, // Currency follows.
typeIssuer = 0x20, // Issuer follows.
typeBoundary = 0xFF, // Boundary between alternate paths.
typeValidBits = 0x3E, // Bits that may be non-zero.
};
protected:
int mType;
uint160 mNode;
int mType;
uint160 mAccountID;
uint160 mCurrency;
uint160 mIssuerID;
public:
STPathElement(int type, const uint160& node) : mType(type), mNode(node) { ; }
int getNodeType() const { return mType; }
bool isAccount() const { return mType == typeAccount; }
bool isOffer() const { return mType == typeOffer; }
STPathElement(const uint160& uAccountID, const uint160& uCurrency, const uint160& uIssuerID)
: mAccountID(uAccountID), mCurrency(uCurrency), mIssuerID(uIssuerID)
{
mType =
(uAccountID.isZero() ? 0 : STPathElement::typeAccount)
| (uCurrency.isZero() ? 0 : STPathElement::typeCurrency)
| (uIssuerID.isZero() ? 0 : STPathElement::typeIssuer);
}
int getNodeType() const { return mType; }
bool isOffer() const { return mAccountID.isZero(); }
bool isAccount() const { return !isOffer(); }
// Nodes are either an account ID or a offer prefix. Offer prefixs denote a class of offers.
const uint160& getNode() const { return mNode; }
void setType(int type) { mType = type; }
void setNode(const uint160& n) { mNode = n; }
const uint160& getAccountID() const { return mAccountID; }
const uint160& getCurrency() const { return mCurrency; }
const uint160& getIssuerID() const { return mIssuerID; }
};
class STPath
@@ -558,12 +573,50 @@ public:
void addElement(const STPathElement& e) { mPath.push_back(e); }
void clear() { mPath.clear(); }
int getSerializeSize() const { return 1 + mPath.size() * 21; }
std::string getText() const;
// std::string getText() const;
Json::Value getJson(int) const;
std::vector<STPathElement>::iterator begin() { return mPath.begin(); }
std::vector<STPathElement>::iterator end() { return mPath.end(); }
std::vector<STPathElement>::const_iterator begin() const { return mPath.begin(); }
std::vector<STPathElement>::const_iterator end() const { return mPath.end(); }
};
inline std::vector<STPathElement>::iterator range_begin(STPath & x)
{
return x.begin();
}
inline std::vector<STPathElement>::iterator range_end(STPath & x)
{
return x.end();
}
inline std::vector<STPathElement>::const_iterator range_begin(const STPath& x)
{
return x.begin();
}
inline std::vector<STPathElement>::const_iterator range_end(const STPath& x)
{
return x.end();
}
namespace boost
{
template<>
struct range_mutable_iterator< STPath >
{
typedef std::vector<STPathElement>::iterator type;
};
template<>
struct range_const_iterator< STPath >
{
typedef std::vector<STPathElement>::const_iterator type;
};
}
class STPathSet : public SerializedType
{ // A set of zero or more payment paths
protected:
@@ -582,7 +635,7 @@ public:
{ return std::auto_ptr<SerializedType>(construct(sit, name)); }
int getLength() const;
std::string getText() const;
// std::string getText() const;
void add(Serializer& s) const;
virtual Json::Value getJson(int) const;

File diff suppressed because it is too large Load Diff

View File

@@ -27,6 +27,7 @@ enum TransactionEngineResult
tenBAD_GEN_AUTH,
tenBAD_ISSUER,
tenBAD_OFFER,
tenBAD_PATH,
tenBAD_PATH_COUNT,
tenBAD_PUBLISH,
tenBAD_SET_ID,
@@ -99,20 +100,71 @@ enum TransactionEngineParams
tepMETADATA = 5, // put metadata in tree, not transaction
};
typedef struct {
uint16 uFlags; // --> From path.
uint160 uAccountID; // --> Recieving/sending account.
uint160 uCurrencyID; // --> Currency to recieve.
// --- For offer's next has currency out.
uint160 uIssuerID; // --> Currency's issuer
// Computed by Reverse.
STAmount saRevRedeem; // <-- Amount to redeem to next.
STAmount saRevIssue; // <-- Amount to issue to next limited by credit and outstanding IOUs.
// Issue isn't used by offers.
STAmount saRevDeliver; // <-- Amount to deliver to next regardless of fee.
// Computed by forward.
STAmount saFwdRedeem; // <-- Amount node will redeem to next.
STAmount saFwdIssue; // <-- Amount node will issue to next.
// Issue isn't used by offers.
STAmount saFwdDeliver; // <-- Amount to deliver to next regardless of fee.
} paymentNode;
// Hold a path state under incremental application.
class PathState
{
protected:
bool pushNode(int iType, uint160 uAccountID, uint160 uCurrencyID, uint160 uIssuerID);
public:
typedef boost::shared_ptr<PathState> pointer;
int mIndex;
uint64 uQuality; // 0 = none.
STAmount saIn;
STAmount saOut;
std::vector<paymentNode> vpnNodes;
LedgerEntrySet lesEntries;
PathState(int iIndex) : mIndex(iIndex) { ; };
int mIndex;
uint64 uQuality; // 0 = none.
STAmount saInReq; // Max amount to spend by sender
STAmount saInAct; // Amount spent by sender (calc output)
STAmount saOutReq; // Amount to send (calc input)
STAmount saOutAct; // Amount actually sent (calc output).
bool bDirty; // Path not computed.
static PathState::pointer createPathState(int iIndex) { return boost::make_shared<PathState>(iIndex); };
PathState(
int iIndex,
const LedgerEntrySet& lesSource,
const STPath& spSourcePath,
uint160 uReceiverID,
uint160 uSenderID,
STAmount saSend,
STAmount saSendMax,
bool bPartialPayment
);
static PathState::pointer createPathState(
int iIndex,
const LedgerEntrySet& lesSource,
const STPath& spSourcePath,
uint160 uReceiverID,
uint160 uSenderID,
STAmount saSend,
STAmount saSendMax,
bool bPartialPayment
)
{ return boost::make_shared<PathState>(iIndex, lesSource, spSourcePath, uReceiverID, uSenderID, saSend, saSendMax, bPartialPayment); };
static bool lessPriority(const PathState::pointer& lhs, const PathState::pointer& rhs);
};
// One instance per ledger.
@@ -137,25 +189,6 @@ private:
bool dirNext(const uint256& uRootIndex, SLE::pointer& sleNode, unsigned int& uDirEntry, uint256& uEntryIndex);
#ifdef WORK_IN_PROGRESS
typedef struct {
uint16 uFlags; // --> from path
STAccount saAccount; // --> recieving/sending account
STAmount saWanted; // --> What this node wants from upstream.
// Maybe this should just be a bool:
STAmount saIOURedeemMax; // --> Max amount of IOUs to redeem downstream.
// Maybe this should just be a bool:
STAmount saIOUIssueMax; // --> Max Amount of IOUs to issue downstream.
STAmount saIOURedeem; // <-- What this node will redeem downstream.
STAmount saIOUIssue; // <-- What this node will issue downstream.
STAmount saSend; // <-- Stamps this node will send downstream.
STAmount saRecieve; // Amount stamps to receive.
} paymentNode;
typedef struct {
std::vector<paymentNode> vpnNodes;
@@ -176,34 +209,49 @@ private:
STAmount& saTakerGot);
protected:
Ledger::pointer mLedger;
uint64 mLedgerParentCloseTime;
Ledger::pointer mLedger;
uint64 mLedgerParentCloseTime;
uint160 mTxnAccountID;
SLE::pointer mTxnAccount;
uint160 mTxnAccountID;
SLE::pointer mTxnAccount;
boost::unordered_set<uint256> mUnfunded; // Indexes that were found unfunded.
SLE::pointer entryCreate(LedgerEntryType letType, const uint256& uIndex);
SLE::pointer entryCache(LedgerEntryType letType, const uint256& uIndex);
void entryDelete(SLE::pointer sleEntry, bool unfunded = false);
void entryModify(SLE::pointer sleEntry);
SLE::pointer entryCreate(LedgerEntryType letType, const uint256& uIndex);
SLE::pointer entryCache(LedgerEntryType letType, const uint256& uIndex);
void entryDelete(SLE::pointer sleEntry, bool unfunded = false);
void entryModify(SLE::pointer sleEntry);
void entryReset();
void entryReset();
STAmount rippleHolds(const uint160& uAccountID, const uint160& uCurrency, const uint160& uIssuerID);
STAmount rippleTransit(const uint160& uSenderID, const uint160& uReceiverID, const uint160& uIssuerID, const STAmount& saAmount);
STAmount rippleSend(const uint160& uSenderID, const uint160& uReceiverID, const STAmount& saAmount);
uint32 rippleTransfer(const uint160& uIssuerID);
STAmount rippleBalance(const uint160& uToAccountID, const uint160& uFromAccountID, const uint160& uCurrencyID);
STAmount rippleLimit(const uint160& uToAccountID, const uint160& uFromAccountID, const uint160& uCurrencyID);
uint32 rippleQualityIn(const uint160& uToAccountID, const uint160& uFromAccountID, const uint160& uCurrencyID);
uint32 rippleQualityOut(const uint160& uToAccountID, const uint160& uFromAccountID, const uint160& uCurrencyID);
STAmount accountHolds(const uint160& uAccountID, const uint160& uCurrency, const uint160& uIssuerID);
STAmount accountSend(const uint160& uSenderID, const uint160& uReceiverID, const STAmount& saAmount);
STAmount accountFunds(const uint160& uAccountID, const STAmount& saDefault);
STAmount rippleHolds(const uint160& uAccountID, const uint160& uCurrencyID, const uint160& uIssuerID);
STAmount rippleTransfer(const uint160& uSenderID, const uint160& uReceiverID, const uint160& uIssuerID, const STAmount& saAmount);
void rippleCredit(const uint160& uSenderID, const uint160& uReceiverID, const STAmount& saAmount);
STAmount rippleSend(const uint160& uSenderID, const uint160& uReceiverID, const STAmount& saAmount);
STAmount accountHolds(const uint160& uAccountID, const uint160& uCurrencyID, const uint160& uIssuerID);
STAmount accountSend(const uint160& uSenderID, const uint160& uReceiverID, const STAmount& saAmount);
STAmount accountFunds(const uint160& uAccountID, const STAmount& saDefault);
PathState::pointer pathCreate(const STPath& spPath);
void pathApply(PathState::pointer pspCur);
void pathNext(PathState::pointer pspCur);
void pathNext(PathState::pointer pspCur, int iPaths);
bool calcNode(unsigned int uIndex, PathState::pointer pspCur, bool bMultiQuality);
bool calcNodeOfferRev(unsigned int uIndex, PathState::pointer pspCur, bool bMultiQuality);
bool calcNodeOfferFwd(unsigned int uIndex, PathState::pointer pspCur, bool bMultiQuality);
bool calcNodeAccountRev(unsigned int uIndex, PathState::pointer pspCur, bool bMultiQuality);
bool calcNodeAccountFwd(unsigned int uIndex, PathState::pointer pspCur, bool bMultiQuality);
void calcNodeRipple(const uint32 uQualityIn, const uint32 uQualityOut,
const STAmount& saPrvReq, const STAmount& saCurReq,
STAmount& saPrvAct, STAmount& saCurAct);
void txnWrite();
void txnWrite();
TransactionEngineResult offerDelete(const SLE::pointer& sleOffer, const uint256& uOfferIndex, const uint160& uOwnerID);