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"
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,
@@ -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<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

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)
: STObject("LedgerEntry"), mIndex(index)
{
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;
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");
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);
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<uint16>(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<const SerializedLedgerEntry*>(&t);
if(!v) return false;
if(mType != v->mType) return false;
if(mObject != v->mObject) return false;
const SerializedLedgerEntry* v = dynamic_cast<const SerializedLedgerEntry*>(&t);
if (!v) return false;
if (mType != v->mType) return false;
if (mObject != v->mObject) return false;
return true;
}

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);