Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve composite and interface static types #2751

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions encoding/json/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,55 +845,55 @@ func prepareType(typ cadence.Type, results typePreparationResults) jsonValue {
return jsonNominalType{
Kind: "Struct",
Type: "",
TypeID: typeId(typ.Location, typ.QualifiedIdentifier),
TypeID: string(common.NewTypeIDFromQualifiedName(nil, typ.Location, typ.QualifiedIdentifier)),
Fields: prepareFields(typ.Fields, results),
Initializers: prepareInitializers(typ.Initializers, results),
}
case *cadence.ResourceType:
return jsonNominalType{
Kind: "Resource",
Type: "",
TypeID: typeId(typ.Location, typ.QualifiedIdentifier),
TypeID: string(common.NewTypeIDFromQualifiedName(nil, typ.Location, typ.QualifiedIdentifier)),
Fields: prepareFields(typ.Fields, results),
Initializers: prepareInitializers(typ.Initializers, results),
}
case *cadence.EventType:
return jsonNominalType{
Kind: "Event",
Type: "",
TypeID: typeId(typ.Location, typ.QualifiedIdentifier),
TypeID: string(common.NewTypeIDFromQualifiedName(nil, typ.Location, typ.QualifiedIdentifier)),
Fields: prepareFields(typ.Fields, results),
Initializers: [][]jsonParameterType{prepareParameters(typ.Initializer, results)},
}
case *cadence.ContractType:
return jsonNominalType{
Kind: "Contract",
Type: "",
TypeID: typeId(typ.Location, typ.QualifiedIdentifier),
TypeID: string(common.NewTypeIDFromQualifiedName(nil, typ.Location, typ.QualifiedIdentifier)),
Fields: prepareFields(typ.Fields, results),
Initializers: prepareInitializers(typ.Initializers, results),
}
case *cadence.StructInterfaceType:
return jsonNominalType{
Kind: "StructInterface",
Type: "",
TypeID: typeId(typ.Location, typ.QualifiedIdentifier),
TypeID: string(common.NewTypeIDFromQualifiedName(nil, typ.Location, typ.QualifiedIdentifier)),
Fields: prepareFields(typ.Fields, results),
Initializers: prepareInitializers(typ.Initializers, results),
}
case *cadence.ResourceInterfaceType:
return jsonNominalType{
Kind: "ResourceInterface",
Type: "",
TypeID: typeId(typ.Location, typ.QualifiedIdentifier),
TypeID: string(common.NewTypeIDFromQualifiedName(nil, typ.Location, typ.QualifiedIdentifier)),
Fields: prepareFields(typ.Fields, results),
Initializers: prepareInitializers(typ.Initializers, results),
}
case *cadence.ContractInterfaceType:
return jsonNominalType{
Kind: "ContractInterface",
Type: "",
TypeID: typeId(typ.Location, typ.QualifiedIdentifier),
TypeID: string(common.NewTypeIDFromQualifiedName(nil, typ.Location, typ.QualifiedIdentifier)),
Fields: prepareFields(typ.Fields, results),
Initializers: prepareInitializers(typ.Initializers, results),
}
Expand Down Expand Up @@ -930,7 +930,7 @@ func prepareType(typ cadence.Type, results typePreparationResults) jsonValue {
case *cadence.EnumType:
return jsonNominalType{
Kind: "Enum",
TypeID: typeId(typ.Location, typ.QualifiedIdentifier),
TypeID: string(common.NewTypeIDFromQualifiedName(nil, typ.Location, typ.QualifiedIdentifier)),
Fields: prepareFields(typ.Fields, results),
Initializers: prepareInitializers(typ.Initializers, results),
Type: prepareType(typ.RawType, results),
Expand Down Expand Up @@ -1034,11 +1034,3 @@ func encodeUFix64(v uint64) string {
fraction,
)
}

func typeId(location common.Location, identifier string) string {
if location == nil {
return identifier
}

return string(location.TypeID(nil, identifier))
}
5 changes: 2 additions & 3 deletions runtime/convertTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,19 +518,18 @@ func exportCapabilityType(
}

func importInterfaceType(memoryGauge common.MemoryGauge, t cadence.InterfaceType) interpreter.InterfaceStaticType {
return interpreter.NewInterfaceStaticType(
return interpreter.NewInterfaceStaticTypeComputeTypeID(
memoryGauge,
t.InterfaceTypeLocation(),
t.InterfaceTypeQualifiedIdentifier(),
)
}

func importCompositeType(memoryGauge common.MemoryGauge, t cadence.CompositeType) interpreter.CompositeStaticType {
return interpreter.NewCompositeStaticType(
return interpreter.NewCompositeStaticTypeComputeTypeID(
memoryGauge,
t.CompositeTypeLocation(),
t.CompositeTypeQualifiedIdentifier(),
"", // intentionally empty
)
}

Expand Down
55 changes: 11 additions & 44 deletions runtime/convertValues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1241,87 +1241,63 @@ func TestImportRuntimeType(t *testing.T) {
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.CompositeStaticType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.NewCompositeStaticTypeComputeTypeID(nil, TestLocation, "S"),
},
{
label: "Resource",
actual: &cadence.ResourceType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.CompositeStaticType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.NewCompositeStaticTypeComputeTypeID(nil, TestLocation, "S"),
},
{
label: "Contract",
actual: &cadence.ContractType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.CompositeStaticType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.NewCompositeStaticTypeComputeTypeID(nil, TestLocation, "S"),
},
{
label: "Event",
actual: &cadence.EventType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.CompositeStaticType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.NewCompositeStaticTypeComputeTypeID(nil, TestLocation, "S"),
},
{
label: "Enum",
actual: &cadence.EnumType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.CompositeStaticType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.NewCompositeStaticTypeComputeTypeID(nil, TestLocation, "S"),
},
{
label: "StructInterface",
actual: &cadence.StructInterfaceType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.InterfaceStaticType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.NewInterfaceStaticTypeComputeTypeID(nil, TestLocation, "S"),
},
{
label: "ResourceInterface",
actual: &cadence.ResourceInterfaceType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.InterfaceStaticType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.NewInterfaceStaticTypeComputeTypeID(nil, TestLocation, "S"),
},
{
label: "ContractInterface",
actual: &cadence.ContractInterfaceType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.InterfaceStaticType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
expected: interpreter.NewInterfaceStaticTypeComputeTypeID(nil, TestLocation, "S"),
},
{
label: "RestrictedType",
Expand All @@ -1337,15 +1313,9 @@ func TestImportRuntimeType(t *testing.T) {
}},
},
expected: &interpreter.RestrictedStaticType{
Type: interpreter.CompositeStaticType{
Location: TestLocation,
QualifiedIdentifier: "S",
},
Type: interpreter.NewCompositeStaticTypeComputeTypeID(nil, TestLocation, "S"),
Restrictions: []interpreter.InterfaceStaticType{
{
Location: TestLocation,
QualifiedIdentifier: "T",
},
interpreter.NewInterfaceStaticTypeComputeTypeID(nil, TestLocation, "T"),
},
},
},
Expand Down Expand Up @@ -2080,10 +2050,7 @@ func TestExportTypeValue(t *testing.T) {
Type: &interpreter.RestrictedStaticType{
Type: interpreter.NewCompositeStaticTypeComputeTypeID(inter, TestLocation, "S"),
Restrictions: []interpreter.InterfaceStaticType{
{
Location: TestLocation,
QualifiedIdentifier: "SI",
},
interpreter.NewInterfaceStaticTypeComputeTypeID(nil, TestLocation, "SI"),
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/interpreter/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,7 @@ func (d TypeDecoder) decodeInterfaceStaticType() (InterfaceStaticType, error) {
return InterfaceStaticType{}, err
}

return NewInterfaceStaticType(d.memoryGauge, location, qualifiedIdentifier), nil
return NewInterfaceStaticTypeComputeTypeID(d.memoryGauge, location, qualifiedIdentifier), nil
}

func (d TypeDecoder) decodeVariableSizedStaticType() (StaticType, error) {
Expand Down
30 changes: 6 additions & 24 deletions runtime/interpreter/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3801,10 +3801,7 @@ func TestEncodeDecodePathLinkValue(t *testing.T) {

value := PathLinkValue{
TargetPath: publicPathValue,
Type: InterfaceStaticType{
Location: utils.TestLocation,
QualifiedIdentifier: "SimpleInterface",
},
Type: NewInterfaceStaticTypeComputeTypeID(nil, utils.TestLocation, "SimpleInterface"),
}

encoded := assemble(
Expand Down Expand Up @@ -4001,14 +3998,8 @@ func TestEncodeDecodePathLinkValue(t *testing.T) {
"S",
),
Restrictions: []InterfaceStaticType{
{
Location: utils.TestLocation,
QualifiedIdentifier: "I1",
},
{
Location: utils.TestLocation,
QualifiedIdentifier: "I2",
},
NewInterfaceStaticTypeComputeTypeID(nil, utils.TestLocation, "I1"),
NewInterfaceStaticTypeComputeTypeID(nil, utils.TestLocation, "I2"),
},
},
}
Expand Down Expand Up @@ -4543,10 +4534,7 @@ func TestEncodeDecodeStorageCapabilityControllerValue(t *testing.T) {
value := &StorageCapabilityControllerValue{
TargetPath: publicPathValue,
BorrowType: ReferenceStaticType{
BorrowedType: InterfaceStaticType{
Location: utils.TestLocation,
QualifiedIdentifier: "SimpleInterface",
},
BorrowedType: NewInterfaceStaticTypeComputeTypeID(nil, utils.TestLocation, "SimpleInterface"),
},
CapabilityID: capabilityID,
}
Expand Down Expand Up @@ -4715,14 +4703,8 @@ func TestEncodeDecodeStorageCapabilityControllerValue(t *testing.T) {
"S",
),
Restrictions: []InterfaceStaticType{
{
Location: utils.TestLocation,
QualifiedIdentifier: "I1",
},
{
Location: utils.TestLocation,
QualifiedIdentifier: "I2",
},
NewInterfaceStaticTypeComputeTypeID(nil, utils.TestLocation, "I1"),
NewInterfaceStaticTypeComputeTypeID(nil, utils.TestLocation, "I2"),
},
},
},
Expand Down
Loading
Loading