/
keys.go
717 lines (609 loc) · 15.8 KB
/
keys.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
package state
// keys.go - database keys.
import (
"bytes"
"encoding/binary"
"github.com/pkg/errors"
"github.com/wavesplatform/gowaves/pkg/crypto"
"github.com/wavesplatform/gowaves/pkg/proto"
)
const (
// Key sizes.
minAccountsDataStorKeySize = 1 + 8 + 2 + 1
wavesBalanceKeySize = 1 + proto.AddressIDSize
assetBalanceKeySize = 1 + proto.AddressIDSize + proto.AssetIDSize
leaseKeySize = 1 + crypto.DigestSize
aliasKeySize = 1 + 2 + proto.AliasMaxLength
addressToAliasesKeySize = 1 + proto.AddressIDSize
disabledAliasKeySize = 1 + 2 + proto.AliasMaxLength
approvedFeaturesKeySize = 1 + 2
votesFeaturesKeySize = 1 + 2
invokeResultKeySize = 1 + crypto.DigestSize
)
// Primary prefixes for storage keys
const (
_ byte = iota // this is a placeholder for a zero value
// Balances.
wavesBalanceKeyPrefix
assetBalanceKeyPrefix
// Unique block num of the last block.
lastBlockNumKeyPrefix
// BlockID --> unique block number.
// Numbers are increasing sequentially.
// These numbers are stored in history records instead of long IDs.
blockIdToNumKeyPrefix
blockNumToIdKeyPrefix
// Valid block unique nums.
validBlockNumKeyPrefix
// For block storage.
// IDs of blocks --> offsets in files.
blockOffsetKeyPrefix
// IDs of transactions --> offsets in files, heights, failure status.
txInfoKeyPrefix
// Minimum height to which rollback is possible.
rollbackMinHeightKeyPrefix
// Height of main db.
dbHeightKeyPrefix
// Score at height.
scoreKeyPrefix
// Assets.
assetConstKeyPrefix
assetHistKeyPrefix
// Leases.
leaseKeyPrefix
// Aliases.
aliasKeyPrefix
addressToAliasesPrefix
disabledAliasKeyPrefix
// Features.
activatedFeaturesKeyPrefix
approvedFeaturesKeyPrefix
votesFeaturesKeyPrefix
// Orders volume.
ordersVolumeKeyPrefix
// Blocks information (fees for now).
blocksInfoKeyPrefix
// Unique address number by address.
// These numbers are only used for accounts data storage.
lastAccountsStorAddrNumKeyPrefix
accountStorAddrToNumKeyPrefix
// Prefix for keys of accounts data entries.
accountsDataStorKeyPrefix
// Sponsored assets storage.
sponsorshipKeyPrefix
// Scripts.
accountScriptKeyPrefix
assetScriptKeyPrefix
scriptBasicInfoKeyPrefix
accountScriptComplexityKeyPrefix
assetScriptComplexityKeyPrefix
accountOriginalEstimatorVersionKeyPrefix
// Block Reward.
blockRewardKeyPrefix
rewardVotesKeyPrefix
// Batched storage (see batched_storage.go).
batchedStorKeyPrefix
// The last batch num by internal key (batched_storage.go).
lastBatchKeyPrefix
// Invoke results.
invokeResultKeyPrefix
// Information about state: version, API support flag, ...
stateInfoKeyPrefix
// Size of TransactionsByAddresses file.
txsByAddressesFileSizeKeyPrefix
// Stores protobuf-related info for blockReadWriter.
rwProtobufInfoKeyPrefix
// Stores state hashes at height.
stateHashKeyPrefix
// Hit source data.
hitSourceKeyPrefix
)
var (
errInvalidDataSize = errors.New("invalid data size")
errInvalidPrefix = errors.New("invalid prefix for given key")
)
func prefixByEntity(entity blockchainEntity) ([]byte, error) {
switch entity {
case alias:
return []byte{aliasKeyPrefix}, nil
case addressToAliases:
return []byte{addressToAliasesPrefix}, nil
case asset:
return []byte{assetHistKeyPrefix}, nil
case lease:
return []byte{leaseKeyPrefix}, nil
case wavesBalance:
return []byte{wavesBalanceKeyPrefix}, nil
case assetBalance:
return []byte{assetBalanceKeyPrefix}, nil
case featureVote:
return []byte{votesFeaturesKeyPrefix}, nil
case approvedFeature:
return []byte{approvedFeaturesKeyPrefix}, nil
case activatedFeature:
return []byte{activatedFeaturesKeyPrefix}, nil
case ordersVolume:
return []byte{ordersVolumeKeyPrefix}, nil
case sponsorship:
return []byte{sponsorshipKeyPrefix}, nil
case dataEntry:
return []byte{accountsDataStorKeyPrefix}, nil
case accountScript:
return []byte{accountScriptKeyPrefix}, nil
case assetScript:
return []byte{assetScriptKeyPrefix}, nil
case scriptBasicInfo:
return []byte{scriptBasicInfoKeyPrefix}, nil
case accountScriptComplexity:
return []byte{accountScriptComplexityKeyPrefix}, nil
case assetScriptComplexity:
return []byte{assetScriptComplexityKeyPrefix}, nil
case rewardVotes:
return []byte{rewardVotesKeyPrefix}, nil
case blockReward:
return []byte{blockRewardKeyPrefix}, nil
case invokeResult:
return []byte{invokeResultKeyPrefix}, nil
case score:
return []byte{scoreKeyPrefix}, nil
case stateHash:
return []byte{stateHashKeyPrefix}, nil
case hitSource:
return []byte{hitSourceKeyPrefix}, nil
case feeDistr:
return []byte{blocksInfoKeyPrefix}, nil
case accountOriginalEstimatorVersion:
return []byte{accountOriginalEstimatorVersionKeyPrefix}, nil
default:
return nil, errors.New("bad entity type")
}
}
type wavesBalanceKey struct {
address proto.AddressID
}
func (k *wavesBalanceKey) bytes() []byte {
buf := make([]byte, wavesBalanceKeySize)
buf[0] = wavesBalanceKeyPrefix
copy(buf[1:], k.address[:])
return buf
}
func (k *wavesBalanceKey) unmarshal(data []byte) error {
if len(data) != wavesBalanceKeySize {
return errInvalidDataSize
}
if data[0] != wavesBalanceKeyPrefix {
return errInvalidPrefix
}
copy(k.address[:], data[1:1+proto.AddressIDSize])
return nil
}
type assetBalanceKey struct {
address proto.AddressID
asset proto.AssetID
}
func (k *assetBalanceKey) addressPrefix() []byte {
buf := make([]byte, 1+proto.AddressIDSize)
buf[0] = assetBalanceKeyPrefix
copy(buf[1:], k.address[:])
return buf
}
func (k *assetBalanceKey) bytes() []byte {
buf := make([]byte, assetBalanceKeySize)
buf[0] = assetBalanceKeyPrefix
copy(buf[1:], k.address[:])
copy(buf[1+proto.AddressIDSize:], k.asset[:])
return buf
}
func (k *assetBalanceKey) unmarshal(data []byte) error {
if len(data) != assetBalanceKeySize {
return errInvalidDataSize
}
if data[0] != assetBalanceKeyPrefix {
return errInvalidPrefix
}
copy(k.address[:], data[1:1+proto.AddressIDSize])
copy(k.asset[:], data[1+proto.AddressIDSize:])
return nil
}
type blockIdToNumKey struct {
blockID proto.BlockID
}
func (k *blockIdToNumKey) bytes() []byte {
idBytes := k.blockID.Bytes()
buf := make([]byte, 1+len(idBytes))
buf[0] = blockIdToNumKeyPrefix
copy(buf[1:], idBytes)
return buf
}
type blockNumToIdKey struct {
blockNum uint32
}
func (k *blockNumToIdKey) bytes() []byte {
buf := make([]byte, 1+4)
buf[0] = blockNumToIdKeyPrefix
binary.BigEndian.PutUint32(buf[1:], k.blockNum)
return buf
}
type validBlockNumKey struct {
blockNum uint32
}
func (k *validBlockNumKey) bytes() []byte {
buf := make([]byte, 1+4)
buf[0] = validBlockNumKeyPrefix
binary.BigEndian.PutUint32(buf[1:], k.blockNum)
return buf
}
type blockOffsetKey struct {
blockID proto.BlockID
}
func (k *blockOffsetKey) bytes() []byte {
idBytes := k.blockID.Bytes()
buf := make([]byte, 1+len(idBytes))
buf[0] = blockOffsetKeyPrefix
copy(buf[1:], idBytes)
return buf
}
type txInfoKey struct {
txID []byte
}
func (k *txInfoKey) bytes() []byte {
buf := make([]byte, 1+crypto.DigestSize)
buf[0] = txInfoKeyPrefix
copy(buf[1:], k.txID)
return buf
}
type scoreKey struct {
height uint64
}
func (k *scoreKey) bytes() []byte {
buf := make([]byte, 9)
buf[0] = scoreKeyPrefix
binary.BigEndian.PutUint64(buf[1:], k.height)
return buf
}
type assetConstKey struct {
assetID proto.AssetID
}
func (k *assetConstKey) bytes() []byte {
buf := make([]byte, 1+proto.AssetIDSize)
buf[0] = assetConstKeyPrefix
copy(buf[1:], k.assetID[:])
return buf
}
type assetHistKey struct {
assetID proto.AssetID
}
func (k *assetHistKey) bytes() []byte {
buf := make([]byte, 1+proto.AssetIDSize)
buf[0] = assetHistKeyPrefix
copy(buf[1:], k.assetID[:])
return buf
}
type leaseKey struct {
leaseID crypto.Digest
}
func (k *leaseKey) unmarshal(data []byte) error {
if len(data) != leaseKeySize {
return errInvalidDataSize
}
if data[0] != leaseKeyPrefix {
return errInvalidPrefix
}
var err error
k.leaseID, err = crypto.NewDigestFromBytes(data[1:])
return err
}
func (k *leaseKey) bytes() []byte {
buf := make([]byte, 1+crypto.DigestSize)
buf[0] = leaseKeyPrefix
copy(buf[1:], k.leaseID[:])
return buf
}
type aliasKey struct {
alias string
}
func (k *aliasKey) bytes() []byte {
buf := make([]byte, aliasKeySize)
buf[0] = aliasKeyPrefix
proto.PutStringWithUInt16Len(buf[1:], k.alias)
return buf
}
func (k *aliasKey) unmarshal(data []byte) error {
if len(data) != aliasKeySize {
return errInvalidDataSize
}
if data[0] != aliasKeyPrefix {
return errInvalidPrefix
}
var err error
k.alias, err = proto.StringWithUInt16Len(data[1:])
return err
}
type addressToAliasesKey struct {
addressID proto.AddressID
}
func (k *addressToAliasesKey) bytes() []byte {
buf := make([]byte, addressToAliasesKeySize)
buf[0] = addressToAliasesPrefix
copy(buf[1:], k.addressID[:])
return buf
}
type disabledAliasKey struct {
alias string
}
func (k *disabledAliasKey) bytes() []byte {
buf := make([]byte, disabledAliasKeySize)
buf[0] = disabledAliasKeyPrefix
proto.PutStringWithUInt16Len(buf[1:], k.alias)
return buf
}
func (k *disabledAliasKey) unmarshal(data []byte) error {
if len(data) != disabledAliasKeySize {
return errInvalidDataSize
}
if data[0] != disabledAliasKeyPrefix {
return errInvalidPrefix
}
var err error
k.alias, err = proto.StringWithUInt16Len(data[1:])
if err != nil {
return errors.Wrap(err, "StringWithUInt16Len() failed")
}
return nil
}
type activatedFeaturesKey struct {
featureID int16
}
func (k *activatedFeaturesKey) bytes() ([]byte, error) {
buf := new(bytes.Buffer)
if _, err := buf.Write([]byte{activatedFeaturesKeyPrefix}); err != nil {
return nil, err
}
if err := binary.Write(buf, binary.BigEndian, k.featureID); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
type approvedFeaturesKey struct {
featureID int16
}
func (k *approvedFeaturesKey) bytes() ([]byte, error) {
buf := new(bytes.Buffer)
if _, err := buf.Write([]byte{approvedFeaturesKeyPrefix}); err != nil {
return nil, err
}
if err := binary.Write(buf, binary.BigEndian, k.featureID); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (k *approvedFeaturesKey) unmarshal(data []byte) error {
if len(data) != approvedFeaturesKeySize {
return errInvalidDataSize
}
if data[0] != approvedFeaturesKeyPrefix {
return errInvalidPrefix
}
buf := bytes.NewBuffer(data[1:])
if err := binary.Read(buf, binary.BigEndian, &k.featureID); err != nil {
return err
}
return nil
}
type votesFeaturesKey struct {
featureID int16
}
func (k *votesFeaturesKey) bytes() ([]byte, error) {
buf := new(bytes.Buffer)
if _, err := buf.Write([]byte{votesFeaturesKeyPrefix}); err != nil {
return nil, err
}
if err := binary.Write(buf, binary.BigEndian, k.featureID); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (k *votesFeaturesKey) unmarshal(data []byte) error {
if len(data) != votesFeaturesKeySize {
return errInvalidDataSize
}
if data[0] != votesFeaturesKeyPrefix {
return errInvalidPrefix
}
buf := bytes.NewBuffer(data[1:])
if err := binary.Read(buf, binary.BigEndian, &k.featureID); err != nil {
return err
}
return nil
}
type ordersVolumeKey struct {
orderId []byte
}
func (k *ordersVolumeKey) bytes() []byte {
buf := make([]byte, 1+len(k.orderId))
buf[0] = ordersVolumeKeyPrefix
copy(buf[1:], k.orderId)
return buf
}
type blocksInfoKey struct {
blockID proto.BlockID
}
func (k *blocksInfoKey) bytes() []byte {
idBytes := k.blockID.Bytes()
buf := make([]byte, 1+len(idBytes))
buf[0] = blocksInfoKeyPrefix
copy(buf[1:], idBytes)
return buf
}
type accountStorAddrToNumKey struct {
addressID proto.AddressID
}
func (k *accountStorAddrToNumKey) bytes() []byte {
buf := make([]byte, 1+proto.AddressIDSize)
buf[0] = accountStorAddrToNumKeyPrefix
copy(buf[1:], k.addressID[:])
return buf
}
type accountsDataStorKey struct {
addrNum uint64
entryKey string
}
func (k *accountsDataStorKey) accountPrefix() []byte {
buf := make([]byte, 1+8)
buf[0] = accountsDataStorKeyPrefix
binary.BigEndian.PutUint64(buf[1:9], k.addrNum)
return buf
}
func (k *accountsDataStorKey) bytes() []byte {
buf := make([]byte, 1+8+2+len(k.entryKey))
buf[0] = accountsDataStorKeyPrefix
binary.BigEndian.PutUint64(buf[1:9], k.addrNum)
proto.PutStringWithUInt16Len(buf[9:], k.entryKey)
return buf
}
func (k *accountsDataStorKey) unmarshal(data []byte) error {
if len(data) < minAccountsDataStorKeySize {
return errInvalidDataSize
}
if data[0] != accountsDataStorKeyPrefix {
return errInvalidPrefix
}
k.addrNum = binary.BigEndian.Uint64(data[1:9])
var err error
k.entryKey, err = proto.StringWithUInt16Len(data[9:])
if err != nil {
return errors.Wrap(err, "StringWithUInt16Len() failed")
}
return nil
}
type sponsorshipKey struct {
assetID proto.AssetID
}
func (k *sponsorshipKey) bytes() []byte {
buf := make([]byte, 1+proto.AssetIDSize)
buf[0] = sponsorshipKeyPrefix
copy(buf[1:], k.assetID[:])
return buf
}
type scriptKey interface {
scriptKeyMarker()
bytes() []byte
}
type accountScriptKey struct {
addr proto.AddressID
}
func (accountScriptKey) scriptKeyMarker() {}
func (k *accountScriptKey) bytes() []byte {
buf := make([]byte, 1+proto.AddressIDSize)
buf[0] = accountScriptKeyPrefix
copy(buf[1:], k.addr[:])
return buf
}
type assetScriptKey struct {
assetID proto.AssetID
}
func (assetScriptKey) scriptKeyMarker() {}
func (k *assetScriptKey) bytes() []byte {
buf := make([]byte, 1+proto.AssetIDSize)
buf[0] = assetScriptKeyPrefix
copy(buf[1:], k.assetID[:])
return buf
}
type scriptBasicInfoKey struct {
scriptKey scriptKey
}
func (k *scriptBasicInfoKey) bytes() []byte {
scriptKeyBytes := k.scriptKey.bytes()
buf := make([]byte, 1+len(scriptKeyBytes))
buf[0] = scriptBasicInfoKeyPrefix
copy(buf[1:], scriptKeyBytes)
return buf
}
type accountScriptComplexityKey struct {
ver int
addressID proto.AddressID
}
func (k *accountScriptComplexityKey) bytes() []byte {
buf := make([]byte, 2+proto.AddressIDSize)
buf[0] = accountScriptComplexityKeyPrefix
buf[1] = byte(k.ver)
copy(buf[2:], k.addressID[:])
return buf
}
type assetScriptComplexityKey struct {
asset proto.AssetID
}
func (k *assetScriptComplexityKey) bytes() []byte {
buf := make([]byte, 1+proto.AssetIDSize)
buf[0] = assetScriptComplexityKeyPrefix
copy(buf[1:], k.asset[:])
return buf
}
type accountOriginalEstimatorVersionKey struct {
addressID proto.AddressID
}
func (k *accountOriginalEstimatorVersionKey) bytes() []byte {
buf := make([]byte, 1+proto.AddressIDSize)
buf[0] = accountOriginalEstimatorVersionKeyPrefix
copy(buf[1:], k.addressID[:])
return buf
}
type batchedStorKey struct {
prefix byte
internalKey []byte
batchNum uint32
}
func (k *batchedStorKey) prefixUntilBatch() []byte {
buf := make([]byte, 2+len(k.internalKey))
buf[0] = batchedStorKeyPrefix
buf[1] = k.prefix
copy(buf[2:], k.internalKey[:])
return buf
}
func (k *batchedStorKey) bytes() []byte {
buf := make([]byte, 2+len(k.internalKey)+4)
buf[0] = batchedStorKeyPrefix
buf[1] = k.prefix
copy(buf[2:], k.internalKey[:])
pos := 2 + len(k.internalKey)
binary.BigEndian.PutUint32(buf[pos:], k.batchNum)
return buf
}
type lastBatchKey struct {
prefix byte
internalKey []byte
}
func (k *lastBatchKey) bytes() []byte {
buf := make([]byte, 2+len(k.internalKey)+4)
buf[0] = lastBatchKeyPrefix
buf[1] = k.prefix
copy(buf[2:], k.internalKey[:])
return buf
}
type invokeResultKey struct {
invokeID crypto.Digest
}
func (k *invokeResultKey) bytes() []byte {
res := make([]byte, invokeResultKeySize)
res[0] = invokeResultKeyPrefix
copy(res[1:], k.invokeID[:])
return res
}
type stateHashKey struct {
height uint64
}
func (k *stateHashKey) bytes() []byte {
buf := make([]byte, 9)
buf[0] = stateHashKeyPrefix
binary.BigEndian.PutUint64(buf[1:], k.height)
return buf
}
type hitSourceKey struct {
height uint64
}
func (k *hitSourceKey) bytes() []byte {
buf := make([]byte, 9)
buf[0] = hitSourceKeyPrefix
binary.LittleEndian.PutUint64(buf[1:], k.height)
return buf
}