From f9e1ee033b2609afa20c639df4c39a80f7ed0106 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Tue, 21 Aug 2012 08:16:10 -0700 Subject: [PATCH] TxMeta rewrite. --- src/TransactionMeta.cpp | 193 +++++++++++++++++++++------------------- src/TransactionMeta.h | 104 ++++++++++++---------- 2 files changed, 159 insertions(+), 138 deletions(-) diff --git a/src/TransactionMeta.cpp b/src/TransactionMeta.cpp index 0ab28f391e..41bd565405 100644 --- a/src/TransactionMeta.cpp +++ b/src/TransactionMeta.cpp @@ -33,114 +33,119 @@ bool TransactionMetaNodeEntry::operator>=(const TransactionMetaNodeEntry& e) con return compare(e) >= 0; } -TMNEBalance::TMNEBalance(SerializerIterator& sit) : TransactionMetaNodeEntry(TMNChangedBalance) +TMNEThread::TMNEThread(SerializerIterator& sit) : TransactionMetaNodeEntry(TMSThread) { - mFlags = sit.get32(); - mFirstAmount = * dynamic_cast(STAmount::deserialize(sit, "FirstAmount").get()); - if ((mFlags & TMBTwoAmounts) != 0) - mSecondAmount = * dynamic_cast(STAmount::deserialize(sit, "SecondAmount").get()); + mPrevTxID = sit.get256(); + mPrevLgrSeq = sit.get32(); } -void TMNEBalance::addRaw(Serializer& sit) const +void TMNEThread::addRaw(Serializer& sit) const { sit.add8(mType); - sit.add32(mFlags); - mFirstAmount.add(sit); - if ((mFlags & TMBTwoAmounts) != 0) - mSecondAmount.add(sit); + sit.add256(mPrevTxID); + sit.add32(mPrevLgrSeq); } -void TMNEBalance::adjustFirstAmount(const STAmount& a) +int TMNEThread::compare(const TransactionMetaNodeEntry&) const { - mFirstAmount += a; -} - -void TMNEBalance::adjustSecondAmount(const STAmount& a) -{ - mSecondAmount += a; - mFlags |= TMBTwoAmounts; -} - -int TMNEBalance::compare(const TransactionMetaNodeEntry&) const -{ - assert(false); // should never be two TMNEBalance entries for the same node (as of now) + assert(false); // should never be two entries for the same node (as of now) return 0; } -Json::Value TMNEBalance::getJson(int p) const +Json::Value TMNEThread::getJson(int) const { - Json::Value ret(Json::objectValue); + Json::Value inner(Json::objectValue); + inner["prev_transaction"] = mPrevTxID.GetHex(); + inner["prev_ledger_seq"] = mPrevLgrSeq; - if ((mFlags & TMBDestroyed) != 0) - ret["destroyed"] = "true"; - if ((mFlags & TMBPaidFee) != 0) - ret["transaction_fee"] = "true"; - - if ((mFlags & TMBRipple) != 0) - ret["type"] = "ripple"; - else if ((mFlags & TMBOffer) != 0) - ret["type"] = "offer"; - else - ret["type"] = "account"; - - if (!mFirstAmount.isZero()) - ret["amount"] = mFirstAmount.getJson(p); - if (!mSecondAmount.isZero()) - ret["second_amount"] = mSecondAmount.getJson(p); - - return ret; + Json::Value outer(Json::objectValue); + outer["thread"] = inner; + return outer; } -void TMNEUnfunded::addRaw(Serializer& sit) const +TMNEAmount::TMNEAmount(int type, SerializerIterator& sit) : TransactionMetaNodeEntry(type) { - sit.add8(mType); + mPrevAmount = *dynamic_cast(STAmount::deserialize(sit, NULL).get()); // Ouch } -Json::Value TMNEUnfunded::getJson(int) const +void TMNEAmount::addRaw(Serializer& s) const { - return Json::Value("delete_unfunded"); + s.add8(mType); + mPrevAmount.add(s); } -void TMNEUnfunded::setBalances(const STAmount& first, const STAmount& second) +Json::Value TMNEAmount::getJson(int v) const { - firstAmount = first; - secondAmount = second; -} - -int TMNEUnfunded::compare(const TransactionMetaNodeEntry&) const -{ - assert(false); // Can't be two deletes for same node - return 0; -} - -TransactionMetaNode::TransactionMetaNode(const uint256& node, SerializerIterator& sit) : mNode(node) -{ - mNode = sit.get256(); - mPreviousTransaction = sit.get256(); - mPreviousLedger = sit.get32(); - int type; - do + Json::Value outer(Json::objectValue); + switch (mType) { - type = sit.get8(); - if (type == TransactionMetaNodeEntry::TMNChangedBalance) - mEntries.push_back(new TMNEBalance(sit)); - if (type == TransactionMetaNodeEntry::TMNDeleteUnfunded) - mEntries.push_back(new TMNEUnfunded()); - else if (type != TransactionMetaNodeEntry::TMNEndOfMetadata) - throw std::runtime_error("Unparseable metadata"); - } while (type != TransactionMetaNodeEntry::TMNEndOfMetadata); + case TMSPrevBalance: outer["prev_balance"] = mPrevAmount.getJson(v); break; + case TMSPrevTakerPays: outer["prev_taker_pays"] = mPrevAmount.getJson(v); break; + case TMSPrevTakerGets: outer["prev_taker_gets"] = mPrevAmount.getJson(v); break; + case TMSFinalTakerPays: outer["final_taker_pays"] = mPrevAmount.getJson(v); break; + case TMSFinalTakerGets: outer["final_taker_gets"] = mPrevAmount.getJson(v); break; + default: assert(false); + } + return outer; +} + +void TMNEAccount::addRaw(Serializer& sit) const +{ + sit.add8(mType); + sit.add256(mPrevAccount); +} + +Json::Value TMNEAccount::getJson(int) const +{ + Json::Value outer(Json::objectValue); + outer["prev_account"] = mPrevAccount.GetHex(); + return outer; +} + +int TMNEAccount::compare(const TransactionMetaNodeEntry&) const +{ + assert(false); // Can't be two modified accounts of same type for same node + return 0; +} + +TransactionMetaNode::TransactionMetaNode(int type, const uint256& node, SerializerIterator& sit) + : mType(type), mNode(node) +{ + while (1) + { + int nType = sit.get8(); + switch (nType) + { + case TMSEndOfNode: + return; + + case TMSThread: + mEntries.push_back(new TMNEThread(sit)); + break; + + // Nodes that contain an amount + case TMSPrevBalance: + case TMSPrevTakerPays: + case TMSPrevTakerGets: + case TMSFinalTakerPays: + case TMSFinalTakerGets: + mEntries.push_back(new TMNEAmount(nType, sit)); + + case TMSPrevAccount: + mEntries.push_back(new TMNEAccount(nType, sit)); + } + } } void TransactionMetaNode::addRaw(Serializer& s) { + s.add8(mType); s.add256(mNode); - s.add256(mPreviousTransaction); - s.add32(mPreviousLedger); mEntries.sort(); for (boost::ptr_vector::const_iterator it = mEntries.begin(), end = mEntries.end(); it != end; ++it) it->addRaw(s); - s.add8(TransactionMetaNodeEntry::TMNEndOfMetadata); + s.add8(TMSEndOfNode); } TransactionMetaNodeEntry* TransactionMetaNode::findEntry(int nodeType) @@ -152,13 +157,13 @@ TransactionMetaNodeEntry* TransactionMetaNode::findEntry(int nodeType) return NULL; } -TMNEBalance* TransactionMetaNode::findBalance() +TMNEAmount* TransactionMetaNode::findAmount(int nType) { for (boost::ptr_vector::iterator it = mEntries.begin(), end = mEntries.end(); it != end; ++it) - if (it->getType() == TransactionMetaNodeEntry::TMNChangedBalance) - return dynamic_cast(&*it); - TMNEBalance* node = new TMNEBalance(); + if (it->getType() == nType) + return dynamic_cast(&*it); + TMNEAmount* node = new TMNEAmount(nType); mEntries.push_back(node); return node; } @@ -170,18 +175,23 @@ void TransactionMetaNode::addNode(TransactionMetaNodeEntry* node) void TransactionMetaNode::thread(const uint256& prevTx, uint32 prevLgr) { - assert((mPreviousLedger == 0) || (mPreviousLedger == prevLgr)); - assert(mPreviousTransaction.isZero() || (mPreviousTransaction == prevTx)); - mPreviousTransaction = prevTx; - mPreviousLedger = prevLgr; + // WRITEME } Json::Value TransactionMetaNode::getJson(int v) const { Json::Value ret = Json::objectValue; + + switch (mType) + { + case TMNCreatedNode: ret["action"] = "create"; break; + case TMNDeletedNode: ret["action"] = "delete"; break; + case TMNModifiedNode: ret["action"] = "modify"; break; + default: + assert(false); + } + ret["node"] = mNode.GetHex(); - ret["previous_transaction"] = mPreviousTransaction.GetHex(); - ret["previous_ledger"] = mPreviousLedger; Json::Value e = Json::arrayValue; for (boost::ptr_vector::const_iterator it = mEntries.begin(), end = mEntries.end(); @@ -199,13 +209,12 @@ TransactionMetaSet::TransactionMetaSet(uint32 ledger, const std::vector::iterator it = mNodes.begin(), end = mNodes.end(); it != end; ++it) it->second.addRaw(s); - s.add256(uint256()); + s.add8(TMNEndOfMetadata); } Json::Value TransactionMetaSet::getJson(int v) const diff --git a/src/TransactionMeta.h b/src/TransactionMeta.h index d863ccb701..8fe20948bd 100644 --- a/src/TransactionMeta.h +++ b/src/TransactionMeta.h @@ -12,18 +12,36 @@ #include "Serializer.h" #include "SerializedTypes.h" +// master record types +static const int TMNEndOfMetadata = 0x00; +static const int TMNCreatedNode = 0x10; // This transaction created this node +static const int TMNDeletedNode = 0x11; +static const int TMNModifiedNode = 0x12; + +// sub record types - special +static const int TMSEndOfNode = 0x00; +static const int TMSThread = 0x01; // Holds previous TxID and LgrSeq for threading + +// sub record types - containing an amount +static const int TMSPrevBalance = 0x11; // Balances prior to the transaction +static const int TMSPrevTakerPays = 0x12; +static const int TMSPrevTakerGets = 0x13; +static const int TMSFinalTakerPays = 0x14; // Balances at node deletion time +static const int TMSFinalTakerGets = 0x15; + +// sub record types - containing an account (for example, for when a nickname is transferred) +static const int TMSPrevAccount = 0x20; + class TransactionMetaNodeEntry { // a way that a transaction has affected a node public: - typedef boost::shared_ptr pointer; - static const int TMNEndOfMetadata = 0; - static const int TMNChangedBalance = 1; - static const int TMNDeleteUnfunded = 2; - +protected: int mType; + +public: TransactionMetaNodeEntry(int type) : mType(type) { ; } int getType() const { return mType; } @@ -43,52 +61,53 @@ protected: virtual TransactionMetaNodeEntry* duplicate(void) const = 0; }; -class TMNEBalance : public TransactionMetaNodeEntry -{ // a transaction affected the balance of a node -public: - - static const int TMBTwoAmounts = 0x001; - static const int TMBDestroyed = 0x010; - static const int TMBPaidFee = 0x020; - static const int TMBRipple = 0x100; - static const int TMBOffer = 0x200; - +class TMNEThread : public TransactionMetaNodeEntry +{ protected: - unsigned mFlags; - STAmount mFirstAmount, mSecondAmount; + uint256 mPrevTxID; + uint32 mPrevLgrSeq; public: - TMNEBalance() : TransactionMetaNodeEntry(TMNChangedBalance), mFlags(0) { ; } + TMNEThread() : TransactionMetaNodeEntry(TMSThread) { ; } + TMNEThread(SerializerIterator&); - TMNEBalance(SerializerIterator&); virtual void addRaw(Serializer&) const; - - unsigned getFlags() const { return mFlags; } - const STAmount& getFirstAmount() const { return mFirstAmount; } - const STAmount& getSecondAmount() const { return mSecondAmount; } - - void adjustFirstAmount(const STAmount&); - void adjustSecondAmount(const STAmount&); - void setFlags(unsigned flags); - virtual Json::Value getJson(int) const; virtual int compare(const TransactionMetaNodeEntry&) const; - virtual TransactionMetaNodeEntry* duplicate(void) const { return new TMNEBalance(*this); } + virtual TransactionMetaNodeEntry* duplicate(void) const { return new TMNEThread(*this); } }; -class TMNEUnfunded : public TransactionMetaNodeEntry +class TMNEAmount : public TransactionMetaNodeEntry +{ // a transaction affected the balance of a node +protected: + STAmount mPrevAmount; + +public: + TMNEAmount(int type) : TransactionMetaNodeEntry(type) { ; } + + TMNEAmount(int type, SerializerIterator&); + virtual void addRaw(Serializer&) const; + + const STAmount& getAmount() const { return mPrevAmount; } + void setAmount(const STAmount& a) { mPrevAmount = a; } + + virtual Json::Value getJson(int) const; + virtual int compare(const TransactionMetaNodeEntry&) const; + virtual TransactionMetaNodeEntry* duplicate(void) const { return new TMNEAmount(*this); } +}; + +class TMNEAccount : public TransactionMetaNodeEntry { // node was deleted because it was unfunded protected: - STAmount firstAmount, secondAmount; // Amounts left when declared unfunded + uint256 mPrevAccount; + public: - TMNEUnfunded() : TransactionMetaNodeEntry(TMNDeleteUnfunded) { ; } - TMNEUnfunded(const STAmount& f, const STAmount& s) : - TransactionMetaNodeEntry(TMNDeleteUnfunded), firstAmount(f), secondAmount(s) { ; } - void setBalances(const STAmount& firstBalance, const STAmount& secondBalance); + TMNEAccount(int type, uint256 prev) : TransactionMetaNodeEntry(type), mPrevAccount(prev) { ; } + TMNEAccount(int type, SerializerIterator&); virtual void addRaw(Serializer&) const; virtual Json::Value getJson(int) const; virtual int compare(const TransactionMetaNodeEntry&) const; - virtual TransactionMetaNodeEntry* duplicate(void) const { return new TMNEUnfunded(*this); } + virtual TransactionMetaNodeEntry* duplicate(void) const { return new TMNEAccount(*this); } }; inline TransactionMetaNodeEntry* new_clone(const TransactionMetaNodeEntry& s) { return s.clone().release(); } @@ -100,21 +119,17 @@ public: typedef boost::shared_ptr pointer; protected: + int mType; uint256 mNode; - uint256 mPreviousTransaction; - uint32 mPreviousLedger; boost::ptr_vector mEntries; public: TransactionMetaNode(const uint256 &node) : mNode(node) { ; } const uint256& getNode() const { return mNode; } - const uint256& getPreviousTransaction() const { return mPreviousTransaction; } - uint32 getPreviousLedger() const { return mPreviousLedger; } const boost::ptr_vector& peekEntries() const { return mEntries; } TransactionMetaNodeEntry* findEntry(int nodeType); - TMNEBalance* findBalance(); void addNode(TransactionMetaNodeEntry*); bool operator<(const TransactionMetaNode& n) const { return mNode < n.mNode; } @@ -124,14 +139,11 @@ public: void thread(const uint256& prevTx, uint32 prevLgr); - TransactionMetaNode(const uint256&node, SerializerIterator&); + TransactionMetaNode(int type, const uint256& node, SerializerIterator&); void addRaw(Serializer&); Json::Value getJson(int) const; - void threadNode(const uint256& previousTransaction, uint32 previousLedger); - void deleteUnfunded(const STAmount& firstBalance, const STAmount& secondBalance); - void adjustBalance(unsigned flags, const STAmount &amount, bool signedBy); - void adjustBalances(unsigned flags, const STAmount &firstAmt, const STAmount &secondAmt); + TMNEAmount* findAmount(int nodeType); };