Skip to content

Commit

Permalink
Re-hash account related types during migration
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Jan 12, 2024
1 parent 11de480 commit 659e292
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 0 deletions.
231 changes: 231 additions & 0 deletions migrations/account_type/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package account_type

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -870,3 +871,233 @@ func TestValuesWithStaticTypeMigration(t *testing.T) {
})
}
}

var testAddress = common.Address{0x42}

func TestRehash(t *testing.T) {

t.Parallel()

locationRange := interpreter.EmptyLocationRange

ledger := NewTestLedger(nil, nil)

storageMapKey := interpreter.StringStorageMapKey("dict")
newStringValue := func(s string) interpreter.Value {
return interpreter.NewUnmeteredStringValue(s)
}

newStorageAndInterpreter := func(t *testing.T) (*runtime.Storage, *interpreter.Interpreter) {
storage := runtime.NewStorage(ledger, nil)
inter, err := interpreter.NewInterpreter(
nil,
utils.TestLocation,
&interpreter.Config{
Storage: storage,
AtreeValueValidationEnabled: false,
AtreeStorageValidationEnabled: true,
},
)
require.NoError(t, err)

return storage, inter
}

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

storage, inter := newStorageAndInterpreter(t)

dictionaryStaticType := interpreter.NewDictionaryStaticType(
nil,
interpreter.PrimitiveStaticTypeMetaType,
interpreter.PrimitiveStaticTypeString,
)
dictValue := interpreter.NewDictionaryValue(inter, locationRange, dictionaryStaticType)

accountTypes := []interpreter.PrimitiveStaticType{
interpreter.PrimitiveStaticTypePublicAccount, //nolint:staticcheck
interpreter.PrimitiveStaticTypeAuthAccount, //nolint:staticcheck
interpreter.PrimitiveStaticTypeAuthAccountCapabilities, //nolint:staticcheck
interpreter.PrimitiveStaticTypePublicAccountCapabilities, //nolint:staticcheck
interpreter.PrimitiveStaticTypeAuthAccountAccountCapabilities, //nolint:staticcheck
interpreter.PrimitiveStaticTypeAuthAccountStorageCapabilities, //nolint:staticcheck
interpreter.PrimitiveStaticTypeAuthAccountContracts, //nolint:staticcheck
interpreter.PrimitiveStaticTypePublicAccountContracts, //nolint:staticcheck
interpreter.PrimitiveStaticTypeAuthAccountKeys, //nolint:staticcheck
interpreter.PrimitiveStaticTypePublicAccountKeys, //nolint:staticcheck
interpreter.PrimitiveStaticTypeAuthAccountInbox, //nolint:staticcheck
interpreter.PrimitiveStaticTypeAccountKey, //nolint:staticcheck
}

for _, typ := range accountTypes {
typeValue := interpreter.NewUnmeteredTypeValue(
migrations.LegacyPrimitiveStaticType{
PrimitiveStaticType: typ,
},
)
dictValue.Insert(
inter,
locationRange,
typeValue,
newStringValue(typ.String()),
)
}

storageMap := storage.GetStorageMap(
testAddress,
common.PathDomainStorage.Identifier(),
true,
)

storageMap.SetValue(inter,
storageMapKey,
dictValue.Transfer(
inter,
locationRange,
atree.Address(testAddress),
false,
nil,
nil,
),
)

err := storage.Commit(inter, false)
require.NoError(t, err)
})

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

storage, inter := newStorageAndInterpreter(t)

migration := migrations.NewStorageMigration(inter, storage)

reporter := newTestReporter()

migration.Migrate(
&migrations.AddressSliceIterator{
Addresses: []common.Address{
testAddress,
},
},
migration.NewValueMigrationsPathMigrator(
reporter,
NewAccountTypeMigration(),
),
)

err := migration.Commit()
require.NoError(t, err)

