Skip to content

Commit

Permalink
Update vm's storage layer to the API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Jan 29, 2025
1 parent 359ce2a commit 44406d4
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 23 deletions.
36 changes: 26 additions & 10 deletions bbq/vm/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,44 @@ func MustConvertStoredValue(gauge common.MemoryGauge, storage interpreter.Storag
}

func ReadStored(
gauge common.MemoryGauge,
storage interpreter.Storage,
config *Config,
address common.Address,
domain string,
identifier string,
) Value {
accountStorage := storage.GetStorageMap(address, domain, false)
storage := config.Storage

storageDomain, _ := common.StorageDomainFromIdentifier(domain)

accountStorage := storage.GetDomainStorageMap(
config.interpreter(),
address,
storageDomain,
false,
)
if accountStorage == nil {
return nil
}

referenced := accountStorage.ReadValue(gauge, interpreter.StringStorageMapKey(identifier))
referenced := accountStorage.ReadValue(config.MemoryGauge, interpreter.StringStorageMapKey(identifier))
return InterpreterValueToVMValue(storage, referenced)
}

func WriteStored(
config *Config,
storageAddress common.Address,
domain string,
domain common.StorageDomain,
key interpreter.StorageMapKey,
value Value,
) (existed bool) {
accountStorage := config.Storage.GetStorageMap(storageAddress, domain, true)

inter := config.interpreter()

accountStorage := config.Storage.GetDomainStorageMap(inter, storageAddress, domain, true)
interValue := VMValueToInterpreterValue(config, value)

return accountStorage.WriteValue(
config.interpreter(),
inter,
key,
interValue,
)
Expand All @@ -90,12 +101,17 @@ func RemoveReferencedSlab(storage interpreter.Storage, storable atree.Storable)
}

func StoredValueExists(
storage interpreter.Storage,
config *Config,
storageAddress common.Address,
domain string,
domain common.StorageDomain,
identifier interpreter.StorageMapKey,
) bool {
accountStorage := storage.GetStorageMap(storageAddress, domain, false)
accountStorage := config.Storage.GetDomainStorageMap(
config.interpreter(),
storageAddress,
domain,
false,
)
if accountStorage == nil {
return false
}
Expand Down
13 changes: 8 additions & 5 deletions bbq/vm/value_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/onflow/cadence/errors"
"github.com/onflow/cadence/interpreter"
"github.com/onflow/cadence/sema"
"github.com/onflow/cadence/stdlib"
)

type AccountIDGenerator interface {
Expand Down Expand Up @@ -143,8 +142,7 @@ func getCapability(
// Read stored capability, if any

readValue := ReadStored(
config.MemoryGauge,
config.Storage,
config,
address,
domain,
identifier,
Expand Down Expand Up @@ -339,7 +337,7 @@ func storeCapabilityController(
existed := WriteStored(
config,
address,
stdlib.CapabilityControllerStorageDomain,
common.StorageDomainCapabilityController,
storageMapKey,
controller,
)
Expand Down Expand Up @@ -376,7 +374,12 @@ func recordStorageCapabilityController(

storageMapKey := interpreter.StringStorageMapKey(identifier)

accountStorage := config.Storage.GetStorageMap(address, stdlib.PathCapabilityStorageDomain, true)
accountStorage := config.Storage.GetDomainStorageMap(
config.interpreter(),
address,
common.StorageDomainPathCapability,
true,
)

referenced := accountStorage.ReadValue(config.MemoryGauge, interpreter.StringStorageMapKey(identifier))
readValue := InterpreterValueToVMValue(config.Storage, referenced)
Expand Down
8 changes: 5 additions & 3 deletions bbq/vm/value_account_capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,17 @@ func init() {
}

domain := path.Domain.Identifier()
storageDomain, _ := common.StorageDomainFromIdentifier(domain)

identifier := path.Identifier

// Prevent an overwrite

storageMapKey := interpreter.StringStorageMapKey(identifier)
if StoredValueExists(
config.Storage,
config,
accountAddress,
domain,
storageDomain,
storageMapKey,
) {
panic(interpreter.OverwriteError{
Expand All @@ -174,7 +176,7 @@ func init() {
WriteStored(
config,
accountAddress,
domain,
storageDomain,
storageMapKey,
capabilityValue,
)
Expand Down
3 changes: 2 additions & 1 deletion bbq/vm/value_account_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ func init() {

// Write new value

storageDomain, _ := common.StorageDomainFromIdentifier(domain)
WriteStored(
config,
address,
domain,
storageDomain,
interpreter.StringStorageMapKey(identifier),
value,
)
Expand Down
8 changes: 6 additions & 2 deletions bbq/vm/value_capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/onflow/cadence/format"
"github.com/onflow/cadence/interpreter"
"github.com/onflow/cadence/sema"
"github.com/onflow/cadence/stdlib"
)

// members
Expand Down Expand Up @@ -210,7 +209,12 @@ func getCapabilityController(

storageMapKey := interpreter.Uint64StorageMapKey(capabilityID)

accountStorage := config.Storage.GetStorageMap(address, stdlib.CapabilityControllerStorageDomain, false)
accountStorage := config.Storage.GetDomainStorageMap(
config.interpreter(),
address,
common.StorageDomainCapabilityController,
false,
)
if accountStorage == nil {
return nil
}
Expand Down
3 changes: 1 addition & 2 deletions bbq/vm/value_storage_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ func (v *StorageReferenceValue) StaticType(config *Config) StaticType {
}

func (v *StorageReferenceValue) dereference(config *Config) (*Value, error) {
memoryGauge := config.MemoryGauge
address := v.TargetStorageAddress
domain := v.TargetPath.Domain.Identifier()
identifier := v.TargetPath.Identifier

vmReferencedValue := ReadStored(memoryGauge, config.Storage, address, domain, identifier)
vmReferencedValue := ReadStored(config, address, domain, identifier)
if vmReferencedValue == nil {
return nil, nil
}
Expand Down

0 comments on commit 44406d4

Please sign in to comment.