-
Notifications
You must be signed in to change notification settings - Fork 6
/
origination.go
196 lines (167 loc) · 4.6 KB
/
origination.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package tezosprotocol
import (
"bytes"
"fmt"
"math/big"
"github.com/anchorageoss/tezosprotocol/v3/zarith"
"golang.org/x/xerrors"
)
// Origination models the tezos origination operation type.
type Origination struct {
Source ContractID
Fee *big.Int
Counter *big.Int
GasLimit *big.Int
StorageLimit *big.Int
Balance *big.Int
Delegate *ContractID
Script ContractScript
}
func (o *Origination) String() string {
return fmt.Sprintf("%#v", o)
}
// GetTag implements OperationContents
func (o *Origination) GetTag() ContentsTag {
return ContentsTagOrigination
}
// GetSource returns the operation's source
func (o *Origination) GetSource() ContractID {
return o.Source
}
// MarshalBinary implements encoding.BinaryMarshaler
func (o *Origination) MarshalBinary() ([]byte, error) {
buf := bytes.Buffer{}
// tag
buf.WriteByte(byte(o.GetTag()))
// source
sourceBytes, err := o.Source.EncodePubKeyHash()
if err != nil {
return nil, xerrors.Errorf("failed to write source: %w", err)
}
buf.Write(sourceBytes)
// fee
fee, err := zarith.Encode(o.Fee)
if err != nil {
return nil, xerrors.Errorf("failed to write Fee: %w", err)
}
buf.Write(fee)
// counter
counter, err := zarith.Encode(o.Counter)
if err != nil {
return nil, xerrors.Errorf("failed to write Counter: %w", err)
}
buf.Write(counter)
// gas limit
gasLimit, err := zarith.Encode(o.GasLimit)
if err != nil {
return nil, xerrors.Errorf("failed to write GasLimit: %w", err)
}
buf.Write(gasLimit)
// storage limit
storageLimit, err := zarith.Encode(o.StorageLimit)
if err != nil {
return nil, xerrors.Errorf("failed to write StorageLimit: %w", err)
}
buf.Write(storageLimit)
// balance
balance, err := zarith.Encode(o.Balance)
if err != nil {
return nil, xerrors.Errorf("failed to write Balance: %w", err)
}
buf.Write(balance)
// delegate
hasDelegate := o.Delegate != nil
buf.WriteByte(serializeBoolean(hasDelegate))
if hasDelegate {
//nolint:govet
delegatePubKeyHashBytes, err := o.Delegate.EncodePubKeyHash()
if err != nil {
return nil, xerrors.Errorf("failed to write delegate: %w", err)
}
buf.Write(delegatePubKeyHashBytes)
}
// script
scriptBytes, err := o.Script.MarshalBinary()
if err != nil {
return nil, xerrors.Errorf("failed to write Script: %w", err)
}
buf.Write(scriptBytes)
return buf.Bytes(), nil
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler
func (o *Origination) 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 != ContentsTagOrigination {
return xerrors.Errorf("invalid tag for origination. Expected %d, saw %d", ContentsTagOrigination, tag)
}
dataPtr = dataPtr[1:]
// source
err = o.Source.UnmarshalBinary(dataPtr[:TaggedPubKeyHashLen])
if err != nil {
return xerrors.Errorf("failed to unmarshal source: %w", err)
}
dataPtr = dataPtr[TaggedPubKeyHashLen:]
// fee
var bytesRead int
o.Fee, bytesRead, err = zarith.ReadNext(dataPtr)
if err != nil {
return xerrors.Errorf("failed to unmarshal fee: %w", err)
}
dataPtr = dataPtr[bytesRead:]
// counter
o.Counter, bytesRead, err = zarith.ReadNext(dataPtr)
if err != nil {
return xerrors.Errorf("failed to unmarshal counter: %w", err)
}
dataPtr = dataPtr[bytesRead:]
// gas limit
o.GasLimit, bytesRead, err = zarith.ReadNext(dataPtr)
if err != nil {
return xerrors.Errorf("failed to unmarshal gas limit: %w", err)
}
dataPtr = dataPtr[bytesRead:]
// storage limit
o.StorageLimit, bytesRead, err = zarith.ReadNext(dataPtr)
if err != nil {
return xerrors.Errorf("failed to unmarshal storage limit: %w", err)
}
dataPtr = dataPtr[bytesRead:]
// balance
o.Balance, bytesRead, err = zarith.ReadNext(dataPtr)
if err != nil {
return xerrors.Errorf("failed to unmarshal balance: %w", err)
}
dataPtr = dataPtr[bytesRead:]
// delegate
hasDelegate, err := deserializeBoolean(dataPtr[0])
if err != nil {
return xerrors.Errorf("failed to deserialize presence of field \"delegate\": %w", err)
}
dataPtr = dataPtr[1:]
if hasDelegate {
taggedPubKeyHash := dataPtr[:TaggedPubKeyHashLen]
var delegate ContractID
err = delegate.UnmarshalBinary(taggedPubKeyHash)
if err != nil {
return xerrors.Errorf("failed to deserialize delegate: %w", err)
}
o.Delegate = &delegate
dataPtr = dataPtr[TaggedPubKeyHashLen:]
}
// script
err = o.Script.UnmarshalBinary(dataPtr)
if err != nil {
return xerrors.Errorf("failed to deserialize script: %w", err)
}
return nil
}