-
Notifications
You must be signed in to change notification settings - Fork 179
/
accounts.go
671 lines (565 loc) · 19.4 KB
/
accounts.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
package state
import (
"bytes"
"encoding/binary"
"fmt"
"math/big"
"sort"
"github.com/fxamacker/cbor/v2"
"github.com/onflow/atree"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/crypto/hash"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/model/flow"
)
const (
KeyExists = "exists"
KeyCode = "code"
KeyContractNames = "contract_names"
KeyPublicKeyCount = "public_key_count"
KeyStorageUsed = "storage_used"
KeyAccountFrozen = "frozen"
KeyStorageIndex = "storage_index"
uint64StorageSize = 8
AccountFrozenValue = 1
AccountNotFrozenValue = 0
)
func keyPublicKey(index uint64) string {
return fmt.Sprintf("public_key_%d", index)
}
type Accounts interface {
Exists(address flow.Address) (bool, error)
Get(address flow.Address) (*flow.Account, error)
GetPublicKeyCount(address flow.Address) (uint64, error)
AppendPublicKey(address flow.Address, key flow.AccountPublicKey) error
GetPublicKey(address flow.Address, keyIndex uint64) (flow.AccountPublicKey, error)
SetPublicKey(address flow.Address, keyIndex uint64, publicKey flow.AccountPublicKey) ([]byte, error)
GetContractNames(address flow.Address) ([]string, error)
GetContract(contractName string, address flow.Address) ([]byte, error)
SetContract(contractName string, address flow.Address, contract []byte) error
DeleteContract(contractName string, address flow.Address) error
Create(publicKeys []flow.AccountPublicKey, newAddress flow.Address) error
GetValue(address flow.Address, key string) (flow.RegisterValue, error)
CheckAccountNotFrozen(address flow.Address) error
GetStorageUsed(address flow.Address) (uint64, error)
SetValue(address flow.Address, key string, value flow.RegisterValue) error
AllocateStorageIndex(address flow.Address) (atree.StorageIndex, error)
SetAccountFrozen(address flow.Address, frozen bool) error
}
var _ Accounts = &StatefulAccounts{}
type StatefulAccounts struct {
stateHolder *StateHolder
}
func NewAccounts(stateHolder *StateHolder) *StatefulAccounts {
return &StatefulAccounts{
stateHolder: stateHolder,
}
}
func (a *StatefulAccounts) AllocateStorageIndex(address flow.Address) (atree.StorageIndex, error) {
indexBytes, err := a.getValue(address, false, KeyStorageIndex)
if err != nil {
return atree.StorageIndex{}, err
}
if len(indexBytes) == 0 {
// if not exist for the first time set it to zero and return
indexBytes = []byte{0, 0, 0, 0, 0, 0, 0, 1}
} else if len(indexBytes) != uint64StorageSize {
return atree.StorageIndex{}, fmt.Errorf("invalid storage index byte size (%d != 8)", len(indexBytes))
}
var index atree.StorageIndex
copy(index[:], indexBytes[:8])
newIndexBytes := index.Next()
// store nil so that the setValue for new allocated slabs would be faster
// and won't do ledger getValue for every new slabs (currently happening to compute storage size changes)
// this way the getValue would load this value from deltas
key := atree.SlabIndexToLedgerKey(index)
err = a.stateHolder.State().Set(string(address.Bytes()), "", string(key), []byte{}, false)
if err != nil {
return atree.StorageIndex{}, fmt.Errorf("failed to store empty value for newly allocated storage index: %w", err)
}
// update the storageIndex bytes
err = a.setValue(address, false, KeyStorageIndex, newIndexBytes[:])
if err != nil {
return atree.StorageIndex{}, fmt.Errorf("failed to store the key storage index: %w", err)
}
return index, nil
}
func (a *StatefulAccounts) Get(address flow.Address) (*flow.Account, error) {
var ok bool
var err error
ok, err = a.Exists(address)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.NewAccountNotFoundError(address)
}
contracts := make(map[string][]byte)
contractNames, err := a.getContractNames(address)
if err != nil {
return nil, err
}
for _, name := range contractNames {
contract, err := a.getContract(name, address)
if err != nil {
return nil, err
}
contracts[name] = contract
}
var publicKeys []flow.AccountPublicKey
publicKeys, err = a.GetPublicKeys(address)
if err != nil {
return nil, err
}
return &flow.Account{
Address: address,
Keys: publicKeys,
Contracts: contracts,
}, nil
}
func (a *StatefulAccounts) Exists(address flow.Address) (bool, error) {
exists, err := a.getValue(address, false, KeyExists)
if err != nil {
return false, err
}
if len(exists) != 0 {
return true, nil
}
return false, nil
}
// Create account sets all required registers on an address.
func (a *StatefulAccounts) Create(publicKeys []flow.AccountPublicKey, newAddress flow.Address) error {
exists, err := a.Exists(newAddress)
if err != nil {
return err
}
if exists {
return errors.NewAccountAlreadyExistsError(newAddress)
}
storageUsedByStorageUsed := uint64(RegisterSize(newAddress, false, KeyStorageUsed, make([]byte, uint64StorageSize)))
err = a.setStorageUsed(newAddress, storageUsedByStorageUsed)
if err != nil {
return err
}
// mark that this account exists
err = a.setValue(newAddress, false, KeyExists, []byte{1})
if err != nil {
return err
}
return a.SetAllPublicKeys(newAddress, publicKeys)
}
func (a *StatefulAccounts) GetPublicKey(address flow.Address, keyIndex uint64) (flow.AccountPublicKey, error) {
publicKey, err := a.getValue(address, true, keyPublicKey(keyIndex))
if err != nil {
return flow.AccountPublicKey{}, err
}
if len(publicKey) == 0 {
return flow.AccountPublicKey{}, errors.NewAccountPublicKeyNotFoundError(address, keyIndex)
}
decodedPublicKey, err := flow.DecodeAccountPublicKey(publicKey, keyIndex)
if err != nil {
return flow.AccountPublicKey{}, fmt.Errorf("failed to decode public key: %w", err)
}
return decodedPublicKey, nil
}
func (a *StatefulAccounts) GetPublicKeyCount(address flow.Address) (uint64, error) {
countBytes, err := a.getValue(address, true, KeyPublicKeyCount)
if err != nil {
return 0, err
}
countInt := new(big.Int).SetBytes(countBytes)
if !countInt.IsUint64() {
return 0, fmt.Errorf(
"retrieved public key account count bytes (hex-encoded): %x does not represent valid uint64",
countBytes,
)
}
return countInt.Uint64(), nil
}
func (a *StatefulAccounts) setPublicKeyCount(address flow.Address, count uint64) error {
newCount := new(big.Int).SetUint64(count)
return a.setValue(address, true, KeyPublicKeyCount, newCount.Bytes())
}
func (a *StatefulAccounts) GetPublicKeys(address flow.Address) (publicKeys []flow.AccountPublicKey, err error) {
count, err := a.GetPublicKeyCount(address)
if err != nil {
return nil, fmt.Errorf("failed to get public key count of account: %w", err)
}
publicKeys = make([]flow.AccountPublicKey, count)
for i := uint64(0); i < count; i++ {
publicKey, err := a.GetPublicKey(address, i)
if err != nil {
return nil, err
}
publicKeys[i] = publicKey
}
return publicKeys, nil
}
func (a *StatefulAccounts) SetPublicKey(
address flow.Address,
keyIndex uint64,
publicKey flow.AccountPublicKey,
) (encodedPublicKey []byte, err error) {
err = publicKey.Validate()
if err != nil {
encoded, _ := publicKey.MarshalJSON()
return nil, errors.NewValueErrorf(string(encoded), "invalid public key value: %w", err)
}
encodedPublicKey, err = flow.EncodeAccountPublicKey(publicKey)
if err != nil {
encoded, _ := publicKey.MarshalJSON()
return nil, errors.NewValueErrorf(string(encoded), "invalid public key value: %w", err)
}
err = a.setValue(address, true, keyPublicKey(keyIndex), encodedPublicKey)
return encodedPublicKey, err
}
func (a *StatefulAccounts) SetAllPublicKeys(address flow.Address, publicKeys []flow.AccountPublicKey) error {
for i, publicKey := range publicKeys {
_, err := a.SetPublicKey(address, uint64(i), publicKey)
if err != nil {
return err
}
}
count := uint64(len(publicKeys)) // len returns int and this will not exceed uint64
return a.setPublicKeyCount(address, count)
}
func (a *StatefulAccounts) AppendPublicKey(address flow.Address, publicKey flow.AccountPublicKey) error {
if !IsValidAccountKeyHashAlgo(publicKey.HashAlgo) {
return errors.NewValueErrorf(publicKey.HashAlgo.String(), "hashing algorithm type not found")
}
if !IsValidAccountKeySignAlgo(publicKey.SignAlgo) {
return errors.NewValueErrorf(publicKey.SignAlgo.String(), "signature algorithm type not found")
}
count, err := a.GetPublicKeyCount(address)
if err != nil {
return err
}
_, err = a.SetPublicKey(address, count, publicKey)
if err != nil {
return err
}
return a.setPublicKeyCount(address, count+1)
}
func IsValidAccountKeySignAlgo(algo crypto.SigningAlgorithm) bool {
switch algo {
case crypto.ECDSAP256, crypto.ECDSASecp256k1:
return true
default:
return false
}
}
func IsValidAccountKeyHashAlgo(algo hash.HashingAlgorithm) bool {
switch algo {
case hash.SHA2_256, hash.SHA3_256:
return true
default:
return false
}
}
func ContractKey(contractName string) string {
return fmt.Sprintf("%s.%s", KeyCode, contractName)
}
func (a *StatefulAccounts) getContract(contractName string, address flow.Address) ([]byte, error) {
contract, err := a.getValue(address,
true,
ContractKey(contractName))
if err != nil {
return nil, err
}
return contract, nil
}
func (a *StatefulAccounts) setContract(contractName string, address flow.Address, contract []byte) error {
ok, err := a.Exists(address)
if err != nil {
return err
}
if !ok {
return errors.NewAccountNotFoundError(address)
}
var prevContract []byte
prevContract, err = a.getValue(address, true, ContractKey(contractName))
if err != nil {
return errors.NewContractNotFoundError(address, contractName)
}
// skip updating if the new contract equals the old
if bytes.Equal(prevContract, contract) {
return nil
}
err = a.setValue(address, true, ContractKey(contractName), contract)
if err != nil {
return err
}
return nil
}
func (a *StatefulAccounts) setContractNames(contractNames contractNames, address flow.Address) error {
ok, err := a.Exists(address)
if err != nil {
return err
}
if !ok {
return errors.NewAccountNotFoundError(address)
}
var buf bytes.Buffer
cborEncoder := cbor.NewEncoder(&buf)
err = cborEncoder.Encode(contractNames)
if err != nil {
msg := fmt.Sprintf("cannot encode contract names: %s", contractNames)
return errors.NewEncodingFailuref(msg, err)
}
newContractNames := buf.Bytes()
var prevContractNames []byte
prevContractNames, err = a.getValue(address, true, KeyContractNames)
if err != nil {
return fmt.Errorf("cannot retrieve current contract names: %w", err)
}
// skip updating if the new contract names equal the old
if bytes.Equal(prevContractNames, newContractNames) {
return nil
}
return a.setValue(address, true, KeyContractNames, newContractNames)
}
// GetStorageUsed returns the amount of storage used in bytes by this account
func (a *StatefulAccounts) GetStorageUsed(address flow.Address) (uint64, error) {
storageUsedRegister, err := a.getValue(address, false, KeyStorageUsed)
if err != nil {
return 0, err
}
if len(storageUsedRegister) != uint64StorageSize {
return 0, fmt.Errorf("account %s storage used is not initialized or not initialized correctly", address.Hex())
}
storageUsed, _, err := readUint64(storageUsedRegister)
if err != nil {
return 0, err
}
return storageUsed, nil
}
func (a *StatefulAccounts) setStorageUsed(address flow.Address, used uint64) error {
usedBinary := uint64ToBinary(used)
return a.setValue(address, false, KeyStorageUsed, usedBinary)
}
func (a *StatefulAccounts) GetValue(address flow.Address, key string) (flow.RegisterValue, error) {
return a.getValue(address, false, key)
}
func (a *StatefulAccounts) getValue(address flow.Address, isController bool, key string) (flow.RegisterValue, error) {
if isController {
return a.stateHolder.State().Get(string(address.Bytes()), string(address.Bytes()), key, a.stateHolder.EnforceInteractionLimits())
}
return a.stateHolder.State().Get(string(address.Bytes()), "", key, a.stateHolder.EnforceInteractionLimits())
}
// SetValue sets a value in address' storage
func (a *StatefulAccounts) SetValue(address flow.Address, key string, value flow.RegisterValue) error {
return a.setValue(address, false, key, value)
}
func (a *StatefulAccounts) setValue(address flow.Address, isController bool, key string, value flow.RegisterValue) error {
err := a.updateRegisterSizeChange(address, isController, key, value)
if err != nil {
return fmt.Errorf("failed to update storage used by key %s on account %s: %w", PrintableKey(key), address, err)
}
if isController {
return a.stateHolder.State().Set(string(address.Bytes()), string(address.Bytes()), key, value, a.stateHolder.EnforceInteractionLimits())
}
return a.stateHolder.State().Set(string(address.Bytes()), "", key, value, a.stateHolder.EnforceInteractionLimits())
}
func (a *StatefulAccounts) updateRegisterSizeChange(address flow.Address, isController bool, key string, value flow.RegisterValue) error {
if key == KeyStorageUsed {
// size of this register is always uint64StorageSize
// don't double check this to save time and prevent recursion
return nil
}
oldValue, err := a.getValue(address, isController, key)
if err != nil {
return err
}
sizeChange := int64(RegisterSize(address, isController, key, value) - RegisterSize(address, isController, key, oldValue))
if sizeChange == 0 {
// register size has not changed. Nothing to do
return nil
}
oldSize, err := a.GetStorageUsed(address)
if err != nil {
return err
}
// two paths to avoid casting uint to int
var newSize uint64
if sizeChange < 0 {
absChange := uint64(-sizeChange)
if absChange > oldSize {
// should never happen
return fmt.Errorf("storage used by key %s on account %s would be negative", PrintableKey(key), address.Hex())
}
newSize = oldSize - absChange
} else {
absChange := uint64(sizeChange)
newSize = oldSize + absChange
}
// this puts us back in the setValue method.
// The difference is that storage_used update exits early from this function so there isn't any recursion.
return a.setStorageUsed(address, newSize)
}
func RegisterSize(address flow.Address, isController bool, key string, value flow.RegisterValue) int {
if len(value) == 0 {
// registers with empty value won't (or don't) exist when stored
return 0
}
var registerID flow.RegisterID
if isController {
registerID = flow.NewRegisterID(string(address.Bytes()), string(address.Bytes()), key)
} else {
registerID = flow.NewRegisterID(string(address.Bytes()), "", key)
}
return getRegisterIDSize(registerID) + len(value)
}
// TODO replace with touch
// TODO handle errors
func (a *StatefulAccounts) touch(address flow.Address, isController bool, key string) {
if isController {
_, _ = a.stateHolder.State().Get(string(address.Bytes()), string(address.Bytes()), key, a.stateHolder.EnforceInteractionLimits())
return
}
_, _ = a.stateHolder.State().Get(string(address.Bytes()), "", key, a.stateHolder.EnforceInteractionLimits())
}
func (a *StatefulAccounts) TouchContract(contractName string, address flow.Address) {
contractNames, err := a.getContractNames(address)
if err != nil {
panic(err)
}
if contractNames.Has(contractName) {
a.touch(address,
true,
ContractKey(contractName))
}
}
// GetContractNames gets a sorted list of names of contracts deployed on an address
func (a *StatefulAccounts) GetContractNames(address flow.Address) ([]string, error) {
return a.getContractNames(address)
}
func (a *StatefulAccounts) getContractNames(address flow.Address) (contractNames, error) {
// TODO return fatal error if can't fetch
encContractNames, err := a.getValue(address, true, KeyContractNames)
if err != nil {
return nil, fmt.Errorf("cannot get deployed contract names: %w", err)
}
identifiers := make([]string, 0)
if len(encContractNames) > 0 {
buf := bytes.NewReader(encContractNames)
cborDecoder := cbor.NewDecoder(buf)
err = cborDecoder.Decode(&identifiers)
if err != nil {
return nil, fmt.Errorf("cannot decode deployed contract names %x: %w", encContractNames, err)
}
}
return identifiers, nil
}
func (a *StatefulAccounts) GetContract(contractName string, address flow.Address) ([]byte, error) {
contractNames, err := a.getContractNames(address)
if err != nil {
return nil, err
}
if !contractNames.Has(contractName) {
return nil, nil
}
return a.getContract(contractName, address)
}
func (a *StatefulAccounts) SetContract(contractName string, address flow.Address, contract []byte) error {
contractNames, err := a.getContractNames(address)
if err != nil {
return err
}
err = a.setContract(contractName, address, contract)
if err != nil {
return err
}
contractNames.add(contractName)
return a.setContractNames(contractNames, address)
}
func (a *StatefulAccounts) DeleteContract(contractName string, address flow.Address) error {
contractNames, err := a.getContractNames(address)
if err != nil {
return err
}
if !contractNames.Has(contractName) {
return nil
}
err = a.setContract(contractName, address, nil)
if err != nil {
return err
}
contractNames.remove(contractName)
return a.setContractNames(contractNames, address)
}
// This tries to compute the amount bytes that will be used by the ledger
// plus 2 on each part of the register id is due to header byte size needed
// for encoding and decoding
func getRegisterIDSize(inp flow.RegisterID) int {
size := 0
size += 2 + len(inp.Owner)
size += 2 + len(inp.Controller)
size += 2 + len(inp.Key)
return size
}
// uint64ToBinary converst a uint64 to a byte slice (big endian)
func uint64ToBinary(integer uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, integer)
return b
}
// readUint64 reads a uint64 from the input and returns the rest
func readUint64(input []byte) (value uint64, rest []byte, err error) {
if len(input) < 8 {
return 0, input, fmt.Errorf("input size (%d) is too small to read a uint64", len(input))
}
return binary.BigEndian.Uint64(input[:8]), input[8:], nil
}
func (a *StatefulAccounts) GetAccountFrozen(address flow.Address) (bool, error) {
frozen, err := a.getValue(address, false, KeyAccountFrozen)
if err != nil {
return false, err
}
if len(frozen) == 0 {
return false, err
}
return frozen[0] != AccountNotFrozenValue, nil
}
func (a *StatefulAccounts) SetAccountFrozen(address flow.Address, frozen bool) error {
val := make([]byte, 1) //zero value for byte is 0
if frozen {
val[0] = AccountFrozenValue
}
return a.setValue(address, false, KeyAccountFrozen, val)
}
// handy function to error out if account is frozen
func (a *StatefulAccounts) CheckAccountNotFrozen(address flow.Address) error {
frozen, err := a.GetAccountFrozen(address)
if err != nil {
return fmt.Errorf("cannot check account freeze status: %w", err)
}
if frozen {
return errors.NewFrozenAccountError(address)
}
return nil
}
// contractNames container for a list of contract names. Should always be sorted.
// To ensure this, don't sort while reading it from storage, but sort it while adding/removing elements
type contractNames []string
func (l contractNames) Has(contractNames string) bool {
i := sort.SearchStrings(l, contractNames)
return i != len(l) && l[i] == contractNames
}
func (l *contractNames) add(contractNames string) {
i := sort.SearchStrings(*l, contractNames)
if i != len(*l) && (*l)[i] == contractNames {
// list already contains element
return
}
*l = append(*l, "")
copy((*l)[i+1:], (*l)[i:])
(*l)[i] = contractNames
}
func (l *contractNames) remove(contractName string) {
i := sort.SearchStrings(*l, contractName)
if i == len(*l) || (*l)[i] != contractName {
// list does not contain the element
return
}
*l = append((*l)[:i], (*l)[i+1:]...)
}