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 Attachment and AttachmentType in CCF codec #3131

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
544 changes: 544 additions & 0 deletions encoding/ccf/ccf_test.go

Large diffs are not rendered by default.

29 changes: 25 additions & 4 deletions encoding/ccf/ccf_type_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import (
"fmt"
"math"
"math/big"

"github.com/onflow/cadence"
Expand All @@ -44,6 +45,13 @@
return id == other
}

func (id ccfTypeID) next() ccfTypeID {
if id == math.MaxUint64 {
panic(fmt.Errorf("failed to create next CCF type id: reached max limit for id"))

Check warning on line 50 in encoding/ccf/ccf_type_id.go

View check run for this annotation

Codecov / codecov/patch

encoding/ccf/ccf_type_id.go#L50

Added line #L50 was not covered by tests
}
return ccfTypeID(id + 1)

Check failure on line 52 in encoding/ccf/ccf_type_id.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary conversion (unconvert)

Check failure on line 52 in encoding/ccf/ccf_type_id.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary conversion (unconvert)
}

// ccfTypeIDByCadenceType maps a Cadence type ID to a CCF type ID
//
// IMPORTANT: Don't use cadence.Type as map key because all Cadence composite/interface
Expand All @@ -62,24 +70,37 @@
type cadenceTypeByCCFTypeID struct {
types map[ccfTypeID]cadence.Type
referencedTypes map[ccfTypeID]struct{}
nextCCFTypeID ccfTypeID
}

func newCadenceTypeByCCFTypeID() *cadenceTypeByCCFTypeID {
return &cadenceTypeByCCFTypeID{
types: make(map[ccfTypeID]cadence.Type),
referencedTypes: make(map[ccfTypeID]struct{}),
}
// types and referencedTypes are created lazily.
return &cadenceTypeByCCFTypeID{}
}

func (ids *cadenceTypeByCCFTypeID) isNextCCFTypeID(id ccfTypeID) bool {
return ids.nextCCFTypeID.Equal(id)
}

func (ids *cadenceTypeByCCFTypeID) addCCFTypeID(id ccfTypeID) {
ids.nextCCFTypeID = id.next()
}

func (ids *cadenceTypeByCCFTypeID) add(id ccfTypeID, typ cadence.Type) bool {
if ids.has(id) {
return false
}
if ids.types == nil {
ids.types = make(map[ccfTypeID]cadence.Type)
}
ids.types[id] = typ
return true
}

func (ids *cadenceTypeByCCFTypeID) reference(id ccfTypeID) {
if ids.referencedTypes == nil {
ids.referencedTypes = make(map[ccfTypeID]struct{})
}
ids.referencedTypes[id] = struct{}{}
}

Expand All @@ -96,7 +117,7 @@
return ok
}

func (ids *cadenceTypeByCCFTypeID) count() int {

Check failure on line 120 in encoding/ccf/ccf_type_id.go

View workflow job for this annotation

GitHub Actions / Lint

func `(*cadenceTypeByCCFTypeID).count` is unused (unused)

Check failure on line 120 in encoding/ccf/ccf_type_id.go

View workflow job for this annotation

GitHub Actions / Lint

func `(*cadenceTypeByCCFTypeID).count` is unused (unused)
return len(ids.types)
}

