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 15 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
154 changes: 91 additions & 63 deletions encoding/ccf/ccf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6661,7 +6661,6 @@ func TestEncodeValueOfIntersectionType(t *testing.T) {
)

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

expectedCountSumIntersectionType := cadence.NewIntersectionType(
nil,
[]cadence.Type{
hasSumInterfaceType,
hasCountInterfaceType,
Expand Down Expand Up @@ -6882,7 +6880,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 @@ -6909,7 +6906,6 @@ func TestEncodeValueOfIntersectionType(t *testing.T) {
)

expectedCountSumIntersectionType := cadence.NewIntersectionType(
expectedStatsType,
[]cadence.Type{
hasSumInterfaceType,
hasCountInterfaceType,
Expand Down Expand Up @@ -7029,10 +7025,8 @@ func TestEncodeValueOfIntersectionType(t *testing.T) {
// array, 2 items follow
0x82,
// type
// tag
0xd8, ccf.CBORTagTypeRef,
// bytes, 0 byte follows
0x40,
// null
0xf6,
// array, 2 items follow
0x82,
// tag
Expand Down Expand Up @@ -9465,13 +9459,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 @@ -9490,33 +9478,74 @@ func TestEncodeType(t *testing.T) {
0x18, 0x29,
// tag
0xd8, ccf.CBORTagIntersectionTypeValue,
// array, 2 elements follow
// array, 2 items follow
0x82,
// type
// 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()

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, 2 items follow
0x82,
// type
// 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())
})

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

testEncodeAndDecodeEx(
t,
cadence.TypeValue{
StaticType: &cadence.IntersectionType{
Types: []cadence.Type{},
Type: cadence.IntType{},
Types: []cadence.Type{
cadence.StringType{},
},
},
},
[]byte{
// language=json, format=json-cdc
// {"value":{"staticType":{"kind":"Intersection","typeID":"Int{String}","type":{"kind":"Int"},"types":[]}},"type":"Type"}
// {"type":"Type","value":{"staticType": { "kind": "Intersection", "typeID":"Int{String}", "type" : {"kind" : "Int"}, "types" : [ {"kind" : "String"} ]} } }
//
// language=edn, format=ccf
// 130([137(41), 191([185(4), []])])
// 130([137(41), 191([185(4), [185(1)]])])
//
// language=cbor, format=ccf
// tag
Expand All @@ -9529,26 +9558,31 @@ func TestEncodeType(t *testing.T) {
0x18, 0x29,
// tag
0xd8, ccf.CBORTagIntersectionTypeValue,
// array, 2 elements follow
// array, 2 items follow
0x82,
// type
// null
0xf6,
// array, 1 element follows
0x81,
// tag
0xd8, ccf.CBORTagSimpleTypeValue,
// Int type ID (4)
0x04,
// array, 0 element follows
0x80,
// String type ID (1)
0x01,
},
// Expected decoded IntersectionType doesn't have type ID.
cadence.TypeValue{
StaticType: &cadence.IntersectionType{
Types: []cadence.Type{},
Type: cadence.IntType{},
Types: []cadence.Type{
cadence.StringType{},
},
},
},
)

})

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

testEncodeAndDecodeEx(
Expand All @@ -9558,7 +9592,7 @@ func TestEncodeType(t *testing.T) {
Types: []cadence.Type{
cadence.StringType{},
},
Type: cadence.IntType{},
LegacyRestrictedType: cadence.IntType{},
},
},
[]byte{
Expand All @@ -9579,12 +9613,13 @@ func TestEncodeType(t *testing.T) {
0x18, 0x29,
// tag
0xd8, ccf.CBORTagIntersectionTypeValue,
// array, 2 elements follow
// array, 2 items follow
0x82,
// tag
0xd8, ccf.CBORTagSimpleTypeValue,
// Int type ID (4)
0x04,
// type
// int type tag
0xd8,
// int type
0xb9, 0x04,
// array, 1 element follows
0x81,
// tag
Expand All @@ -9598,7 +9633,7 @@ func TestEncodeType(t *testing.T) {
Types: []cadence.Type{
cadence.StringType{},
},
Type: cadence.IntType{},
LegacyRestrictedType: cadence.IntType{},
},
},
)
Expand All @@ -9616,7 +9651,6 @@ func TestEncodeType(t *testing.T) {
cadence.NewAnyStructType(),
cadence.StringType{},
},
Type: cadence.IntType{},
},
},
[]byte{
Expand All @@ -9637,12 +9671,11 @@ func TestEncodeType(t *testing.T) {
0x18, 0x29,
// tag
0xd8, ccf.CBORTagIntersectionTypeValue,
// array, 2 elements follow
// array, 2 items follow
0x82,
// tag
0xd8, ccf.CBORTagSimpleTypeValue,
// Int type ID (4)
0x04,
// type
// null
0xf6,
// array, 2 element follows
0x82,
// tag
Expand All @@ -9661,7 +9694,6 @@ func TestEncodeType(t *testing.T) {
cadence.StringType{},
cadence.NewAnyStructType(),
},
Type: cadence.IntType{},
},
},
)
Expand All @@ -9675,7 +9707,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 @@ -9716,12 +9747,11 @@ func TestEncodeType(t *testing.T) {
0x18, 0x29,
// tag
0xd8, ccf.CBORTagIntersectionTypeValue,
// array, 2 elements follow
// array, 2 items follow
0x82,
// tag
0xd8, ccf.CBORTagSimpleTypeValue,
// AnyStruct type ID (39)
0x18, 0x27,
// type
// null
0xf6,
// 3 sorted types
// array, 3 element follows
0x83,
Expand Down Expand Up @@ -9790,7 +9820,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 @@ -14360,6 +14389,11 @@ func TestDecodeInvalidData(t *testing.T) {
0xd8, ccf.CBORTagIntersectionTypeValue,
// array, 2 items follow
0x82,
// type
// null
0xf6,
// array, 2 items follow
0x82,
// tag
0xd8, ccf.CBORTagStructTypeValue,
// array, 5 items follow
Expand Down Expand Up @@ -14437,7 +14471,7 @@ func TestEncodeValueOfIntersectedInterface(t *testing.T) {
}

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

struct interface Interface {}
Expand Down Expand Up @@ -14466,7 +14500,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 @@ -14597,12 +14631,11 @@ func TestEncodeValueOfIntersectedInterface(t *testing.T) {
0x66, 0x69, 0x65, 0x6c, 0x64,
// tag
0xd8, ccf.CBORTagIntersectionType,
// array, 2 item follows
// array, 2 items follow
0x82,
// tag
0xd8, ccf.CBORTagSimpleType,
// AnyStruct type ID (39)
0x18, 0x27,
// type
// null
0xf6,
// array, 1 item follows
0x81,
// tag
Expand Down Expand Up @@ -14779,7 +14812,6 @@ func TestSortOptions(t *testing.T) {
)

countSumIntersectionType := cadence.NewIntersectionType(
nil,
[]cadence.Type{
hasCountInterfaceType,
hasSumInterfaceType,
Expand Down Expand Up @@ -14807,7 +14839,6 @@ func TestSortOptions(t *testing.T) {
)

expectedCountSumIntersectionType := cadence.NewIntersectionType(
nil,
[]cadence.Type{
hasCountInterfaceType,
hasSumInterfaceType,
Expand Down Expand Up @@ -15002,7 +15033,6 @@ func TestSortOptions(t *testing.T) {
)

expectedCountSumIntersectionType := cadence.NewIntersectionType(
nil,
[]cadence.Type{
hasCountInterfaceType,
hasSumInterfaceType,
Expand Down Expand Up @@ -15197,7 +15227,6 @@ func TestSortOptions(t *testing.T) {
)

expectedCountSumIntersectionType := cadence.NewIntersectionType(
nil,
[]cadence.Type{
hasSumInterfaceType,
hasCountInterfaceType,
Expand Down Expand Up @@ -15392,7 +15421,6 @@ func TestSortOptions(t *testing.T) {
)

expectedCountSumIntersectionType := cadence.NewIntersectionType(
nil,
[]cadence.Type{
hasSumInterfaceType,
hasCountInterfaceType,
Expand Down
Loading