Skip to content

Commit

Permalink
Simplify migrate version handling
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Jul 4, 2024
1 parent 6d6303f commit be0fbf3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 52 deletions.
40 changes: 4 additions & 36 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,42 +493,10 @@ func (k Keeper) migrate(
return nil, errorsmod.Wrap(types.ErrVMError, err.Error())
}

switch {
case report.ContractMigrateVersion != nil && oldReport.ContractMigrateVersion != nil:
// if both are set, we only migrate if the new version is higher
switch {
case *report.ContractMigrateVersion < *oldReport.ContractMigrateVersion:
// prevent downgrades
return nil, errorsmod.Wrapf(
types.ErrMigrationFailed,
"new migrate version %d is lower than current version %d",
*report.ContractMigrateVersion,
*oldReport.ContractMigrateVersion,
)
case *report.ContractMigrateVersion > *oldReport.ContractMigrateVersion:
// in the future, we will provide MigrateInfo to the contract in this case
response, err = k.callMigrateEntrypoint(sdkCtx, contractAddress, wasmvmtypes.Checksum(newCodeInfo.CodeHash), msg, setupCost)
if err != nil {
return nil, err
}
}
// no need to call the migrate entrypoint if the versions are the same
case report.ContractMigrateVersion != nil:
// migrating from contract without migrate version to one with migrate version
// in the future, we will provide MigrateInfo to the contract in this case
response, err = k.callMigrateEntrypoint(sdkCtx, contractAddress, wasmvmtypes.Checksum(newCodeInfo.CodeHash), msg, setupCost)
if err != nil {
return nil, err
}
case oldReport.ContractMigrateVersion != nil:
// prevent migrating from contract with migrate version to one without migrate version
return nil, errorsmod.Wrapf(
types.ErrMigrationFailed,
"cannot migrate from contract with migrate version %d to contract without migrate version",
*oldReport.ContractMigrateVersion,
)
default:
// we need to call the normal migrate entrypoint in this case
// call migrate entrypoint, except if both migrate versions are set and the same value
if report.ContractMigrateVersion == nil ||
oldReport.ContractMigrateVersion == nil ||
*report.ContractMigrateVersion != *oldReport.ContractMigrateVersion {
response, err = k.callMigrateEntrypoint(sdkCtx, contractAddress, wasmvmtypes.Checksum(newCodeInfo.CodeHash), msg, setupCost)
if err != nil {
return nil, err
Expand Down
32 changes: 16 additions & 16 deletions x/wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1382,23 +1382,23 @@ func TestMigrate(t *testing.T) {
migrateMsg: migMsgBz,
expVerifier: fred, // not updated
},
"fail when migrate version contract to no migrate version contract": {
admin: creator,
caller: creator,
initMsg: initMsgBz,
fromCodeID: hackatom42.CodeID,
toCodeID: originalCodeID,
migrateMsg: migMsgBz,
expErr: types.ErrMigrationFailed,
"all good with migrate version contract to no migrate version contract": {
admin: creator,
caller: creator,
initMsg: initMsgBz,
fromCodeID: hackatom42.CodeID,
toCodeID: originalCodeID,
migrateMsg: migMsgBz,
expVerifier: newVerifierAddr,
},
"prevent migration to older migrate version": {
admin: creator,
caller: creator,
initMsg: initMsgBz,
fromCodeID: hackatom420.CodeID,
toCodeID: hackatom42.CodeID,
migrateMsg: migMsgBz,
expErr: types.ErrMigrationFailed,
"all good with migration to older migrate version": {
admin: creator,
caller: creator,
initMsg: initMsgBz,
fromCodeID: hackatom420.CodeID,
toCodeID: hackatom42.CodeID,
migrateMsg: migMsgBz,
expVerifier: newVerifierAddr,
},
}

Expand Down

0 comments on commit be0fbf3

Please sign in to comment.