github.com/mavryk-network/mvgo@v1.19.9/codec/proposals.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 // Proposals represents "proposals" operation 15 type Proposals struct { 16 Simple 17 Source mavryk.Address `json:"source"` 18 Period int32 `json:"period"` 19 Proposals []mavryk.ProtocolHash `json:"proposals"` 20 } 21 22 func (o Proposals) Kind() mavryk.OpType { 23 return mavryk.OpTypeProposals 24 } 25 26 func (o Proposals) MarshalJSON() ([]byte, error) { 27 buf := bytes.NewBuffer(nil) 28 buf.WriteByte('{') 29 buf.WriteString(`"kind":`) 30 buf.WriteString(strconv.Quote(o.Kind().String())) 31 buf.WriteString(`,"source":`) 32 buf.WriteString(strconv.Quote(o.Source.String())) 33 buf.WriteString(`,"period":`) 34 buf.WriteString(strconv.Itoa(int(o.Period))) 35 buf.WriteString(`,"proposals":[`) 36 for i, v := range o.Proposals { 37 if i > 0 { 38 buf.WriteByte(',') 39 } 40 buf.WriteString(strconv.Quote(v.String())) 41 } 42 buf.WriteString(`]}`) 43 return buf.Bytes(), nil 44 } 45 46 func (o Proposals) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error { 47 buf.WriteByte(o.Kind().TagVersion(p.OperationTagsVersion)) 48 buf.Write(o.Source.Encode()) 49 binary.Write(buf, enc, o.Period) 50 binary.Write(buf, enc, int32(len(o.Proposals)*mavryk.HashTypeProtocol.Len)) 51 for _, v := range o.Proposals { 52 buf.Write(v.Bytes()) 53 } 54 return nil 55 } 56 57 func (o *Proposals) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) { 58 if err = ensureTagAndSize(buf, o.Kind(), p.OperationTagsVersion); err != nil { 59 return 60 } 61 if err = o.Source.Decode(buf.Next(21)); err != nil { 62 return 63 } 64 o.Period, err = readInt32(buf.Next(4)) 65 if err != nil { 66 return 67 } 68 l, err := readInt32(buf.Next(4)) 69 if err != nil { 70 return err 71 } 72 o.Proposals = make([]mavryk.ProtocolHash, l/32) 73 for i := range o.Proposals { 74 if err = o.Proposals[i].UnmarshalBinary(buf.Next(32)); err != nil { 75 return 76 } 77 } 78 return nil 79 } 80 81 func (o Proposals) MarshalBinary() ([]byte, error) { 82 buf := bytes.NewBuffer(nil) 83 err := o.EncodeBuffer(buf, mavryk.DefaultParams) 84 return buf.Bytes(), err 85 } 86 87 func (o *Proposals) UnmarshalBinary(data []byte) error { 88 return o.DecodeBuffer(bytes.NewBuffer(data), mavryk.DefaultParams) 89 }