Skip to content

Commit

Permalink
Refactor storage domains to prevent import cycles
Browse files Browse the repository at this point in the history
Currently, various storage domains are defined in
different packages, such as:
- common
- runtime
- stdlib

This commit moves storage domains from different
packages to a single place: common/storagedomain.go.

This change will help us avoid import cycles when
using domains across different packages.
  • Loading branch information
fxamacker committed Nov 8, 2024
1 parent a56e521 commit d3bfdb8
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 52 deletions.
117 changes: 117 additions & 0 deletions common/storagedomain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package common

import (
"github.com/onflow/cadence/errors"
)

type StorageDomain uint8

const (
StorageDomainUnknown StorageDomain = iota

StorageDomainStorage

StorageDomainPrivate

StorageDomainPublic

StorageDomainContract

StorageDomainInbox

// StorageDomainCapabilityController is the storage domain which stores
// capability controllers by capability ID
StorageDomainCapabilityController

// StorageDomainCapabilityControllerTag is the storage domain which stores
// capability controller tags by capability ID
StorageDomainCapabilityControllerTag

// StorageDomainPathCapability is the storage domain which stores
// capability ID dictionaries (sets) by storage path identifier
StorageDomainPathCapability

// StorageDomainAccountCapability is the storage domain which
// records active account capability controller IDs
StorageDomainAccountCapability
)

var AllStorageDomains = []StorageDomain{
StorageDomainStorage,
StorageDomainPrivate,
StorageDomainPublic,
StorageDomainContract,
StorageDomainInbox,
StorageDomainCapabilityController,
StorageDomainCapabilityControllerTag,
StorageDomainPathCapability,
StorageDomainAccountCapability,
}

var AllStorageDomainsByIdentifier = map[string]StorageDomain{}

func init() {
for _, domain := range AllStorageDomains {
identifier := domain.Identifier()
AllStorageDomainsByIdentifier[identifier] = domain
}
}

func StorageDomainFromIdentifier(domain string) (StorageDomain, bool) {
result, ok := AllStorageDomainsByIdentifier[domain]
if !ok {
return StorageDomainUnknown, false
}
return result, true
}

func (d StorageDomain) Identifier() string {
switch d {
case StorageDomainStorage:
return PathDomainStorage.Identifier()

case StorageDomainPrivate:
return PathDomainPrivate.Identifier()

case StorageDomainPublic:
return PathDomainPublic.Identifier()

case StorageDomainContract:
return "contract"

case StorageDomainInbox:
return "inbox"

case StorageDomainCapabilityController:
return "cap_con"

case StorageDomainCapabilityControllerTag:
return "cap_tag"

case StorageDomainPathCapability:
return "path_cap"

case StorageDomainAccountCapability:
return "acc_cap"
}

panic(errors.NewUnreachableError())
}
4 changes: 2 additions & 2 deletions interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ func (interpreter *Interpreter) declareSelfVariable(value Value, locationRange L
}

func (interpreter *Interpreter) visitAssignment(
transferOperation ast.TransferOperation,
_ ast.TransferOperation,
targetGetterSetter getterSetter, targetType sema.Type,
valueExpression ast.Expression, valueType sema.Type,
position ast.HasPosition,
Expand Down Expand Up @@ -1271,7 +1271,7 @@ func (declarationInterpreter *Interpreter) declareNonEnumCompositeValue(
functions.Set(resourceDefaultDestroyEventName(compositeType), destroyEventConstructor)
}

applyDefaultFunctions := func(ty *sema.InterfaceType, code WrapperCode) {
applyDefaultFunctions := func(_ *sema.InterfaceType, code WrapperCode) {

// Apply default functions, if conforming type does not provide the function

Expand Down
2 changes: 1 addition & 1 deletion interpreter/value_composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ func (v *CompositeValue) getBaseValue(
return NewEphemeralReferenceValue(interpreter, functionAuthorization, v.base, baseType, locationRange)
}

func (v *CompositeValue) setBaseValue(interpreter *Interpreter, base *CompositeValue) {
func (v *CompositeValue) setBaseValue(_ *Interpreter, base *CompositeValue) {
v.base = base
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/capabilitycontrollers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3253,7 +3253,7 @@ func TestRuntimeCapabilityControllers(t *testing.T) {

storageMap := storage.GetStorageMap(
common.MustBytesToAddress([]byte{0x1}),
stdlib.PathCapabilityStorageDomain,
common.StorageDomainPathCapability.Identifier(),
false,
)
require.Zero(t, storageMap.Count())
Expand Down
2 changes: 1 addition & 1 deletion runtime/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func TestRuntimeContract(t *testing.T) {

getContractValueExists := func() bool {
storageMap := NewStorage(storage, nil).
GetStorageMap(signerAddress, StorageDomainContract, false)
GetStorageMap(signerAddress, common.StorageDomainContract.Identifier(), false)
if storageMap == nil {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ func (e *interpreterEnvironment) loadContract(
if addressLocation, ok := location.(common.AddressLocation); ok {
storageMap := e.storage.GetStorageMap(
addressLocation.Address,
StorageDomainContract,
common.StorageDomainContract.Identifier(),
false,
)
if storageMap != nil {
Expand Down
2 changes: 1 addition & 1 deletion runtime/ft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ func TestRuntimeBrokenFungibleTokenRecovery(t *testing.T) {

contractStorage := storage.GetStorageMap(
contractsAddress,
StorageDomainContract,
common.StorageDomainContract.Identifier(),
true,
)
contractStorage.SetValue(
Expand Down
2 changes: 1 addition & 1 deletion runtime/runtime_memory_metering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ func TestRuntimeMemoryMeteringErrors(t *testing.T) {

type memoryMeter map[common.MemoryKind]uint64

runtimeInterface := func(meter memoryMeter) *TestRuntimeInterface {
runtimeInterface := func(memoryMeter) *TestRuntimeInterface {
return &TestRuntimeInterface{
OnMeterMemory: func(usage common.MemoryUsage) error {
if usage.Kind == common.MemoryKindStringValue ||
Expand Down
4 changes: 2 additions & 2 deletions runtime/sharedstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ func TestRuntimeSharedState(t *testing.T) {
[]ownerKeyPair{
{
owner: signerAddress[:],
key: []byte(StorageDomainContract),
key: []byte(common.StorageDomainContract.Identifier()),
},
{
owner: signerAddress[:],
key: []byte(StorageDomainContract),
key: []byte(common.StorageDomainContract.Identifier()),
},
{
owner: signerAddress[:],
Expand Down
4 changes: 1 addition & 3 deletions runtime/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import (
"github.com/onflow/cadence/interpreter"
)

const StorageDomainContract = "contract"

type Storage struct {
*atree.PersistentSlabStorage
NewStorageMaps *orderedmap.OrderedMap[interpreter.StorageKey, atree.SlabIndex]
Expand Down Expand Up @@ -216,7 +214,7 @@ func (s *Storage) writeContractUpdate(
key interpreter.StorageKey,
contractValue *interpreter.CompositeValue,
) {
storageMap := s.GetStorageMap(key.Address, StorageDomainContract, true)
storageMap := s.GetStorageMap(key.Address, common.StorageDomainContract.Identifier(), true)
// NOTE: pass nil instead of allocating a Value-typed interface that points to nil
storageMapKey := interpreter.StringStorageMapKey(key.Key)
if contractValue == nil {
Expand Down
Loading

0 comments on commit d3bfdb8

Please sign in to comment.