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);
};
extern SField sfInvalid, sfGeneric, sfLedgerEntry, sfTransaction;
extern SField sfInvalid, sfGeneric, sfLedgerEntry, sfTransaction, sfValidation;
#define FIELD(name, type, index) extern SField sf##name;
#define TYPE(name, type, index)

View File

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

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:

View File

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

View File

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

View File

@@ -4,24 +4,28 @@
#include "HashPrefixes.h"
SOElement SerializedValidation::sValidationFormat[] = {
{ sfFlags, "Flags", STI_UINT32, SOE_FLAGS, 0 },
{ sfLedgerHash, "LedgerHash", STI_HASH256, SOE_REQUIRED, 0 },
{ sfSigningTime, "SignTime", STI_UINT32, SOE_REQUIRED, 0 },
{ sfSigningKey, "SigningKey", STI_VL, SOE_REQUIRED, 0 },
{ sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 },
{ sfFlags, SOE_REQUIRED },
{ sfLedgerHash, SOE_REQUIRED },
{ sfLedgerSequence, SOE_OPTIONAL },
{ sfCloseTime, SOE_OPTIONAL },
{ sfLoadFee, SOE_OPTIONAL },
{ sfBaseFee, SOE_OPTIONAL },
{ sfSigningTime, SOE_REQUIRED },
{ sfSigningKey, SOE_REQUIRED },
{ sfInvalid, SOE_END }
};
const uint32 SerializedValidation::sFullFlag = 0x00010000;
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");
}
SerializedValidation::SerializedValidation(const uint256& ledgerHash, uint32 signTime,
const NewcoinAddress& naSeed, bool isFull)
: STObject(sValidationFormat), mSignature("Signature"), mTrusted(false)
: STObject(sValidationFormat, sfValidation), mSignature(sfSignature), mTrusted(false)
{
setValueFieldH256(sfLedgerHash, ledgerHash);
setValueFieldU32(sfSigningTime, signTime);