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

Remove Restricted Type from Cadence #2596

Merged
merged 18 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
138 changes: 34 additions & 104 deletions encoding/ccf/ccf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6530,7 +6530,6 @@ func TestEncodeValueOfIntersectionType(t *testing.T) {
)

countSumIntersectionType := cadence.NewIntersectionType(
nil,
[]cadence.Type{
hasCountInterfaceType,
hasSumInterfaceType,
Expand All @@ -6557,7 +6556,6 @@ func TestEncodeValueOfIntersectionType(t *testing.T) {
)

expectedCountSumIntersectionType := cadence.NewIntersectionType(
nil,
[]cadence.Type{
hasSumInterfaceType,
hasCountInterfaceType,
Expand Down Expand Up @@ -6676,11 +6674,6 @@ func TestEncodeValueOfIntersectionType(t *testing.T) {
0xd8, ccf.CBORTagIntersectionType,
// array, 2 items follow
0x82,
// type
// null
0xf6,
// array, 2 items follow
0x82,
// tag
0xd8, ccf.CBORTagTypeRef,
// bytes, 1 byte follows
Expand Down Expand Up @@ -6751,7 +6744,6 @@ func TestEncodeValueOfIntersectionType(t *testing.T) {
)

countSumIntersectionType := cadence.NewIntersectionType(
statsType,
[]cadence.Type{
hasCountInterfaceType,
hasSumInterfaceType,
fxamacker marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -6778,7 +6770,6 @@ func TestEncodeValueOfIntersectionType(t *testing.T) {
)

expectedCountSumIntersectionType := cadence.NewIntersectionType(
expectedStatsType,
[]cadence.Type{
hasSumInterfaceType,
hasCountInterfaceType,
Expand Down Expand Up @@ -6897,13 +6888,6 @@ func TestEncodeValueOfIntersectionType(t *testing.T) {
0xd8, ccf.CBORTagIntersectionType,
// array, 2 items follow
0x82,
// type
// tag
0xd8, ccf.CBORTagTypeRef,
// bytes, 0 byte follows
0x40,
// array, 2 items follow
0x82,
// tag
0xd8, ccf.CBORTagTypeRef,
// bytes, 1 byte follows
Expand Down Expand Up @@ -9174,13 +9158,7 @@ func TestEncodeType(t *testing.T) {
t.Run("with static nil intersection type", func(t *testing.T) {
t.Parallel()

testEncodeAndDecode(
t,
cadence.TypeValue{
StaticType: &cadence.IntersectionType{
Types: []cadence.Type{},
},
},
encodedData :=
[]byte{
// language=json, format=json-cdc
// {"value":{"staticType":{"kind":"Intersection","type":"","types":[]}},"type":"Type"}
Expand All @@ -9199,62 +9177,44 @@ func TestEncodeType(t *testing.T) {
0x18, 0x29,
// tag
0xd8, ccf.CBORTagIntersectionTypeValue,
// array, 2 elements follow
0x82,
// null
0xf6,
// array, 0 element follows
0x80,
},
)
}

_, err := ccf.Decode(nil, encodedData)
require.Error(t, err)
assert.Equal(t, "ccf: failed to decode: unexpected empty intersection type", err.Error())
fxamacker marked this conversation as resolved.
Show resolved Hide resolved

})

t.Run("with static no intersection type", func(t *testing.T) {
t.Parallel()

testEncodeAndDecodeEx(
t,
cadence.TypeValue{
StaticType: &cadence.IntersectionType{
Types: []cadence.Type{},
Type: cadence.IntType{},
},
},
[]byte{
// language=json, format=json-cdc
// {"value":{"staticType":{"kind":"Intersection","typeID":"Int{String}","type":{"kind":"Int"},"types":[]}},"type":"Type"}
//
// language=edn, format=ccf
// 130([137(41), 191([185(4), []])])
//
// language=cbor, format=ccf
// tag
0xd8, ccf.CBORTagTypeAndValue,
// array, 2 elements follow
0x82,
// tag
0xd8, ccf.CBORTagSimpleType,
// Meta type ID (41)
0x18, 0x29,
// tag
0xd8, ccf.CBORTagIntersectionTypeValue,
// array, 2 elements follow
0x82,
// tag
0xd8, ccf.CBORTagSimpleTypeValue,
// Int type ID (4)
0x04,
// array, 0 element follows
0x80,
},
// Expected decoded IntersectionType doesn't have type ID.
cadence.TypeValue{
StaticType: &cadence.IntersectionType{
Types: []cadence.Type{},
Type: cadence.IntType{},
},
},
)
encodedData := []byte{
// language=json, format=json-cdc
// {"value":{"staticType":{"kind":"Intersection","typeID":"Int{String}","type":{"kind":"Int"},"types":[]}},"type":"Type"}
//
// language=edn, format=ccf
// 130([137(41), 191([185(4), []])])
//
// language=cbor, format=ccf
// tag
0xd8, ccf.CBORTagTypeAndValue,
// array, 2 elements follow
0x82,
// tag
0xd8, ccf.CBORTagSimpleType,
// Meta type ID (41)
0x18, 0x29,
// tag
0xd8, ccf.CBORTagIntersectionTypeValue,
// array, 0 element follows
0x80,
}

_, err := ccf.Decode(nil, encodedData)
require.Error(t, err)
assert.Equal(t, "ccf: failed to decode: unexpected empty intersection type", err.Error())
})

t.Run("with static intersection type", func(t *testing.T) {
Expand All @@ -9267,7 +9227,6 @@ func TestEncodeType(t *testing.T) {
Types: []cadence.Type{
cadence.StringType{},
},
Type: cadence.IntType{},
},
},
[]byte{
Expand All @@ -9288,12 +9247,6 @@ func TestEncodeType(t *testing.T) {
0x18, 0x29,
// tag
0xd8, ccf.CBORTagIntersectionTypeValue,
// array, 2 elements follow
0x82,
// tag
0xd8, ccf.CBORTagSimpleTypeValue,
// Int type ID (4)
0x04,
// array, 1 element follows
0x81,
// tag
Expand All @@ -9307,7 +9260,6 @@ func TestEncodeType(t *testing.T) {
Types: []cadence.Type{
cadence.StringType{},
},
Type: cadence.IntType{},
},
},
)
Expand All @@ -9325,7 +9277,6 @@ func TestEncodeType(t *testing.T) {
cadence.NewAnyStructType(),
cadence.StringType{},
},
Type: cadence.IntType{},
},
},
[]byte{
Expand All @@ -9346,12 +9297,6 @@ func TestEncodeType(t *testing.T) {
0x18, 0x29,
// tag
0xd8, ccf.CBORTagIntersectionTypeValue,
// array, 2 elements follow
0x82,
// tag
0xd8, ccf.CBORTagSimpleTypeValue,
// Int type ID (4)
0x04,
// array, 2 element follows
0x82,
// tag
Expand All @@ -9370,7 +9315,6 @@ func TestEncodeType(t *testing.T) {
cadence.StringType{},
cadence.NewAnyStructType(),
},
Type: cadence.IntType{},
},
},
)
Expand All @@ -9384,7 +9328,6 @@ func TestEncodeType(t *testing.T) {
t,
cadence.TypeValue{
StaticType: &cadence.IntersectionType{
Type: cadence.TheAnyStructType,
Types: []cadence.Type{
cadence.NewStructInterfaceType(
common.NewAddressLocation(nil, common.Address{0x01}, "TypeA"),
Expand Down Expand Up @@ -9425,12 +9368,6 @@ func TestEncodeType(t *testing.T) {
0x18, 0x29,
// tag
0xd8, ccf.CBORTagIntersectionTypeValue,
// array, 2 elements follow
0x82,
// tag
0xd8, ccf.CBORTagSimpleTypeValue,
// AnyStruct type ID (39)
0x18, 0x27,
// 3 sorted types
// array, 3 element follows
0x83,
Expand Down Expand Up @@ -9499,7 +9436,6 @@ func TestEncodeType(t *testing.T) {
// Expected decoded IntersectionType has sorted types and no type ID.
cadence.TypeValue{
StaticType: &cadence.IntersectionType{
Type: cadence.TheAnyStructType,
Types: []cadence.Type{
cadence.NewStructInterfaceType(
common.IdentifierLocation("LocationC"),
Expand Down Expand Up @@ -14145,7 +14081,7 @@ func TestEncodeValueOfIntersectedInterface(t *testing.T) {
}

struct MiddleStruct {
var field: AnyStruct{Interface}
var field: {Interface}
}

struct interface Interface {}
Expand Down Expand Up @@ -14174,7 +14110,7 @@ func TestEncodeValueOfIntersectedInterface(t *testing.T) {
[]cadence.Field{
{
Type: cadence.NewIntersectionType(
cadence.TheAnyStructType, []cadence.Type{interfaceType}),
[]cadence.Type{interfaceType}),
Identifier: "field",
},
},
Expand Down Expand Up @@ -14305,12 +14241,6 @@ func TestEncodeValueOfIntersectedInterface(t *testing.T) {
0x66, 0x69, 0x65, 0x6c, 0x64,
// tag
0xd8, ccf.CBORTagIntersectionType,
// array, 2 item follows
0x82,
// tag
0xd8, ccf.CBORTagSimpleType,
// AnyStruct type ID (39)
0x18, 0x27,
// array, 1 item follows
0x81,
// tag
Expand Down
22 changes: 8 additions & 14 deletions encoding/ccf/decode_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,23 +544,14 @@ func (d *Decoder) decodeIntersectionType(
decodeTypeFn decodeTypeFn,
decodeIntersectionTypeFn decodeTypeFn,
) (cadence.Type, error) {
// Decode array of length 2.
err := decodeCBORArrayWithKnownSize(d.dec, 2)
if err != nil {
return nil, err
}

// element 0: type
typ, err := decodeTypeFn(types)
if err != nil {
return nil, err
}

// element 1: types
// types
typeCount, err := d.dec.DecodeArrayHead()
if err != nil {
return nil, err
}
if typeCount == 0 {
return nil, errors.New("unexpected empty intersection type")
}

intersectionTypeIDs := make(map[string]struct{}, typeCount)
var previousIntersectionTypeID string
Expand Down Expand Up @@ -600,9 +591,12 @@ func (d *Decoder) decodeIntersectionType(
intersectionTypes[i] = intersectedType
}

if len(intersectionTypes) == 0 {
return nil, errors.New("unexpected empty intersection type")
}

return cadence.NewMeteredIntersectionType(
d.gauge,
typ,
intersectionTypes,
), nil
}
Expand Down
14 changes: 1 addition & 13 deletions encoding/ccf/encode_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,19 +390,7 @@ func (e *Encoder) encodeIntersectionTypeWithRawTag(
return err
}

// Encode array head of length 2.
err = e.enc.EncodeArrayHead(2)
if err != nil {
return err
}

// element 0: type with given encodeTypeFn
err = encodeTypeFn(typ.Type, tids)
if err != nil {
return err
}

// element 1: types as array.
// types as array.

// Encode array head with number of types.
intersectionTypes := typ.Types
Expand Down
2 changes: 1 addition & 1 deletion encoding/ccf/traverse_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (ct *compositeTypes) traverseType(typ cadence.Type) (checkRuntimeType bool)
return ct.traverseType(typ.Type)

case *cadence.IntersectionType:
check := ct.traverseType(typ.Type)
check := false
for _, typ := range typ.Types {
checkTyp := ct.traverseType(typ)
check = check || checkTyp
Expand Down
6 changes: 0 additions & 6 deletions encoding/json/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1114,20 +1114,16 @@ func (d *Decoder) decodeNominalType(
}

func (d *Decoder) decodeIntersectionType(
typeValue any,
intersectionValue []any,
results typeDecodingResults,
) cadence.Type {
typ := d.decodeType(typeValue, results)

types := make([]cadence.Type, 0, len(intersectionValue))
for _, typ := range intersectionValue {
types = append(types, d.decodeType(typ, results))
}

return cadence.NewMeteredIntersectionType(
d.gauge,
typ,
types,
)
}
Expand Down Expand Up @@ -1166,9 +1162,7 @@ func (d *Decoder) decodeType(valueJSON any, results typeDecodingResults) cadence
return d.decodeFunctionType(typeParametersValue, parametersValue, returnValue, purity, results)
case "Intersection":
intersectionValue := obj.Get(intersectionTypesKey)
typeValue := obj.Get(typeKey)
return d.decodeIntersectionType(
typeValue,
toSlice(intersectionValue),
results,
)
Expand Down
Loading