Skip to content

Commit

Permalink
Merge pull request #3432 from onflow/bastian/2952-remove-type-fields-…
Browse files Browse the repository at this point in the history
…access
  • Loading branch information
turbolent authored Jun 24, 2024
2 parents a1b1e4e + 3af6c8a commit b69fa58
Show file tree
Hide file tree
Showing 22 changed files with 1,621 additions and 1,149 deletions.
796 changes: 432 additions & 364 deletions encoding/ccf/ccf_test.go

Large diffs are not rendered by default.

22 changes: 12 additions & 10 deletions encoding/ccf/ccf_type_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,24 @@ func TestCadenceTypeByCCFTypeID(t *testing.T) {
}

func simpleStructType() *cadence.StructType {
return &cadence.StructType{
Location: utils.TestLocation,
QualifiedIdentifier: "FooStruct",
Fields: []cadence.Field{
return cadence.NewStructType(
utils.TestLocation,
"FooStruct",
[]cadence.Field{
{
Identifier: "a",
Type: cadence.IntType,
},
},
}
nil,
)
}

func simpleStructType2() *cadence.StructType {
return &cadence.StructType{
Location: utils.TestLocation,
QualifiedIdentifier: "FooStruct2",
Fields: []cadence.Field{
return cadence.NewStructType(
utils.TestLocation,
"FooStruct2",
[]cadence.Field{
{
Identifier: "a",
Type: cadence.IntType,
Expand All @@ -142,5 +143,6 @@ func simpleStructType2() *cadence.StructType {
Type: cadence.StringType,
},
},
}
nil,
)
}
45 changes: 29 additions & 16 deletions encoding/ccf/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"math"
"math/big"
goRuntime "runtime"
_ "unsafe"

"github.com/fxamacker/cbor/v2"

Expand Down Expand Up @@ -1210,11 +1211,17 @@ func (d *Decoder) decodeComposite(fieldTypes []cadence.Field, types *cadenceType
return fieldValues, nil
}

//go:linkname getCompositeTypeFields github.com/onflow/cadence.getCompositeTypeFields
func getCompositeTypeFields(cadence.CompositeType) []cadence.Field

//go:linkname getInterfaceTypeFields github.com/onflow/cadence.getInterfaceTypeFields
func getInterfaceTypeFields(cadence.InterfaceType) []cadence.Field

// decodeStruct decodes encoded composite-value as
// language=CDDL
// composite-value = [* (field: value)]
func (d *Decoder) decodeStruct(typ *cadence.StructType, types *cadenceTypeByCCFTypeID) (cadence.Value, error) {
fieldValues, err := d.decodeComposite(typ.Fields, types)
fieldValues, err := d.decodeComposite(getCompositeTypeFields(typ), types)
if err != nil {
return nil, err
}
Expand All @@ -1238,7 +1245,7 @@ func (d *Decoder) decodeStruct(typ *cadence.StructType, types *cadenceTypeByCCFT
// language=CDDL
// composite-value = [* (field: value)]
func (d *Decoder) decodeResource(typ *cadence.ResourceType, types *cadenceTypeByCCFTypeID) (cadence.Value, error) {
fieldValues, err := d.decodeComposite(typ.Fields, types)
fieldValues, err := d.decodeComposite(getCompositeTypeFields(typ), types)
if err != nil {
return nil, err
}
Expand All @@ -1262,7 +1269,7 @@ func (d *Decoder) decodeResource(typ *cadence.ResourceType, types *cadenceTypeBy
// language=CDDL
// composite-value = [* (field: value)]
func (d *Decoder) decodeEvent(typ *cadence.EventType, types *cadenceTypeByCCFTypeID) (cadence.Value, error) {
fieldValues, err := d.decodeComposite(typ.Fields, types)
fieldValues, err := d.decodeComposite(getCompositeTypeFields(typ), types)
if err != nil {
return nil, err
}
Expand All @@ -1286,7 +1293,7 @@ func (d *Decoder) decodeEvent(typ *cadence.EventType, types *cadenceTypeByCCFTyp
// language=CDDL
// composite-value = [* (field: value)]
func (d *Decoder) decodeContract(typ *cadence.ContractType, types *cadenceTypeByCCFTypeID) (cadence.Value, error) {
fieldValues, err := d.decodeComposite(typ.Fields, types)
fieldValues, err := d.decodeComposite(getCompositeTypeFields(typ), types)
if err != nil {
return nil, err
}
Expand All @@ -1310,7 +1317,7 @@ func (d *Decoder) decodeContract(typ *cadence.ContractType, types *cadenceTypeBy
// language=CDDL
// composite-value = [* (field: value)]
func (d *Decoder) decodeEnum(typ *cadence.EnumType, types *cadenceTypeByCCFTypeID) (cadence.Value, error) {
fieldValues, err := d.decodeComposite(typ.Fields, types)
fieldValues, err := d.decodeComposite(getCompositeTypeFields(typ), types)
if err != nil {
return nil, err
}
Expand All @@ -1334,7 +1341,7 @@ func (d *Decoder) decodeEnum(typ *cadence.EnumType, types *cadenceTypeByCCFTypeI
// 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)
fieldValues, err := d.decodeComposite(getCompositeTypeFields(typ), types)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1780,8 +1787,8 @@ func (d *Decoder) decodeAttachmentTypeValue(visited *cadenceTypeByCCFTypeID) (ca
return cadence.NewMeteredAttachmentType(
d.gauge,
location,
typ,
qualifiedIdentifier,
typ,
nil,
nil,
), nil
Expand Down Expand Up @@ -1894,6 +1901,12 @@ type compositeTypeValue struct {
rawInitializers []byte
}

//go:linkname setCompositeTypeFields github.com/onflow/cadence.setCompositeTypeFields
func setCompositeTypeFields(cadence.CompositeType, []cadence.Field)

//go:linkname setInterfaceTypeFields github.com/onflow/cadence.setInterfaceTypeFields
func setInterfaceTypeFields(cadence.InterfaceType, []cadence.Field)

// decodeCompositeTypeValue decodes composite-type-value.
// See _decodeCompositeTypeValue for details.
func (d *Decoder) decodeCompositeTypeValue(
Expand Down Expand Up @@ -1943,11 +1956,11 @@ func (d *Decoder) decodeCompositeTypeValue(

switch compositeType := compositeType.(type) {
case *cadence.StructType:
compositeType.Fields = fields
setCompositeTypeFields(compositeType, fields)
compositeType.Initializers = initializers

case *cadence.ResourceType:
compositeType.Fields = fields
setCompositeTypeFields(compositeType, fields)
compositeType.Initializers = initializers

case *cadence.EventType:
Expand All @@ -1957,31 +1970,31 @@ func (d *Decoder) decodeCompositeTypeValue(
len(initializers),
)
}
compositeType.Fields = fields
setCompositeTypeFields(compositeType, fields)
compositeType.Initializer = initializers[0]

case *cadence.ContractType:
compositeType.Fields = fields
setCompositeTypeFields(compositeType, fields)
compositeType.Initializers = initializers

case *cadence.EnumType:
compositeType.Fields = fields
setCompositeTypeFields(compositeType, fields)
compositeType.Initializers = initializers

case *cadence.AttachmentType:
compositeType.Fields = fields
setCompositeTypeFields(compositeType, fields)
compositeType.Initializers = initializers

case *cadence.StructInterfaceType:
compositeType.Fields = fields
setInterfaceTypeFields(compositeType, fields)
compositeType.Initializers = initializers

case *cadence.ResourceInterfaceType:
compositeType.Fields = fields
setInterfaceTypeFields(compositeType, fields)
compositeType.Initializers = initializers

case *cadence.ContractInterfaceType:
compositeType.Fields = fields
setInterfaceTypeFields(compositeType, fields)
compositeType.Initializers = initializers
}

Expand Down
4 changes: 2 additions & 2 deletions encoding/ccf/decode_typedef.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (d *Decoder) decodeTypeDefs() (*cadenceTypeByCCFTypeID, error) {

switch typ := typ.(type) {
case cadence.CompositeType:
typ.SetCompositeFields(fields)
setCompositeTypeFields(typ, fields)

default:
return nil, fmt.Errorf("unsupported type %s (%T) in composite-typedef", typ.ID(), typ)
Expand Down Expand Up @@ -269,10 +269,10 @@ func (d *Decoder) decodeTypeDef(
return cadence.NewMeteredAttachmentType(
d.gauge,
location,
nil,
identifier,
nil,
nil,
nil,
)
}
return d.decodeCompositeType(types, ctr)
Expand Down
40 changes: 20 additions & 20 deletions encoding/ccf/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ func (e *Encoder) encodeValue(
// encodeVoid encodes cadence.Void as
// language=CDDL
// void-value = nil
func (e *Encoder) encodeVoid(v cadence.Void) error {
func (e *Encoder) encodeVoid(_ cadence.Void) error {
return e.enc.EncodeNil()
}

Expand Down Expand Up @@ -1022,16 +1022,16 @@ func (e *Encoder) encodeInclusiveRange(v *cadence.InclusiveRange, tids ccfTypeID
return e.encodeValue(v.Step, staticElementType, tids)
}

//go:linkname getFieldValues github.com/onflow/cadence.getFieldValues
func getFieldValues(cadence.Composite) []cadence.Value
//go:linkname getCompositeFieldValues github.com/onflow/cadence.getCompositeFieldValues
func getCompositeFieldValues(cadence.Composite) []cadence.Value

// encodeStruct encodes cadence.Struct as
// language=CDDL
// composite-value = [* (field: value)]
func (e *Encoder) encodeStruct(v cadence.Struct, tids ccfTypeIDByCadenceType) error {
return e.encodeComposite(
v.StructType,
getFieldValues(v),
getCompositeFieldValues(v),
tids,
)
}
Expand All @@ -1042,7 +1042,7 @@ func (e *Encoder) encodeStruct(v cadence.Struct, tids ccfTypeIDByCadenceType) er
func (e *Encoder) encodeResource(v cadence.Resource, tids ccfTypeIDByCadenceType) error {
return e.encodeComposite(
v.ResourceType,
getFieldValues(v),
getCompositeFieldValues(v),
tids,
)
}
Expand All @@ -1053,7 +1053,7 @@ func (e *Encoder) encodeResource(v cadence.Resource, tids ccfTypeIDByCadenceType
func (e *Encoder) encodeEvent(v cadence.Event, tids ccfTypeIDByCadenceType) error {
return e.encodeComposite(
v.EventType,
getFieldValues(v),
getCompositeFieldValues(v),
tids,
)
}
Expand All @@ -1064,7 +1064,7 @@ func (e *Encoder) encodeEvent(v cadence.Event, tids ccfTypeIDByCadenceType) erro
func (e *Encoder) encodeContract(v cadence.Contract, tids ccfTypeIDByCadenceType) error {
return e.encodeComposite(
v.ContractType,
getFieldValues(v),
getCompositeFieldValues(v),
tids,
)
}
Expand All @@ -1075,7 +1075,7 @@ func (e *Encoder) encodeContract(v cadence.Contract, tids ccfTypeIDByCadenceType
func (e *Encoder) encodeEnum(v cadence.Enum, tids ccfTypeIDByCadenceType) error {
return e.encodeComposite(
v.EnumType,
getFieldValues(v),
getCompositeFieldValues(v),
tids,
)
}
Expand All @@ -1086,7 +1086,7 @@ func (e *Encoder) encodeEnum(v cadence.Enum, tids ccfTypeIDByCadenceType) error
func (e *Encoder) encodeAttachment(v cadence.Attachment, tids ccfTypeIDByCadenceType) error {
return e.encodeComposite(
v.AttachmentType,
getFieldValues(v),
getCompositeFieldValues(v),
tids,
)
}
Expand All @@ -1099,7 +1099,7 @@ func (e *Encoder) encodeComposite(
fields []cadence.Value,
tids ccfTypeIDByCadenceType,
) error {
staticFieldTypes := typ.CompositeFields()
staticFieldTypes := getCompositeTypeFields(typ)

if len(staticFieldTypes) != len(fields) {
panic(cadenceErrors.NewUnexpectedError(
Expand Down Expand Up @@ -1564,7 +1564,7 @@ func (e *Encoder) encodeStructTypeValue(typ *cadence.StructType, visited ccfType
return e.encodeCompositeTypeValue(
typ.ID(),
nil,
typ.Fields,
getCompositeTypeFields(typ),
typ.Initializers,
visited,
rawTagNum,
Expand All @@ -1582,7 +1582,7 @@ func (e *Encoder) encodeResourceTypeValue(typ *cadence.ResourceType, visited ccf
return e.encodeCompositeTypeValue(
typ.ID(),
nil,
typ.Fields,
getCompositeTypeFields(typ),
typ.Initializers,
visited,
rawTagNum,
Expand All @@ -1600,7 +1600,7 @@ func (e *Encoder) encodeEventTypeValue(typ *cadence.EventType, visited ccfTypeID
return e.encodeCompositeTypeValue(
typ.ID(),
nil,
typ.Fields,
getCompositeTypeFields(typ),
[][]cadence.Parameter{typ.Initializer},
visited,
rawTagNum,
Expand All @@ -1618,7 +1618,7 @@ func (e *Encoder) encodeContractTypeValue(typ *cadence.ContractType, visited ccf
return e.encodeCompositeTypeValue(
typ.ID(),
nil,
typ.Fields,
getCompositeTypeFields(typ),
typ.Initializers,
visited,
rawTagNum,
Expand All @@ -1636,7 +1636,7 @@ func (e *Encoder) encodeEnumTypeValue(typ *cadence.EnumType, visited ccfTypeIDBy
return e.encodeCompositeTypeValue(
typ.ID(),
typ.RawType,
typ.Fields,
getCompositeTypeFields(typ),
typ.Initializers,
visited,
rawTagNum,
Expand All @@ -1654,7 +1654,7 @@ func (e *Encoder) encodeAttachmentTypeValue(typ *cadence.AttachmentType, visited
return e.encodeCompositeTypeValue(
typ.ID(),
typ.BaseType,
typ.Fields,
getCompositeTypeFields(typ),
typ.Initializers,
visited,
rawTagNum,
Expand All @@ -1672,7 +1672,7 @@ func (e *Encoder) encodeStructInterfaceTypeValue(typ *cadence.StructInterfaceTyp
return e.encodeCompositeTypeValue(
typ.ID(),
nil,
typ.Fields,
getInterfaceTypeFields(typ),
typ.Initializers,
visited,
rawTagNum,
Expand All @@ -1690,7 +1690,7 @@ func (e *Encoder) encodeResourceInterfaceTypeValue(typ *cadence.ResourceInterfac
return e.encodeCompositeTypeValue(
typ.ID(),
nil,
typ.Fields,
getInterfaceTypeFields(typ),
typ.Initializers,
visited,
rawTagNum,
Expand All @@ -1708,7 +1708,7 @@ func (e *Encoder) encodeContractInterfaceTypeValue(typ *cadence.ContractInterfac
return e.encodeCompositeTypeValue(
typ.ID(),
nil,
typ.Fields,
getInterfaceTypeFields(typ),
typ.Initializers,
visited,
rawTagNum,
Expand Down Expand Up @@ -2182,7 +2182,7 @@ func (e *Encoder) getSortedFieldIndex(t cadence.CompositeType) []int {
// NOTE: bytewiseFieldIdentifierSorter doesn't sort fields in place.
// bytewiseFieldIdentifierSorter.indexes is used as sorted fieldTypes
// index.
sorter := newBytewiseFieldSorter(t.CompositeFields())
sorter := newBytewiseFieldSorter(getCompositeTypeFields(t))

sort.Sort(sorter)

Expand Down
2 changes: 1 addition & 1 deletion encoding/ccf/encode_typedef.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (e *Encoder) encodeCompositeType(typ cadence.CompositeType, tids ccfTypeIDB
// ]
// ]
func (e *Encoder) encodeCompositeTypeFields(typ cadence.CompositeType, tids ccfTypeIDByCadenceType) error {
fieldTypes := typ.CompositeFields()
fieldTypes := getCompositeTypeFields(typ)

// Encode array head with number of fields.
err := e.enc.EncodeArrayHead(uint64(len(fieldTypes)))
Expand Down
Loading

0 comments on commit b69fa58

Please sign in to comment.