Skip to content

Commit

Permalink
get storage functions return errors as well
Browse files Browse the repository at this point in the history
  • Loading branch information
janezpodhostnik authored and turbolent committed Dec 4, 2020
1 parent 8bd6832 commit a2f2ec9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
16 changes: 8 additions & 8 deletions runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ type Interface interface {
) bool
// Hash returns the digest of hashing the given data with using the given hash algorithm
Hash(data []byte, hashAlgorithm string) []byte
// GetStorageUsed returns the current storage used by this address
GetStorageUsed(address Address) uint64
// GetStorageCapacity returns the current storage capacity of this address
GetStorageCapacity(address Address) uint64
// GetStorageUsed gets storage used in bytes by the address at the moment of the function call.
GetStorageUsed(address Address) (value uint64, err error)
// GetStorageCapacity gets storage capacity in bytes on the address.
GetStorageCapacity(address Address) (value uint64, err error)
}

type HighLevelStorage interface {
Expand Down Expand Up @@ -238,10 +238,10 @@ func (i *EmptyRuntimeInterface) Hash(
return nil
}

func (i EmptyRuntimeInterface) GetStorageUsed(_ Address) uint64 {
return 0
func (i EmptyRuntimeInterface) GetStorageUsed(_ Address) (uint64,error) {
return 0, nil
}

func (i EmptyRuntimeInterface) GetStorageCapacity(_ Address) uint64 {
return 0
func (i EmptyRuntimeInterface) GetStorageCapacity(_ Address) (uint64,error) {
return 0, nil
}
20 changes: 14 additions & 6 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,22 +999,30 @@ func (r *interpreterRuntime) newCreateAccountFunction(
func storageUsedGetFunction(addressValue interpreter.AddressValue, runtimeInterface Interface) func() interpreter.UInt64Value {
address := addressValue.ToAddress()
return func() interpreter.UInt64Value {
var used interpreter.UInt64Value
var capacity uint64
var err error
wrapPanic(func() {
used = interpreter.UInt64Value(runtimeInterface.GetStorageUsed(address))
capacity, err = runtimeInterface.GetStorageUsed(address)
})
return used
if err != nil {
panic(err)
}
return interpreter.UInt64Value(capacity)
}
}

func storageCapacityGetFunction(addressValue interpreter.AddressValue, runtimeInterface Interface) func() interpreter.UInt64Value {
address := addressValue.ToAddress()
return func() interpreter.UInt64Value {
var capacity interpreter.UInt64Value
var capacity uint64
var err error
wrapPanic(func() {
capacity = interpreter.UInt64Value(runtimeInterface.GetStorageCapacity(address))
capacity, err = runtimeInterface.GetStorageCapacity(address)
})
return capacity
if err != nil {
panic(err)
}
return interpreter.UInt64Value(capacity)
}
}

Expand Down
8 changes: 4 additions & 4 deletions runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ type testRuntimeInterface struct {
) bool
hash func(data []byte, hashAlgorithm string) []byte
setCadenceValue func(owner Address, key string, value cadence.Value) (err error)
getStorageUsed func(_ Address) uint64
getStorageCapacity func(_ Address) uint64
getStorageUsed func(_ Address) (uint64, error)
getStorageCapacity func(_ Address) (uint64, error)
}

var _ Interface = &testRuntimeInterface{}
Expand Down Expand Up @@ -328,11 +328,11 @@ func (i *testRuntimeInterface) SetCadenceValue(owner common.Address, key string,
return i.setCadenceValue(owner, key, value)
}

func (i *testRuntimeInterface) GetStorageUsed(address Address) uint64 {
func (i *testRuntimeInterface) GetStorageUsed(address Address) (uint64, error) {
return i.getStorageUsed(address)
}

func (i *testRuntimeInterface) GetStorageCapacity(address Address) uint64 {
func (i *testRuntimeInterface) GetStorageCapacity(address Address) (uint64, error) {
return i.getStorageCapacity(address)
}

Expand Down

0 comments on commit a2f2ec9

Please sign in to comment.