More serialization work.

This commit is contained in:
JoelKatz
2012-09-26 16:26:39 -07:00
parent e7d9b99133
commit 3f469c914d
6 changed files with 50 additions and 56 deletions

View File

@@ -6,27 +6,29 @@
#include "Log.h"
SerializedLedgerEntry::SerializedLedgerEntry(SerializerIterator& sit, const uint256& index)
: SerializedType("LedgerEntry"), mIndex(index)
: STObject(sfLedgerEntry), mIndex(index)
{
uint16 type = sit.get16();
set(sit);
uint16 type = getValueFieldU16(sfLedgerEntryType);
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;
mVersion.setValue(type);
mObject = STObject(mFormat->elements, sit);
setType(mType);
}
SerializedLedgerEntry::SerializedLedgerEntry(const Serializer& s, const uint256& index)
: SerializedType("LedgerEntry"), mIndex(index)
: STObject(sfLedgerEntry), mIndex(index)
{
SerializerIterator sit(s);
set(sit);
uint16 type = sit.get16();
uint16 type = getValueFieldU16(sfLedgerEntryType);
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;
mVersion.setValue(type);
mObject.set(mFormat->elements, sit);
setType(mTyhpe);
}
SerializedLedgerEntry::SerializedLedgerEntry(LedgerEntryType type) : SerializedType("LedgerEntry"), mType(type)
@@ -34,7 +36,7 @@ SerializedLedgerEntry::SerializedLedgerEntry(LedgerEntryType type) : SerializedT
mFormat = getLgrFormat(type);
if (mFormat == NULL) throw std::runtime_error("invalid ledger entry type");
mVersion.setValue(static_cast<uint16>(mFormat->t_type));
mObject.set(mFormat->elements);
set(mFormat->elements);
}
std::string SerializedLedgerEntry::getFullText() const
@@ -44,7 +46,7 @@ std::string SerializedLedgerEntry::getFullText() const
ret += "\" = { ";
ret += mFormat->t_name;
ret += ", ";
ret += mObject.getFullText();
ret += getFullText();
ret += "}";
return ret;
}
@@ -53,67 +55,55 @@ std::string SerializedLedgerEntry::getText() const
{
return str(boost::format("{ %s, %s, %s }")
% mIndex.GetHex()
% mVersion.getText()
% mObject.getText());
% STObject::getText());
}
Json::Value SerializedLedgerEntry::getJson(int options) const
{
Json::Value ret(mObject.getJson(options));
Json::Value ret(SerializedObject::getJson(options));
ret["type"] = mFormat->t_name;
ret["index"] = mIndex.GetHex();
ret["version"] = std::string(1, mVersion);
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;
return true;
}
bool SerializedLedgerEntry::isThreadedType()
{
return getIFieldIndex(sfLastTxnID) != -1;
return getFieldIndex(sfLastTxnID) != -1;
}
bool SerializedLedgerEntry::isThreaded()
{
return getIFieldPresent(sfLastTxnID);
return isFieldPresent(sfLastTxnID);
}
uint256 SerializedLedgerEntry::getThreadedTransaction()
{
return getIFieldH256(sfLastTxnID);
return getValueFieldH256(sfLastTxnID);
}
uint32 SerializedLedgerEntry::getThreadedLedger()
{
return getIFieldU32(sfLastTxnSeq);
return getValueFieldU32(sfLastTxnSeq);
}
bool SerializedLedgerEntry::thread(const uint256& txID, uint32 ledgerSeq, uint256& prevTxID, uint32& prevLedgerID)
{
uint256 oldPrevTxID = getIFieldH256(sfLastTxnID);
uint256 oldPrevTxID = getValueFieldH256(sfLastTxnID);
Log(lsTRACE) << "Thread Tx:" << txID << " prev:" << oldPrevTxID;
if (oldPrevTxID == txID)
return false;
prevTxID = oldPrevTxID;
prevLedgerID = getIFieldU32(sfLastTxnSeq);
prevLedgerID = getValueFieldU32(sfLastTxnSeq);
assert(prevTxID != txID);
setIFieldH256(sfLastTxnID, txID);
setIFieldU32(sfLastTxnSeq, ledgerSeq);
setFieldH256(sfLastTxnID, txID);
setFieldU32(sfLastTxnSeq, ledgerSeq);
return true;
}
bool SerializedLedgerEntry::hasOneOwner()
{
return (mType != ltACCOUNT_ROOT) && (getIFieldIndex(sfAccount) != -1);
return (mType != ltACCOUNT_ROOT) && (getFieldIndex(sfAccount) != -1);
}
bool SerializedLedgerEntry::hasTwoOwners()
@@ -123,17 +113,17 @@ bool SerializedLedgerEntry::hasTwoOwners()
NewcoinAddress SerializedLedgerEntry::getOwner()
{
return getIValueFieldAccount(sfAccount);
return getValueFieldAccount(sfAccount);
}
NewcoinAddress SerializedLedgerEntry::getFirstOwner()
{
return getIValueFieldAccount(sfLowID);
return getValueFieldAccount(sfLowID);
}
NewcoinAddress SerializedLedgerEntry::getSecondOwner()
{
return getIValueFieldAccount(sfHighID);
return getValueFieldAccount(sfHighID);
}
std::vector<uint256> SerializedLedgerEntry::getOwners()
@@ -141,9 +131,9 @@ std::vector<uint256> SerializedLedgerEntry::getOwners()
std::vector<uint256> owners;
uint160 account;
for (int i = 0, fields = getIFieldCount(); i < fields; ++i)
for (int i = 0, fields = getCount(); i < fields; ++i)
{
switch (getIFieldSType(i))
switch (getFieldSType(i))
{
case sfAccount:
case sfLowID: