github.com/annchain/OG@v0.0.9/og/protocol/ogmessage/archive/tx_marshaler.go (about)

     1  //// Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  ////
     3  //// Licensed under the Apache License, Version 2.0 (the "License");
     4  //// you may not use this file except in compliance with the License.
     5  //// You may obtain a copy of the License at
     6  ////
     7  ////     http://www.apache.org/licenses/LICENSE-2.0
     8  ////
     9  //// Unless required by applicable law or agreed to in writing, software
    10  //// distributed under the License is distributed on an "AS IS" BASIS,
    11  //// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //// See the License for the specific language governing permissions and
    13  //// limitations under the License.
    14  package archive
    15  
    16  //
    17  ////do not use go gen msgp for this file  , this is written by hand
    18  //import (
    19  //	"encoding/binary"
    20  //	"fmt"
    21  //	"github.com/tinylib/msgp/msgp"
    22  //)
    23  //
    24  ////TxMarshaler just for marshaller , put type to front 2 bytes, and marshal
    25  //
    26  ////TxMarshaler just for marshaller , put type to front 2 bytes, and marshal
    27  //type RawTxMarshaler struct {
    28  //	RawTxi `msg:"-"`
    29  //}
    30  //
    31  //func (t *RawTxMarshaler) MarshalMsg(b []byte) (o []byte, err error) {
    32  //	if t == nil || t.RawTxi == nil {
    33  //		panic("nil txi")
    34  //	}
    35  //	head := make([]byte, 2)
    36  //	binary.BigEndian.PutUint16(head, uint16(GetType()))
    37  //	b = append(b, head...)
    38  //	if GetType() == TxBaseAction {
    39  //		r := t.RawTxi.(*RawActionTx)
    40  //		b = append(b, r.Action)
    41  //	}
    42  //	return MarshalMsg(b)
    43  //}
    44  //
    45  //func (t *RawTxMarshaler) UnmarshalMsg(bts []byte) (o []byte, err error) {
    46  //	if len(bts) <= 3 {
    47  //		return bts, fmt.Errorf("size mismatch")
    48  //	}
    49  //	tp := binary.BigEndian.Uint16(bts)
    50  //	switch TxBaseType(tp) {
    51  //	case TxBaseTypeNormal:
    52  //		t.RawTxi = &RawTx{TxBase: TxBase{Type: TxBaseTypeNormal}}
    53  //	//case TxBaseTypeCampaign:
    54  //	//	t.RawTxi = &RawCampaign{TxBase: TxBase{Type: TxBaseTypeCampaign}}
    55  //	//case TxBaseTypeTermChange:
    56  //	//	t.RawTxi = &RawTermChange{TxBase: TxBase{Type: TxBaseTypeTermChange}}
    57  //	case TxBaseTypeSequencer:
    58  //		t.RawTxi = &RawSequencer{TxBase: TxBase{Type: TxBaseTypeSequencer}}
    59  //	//case types.TxBaseTypeArchive:
    60  //	//	t.RawTxi = &protocol_message.RawArchive{Archive: archive.Archive{TxBase: types.TxBase{Type: types.TxBaseTypeArchive}}}
    61  //	case TxBaseAction:
    62  //		rawTx := &RawActionTx{TxBase: TxBase{Type: TxBaseAction}}
    63  //		action := bts[3]
    64  //		if action == ActionRequestDomainName {
    65  //			rawTx.ActionData = &RequestDomain{}
    66  //		} else if action == ActionTxActionIPO || action == ActionTxActionSPO || action == ActionTxActionDestroy {
    67  //			rawTx.ActionData = &PublicOffering{}
    68  //		} else {
    69  //			return bts, fmt.Errorf("unkown action %d", action)
    70  //		}
    71  //		t.RawTxi = rawTx
    72  //		return UnmarshalMsg(bts[3:])
    73  //	default:
    74  //		return bts, fmt.Errorf("unkown type")
    75  //	}
    76  //	return UnmarshalMsg(bts[2:])
    77  //}
    78  //
    79  //func (t *RawTxMarshaler) Msgsize() (s int) {
    80  //	if GetType() == TxBaseAction {
    81  //		return 3 + Msgsize()
    82  //	}
    83  //	return 2 + Msgsize()
    84  //}
    85  //
    86  //func (t *RawTxMarshaler) DecodeMsg(dc *msgp.Reader) (err error) {
    87  //	head := make([]byte, 2)
    88  //	_, err = dc.ReadFull(head)
    89  //	if err != nil {
    90  //		return
    91  //	}
    92  //	if len(head) < 2 {
    93  //		return fmt.Errorf("size mismatch")
    94  //	}
    95  //	tp := binary.BigEndian.Uint16(head)
    96  //	switch TxBaseType(tp) {
    97  //	case TxBaseTypeNormal:
    98  //		t.RawTxi = &RawTx{TxBase: TxBase{Type: TxBaseTypeNormal}}
    99  //	//case TxBaseTypeCampaign:
   100  //	//	t.RawTxi = &RawCampaign{TxBase: TxBase{Type: TxBaseTypeCampaign}}
   101  //	//case TxBaseTypeTermChange:
   102  //	//	t.RawTxi = &RawTermChange{TxBase: TxBase{Type: TxBaseTypeTermChange}}
   103  //	case TxBaseTypeSequencer:
   104  //		t.RawTxi = &RawSequencer{TxBase: TxBase{Type: TxBaseTypeSequencer}}
   105  //	//case types.TxBaseTypeArchive:
   106  //	//	t.RawTxi = &protocol_message.RawArchive{Archive: archive.Archive{TxBase: types.TxBase{Type: types.TxBaseTypeArchive}}}
   107  //	case TxBaseAction:
   108  //		rawTx := &RawActionTx{TxBase: TxBase{Type: TxBaseAction}}
   109  //		head := make([]byte, 1)
   110  //		_, err := dc.ReadFull(head)
   111  //		if err != nil {
   112  //			return err
   113  //		}
   114  //		if len(head) < 1 {
   115  //			return fmt.Errorf("size mismatch")
   116  //		}
   117  //		action := head[0]
   118  //		if action == ActionRequestDomainName {
   119  //			rawTx.ActionData = &RequestDomain{}
   120  //		} else if action == ActionTxActionIPO || action == ActionTxActionSPO || action == ActionTxActionDestroy {
   121  //			rawTx.ActionData = &PublicOffering{}
   122  //		} else {
   123  //			return fmt.Errorf("unkown action %d", action)
   124  //		}
   125  //		t.RawTxi = rawTx
   126  //		return DecodeMsg(dc)
   127  //	default:
   128  //		return fmt.Errorf("unkown type")
   129  //	}
   130  //	return DecodeMsg(dc)
   131  //}
   132  //
   133  //func (t *RawTxMarshaler) EncodeMsg(en *msgp.Writer) (err error) {
   134  //	if t == nil || t.RawTxi == nil {
   135  //		panic("nil txi")
   136  //	}
   137  //	head := make([]byte, 2)
   138  //	binary.BigEndian.PutUint16(head, uint16(GetType()))
   139  //	_, err = en.Write(head)
   140  //	if err != nil {
   141  //		return err
   142  //	}
   143  //	if GetType() == TxBaseAction {
   144  //		r := t.RawTxi.(*RawActionTx)
   145  //		err = en.WriteByte(r.Action)
   146  //	}
   147  //	if err != nil {
   148  //		return err
   149  //	}
   150  //	return EncodeMsg(en)
   151  //}
   152  //
   153  ////func (t *RawTxMarshaler) Txi() types.Txi {
   154  ////	if t == nil || t.RawTxi == nil {
   155  ////		return nil
   156  ////	}
   157  ////	switch raw := t.RawTxi.(type) {
   158  ////	case *protocol_message.RawTx:
   159  ////		return raw.Tx()
   160  ////	case *protocol_message.RawSequencer:
   161  ////		return raw.Sequencer()
   162  ////	case *protocol_message.RawCampaign:
   163  ////		return raw.Campaign()
   164  ////	case *protocol_message.RawTermChange:
   165  ////		return raw.TermChange()
   166  ////	case *protocol_message.RawArchive:
   167  ////		return &raw.Archive
   168  ////	case *protocol_message.RawActionTx:
   169  ////		return raw.ActionTx()
   170  ////	default:
   171  ////		return nil
   172  ////	}
   173  ////}
   174  //
   175  //func NewTxisMarshaler(t Txis) TxisMarshaler {
   176  //	var txs TxisMarshaler
   177  //	for _, tx := range t {
   178  //		txs.Append(tx)
   179  //	}
   180  //	if len(txs) == 0 {
   181  //		txs = nil
   182  //	}
   183  //	return txs
   184  //}