Skip to content

Commit

Permalink
add version control of payload.
Browse files Browse the repository at this point in the history
pass the version to payload, and payload can decide how to Deserialize.

Signed-off-by: luodanwg <[email protected]>
  • Loading branch information
Arbio5zt authored and dreamfly281 committed Jul 28, 2017
1 parent bda569e commit 6bd4f3b
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 58 deletions.
10 changes: 6 additions & 4 deletions core/transaction/payload/BookKeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"io"
)

const BookKeeperPayloadVersion byte = 0x00

type BookKeeperAction byte

const (
Expand All @@ -21,7 +23,7 @@ type BookKeeper struct {
Cert []byte
}

func (self *BookKeeper) Data() []byte {
func (self *BookKeeper) Data(version byte) []byte {
var buf bytes.Buffer
self.PubKey.Serialize(&buf)
buf.WriteByte(byte(self.Action))
Expand All @@ -30,13 +32,13 @@ func (self *BookKeeper) Data() []byte {
return buf.Bytes()
}

func (self *BookKeeper) Serialize(w io.Writer) error {
_, err := w.Write(self.Data())
func (self *BookKeeper) Serialize(w io.Writer, version byte) error {
_, err := w.Write(self.Data(version))

return err
}

func (self *BookKeeper) Deserialize(r io.Reader) error {
func (self *BookKeeper) Deserialize(r io.Reader, version byte) error {
self.PubKey = new(crypto.PubKey)
err := self.PubKey.DeSerialize(r)
if err != nil {
Expand Down
13 changes: 10 additions & 3 deletions core/transaction/payload/BookKeeping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,31 @@ import (
)

const BookKeepingPayloadVersion byte = 0x03
const BookKeepingPayloadVersionBase byte = 0x02

type BookKeeping struct {
Nonce uint64
}

func (a *BookKeeping) Data() []byte {
func (a *BookKeeping) Data(version byte) []byte {
return []byte{0}
}

func (a *BookKeeping) Serialize(w io.Writer) error {
func (a *BookKeeping) Serialize(w io.Writer, version byte) error {
if version == BookKeepingPayloadVersionBase {
return nil
}
err := serialization.WriteUint64(w, a.Nonce)
if err != nil {
return err
}
return nil
}

func (a *BookKeeping) Deserialize(r io.Reader) error {
func (a *BookKeeping) Deserialize(r io.Reader, version byte) error {
if version == BookKeepingPayloadVersionBase {
return nil
}
var err error
a.Nonce, err = serialization.ReadUint64(r)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions core/transaction/payload/DataFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
)

const DataFilePayloadVersion byte = 0x00

type DataFile struct {
IPFSPath string
Filename string
Expand All @@ -16,13 +18,13 @@ type DataFile struct {

}

func (a *DataFile) Data() []byte {
func (a *DataFile) Data(version byte) []byte {
//TODO: implement RegisterRecord.Data()
return []byte{0}
}

// Serialize is the implement of SignableData interface.
func (a *DataFile) Serialize(w io.Writer) error {
func (a *DataFile) Serialize(w io.Writer, version byte) error {
err := serialization.WriteVarString(w, a.IPFSPath)
if err != nil {
return NewDetailErr(err, ErrNoCode, "[DataFileDetail], IPFSPath serialize failed.")
Expand All @@ -41,7 +43,7 @@ func (a *DataFile) Serialize(w io.Writer) error {
}

// Deserialize is the implement of SignableData interface.
func (a *DataFile) Deserialize(r io.Reader) error {
func (a *DataFile) Deserialize(r io.Reader, version byte) error {
var err error
a.IPFSPath, err = serialization.ReadVarString(r)
if err != nil {
Expand Down
43 changes: 22 additions & 21 deletions core/transaction/payload/DeployCode.go
Original file line number Diff line number Diff line change
@@ -1,92 +1,93 @@
package payload

import (
. "DNA/core/code"
"DNA/common/serialization"
. "DNA/core/code"
"io"
)

const DeployCodePayloadVersion byte = 0x00

type DeployCode struct {
Code *FunctionCode
Name string
CodeVersion string
Author string
Email string
Description string
Code *FunctionCode
Name string
CodeVersion string
Author string
Email string
Description string
}

func (dc *DeployCode) Data() []byte {
func (dc *DeployCode) Data(version byte) []byte {
// TODO: Data()

return []byte{0}
}

func (dc *DeployCode) Serialize(w io.Writer) error {
func (dc *DeployCode) Serialize(w io.Writer, version byte) error {

err := dc.Code.Serialize(w)
if err != nil {
return err
}

err = serialization.WriteVarString(w,dc.Name)
err = serialization.WriteVarString(w, dc.Name)
if err != nil {
return err
}

err = serialization.WriteVarString(w,dc.CodeVersion)
err = serialization.WriteVarString(w, dc.CodeVersion)
if err != nil {
return err
}

err = serialization.WriteVarString(w,dc.Author)
err = serialization.WriteVarString(w, dc.Author)
if err != nil {
return err
}

err = serialization.WriteVarString(w,dc.Email)
err = serialization.WriteVarString(w, dc.Email)
if err != nil {
return err
}

err = serialization.WriteVarString(w,dc.Description)
err = serialization.WriteVarString(w, dc.Description)
if err != nil {
return err
}

return nil
}

func (dc *DeployCode) Deserialize(r io.Reader) error {
func (dc *DeployCode) Deserialize(r io.Reader, version byte) error {
err := dc.Code.Deserialize(r)
if err != nil {
return err
}

dc.Name,err = serialization.ReadVarString(r)
dc.Name, err = serialization.ReadVarString(r)
if err != nil {
return err
}

dc.CodeVersion,err = serialization.ReadVarString(r)
dc.CodeVersion, err = serialization.ReadVarString(r)
if err != nil {
return err
}

dc.Author,err = serialization.ReadVarString(r)
dc.Author, err = serialization.ReadVarString(r)
if err != nil {
return err
}

dc.Email,err = serialization.ReadVarString(r)
dc.Email, err = serialization.ReadVarString(r)
if err != nil {
return err
}

dc.Description,err = serialization.ReadVarString(r)
dc.Description, err = serialization.ReadVarString(r)
if err != nil {
return err
}

return nil
}

9 changes: 5 additions & 4 deletions core/transaction/payload/IssueAsset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package payload

import "io"

type IssueAsset struct {
const IssueAssetPayloadVersion byte = 0x00

type IssueAsset struct {
}

func (a *IssueAsset) Data() []byte {
func (a *IssueAsset) Data(version byte) []byte {
//TODO: implement IssueAsset.Data()
return []byte{0}

}

func (a *IssueAsset) Serialize(w io.Writer) error {
func (a *IssueAsset) Serialize(w io.Writer, version byte) error {
return nil
}

func (a *IssueAsset) Deserialize(r io.Reader) error {
func (a *IssueAsset) Deserialize(r io.Reader, version byte) error {
return nil
}
8 changes: 5 additions & 3 deletions core/transaction/payload/PrivacyPayload.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"time"
)

const PrivacyPayloadVersion byte = 0x00

type EncryptedPayloadType byte

type EncryptedPayload []byte
Expand Down Expand Up @@ -39,12 +41,12 @@ type PrivacyPayload struct {
EncryptAttr PayloadEncryptAttr
}

func (pp *PrivacyPayload) Data() []byte {
func (pp *PrivacyPayload) Data(version byte) []byte {
//TODO: implement PrivacyPayload.Data()
return []byte{0}
}

func (pp *PrivacyPayload) Serialize(w io.Writer) error {
func (pp *PrivacyPayload) Serialize(w io.Writer, version byte) error {
w.Write([]byte{byte(pp.PayloadType)})
err := serialization.WriteVarBytes(w, pp.Payload)
if err != nil {
Expand All @@ -56,7 +58,7 @@ func (pp *PrivacyPayload) Serialize(w io.Writer) error {
return err
}

func (pp *PrivacyPayload) Deserialize(r io.Reader) error {
func (pp *PrivacyPayload) Deserialize(r io.Reader, version byte) error {
var PayloadType [1]byte
_, err := io.ReadFull(r, PayloadType[:])
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions core/transaction/payload/Record.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import (
"io"
)

const RecordPayloadVersion byte = 0x00

type Record struct {
RecordType string
RecordData []byte
}

func (a *Record) Data() []byte {
func (a *Record) Data(version byte) []byte {
//TODO: implement RegisterRecord.Data()
return []byte{0}
}

// Serialize is the implement of SignableData interface.
func (a *Record) Serialize(w io.Writer) error {
func (a *Record) Serialize(w io.Writer, version byte) error {
err := serialization.WriteVarString(w, a.RecordType)
if err != nil {
return NewDetailErr(err, ErrNoCode, "[RecordDetail], RecordType serialize failed.")
Expand All @@ -31,7 +33,7 @@ func (a *Record) Serialize(w io.Writer) error {
}

// Deserialize is the implement of SignableData interface.
func (a *Record) Deserialize(r io.Reader) error {
func (a *Record) Deserialize(r io.Reader, version byte) error {
var err error
a.RecordType, err = serialization.ReadVarString(r)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions core/transaction/payload/RegisterAsset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ import (
"DNA/common"
"DNA/core/asset"
"DNA/crypto"
"io"
. "DNA/errors"

"io"
)

const RegisterPayloadVersion byte = 0x00

type RegisterAsset struct {
Asset *asset.Asset
Amount common.Fixed64
Asset *asset.Asset
Amount common.Fixed64
//Precision byte
Issuer *crypto.PubKey
Controller common.Uint160
}

func (a *RegisterAsset) Data() []byte {
func (a *RegisterAsset) Data(version byte) []byte {
//TODO: implement RegisterAsset.Data()
return []byte{0}

}

func (a *RegisterAsset) Serialize(w io.Writer) error {
func (a *RegisterAsset) Serialize(w io.Writer, version byte) error {
a.Asset.Serialize(w)
a.Amount.Serialize(w)
//w.Write([]byte{a.Precision})
Expand All @@ -32,7 +33,7 @@ func (a *RegisterAsset) Serialize(w io.Writer) error {
return nil
}

func (a *RegisterAsset) Deserialize(r io.Reader) error {
func (a *RegisterAsset) Deserialize(r io.Reader, version byte) error {

//asset
a.Asset = new(asset.Asset)
Expand Down Expand Up @@ -72,4 +73,3 @@ func (a *RegisterAsset) Deserialize(r io.Reader) error {
}
return nil
}

8 changes: 5 additions & 3 deletions core/transaction/payload/TransferAsset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package payload

import "io"

const TransferAssetayloadVersion byte = 0x00

type TransferAsset struct {
}

func (a *TransferAsset) Data() []byte {
func (a *TransferAsset) Data(version byte) []byte {
//TODO: implement TransferAsset.Data()
return []byte{0}

}

func (a *TransferAsset) Serialize(w io.Writer) error {
func (a *TransferAsset) Serialize(w io.Writer, version byte) error {
return nil
}

func (a *TransferAsset) Deserialize(r io.Reader) error {
func (a *TransferAsset) Deserialize(r io.Reader, version byte) error {
return nil
}
Loading

0 comments on commit 6bd4f3b

Please sign in to comment.