/
ballot.go
81 lines (71 loc) · 2.02 KB
/
ballot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package codec
import (
"bytes"
"encoding/binary"
"strconv"
"blockwatch.cc/tzgo/tezos"
)
// Ballot represents "ballot" operation
type Ballot struct {
Simple
Source tezos.Address `json:"source"`
Period int32 `json:"period"`
Proposal tezos.ProtocolHash `json:"proposal"`
Ballot tezos.BallotVote `json:"ballot"`
}
func (o Ballot) Kind() tezos.OpType {
return tezos.OpTypeBallot
}
func (o Ballot) MarshalJSON() ([]byte, error) {
buf := bytes.NewBuffer(nil)
buf.WriteByte('{')
buf.WriteString(`"kind":`)
buf.WriteString(strconv.Quote(o.Kind().String()))
buf.WriteString(`,"source":`)
buf.WriteString(strconv.Quote(o.Source.String()))
buf.WriteString(`,"period":`)
buf.WriteString(strconv.Itoa(int(o.Period)))
buf.WriteString(`,"proposal":`)
buf.WriteString(strconv.Quote(o.Proposal.String()))
buf.WriteString(`,"ballot":`)
buf.WriteString(strconv.Quote(o.Ballot.String()))
buf.WriteByte('}')
return buf.Bytes(), nil
}
func (o Ballot) EncodeBuffer(buf *bytes.Buffer, p *tezos.Params) error {
buf.WriteByte(o.Kind().TagVersion(p.OperationTagsVersion))
buf.Write(o.Source.Encode())
binary.Write(buf, enc, o.Period)
buf.Write(o.Proposal.Bytes())
buf.WriteByte(o.Ballot.Tag())
return nil
}
func (o *Ballot) DecodeBuffer(buf *bytes.Buffer, p *tezos.Params) (err error) {
if err = ensureTagAndSize(buf, o.Kind(), p.OperationTagsVersion); err != nil {
return
}
if err = o.Source.Decode(buf.Next(21)); err != nil {
return
}
o.Period, err = readInt32(buf.Next(4))
if err != nil {
return
}
if err = o.Proposal.UnmarshalBinary(buf.Next(32)); err != nil {
return
}
if err = o.Ballot.UnmarshalBinary(buf.Next(1)); err != nil {
return
}
return nil
}
func (o Ballot) MarshalBinary() ([]byte, error) {
buf := bytes.NewBuffer(nil)
err := o.EncodeBuffer(buf, tezos.DefaultParams)
return buf.Bytes(), err
}
func (o *Ballot) UnmarshalBinary(data []byte) error {
return o.DecodeBuffer(bytes.NewBuffer(data), tezos.DefaultParams)
}