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

     1  // Copyright (c) 2020-2022 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  // Endorsement represents "endorsement" operation
    15  type Endorsement struct {
    16  	Simple
    17  	Level int32 `json:"level"`
    18  }
    19  
    20  func (o Endorsement) MarshalJSON() ([]byte, error) {
    21  	buf := bytes.NewBuffer(nil)
    22  	buf.WriteByte('{')
    23  	buf.WriteString(`"kind":`)
    24  	buf.WriteString(strconv.Quote(o.Kind().String()))
    25  	buf.WriteString(`,"level":`)
    26  	buf.WriteString(strconv.Itoa(int(o.Level)))
    27  	buf.WriteByte('}')
    28  	return buf.Bytes(), nil
    29  }
    30  
    31  func (o Endorsement) Kind() mavryk.OpType {
    32  	return mavryk.OpTypeEndorsement
    33  }
    34  
    35  func (o Endorsement) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error {
    36  	buf.WriteByte(o.Kind().TagVersion(p.OperationTagsVersion))
    37  	binary.Write(buf, enc, o.Level)
    38  	return nil
    39  }
    40  
    41  func (o *Endorsement) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) {
    42  	if err = ensureTagAndSize(buf, o.Kind(), p.OperationTagsVersion); err != nil {
    43  		return
    44  	}
    45  	o.Level, err = readInt32(buf.Next(4))
    46  	if err != nil {
    47  		return
    48  	}
    49  	return nil
    50  }
    51  
    52  func (o Endorsement) MarshalBinary() ([]byte, error) {
    53  	buf := bytes.NewBuffer(nil)
    54  	err := o.EncodeBuffer(buf, mavryk.DefaultParams)
    55  	return buf.Bytes(), err
    56  }
    57  
    58  func (o *Endorsement) UnmarshalBinary(data []byte) error {
    59  	return o.DecodeBuffer(bytes.NewBuffer(data), mavryk.DefaultParams)
    60  }
    61  
    62  // InlinedEndorsement represents inlined endorsement operation with signature. This
    63  // type is uses as part of other operations, but is not a stand-alone operation.
    64  type InlinedEndorsement struct {
    65  	Branch      mavryk.BlockHash `json:"branch"`
    66  	Endorsement Endorsement      `json:"operations"`
    67  	Signature   mavryk.Signature `json:"signature"`
    68  }
    69  
    70  func (o InlinedEndorsement) MarshalJSON() ([]byte, error) {
    71  	buf := bytes.NewBuffer(nil)
    72  	buf.WriteByte('{')
    73  	buf.WriteString(`"branch":`)
    74  	buf.WriteString(strconv.Quote(o.Branch.String()))
    75  	buf.WriteString(`,"operations":`)
    76  	if b, err := o.Endorsement.MarshalJSON(); err != nil {
    77  		return nil, err
    78  	} else {
    79  		buf.Write(b)
    80  	}
    81  	buf.WriteString(`,"signature":`)
    82  	buf.WriteString(strconv.Quote(o.Signature.String()))
    83  	buf.WriteByte('}')
    84  	return buf.Bytes(), nil
    85  }
    86  
    87  func (o InlinedEndorsement) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error {
    88  	buf.Write(o.Branch.Bytes())
    89  	o.Endorsement.EncodeBuffer(buf, p)
    90  	buf.Write(o.Signature.Data) // generic sig, no tag (!)
    91  	return nil
    92  }
    93  
    94  func (o *InlinedEndorsement) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) {
    95  	err = o.Branch.UnmarshalBinary(buf.Next(mavryk.HashTypeBlock.Len))
    96  	if err != nil {
    97  		return
    98  	}
    99  	if err = o.Endorsement.DecodeBuffer(buf, p); err != nil {
   100  		return
   101  	}
   102  	if err := o.Signature.DecodeBuffer(buf); err != nil {
   103  		return err
   104  	}
   105  	return nil
   106  }
   107  
   108  // EndorsementWithSlot represents "endorsement_with_slot" operation
   109  type EndorsementWithSlot struct {
   110  	Simple
   111  	Endorsement InlinedEndorsement `json:"endorsement"`
   112  	Slot        int16              `json:"slot"`
   113  }
   114  
   115  func (o EndorsementWithSlot) Kind() mavryk.OpType {
   116  	return mavryk.OpTypeEndorsementWithSlot
   117  }
   118  
   119  func (o EndorsementWithSlot) MarshalJSON() ([]byte, error) {
   120  	buf := bytes.NewBuffer(nil)
   121  	buf.WriteByte('{')
   122  	buf.WriteString(`"kind":`)
   123  	buf.WriteString(strconv.Quote(o.Kind().String()))
   124  	buf.WriteString(`,"endorsement":`)
   125  	b, _ := o.Endorsement.MarshalJSON()
   126  	buf.Write(b)
   127  	buf.WriteString(`,"slot":`)
   128  	buf.WriteString(strconv.Itoa(int(o.Slot)))
   129  	buf.WriteByte('}')
   130  	return buf.Bytes(), nil
   131  }
   132  
   133  func (o EndorsementWithSlot) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error {
   134  	buf.WriteByte(o.Kind().TagVersion(p.OperationTagsVersion))
   135  	b2 := bytes.NewBuffer(nil)
   136  	o.Endorsement.EncodeBuffer(b2, p)
   137  	binary.Write(buf, enc, uint32(b2.Len()))
   138  	buf.Write(b2.Bytes())
   139  	binary.Write(buf, enc, o.Slot)
   140  	return nil
   141  }
   142  
   143  func (o *EndorsementWithSlot) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) {
   144  	if err = ensureTagAndSize(buf, o.Kind(), p.OperationTagsVersion); err != nil {
   145  		return
   146  	}
   147  	l, err := readInt32(buf.Next(4))
   148  	if err != nil {
   149  		return err
   150  	}
   151  	if err = o.Endorsement.DecodeBuffer(bytes.NewBuffer(buf.Next(int(l))), p); err != nil {
   152  		return err
   153  	}
   154  	o.Slot, err = readInt16(buf.Next(2))
   155  	return err
   156  }
   157  
   158  func (o EndorsementWithSlot) MarshalBinary() ([]byte, error) {
   159  	buf := bytes.NewBuffer(nil)
   160  	err := o.EncodeBuffer(buf, mavryk.DefaultParams)
   161  	return buf.Bytes(), err
   162  }
   163  
   164  func (o *EndorsementWithSlot) UnmarshalBinary(data []byte) error {
   165  	return o.DecodeBuffer(bytes.NewBuffer(data), mavryk.DefaultParams)
   166  }
   167  
   168  // TenderbakeEndorsement represents tenderbake endorsement operation
   169  type TenderbakeEndorsement struct {
   170  	Simple
   171  	Slot             int16              `json:"slot"`
   172  	Level            int32              `json:"level"`
   173  	Round            int32              `json:"round"`
   174  	BlockPayloadHash mavryk.PayloadHash `json:"block_payload_hash"`
   175  }
   176  
   177  func (o TenderbakeEndorsement) Kind() mavryk.OpType {
   178  	return mavryk.OpTypeEndorsement
   179  }
   180  
   181  func (o TenderbakeEndorsement) MarshalJSON() ([]byte, error) {
   182  	buf := bytes.NewBuffer(nil)
   183  	buf.WriteByte('{')
   184  	buf.WriteString(`"kind":`)
   185  	buf.WriteString(strconv.Quote(o.Kind().String()))
   186  	buf.WriteString(`,"slot":`)
   187  	buf.WriteString(strconv.Itoa(int(o.Slot)))
   188  	buf.WriteString(`,"level":`)
   189  	buf.WriteString(strconv.Itoa(int(o.Level)))
   190  	buf.WriteString(`,"round":`)
   191  	buf.WriteString(strconv.Itoa(int(o.Round)))
   192  	buf.WriteString(`,"block_payload_hash":`)
   193  	buf.WriteString(strconv.Quote(o.BlockPayloadHash.String()))
   194  	buf.WriteByte('}')
   195  	return buf.Bytes(), nil
   196  }
   197  
   198  func (o TenderbakeEndorsement) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error {
   199  	buf.WriteByte(o.Kind().TagVersion(p.OperationTagsVersion))
   200  	binary.Write(buf, enc, o.Slot)
   201  	binary.Write(buf, enc, o.Level)
   202  	binary.Write(buf, enc, o.Round)
   203  	buf.Write(o.BlockPayloadHash.Bytes())
   204  	return nil
   205  }
   206  
   207  func (o *TenderbakeEndorsement) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) {
   208  	if err = ensureTagAndSize(buf, o.Kind(), p.OperationTagsVersion); err != nil {
   209  		return
   210  	}
   211  	o.Slot, err = readInt16(buf.Next(2))
   212  	if err != nil {
   213  		return err
   214  	}
   215  	o.Level, err = readInt32(buf.Next(4))
   216  	if err != nil {
   217  		return err
   218  	}
   219  	o.Round, err = readInt32(buf.Next(4))
   220  	if err != nil {
   221  		return err
   222  	}
   223  	err = o.BlockPayloadHash.UnmarshalBinary(buf.Next(32))
   224  	return err
   225  }
   226  
   227  func (o TenderbakeEndorsement) MarshalBinary() ([]byte, error) {
   228  	buf := bytes.NewBuffer(nil)
   229  	err := o.EncodeBuffer(buf, mavryk.DefaultParams)
   230  	return buf.Bytes(), err
   231  }
   232  
   233  func (o *TenderbakeEndorsement) UnmarshalBinary(data []byte) error {
   234  	return o.DecodeBuffer(bytes.NewBuffer(data), mavryk.DefaultParams)
   235  }
   236  
   237  // TenderbakeInlinedEndorsement represents inlined endorsement operation with signature. This
   238  // type is uses as part of other operations, but is not a stand-alone operation.
   239  type TenderbakeInlinedEndorsement struct {
   240  	Branch      mavryk.BlockHash      `json:"branch"`
   241  	Endorsement TenderbakeEndorsement `json:"operations"`
   242  	Signature   mavryk.Signature      `json:"signature"`
   243  }
   244  
   245  func (o TenderbakeInlinedEndorsement) MarshalJSON() ([]byte, error) {
   246  	buf := bytes.NewBuffer(nil)
   247  	buf.WriteByte('{')
   248  	buf.WriteString(`"branch":`)
   249  	buf.WriteString(strconv.Quote(o.Branch.String()))
   250  	buf.WriteString(`,"operations":`)
   251  	b, _ := o.Endorsement.MarshalJSON()
   252  	buf.Write(b)
   253  	buf.WriteString(`,"signature":`)
   254  	buf.WriteString(strconv.Quote(o.Signature.String()))
   255  	buf.WriteByte('}')
   256  	return buf.Bytes(), nil
   257  }
   258  
   259  func (o TenderbakeInlinedEndorsement) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error {
   260  	buf.Write(o.Branch.Bytes())
   261  	o.Endorsement.EncodeBuffer(buf, p)
   262  	buf.Write(o.Signature.Data) // generic sig, no tag (!)
   263  	return nil
   264  }
   265  
   266  func (o *TenderbakeInlinedEndorsement) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) {
   267  	err = o.Branch.UnmarshalBinary(buf.Next(mavryk.HashTypeBlock.Len))
   268  	if err != nil {
   269  		return
   270  	}
   271  	if err = o.Endorsement.DecodeBuffer(buf, p); err != nil {
   272  		return
   273  	}
   274  	if err := o.Signature.DecodeBuffer(buf); err != nil {
   275  		return err
   276  	}
   277  	return nil
   278  }
   279  
   280  // TenderbakePreendorsement represents tenderbake preendorsement operation
   281  type TenderbakePreendorsement struct {
   282  	Simple
   283  	Slot             int16              `json:"slot"`
   284  	Level            int32              `json:"level"`
   285  	Round            int32              `json:"round"`
   286  	BlockPayloadHash mavryk.PayloadHash `json:"block_payload_hash"`
   287  }
   288  
   289  func (o TenderbakePreendorsement) Kind() mavryk.OpType {
   290  	return mavryk.OpTypePreendorsement
   291  }
   292  
   293  func (o TenderbakePreendorsement) MarshalJSON() ([]byte, error) {
   294  	buf := bytes.NewBuffer(nil)
   295  	buf.WriteByte('{')
   296  	buf.WriteString(`"kind":`)
   297  	buf.WriteString(strconv.Quote(o.Kind().String()))
   298  	buf.WriteString(`,"slot":`)
   299  	buf.WriteString(strconv.Itoa(int(o.Slot)))
   300  	buf.WriteString(`,"level":`)
   301  	buf.WriteString(strconv.Itoa(int(o.Level)))
   302  	buf.WriteString(`,"round":`)
   303  	buf.WriteString(strconv.Itoa(int(o.Round)))
   304  	buf.WriteString(`,"block_payload_hash":`)
   305  	buf.WriteString(strconv.Quote(o.BlockPayloadHash.String()))
   306  	buf.WriteByte('}')
   307  	return buf.Bytes(), nil
   308  }
   309  
   310  func (o TenderbakePreendorsement) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error {
   311  	buf.WriteByte(o.Kind().TagVersion(p.OperationTagsVersion))
   312  	binary.Write(buf, enc, o.Slot)
   313  	binary.Write(buf, enc, o.Level)
   314  	binary.Write(buf, enc, o.Round)
   315  	buf.Write(o.BlockPayloadHash.Bytes())
   316  	return nil
   317  }
   318  
   319  func (o *TenderbakePreendorsement) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) {
   320  	if err = ensureTagAndSize(buf, o.Kind(), p.OperationTagsVersion); err != nil {
   321  		return
   322  	}
   323  	o.Slot, err = readInt16(buf.Next(2))
   324  	if err != nil {
   325  		return err
   326  	}
   327  	o.Level, err = readInt32(buf.Next(4))
   328  	if err != nil {
   329  		return err
   330  	}
   331  	o.Round, err = readInt32(buf.Next(4))
   332  	if err != nil {
   333  		return err
   334  	}
   335  	err = o.BlockPayloadHash.UnmarshalBinary(buf.Next(32))
   336  	return err
   337  }
   338  
   339  func (o TenderbakePreendorsement) MarshalBinary() ([]byte, error) {
   340  	buf := bytes.NewBuffer(nil)
   341  	err := o.EncodeBuffer(buf, mavryk.DefaultParams)
   342  	return buf.Bytes(), err
   343  }
   344  
   345  func (o *TenderbakePreendorsement) UnmarshalBinary(data []byte) error {
   346  	return o.DecodeBuffer(bytes.NewBuffer(data), mavryk.DefaultParams)
   347  }
   348  
   349  // TenderbakeInlinedPreendorsement represents inlined preendorsement operation with signature. This
   350  // type is uses as part of other operations, but is not a stand-alone operation.
   351  type TenderbakeInlinedPreendorsement struct {
   352  	Branch      mavryk.BlockHash         `json:"branch"`
   353  	Endorsement TenderbakePreendorsement `json:"operations"`
   354  	Signature   mavryk.Signature         `json:"signature"`
   355  }
   356  
   357  func (o TenderbakeInlinedPreendorsement) MarshalJSON() ([]byte, error) {
   358  	buf := bytes.NewBuffer(nil)
   359  	buf.WriteByte('{')
   360  	buf.WriteString(`"branch":`)
   361  	buf.WriteString(strconv.Quote(o.Branch.String()))
   362  	buf.WriteString(`,"operations":`)
   363  	b, _ := o.Endorsement.MarshalJSON()
   364  	buf.Write(b)
   365  	buf.WriteString(`,"signature":`)
   366  	buf.WriteString(strconv.Quote(o.Signature.String()))
   367  	buf.WriteByte('}')
   368  	return buf.Bytes(), nil
   369  }
   370  
   371  func (o TenderbakeInlinedPreendorsement) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error {
   372  	buf.Write(o.Branch.Bytes())
   373  	o.Endorsement.EncodeBuffer(buf, p)
   374  	buf.Write(o.Signature.Data) // generic sig, no tag (!)
   375  	return nil
   376  }
   377  
   378  func (o *TenderbakeInlinedPreendorsement) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) {
   379  	err = o.Branch.UnmarshalBinary(buf.Next(mavryk.HashTypeBlock.Len))
   380  	if err != nil {
   381  		return
   382  	}
   383  	if err = o.Endorsement.DecodeBuffer(buf, p); err != nil {
   384  		return
   385  	}
   386  	if err := o.Signature.DecodeBuffer(buf); err != nil {
   387  		return err
   388  	}
   389  	return nil
   390  }