require.Equal(t,
map[interpreter.AddressPath]struct{}{
{
Address: testAddress,
Path: interpreter.PathValue{
Domain: common.PathDomainStorage,
Identifier: string(storageMapKey),
},
}: {},
},
reporter.migratedPaths,
)
})

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

storage, inter := newStorageAndInterpreter(t)

storageMap := storage.GetStorageMap(testAddress, common.PathDomainStorage.Identifier(), false)
storedValue := storageMap.ReadValue(inter, storageMapKey)

require.IsType(t, &interpreter.DictionaryValue{}, storedValue)

dictValue := storedValue.(*interpreter.DictionaryValue)

var existingKeys []interpreter.Value
dictValue.Iterate(inter, func(key, value interpreter.Value) (resume bool) {
existingKeys = append(existingKeys, key)
// continue iteration
return true
})

for _, key := range existingKeys {
actual := dictValue.Remove(
inter,
interpreter.EmptyLocationRange,
key,
)

assert.NotNil(t, actual)

staticType := key.(interpreter.TypeValue).Type

var possibleExpectedValues []interpreter.Value
var str string

switch {
case staticType.Equal(unauthorizedAccountReferenceType):
str = "PublicAccount"
case staticType.Equal(authAccountReferenceType):
str = "AuthAccount"
case staticType.Equal(interpreter.PrimitiveStaticTypeAccount_Capabilities):
// For both `AuthAccount.Capabilities` and `PublicAccount.Capabilities`,
// the migrated key is the same (`Account_Capabilities`).
// So the value at the key could be any of the two original values,
// depending on the order of migration.
possibleExpectedValues = []interpreter.Value{
interpreter.NewUnmeteredSomeValueNonCopying(
interpreter.NewUnmeteredStringValue("AuthAccountCapabilities"),
),
interpreter.NewUnmeteredSomeValueNonCopying(
interpreter.NewUnmeteredStringValue("PublicAccountCapabilities"),
),
}
case staticType.Equal(interpreter.PrimitiveStaticTypeAccount_AccountCapabilities):
str = "AuthAccountAccountCapabilities"
case staticType.Equal(interpreter.PrimitiveStaticTypeAccount_StorageCapabilities):
str = "AuthAccountStorageCapabilities"
case staticType.Equal(interpreter.PrimitiveStaticTypeAccount_Contracts):
// For both `AuthAccount.Contracts` and `PublicAccount.Contracts`,
// the migrated key is the same (Account_Contracts).
// So the value at the key could be any of the two original values,
// depending on the order of migration.
possibleExpectedValues = []interpreter.Value{
interpreter.NewUnmeteredSomeValueNonCopying(
interpreter.NewUnmeteredStringValue("AuthAccountContracts"),
),
interpreter.NewUnmeteredSomeValueNonCopying(
interpreter.NewUnmeteredStringValue("PublicAccountContracts"),
),
}
case staticType.Equal(interpreter.PrimitiveStaticTypeAccount_Keys):
// For both `AuthAccount.Keys` and `PublicAccount.Keys`,
// the migrated key is the same (Account_Keys).
// So the value at the key could be any of the two original values,
// depending on the order of migration.
possibleExpectedValues = []interpreter.Value{
interpreter.NewUnmeteredSomeValueNonCopying(
interpreter.NewUnmeteredStringValue("AuthAccountKeys"),
),
interpreter.NewUnmeteredSomeValueNonCopying(
interpreter.NewUnmeteredStringValue("PublicAccountKeys"),
),
}
case staticType.Equal(interpreter.PrimitiveStaticTypeAccount_Inbox):
str = "AuthAccountInbox"
case staticType.Equal(interpreter.AccountKeyStaticType):
str = "AccountKey"
default:
require.Fail(t, fmt.Sprintf("Unexpected type `%s` in dictionary key", staticType.ID()))
}

if possibleExpectedValues != nil {
assert.Contains(t, possibleExpectedValues, actual)
} else {
expected := interpreter.NewUnmeteredSomeValueNonCopying(
interpreter.NewUnmeteredStringValue(str),
)
assert.Equal(t, expected, actual)
}
}
})
}
65 changes: 65 additions & 0 deletions migrations/legacy_primitivestatic_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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 (
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/errors"
"github.com/onflow/cadence/runtime/interpreter"
)

