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

Introduce HashableStruct type to represent Dictionary keys #2872

Merged
merged 15 commits into from
Oct 27, 2023
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
32 changes: 32 additions & 0 deletions encoding/ccf/ccf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9249,6 +9249,38 @@ func TestEncodeType(t *testing.T) {

})

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

testEncodeAndDecode(
t,
cadence.TypeValue{
StaticType: cadence.HashableStructType{},
},
[]byte{
// language=json, format=json-cdc
// {"type":"Type","value":{"staticType":{"kind":"Struct", "type" : {"kind" : "HashableStruct"}}}}
//
// language=edn, format=ccf
// 130([137(41), 185(56)])
//
// 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.CBORTagSimpleTypeValue,
// HashableStruct type (56)
0x18, 0x38,
},
)
})

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

Expand Down
3 changes: 3 additions & 0 deletions encoding/ccf/decode_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ func (d *Decoder) decodeSimpleTypeID() (cadence.Type, error) {
case TypeAnyStruct:
return cadence.TheAnyStructType, nil

case TypeHashableStruct:
return cadence.TheHashableStructType, nil

case TypeAnyResource:
return cadence.TheAnyResourceType, nil

Expand Down
4 changes: 4 additions & 0 deletions encoding/ccf/simple_type_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const ( // Cadence simple type IDs
TypeWord256
TypeAnyStructAttachmentType
TypeAnyResourceAttachmentType
TypeHashableStruct
)

// NOTE: cadence.FunctionType isn't included in simpleTypeIDByType
Expand All @@ -99,6 +100,9 @@ func simpleTypeIDByType(typ cadence.Type) (uint64, bool) {
case cadence.AnyStructType:
return TypeAnyStruct, true

case cadence.HashableStructType:
return TypeHashableStruct, true

case cadence.AnyResourceType:
return TypeAnyResource, true

Expand Down
2 changes: 2 additions & 0 deletions encoding/json/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,8 @@ func (d *Decoder) decodeType(valueJSON any, results typeDecodingResults) cadence
return cadence.TheAnyType
case "AnyStruct":
return cadence.TheAnyStructType
case "HashableStruct":
return cadence.TheHashableStructType
case "AnyStructAttachment":
return cadence.TheAnyStructAttachmentType
case "AnyResource":
Expand Down
1 change: 1 addition & 0 deletions encoding/json/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,7 @@ func prepareType(typ cadence.Type, results typePreparationResults) jsonValue {
switch typ := typ.(type) {
case cadence.AnyType,
cadence.AnyStructType,
cadence.HashableStructType,
cadence.AnyStructAttachmentType,
cadence.AnyResourceType,
cadence.AnyResourceAttachmentType,
Expand Down
1 change: 1 addition & 0 deletions encoding/json/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,7 @@ func TestEncodeSimpleTypes(t *testing.T) {
for _, ty := range []cadence.Type{
cadence.AnyType{},
cadence.AnyStructType{},
cadence.HashableStructType{},
cadence.AnyStructAttachmentType{},
cadence.AnyResourceType{},
cadence.AnyResourceAttachmentType{},
Expand Down
4 changes: 4 additions & 0 deletions runtime/convertTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ func ExportMeteredType(
return cadence.TheAnyType
case sema.AnyStructType:
return cadence.TheAnyStructType
case sema.HashableStructType:
return cadence.TheHashableStructType
case sema.AnyResourceType:
return cadence.TheAnyResourceType
case sema.BlockType:
Expand Down Expand Up @@ -539,6 +541,8 @@ func ImportType(memoryGauge common.MemoryGauge, t cadence.Type) interpreter.Stat
return interpreter.NewPrimitiveStaticType(memoryGauge, interpreter.PrimitiveStaticTypeAny)
case cadence.AnyStructType:
return interpreter.NewPrimitiveStaticType(memoryGauge, interpreter.PrimitiveStaticTypeAnyStruct)
case cadence.HashableStructType:
return interpreter.NewPrimitiveStaticType(memoryGauge, interpreter.PrimitiveStaticTypeHashableStruct)
case cadence.AnyResourceType:
return interpreter.NewPrimitiveStaticType(memoryGauge, interpreter.PrimitiveStaticTypeAnyResource)
case *cadence.OptionalType:
Expand Down
2 changes: 1 addition & 1 deletion runtime/convertValues.go
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ func (i valueImporter) importDictionaryValue(
keySuperType := sema.LeastCommonSuperType(keyTypes...)
valueSuperType := sema.LeastCommonSuperType(valueTypes...)

if !sema.IsValidDictionaryKeyType(keySuperType) {
if !sema.IsSubType(keySuperType, sema.HashableStructType) {
return nil, errors.NewDefaultUserError(
"cannot import dictionary: keys does not belong to the same type",
)
Expand Down
Loading
Loading