Skip to content

Commit

Permalink
Merge pull request #2756 from onflow/bastian/improve-static-types
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Sep 1, 2023
2 parents 4b32c22 + 9c733bb commit 147803a
Show file tree
Hide file tree
Showing 57 changed files with 3,444 additions and 1,776 deletions.
32 changes: 16 additions & 16 deletions encoding/ccf/ccf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6125,8 +6125,8 @@ func TestEncodeEvent(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t,
cadence.ValueWithCachedTypeID(tc.val),
cadence.ValueWithCachedTypeID(decodedVal),
tc.val,
decodedVal,
)
})
}
Expand Down Expand Up @@ -8295,8 +8295,8 @@ func TestEncodeType(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t,
cadence.ValueWithCachedTypeID(val),
cadence.ValueWithCachedTypeID(decodedVal),
val,
decodedVal,
)
})

Expand Down Expand Up @@ -11571,8 +11571,8 @@ func testDecode(t *testing.T, actualCBOR []byte, expectedVal cadence.Value) {
require.NoError(t, err)
assert.Equal(
t,
cadence.ValueWithCachedTypeID(expectedVal),
cadence.ValueWithCachedTypeID(decodedVal),
expectedVal,
decodedVal,
)
}

Expand Down Expand Up @@ -12853,8 +12853,8 @@ func TestDeployedEvents(t *testing.T) {
// Since event encoding doesn't sort fields, make sure that input event is identical to decoded event.
require.Equal(
t,
cadence.ValueWithCachedTypeID(tc.event),
cadence.ValueWithCachedTypeID(decodedEvent),
tc.event,
decodedEvent,
)
})
}
Expand Down Expand Up @@ -14616,8 +14616,8 @@ func TestSortOptions(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t,
cadence.ValueWithCachedTypeID(expectedVal),
cadence.ValueWithCachedTypeID(decodedVal),
expectedVal,
decodedVal,
)

// Decode value enforcing sorting of composite fields should return error.
Expand Down Expand Up @@ -14805,8 +14805,8 @@ func TestSortOptions(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t,
cadence.ValueWithCachedTypeID(expectedVal),
cadence.ValueWithCachedTypeID(decodedVal),
expectedVal,
decodedVal,
)

// Decode value without enforcing sorting should return no error.
Expand Down Expand Up @@ -14994,8 +14994,8 @@ func TestSortOptions(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t,
cadence.ValueWithCachedTypeID(expectedVal),
cadence.ValueWithCachedTypeID(decodedVal),
expectedVal,
decodedVal,
)

