Skip to content

Commit

Permalink
Merge pull request #3678 from onflow/bastian/refactor-account-storage
Browse files Browse the repository at this point in the history
[Account Storage Maps] Refactor storage into V1 and V2
  • Loading branch information
turbolent authored Nov 20, 2024
2 parents 0abfa9a + 4fd610a commit caaf254
Show file tree
Hide file tree
Showing 34 changed files with 2,813 additions and 1,572 deletions.
9 changes: 7 additions & 2 deletions cmd/decode-state-values/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,13 @@ type interpreterStorage struct {

var _ interpreter.Storage = &interpreterStorage{}

func (i interpreterStorage) GetStorageMap(_ *interpreter.Interpreter, _ common.Address, _ common.StorageDomain, _ bool) *interpreter.DomainStorageMap {
panic("unexpected GetStorageMap call")
func (i interpreterStorage) GetDomainStorageMap(
_ *interpreter.Interpreter,
_ common.Address,
_ common.StorageDomain,
_ bool,
) *interpreter.DomainStorageMap {
panic("unexpected GetDomainStorageMap call")
}

func (i interpreterStorage) CheckHealth() error {
Expand Down
5 changes: 5 additions & 0 deletions common/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package common

import (
"bytes"
"encoding/hex"
goErrors "errors"
"fmt"
Expand Down Expand Up @@ -112,6 +113,10 @@ func (a Address) HexWithPrefix() string {
return fmt.Sprintf("0x%x", [AddressLength]byte(a))
}

func (a Address) Compare(other Address) int {
return bytes.Compare(a[:], other[:])
}

// HexToAddress converts a hex string to an Address after
// ensuring that the hex string starts with the prefix 0x.
func HexToAddressAssertPrefix(h string) (Address, error) {
Expand Down
14 changes: 7 additions & 7 deletions interpreter/account_storagemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,20 @@ func (s *AccountStorageMap) NewDomain(
func (s *AccountStorageMap) WriteDomain(
interpreter *Interpreter,
domain common.StorageDomain,
storageMap *DomainStorageMap,
domainStorageMap *DomainStorageMap,
) (existed bool) {
if storageMap == nil {
if domainStorageMap == nil {
return s.removeDomain(interpreter, domain)
}
return s.setDomain(interpreter, domain, storageMap)
return s.setDomain(interpreter, domain, domainStorageMap)
}

// setDomain sets domain storage map in the account storage map and returns true if domain previously existed.
// If the given domain already stores a domain storage map, it is overwritten.
func (s *AccountStorageMap) setDomain(
interpreter *Interpreter,
domain common.StorageDomain,
storageMap *DomainStorageMap,
newDomainStorageMap *DomainStorageMap,
) (existed bool) {
interpreter.recordStorageMutation()

Expand All @@ -187,7 +187,7 @@ func (s *AccountStorageMap) setDomain(
key.AtreeValueCompare,
key.AtreeValueHashInput,
key.AtreeValue(),
storageMap.orderedMap,
newDomainStorageMap.orderedMap,
)
if err != nil {
panic(errors.NewExternalError(err))
Expand All @@ -196,10 +196,10 @@ func (s *AccountStorageMap) setDomain(
existed = existingValueStorable != nil
if existed {
// Create domain storage map from overwritten storable
domainStorageMap := newDomainStorageMapWithAtreeStorable(s.orderedMap.Storage, existingValueStorable)
existingDomainStorageMap := newDomainStorageMapWithAtreeStorable(s.orderedMap.Storage, existingValueStorable)

// Deep remove elements in domain storage map
domainStorageMap.DeepRemove(interpreter, true)
existingDomainStorageMap.DeepRemove(interpreter, true)

// Remove domain storage map slab
interpreter.RemoveReferencedSlab(existingValueStorable)
Expand Down
Loading

0 comments on commit caaf254

Please sign in to comment.