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

Improve Cadence 1.0 state migration #3211

Merged
merged 5 commits into from
Apr 2, 2024
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
30 changes: 18 additions & 12 deletions migrations/entitlements/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3108,7 +3108,8 @@ func TestRehash(t *testing.T) {
sema.Conjunction,
)

t.Run("prepare", func(t *testing.T) {
// Prepare
(func() {

storage, inter := newStorageAndInterpreter(t)

Expand Down Expand Up @@ -3169,9 +3170,10 @@ func TestRehash(t *testing.T) {

err = storage.CheckHealth()
require.NoError(t, err)
})
})()

t.Run("migrate", func(t *testing.T) {
// Migrate
(func() {

storage, inter := newStorageAndInterpreter(t)

Expand Down Expand Up @@ -3236,9 +3238,10 @@ func TestRehash(t *testing.T) {
},
reporter.migrated,
)
})
})()

t.Run("load", func(t *testing.T) {
// Load
(func() {

storage, inter := newStorageAndInterpreter(t)

Expand Down Expand Up @@ -3278,7 +3281,7 @@ func TestRehash(t *testing.T) {
newTestValue(),
value.(*interpreter.StringValue),
)
})
})()
}

func TestIntersectionTypeWithIntersectionLegacyType(t *testing.T) {
Expand Down Expand Up @@ -3324,7 +3327,8 @@ func TestIntersectionTypeWithIntersectionLegacyType(t *testing.T) {
return storage, inter
}

t.Run("prepare", func(t *testing.T) {
// Prepare
(func() {

storage, inter := newStorageAndInterpreter(t)

Expand Down Expand Up @@ -3367,9 +3371,10 @@ func TestIntersectionTypeWithIntersectionLegacyType(t *testing.T) {

err = storage.CheckHealth()
require.NoError(t, err)
})
})()

t.Run("migrate", func(t *testing.T) {
// Migrate
(func() {

storage, inter := newStorageAndInterpreter(t)

Expand Down Expand Up @@ -3426,9 +3431,10 @@ func TestIntersectionTypeWithIntersectionLegacyType(t *testing.T) {
},
reporter.migrated,
)
})
})()

t.Run("load", func(t *testing.T) {
// Load
(func() {

storage, inter := newStorageAndInterpreter(t)

Expand Down Expand Up @@ -3460,5 +3466,5 @@ func TestIntersectionTypeWithIntersectionLegacyType(t *testing.T) {
)

require.Equal(t, expectedType, typeValue.Type)
})
})()
}
11 changes: 11 additions & 0 deletions migrations/legacy_character_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ func (v *LegacyCharacterValue) HashInput(_ *interpreter.Interpreter, _ interpret
return buffer
}

func (v *LegacyCharacterValue) Equal(
inter *interpreter.Interpreter,
locationRange interpreter.LocationRange,
other interpreter.Value,
) bool {
if otherLegacy, ok := other.(*LegacyCharacterValue); ok {
other = otherLegacy.CharacterValue
}
return v.CharacterValue.Equal(inter, locationRange, other)
}

func (v *LegacyCharacterValue) Transfer(
interpreter *interpreter.Interpreter,
_ interpreter.LocationRange,
Expand Down
7 changes: 7 additions & 0 deletions migrations/legacy_intersection_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@ func (t *LegacyIntersectionType) ID() common.TypeID {
result.WriteByte('}')
return common.TypeID(result.String())
}

func (t *LegacyIntersectionType) Equal(other interpreter.StaticType) bool {
if otherLegacy, ok := other.(*LegacyIntersectionType); ok {
other = otherLegacy.IntersectionStaticType
}
return t.IntersectionStaticType.Equal(other)
}
7 changes: 7 additions & 0 deletions migrations/legacy_primitivestatic_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@ func (t LegacyPrimitiveStaticType) ID() common.TypeID {
panic(errors.NewUnexpectedError("unexpected non-legacy primitive static type: %s", primitiveStaticType))
}
}

func (t LegacyPrimitiveStaticType) Equal(other interpreter.StaticType) bool {
if otherLegacy, ok := other.(LegacyPrimitiveStaticType); ok {
other = otherLegacy.PrimitiveStaticType
}
return t.PrimitiveStaticType.Equal(other)
}
7 changes: 7 additions & 0 deletions migrations/legacy_reference_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,10 @@ func (t *LegacyReferenceType) Encode(e *cbor.StreamEncoder) error {
// Encode type at array index encodedReferenceStaticTypeTypeFieldKey
return t.ReferencedType.Encode(e)
}

