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

Add support for FunctionType.Purity in CCF codec #3107

Merged
merged 1 commit into from
Feb 28, 2024
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
76 changes: 65 additions & 11 deletions encoding/ccf/ccf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9475,14 +9475,15 @@ func TestEncodeType(t *testing.T) {
{Label: "qux", Identifier: "baz", Type: cadence.StringType},
},
ReturnType: cadence.IntType,
Purity: cadence.FunctionPurityView,
},
},
[]byte{
// language=json, format=json-cdc
// {"value":{"staticType":{"kind":"Function","typeParameters":[{"name":"T","typeBound":{"kind":"AnyStruct"}}],"parameters":[{"type":{"kind":"String"},"label":"qux","id":"baz"}],"return":{"kind":"Int"}}},"type":"Type"}
// {"value":{"staticType":{"kind":"Function","typeParameters":[{"name":"T","typeBound":{"kind":"AnyStruct"}}],"parameters":[{"type":{"kind":"String"},"label":"qux","id":"baz"}],"return":{"kind":"Int"},"purity":"view"}},"type":"Type"}
//
// language=edn, format=ccf
// 130([137(41), 193([[["T", 185(39)]], [["qux", "baz", 185(1)]], 185(4)])])
// 130([137(41), 193([[["T", 185(39)]], [["qux", "baz", 185(1)]], 185(4), 1])])
//
// language=cbor, format=ccf
// tag
Expand All @@ -9495,8 +9496,8 @@ func TestEncodeType(t *testing.T) {
0x18, 0x29,
// tag
0xd8, ccf.CBORTagFunctionTypeValue,
// array, 3 elements follow
0x83,
// array, 4 elements follow
0x84,
// array, 1 elements follow
0x81,
// array, 2 elements follow
Expand Down Expand Up @@ -9529,6 +9530,8 @@ func TestEncodeType(t *testing.T) {
0xd8, ccf.CBORTagSimpleTypeValue,
// Int type ID (4)
0x04,
// Purity 1
0x01,
},
)
})
Expand All @@ -9554,7 +9557,7 @@ func TestEncodeType(t *testing.T) {
// {"value":{"staticType":{"kind":"Function","typeParameters":[{"name":"T","typeBound":null}],"parameters":[{"type":{"kind":"String"},"label":"qux","id":"baz"}],"return":{"kind":"Int"}}},"type":"Type"}
//
// language=edn, format=ccf
// 130([137(41), 193([[["T", null]], [["qux", "baz", 185(1)]], 185(4)])])
// 130([137(41), 193([[["T", null]], [["qux", "baz", 185(1)]], 185(4), 0])])
//
// language=cbor, format=ccf
// tag
Expand All @@ -9567,8 +9570,8 @@ func TestEncodeType(t *testing.T) {
0x18, 0x29,
// tag
0xd8, ccf.CBORTagFunctionTypeValue,
// array, 3 elements follow
0x83,
// array, 4 elements follow
0x84,
// array, 1 elements follow
0x81,
// array, 2 elements follow
Expand Down Expand Up @@ -9599,6 +9602,8 @@ func TestEncodeType(t *testing.T) {
0xd8, ccf.CBORTagSimpleTypeValue,
// Int type ID (4)
0x04,
// purity 0
0x00,
},
)
})
Expand Down Expand Up @@ -12182,14 +12187,15 @@ func TestExportFunctionValue(t *testing.T) {
FunctionType: &cadence.FunctionType{
Parameters: []cadence.Parameter{},
ReturnType: cadence.VoidType,
Purity: cadence.FunctionPurityView,
},
},
[]byte{
// language=json, format=json-cdc
// {"value":{"functionType":{"kind":"Function","typeParameters":[],"parameters":[],"return":{"kind":"Void"}}},"type":"Function"}
// {"value":{"functionType":{"kind":"Function","typeParameters":[],"parameters":[],"return":{"kind":"Void"},"purity":"view"}},"type":"Function"}
//
// language=edn, format=ccf
// 130([137(51), [[], [], 185(50)]])
// 130([137(51), [[], [], 185(50), 1]])
//
// language=cbor, format=ccf
// tag
Expand All @@ -12200,8 +12206,8 @@ func TestExportFunctionValue(t *testing.T) {
0xd8, ccf.CBORTagSimpleType,
// Function type ID (51)
0x18, 0x33,
// array, 3 elements follow
0x83,
// array, 4 elements follow
0x84,
// element 0: type parameters
0x80,
// element 1: parameters
Expand All @@ -12212,6 +12218,8 @@ func TestExportFunctionValue(t *testing.T) {
0xd8, ccf.CBORTagSimpleTypeValue,
// Void type ID (50)
0x18, 0x32,
// element 3: purity
0x01,
},
)
}
Expand Down Expand Up @@ -15661,3 +15669,49 @@ func TestHasMsgPrefix(t *testing.T) {
})
}
}

