Skip to content

Commit

Permalink
Exported genesis validation fix (#934)
Browse files Browse the repository at this point in the history
* update expected keeper

* add methods to expose other keepers from bankKeeper

* fix amm module to support exported genesis validation

* Add amm module v7 migration

* update amm module module.go for v7 migration

* update amm migration, return error

---------

Co-authored-by: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com>
  • Loading branch information
99adarsh and cosmic-vagabond authored Nov 20, 2024
1 parent 0b231d2 commit 07bdc9d
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 8 deletions.
19 changes: 19 additions & 0 deletions x/amm/keeper/get_internal_keepers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package keeper

import (
"github.com/elys-network/elys/x/amm/types"
)

// To be used in migration

func (k Keeper) GetAccountKeeper() types.AccountKeeper {
return k.accountKeeper
}

func (k Keeper) GetAssetProfileKeeper() types.AssetProfileKeeper {
return k.assetProfileKeeper
}

func (k Keeper) GetBankKeeper() types.BankKeeper {
return k.bankKeeper
}
5 changes: 4 additions & 1 deletion x/amm/keeper/keeper_create_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func (k Keeper) CreatePool(ctx sdk.Context, msg *types.MsgCreatePool) (uint64, e
}

// create and save the pool's module account to the account keeper
if err := utils.CreateModuleAccount(ctx, k.accountKeeper, address); err != nil {
poolAccountModuleName := types.GetPoolIdModuleName(pool.PoolId)
if err := utils.CreateModuleAccount(ctx, k.accountKeeper, address, poolAccountModuleName); err != nil {
return 0, fmt.Errorf("creating pool module account for id %d: %w", poolId, err)
}

Expand Down Expand Up @@ -120,6 +121,8 @@ func (k Keeper) InitializePool(ctx sdk.Context, pool *types.Pool, sender sdk.Acc
},
Base: poolShareBaseDenom,
Display: poolShareDisplayDenom,
Name: poolShareBaseDenom,
Symbol: poolShareDisplayDenom,
})

k.SetPool(ctx, *pool)
Expand Down
54 changes: 54 additions & 0 deletions x/amm/migrations/v7_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package migrations

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/amm/types"
"github.com/elys-network/elys/x/amm/utils"
)

func (m Migrator) V7Migration(ctx sdk.Context) error {
pools := m.keeper.GetAllPool(ctx)
for _, pool := range pools {
newPoolAddress := types.NewPoolAddress(pool.PoolId)
poolAccountModuleName := types.GetPoolIdModuleName(pool.PoolId)
if err := utils.CreateModuleAccount(ctx, m.keeper.GetAccountKeeper(), newPoolAddress, poolAccountModuleName); err != nil {
return fmt.Errorf("error creating new pool account for %d: %w", pool.PoolId, err)
}
poolAccAddress := sdk.MustAccAddressFromBech32(pool.Address)
// Bank: Transfer funds from prevPoolAddress to new newPoolAddress
prevPoolAddressBalances := m.keeper.GetBankKeeper().GetAllBalances(ctx, poolAccAddress)
m.keeper.GetBankKeeper().SendCoins(ctx, poolAccAddress, newPoolAddress, prevPoolAddressBalances)

// AssetProfile: Update authority in assetprofile entry
poolBaseDenom := types.GetPoolShareDenom(pool.PoolId)

entry, found := m.keeper.GetAssetProfileKeeper().GetEntry(ctx, poolBaseDenom)
// Should not happen
if !found {
return fmt.Errorf("assetprofile not found for basedenom: %s", poolBaseDenom)
}

entry.Authority = newPoolAddress.String()
m.keeper.GetAssetProfileKeeper().SetEntry(ctx, entry)

pool.Address = newPoolAddress.String()

m.keeper.SetPool(ctx, pool)

// Update the name and Symbol of pool share token metadata
metadata, found := m.keeper.GetBankKeeper().GetDenomMetaData(ctx, poolBaseDenom)
// Should not happen
if !found {
return fmt.Errorf("denom metadata for poolshare denom not found in bank denom metadata: %s", poolBaseDenom)
}
metadata.Name = metadata.Base
metadata.Symbol = metadata.Display

m.keeper.GetBankKeeper().SetDenomMetaData(ctx, metadata)
}
ctx.Logger().Info("Bank Module Migration Finished")

return nil
}
8 changes: 5 additions & 3 deletions x/amm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package amm

