From a037ca3e1970531d7b1ef17da162b214d016db17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Tue, 3 Dec 2024 16:15:13 -0800 Subject: [PATCH] adjust code to Cadence API changes related to storage map domains --- cmd/util/cmd/check-storage/cmd.go | 27 ++- .../evm_account_storage_health.go | 29 ++- .../evm_account_storage_health_test.go | 15 +- cmd/util/cmd/diff-states/cmd.go | 2 +- .../cmd/generate-authorization-fixes/cmd.go | 6 +- .../ledger/migrations/cadence_value_diff.go | 174 +++++++++--------- .../migrations/cadence_value_diff_test.go | 58 +++--- .../filter_unreferenced_slabs_migration.go | 15 +- ...ilter_unreferenced_slabs_migration_test.go | 7 +- .../ledger/migrations/migrator_runtime.go | 2 +- 10 files changed, 201 insertions(+), 134 deletions(-) diff --git a/cmd/util/cmd/check-storage/cmd.go b/cmd/util/cmd/check-storage/cmd.go index 5bd4e1d634a..8b8e08b2d26 100644 --- a/cmd/util/cmd/check-storage/cmd.go +++ b/cmd/util/cmd/check-storage/cmd.go @@ -3,6 +3,7 @@ package check_storage import ( "context" + "github.com/onflow/cadence/interpreter" "github.com/rs/zerolog/log" "github.com/spf13/cobra" "golang.org/x/sync/errgroup" @@ -322,9 +323,28 @@ func checkAccountStorageHealth(accountRegisters *registers.AccountRegisters, nWo // Check atree storage health ledger := ®isters.ReadOnlyLedger{Registers: accountRegisters} - storage := runtime.NewStorage(ledger, nil) + storage := runtime.NewStorage(ledger, nil, runtime.StorageConfig{}) + + inter, err := interpreter.NewInterpreter( + nil, + nil, + &interpreter.Config{ + Storage: storage, + }, + ) + if err != nil { + issues = append( + issues, + accountStorageIssue{ + Address: address.Hex(), + Kind: storageErrorKindString[otherErrorKind], + Msg: err.Error(), + }, + ) + return issues + } - err = util.CheckStorageHealth(address, storage, accountRegisters, util.StorageMapDomains, nWorkers) + err = util.CheckStorageHealth(inter, address, storage, accountRegisters, common.AllStorageDomains, nWorkers) if err != nil { issues = append( issues, @@ -332,7 +352,8 @@ func checkAccountStorageHealth(accountRegisters *registers.AccountRegisters, nWo Address: address.Hex(), Kind: storageErrorKindString[cadenceAtreeStorageErrorKind], Msg: err.Error(), - }) + }, + ) } // TODO: check health of non-atree registers diff --git a/cmd/util/cmd/check-storage/evm_account_storage_health.go b/cmd/util/cmd/check-storage/evm_account_storage_health.go index 80c57cd5a67..1a791438afa 100644 --- a/cmd/util/cmd/check-storage/evm_account_storage_health.go +++ b/cmd/util/cmd/check-storage/evm_account_storage_health.go @@ -6,6 +6,7 @@ import ( "fmt" "slices" + "github.com/onflow/cadence/interpreter" "golang.org/x/exp/maps" "github.com/onflow/atree" @@ -13,7 +14,6 @@ import ( "github.com/onflow/cadence/common" "github.com/onflow/cadence/runtime" - "github.com/onflow/flow-go/cmd/util/ledger/util" "github.com/onflow/flow-go/cmd/util/ledger/util/registers" "github.com/onflow/flow-go/fvm/evm/emulator/state" "github.com/onflow/flow-go/model/flow" @@ -70,16 +70,35 @@ func checkCadenceAtreeRegistersInEVMAccount( ) []accountStorageIssue { var issues []accountStorageIssue - storage := runtime.NewStorage(ledger, nil) + storage := runtime.NewStorage(ledger, nil, runtime.StorageConfig{}) + + inter, err := interpreter.NewInterpreter( + nil, + nil, + &interpreter.Config{ + Storage: storage, + }, + ) + if err != nil { + issues = append( + issues, + accountStorageIssue{ + Address: address.Hex(), + Kind: storageErrorKindString[otherErrorKind], + Msg: fmt.Sprintf("failed to create interpreter for cadence registers: %s", err), + }, + ) + return issues + } // Load Cadence domains storage map, so atree slab iterator can traverse connected slabs from loaded root slab. // NOTE: don't preload all atree slabs in evm account because evm-atree registers require evm-atree decoder. - for _, domain := range util.StorageMapDomains { - _ = storage.GetStorageMap(address, domain, false) + for _, domain := range common.AllStorageDomains { + _ = storage.GetDomainStorageMap(inter, address, domain, false) } - err := storage.CheckHealth() + err = storage.CheckHealth() if err != nil { issues = append( issues, diff --git a/cmd/util/cmd/check-storage/evm_account_storage_health_test.go b/cmd/util/cmd/check-storage/evm_account_storage_health_test.go index ea3384cc9fb..59cbe2163c5 100644 --- a/cmd/util/cmd/check-storage/evm_account_storage_health_test.go +++ b/cmd/util/cmd/check-storage/evm_account_storage_health_test.go @@ -111,7 +111,8 @@ func createEVMStorage(t *testing.T, ledger atree.Ledger, address common.Address) } func createCadenceStorage(t *testing.T, ledger atree.Ledger, address common.Address) { - storage := runtime.NewStorage(ledger, nil) + + storage := runtime.NewStorage(ledger, nil, runtime.StorageConfig{}) inter, err := interpreter.NewInterpreter( nil, @@ -123,13 +124,17 @@ func createCadenceStorage(t *testing.T, ledger atree.Ledger, address common.Addr require.NoError(t, err) // Create storage and public domains - for _, domain := range []string{"storage", "public"} { - storageDomain := storage.GetStorageMap(address, domain, true) + for _, domain := range []common.StorageDomain{ + common.StorageDomainPathStorage, + common.StorageDomainPathPublic, + } { + storageDomain := storage.GetDomainStorageMap(inter, address, domain, true) // Create large domain map so there are more than one atree registers under the hood. for i := 0; i < 100; i++ { - key := interpreter.StringStorageMapKey(domain + "_key_" + strconv.Itoa(i)) - value := interpreter.NewUnmeteredStringValue(domain + "_value_" + strconv.Itoa(i)) + domainStr := domain.Identifier() + key := interpreter.StringStorageMapKey(domainStr + "_key_" + strconv.Itoa(i)) + value := interpreter.NewUnmeteredStringValue(domainStr + "_value_" + strconv.Itoa(i)) storageDomain.SetValue(inter, key, value) } } diff --git a/cmd/util/cmd/diff-states/cmd.go b/cmd/util/cmd/diff-states/cmd.go index ec92cf591e9..19a598bd916 100644 --- a/cmd/util/cmd/diff-states/cmd.go +++ b/cmd/util/cmd/diff-states/cmd.go @@ -405,7 +405,7 @@ func diffAccount( ).DiffStates( accountRegisters1, accountRegisters2, - util.StorageMapDomains, + common.AllStorageDomains, ) } diff --git a/cmd/util/cmd/generate-authorization-fixes/cmd.go b/cmd/util/cmd/generate-authorization-fixes/cmd.go index ac2e5825aea..845ae5cbec8 100644 --- a/cmd/util/cmd/generate-authorization-fixes/cmd.go +++ b/cmd/util/cmd/generate-authorization-fixes/cmd.go @@ -11,7 +11,6 @@ import ( "github.com/onflow/cadence/common" "github.com/onflow/cadence/interpreter" "github.com/onflow/cadence/sema" - "github.com/onflow/cadence/stdlib" "github.com/rs/zerolog/log" "github.com/schollz/progressbar/v3" "github.com/spf13/cobra" @@ -334,9 +333,10 @@ func (g *AuthorizationFixGenerator) generateFixesForAccount(address common.Addre log.Fatal().Err(err) } - capabilityControllerStorage := mr.Storage.GetStorageMap( + capabilityControllerStorage := mr.Storage.GetDomainStorageMap( + mr.Interpreter, address, - stdlib.CapabilityControllerStorageDomain, + common.StorageDomainCapabilityController, false, ) if capabilityControllerStorage == nil { diff --git a/cmd/util/ledger/migrations/cadence_value_diff.go b/cmd/util/ledger/migrations/cadence_value_diff.go index 1887fad1264..9241c4b5ac4 100644 --- a/cmd/util/ledger/migrations/cadence_value_diff.go +++ b/cmd/util/ledger/migrations/cadence_value_diff.go @@ -102,7 +102,7 @@ func NewCadenceValueDiffReporter( } } -func (dr *CadenceValueDiffReporter) DiffStates(oldRegs, newRegs registers.Registers, domains []string) { +func (dr *CadenceValueDiffReporter) DiffStates(oldRegs, newRegs registers.Registers, domains []common.StorageDomain) { oldStorage := newReadonlyStorage(oldRegs) @@ -136,14 +136,50 @@ func (dr *CadenceValueDiffReporter) DiffStates(oldRegs, newRegs registers.Regist return } + oldInter, err := interpreter.NewInterpreter( + nil, + nil, + &interpreter.Config{ + Storage: oldStorage, + }, + ) + if err != nil { + dr.reportWriter.Write( + diffError{ + Address: dr.address.Hex(), + Kind: diffErrorKindString[abortErrorKind], + Msg: fmt.Sprintf("failed to create interpreter for old registers: %s", err), + }, + ) + return + } + + newInter, err := interpreter.NewInterpreter( + nil, + nil, + &interpreter.Config{ + Storage: newStorage, + }, + ) + if err != nil { + dr.reportWriter.Write( + diffError{ + Address: dr.address.Hex(), + Kind: diffErrorKindString[abortErrorKind], + Msg: fmt.Sprintf("failed to create interpreter for new registers: %s", err), + }, + ) + return + } + if oldRegs.Count() > minLargeAccountRegisterCount { // Add concurrency to diff domains var g errgroup.Group // NOTE: preload storage map in the same goroutine for _, domain := range domains { - _ = oldStorage.GetStorageMap(dr.address, domain, false) - _ = newStorage.GetStorageMap(dr.address, domain, false) + _ = oldStorage.GetDomainStorageMap(oldInter, dr.address, domain, false) + _ = newStorage.GetDomainStorageMap(newInter, dr.address, domain, false) } // Create goroutine to diff storage domain @@ -158,7 +194,7 @@ func (dr *CadenceValueDiffReporter) DiffStates(oldRegs, newRegs registers.Regist return fmt.Errorf("failed to create runtime for new registers: %s", err) } - dr.diffDomain(oldRuntime, newRuntime, common.PathDomainStorage.Identifier()) + dr.diffDomain(oldRuntime, newRuntime, common.StorageDomainPathStorage) return nil }) @@ -175,7 +211,7 @@ func (dr *CadenceValueDiffReporter) DiffStates(oldRegs, newRegs registers.Regist } for _, domain := range domains { - if domain != common.PathDomainStorage.Identifier() { + if domain != common.StorageDomainPathStorage { dr.diffDomain(oldRuntime, newRuntime, domain) } } @@ -226,14 +262,14 @@ func (dr *CadenceValueDiffReporter) DiffStates(oldRegs, newRegs registers.Regist func (dr *CadenceValueDiffReporter) diffDomain( oldRuntime *readonlyStorageRuntime, newRuntime *readonlyStorageRuntime, - domain string, + domain common.StorageDomain, ) { defer func() { if r := recover(); r != nil { dr.reportWriter.Write( diffProblem{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffErrorKindString[abortErrorKind], Msg: fmt.Sprintf( "panic while diffing storage maps: %s", @@ -244,8 +280,8 @@ func (dr *CadenceValueDiffReporter) diffDomain( } }() - oldStorageMap := oldRuntime.Storage.GetStorageMap(dr.address, domain, false) - newStorageMap := newRuntime.Storage.GetStorageMap(dr.address, domain, false) + oldStorageMap := oldRuntime.Storage.GetDomainStorageMap(oldRuntime.Interpreter, dr.address, domain, false) + newStorageMap := newRuntime.Storage.GetDomainStorageMap(newRuntime.Interpreter, dr.address, domain, false) if oldStorageMap == nil && newStorageMap == nil { // No storage maps for this domain. @@ -256,7 +292,7 @@ func (dr *CadenceValueDiffReporter) diffDomain( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[storageMapExistDiffKind], Msg: fmt.Sprintf( "old storage map doesn't exist, new storage map has %d elements with keys %v", @@ -272,7 +308,7 @@ func (dr *CadenceValueDiffReporter) diffDomain( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[storageMapExistDiffKind], Msg: fmt.Sprintf( "new storage map doesn't exist, old storage map has %d elements with keys %v", @@ -294,7 +330,7 @@ func (dr *CadenceValueDiffReporter) diffDomain( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[storageMapKeyDiffKind], Msg: fmt.Sprintf( "old storage map has %d elements with keys %v, that are not present in new storge map", @@ -309,7 +345,7 @@ func (dr *CadenceValueDiffReporter) diffDomain( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[storageMapKeyDiffKind], Msg: fmt.Sprintf( "new storage map has %d elements with keys %v, that are not present in old storge map", @@ -325,7 +361,7 @@ func (dr *CadenceValueDiffReporter) diffDomain( getValues := func(key any) (interpreter.Value, interpreter.Value, *util.Trace, bool) { - trace := util.NewTrace(fmt.Sprintf("%s[%v]", domain, key)) + trace := util.NewTrace(fmt.Sprintf("%s[%v]", domain.Identifier(), key)) var mapKey interpreter.StorageMapKey @@ -346,7 +382,7 @@ func (dr *CadenceValueDiffReporter) diffDomain( dr.reportWriter.Write( diffProblem{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffErrorKindString[storageMapKeyNotImplementingStorageMapKeyDiffErrorKind], Trace: trace.String(), Msg: fmt.Sprintf( @@ -386,7 +422,7 @@ func (dr *CadenceValueDiffReporter) diffDomain( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[storageMapValueDiffKind], Msg: "storage map elements are different", Trace: trace.String(), @@ -400,7 +436,7 @@ func (dr *CadenceValueDiffReporter) diffDomain( } // Skip goroutine overhead for non-storage domain and small accounts. - if domain != common.PathDomainStorage.Identifier() || + if domain != common.StorageDomainPathStorage || oldRuntime.PayloadCount < minLargeAccountRegisterCount || len(sharedKeys) == 1 { @@ -448,42 +484,14 @@ func (dr *CadenceValueDiffReporter) diffDomain( for i := 0; i < nWorkers; i++ { g.Go(func() error { - oldInterpreter, err := interpreter.NewInterpreter( - nil, - nil, - &interpreter.Config{ - Storage: oldRuntime.Storage, - }, - ) - if err != nil { - dr.reportWriter.Write( - diffError{ - Address: dr.address.Hex(), - Kind: diffErrorKindString[abortErrorKind], - Msg: fmt.Sprintf("failed to create interpreter for old registers: %s", err), - }) - return nil - } - - newInterpreter, err := interpreter.NewInterpreter( - nil, - nil, - &interpreter.Config{ - Storage: newRuntime.Storage, - }, - ) - if err != nil { - dr.reportWriter.Write( - diffError{ - Address: dr.address.Hex(), - Kind: diffErrorKindString[abortErrorKind], - Msg: fmt.Sprintf("failed to create interpreter for new registers: %s", err), - }) - return nil - } - for job := range jobs { - diffValues(oldInterpreter, job.oldValue, newInterpreter, job.newValue, job.trace) + diffValues( + oldRuntime.Interpreter, + job.oldValue, + newRuntime.Interpreter, + job.newValue, + job.trace, + ) } return nil @@ -513,7 +521,7 @@ func (dr *CadenceValueDiffReporter) diffDomain( diffError{ Address: dr.address.Hex(), Kind: diffErrorKindString[abortErrorKind], - Msg: fmt.Sprintf("failed to diff domain %s: %s", domain, err), + Msg: fmt.Sprintf("failed to diff domain %s: %s", domain.Identifier(), err), }) } @@ -532,7 +540,7 @@ func (dr *CadenceValueDiffReporter) diffValues( v interpreter.Value, otherInterpreter *interpreter.Interpreter, other interpreter.Value, - domain string, + domain common.StorageDomain, trace *util.Trace, ) (hasDifference bool) { switch v := v.(type) { @@ -558,7 +566,7 @@ func (dr *CadenceValueDiffReporter) diffEquatable( v interpreter.Value, otherInterpreter *interpreter.Interpreter, other interpreter.Value, - domain string, + domain common.StorageDomain, trace *util.Trace, ) (hasDifference bool) { @@ -567,7 +575,7 @@ func (dr *CadenceValueDiffReporter) diffEquatable( dr.reportWriter.Write( diffProblem{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffErrorKindString[abortErrorKind], Trace: trace.String(), Msg: fmt.Sprintf( @@ -584,7 +592,7 @@ func (dr *CadenceValueDiffReporter) diffEquatable( dr.reportWriter.Write( diffProblem{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffErrorKindString[cadenceValueNotImplementEquatableValueDiffErrorKind], Trace: trace.String(), Msg: fmt.Sprintf( @@ -600,7 +608,7 @@ func (dr *CadenceValueDiffReporter) diffEquatable( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueDiffKind], Msg: fmt.Sprintf("values differ: %T vs %T", oldValue, other), Trace: trace.String(), @@ -620,7 +628,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceSomeValue( v *interpreter.SomeValue, otherInterpreter *interpreter.Interpreter, other interpreter.Value, - domain string, + domain common.StorageDomain, trace *util.Trace, ) (hasDifference bool) { @@ -629,7 +637,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceSomeValue( dr.reportWriter.Write( diffProblem{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffErrorKindString[abortErrorKind], Trace: trace.String(), Msg: fmt.Sprintf( @@ -646,7 +654,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceSomeValue( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueTypeDiffKind], Trace: trace.String(), Msg: fmt.Sprintf("types differ: %T != %T", v, other), @@ -673,7 +681,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceArrayValue( v *interpreter.ArrayValue, otherInterpreter *interpreter.Interpreter, other interpreter.Value, - domain string, + domain common.StorageDomain, trace *util.Trace, ) (hasDifference bool) { @@ -682,7 +690,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceArrayValue( dr.reportWriter.Write( diffProblem{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffErrorKindString[abortErrorKind], Trace: trace.String(), Msg: fmt.Sprintf( @@ -699,7 +707,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceArrayValue( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueTypeDiffKind], Trace: trace.String(), Msg: fmt.Sprintf("types differ: %T != %T", v, other), @@ -713,7 +721,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceArrayValue( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueStaticTypeDiffKind], Trace: trace.String(), Msg: fmt.Sprintf("array static types differ: nil != %s", otherArray.Type), @@ -726,7 +734,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceArrayValue( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueStaticTypeDiffKind], Trace: trace.String(), Msg: fmt.Sprintf("array static types differ: %s != nil", v.Type), @@ -739,7 +747,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceArrayValue( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueStaticTypeDiffKind], Trace: trace.String(), Msg: fmt.Sprintf("array static types differ: %s != %s", v.Type, otherArray.Type), @@ -752,7 +760,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceArrayValue( d := difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueDiffKind], Trace: trace.String(), Msg: fmt.Sprintf("array counts differ: %d != %d", count, otherArray.Count()), @@ -793,7 +801,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceCompositeValue( v *interpreter.CompositeValue, otherInterpreter *interpreter.Interpreter, other interpreter.Value, - domain string, + domain common.StorageDomain, trace *util.Trace, ) (hasDifference bool) { @@ -802,7 +810,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceCompositeValue( dr.reportWriter.Write( diffProblem{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffErrorKindString[abortErrorKind], Trace: trace.String(), Msg: fmt.Sprintf( @@ -819,7 +827,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceCompositeValue( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueTypeDiffKind], Trace: trace.String(), Msg: fmt.Sprintf("types differ: %T != %T", v, other), @@ -833,7 +841,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceCompositeValue( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueStaticTypeDiffKind], Trace: trace.String(), Msg: fmt.Sprintf( @@ -849,7 +857,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceCompositeValue( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueStaticTypeDiffKind], Trace: trace.String(), Msg: fmt.Sprintf( @@ -881,7 +889,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceCompositeValue( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueDiffKind], Trace: trace.String(), Msg: fmt.Sprintf( @@ -899,7 +907,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceCompositeValue( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueDiffKind], Trace: trace.String(), Msg: fmt.Sprintf( @@ -937,7 +945,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceDictionaryValue( v *interpreter.DictionaryValue, otherInterpreter *interpreter.Interpreter, other interpreter.Value, - domain string, + domain common.StorageDomain, trace *util.Trace, ) (hasDifference bool) { @@ -946,7 +954,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceDictionaryValue( dr.reportWriter.Write( diffProblem{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffErrorKindString[abortErrorKind], Trace: trace.String(), Msg: fmt.Sprintf( @@ -963,7 +971,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceDictionaryValue( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueTypeDiffKind], Trace: trace.String(), Msg: fmt.Sprintf("types differ: %T != %T", v, other), @@ -977,7 +985,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceDictionaryValue( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueStaticTypeDiffKind], Trace: trace.String(), Msg: fmt.Sprintf( @@ -1035,7 +1043,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceDictionaryValue( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueDiffKind], Trace: trace.String(), Msg: fmt.Sprintf( @@ -1065,7 +1073,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceDictionaryValue( dr.reportWriter.Write( difference{ Address: dr.address.Hex(), - Domain: domain, + Domain: domain.Identifier(), Kind: diffKindString[cadenceValueDiffKind], Trace: trace.String(), Msg: fmt.Sprintf( @@ -1080,7 +1088,7 @@ func (dr *CadenceValueDiffReporter) diffCadenceDictionaryValue( return hasDifference } -func getStorageMapKeys(storageMap *interpreter.StorageMap) []any { +func getStorageMapKeys(storageMap *interpreter.DomainStorageMap) []any { keys := make([]any, 0, storageMap.Count()) iter := storageMap.Iterator(nil) @@ -1137,7 +1145,7 @@ func min(a, b int) int { func newReadonlyStorage(regs registers.Registers) *runtime.Storage { ledger := ®isters.ReadOnlyLedger{Registers: regs} - return runtime.NewStorage(ledger, nil) + return runtime.NewStorage(ledger, nil, runtime.StorageConfig{}) } type readonlyStorageRuntime struct { diff --git a/cmd/util/ledger/migrations/cadence_value_diff_test.go b/cmd/util/ledger/migrations/cadence_value_diff_test.go index c125859a24d..43b953dce80 100644 --- a/cmd/util/ledger/migrations/cadence_value_diff_test.go +++ b/cmd/util/ledger/migrations/cadence_value_diff_test.go @@ -24,7 +24,7 @@ func TestDiffCadenceValues(t *testing.T) { address, err := common.HexToAddress("0x1") require.NoError(t, err) - domain := common.PathDomainStorage.Identifier() + const domain = common.StorageDomainPathStorage t.Run("no diff", func(t *testing.T) { t.Parallel() @@ -36,7 +36,7 @@ func TestDiffCadenceValues(t *testing.T) { diffReporter.DiffStates( createTestRegisters(t, address, domain), createTestRegisters(t, address, domain), - []string{domain}, + []common.StorageDomain{domain}, ) require.NoError(t, err) require.Equal(t, 0, len(writer.entries)) @@ -52,7 +52,7 @@ func TestDiffCadenceValues(t *testing.T) { diffReporter.DiffStates( createTestRegisters(t, address, domain), registers.NewByAccount(), - []string{domain}, + []common.StorageDomain{domain}, ) require.NoError(t, err) require.Equal(t, 1, len(writer.entries)) @@ -60,7 +60,7 @@ func TestDiffCadenceValues(t *testing.T) { diff := writer.entries[0].(difference) require.Equal(t, diffKindString[storageMapExistDiffKind], diff.Kind) require.Equal(t, address.Hex(), diff.Address) - require.Equal(t, domain, diff.Domain) + require.Equal(t, domain.Identifier(), diff.Domain) }) t.Run("storage maps have different sets of keys", func(t *testing.T) { @@ -73,7 +73,7 @@ func TestDiffCadenceValues(t *testing.T) { diffReporter.DiffStates( createTestRegisters(t, address, domain), createStorageMapRegisters(t, address, domain, []string{"unique_key"}, []interpreter.Value{interpreter.UInt64Value(0)}), - []string{domain}, + []common.StorageDomain{domain}, ) require.NoError(t, err) @@ -86,7 +86,7 @@ func TestDiffCadenceValues(t *testing.T) { diff := entry.(difference) require.Equal(t, diffKindString[storageMapKeyDiffKind], diff.Kind) require.Equal(t, address.Hex(), diff.Address) - require.Equal(t, domain, diff.Domain) + require.Equal(t, domain.Identifier(), diff.Domain) } }) @@ -100,7 +100,7 @@ func TestDiffCadenceValues(t *testing.T) { diffReporter.DiffStates( createStorageMapRegisters(t, address, domain, []string{"0", "1"}, []interpreter.Value{interpreter.UInt64Value(0), interpreter.UInt64Value(0)}), createStorageMapRegisters(t, address, domain, []string{"2", "0"}, []interpreter.Value{interpreter.UInt64Value(0), interpreter.UInt64Value(0)}), - []string{domain}, + []common.StorageDomain{domain}, ) require.NoError(t, err) @@ -113,7 +113,7 @@ func TestDiffCadenceValues(t *testing.T) { diff := entry.(difference) require.Equal(t, diffKindString[storageMapKeyDiffKind], diff.Kind) require.Equal(t, address.Hex(), diff.Address) - require.Equal(t, domain, diff.Domain) + require.Equal(t, domain.Identifier(), diff.Domain) } }) @@ -127,7 +127,7 @@ func TestDiffCadenceValues(t *testing.T) { diffReporter.DiffStates( createStorageMapRegisters(t, address, domain, []string{"0", "1"}, []interpreter.Value{interpreter.UInt64Value(100), interpreter.UInt64Value(101)}), createStorageMapRegisters(t, address, domain, []string{"0", "1"}, []interpreter.Value{interpreter.UInt64Value(111), interpreter.UInt64Value(101)}), - []string{domain}, + []common.StorageDomain{domain}, ) require.NoError(t, err) @@ -138,7 +138,7 @@ func TestDiffCadenceValues(t *testing.T) { diff := writer.entries[0].(difference) require.Equal(t, diffKindString[cadenceValueDiffKind], diff.Kind) require.Equal(t, address.Hex(), diff.Address) - require.Equal(t, domain, diff.Domain) + require.Equal(t, domain.Identifier(), diff.Domain) require.Equal(t, "storage[0]", diff.Trace) require.Equal(t, "100", diff.OldValue) require.Equal(t, "111", diff.NewValue) @@ -154,7 +154,7 @@ func TestDiffCadenceValues(t *testing.T) { diffReporter.DiffStates( createStorageMapRegisters(t, address, domain, []string{"0", "1"}, []interpreter.Value{interpreter.UInt64Value(100), interpreter.UInt64Value(101)}), createStorageMapRegisters(t, address, domain, []string{"0", "1"}, []interpreter.Value{interpreter.UInt64Value(111), interpreter.UInt64Value(102)}), - []string{domain}, + []common.StorageDomain{domain}, ) require.NoError(t, err) @@ -165,7 +165,7 @@ func TestDiffCadenceValues(t *testing.T) { diff := entry.(difference) require.Equal(t, diffKindString[cadenceValueDiffKind], diff.Kind) require.Equal(t, address.Hex(), diff.Address) - require.Equal(t, domain, diff.Domain) + require.Equal(t, domain.Identifier(), diff.Domain) require.True(t, diff.Trace == "storage[0]" || diff.Trace == "storage[1]") } }) @@ -204,7 +204,7 @@ func TestDiffCadenceValues(t *testing.T) { require.NoError(t, err) // Create new storage map - storageMap := mr.Storage.GetStorageMap(address, domain, true) + storageMap := mr.Storage.GetDomainStorageMap(mr.Interpreter, address, domain, true) nestedArray := interpreter.NewArrayValue( mr.Interpreter, @@ -260,7 +260,7 @@ func TestDiffCadenceValues(t *testing.T) { interpreter.UInt64Value(3), interpreter.UInt64Value(5), }), - []string{domain}, + []common.StorageDomain{domain}, ) require.NoError(t, err) @@ -272,7 +272,7 @@ func TestDiffCadenceValues(t *testing.T) { diff := entry.(difference) require.Equal(t, diffKindString[cadenceValueDiffKind], diff.Kind) require.Equal(t, address.Hex(), diff.Address) - require.Equal(t, domain, diff.Domain) + require.Equal(t, domain.Identifier(), diff.Domain) require.True(t, diff.Trace == "storage[key_0][0][0]" || diff.Trace == "storage[key_0][0][1]" || diff.Trace == "storage[key_0][0][2]") switch diff.Trace { @@ -325,7 +325,7 @@ func TestDiffCadenceValues(t *testing.T) { require.NoError(t, err) // Create new storage map - storageMap := mr.Storage.GetStorageMap(address, domain, true) + storageMap := mr.Storage.GetDomainStorageMap(mr.Interpreter, address, domain, true) nestedDict := interpreter.NewDictionaryValueWithAddress( mr.Interpreter, @@ -384,7 +384,7 @@ func TestDiffCadenceValues(t *testing.T) { interpreter.NewUnmeteredStringValue("dict_key_1"), interpreter.UInt64Value(3), }), - []string{domain}, + []common.StorageDomain{domain}, ) require.NoError(t, err) @@ -396,7 +396,7 @@ func TestDiffCadenceValues(t *testing.T) { diff := entry.(difference) require.Equal(t, diffKindString[cadenceValueDiffKind], diff.Kind) require.Equal(t, address.Hex(), diff.Address) - require.Equal(t, domain, diff.Domain) + require.Equal(t, domain.Identifier(), diff.Domain) require.True(t, diff.Trace == "storage[key_0][0][\"dict_key_0\"]" || diff.Trace == "storage[key_0][0][\"dict_key_1\"]") switch diff.Trace { @@ -445,7 +445,7 @@ func TestDiffCadenceValues(t *testing.T) { require.NoError(t, err) // Create new storage map - storageMap := mr.Storage.GetStorageMap(address, domain, true) + storageMap := mr.Storage.GetDomainStorageMap(mr.Interpreter, address, domain, true) var fields []interpreter.CompositeField @@ -515,7 +515,7 @@ func TestDiffCadenceValues(t *testing.T) { interpreter.UInt64Value(1), interpreter.UInt64Value(3), }), - []string{domain}, + []common.StorageDomain{domain}, ) require.NoError(t, err) @@ -527,7 +527,7 @@ func TestDiffCadenceValues(t *testing.T) { diff := entry.(difference) require.Equal(t, diffKindString[cadenceValueDiffKind], diff.Kind) require.Equal(t, address.Hex(), diff.Address) - require.Equal(t, domain, diff.Domain) + require.Equal(t, domain.Identifier(), diff.Domain) require.True(t, diff.Trace == "storage[key_0][0].Field_0" || diff.Trace == "storage[key_0][0].Field_1") switch diff.Trace { @@ -576,7 +576,7 @@ func TestDiffCadenceValues(t *testing.T) { require.NoError(t, err) // Create new storage map - storageMap := mr.Storage.GetStorageMap(address, domain, true) + storageMap := mr.Storage.GetDomainStorageMap(mr.Interpreter, address, domain, true) var fields []interpreter.CompositeField @@ -646,7 +646,7 @@ func TestDiffCadenceValues(t *testing.T) { interpreter.UInt64Value(1), interpreter.UInt64Value(3), }), - []string{domain}, + []common.StorageDomain{domain}, ) require.NoError(t, err) @@ -660,7 +660,7 @@ func TestDiffCadenceValues(t *testing.T) { diff := entry.(difference) require.Equal(t, diffKindString[cadenceValueDiffKind], diff.Kind) require.Equal(t, address.Hex(), diff.Address) - require.Equal(t, domain, diff.Domain) + require.Equal(t, domain.Identifier(), diff.Domain) require.True(t, diff.Trace == "storage[key_0][0].Field_0" || diff.Trace == "storage[key_0][0].Field_1") switch diff.Trace { @@ -678,7 +678,7 @@ func TestDiffCadenceValues(t *testing.T) { diff := writer.entries[2].(difference) require.Equal(t, diffKindString[storageMapValueDiffKind], diff.Kind) require.Equal(t, address.Hex(), diff.Address) - require.Equal(t, domain, diff.Domain) + require.Equal(t, domain.Identifier(), diff.Domain) require.Equal(t, "storage[key_0]", diff.Trace) require.Equal(t, "[S.test.Test(Field_1: 2, Field_0: 0)]", diff.OldValue) require.Equal(t, "[S.test.Test(Field_1: 3, Field_0: 1)]", diff.NewValue) @@ -688,7 +688,7 @@ func TestDiffCadenceValues(t *testing.T) { func createStorageMapRegisters( t *testing.T, address common.Address, - domain string, + domain common.StorageDomain, keys []string, values []interpreter.Value, ) registers.Registers { @@ -718,7 +718,7 @@ func createStorageMapRegisters( require.NoError(t, err) // Create new storage map - storageMap := mr.Storage.GetStorageMap(address, domain, true) + storageMap := mr.Storage.GetDomainStorageMap(mr.Interpreter, address, domain, true) for i, k := range keys { storageMap.WriteValue( @@ -747,7 +747,7 @@ func createStorageMapRegisters( return registers } -func createTestRegisters(t *testing.T, address common.Address, domain string) registers.Registers { +func createTestRegisters(t *testing.T, address common.Address, domain common.StorageDomain) registers.Registers { // Create account status payload accountStatus := environment.NewAccountStatus() @@ -773,7 +773,7 @@ func createTestRegisters(t *testing.T, address common.Address, domain string) re require.NoError(t, err) // Create new storage map - storageMap := mr.Storage.GetStorageMap(address, domain, true) + storageMap := mr.Storage.GetDomainStorageMap(mr.Interpreter, address, domain, true) // Add Cadence UInt64Value storageMap.WriteValue( diff --git a/cmd/util/ledger/migrations/filter_unreferenced_slabs_migration.go b/cmd/util/ledger/migrations/filter_unreferenced_slabs_migration.go index a9fac706c8c..91f82e37f6e 100644 --- a/cmd/util/ledger/migrations/filter_unreferenced_slabs_migration.go +++ b/cmd/util/ledger/migrations/filter_unreferenced_slabs_migration.go @@ -13,6 +13,7 @@ import ( "github.com/onflow/atree" "github.com/onflow/cadence/common" + "github.com/onflow/cadence/interpreter" "github.com/onflow/cadence/runtime" "github.com/rs/zerolog" @@ -91,9 +92,21 @@ func (m *FilterUnreferencedSlabsMigration) MigrateAccount( Registers: accountRegisters, }, nil, + runtime.StorageConfig{}, ) - err := util.CheckStorageHealth(address, storage, accountRegisters, AllStorageMapDomains, m.nWorkers) + inter, err := interpreter.NewInterpreter( + nil, + nil, + &interpreter.Config{ + Storage: storage, + }, + ) + if err != nil { + return fmt.Errorf("failed to create interpreter: %w", err) + } + + err = util.CheckStorageHealth(inter, address, storage, accountRegisters, common.AllStorageDomains, m.nWorkers) if err == nil { return nil } diff --git a/cmd/util/ledger/migrations/filter_unreferenced_slabs_migration_test.go b/cmd/util/ledger/migrations/filter_unreferenced_slabs_migration_test.go index 1d3149cb988..43b10e012d6 100644 --- a/cmd/util/ledger/migrations/filter_unreferenced_slabs_migration_test.go +++ b/cmd/util/ledger/migrations/filter_unreferenced_slabs_migration_test.go @@ -92,7 +92,7 @@ func TestFilterUnreferencedSlabs(t *testing.T) { return index, nil } - storage := runtime.NewStorage(payloadsLedger, nil) + storage := runtime.NewStorage(payloadsLedger, nil, runtime.StorageConfig{}) // {Int: Int} dict1StaticType := interpreter.NewDictionaryStaticType( @@ -156,9 +156,10 @@ func TestFilterUnreferencedSlabs(t *testing.T) { array, ) - storageMap := storage.GetStorageMap( + storageMap := storage.GetDomainStorageMap( + inter, testAddress, - common.PathDomainStorage.Identifier(), + common.StorageDomainPathStorage, true, ) diff --git a/cmd/util/ledger/migrations/migrator_runtime.go b/cmd/util/ledger/migrations/migrator_runtime.go index 2baf8bb2468..57f3f3b3693 100644 --- a/cmd/util/ledger/migrations/migrator_runtime.go +++ b/cmd/util/ledger/migrations/migrator_runtime.go @@ -142,7 +142,7 @@ func NewBasicMigrationRuntime(regs registers.Registers) *BasicMigrationRuntime { accounts := environment.NewAccounts(transactionState) accountsAtreeLedger := util.NewAccountsAtreeLedger(accounts) - runtimeStorage := runtime.NewStorage(accountsAtreeLedger, nil) + runtimeStorage := runtime.NewStorage(accountsAtreeLedger, nil, runtime.StorageConfig{}) return &BasicMigrationRuntime{ Registers: regs,