github.com/onflow/flow-go@v0.33.17/cmd/util/ledger/migrations/utils.go (about) 1 package migrations 2 3 import ( 4 "fmt" 5 6 "github.com/onflow/atree" 7 8 "github.com/onflow/flow-go/fvm/environment" 9 "github.com/onflow/flow-go/model/flow" 10 ) 11 12 type AccountsAtreeLedger struct { 13 Accounts environment.Accounts 14 } 15 16 func NewAccountsAtreeLedger(accounts environment.Accounts) *AccountsAtreeLedger { 17 return &AccountsAtreeLedger{Accounts: accounts} 18 } 19 20 var _ atree.Ledger = &AccountsAtreeLedger{} 21 22 func (a *AccountsAtreeLedger) GetValue(owner, key []byte) ([]byte, error) { 23 v, err := a.Accounts.GetValue( 24 flow.NewRegisterID( 25 flow.BytesToAddress(owner), 26 string(key))) 27 if err != nil { 28 return nil, fmt.Errorf("getting value failed: %w", err) 29 } 30 return v, nil 31 } 32 33 func (a *AccountsAtreeLedger) SetValue(owner, key, value []byte) error { 34 err := a.Accounts.SetValue( 35 flow.NewRegisterID( 36 flow.BytesToAddress(owner), 37 string(key)), 38 value) 39 if err != nil { 40 return fmt.Errorf("setting value failed: %w", err) 41 } 42 return nil 43 } 44 45 func (a *AccountsAtreeLedger) ValueExists(owner, key []byte) (exists bool, err error) { 46 v, err := a.GetValue(owner, key) 47 if err != nil { 48 return false, fmt.Errorf("checking value existence failed: %w", err) 49 } 50 51 return len(v) > 0, nil 52 } 53 54 // AllocateStorageIndex allocates new storage index under the owner accounts to store a new register 55 func (a *AccountsAtreeLedger) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) { 56 v, err := a.Accounts.AllocateStorageIndex(flow.BytesToAddress(owner)) 57 if err != nil { 58 return atree.StorageIndex{}, fmt.Errorf("storage address allocation failed: %w", err) 59 } 60 return v, nil 61 }