More ledger work:

Ledger::getAccountRoot
LedgerStateParms
SerializedLedgerEntry(const Serialier&..
This commit is contained in:
JoelKatz
2012-04-17 05:06:16 -07:00
parent fb415dc6ac
commit e0e7a822a1
4 changed files with 111 additions and 31 deletions

View File

@@ -18,11 +18,25 @@
#include "SerializedLedger.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> class Ledger : public boost::enable_shared_from_this<Ledger>
{ // The basic Ledger structure, can be opened, closed, or synching { // The basic Ledger structure, can be opened, closed, or synching
public: public:
typedef boost::shared_ptr<Ledger> pointer; typedef boost::shared_ptr<Ledger> pointer;
enum TransResult enum TransResult
{ {
TR_ERROR =-1, TR_ERROR =-1,
@@ -38,10 +52,6 @@ public:
TR_TOOSMALL =9, // amount is less than Tx fee TR_TOOSMALL =9, // amount is less than Tx fee
}; };
enum LedgerStateParms
{
lepCREATE = 1, // Create if not present
};
private: private:
uint256 mHash, mParentHash, mTransHash, mAccountHash; uint256 mHash, mParentHash, mTransHash, mAccountHash;
@@ -113,9 +123,10 @@ public:
Ledger::pointer switchPreviousLedger(Ledger::pointer oldPrevious, Ledger::pointer newPrevious, int limit); Ledger::pointer switchPreviousLedger(Ledger::pointer oldPrevious, Ledger::pointer newPrevious, int limit);
// high-level functions // high-level functions
SerializedLedgerEntry::pointer getAccountRoot(LedgerStateParms parms, const uint160& accountID); LedgerStateParms writeBack(LedgerStateParms parms, SerializedLedgerEntry::pointer);
SerializedLedgerEntry::pointer getNickname(LedgerStateParms parms, const std::string& nickname); SerializedLedgerEntry::pointer getAccountRoot(LedgerStateParms& parms, const uint160& accountID);
SerializedLedgerEntry::pointer getNickname(LedgerStateParms parms, const uint256& nickHash); 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, // SerializedLedgerEntry::pointer getRippleState(LedgerStateParms parms, const uint160& offeror,
// const uint160& borrower, const Currency& currency); // const uint160& borrower, const Currency& currency);
@@ -137,4 +148,14 @@ public:
static bool unitTest(); 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 #endif

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

@@ -4,49 +4,62 @@
SerializedLedgerEntry::SerializedLedgerEntry(SerializerIterator& sit, const uint256& index) SerializedLedgerEntry::SerializedLedgerEntry(SerializerIterator& sit, const uint256& index)
: STObject("LedgerEntry"), mIndex(index) : STObject("LedgerEntry"), mIndex(index)
{ {
uint16 type=sit.get16(); uint16 type = sit.get16();
mFormat=getLgrFormat(static_cast<LedgerEntryType>(type)); mFormat = getLgrFormat(static_cast<LedgerEntryType>(type));
if(mFormat==NULL) throw std::runtime_error("invalid ledger entry type"); if (mFormat == NULL) throw std::runtime_error("invalid ledger entry type");
mType=mFormat->t_type; mType = mFormat->t_type;
mVersion.setValue(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<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) SerializedLedgerEntry::SerializedLedgerEntry(LedgerEntryType type) : STObject("LedgerEntry"), mType(type)
{ {
mFormat=getLgrFormat(type); mFormat = getLgrFormat(type);
if(mFormat==NULL) throw std::runtime_error("invalid ledger entry type"); if (mFormat == NULL) throw std::runtime_error("invalid ledger entry type");
mVersion.setValue(static_cast<uint16>(mFormat->t_type)); mVersion.setValue(static_cast<uint16>(mFormat->t_type));
mObject=STObject(mFormat->elements, "Entry"); mObject = STObject(mFormat->elements, "Entry");
} }
std::string SerializedLedgerEntry::getFullText() const std::string SerializedLedgerEntry::getFullText() const
{ {
std::string ret="\""; std::string ret = "\"";
ret+=mIndex.GetHex(); ret += mIndex.GetHex();
ret+="\" = { "; ret += "\" = { ";
ret+=mFormat->t_name; ret += mFormat->t_name;
ret+=", "; ret += ", ";
ret+=mObject.getFullText(); ret += mObject.getFullText();
ret+="}"; ret += "}";
return ret; return ret;
} }
std::string SerializedLedgerEntry::getText() const std::string SerializedLedgerEntry::getText() const
{ {
std::string ret="{"; std::string ret = "{";
ret+=mIndex.GetHex(); ret += mIndex.GetHex();
ret+=mVersion.getText(); ret += mVersion.getText();
ret+=mObject.getText(); ret += mObject.getText();
ret+="}"; ret += "}";
return ret; return ret;
} }
bool SerializedLedgerEntry::isEquivalent(const SerializedType& t) const bool SerializedLedgerEntry::isEquivalent(const SerializedType& t) const
{ // locators are not compared { // locators are not compared
const SerializedLedgerEntry* v=dynamic_cast<const SerializedLedgerEntry*>(&t); const SerializedLedgerEntry* v = dynamic_cast<const SerializedLedgerEntry*>(&t);
if(!v) return false; if (!v) return false;
if(mType != v->mType) return false; if (mType != v->mType) return false;
if(mObject != v->mObject) return false; if (mObject != v->mObject) return false;
return true; return true;
} }

View File

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