import (
"context"
"cosmossdk.io/core/appmodule"
"encoding/json"
"fmt"

"cosmossdk.io/core/appmodule"

// this line is used by starport scaffolding # 1

"github.com/grpc-ecosystem/grpc-gateway/runtime"
Expand Down Expand Up @@ -130,7 +132,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
m := migrations.NewMigrator(am.keeper)
err := cfg.RegisterMigration(types.ModuleName, 5, m.V6Migration)
err := cfg.RegisterMigration(types.ModuleName, 6, m.V7Migration)
if err != nil {
panic(err)
}
Expand All @@ -157,7 +159,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
func (AppModule) ConsensusVersion() uint64 { return 6 }
func (AppModule) ConsensusVersion() uint64 { return 7 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(_ context.Context) error {
Expand Down
2 changes: 2 additions & 0 deletions x/amm/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ type AccountKeeper interface {
// BankKeeper defines the expected interface needed to retrieve account balances.
type BankKeeper interface {
GetBalance(goCtx context.Context, addr sdk.AccAddress, denom string) sdk.Coin
GetAllBalances(ctx context.Context, addr sdk.AccAddress) sdk.Coins
SpendableCoins(goCtx context.Context, addr sdk.AccAddress) sdk.Coins
MintCoins(goCtx context.Context, moduleName string, amt sdk.Coins) error
BurnCoins(goCtx context.Context, name string, amt sdk.Coins) error
SendCoinsFromModuleToAccount(goCtx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromAccountToModule(goCtx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
SetDenomMetaData(goCtx context.Context, denomMetaData banktypes.Metadata)
GetDenomMetaData(ctx context.Context, denom string) (banktypes.Metadata, bool)
SendCoins(goCtx context.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error
// Methods imported from bank should be defined here
}
Expand Down
12 changes: 10 additions & 2 deletions x/amm/types/new_pool_address.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package types

import (
"strconv"

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

func NewPoolAddress(poolId uint64) sdk.AccAddress {
key := append([]byte("pool_account"), sdk.Uint64ToBigEndian(poolId)...)
return address.Module(ModuleName, key)
poolIdModuleName := GetPoolIdModuleName(poolId)
return address.Module(poolIdModuleName)
}

func GetPoolIdModuleName(poolId uint64) string {
poolIdStr := strconv.FormatUint(poolId, 10)
poolIdModuleName := ModuleName + "/pool/account/" + poolIdStr
return poolIdModuleName
}

func NewPoolRebalanceTreasury(poolId uint64) sdk.AccAddress {
Expand Down
4 changes: 2 additions & 2 deletions x/amm/utils/create_module_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// CreateModuleAccount creates a module account at the provided address.
// It overrides an account if it exists at that address, with a non-zero sequence number & pubkey
// Contract: addr is derived from `address.Module(ModuleName, key)`
func CreateModuleAccount(ctx sdk.Context, ak types.AccountKeeper, addr sdk.AccAddress) error {
func CreateModuleAccount(ctx sdk.Context, ak types.AccountKeeper, addr sdk.AccAddress, name string) error {
err := CanCreateModuleAccountAtAddr(ctx, ak, addr)
if err != nil {
return err
Expand All @@ -19,7 +19,7 @@ func CreateModuleAccount(ctx sdk.Context, ak types.AccountKeeper, addr sdk.AccAd
ctx,
authtypes.NewModuleAccount(
authtypes.NewBaseAccountWithAddress(addr),
addr.String(),
name,
),
)
ak.SetAccount(ctx, acc)
Expand Down

0 comments on commit 07bdc9d

Please sign in to comment.