Skip to content

Commit

Permalink
Merge pull request #2727 from onflow/bastian/capability-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Aug 17, 2023
2 parents 7b221f6 + 4ea1b51 commit 898264e
Show file tree
Hide file tree
Showing 32 changed files with 396 additions and 388 deletions.
14 changes: 7 additions & 7 deletions encoding/ccf/ccf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9781,7 +9781,7 @@ func TestEncodeType(t *testing.T) {
})
}

func TestEncodeIDCapability(t *testing.T) {
func TestEncodeCapability(t *testing.T) {

t.Parallel()

Expand All @@ -9790,7 +9790,7 @@ func TestEncodeIDCapability(t *testing.T) {

testEncodeAndDecode(
t,
cadence.IDCapability{
cadence.Capability{
ID: 42,
Address: cadence.BytesToAddress([]byte{1, 2, 3, 4, 5}),
},
Expand Down Expand Up @@ -9836,13 +9836,13 @@ func TestEncodeIDCapability(t *testing.T) {
},
}

capability1 := cadence.IDCapability{
capability1 := cadence.Capability{
ID: 42,
Address: cadence.BytesToAddress([]byte{1, 2, 3, 4, 5}),
BorrowType: cadence.IntType{},
}

capability2 := cadence.IDCapability{
capability2 := cadence.Capability{
ID: 43,
Address: cadence.BytesToAddress([]byte{1, 2, 3, 4, 5}),
BorrowType: simpleStructType,
Expand Down Expand Up @@ -9961,7 +9961,7 @@ func TestEncodeIDCapability(t *testing.T) {

testEncodeAndDecode(
t,
cadence.IDCapability{
cadence.Capability{
ID: 42,
Address: cadence.BytesToAddress([]byte{1, 2, 3, 4, 5}),
BorrowType: cadence.IntType{},
Expand Down Expand Up @@ -9999,12 +9999,12 @@ func TestEncodeIDCapability(t *testing.T) {
t.Run("array of Capability<Int>", func(t *testing.T) {
t.Parallel()

capability1 := cadence.IDCapability{
capability1 := cadence.Capability{
ID: 42,
Address: cadence.BytesToAddress([]byte{1, 2, 3, 4, 5}),
BorrowType: cadence.IntType{},
}
capability2 := cadence.IDCapability{
capability2 := cadence.Capability{
ID: 43,
Address: cadence.BytesToAddress([]byte{1, 2, 3, 4, 5}),
BorrowType: cadence.IntType{},
Expand Down
2 changes: 1 addition & 1 deletion encoding/ccf/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ func (d *Decoder) decodeCapability(typ *cadence.CapabilityType, types *cadenceTy
return nil, err
}

return cadence.NewMeteredIDCapability(
return cadence.NewMeteredCapability(
d.gauge,
id.(cadence.UInt64),
address.(cadence.Address),
Expand Down
8 changes: 4 additions & 4 deletions encoding/ccf/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,8 @@ func (e *Encoder) encodeValue(
// If x.StaticType is nil, type value is encoded as nil.
return e.encodeNullableTypeValue(v.StaticType, ccfTypeIDByCadenceType{})

case cadence.IDCapability:
return e.encodeIDCapability(v)
case cadence.Capability:
return e.encodeCapability(v)

case cadence.Enum:
return e.encodeEnum(v, tids)
Expand Down Expand Up @@ -1106,15 +1106,15 @@ func (e *Encoder) encodePath(x cadence.Path) error {
return e.enc.EncodeString(x.Identifier)
}

// encodeIDCapability encodes cadence.IDCapability as
// encodeCapability encodes cadence.Capability as
// language=CDDL
// id-capability-value = [
//
// address: address-value,
// id: uint64-value
//
// ]
func (e *Encoder) encodeIDCapability(capability cadence.IDCapability) error {
func (e *Encoder) encodeCapability(capability cadence.Capability) error {
// Encode array head with length 2.
err := e.enc.EncodeRawBytes([]byte{
// array, 2 items follow
Expand Down
2 changes: 1 addition & 1 deletion encoding/json/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@ func (d *Decoder) decodeTypeValue(valueJSON any) cadence.TypeValue {
func (d *Decoder) decodeCapability(valueJSON any) cadence.Capability {
obj := toObject(valueJSON)

return cadence.NewMeteredIDCapability(
return cadence.NewMeteredCapability(
d.gauge,
d.decodeUInt64(obj.Get(idKey)),
d.decodeAddress(obj.Get(addressKey)),
Expand Down
10 changes: 5 additions & 5 deletions encoding/json/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ type jsonTypeValue struct {
StaticType jsonValue `json:"staticType"`
}

type jsonIDCapabilityValue struct {
type jsonCapabilityValue struct {
BorrowType jsonValue `json:"borrowType"`
Address string `json:"address"`
ID string `json:"id"`
Expand Down Expand Up @@ -335,8 +335,8 @@ func Prepare(v cadence.Value) jsonValue {
return preparePath(v)
case cadence.TypeValue:
return prepareTypeValue(v)
case cadence.IDCapability:
return prepareIDCapability(v)
case cadence.Capability:
return prepareCapability(v)
case cadence.Enum:
return prepareEnum(v)
case cadence.Attachment:
Expand Down Expand Up @@ -960,10 +960,10 @@ func prepareTypeValue(typeValue cadence.TypeValue) jsonValue {
}
}

func prepareIDCapability(capability cadence.IDCapability) jsonValue {
func prepareCapability(capability cadence.Capability) jsonValue {
return jsonValueObject{
Type: capabilityTypeStr,
Value: jsonIDCapabilityValue{
Value: jsonCapabilityValue{
ID: encodeUInt(uint64(capability.ID)),
Address: encodeBytes(capability.Address.Bytes()),
BorrowType: prepareType(capability.BorrowType, typePreparationResults{}),
Expand Down
4 changes: 2 additions & 2 deletions encoding/json/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2754,13 +2754,13 @@ func TestEncodeType(t *testing.T) {
})
}

func TestEncodeIDCapability(t *testing.T) {
func TestEncodeCapability(t *testing.T) {

t.Parallel()

testEncodeAndDecode(
t,
cadence.NewIDCapability(
cadence.NewCapability(
6,
cadence.BytesToAddress([]byte{1, 2, 3, 4, 5}),
cadence.IntType{},
Expand Down
4 changes: 2 additions & 2 deletions runtime/common/memorykind.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const (
MemoryKindOptionalValue
MemoryKindTypeValue
MemoryKindPathValue
MemoryKindIDCapabilityValue
MemoryKindCapabilityValue
MemoryKindStorageReferenceValue
MemoryKindAccountReferenceValue
MemoryKindEphemeralReferenceValue
Expand Down Expand Up @@ -104,7 +104,7 @@ const (
MemoryKindCadenceEnumValueSize
MemoryKindCadencePathValue
MemoryKindCadenceTypeValue
MemoryKindCadenceIDCapabilityValue
MemoryKindCadenceCapabilityValue
MemoryKindCadenceFunctionValue

// Cadence Types
Expand Down
8 changes: 4 additions & 4 deletions runtime/common/memorykind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions runtime/common/metering.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ var (
BoundFunctionValueMemoryUsage = NewConstantMemoryUsage(MemoryKindBoundFunctionValue)
HostFunctionValueMemoryUsage = NewConstantMemoryUsage(MemoryKindHostFunctionValue)
InterpretedFunctionValueMemoryUsage = NewConstantMemoryUsage(MemoryKindInterpretedFunctionValue)
IDCapabilityValueMemoryUsage = NewConstantMemoryUsage(MemoryKindIDCapabilityValue)
CapabilityValueMemoryUsage = NewConstantMemoryUsage(MemoryKindCapabilityValue)
EphemeralReferenceValueMemoryUsage = NewConstantMemoryUsage(MemoryKindEphemeralReferenceValue)
StorageReferenceValueMemoryUsage = NewConstantMemoryUsage(MemoryKindStorageReferenceValue)
AccountReferenceValueMemoryUsage = NewConstantMemoryUsage(MemoryKindAccountReferenceValue)
Expand Down Expand Up @@ -202,7 +202,7 @@ var (
CadenceEnumValueBaseMemoryUsage = NewConstantMemoryUsage(MemoryKindCadenceEnumValueBase)
CadenceAddressValueMemoryUsage = NewConstantMemoryUsage(MemoryKindCadenceAddressValue)
CadenceBoolValueMemoryUsage = NewConstantMemoryUsage(MemoryKindCadenceBoolValue)
CadenceIDCapabilityValueMemoryUsage = NewConstantMemoryUsage(MemoryKindCadenceIDCapabilityValue)
CadenceCapabilityValueMemoryUsage = NewConstantMemoryUsage(MemoryKindCadenceCapabilityValue)
CadenceFunctionValueMemoryUsage = NewConstantMemoryUsage(MemoryKindCadenceFunctionValue)
CadenceKeyValuePairMemoryUsage = NewConstantMemoryUsage(MemoryKindCadenceKeyValuePair)
CadenceOptionalValueMemoryUsage = NewConstantMemoryUsage(MemoryKindCadenceOptionalValue)
Expand Down Expand Up @@ -254,7 +254,7 @@ var (
AuthAccountCapabilitiesStringMemoryUsage = NewRawStringMemoryUsage(len("AuthAccount.Capabilities()"))
PublicAccountCapabilitiesStringMemoryUsage = NewRawStringMemoryUsage(len("PublicAccount.Capabilities()"))
AuthAccountInboxStringMemoryUsage = NewRawStringMemoryUsage(len("AuthAccount.Inbox()"))
IDCapabilityValueStringMemoryUsage = NewRawStringMemoryUsage(len("Capability<>(address: , id: )"))
CapabilityValueStringMemoryUsage = NewRawStringMemoryUsage(len("Capability<>(address: , id: )"))
StorageCapabilityControllerValueStringMemoryUsage = NewRawStringMemoryUsage(len("StorageCapabilityController(borrowType: , capabilityID: , target: )"))
AccountCapabilityControllerValueStringMemoryUsage = NewRawStringMemoryUsage(len("AccountCapabilityController(borrowType: , capabilityID: )"))
PublishedValueStringMemoryUsage = NewRawStringMemoryUsage(len("PublishedValue<>()"))
Expand Down
22 changes: 11 additions & 11 deletions runtime/convertValues.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ func exportValueWithInterpreter(
return exportPathValue(inter, v)
case interpreter.TypeValue:
return exportTypeValue(v, inter), nil
case *interpreter.IDCapabilityValue:
return exportIDCapabilityValue(v, inter)
case *interpreter.CapabilityValue:
return exportCapabilityValue(v, inter)
case *interpreter.EphemeralReferenceValue:
// Break recursion through references
if _, ok := seenReferences[v]; ok {
Expand Down Expand Up @@ -626,14 +626,14 @@ func exportTypeValue(v interpreter.TypeValue, inter *interpreter.Interpreter) ca
)
}

func exportIDCapabilityValue(
v *interpreter.IDCapabilityValue,
func exportCapabilityValue(
v *interpreter.CapabilityValue,
inter *interpreter.Interpreter,
) (cadence.IDCapability, error) {
) (cadence.Capability, error) {
borrowType := inter.MustConvertStaticToSemaType(v.BorrowType)
exportedBorrowType := ExportMeteredType(inter, borrowType, map[sema.TypeID]cadence.Type{})

return cadence.NewMeteredIDCapability(
return cadence.NewMeteredCapability(
inter,
cadence.NewMeteredUInt64(inter, uint64(v.ID)),
cadence.NewMeteredAddress(inter, v.Address),
Expand Down Expand Up @@ -814,8 +814,8 @@ func (i valueImporter) importValue(value cadence.Value, expectedType sema.Type)
)
case cadence.TypeValue:
return i.importTypeValue(v.StaticType)
case cadence.IDCapability:
return i.importIDCapability(
case cadence.Capability:
return i.importCapability(
v.ID,
v.Address,
v.BorrowType,
Expand Down Expand Up @@ -1099,12 +1099,12 @@ func (i valueImporter) importTypeValue(v cadence.Type) (interpreter.TypeValue, e
return interpreter.NewTypeValue(inter, typ), nil
}

func (i valueImporter) importIDCapability(
func (i valueImporter) importCapability(
id cadence.UInt64,
address cadence.Address,
borrowType cadence.Type,
) (
*interpreter.IDCapabilityValue,
*interpreter.CapabilityValue,
error,
) {
_, ok := borrowType.(*cadence.ReferenceType)
Expand All @@ -1122,7 +1122,7 @@ func (i valueImporter) importIDCapability(
common.Address(address),
)

return interpreter.NewIDCapabilityValue(
return interpreter.NewCapabilityValue(
inter,
i.importUInt64(id),
addressValue,
Expand Down
Loading

0 comments on commit 898264e

Please sign in to comment.