github.com/koko1123/flow-go-1@v0.29.6/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/koko1123/flow-go-1/engine/execution/state"
     9  	"github.com/koko1123/flow-go-1/fvm/environment"
    10  	"github.com/koko1123/flow-go-1/ledger"
    11  	"github.com/koko1123/flow-go-1/model/flow"
    12  )
    13  
    14  func KeyToRegisterID(key ledger.Key) (flow.RegisterID, error) {
    15  	if len(key.KeyParts) != 2 ||
    16  		key.KeyParts[0].Type != state.KeyPartOwner ||
    17  		key.KeyParts[1].Type != state.KeyPartKey {
    18  		return flow.RegisterID{}, fmt.Errorf("key not in expected format %s", key.String())
    19  	}
    20  
    21  	return flow.NewRegisterID(
    22  		string(key.KeyParts[0].Value),
    23  		string(key.KeyParts[1].Value),
    24  	), nil
    25  }
    26  
    27  func registerIDToKey(registerID flow.RegisterID) ledger.Key {
    28  	newKey := ledger.Key{}
    29  	newKey.KeyParts = []ledger.KeyPart{
    30  		{
    31  			Type:  state.KeyPartOwner,
    32  			Value: []byte(registerID.Owner),
    33  		},
    34  		{
    35  			Type:  state.KeyPartKey,
    36  			Value: []byte(registerID.Key),
    37  		},
    38  	}
    39  	return newKey
    40  }
    41  
    42  type AccountsAtreeLedger struct {
    43  	Accounts environment.Accounts
    44  }
    45  
    46  func NewAccountsAtreeLedger(accounts environment.Accounts) *AccountsAtreeLedger {
    47  	return &AccountsAtreeLedger{Accounts: accounts}
    48  }
    49  
    50  var _ atree.Ledger = &AccountsAtreeLedger{}
    51  
    52  func (a *AccountsAtreeLedger) GetValue(owner, key []byte) ([]byte, error) {
    53  	v, err := a.Accounts.GetValue(
    54  		flow.BytesToAddress(owner),
    55  		string(key),
    56  	)
    57  	if err != nil {
    58  		return nil, fmt.Errorf("getting value failed: %w", err)
    59  	}
    60  	return v, nil
    61  }
    62  
    63  func (a *AccountsAtreeLedger) SetValue(owner, key, value []byte) error {
    64  	err := a.Accounts.SetValue(
    65  		flow.BytesToAddress(owner),
    66  		string(key),
    67  		value,
    68  	)
    69  	if err != nil {
    70  		return fmt.Errorf("setting value failed: %w", err)
    71  	}
    72  	return nil
    73  }
    74  
    75  func (a *AccountsAtreeLedger) ValueExists(owner, key []byte) (exists bool, err error) {
    76  	v, err := a.GetValue(owner, key)
    77  	if err != nil {
    78  		return false, fmt.Errorf("checking value existence failed: %w", err)
    79  	}
    80  
    81  	return len(v) > 0, nil
    82  }
    83  
    84  // AllocateStorageIndex allocates new storage index under the owner accounts to store a new register
    85  func (a *AccountsAtreeLedger) AllocateStorageIndex(owner []byte) (atree.StorageIndex, error) {
    86  	v, err := a.Accounts.AllocateStorageIndex(flow.BytesToAddress(owner))
    87  	if err != nil {
    88  		return atree.StorageIndex{}, fmt.Errorf("storage address allocation failed: %w", err)
    89  	}
    90  	return v, nil
    91  }