// Decode value without enforcing sorting should return no error.
Expand Down Expand Up @@ -15183,8 +15183,8 @@ func TestSortOptions(t *testing.T) {
require.NoError(t, err)
assert.Equal(
t,
cadence.ValueWithCachedTypeID(expectedVal),
cadence.ValueWithCachedTypeID(decodedVal),
expectedVal,
decodedVal,
)

// Decode value without enforcing sorting should return no error.
Expand Down
22 changes: 11 additions & 11 deletions encoding/ccf/decode_typedef.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (d *Decoder) decodeTypeDef(
) {
tagNum, err := d.dec.DecodeTagNumber()
if err != nil {
return ccfTypeID(0), cadenceTypeID(""), nil, err
return ccfTypeID(0), "", nil, err
}

switch tagNum {
Expand Down Expand Up @@ -296,7 +296,7 @@ func (d *Decoder) decodeTypeDef(

default:
return ccfTypeID(0),
cadenceTypeID(""),
"",
nil,
fmt.Errorf("unsupported type definition with CBOR tag number %d", tagNum)
}
Expand Down Expand Up @@ -329,32 +329,32 @@ func (d *Decoder) decodeCompositeType(
// Decode array head of length 3.
err := decodeCBORArrayWithKnownSize(d.dec, 3)
if err != nil {
return ccfTypeID(0), cadenceTypeID(""), nil, err
return ccfTypeID(0), "", nil, err
}

// element 0: id
ccfID, err := d.decodeCCFTypeID()
if err != nil {
return ccfTypeID(0), cadenceTypeID(""), nil, err
return ccfTypeID(0), "", nil, err
}

// "Valid CCF Encoding Requirements" in CCF specs:
//
// "composite-type.id MUST be unique in ccf-typedef-message or ccf-typedef-and-value-message."
if types.has(ccfID) {
return ccfTypeID(0), cadenceTypeID(""), nil, fmt.Errorf("found duplicate CCF type ID %d in composite-type", ccfID)
return ccfTypeID(0), "", nil, fmt.Errorf("found duplicate CCF type ID %d in composite-type", ccfID)
}

// element 1: cadence-type-id
cadenceID, location, identifier, err := d.decodeCadenceTypeID()
if err != nil {
return ccfTypeID(0), cadenceTypeID(""), nil, err
return ccfTypeID(0), "", nil, err
}

// element 2: fields
rawField, err := d.dec.DecodeRawBytes()
if err != nil {
return ccfTypeID(0), cadenceTypeID(""), nil, err
return ccfTypeID(0), "", nil, err
}

// The return value can be ignored, because its non-existence was already checked above
Expand Down Expand Up @@ -476,26 +476,26 @@ func (d *Decoder) decodeInterfaceType(
// Decode array head of length 2.
err := decodeCBORArrayWithKnownSize(d.dec, 2)
if err != nil {
return ccfTypeID(0), cadenceTypeID(""), nil, err
return ccfTypeID(0), "", nil, err
}

// element 0: id
ccfID, err := d.decodeCCFTypeID()
if err != nil {
return ccfTypeID(0), cadenceTypeID(""), nil, err
return ccfTypeID(0), "", nil, err
}

// "Valid CCF Encoding Requirements" in CCF specs:
//
// "composite-type.id MUST be unique in ccf-typedef-message or ccf-typedef-and-value-message."
if types.has(ccfID) {
return ccfTypeID(0), cadenceTypeID(""), nil, fmt.Errorf("found duplicate CCF type ID %d in interface-type", ccfID)
return ccfTypeID(0), "", nil, fmt.Errorf("found duplicate CCF type ID %d in interface-type", ccfID)
}

// element 1: cadence-type-id
cadenceID, location, identifier, err := d.decodeCadenceTypeID()
if err != nil {
return ccfTypeID(0), cadenceTypeID(""), nil, err
return ccfTypeID(0), "", nil, err
}

// The return value can be ignored, because its non-existence was already checked above
Expand Down
4 changes: 2 additions & 2 deletions encoding/json/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3288,8 +3288,8 @@ func testDecode(t *testing.T, actualJSON string, expectedVal cadence.Value, opti

assert.Equal(
t,
cadence.ValueWithCachedTypeID(expectedVal),
cadence.ValueWithCachedTypeID(decodedVal),
expectedVal,
decodedVal,
)
}

Expand Down
24 changes: 2 additions & 22 deletions runtime/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,13 +881,7 @@ var SignAlgoType = ExportedBuiltinType(sema.SignatureAlgorithmType).(*cadence.En
var HashAlgoType = ExportedBuiltinType(sema.HashAlgorithmType).(*cadence.EnumType)

func ExportedBuiltinType(internalType sema.Type) cadence.Type {
typ := ExportType(internalType, map[sema.TypeID]cadence.Type{})

// These types are re-used across tests.
// Hence, cache the ID always to avoid any non-determinism.
typ = cadence.TypeWithCachedTypeID(typ)

return typ
return ExportType(internalType, map[sema.TypeID]cadence.Type{})
}

func newBytesValue(bytes []byte) cadence.Array {
Expand Down Expand Up @@ -921,7 +915,7 @@ func accountKeyExportedValue(
panic(err)
}

value := cadence.Struct{
return cadence.Struct{
StructType: AccountKeyType,
Fields: []cadence.Value{
// Key index
Expand Down Expand Up @@ -951,8 +945,6 @@ func accountKeyExportedValue(
cadence.NewBool(isRevoked),
},
}

return cadence.ValueWithCachedTypeID(value)
}

func getAccountKeyTestRuntimeInterface(storage *testAccountKeyStorage) *testRuntimeInterface {
Expand Down Expand Up @@ -1115,10 +1107,6 @@ func (test accountKeyTestCase) executeScript(
},
)

if err == nil {
value = cadence.ValueWithCachedTypeID(value)
}

return value, err
}

Expand Down Expand Up @@ -1155,10 +1143,6 @@ func TestRuntimePublicKey(t *testing.T) {
},
)

if err == nil {
value = cadence.ValueWithCachedTypeID(value)
}

return value, err
}

Expand Down Expand Up @@ -1197,7 +1181,6 @@ func TestRuntimePublicKey(t *testing.T) {
},
}

expected = cadence.ValueWithCachedTypeID(expected)
assert.Equal(t, expected, value)
})

Expand Down Expand Up @@ -1463,8 +1446,6 @@ func TestRuntimePublicKey(t *testing.T) {
},
}

expected = cadence.ValueWithCachedTypeID(expected)

assert.Equal(t, expected, value)
})

Expand Down Expand Up @@ -1505,7 +1486,6 @@ func TestRuntimePublicKey(t *testing.T) {
},
}

expected = cadence.ValueWithCachedTypeID(expected)
assert.Equal(t, expected, value)
})

