github.com/mavryk-network/mvgo@v1.19.9/codec/manager.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 "strconv" 9 10 "github.com/mavryk-network/mvgo/mavryk" 11 ) 12 13 // Simple is an empty helper struct that's used to fulfil the Operation interface 14 // for anonymous, consensus and voting operations which do not contain fees and 15 // counter. 16 type Simple struct{} 17 18 func (o *Simple) Limits() mavryk.Limits { 19 return mavryk.Limits{} 20 } 21 22 func (o Simple) GetCounter() int64 { 23 return -1 24 } 25 26 func (o *Simple) WithCounter(int64) {} 27 28 func (o *Simple) WithLimits(mavryk.Limits) {} 29 30 func (o *Simple) WithSource(mavryk.Address) {} 31 32 // Manager contains fields common for all manager operations 33 type Manager struct { 34 Source mavryk.Address `json:"source"` 35 Fee mavryk.N `json:"fee"` 36 Counter mavryk.N `json:"counter"` 37 GasLimit mavryk.N `json:"gas_limit"` 38 StorageLimit mavryk.N `json:"storage_limit"` 39 } 40 41 func (o Manager) Limits() mavryk.Limits { 42 return mavryk.Limits{ 43 Fee: o.Fee.Int64(), 44 GasLimit: o.GasLimit.Int64(), 45 StorageLimit: o.StorageLimit.Int64(), 46 } 47 } 48 49 func (o Manager) EncodeJSON(buf *bytes.Buffer) error { 50 buf.WriteString(`"source":`) 51 buf.WriteString(strconv.Quote(o.Source.String())) 52 buf.WriteString(`,"fee":`) 53 buf.WriteString(strconv.Quote(o.Fee.String())) 54 buf.WriteString(`,"counter":`) 55 buf.WriteString(strconv.Quote(o.Counter.String())) 56 buf.WriteString(`,"gas_limit":`) 57 buf.WriteString(strconv.Quote(o.GasLimit.String())) 58 buf.WriteString(`,"storage_limit":`) 59 buf.WriteString(strconv.Quote(o.StorageLimit.String())) 60 return nil 61 } 62 63 func (o Manager) EncodeBuffer(buf *bytes.Buffer, _ *mavryk.Params) error { 64 buf.Write(o.Source.Encode()) 65 o.Fee.EncodeBuffer(buf) 66 o.Counter.EncodeBuffer(buf) 67 o.GasLimit.EncodeBuffer(buf) 68 o.StorageLimit.EncodeBuffer(buf) 69 return nil 70 } 71 72 func (o *Manager) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) { 73 if err = o.Source.Decode(buf.Next(21)); err != nil { 74 return 75 } 76 if err = o.Fee.DecodeBuffer(buf); err != nil { 77 return err 78 } 79 if err = o.Counter.DecodeBuffer(buf); err != nil { 80 return err 81 } 82 if err = o.GasLimit.DecodeBuffer(buf); err != nil { 83 return err 84 } 85 if err = o.StorageLimit.DecodeBuffer(buf); err != nil { 86 return err 87 } 88 return nil 89 } 90 91 func (o *Manager) WithSource(addr mavryk.Address) { 92 o.Source = addr 93 } 94 95 func (o *Manager) WithCounter(c int64) { 96 o.Counter.SetInt64(c) 97 } 98 99 func (o Manager) GetCounter() int64 { 100 return o.Counter.Int64() 101 } 102 103 func (o *Manager) WithLimits(limits mavryk.Limits) { 104 o.Fee.SetInt64(limits.Fee) 105 o.GasLimit.SetInt64(limits.GasLimit) 106 o.StorageLimit.SetInt64(limits.StorageLimit) 107 }