-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathendorsement.go
80 lines (65 loc) · 1.67 KB
/
endorsement.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
package tezosprotocol
import (
"bytes"
"encoding/binary"
"fmt"
"golang.org/x/xerrors"
)
// Endorsement models the tezos endorsement operation type
type Endorsement struct {
Level int32
}
func (e *Endorsement) String() string {
return fmt.Sprintf("%#v", e)
}
// GetTag implements OperationContents
func (e *Endorsement) GetTag() ContentsTag {
return ContentsTagEndorsement
}
// MarshalBinary implements encoding.BinaryMarshaler
func (e *Endorsement) MarshalBinary() ([]byte, error) {
buf := bytes.Buffer{}
// tag
buf.WriteByte(byte(e.GetTag()))
// Level
levelBytesBuf := new(bytes.Buffer)
err := binary.Write(levelBytesBuf, binary.BigEndian, e.Level)
if err != nil {
return []byte(""), xerrors.Errorf("%w", err)
}
_, err = buf.Write(levelBytesBuf.Bytes())
if err != nil {
return []byte(""), xerrors.Errorf("%w", err)
}
return buf.Bytes(), nil
}
func readInt32(data []byte) (ret int32, err error) {
buf := bytes.NewBuffer(data)
err = binary.Read(buf, binary.BigEndian, &ret)
return ret, err
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler
func (e *Endorsement) UnmarshalBinary(data []byte) (err error) {
// cleanly recover from out of bounds exceptions
defer func() {
if err == nil {
if r := recover(); r != nil {
err = catchOutOfRangeExceptions(r)
}
}
}()
dataPtr := data
// tag
tag := ContentsTag(dataPtr[0])
if tag != ContentsTagEndorsement {
return xerrors.Errorf("invalid tag for endorsement. Expected %d, saw %d", ContentsTagEndorsement, tag)
}
dataPtr = dataPtr[1:]
// Level
level, err := readInt32(dataPtr)
if err != nil {
return xerrors.Errorf("failed to unmarshal level: %w", err)
}
e.Level = level
return nil
}