Expand Down
10 changes: 6 additions & 4 deletions runtime/common/metering.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,12 @@ var (

// Static types string representations

VariableSizedStaticTypeStringMemoryUsage = NewRawStringMemoryUsage(2) // []
DictionaryStaticTypeStringMemoryUsage = NewRawStringMemoryUsage(4) // {: }
OptionalStaticTypeStringMemoryUsage = NewRawStringMemoryUsage(1) // ?
CapabilityStaticTypeStringMemoryUsage = NewRawStringMemoryUsage(12) // Capability<>
VariableSizedStaticTypeStringMemoryUsage = NewRawStringMemoryUsage(2) // []
DictionaryStaticTypeStringMemoryUsage = NewRawStringMemoryUsage(4) // {: }
OptionalStaticTypeStringMemoryUsage = NewRawStringMemoryUsage(1) // ?
CapabilityStaticTypeStringMemoryUsage = NewRawStringMemoryUsage(12) // Capability<>
IntersectionStaticTypeStringMemoryUsage = NewRawStringMemoryUsage(2) // {}
IntersectionStaticTypeSeparatorStringMemoryUsage = NewRawStringMemoryUsage(2) // ,
)

func UseMemory(gauge MemoryGauge, usage MemoryUsage) {
Expand Down
34 changes: 0 additions & 34 deletions runtime/common/orderedmap/orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
package orderedmap

import (
"sort"

"golang.org/x/exp/maps"

"github.com/onflow/cadence/runtime/common/list"
)

Expand Down Expand Up @@ -233,36 +229,6 @@ func (om *OrderedMap[K, V]) ForAllKeys(predicate func(key K) bool) bool {
return true
}

// MapKeys returns a new ordered map whose keys are mapped according to
// the provided mapping function between
func MapKeys[T OrderedMap[K, V], K comparable, V any, H comparable](om *OrderedMap[K, V], f func(K) H) *OrderedMap[H, V] {
mapped := New[OrderedMap[H, V]](om.Len())

om.Foreach(func(key K, value V) {
mapped.Set(f(key), value)
})

return mapped
}

// SortByKeys returns a new ordered map whose insertion order is sorted according to
// the provided comparison function between keys
func (om *OrderedMap[K, V]) SortByKey(compare func(K, K) bool) *OrderedMap[K, V] {
sorted := New[OrderedMap[K, V]](om.Len())
// non-deterministic order is okay here because the result is immediately sorted
keys := maps.Keys(om.pairs) //nolint:forbidigo
sort.Slice(keys, func(i, j int) bool {
return compare(keys[i], keys[j])
})

for _, key := range keys {
value, _ := om.Get(key)
sorted.Set(key, value)
}

return sorted
}

// ForAnyKey iterates over the keys of the map, and returns whether the provided
// predicate is true for any of them
func (om *OrderedMap[K, V]) ForAnyKey(predicate func(key K) bool) bool {
Expand Down
10 changes: 5 additions & 5 deletions runtime/convertTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ func exportAuthorization(
})
return cadence.EntitlementSetAuthorization{
Entitlements: entitlements,
Kind: cadence.EntitlementSetKind(access.SetKind),
Kind: access.SetKind,
}
}
panic(fmt.Sprintf("cannot export authorization with access %T", access))
Expand Down Expand Up @@ -619,15 +619,15 @@ func exportCapabilityType(
)
}

func importInterfaceType(memoryGauge common.MemoryGauge, t cadence.InterfaceType) interpreter.InterfaceStaticType {
func importInterfaceType(memoryGauge common.MemoryGauge, t cadence.InterfaceType) *interpreter.InterfaceStaticType {
return interpreter.NewInterfaceStaticTypeComputeTypeID(
memoryGauge,
t.InterfaceTypeLocation(),
t.InterfaceTypeQualifiedIdentifier(),
)
}

func importCompositeType(memoryGauge common.MemoryGauge, t cadence.CompositeType) interpreter.CompositeStaticType {
func importCompositeType(memoryGauge common.MemoryGauge, t cadence.CompositeType) *interpreter.CompositeStaticType {
return interpreter.NewCompositeStaticTypeComputeTypeID(
memoryGauge,
t.CompositeTypeLocation(),
Expand All @@ -646,7 +646,7 @@ func importAuthorization(memoryGauge common.MemoryGauge, auth cadence.Authorizat
memoryGauge,
func() []common.TypeID { return auth.Entitlements },
len(auth.Entitlements),
sema.EntitlementSetKind(auth.Kind),
auth.Kind,
)
}
panic(fmt.Sprintf("cannot import authorization of type %T", auth))
Expand Down Expand Up @@ -712,7 +712,7 @@ func ImportType(memoryGauge common.MemoryGauge, t cadence.Type) interpreter.Stat
)

case *cadence.IntersectionType:
types := make([]interpreter.InterfaceStaticType, 0, len(t.Types))
types := make([]*interpreter.InterfaceStaticType, 0, len(t.Types))
for _, typ := range t.Types {
intf, ok := typ.(cadence.InterfaceType)
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion runtime/convertValues.go
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ func (i valueImporter) importDictionaryValue(
keysAndValues[pairIndex*2+1] = value
}

var dictionaryStaticType interpreter.DictionaryStaticType
var dictionaryStaticType *interpreter.DictionaryStaticType
if dictionaryType != nil {
dictionaryStaticType = interpreter.ConvertSemaDictionaryTypeToStaticDictionaryType(inter, dictionaryType)
} else {
Expand Down
Loading

0 comments on commit 147803a

Please sign in to comment.