func (t *LegacyReferenceType) Equal(other interpreter.StaticType) bool {
if otherLegacy, ok := other.(*LegacyReferenceType); ok {
other = otherLegacy.ReferenceStaticType
}
return t.ReferenceStaticType.Equal(other)
}
11 changes: 11 additions & 0 deletions migrations/legacy_string_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ func (v *LegacyStringValue) HashInput(_ *interpreter.Interpreter, _ interpreter.
return buffer
}

func (v *LegacyStringValue) Equal(
inter *interpreter.Interpreter,
locationRange interpreter.LocationRange,
other interpreter.Value,
) bool {
if otherLegacy, ok := other.(*LegacyStringValue); ok {
other = otherLegacy.StringValue
}
return v.StringValue.Equal(inter, locationRange, other)
}

func (v *LegacyStringValue) Transfer(
interpreter *interpreter.Interpreter,
_ interpreter.LocationRange,
Expand Down
116 changes: 116 additions & 0 deletions migrations/legacy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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 (
"testing"

"github.com/stretchr/testify/require"

"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/cadence/runtime/tests/utils"
)

func TestLegacyEquality(t *testing.T) {

t.Parallel()

t.Run("Character value", func(t *testing.T) {
t.Parallel()

require.True(t,
(&LegacyCharacterValue{
CharacterValue: interpreter.NewUnmeteredCharacterValue("foo"),
}).Equal(nil, emptyLocationRange, &LegacyCharacterValue{
CharacterValue: interpreter.NewUnmeteredCharacterValue("foo"),
}),
)
})

t.Run("String value", func(t *testing.T) {
t.Parallel()

require.True(t,
(&LegacyStringValue{
StringValue: interpreter.NewUnmeteredStringValue("foo"),
}).Equal(nil, emptyLocationRange, &LegacyStringValue{
StringValue: interpreter.NewUnmeteredStringValue("foo"),
}),
)
})

t.Run("Intersection type", func(t *testing.T) {
t.Parallel()

fooQualifiedIdentifier := "Test.Foo"
fooType := &interpreter.InterfaceStaticType{
Location: utils.TestLocation,
QualifiedIdentifier: fooQualifiedIdentifier,
TypeID: utils.TestLocation.TypeID(nil, fooQualifiedIdentifier),
}

require.True(t,
(&LegacyIntersectionType{
IntersectionStaticType: interpreter.NewIntersectionStaticType(
nil,
[]*interpreter.InterfaceStaticType{
fooType,
},
),
}).Equal(&LegacyIntersectionType{
IntersectionStaticType: interpreter.NewIntersectionStaticType(
nil,
[]*interpreter.InterfaceStaticType{
fooType,
},
),
}),
)
})

t.Run("Primitive type", func(t *testing.T) {
t.Parallel()

require.True(t,
(LegacyPrimitiveStaticType{
PrimitiveStaticType: interpreter.PrimitiveStaticTypeInt,
}).Equal(LegacyPrimitiveStaticType{
PrimitiveStaticType: interpreter.PrimitiveStaticTypeInt,
}),
)
})

t.Run("Reference type", func(t *testing.T) {
t.Parallel()

require.True(t,
(&LegacyReferenceType{
ReferenceStaticType: &interpreter.ReferenceStaticType{
Authorization: interpreter.UnauthorizedAccess,
ReferencedType: interpreter.PrimitiveStaticTypeInt,
},
}).Equal(&LegacyReferenceType{
ReferenceStaticType: &interpreter.ReferenceStaticType{
Authorization: interpreter.UnauthorizedAccess,
ReferencedType: interpreter.PrimitiveStaticTypeInt,
},
}),
)
})
}
7 changes: 7 additions & 0 deletions migrations/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,13 @@ func (m *StorageMigration) MigrateNestedValue(
inter.RemoveReferencedSlab(existingValueStorable)
}

if dictionary.ContainsKey(inter, emptyLocationRange, keyToSet) {
panic(errors.NewUnexpectedError(
"dictionary contains new key after removal of old key (conflict): %s",
keyToSet,
))
}

dictionary.InsertWithoutTransfer(
inter,
emptyLocationRange,
Expand Down
Loading
Loading