Skip to content

Commit

Permalink
Merge pull request #3272 from onflow/sainati/optional-type-id
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Apr 23, 2024
2 parents f7975d7 + 37e3c26 commit 5131b0a
Show file tree
Hide file tree
Showing 12 changed files with 620 additions and 185 deletions.
28 changes: 22 additions & 6 deletions migrations/entitlements/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3156,7 +3156,16 @@ func TestRehash(t *testing.T) {
ReferenceStaticType: refType,
}

typeValue := interpreter.NewUnmeteredTypeValue(legacyRefType)
optType := interpreter.NewOptionalStaticType(
nil,
legacyRefType,
)

legacyOptType := &migrations.LegacyOptionalType{
OptionalStaticType: optType,
}

typeValue := interpreter.NewUnmeteredTypeValue(legacyOptType)

dictValue.Insert(
inter,
Expand All @@ -3167,8 +3176,8 @@ func TestRehash(t *testing.T) {

// Note: ID is in the old format
assert.Equal(t,
common.TypeID("auth&A.4200000000000000.Foo.Bar"),
legacyRefType.ID(),
common.TypeID("auth&A.4200000000000000.Foo.Bar?"),
legacyOptType.ID(),
)

storageMap := storage.GetStorageMap(
Expand Down Expand Up @@ -3289,14 +3298,21 @@ func TestRehash(t *testing.T) {
newCompositeType(),
)

typeValue := interpreter.NewUnmeteredTypeValue(refType)
optType := interpreter.NewOptionalStaticType(
nil,
refType,
)

typeValue := interpreter.NewUnmeteredTypeValue(optType)

// Note: ID is in the new format
assert.Equal(t,
common.TypeID("auth(A.4200000000000000.E)&A.4200000000000000.Foo.Bar"),
refType.ID(),
common.TypeID("(auth(A.4200000000000000.E)&A.4200000000000000.Foo.Bar)?"),
optType.ID(),
)

assert.Equal(t, 1, dictValue.Count())

value, ok := dictValue.Get(inter, locationRange, typeValue)
require.True(t, ok)

Expand Down
44 changes: 44 additions & 0 deletions migrations/legacy_optional_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package migrations

import (
"fmt"

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

// LegacyOptionalType simulates the old optional type with the old typeID generation.
type LegacyOptionalType struct {
*interpreter.OptionalStaticType
}

var _ interpreter.StaticType = &LegacyOptionalType{}

func (t *LegacyOptionalType) ID() common.TypeID {
return common.TypeID(fmt.Sprintf("%s?", t.Type.ID()))
}

func (t *LegacyOptionalType) Equal(other interpreter.StaticType) bool {
if otherLegacy, ok := other.(*LegacyOptionalType); ok {
other = otherLegacy.OptionalStaticType
}
return t.OptionalStaticType.Equal(other)
}
Loading

0 comments on commit 5131b0a

Please sign in to comment.