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

@@ -73,7 +73,7 @@ public:
static int compare(SField::ref f1, SField::ref f2); static int compare(SField::ref f1, SField::ref f2);
}; };
extern SField sfInvalid, sfGeneric, sfLedgerEntry, sfTransaction; extern SField sfInvalid, sfGeneric, sfLedgerEntry, sfTransaction, sfValidation;
#define FIELD(name, type, index) extern SField sf##name; #define FIELD(name, type, index) extern SField sf##name;
#define TYPE(name, type, index) #define TYPE(name, type, index)

View File

@@ -71,8 +71,9 @@
FIELD(TransactionHash, HASH256, 3) FIELD(TransactionHash, HASH256, 3)
FIELD(AccountHash, HASH256, 4) FIELD(AccountHash, HASH256, 4)
FIELD(LastTxnID, HASH256, 5) FIELD(LastTxnID, HASH256, 5)
FIELD(WalletLocator, HASH256, 6) FIELD(LedgerIndex, HASH256, 6)
FIELD(PublishHash, HASH256, 7) FIELD(WalletLocator, HASH256, 7)
FIELD(PublishHash, HASH256, 8)
// 256-bit (uncommon) // 256-bit (uncommon)
FIELD(BookDirectory, HASH256, 16) FIELD(BookDirectory, HASH256, 16)

View File

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

View File

@@ -27,7 +27,6 @@ public:
std::string getFullText() const; std::string getFullText() const;
std::string getText() const; std::string getText() const;
Json::Value getJson(int options) const; Json::Value getJson(int options) const;
virtual bool isEquivalent(const SerializedType& t) const;
const uint256& getIndex() const; const uint256& getIndex() const;
void setIndex(const uint256& i); void setIndex(const uint256& i);

View File

@@ -12,11 +12,11 @@ SerializedTransaction::SerializedTransaction(TransactionType type) : mType(type)
mFormat = getTxnFormat(type); mFormat = getTxnFormat(type);
if (mFormat == NULL) throw std::runtime_error("invalid transaction type"); if (mFormat == NULL) throw std::runtime_error("invalid transaction type");
mMiddleTxn.giveObject(new STVariableLength("SigningPubKey")); mMiddleTxn.giveObject(new STVariableLength(sfSigningPubKey));
mMiddleTxn.giveObject(new STAccount("SourceAccount")); mMiddleTxn.giveObject(new STAccount(sfSourceAccount));
mMiddleTxn.giveObject(new STUInt32("Sequence")); mMiddleTxn.giveObject(new STUInt32(sfSequence));
mMiddleTxn.giveObject(new STUInt16("Type", static_cast<uint16>(type))); mMiddleTxn.giveObject(new STUInt16(sfTransactionType, static_cast<uint16>(type)));
mMiddleTxn.giveObject(new STAmount("Fee")); mMiddleTxn.giveObject(new STAmount(sfFee));
mInnerTxn = STObject(mFormat->elements, "InnerTransaction"); mInnerTxn = STObject(mFormat->elements, "InnerTransaction");
} }

View File

@@ -4,24 +4,28 @@
#include "HashPrefixes.h" #include "HashPrefixes.h"
SOElement SerializedValidation::sValidationFormat[] = { SOElement SerializedValidation::sValidationFormat[] = {
{ sfFlags, "Flags", STI_UINT32, SOE_FLAGS, 0 }, { sfFlags, SOE_REQUIRED },
{ sfLedgerHash, "LedgerHash", STI_HASH256, SOE_REQUIRED, 0 }, { sfLedgerHash, SOE_REQUIRED },
{ sfSigningTime, "SignTime", STI_UINT32, SOE_REQUIRED, 0 }, { sfLedgerSequence, SOE_OPTIONAL },
{ sfSigningKey, "SigningKey", STI_VL, SOE_REQUIRED, 0 }, { sfCloseTime, SOE_OPTIONAL },
{ sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 }, { sfLoadFee, SOE_OPTIONAL },
{ sfBaseFee, SOE_OPTIONAL },
{ sfSigningTime, SOE_REQUIRED },
{ sfSigningKey, SOE_REQUIRED },
{ sfInvalid, SOE_END }
}; };
const uint32 SerializedValidation::sFullFlag = 0x00010000; const uint32 SerializedValidation::sFullFlag = 0x00010000;
SerializedValidation::SerializedValidation(SerializerIterator& sit, bool checkSignature) SerializedValidation::SerializedValidation(SerializerIterator& sit, bool checkSignature)
: STObject(sValidationFormat, sit), mSignature(sit, "Signature"), mTrusted(false) : STObject(sValidationFormat, sit, sfValidation), mSignature(sit, sfSignature), mTrusted(false)
{ {
if (checkSignature && !isValid()) throw std::runtime_error("Invalid validation"); if (checkSignature && !isValid()) throw std::runtime_error("Invalid validation");
} }
SerializedValidation::SerializedValidation(const uint256& ledgerHash, uint32 signTime, SerializedValidation::SerializedValidation(const uint256& ledgerHash, uint32 signTime,
const NewcoinAddress& naSeed, bool isFull) const NewcoinAddress& naSeed, bool isFull)
: STObject(sValidationFormat), mSignature("Signature"), mTrusted(false) : STObject(sValidationFormat, sfValidation), mSignature(sfSignature), mTrusted(false)
{ {
setValueFieldH256(sfLedgerHash, ledgerHash); setValueFieldH256(sfLedgerHash, ledgerHash);
setValueFieldU32(sfSigningTime, signTime); setValueFieldU32(sfSigningTime, signTime);