Expand Down
4 changes: 2 additions & 2 deletions encoding/ccf/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const (
CBORTagEventType
CBORTagContractType
CBORTagEnumType
_
CBORTagAttachmentType
_
_
_
Expand Down Expand Up @@ -141,7 +141,7 @@ const (
CBORTagEventTypeValue
CBORTagContractTypeValue
CBORTagEnumTypeValue
_
CBORTagAttachmentTypeValue
_
_
_
Expand Down
85 changes: 75 additions & 10 deletions encoding/ccf/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,9 @@
case *cadence.EnumType:
return d.decodeEnum(t, types)

case *cadence.AttachmentType:
return d.decodeAttachment(t, types)

case *cadence.ReferenceType:
// When static type is a reference type, encoded value is its deferenced type.
return d.decodeValue(t.Type, types)
Expand Down Expand Up @@ -1315,6 +1318,30 @@
return v.WithType(typ), nil
}

// decodeAttachment decodes encoded composite-value as
// language=CDDL
// composite-value = [* (field: value)]
func (d *Decoder) decodeAttachment(typ *cadence.AttachmentType, types *cadenceTypeByCCFTypeID) (cadence.Value, error) {
fieldValues, err := d.decodeComposite(typ.Fields, types)
if err != nil {
return nil, err
}

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

View check run for this annotation

Codecov / codecov/patch

encoding/ccf/decode.go#L1327-L1328

Added lines #L1327 - L1328 were not covered by tests

v, err := cadence.NewMeteredAttachment(
d.gauge,
len(fieldValues),
func() ([]cadence.Value, error) {
return fieldValues, nil
},
)
if err != nil {
return nil, err
}

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

View check run for this annotation

Codecov / codecov/patch

encoding/ccf/decode.go#L1338-L1339

Added lines #L1338 - L1339 were not covered by tests

// typ is already metered at creation.
return v.WithType(typ), nil
}

// decodeInclusiveRange decodes encoded inclusiverange-value as
// language=CDDL
// inclusiverange-value = [
Expand Down Expand Up @@ -1479,6 +1506,7 @@
// / contract-type-value
// / event-type-value
// / enum-type-value
// / attachment-type-value
// / struct-interface-type-value
// / resource-interface-type-value
// / contract-interface-type-value
Expand Down Expand Up @@ -1544,6 +1572,9 @@
case CBORTagEnumTypeValue:
return d.decodeEnumTypeValue(visited)

case CBORTagAttachmentTypeValue:
return d.decodeAttachmentTypeValue(visited)

case CBORTagStructInterfaceTypeValue:
return d.decodeStructInterfaceTypeValue(visited)

Expand Down Expand Up @@ -1719,6 +1750,34 @@
return d.decodeCompositeTypeValue(visited, ctr)
}

// decodeAttachmentTypeValue decodes attachment-type-value as
// language=CDDL
// attachment-type-value =
//
// ; cbor-tag-attachment-type-value
// #6.213(composite-type-value)
func (d *Decoder) decodeAttachmentTypeValue(visited *cadenceTypeByCCFTypeID) (cadence.Type, error) {
ctr := func(
location common.Location,
qualifiedIdentifier string,
typ cadence.Type,
) (cadence.Type, error) {
if typ == nil {
return nil, fmt.Errorf("encoded attachment-type-value has nil base type")
}

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

View check run for this annotation

Codecov / codecov/patch

encoding/ccf/decode.go#L1766-L1767

Added lines #L1766 - L1767 were not covered by tests
return cadence.NewMeteredAttachmentType(
d.gauge,
location,
typ,
qualifiedIdentifier,
nil,
nil,
), nil
}

return d.decodeCompositeTypeValue(visited, ctr)
}

// decodeStructInterfaceTypeValue decodes struct-inteface-type-value as
// language=CDDL
// struct-interface-type-value =
Expand Down Expand Up @@ -1848,16 +1907,6 @@
return nil, errors.New("unexpected nil composite type value")
}

// "Deterministic CCF Encoding Requirements" in CCF specs:
//
// "composite-type-value.id MUST be identical to the zero-based encoding order type-value."
if compTypeValue.ccfID != newCCFTypeIDFromUint64(uint64(visited.count())) {
return nil, fmt.Errorf(
"encoded composite-type-value's CCF type ID %d doesn't match zero-based encoding order composite-type-value",
compTypeValue.ccfID,
)
}

