diff --git a/src/SerializedObject.h b/src/SerializedObject.h index d3cdeca37..a0f8c3413 100644 --- a/src/SerializedObject.h +++ b/src/SerializedObject.h @@ -1,6 +1,8 @@ #ifndef __SERIALIZEDOBJECT__ #define __SERIALIZEDOBJECT__ +#include + #include #include "SerializedTypes.h" @@ -18,22 +20,16 @@ struct SOElement int e_flags; }; -struct SOType -{ // A type of serialized object - const char *name; - std::list elements; -}; - class STUObject : public SerializedType { protected: - SOType *type; boost::ptr_vector data; + std::vector type; public: - STUObject(const char *n=NULL) : SerializedType(n), type(NULL) { ; } - STUObject(SOType *t) : type(t) { ; } - STUObject(SOType *t, SerializerIterator& u); + STUObject(const char *n=NULL) : SerializedType(n) { ; } + STUObject(SOElement *t, const char *n=NULL); + STUObject(SOElement *t, SerializerIterator& u, const char *n=NULL); virtual ~STUObject() { ; } int getLength() const; diff --git a/src/SerializedTransaction.cpp b/src/SerializedTransaction.cpp index 6b33f0442..ce7d97f29 100644 --- a/src/SerializedTransaction.cpp +++ b/src/SerializedTransaction.cpp @@ -10,38 +10,7 @@ SerializedTransaction::SerializedTransaction(TransactionType type) mMiddleTxn.giveObject(new STVariableLength("Signature")); // signature mMiddleTxn.giveObject(new STUInt8("Type", static_cast(type))); - SOElement* elem=mFormat->elements; - while(elem->e_id!=STI_DONE) - { - if( (elem->e_type==SOE_IFFLAG) || (elem->e_type==SOE_IFNFLAG) ) - mInnerTxn.giveObject(new STUObject(elem->e_name)); - else switch(elem->e_id) - { - case STI_UINT16: - mInnerTxn.giveObject(new STUInt16(elem->e_name)); - break; - case STI_UINT32: - mInnerTxn.giveObject(new STUInt32(elem->e_name)); - break; - case STI_UINT64: - mInnerTxn.giveObject(new STUInt64(elem->e_name)); - case STI_HASH160: - case STI_HASH256: - case STI_VL: - mInnerTxn.giveObject(new STVariableLength(elem->e_name)); - break; - case STI_TL: - mInnerTxn.giveObject(new STTaggedList(elem->e_name)); - break; -#if 0 - case STI_ACCOUNT: // CHECKME: Should an account be variable length? - mInnerTxn.giveObject(new STVariableLength(elem->e_name)); - break; -#endif - default: throw(std::runtime_error("invalid transaction element")); - } - elem++; - } + mInnerTxn=STUObject(mFormat->elements, "InnerTransaction"); } SerializedTransaction::SerializedTransaction(SerializerIterator& sit, int length) @@ -57,9 +26,13 @@ SerializedTransaction::SerializedTransaction(SerializerIterator& sit, int length mMiddleTxn.giveObject(new STUInt32("Magic", TransactionMagic)); mMiddleTxn.giveObject(new STVariableLength("Signature", sit.getVL())); - mMiddleTxn.giveObject(new STUInt32("Type", sit.get32())); - // WRITEME + int type=sit.get32(); + mMiddleTxn.giveObject(new STUInt32("Type", type)); + mFormat=getFormat(static_cast(type)); + if(!mFormat) throw(std::runtime_error("Transaction has invalid type")); + + mInnerTxn=STUObject(mFormat->elements, sit, "InnerTransaction"); } int SerializedTransaction::getLength() const diff --git a/src/SerializedTypes.cpp b/src/SerializedTypes.cpp index 6be7880f9..9f94631b7 100644 --- a/src/SerializedTypes.cpp +++ b/src/SerializedTypes.cpp @@ -20,9 +20,9 @@ std::string SerializedType::getFullText() const return ret; } -STUInt8* STUInt8::construct(SerializerIterator& u) +STUInt8* STUInt8::construct(SerializerIterator& u, const char *name) { - return new STUInt8(u.get8()); + return new STUInt8(name, u.get8()); } std::string STUInt8::getText() const @@ -30,9 +30,9 @@ std::string STUInt8::getText() const return boost::lexical_cast(value); } -STUInt16* STUInt16::construct(SerializerIterator& u) +STUInt16* STUInt16::construct(SerializerIterator& u, const char *name) { - return new STUInt16(u.get16()); + return new STUInt16(name, u.get16()); } std::string STUInt16::getText() const @@ -40,9 +40,9 @@ std::string STUInt16::getText() const return boost::lexical_cast(value); } -STUInt32* STUInt32::construct(SerializerIterator& u) +STUInt32* STUInt32::construct(SerializerIterator& u, const char *name) { - return new STUInt32(u.get32()); + return new STUInt32(name, u.get32()); } std::string STUInt32::getText() const @@ -50,9 +50,9 @@ std::string STUInt32::getText() const return boost::lexical_cast(value); } -STUInt64* STUInt64::construct(SerializerIterator& u) +STUInt64* STUInt64::construct(SerializerIterator& u, const char *name) { - return new STUInt64(u.get64()); + return new STUInt64(name, u.get64()); } std::string STUInt64::getText() const @@ -60,22 +60,22 @@ std::string STUInt64::getText() const return boost::lexical_cast(value); } -STUHash160* STUHash160::construct(SerializerIterator& u) +STHash160* STHash160::construct(SerializerIterator& u, const char *name) { - return new STUHash160(u.get160()); + return new STHash160(name, u.get160()); } -std::string STUHash160::getText() const +std::string STHash160::getText() const { return value.GetHex(); } -STUHash256* STUHash256::construct(SerializerIterator& u) +STHash256* STHash256::construct(SerializerIterator& u, const char *name) { - return new STUHash256(u.get256()); + return new STHash256(name, u.get256()); } -std::string STUHash256::getText() const +std::string STHash256::getText() const { return value.GetHex(); } @@ -94,7 +94,7 @@ std::string STVariableLength::getText() const return hex(value); } -STVariableLength* STVariableLength::construct(SerializerIterator& u) +STVariableLength* STVariableLength::construct(SerializerIterator& u, const char *name) { return new STVariableLength(u.getVL()); } @@ -116,60 +116,15 @@ std::string STTaggedList::getText() const return ret; } -STTaggedList* STTaggedList::construct(SerializerIterator& u) +STTaggedList* STTaggedList::construct(SerializerIterator& u, const char *name) { - return new STTaggedList(u.getTaggedList()); + return new STTaggedList(name, u.getTaggedList()); } int STTaggedList::getLength() const { int ret=Serializer::getTaggedListLength(value); - if(ret<0) throw(0); + if(ret<0) throw(std::overflow_error("bad TL length")); return ret; } -std::string STUObject::getFullText() const -{ - std::string ret; - if(name!=NULL) - { - ret=name; - ret+=" = {"; - } - else ret="{"; - for(boost::ptr_vector::const_iterator it=data.begin(), end=data.end(); it!=end; ++it) - ret+=it->getFullText(); - ret+="}"; - return ret; -} - -int STUObject::getLength() const -{ - int ret=0; - for(boost::ptr_vector::const_iterator it=data.begin(), end=data.end(); it!=end; ++it) - ret+=it->getLength(); - return ret; -} - -void STUObject::add(Serializer& s) const -{ - for(boost::ptr_vector::const_iterator it=data.begin(), end=data.end(); it!=end; ++it) - it->add(s); -} - -std::string STUObject::getText() const -{ - std::string ret="{"; - bool first=false; - for(boost::ptr_vector::const_iterator it=data.begin(), end=data.end(); it!=end; ++it) - { - if(!first) - { - ret+=", "; - first=false; - } - ret+=it->getText(); - } - ret+="}"; - return ret; -} diff --git a/src/SerializedTypes.h b/src/SerializedTypes.h index 617948c29..8a9fced78 100644 --- a/src/SerializedTypes.h +++ b/src/SerializedTypes.h @@ -36,7 +36,7 @@ public: virtual int getLength() const { return 0; } virtual SerializedTypeID getType() const { return STI_NOTPRESENT; } - virtual SerializedType* duplicate() const { return new SerializedType(); } + virtual SerializedType* duplicate() const { return new SerializedType(name); } virtual std::string getFullText() const; virtual std::string getText() const // just the value @@ -57,11 +57,11 @@ public: STUInt8(unsigned char v=0) : value(v) { ; } STUInt8(const char *n, unsigned char v=0) : SerializedType(n), value(v) { ; } - static STUInt8* construct(SerializerIterator&); + static STUInt8* construct(SerializerIterator&, const char *name=NULL); int getLength() const { return 1; } SerializedTypeID getType() const { return STI_UINT8; } - STUInt8 *duplicate() const { return new STUInt8(value); } + STUInt8 *duplicate() const { return new STUInt8(name, value); } std::string getText() const; virtual void add(Serializer& s) const { s.add8(value); } @@ -81,11 +81,11 @@ public: STUInt16(uint16 v=0) : value(v) { ; } STUInt16(const char *n, uint16 v=0) : SerializedType(n), value(v) { ; } - static STUInt16* construct(SerializerIterator&); + static STUInt16* construct(SerializerIterator&, const char *name=NULL); int getLength() const { return 2; } SerializedTypeID getType() const { return STI_UINT16; } - STUInt16 *duplicate() const { return new STUInt16(value); } + STUInt16 *duplicate() const { return new STUInt16(name, value); } std::string getText() const; virtual void add(Serializer& s) const { s.add16(value); } @@ -105,11 +105,11 @@ public: STUInt32(uint32 v=0) : value(v) { ; } STUInt32(const char *n, uint32 v=0) : SerializedType(n), value(v) { ; } - static STUInt32* construct(SerializerIterator&); + static STUInt32* construct(SerializerIterator&, const char *name=NULL); int getLength() const { return 4; } SerializedTypeID getType() const { return STI_UINT32; } - STUInt32 *duplicate() const { return new STUInt32(value); } + STUInt32 *duplicate() const { return new STUInt32(name, value); } std::string getText() const; virtual void add(Serializer& s) const { s.add32(value); } @@ -129,11 +129,11 @@ public: STUInt64(uint64 v=0) : value(v) { ; } STUInt64(const char *n, uint64 v=0) : SerializedType(n), value(v) { ; } - static STUInt64* construct(SerializerIterator&); + static STUInt64* construct(SerializerIterator&, const char *name=NULL); int getLength() const { return 8; } SerializedTypeID getType() const { return STI_UINT64; } - STUInt64 *duplicate() const { return new STUInt64(value); } + STUInt64 *duplicate() const { return new STUInt64(name, value); } std::string getText() const; virtual void add(Serializer& s) const { s.add64(value); } @@ -144,21 +144,21 @@ public: STUInt64& operator=(uint64 v) { value=v; return *this; } }; -class STUHash160 : public SerializedType +class STHash160 : public SerializedType { protected: uint160 value; public: - STUHash160(const uint160& v=uint160()) : value(v) { ; } - STUHash160(const char *n, const uint160& v=uint160()) : SerializedType(n), value(v) { ; } - STUHash160() { ; } - static STUHash160* construct(SerializerIterator&); + STHash160(const uint160& v=uint160()) : value(v) { ; } + STHash160(const char *n, const uint160& v=uint160()) : SerializedType(n), value(v) { ; } + STHash160() { ; } + static STHash160* construct(SerializerIterator&, const char *name=NULL); int getLength() const { return 20; } SerializedTypeID getType() const { return STI_HASH160; } - STUHash160 *duplicate() const { return new STUHash160(value); } + STHash160 *duplicate() const { return new STHash160(name, value); } std::string getText() const; virtual void add(Serializer& s) const { s.add160(value); } @@ -166,24 +166,24 @@ public: void setValue(const uint160& v) { value=v; } operator uint160() const { return value; } - STUHash160& operator=(const uint160& v) { value=v; return *this; } + STHash160& operator=(const uint160& v) { value=v; return *this; } }; -class STUHash256 : public SerializedType +class STHash256 : public SerializedType { protected: uint256 value; public: - STUHash256(const uint256& v) : value(v) { ; } - STUHash256(const char *n, const uint256& v=uint256()) : SerializedType(n), value(v) { ; } - STUHash256() { ; } - static STUHash256* construct(SerializerIterator&); + STHash256(const uint256& v) : value(v) { ; } + STHash256(const char *n, const uint256& v=uint256()) : SerializedType(n), value(v) { ; } + STHash256() { ; } + static STHash256* construct(SerializerIterator&, const char *name=NULL); int getLength() const { return 32; } SerializedTypeID getType() const { return STI_HASH256; } - STUHash256 *duplicate() const { return new STUHash256(value); } + STHash256 *duplicate() const { return new STHash256(name, value); } std::string getText() const; virtual void add(Serializer& s) const { s.add256(value); } @@ -191,7 +191,7 @@ public: void setValue(const uint256& v) { value=v; } operator uint256() const { return value; } - STUHash256& operator=(const uint256& v) { value=v; return *this; } + STHash256& operator=(const uint256& v) { value=v; return *this; } }; class STVariableLength : public SerializedType @@ -205,11 +205,11 @@ public: STVariableLength(const char *n, const std::vector& v) : SerializedType(n), value(v) { ; } STVariableLength(const char *n) : SerializedType(n) { ; } STVariableLength() { ; } - static STVariableLength* construct(SerializerIterator&); + static STVariableLength* construct(SerializerIterator&, const char *name=NULL); int getLength() const; SerializedTypeID getType() const { return STI_VL; } - STVariableLength *duplicate() const { return new STVariableLength(value); } + STVariableLength *duplicate() const { return new STVariableLength(name, value); } std::string getText() const; virtual void add(Serializer& s) const { s.addVL(value); } @@ -233,11 +233,11 @@ public: STTaggedList(const char *n) : SerializedType(n) { ; } STTaggedList(const std::vector& v) : value(v) { ; } STTaggedList(const char *n, const std::vector& v) : SerializedType(n), value(v) { ; } - static STTaggedList* construct(SerializerIterator&); + static STTaggedList* construct(SerializerIterator&, const char *name=NULL); int getLength() const; SerializedTypeID getType() const { return STI_TL; } - STTaggedList *duplicate() const { return new STTaggedList(value); } + STTaggedList *duplicate() const { return new STTaggedList(name, value); } std::string getText() const; virtual void add(Serializer& s) const { if(s.addTaggedList(value)<0) throw(0); }