-
Notifications
You must be signed in to change notification settings - Fork 22
/
models.go
406 lines (366 loc) · 8.68 KB
/
models.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package tlb
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"math/big"
"strconv"
"strings"
"unicode/utf8"
"github.com/tonkeeper/tongo/boc"
)
var ErrGramsOverflow = errors.New("grams overflow")
// Grams
// nanograms$_ amount:(VarUInteger 16) = Grams;
type Grams uint64 // total value fit to uint64
type Coins = Grams
type SignedCoins int64
func (g Grams) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
var amount VarUInteger16
amount = VarUInteger16(*big.NewInt(int64(g)))
err := encode(c, "", amount, encoder)
return err
}
func (g *Grams) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
ln, err := c.ReadLimUint(15)
if err != nil {
return err
}
if ln > 8 {
return ErrGramsOverflow
}
var amount uint64
for i := 0; i < int(ln); i++ {
b, err := c.ReadUint(8)
if err != nil {
return err
}
amount = uint64(b) | (amount << 8)
}
*g = Grams(amount)
return nil
}
func (g SignedCoins) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
err := c.WriteBit(g < 0)
if err != nil {
return err
}
if g < 0 {
g = -g
}
amount := VarUInteger16(*big.NewInt(int64(g)))
return encode(c, "", amount, encoder)
}
func (g *SignedCoins) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
negative, err := c.ReadBit()
if err != nil {
return err
}
ln, err := c.ReadLimUint(15)
if err != nil {
return err
}
if ln > 8 {
return ErrGramsOverflow
}
var amount uint64
for i := 0; i < int(ln); i++ {
b, err := c.ReadUint(8)
if err != nil {
return err
}
amount = uint64(b) | (amount << 8)
}
if amount > 1<<63 {
return ErrGramsOverflow
}
if negative {
amount = -amount
}
*g = SignedCoins(amount)
return nil
}
func (g Grams) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%d\"", g)), nil
}
func (g *Grams) UnmarshalJSON(data []byte) error {
val, err := strconv.ParseUint(string(bytes.Trim(data, "\" \n")), 10, 64)
if err != nil {
return err
}
*g = Grams(val)
return nil
}
func (g SignedCoins) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%d\"", g)), nil
}
func (g *SignedCoins) UnmarshalJSON(data []byte) error {
val, err := strconv.ParseUint(string(bytes.Trim(data, "\" \n")), 10, 64)
if err != nil {
return err
}
*g = SignedCoins(val)
return nil
}
// CurrencyCollection
// currencies$_ grams:Grams other:ExtraCurrencyCollection
// = CurrencyCollection;
type CurrencyCollection struct {
Grams Grams
Other ExtraCurrencyCollection
}
// ExtraCurrencyCollection
// extra_currencies$_ dict:(HashmapE 32 (VarUInteger 32))
// = ExtraCurrencyCollection;
type ExtraCurrencyCollection struct {
Dict HashmapE[Uint32, VarUInteger32]
}
func (h HashmapE[keyT, T]) MarshalJSON() ([]byte, error) {
m := make(map[string]T, len(h.Keys()))
for _, item := range h.Items() {
key, err := json.Marshal(item.Key)
if err != nil {
return nil, err
}
m[strings.Trim(string(key), "\"")] = item.Value
}
return json.Marshal(m)
}
func (f ExtraCurrencyCollection) MarshalJSON() ([]byte, error) {
return f.Dict.MarshalJSON()
}
// HashUpdate
// update_hashes#72 {X:Type} old_hash:bits256 new_hash:bits256
// = HASH_UPDATE X;
type HashUpdate struct {
Magic Magic `tlb:"update_hashes#72"`
OldHash Bits256
NewHash Bits256
}
// SnakeData
// tail#_ {bn:#} b:(bits bn) = SnakeData ~0;
// cons#_ {bn:#} {n:#} b:(bits bn) next:^(SnakeData ~n) = SnakeData ~(n + 1);
type SnakeData boc.BitString
func (s SnakeData) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
bs := boc.BitString(s)
if c.BitsAvailableForWrite() < bs.GetWriteCursor() {
s, err := bs.ReadBits(c.BitsAvailableForWrite())
if err != nil {
return err
}
err = c.WriteBitString(s)
if err != nil {
return err
}
ref := boc.NewCell()
err = Marshal(ref, SnakeData(bs.ReadRemainingBits()))
if err != nil {
return err
}
err = c.AddRef(ref)
return err
}
return c.WriteBitString(bs)
}
func (s *SnakeData) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
b := c.ReadRemainingBits()
if c.RefsAvailableForRead() > 0 {
cell, err := c.NextRef()
if err != nil {
return err
}
var sn SnakeData
err = decoder.Unmarshal(cell, &sn)
if err != nil {
return err
}
b.Append(boc.BitString(sn))
}
*s = SnakeData(b)
return nil
}
type Bytes []byte
func (b Bytes) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
bs := boc.NewBitString(len(b) * 8)
err := bs.WriteBytes(b)
if err != nil {
return err
}
return Marshal(c, SnakeData(bs))
}
func (b *Bytes) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
var sn SnakeData
err := decoder.Unmarshal(c, &sn)
if err != nil {
return err
}
bs := boc.BitString(sn)
if bs.BitsAvailableForRead()%8 != 0 {
return fmt.Errorf("text data must be a multiple of 8 bits")
}
buf, err := bs.GetTopUppedArray()
if err != nil {
return err
}
*b = buf
return nil
}
// text#_ {n:#} data:(SnakeData ~n) = Text;
type Text string
func (t Text) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
return Bytes(t).MarshalTLB(c, encoder)
}
func (t *Text) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
var b Bytes
err := b.UnmarshalTLB(c, decoder)
if err != nil {
return err
}
if !utf8.Valid(b) {
return fmt.Errorf("invalid unicode characters in text")
}
*t = Text(b)
return nil
}
type FixedLengthText string
func (t FixedLengthText) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
l := len(t)
err := c.WriteUint(uint64(l), 8)
if err != nil {
return err
}
return c.WriteBytes([]byte(t))
}
func (t *FixedLengthText) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
l, err := c.ReadUint(8)
if err != nil {
return err
}
b, err := c.ReadBytes(int(l))
*t = FixedLengthText(b)
return err
}
// FullContent
// onchain#00 data:(HashMapE 256 ^ContentData) = FullContent;
// offchain#01 uri:Text = FullContent;
// text#_ {n:#} data:(SnakeData ~n) = Text;
type FullContent struct {
SumType
Onchain struct {
Data HashmapE[Bits256, Ref[ContentData]]
} `tlbSumType:"onchain#00"`
Offchain struct {
Uri SnakeData // Text
} `tlbSumType:"offchain#01"`
}
// ContentData
// snake#00 data:(SnakeData ~n) = ContentData;
// chunks#01 data:ChunkedData = ContentData;
type ContentData struct {
SumType
Snake struct {
Data SnakeData
} `tlbSumType:"snake#00"`
Chunks struct {
Data ChunkedData
} `tlbSumType:"chunks#01"`
}
func (c ContentData) Bytes() ([]byte, error) {
var bs boc.BitString
switch c.SumType {
case "Snake":
bs = boc.BitString(c.Snake.Data)
case "Chunks":
bs = boc.BitString(c.Chunks.Data)
default:
return nil, fmt.Errorf("empty content data struct")
}
if bs.BitsAvailableForRead()%8 != 0 {
return nil, fmt.Errorf("data is not multiple of 8 bits")
}
return bs.GetTopUppedArray()
}
// ChunkedData
// chunked_data#_ data:(HashMapE 32 ^(SnakeData ~0)) = ChunkedData;
type ChunkedData boc.BitString
func (d ChunkedData) MarshalTLB(c *boc.Cell, encoder *Encoder) error {
// TODO: implement
return fmt.Errorf("ChunkedData marshaling not implemented")
}
func (d *ChunkedData) UnmarshalTLB(c *boc.Cell, decoder *Decoder) error {
type chunkedData struct {
Data HashmapE[Uint32, Ref[SnakeData]]
}
var (
cd chunkedData
)
b := boc.NewBitString(boc.CellBits)
err := decoder.Unmarshal(c, &cd)
if err != nil {
return err
}
// TODO: check keys sort
for _, x := range cd.Data.Values() {
b.Append(boc.BitString(x.Value))
}
*d = ChunkedData(b)
return nil
}
type ShardDesc struct {
SumType
Old struct {
SeqNo uint32
RegMcSeqno uint32
StartLT uint64
EndLT uint64
RootHash Bits256
FileHash Bits256
BeforeSplit bool
BeforeMerge bool
WantSplit bool
WantMerge bool
NXCCUpdated bool
Flags Uint3
NextCatchainSeqNo uint32
NextValidatorShard int64
MinRefMcSeqNo uint32
GenUTime uint32
//todo: add split_merge_at:FutureSplitMerge
// ^[ fees_collected:CurrencyCollection
// funds_created:CurrencyCollection ]
} `tlbSumType:"old#b"`
New struct {
SeqNo uint32
RegMcSeqno uint32
StartLT uint64
EndLT uint64
RootHash Bits256
FileHash Bits256
BeforeSplit bool
BeforeMerge bool
WantSplit bool
WantMerge bool
NXCCUpdated bool
Flags Uint3
NextCatchainSeqNo uint32
NextValidatorShard int64
MinRefMcSeqNo uint32
GenUTime uint32
//todo: add split_merge_at:FutureSplitMerge
// fees_collected:CurrencyCollection
// funds_created:CurrencyCollection
} `tlbSumType:"new#a"`
}
func (d *ShardDesc) SeqNo() uint32 {
if d.SumType == "New" {
return d.New.SeqNo
}
return d.Old.SeqNo
}
type ShardInfoBinTree struct {
BinTree BinTree[ShardDesc]
}
type AllShardsInfo struct {
ShardHashes HashmapE[Uint32, Ref[ShardInfoBinTree]]
}