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

Feat/wasmd migration #11

Merged
merged 2 commits into from
Mar 7, 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
3 changes: 3 additions & 0 deletions app/upgrades/v6_4_7/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v6_4_7

import (
"github.com/CosmWasm/wasmd/x/wasm"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand All @@ -20,6 +21,7 @@ import (
bech32slashingmigration "github.com/notional-labs/composable/v6/bech32-migration/slashing"
bech32stakingmigration "github.com/notional-labs/composable/v6/bech32-migration/staking"
bech32transfermiddlewaremigration "github.com/notional-labs/composable/v6/bech32-migration/transfermiddleware"
bech32WasmMigration "github.com/notional-labs/composable/v6/bech32-migration/wasm"
transfermiddlewaretypes "github.com/notional-labs/composable/v6/x/transfermiddleware/types"
)

Expand All @@ -42,6 +44,7 @@ func CreateUpgradeHandler(
bech32icamigration.MigrateAddressBech32(ctx, keys[icahosttypes.StoreKey], codec)
bech32mintmigration.MigrateAddressBech32(ctx, keys[minttypes.StoreKey], codec)
bech32transfermiddlewaremigration.MigrateAddressBech32(ctx, keys[transfermiddlewaretypes.StoreKey], codec)
bech32WasmMigration.MigrateAddressBech32(ctx, keys[wasm.StoreKey], codec)
return mm.RunMigrations(ctx, configurator, vm)
}
}
22 changes: 22 additions & 0 deletions bech32-migration/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ func ConvertAccAddr(accAddr string) string {
return bech32Addr
}

// Input is type string -> need safe convert
func SafeConvertAddress(accAddr string) string {
if len(accAddr) == 0 {
return ""
}

parsedAccAddr, err := AccAddressFromOldBech32(accAddr, OldBech32PrefixAccAddr)
if err != nil {
return accAddr
}
_, bz, err := bech32.DecodeAndConvert(parsedAccAddr.String())
if err != nil {
return accAddr
}
bech32Addr, err := bech32.ConvertAndEncode(NewBech32PrefixAccAddr, bz)
if err != nil {
return accAddr
}

return bech32Addr
}

func ConvertConsAddr(consAddr string) string {
parsedConsAddr, err := ConsAddressFromOldBech32(consAddr, OldBech32PrefixConsAddr)
_, bz, _ := bech32.DecodeAndConvert(parsedConsAddr.String())
Expand Down
78 changes: 78 additions & 0 deletions bech32-migration/wasm/wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package wasm

import (
"github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/notional-labs/composable/v6/bech32-migration/utils"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func MigrateAddressBech32(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) {
migrateCodeInfo(ctx, storeKey, cdc)
migrateContractInfo(ctx, storeKey, cdc)
}

func migrateCodeInfo(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) {
// Code id
ctx.Logger().Debug("Migrationg of address bech32 for wasm module Code Info begin")
prefixStore := prefix.NewStore(ctx.KVStore(storeKey), types.CodeKeyPrefix)
iter := prefixStore.Iterator(nil, nil)
defer iter.Close()

totalMigratedCodeId := uint64(0)
for ; iter.Valid(); iter.Next() {
// get code info value
var c types.CodeInfo
cdc.MustUnmarshal(iter.Value(), &c)

// Update info
c.Creator = utils.SafeConvertAddress(c.Creator)
c.InstantiateConfig.Address = utils.SafeConvertAddress(c.InstantiateConfig.Address)
for i := range c.InstantiateConfig.Addresses {
c.InstantiateConfig.Addresses[i] = utils.SafeConvertAddress(c.InstantiateConfig.Addresses[i])
}

// save updated code info
prefixStore.Set(iter.Key(), cdc.MustMarshal(&c))

totalMigratedCodeId++
}

// contract info prefix store
ctx.Logger().Debug(
"Migration of address bech32 for wasm module code info done",
"total_migrated_code_id", totalMigratedCodeId,
)
}

func migrateContractInfo(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) {
ctx.Logger().Debug("Migrating of addresses bech32 for wasm module Contract info begin")
// contract info prefix store
prefixStore := prefix.NewStore(ctx.KVStore(storeKey), types.ContractKeyPrefix)
iter := prefixStore.Iterator(nil, nil)

defer iter.Close()

totalMigratedContractAddresses := uint64(0)
for ; iter.Valid(); iter.Next() {
// get code info value
var c types.ContractInfo
cdc.MustUnmarshal(iter.Value(), &c)

// Update info
c.Creator = utils.SafeConvertAddress(c.Creator)
c.Admin = utils.SafeConvertAddress(c.Admin)
// save updated code info
prefixStore.Set(iter.Key(), cdc.MustMarshal(&c))

totalMigratedContractAddresses++
}

ctx.Logger().Debug(
"Migrating of addresses bech32 for wasm module Contract info done",
"total_migrated_contract_addresses", totalMigratedContractAddresses,
)
}
19 changes: 19 additions & 0 deletions scripts/upgrade/v_6_4_7/post-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ CODE_ID=1 ## TODO: hardfix for now to get the contract, and overide the contract
CONTRACT_ADDRESS=$($BINARY query wasm list-contract-by-code $CODE_ID -o json | jq -r '.contracts[0]')
echo "Query contract address: $CONTRACT_ADDRESS"

## Fetch code info
CREATOR=$($BINARY query wasm code-info $CODE_ID -o json | jq -r '.creator')
if [ "$CREATOR" == "$WALLET_1" ]; then
echo "Assertion passed: Code creator ($CREATOR) is equal to Wallet 1 ($WALLET_1)"
else
echo "Assertion failed: Code creator ($CREATOR) is not equal to Wallet 1 ($WALLET_1)"
exit 1
fi


## Fetch contract info
CONTRACT_CREATOR=$($BINARY query wasm contract $CONTRACT_ADDRESS -o json | jq -r '.contract_info.creator')
if [ "$CONTRACT_CREATOR" == "$WALLET_1" ]; then
echo "Assertion passed: Contract creator ($CONTRACT_CREATOR) is equal to Wallet 1 ($WALLET_1)"
else
echo "Assertion failed: Contract creator ($CONTRACT_CREATOR) is not equal to Wallet 1 ($WALLET_1)"
exit 1
fi

echo -e "\n Testing with new wallet / wallet that has not interacted with the contract before"
## Execute contract with new address
echo "wallet2: init the counter"
Expand Down
Loading