// TestDecodeFunctionTypeBackwardCompatibility tests decoding of FunctionType
// without Purity encoded.
func TestDecodeFunctionTypeBackwardCompatibility(t *testing.T) {

val := cadence.TypeValue{
StaticType: &cadence.FunctionType{
TypeParameters: []cadence.TypeParameter{},
Parameters: []cadence.Parameter{},
ReturnType: cadence.VoidType,
Purity: cadence.FunctionPurityUnspecified,
},
}

data := []byte{
// language=edn, format=ccf
// 130([137(41), 193([[], [], 185(50)])])
//
// 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.CBORTagFunctionTypeValue,
// array, 3 elements follow
0x83,
// element 0: type parameters
// array, 0 element
0x80,
// element 1: parameters
// array, 0 element
0x80,
// element 2: return type
// tag
0xd8, ccf.CBORTagSimpleTypeValue,
// Void type ID (50)
0x18, 0x32,
}

testDecode(t, data, val)
}
23 changes: 20 additions & 3 deletions encoding/ccf/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2227,15 +2227,20 @@
// ]
// ]
// return-type: type-value
// purity: int (optional)
//
// ]
func (d *Decoder) decodeFunctionTypeValue(visited *cadenceTypeByCCFTypeID) (cadence.Type, error) {
// Decode array head of length 3
err := decodeCBORArrayWithKnownSize(d.dec, 3)
// Decode array head for element count
c, err := d.dec.DecodeArrayHead()
if err != nil {
return nil, err
}

if c != 3 && c != 4 {
return nil, fmt.Errorf("CBOR array of function-value has %d elements (expected 3 or 4 elements)", c)
}

Check warning on line 2242 in encoding/ccf/decode.go

View check run for this annotation

Codecov / codecov/patch

encoding/ccf/decode.go#L2241-L2242

Added lines #L2241 - L2242 were not covered by tests

// element 0: type parameters
typeParameters, err := d.decodeTypeParameterTypeValues(visited)
if err != nil {
Expand All @@ -2258,9 +2263,21 @@
return nil, errors.New("unexpected nil function return type")
}

// TODO:
purity := cadence.FunctionPurityUnspecified

// optional element 3: purity
if c == 4 {
rawPurity, err := d.dec.DecodeInt64()
if err != nil {
return nil, err
}

Check warning on line 2273 in encoding/ccf/decode.go

View check run for this annotation

Codecov / codecov/patch

encoding/ccf/decode.go#L2272-L2273

Added lines #L2272 - L2273 were not covered by tests

purity, err = cadence.NewFunctionaryPurity(int(rawPurity))
if err != nil {
return nil, err
}

Check warning on line 2278 in encoding/ccf/decode.go

View check run for this annotation

Codecov / codecov/patch

encoding/ccf/decode.go#L2277-L2278

Added lines #L2277 - L2278 were not covered by tests
}

return cadence.NewMeteredFunctionType(
d.gauge,
purity,
Expand Down
15 changes: 11 additions & 4 deletions encoding/ccf/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,13 +1184,14 @@
// ]
// ]
// return-type: type-value
// purity: int
//
// ]
func (e *Encoder) encodeFunction(typ *cadence.FunctionType, visited ccfTypeIDByCadenceType) error {
// Encode array head of length 3.
// Encode array head of length 4.
err := e.enc.EncodeRawBytes([]byte{
// array, 3 items follow
0x83,
// array, 4 items follow
0x84,
})
if err != nil {
return err
Expand All @@ -1209,7 +1210,13 @@
}

// element 2: return type as type-value.
return e.encodeTypeValue(typ.ReturnType, visited)
err = e.encodeTypeValue(typ.ReturnType, visited)
if err != nil {
return err
}

Check warning on line 1216 in encoding/ccf/encode.go

View check run for this annotation

Codecov / codecov/patch

encoding/ccf/encode.go#L1215-L1216

Added lines #L1215 - L1216 were not covered by tests

// element 3: purity as int.
return e.enc.EncodeInt(int(typ.Purity))
}

// encodeTypeValue encodes cadence.Type as
Expand Down
10 changes: 10 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1308,8 +1308,18 @@
const (
FunctionPurityUnspecified FunctionPurity = iota
FunctionPurityView

// DO NOT add item after maxFunctionPurity
maxFunctionPurity
)

func NewFunctionaryPurity(rawPurity int) (FunctionPurity, error) {
if rawPurity < 0 || rawPurity >= int(maxFunctionPurity) {
return FunctionPurityUnspecified, fmt.Errorf("failed to convert %d to FunctionPurity", rawPurity)
}

Check warning on line 1319 in types.go

View check run for this annotation

Codecov / codecov/patch

types.go#L1318-L1319

Added lines #L1318 - L1319 were not covered by tests
return FunctionPurity(rawPurity), nil
}

type FunctionType struct {
TypeParameters []TypeParameter
Parameters []Parameter
Expand Down
Loading