From e0e7a822a1f62b2915c0831b9d76bbd11a7f0b61 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Tue, 17 Apr 2012 05:06:16 -0700 Subject: [PATCH] More ledger work: Ledger::getAccountRoot LedgerStateParms SerializedLedgerEntry(const Serialier&.. --- src/Ledger.h | 35 ++++++++++++++++++----- src/LedgerNode.cpp | 45 +++++++++++++++++++++++++++++ src/SerializedLedger.cpp | 61 ++++++++++++++++++++++++---------------- src/SerializedLedger.h | 1 + 4 files changed, 111 insertions(+), 31 deletions(-) create mode 100644 src/LedgerNode.cpp diff --git a/src/Ledger.h b/src/Ledger.h index 668ef93c04..96c9347e34 100644 --- a/src/Ledger.h +++ b/src/Ledger.h @@ -18,11 +18,25 @@ #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 { // The basic Ledger structure, can be opened, closed, or synching public: typedef boost::shared_ptr pointer; + enum TransResult { TR_ERROR =-1, @@ -38,10 +52,6 @@ public: TR_TOOSMALL =9, // amount is less than Tx fee }; - enum LedgerStateParms - { - lepCREATE = 1, // Create if not present - }; private: uint256 mHash, mParentHash, mTransHash, mAccountHash; @@ -113,9 +123,10 @@ public: Ledger::pointer switchPreviousLedger(Ledger::pointer oldPrevious, Ledger::pointer newPrevious, int limit); // high-level functions - 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); + 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); @@ -137,4 +148,14 @@ public: static bool unitTest(); }; +inline LedgerStateParms operator|(const LedgerStateParms& l1, const LedgerStateParms& l2) +{ + return static_cast(static_cast(l1) | static_cast(l2)); +} + +inline LedgerStateParms operator&(const LedgerStateParms& l1, const LedgerStateParms& l2) +{ + return static_cast(static_cast(l1) & static_cast(l2)); +} + #endif diff --git a/src/LedgerNode.cpp b/src/LedgerNode.cpp new file mode 100644 index 0000000000..05156e657a --- /dev/null +++ b/src/LedgerNode.cpp @@ -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(ltACCOUNT_ROOT); + sle->setIndex(nodeID); + return sle; + } + + try + { + SerializedLedgerEntry::pointer sle = + boost::make_shared(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(); + } +} diff --git a/src/SerializedLedger.cpp b/src/SerializedLedger.cpp index 05031b6f74..2f39d73664 100644 --- a/src/SerializedLedger.cpp +++ b/src/SerializedLedger.cpp @@ -4,49 +4,62 @@ SerializedLedgerEntry::SerializedLedgerEntry(SerializerIterator& sit, const uint256& index) : STObject("LedgerEntry"), mIndex(index) { - uint16 type=sit.get16(); - mFormat=getLgrFormat(static_cast(type)); - if(mFormat==NULL) throw std::runtime_error("invalid ledger entry type"); - mType=mFormat->t_type; + uint16 type = sit.get16(); + mFormat = getLgrFormat(static_cast(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"); + 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(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); - if(mFormat==NULL) throw std::runtime_error("invalid ledger entry type"); + mFormat = getLgrFormat(type); + if (mFormat == NULL) throw std::runtime_error("invalid ledger entry type"); mVersion.setValue(static_cast(mFormat->t_type)); - mObject=STObject(mFormat->elements, "Entry"); + mObject = STObject(mFormat->elements, "Entry"); } std::string SerializedLedgerEntry::getFullText() const { - std::string ret="\""; - ret+=mIndex.GetHex(); - ret+="\" = { "; - ret+=mFormat->t_name; - ret+=", "; - ret+=mObject.getFullText(); - ret+="}"; + std::string ret = "\""; + ret += mIndex.GetHex(); + ret += "\" = { "; + ret += mFormat->t_name; + ret += ", "; + ret += mObject.getFullText(); + ret += "}"; return ret; } std::string SerializedLedgerEntry::getText() const { - std::string ret="{"; - ret+=mIndex.GetHex(); - ret+=mVersion.getText(); - ret+=mObject.getText(); - ret+="}"; + std::string ret = "{"; + ret += mIndex.GetHex(); + ret += mVersion.getText(); + ret += mObject.getText(); + ret += "}"; return ret; } bool SerializedLedgerEntry::isEquivalent(const SerializedType& t) const { // locators are not compared - const SerializedLedgerEntry* v=dynamic_cast(&t); - if(!v) return false; - if(mType != v->mType) return false; - if(mObject != v->mObject) return false; + const SerializedLedgerEntry* v = dynamic_cast(&t); + if (!v) return false; + if (mType != v->mType) return false; + if (mObject != v->mObject) return false; return true; } diff --git a/src/SerializedLedger.h b/src/SerializedLedger.h index a952e8b6bb..3634c74881 100644 --- a/src/SerializedLedger.h +++ b/src/SerializedLedger.h @@ -17,6 +17,7 @@ protected: LedgerEntryFormat* mFormat; public: + SerializedLedgerEntry(const Serializer& s, const uint256& index); SerializedLedgerEntry(SerializerIterator& sit, const uint256& index); SerializedLedgerEntry(LedgerEntryType type);