newType := visited.add(compTypeValue.ccfID, compositeType)
if !newType {
// "Valid CCF Encoding Requirements" in CCF specs:
Expand Down Expand Up @@ -1907,6 +1956,10 @@
compositeType.Fields = fields
compositeType.Initializers = initializers

case *cadence.AttachmentType:
compositeType.Fields = fields
compositeType.Initializers = initializers

case *cadence.StructInterfaceType:
compositeType.Fields = fields
compositeType.Initializers = initializers
Expand Down Expand Up @@ -1961,6 +2014,18 @@
return nil, err
}

// "Deterministic CCF Encoding Requirements" in CCF specs:
//
// "composite-type-value.id MUST be identical to the zero-based encoding order type-value."
if !visited.isNextCCFTypeID(ccfID) {
return nil, fmt.Errorf(
"encoded composite-type-value's CCF type ID %d doesn't match zero-based encoding order composite-type-value",
ccfID,
)
}

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

View check run for this annotation

Codecov / codecov/patch

encoding/ccf/decode.go#L2021-L2025

Added lines #L2021 - L2025 were not covered by tests

visited.addCCFTypeID(ccfID)

// element 1: cadence-type-id
_, location, identifier, err := d.decodeCadenceTypeID()
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions encoding/ccf/decode_typedef.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type rawFieldsWithCCFTypeID struct {
// / contract-type
// / event-type
// / enum-type
// / attachment-type
// / struct-interface-type
// / resource-interface-type
// / contract-interface-type
Expand Down Expand Up @@ -169,6 +170,11 @@ func (d *Decoder) decodeTypeDefs() (*cadenceTypeByCCFTypeID, error) {
// ; cbor-tag-enum-type
// #6.164(composite-type)
//
// attachment-type =
//
// ; cbor-tag-attachment-type
// #6.165(composite-type)
//
// struct-interface-type =
//
// ; cbor-tag-struct-interface-type
Expand Down Expand Up @@ -258,6 +264,19 @@ func (d *Decoder) decodeTypeDef(
}
return d.decodeCompositeType(types, ctr)

case CBORTagAttachmentType:
ctr := func(location common.Location, identifier string) cadence.Type {
return cadence.NewMeteredAttachmentType(
d.gauge,
location,
nil,
identifier,
nil,
nil,
)
}
return d.decodeCompositeType(types, ctr)

case CBORTagStructInterfaceType:
ctr := func(location common.Location, identifier string) cadence.Type {
return cadence.NewMeteredStructInterfaceType(
Expand Down
37 changes: 35 additions & 2 deletions encoding/ccf/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ func (e *Encoder) encodeInlineTypeAndValue(value cadence.Value, tids ccfTypeIDBy
// / contract-type
// / event-type
// / enum-type
// / attachment-type
// / struct-interface-type
// / resource-interface-type
// / contract-interface-type
Expand All @@ -344,7 +345,7 @@ func (e *Encoder) encodeTypeDefs(types []cadence.Type, tids ccfTypeIDByCadenceTy

switch typ := typ.(type) {
case cadence.CompositeType:
// Encode struct-type, resource-type, contract-type, event-type, or enum-type.
// Encode struct-type, resource-type, contract-type, event-type, enum-type, or attachment-type.
err = e.encodeCompositeType(typ, tids)
if err != nil {
return err
Expand Down Expand Up @@ -579,6 +580,9 @@ func (e *Encoder) encodeValue(
case cadence.Contract:
return e.encodeContract(v, tids)

case cadence.Attachment:
return e.encodeAttachment(v, tids)

case cadence.Path:
return e.encodePath(v)

Expand Down Expand Up @@ -1041,6 +1045,13 @@ func (e *Encoder) encodeEnum(v cadence.Enum, tids ccfTypeIDByCadenceType) error
return e.encodeComposite(v.EnumType, v.Fields, tids)
}

// encodeAttachment encodes cadence.Attachment as
// language=CDDL
// composite-value = [* (field: value)]
func (e *Encoder) encodeAttachment(v cadence.Attachment, tids ccfTypeIDByCadenceType) error {
return e.encodeComposite(v.AttachmentType, v.Fields, tids)
}

// encodeComposite encodes composite types as
// language=CDDL
// composite-value = [* (field: value)]
Expand Down Expand Up @@ -1233,6 +1244,7 @@ func (e *Encoder) encodeFunction(typ *cadence.FunctionType, visited ccfTypeIDByC
// / contract-type-value
// / event-type-value
// / enum-type-value
// / attachment-type-value
// / struct-interface-type-value
// / resource-interface-type-value
// / contract-interface-type-value
Expand Down Expand Up @@ -1291,6 +1303,9 @@ func (e *Encoder) encodeTypeValue(typ cadence.Type, visited ccfTypeIDByCadenceTy
case *cadence.ContractType:
return e.encodeContractTypeValue(typ, visited)

case *cadence.AttachmentType:
return e.encodeAttachmentTypeValue(typ, visited)

case *cadence.InclusiveRangeType:
return e.encodeInclusiveRangeTypeValue(typ, visited)

Expand Down Expand Up @@ -1588,6 +1603,24 @@ func (e *Encoder) encodeEnumTypeValue(typ *cadence.EnumType, visited ccfTypeIDBy
)
}

// encodeAttachmentTypeValue encodes cadence.AttachmentType as
// language=CDDL
// attachment-type-value =
//
// ; cbor-tag-attachment-type-value
// #6.213(composite-type-value)
func (e *Encoder) encodeAttachmentTypeValue(typ *cadence.AttachmentType, visited ccfTypeIDByCadenceType) error {
rawTagNum := []byte{0xd8, CBORTagAttachmentTypeValue}
return e.encodeCompositeTypeValue(
typ.ID(),
typ.BaseType,
typ.Fields,
typ.Initializers,
visited,
rawTagNum,
)
}

// encodeStructInterfaceTypeValue encodes cadence.StructInterfaceType as
// language=CDDL
// struct-interface-type-value =
Expand Down Expand Up @@ -1648,7 +1681,7 @@ func (e *Encoder) encodeContractInterfaceTypeValue(typ *cadence.ContractInterfac
//
// id: id,
// cadence-type-id: cadence-type-id,
// ; type is only used by enum type value
// ; type is only used by enum type value (as RawType) and attachment type value (as BaseType)
// type: nil / type-value,
// fields: [
// * [
Expand Down
8 changes: 8 additions & 0 deletions encoding/ccf/encode_typedef.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ import (
// ; cbor-tag-enum-type
// #6.164(composite-type)
//
// attachment-type =
//
// ; cbor-tag-attachment-type
// #6.165(composite-type)
//
// composite-type = [
//
// id: id,
Expand Down Expand Up @@ -86,6 +91,9 @@ func (e *Encoder) encodeCompositeType(typ cadence.CompositeType, tids ccfTypeIDB
case *cadence.EnumType:
cborTagNum = CBORTagEnumType

case *cadence.AttachmentType:
cborTagNum = CBORTagAttachmentType

default:
panic(cadenceErrors.NewUnexpectedError("unexpected composite type %s (%T)", typ.ID(), typ))
}
Expand Down
6 changes: 5 additions & 1 deletion encoding/ccf/traverse_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
ct.traverseValue(field)
}

case cadence.Attachment:
for _, field := range v.Fields {
ct.traverseValue(field)
}

Check warning on line 122 in encoding/ccf/traverse_value.go

View check run for this annotation

Codecov / codecov/patch

encoding/ccf/traverse_value.go#L119-L122

Added lines #L119 - L122 were not covered by tests
}
}

Expand Down Expand Up @@ -152,7 +156,7 @@
}
return check

case cadence.CompositeType: // struct, resource, event, contract, enum
case cadence.CompositeType: // struct, resource, event, contract, enum, attachment
newType := ct.add(typ)
if !newType {
return ct.abstractTypes[typ.ID()]
Expand Down
Loading