// LegacyPrimitiveStaticType simulates the old primitive-static-type
// which uses the un-normalized string for hashing.
type LegacyPrimitiveStaticType struct {
interpreter.PrimitiveStaticType
}

var _ interpreter.StaticType = LegacyPrimitiveStaticType{}

func (t LegacyPrimitiveStaticType) ID() common.TypeID {
switch t.PrimitiveStaticType {
case interpreter.PrimitiveStaticTypeAuthAccount: //nolint:staticcheck
return "AuthAccount"
case interpreter.PrimitiveStaticTypePublicAccount: //nolint:staticcheck
return "PublicAccount"
case interpreter.PrimitiveStaticTypeAuthAccountCapabilities: //nolint:staticcheck
return "AuthAccount.Capabilities"
case interpreter.PrimitiveStaticTypePublicAccountCapabilities: //nolint:staticcheck
return "PublicAccount.Capabilities"
case interpreter.PrimitiveStaticTypeAuthAccountAccountCapabilities: //nolint:staticcheck
return "AuthAccount.AccountCapabilities"
case interpreter.PrimitiveStaticTypeAuthAccountStorageCapabilities: //nolint:staticcheck
return "AuthAccount.StorageCapabilities"
case interpreter.PrimitiveStaticTypeAuthAccountContracts: //nolint:staticcheck
return "AuthAccount.Contracts"
case interpreter.PrimitiveStaticTypePublicAccountContracts: //nolint:staticcheck
return "PublicAccount.Contracts"
case interpreter.PrimitiveStaticTypeAuthAccountKeys: //nolint:staticcheck
return "AuthAccount.Keys"
case interpreter.PrimitiveStaticTypePublicAccountKeys: //nolint:staticcheck
return "PublicAccount.Keys"
case interpreter.PrimitiveStaticTypeAuthAccountInbox: //nolint:staticcheck
return "AuthAccount.Inbox"
case interpreter.PrimitiveStaticTypeAccountKey:
return "AccountKey"
default:
panic(errors.NewUnreachableError())

}
}
19 changes: 19 additions & 0 deletions migrations/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,25 @@ func legacyType(staticType interpreter.StaticType) interpreter.StaticType {
return &LegacyReferenceType{
ReferenceStaticType: referenceType,
}

case interpreter.PrimitiveStaticType:
switch typ {
case interpreter.PrimitiveStaticTypeAuthAccount,
interpreter.PrimitiveStaticTypePublicAccount,
interpreter.PrimitiveStaticTypeAuthAccountCapabilities, //nolint:staticcheck
interpreter.PrimitiveStaticTypePublicAccountCapabilities, //nolint:staticcheck
interpreter.PrimitiveStaticTypeAuthAccountAccountCapabilities, //nolint:staticcheck
interpreter.PrimitiveStaticTypeAuthAccountStorageCapabilities, //nolint:staticcheck
interpreter.PrimitiveStaticTypeAuthAccountContracts, //nolint:staticcheck
interpreter.PrimitiveStaticTypePublicAccountContracts, //nolint:staticcheck
interpreter.PrimitiveStaticTypeAuthAccountKeys, //nolint:staticcheck
interpreter.PrimitiveStaticTypePublicAccountKeys, //nolint:staticcheck
interpreter.PrimitiveStaticTypeAuthAccountInbox, //nolint:staticcheck
interpreter.PrimitiveStaticTypeAccountKey:
return LegacyPrimitiveStaticType{
PrimitiveStaticType: typ, //nolint:staticcheck
}
}
}

return nil
Expand Down

0 comments on commit 659e292

Please sign in to comment.