-
Notifications
You must be signed in to change notification settings - Fork 22
/
messages.go
362 lines (341 loc) · 8.54 KB
/
messages.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package abi
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"reflect"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
)
func pointer[T any](t T) *T {
return &t
}
func decodeMsg(tag tlb.Tag, name MsgOpName, bodyType any) msgDecoderFunc {
return func(cell *boc.Cell) (*uint32, *MsgOpName, any, error) {
cell.ResetCounters()
readTag, err := cell.ReadUint(tag.Len)
if err != nil {
return nil, nil, nil, err
}
var uintTag *uint32
if readTag != tag.Val {
return nil, nil, nil, fmt.Errorf("invalid tag")
}
if tag.Len == 32 {
uintTag = pointer(uint32(readTag))
}
body := reflect.New(reflect.TypeOf(bodyType))
err = tlb.Unmarshal(cell, body.Interface())
if err == nil {
return uintTag, pointer(name), body.Elem().Interface(), nil
}
return uintTag, nil, nil, err
}
}
func decodeMultipleMsgs(funcs []msgDecoderFunc, tag string) msgDecoderFunc {
return func(cell *boc.Cell) (*uint32, *MsgOpName, any, error) {
for _, f := range funcs {
tag, opName, object, err := f(cell)
if err == nil && completedRead(cell) {
return tag, opName, object, err
}
}
return nil, nil, nil, fmt.Errorf("no one message can be unmarshled for %v", tag)
}
}
type InMsgBody struct {
SumType MsgOpName
OpCode *uint32
Value any
}
func (body *InMsgBody) UnmarshalTLB(cell *boc.Cell, decoder *tlb.Decoder) error {
body.SumType = UnknownMsgOp
tag, name, value, err := InternalMessageDecoder(cell, nil)
if err != nil {
return err
}
if tag == nil && name == nil {
body.SumType = EmptyMsgOp
return nil
}
body.OpCode = tag
if name != nil {
body.SumType = *name
body.Value = value
} else {
if cell.BitsAvailableForRead() != cell.BitSize() {
cell = cell.CopyRemaining() //because body can be part of the message cell
}
body.Value = cell
}
return nil
}
func (body *InMsgBody) UnmarshalJSON(data []byte) error {
var r struct {
SumType string
OpCode *uint32
Value json.RawMessage
}
if err := json.Unmarshal(data, &r); err != nil {
return err
}
body.SumType = r.SumType
body.OpCode = r.OpCode
if body.SumType == EmptyMsgOp {
return nil
}
if body.SumType == UnknownMsgOp {
c := boc.NewCell()
err := json.Unmarshal(r.Value, c)
if err != nil {
return err
}
body.Value = c
return nil
}
t, ok := KnownMsgInTypes[body.SumType]
if !ok {
return fmt.Errorf("unknown message body type '%v'", body.SumType)
}
o := reflect.New(reflect.TypeOf(t))
err := json.Unmarshal(r.Value, o.Interface())
if err != nil {
return err
}
body.Value = o.Elem().Interface()
return nil
}
func (body InMsgBody) MarshalJSON() ([]byte, error) {
if body.SumType == EmptyMsgOp {
return []byte("{}"), nil
}
buf := bytes.NewBufferString(`{"SumType": "` + body.SumType + `",`)
if body.OpCode != nil {
fmt.Fprintf(buf, `"OpCode":%v,`, *body.OpCode)
}
buf.WriteString(`"Value":`)
if body.SumType == UnknownMsgOp {
c, ok := body.Value.(*boc.Cell)
if !ok {
return nil, fmt.Errorf("unknown MsgBody should be Cell")
}
b, err := c.ToBoc()
if err != nil {
return nil, err
}
buf.WriteRune('"')
hex.NewEncoder(buf).Write(b)
buf.WriteString(`"}`)
return buf.Bytes(), nil
}
if KnownMsgInTypes[body.SumType] == nil {
return nil, fmt.Errorf("unknown MsgBosy type %v", body.SumType)
}
b, err := json.Marshal(body.Value)
if err != nil {
return nil, err
}
buf.Write(b)
buf.WriteRune('}')
return buf.Bytes(), nil
}
type ExtOutMsgBody struct {
SumType string
OpCode *uint32
Value any
}
func (b *ExtOutMsgBody) UnmarshalTLB(cell *boc.Cell, decoder *tlb.Decoder) error {
b.SumType = UnknownMsgOp
tag, name, value, err := ExtOutMessageDecoder(cell, nil, tlb.MsgAddress{SumType: "AddrNone"})
if err != nil {
return err
}
if tag == nil && name == nil {
b.SumType = EmptyMsgOp
}
b.OpCode = tag
if name != nil {
b.SumType = *name
b.Value = value
} else {
if cell.BitsAvailableForRead() != cell.BitSize() {
cell = cell.CopyRemaining() //because body can be part of the message cell
}
b.Value = cell
}
return nil
}
func (body *ExtOutMsgBody) UnmarshalJSON(data []byte) error {
var r struct {
SumType string
OpCode *uint32
Value json.RawMessage
}
if err := json.Unmarshal(data, &r); err != nil {
return err
}
body.SumType = r.SumType
body.OpCode = r.OpCode
if body.SumType == EmptyMsgOp {
return nil
}
if body.SumType == UnknownMsgOp {
c := boc.NewCell()
err := json.Unmarshal(r.Value, c)
if err != nil {
return err
}
body.Value = c
return nil
}
t, ok := KnownMsgExtOutTypes[body.SumType]
if !ok {
return fmt.Errorf("unknown message body type '%v'", body.SumType)
}
o := reflect.New(reflect.TypeOf(t))
err := json.Unmarshal(r.Value, o.Interface())
if err != nil {
return err
}
body.Value = o.Elem().Interface()
return nil
}
func (body ExtOutMsgBody) MarshalJSON() ([]byte, error) {
if body.SumType == EmptyMsgOp {
return []byte("{}"), nil
}
buf := bytes.NewBufferString(`{"SumType": "` + body.SumType + `",`)
if body.OpCode != nil {
fmt.Fprintf(buf, `"OpCode":%v,`, *body.OpCode)
}
buf.WriteString(`"Value":`)
if body.SumType == UnknownMsgOp {
c, ok := body.Value.(*boc.Cell)
if !ok {
return nil, fmt.Errorf("unknown MsgBody should be Cell")
}
b, err := c.ToBoc()
if err != nil {
return nil, err
}
buf.WriteRune('"')
hex.NewEncoder(buf).Write(b)
buf.WriteString(`"}`)
return buf.Bytes(), nil
}
if KnownMsgExtOutTypes[body.SumType] == nil {
return nil, fmt.Errorf("unknown MsgBosy type %v", body.SumType)
}
b, err := json.Marshal(body.Value)
if err != nil {
return nil, err
}
buf.Write(b)
buf.WriteRune('}')
return buf.Bytes(), nil
}
type msgDecoderFunc func(cell *boc.Cell) (*uint32, *MsgOpName, any, error)
// InternalMessageDecoder takes in a message body as a cell and tries to decode it based on the contract type or the first 4 bytes.
// It returns an opcode, an operation name and a decoded body.
func InternalMessageDecoder(cell *boc.Cell, interfaces []ContractInterface) (*MsgOpCode, *MsgOpName, any, error) {
if cell.BitsAvailableForRead() < 32 {
return nil, nil, nil, nil
}
if cell.BitsAvailableForRead() != cell.BitSize() {
cell = cell.CopyRemaining() //because body can be part of the message cell
}
for _, i := range interfaces {
for _, f := range i.IntMsgs() {
t, m, v, err := f(cell)
if err == nil {
return t, m, v, nil
}
}
}
cell.ResetCounters()
tag64, err := cell.PickUint(32)
if err != nil {
return nil, nil, nil, err
}
tag := uint32(tag64)
f := opcodedMsgInDecodeFunctions[tag]
if f != nil {
t, o, b, err := f(cell)
if err == nil {
return t, o, b, nil
}
}
return &tag, nil, nil, nil
}
func ExtInMessageDecoder(cell *boc.Cell, interfaces []ContractInterface) (*MsgOpCode, *MsgOpName, any, error) {
if cell.BitsAvailableForRead() < 32 {
return nil, nil, nil, nil
}
if cell.BitsAvailableForRead() != cell.BitSize() {
cell = cell.CopyRemaining() //because body can be part of the message cell
}
for _, i := range interfaces {
for _, f := range i.ExtInMsgs() {
t, m, v, err := f(cell)
if err == nil {
return t, m, v, nil
}
}
}
cell.ResetCounters()
tag64, err := cell.PickUint(32)
if err != nil {
return nil, nil, nil, err
}
tag := uint32(tag64)
f := opcodedMsgExtInDecodeFunctions[tag]
if f != nil {
t, o, b, err := f(cell)
if err == nil {
return t, o, b, nil
}
}
return &tag, nil, nil, nil
}
func ExtOutMessageDecoder(cell *boc.Cell, interfaces []ContractInterface, dest tlb.MsgAddress) (*MsgOpCode, *MsgOpName, any, error) { //todo: change to tlb.MsgAddressExt
if cell.BitsAvailableForRead() < 32 {
return nil, nil, nil, nil
}
if cell.BitsAvailableForRead() != cell.BitSize() {
cell = cell.CopyRemaining() //because body can be part of the message cell
}
for _, i := range interfaces {
for _, f := range i.ExtOutMsgs() {
t, m, v, err := f(cell)
if err == nil {
return t, m, v, nil
}
}
}
cell.ResetCounters()
tag64, err := cell.PickUint(32)
if err != nil {
return nil, nil, nil, err
}
tag := uint32(tag64)
f := opcodedMsgExtOutDecodeFunctions[tag]
if f != nil {
t, o, b, err := f(cell)
if err == nil {
return t, o, b, nil
}
}
return &tag, nil, nil, nil
}
func completedRead(cell *boc.Cell) bool {
return cell.RefsAvailableForRead() == 0 && cell.BitsAvailableForRead() == 0
}
// MsgOpName is a human-friendly name for a message's operation which is identified by the first 4 bytes of the message's body.
type MsgOpName = string
const (
UnknownMsgOp MsgOpName = "Unknown"
EmptyMsgOp MsgOpName = ""
)
// MsgOpCode is the first 4 bytes of a message body identifying an operation to be performed.
type MsgOpCode = uint32