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

     1  // Copyright (c) 2023 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc
     3  
     4  package codec
     5  
     6  import (
     7  	"bytes"
     8  	"strconv"
     9  
    10  	"github.com/mavryk-network/mvgo/mavryk"
    11  )
    12  
    13  // SmartRollupExecuteOutboxMessage represents "smart_rollup_execute_outbox_message" operation
    14  type SmartRollupExecuteOutboxMessage struct {
    15  	Manager
    16  	Rollup   mavryk.Address               `json:"rollup"`
    17  	Cemented mavryk.SmartRollupCommitHash `json:"cemented_commitment"`
    18  	Proof    mavryk.HexBytes              `json:"output_proof"`
    19  }
    20  
    21  func (o SmartRollupExecuteOutboxMessage) Kind() mavryk.OpType {
    22  	return mavryk.OpTypeSmartRollupExecuteOutboxMessage
    23  }
    24  
    25  func (o SmartRollupExecuteOutboxMessage) MarshalJSON() ([]byte, error) {
    26  	buf := bytes.NewBuffer(nil)
    27  	buf.WriteByte('{')
    28  	buf.WriteString(`"kind":`)
    29  	buf.WriteString(strconv.Quote(o.Kind().String()))
    30  	buf.WriteByte(',')
    31  	o.Manager.EncodeJSON(buf)
    32  	buf.WriteString(`,"rollup":`)
    33  	buf.WriteString(strconv.Quote(o.Rollup.String()))
    34  	buf.WriteString(`,"cemented_commitment":`)
    35  	buf.WriteString(strconv.Quote(o.Cemented.String()))
    36  	buf.WriteString(`,"output_proof":`)
    37  	buf.WriteString(strconv.Quote(o.Proof.String()))
    38  	buf.WriteByte('}')
    39  	return buf.Bytes(), nil
    40  }
    41  
    42  func (o SmartRollupExecuteOutboxMessage) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error {
    43  	buf.WriteByte(o.Kind().TagVersion(p.OperationTagsVersion))
    44  	o.Manager.EncodeBuffer(buf, p)
    45  	buf.Write(o.Rollup.Hash()) // 20 byte only
    46  	buf.Write(o.Cemented[:])
    47  	writeBytesWithLen(buf, o.Proof)
    48  	return nil
    49  }
    50  
    51  func (o *SmartRollupExecuteOutboxMessage) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) {
    52  	if err = ensureTagAndSize(buf, o.Kind(), p.OperationTagsVersion); err != nil {
    53  		return
    54  	}
    55  	if err = o.Manager.DecodeBuffer(buf, p); err != nil {
    56  		return
    57  	}
    58  	o.Rollup = mavryk.NewAddress(mavryk.AddressTypeSmartRollup, buf.Next(20))
    59  	o.Cemented = mavryk.NewSmartRollupCommitHash(buf.Next(32))
    60  	o.Proof, err = readBytesWithLen(buf)
    61  	return
    62  }
    63  
    64  func (o SmartRollupExecuteOutboxMessage) MarshalBinary() ([]byte, error) {
    65  	buf := bytes.NewBuffer(nil)
    66  	err := o.EncodeBuffer(buf, mavryk.DefaultParams)
    67  	return buf.Bytes(), err
    68  }
    69  
    70  func (o *SmartRollupExecuteOutboxMessage) UnmarshalBinary(data []byte) error {
    71  	return o.DecodeBuffer(bytes.NewBuffer(data), mavryk.DefaultParams)
    72  }