github.com/mavryk-network/mvgo@v1.19.9/codec/smart_rollup_add_messages.go (about)

     1  // Copyright (c) 2023 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc
     3  
     4  package codec
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/binary"
     9  	"strconv"
    10  
    11  	"github.com/mavryk-network/mvgo/mavryk"
    12  )
    13  
    14  // SmartRollupAddMessages represents "smart_rollup_add_messages" operation
    15  type SmartRollupAddMessages struct {
    16  	Manager
    17  	Messages []mavryk.HexBytes `json:"messages"`
    18  }
    19  
    20  func (o SmartRollupAddMessages) Kind() mavryk.OpType {
    21  	return mavryk.OpTypeSmartRollupAddMessages
    22  }
    23  
    24  func (o SmartRollupAddMessages) MarshalJSON() ([]byte, error) {
    25  	buf := bytes.NewBuffer(nil)
    26  	buf.WriteByte('{')
    27  	buf.WriteString(`"kind":`)
    28  	buf.WriteString(strconv.Quote(o.Kind().String()))
    29  	buf.WriteByte(',')
    30  	o.Manager.EncodeJSON(buf)
    31  	buf.WriteString(`,"messages":[`)
    32  	if len(o.Messages) > 0 {
    33  		buf.WriteString(strconv.Quote(o.Messages[0].String()))
    34  		if len(o.Messages) > 1 {
    35  			for _, v := range o.Messages[1:] {
    36  				buf.WriteByte(',')
    37  				buf.WriteString(strconv.Quote(v.String()))
    38  			}
    39  		}
    40  	}
    41  	buf.WriteString("]}")
    42  	return buf.Bytes(), nil
    43  }
    44  
    45  func (o SmartRollupAddMessages) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error {
    46  	buf.WriteByte(o.Kind().TagVersion(p.OperationTagsVersion))
    47  	o.Manager.EncodeBuffer(buf, p)
    48  	var sz int
    49  	for _, v := range o.Messages {
    50  		sz += len(v) + 4
    51  	}
    52  	binary.Write(buf, enc, uint32(sz))
    53  	for _, v := range o.Messages {
    54  		writeBytesWithLen(buf, v)
    55  	}
    56  	return nil
    57  }
    58  
    59  func (o *SmartRollupAddMessages) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) {
    60  	if err = ensureTagAndSize(buf, o.Kind(), p.OperationTagsVersion); err != nil {
    61  		return
    62  	}
    63  	if err = o.Manager.DecodeBuffer(buf, p); err != nil {
    64  		return
    65  	}
    66  	var sz int32
    67  	sz, err = readInt32(buf.Next(4))
    68  	if err != nil {
    69  		return
    70  	}
    71  	for sz > 0 {
    72  		var msg mavryk.HexBytes
    73  		msg, err = readBytesWithLen(buf)
    74  		if err != nil {
    75  			return
    76  		}
    77  		o.Messages = append(o.Messages, msg)
    78  		sz -= int32(len(msg))
    79  	}
    80  	return
    81  }
    82  
    83  func (o SmartRollupAddMessages) MarshalBinary() ([]byte, error) {
    84  	buf := bytes.NewBuffer(nil)
    85  	err := o.EncodeBuffer(buf, mavryk.DefaultParams)
    86  	return buf.Bytes(), err
    87  }
    88  
    89  func (o *SmartRollupAddMessages) UnmarshalBinary(data []byte) error {
    90  	return o.DecodeBuffer(bytes.NewBuffer(data), mavryk.DefaultParams)
    91  }