Skip to content

Commit

Permalink
Merge pull request #1548 from scrtlabs/fix-hardcoded-admins-migration…
Browse files Browse the repository at this point in the history
…-for-v1.12

Fix migration code for hardcoded admins (v1.12)
  • Loading branch information
assafmo authored Sep 28, 2023
2 parents f41258f + 11baa69 commit a51ce39
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
60 changes: 60 additions & 0 deletions x/compute/internal/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"fmt"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/scrtlabs/SecretNetwork/x/compute/internal/types"
Expand Down Expand Up @@ -63,3 +65,61 @@ func (m Migrator) Migrate1to2(ctx sdk.Context) error {

return nil
}

func (m Migrator) Migrate2to3(_ sdk.Context) error {
// Keep it empty it is only here because we had a bug in testnet
return nil
}

func (m Migrator) Migrate3to4(_ sdk.Context) error {
// Keep it empty it is only here because we had a bug in testnet
return nil
}

func (m Migrator) Migrate4to5(ctx sdk.Context) error {
store := prefix.NewStore(ctx.KVStore(m.keeper.storeKey), types.ContractKeyPrefix)
iter := store.Iterator(nil, nil)
defer iter.Close()

migrated := 0
ctx.Logger().Info(fmt.Sprintf("Migrated contracts: %d\n", migrated))

for ; iter.Valid(); iter.Next() {
var contractAddress sdk.AccAddress = iter.Key()

var contractInfo types.ContractInfo
m.keeper.cdc.MustUnmarshal(iter.Value(), &contractInfo)

// Pre v1.11 contracts don't have a history, so we'll add an initial history entry for them.
// This is required for the hardcode admin feature to work.
// This will also prevent an inconsistent state between pre v1.11 and post v1.11 contracts.
contractHistory := m.keeper.GetContractHistory(ctx, contractAddress)
if len(contractHistory) == 0 {
historyEntry := contractInfo.InitialHistory(nil)

// Persist the history entry changes.
m.keeper.addToContractCodeSecondaryIndex(ctx, contractAddress, historyEntry)
m.keeper.appendToContractHistory(ctx, contractAddress, historyEntry)
}

if hardcodedContractAdmins[contractAddress.String()] != "" {
// This is the same code as in Migrate1to2() but with store.Set() to persist the changes.

contractInfo.Admin = hardcodedContractAdmins[contractAddress.String()]
// When the contract has a hardcoded admin via gov, adminProof is ignored inside the enclave.
// Otherwise and if valid, adminProof is a 32 bytes array (output of sha256).
// For future proofing and avoiding passing null pointers to the enclave, we'll set it to a 32 bytes array of 0.
contractInfo.AdminProof = make([]byte, 32)

// Persist the contractInfo changes.
newContractInfoBz := m.keeper.cdc.MustMarshal(&contractInfo)
store.Set(iter.Key(), newContractInfoBz)
}

migrated++
if migrated%50 == 0 {
ctx.Logger().Info(fmt.Sprintf("Migrated contracts: %d\n", migrated))
}
}
return nil
}
16 changes: 15 additions & 1 deletion x/compute/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func NewAppModule(keeper Keeper) AppModule {
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 2 }
func (AppModule) ConsensusVersion() uint64 { return 5 }

func (am AppModule) RegisterServices(configurator module.Configurator) {
types.RegisterMsgServer(configurator.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
Expand All @@ -114,6 +114,20 @@ func (am AppModule) RegisterServices(configurator module.Configurator) {
if err != nil {
panic(err)
}
err = configurator.RegisterMigration(types.ModuleName, 2, m.Migrate2to3)
if err != nil {
panic(err)
}

err = configurator.RegisterMigration(types.ModuleName, 3, m.Migrate3to4)
if err != nil {
panic(err)
}

err = configurator.RegisterMigration(types.ModuleName, 4, m.Migrate4to5)
if err != nil {
panic(err)
}
}

func (am AppModule) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier {
Expand Down

0 comments on commit a51ce39

Please sign in to comment.