Skip to content

Commit

Permalink
Merge pull request #2872 from darkdrag00nv2/hashable_struct
Browse files Browse the repository at this point in the history
Introduce `HashableStruct` type to represent Dictionary keys
  • Loading branch information
SupunS authored Oct 27, 2023
2 parents 8be0edf + 8a16ed2 commit 323b887
Show file tree
Hide file tree
Showing 30 changed files with 535 additions and 188 deletions.
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 @@ -1348,7 +1348,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

0 comments on commit 323b887

Please sign in to comment.