-
Notifications
You must be signed in to change notification settings - Fork 178
/
utils.go
61 lines (50 loc) · 1.54 KB
/
utils.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
package migrations
import (
"fmt"
"github.com/onflow/atree"
"github.com/onflow/flow-go/fvm/environment"
"github.com/onflow/flow-go/model/flow"
)
type AccountsAtreeLedger struct {
Accounts environment.Accounts
}
func NewAccountsAtreeLedger(accounts environment.Accounts) *AccountsAtreeLedger {
return &AccountsAtreeLedger{Accounts: accounts}
}
var _ atree.Ledger = &AccountsAtreeLedger{}
func (a *AccountsAtreeLedger) GetValue(owner, key []byte) ([]byte, error) {
v, err := a.Accounts.GetValue(
flow.NewRegisterID(
flow.BytesToAddress(owner),
string(key)))
if err != nil {
return nil, fmt.Errorf("getting value failed: %w", err)
}
return v, nil
}
func (a *AccountsAtreeLedger) SetValue(owner, key, value []byte) error {
err := a.Accounts.SetValue(
flow.NewRegisterID(
flow.BytesToAddress(owner),
string(key)),
value)
if err != nil {
return fmt.Errorf("setting value failed: %w", err)
}
return nil
}
func (a *AccountsAtreeLedger) ValueExists(owner, key []byte) (exists bool, err error) {
v, err := a.GetValue(owner, key)
if err != nil {
return false, fmt.Errorf("checking value existence failed: %w", err)
}
return len(v) > 0, nil
}
// AllocateStorageIndex allocates new storage index under the owner accounts to store a new register
func (a *AccountsAtreeLedger) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) {
v, err := a.Accounts.AllocateStorageIndex(flow.BytesToAddress(owner))
if err != nil {
return atree.StorageIndex{}, fmt.Errorf("storage address allocation failed: %w", err)
}
return v, nil
}