From a8ce6422373ddcfd41bba298439c1a7c3b891862 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Thu, 12 Oct 2023 11:10:59 -0600 Subject: [PATCH] fix: cosmwasmpool state export (#6666) * use CosmWasmExtension * add changelog * just implement getId for deserialization * separate method for serializable pools * fix accident * add test (cherry picked from commit d4582d49b53b12eb1c4bca7a1f3ab815a04fbd54) # Conflicts: # CHANGELOG.md # x/cosmwasmpool/genesis.go # x/cosmwasmpool/genesis_test.go --- CHANGELOG.md | 5 +++++ x/cosmwasmpool/genesis.go | 19 +++++++++++++++++++ x/cosmwasmpool/genesis_test.go | 18 ++++++++++++++++++ x/cosmwasmpool/model/store_model.go | 2 +- x/cosmwasmpool/pool_module.go | 17 +++++++++++++++++ 5 files changed, 60 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f44f177acda..5468e418821 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Bug Fixes * [#6644](https://github.com/osmosis-labs/osmosis/pull/6644) fix: genesis bug in pool incentives linking NoLock gauges and PoolIDs +<<<<<<< HEAD +======= +* [#6666](https://github.com/osmosis-labs/osmosis/pull/6666) fix: cosmwasmpool state export bug +* [#6674](https://github.com/osmosis-labs/osmosis/pull/6674) fix: remove dragonberry replace directive +>>>>>>> d4582d49 (fix: cosmwasmpool state export (#6666)) ## v19.2.0 diff --git a/x/cosmwasmpool/genesis.go b/x/cosmwasmpool/genesis.go index ab1b38c762a..ee28991f2d4 100644 --- a/x/cosmwasmpool/genesis.go +++ b/x/cosmwasmpool/genesis.go @@ -26,6 +26,7 @@ func (k *Keeper) InitGenesis(ctx sdk.Context, gen *types.GenesisState, unpacker func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { params := k.GetParams(ctx) +<<<<<<< HEAD // TODO: We remove this as there is an issue with resolving the // type url for the cosmwasm pools. // panic: unable to resolve type URL / @@ -45,6 +46,24 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { // } // poolAnys = append(poolAnys, any) //} +======= + pools, err := k.GetPoolsSerializable(ctx) + if err != nil { + panic(err) + } + poolAnys := []*codectypes.Any{} + for _, poolI := range pools { + cosmwasmPool, ok := poolI.(types.CosmWasmExtension) + if !ok { + panic("invalid pool type") + } + any, err := codectypes.NewAnyWithValue(cosmwasmPool) + if err != nil { + panic(err) + } + poolAnys = append(poolAnys, any) + } +>>>>>>> d4582d49 (fix: cosmwasmpool state export (#6666)) return &types.GenesisState{ Params: params, diff --git a/x/cosmwasmpool/genesis_test.go b/x/cosmwasmpool/genesis_test.go index e47607cdb45..bda5bda4f1d 100644 --- a/x/cosmwasmpool/genesis_test.go +++ b/x/cosmwasmpool/genesis_test.go @@ -81,6 +81,7 @@ func (s *PoolModuleSuite) TestInitGenesis() { s.Require().Equal(expectedTotalLiquidity.String(), liquidity.String()) } +<<<<<<< HEAD // TODO: Fix this test when fixing genesis export functionality //func (s *PoolModuleSuite) TestExportGenesis() { // s.Setup() @@ -93,6 +94,23 @@ func (s *PoolModuleSuite) TestInitGenesis() { // genesis := s.App.CosmwasmPoolKeeper.ExportGenesis(s.Ctx) // s.Require().Len(genesis.Pools, 2) //} +======= +func (s *PoolModuleSuite) TestExportGenesis() { + s.Setup() + + for i := 0; i < 2; i++ { + s.FundAcc(s.TestAccs[0], initalDefaultSupply) + s.PrepareCustomTransmuterPool(s.TestAccs[0], defaultDenoms) + } + + genesis := s.App.CosmwasmPoolKeeper.ExportGenesis(s.Ctx) + s.Require().Len(genesis.Pools, 2) + + for _, pool := range genesis.Pools { + s.Require().Equal("/osmosis.cosmwasmpool.v1beta1.CosmWasmPool", pool.GetTypeUrl()) + } +} +>>>>>>> d4582d49 (fix: cosmwasmpool state export (#6666)) func (s *PoolModuleSuite) TestMarshalUnmarshalGenesis() { s.Setup() diff --git a/x/cosmwasmpool/model/store_model.go b/x/cosmwasmpool/model/store_model.go index 009eb8ed7f2..592378edf5b 100644 --- a/x/cosmwasmpool/model/store_model.go +++ b/x/cosmwasmpool/model/store_model.go @@ -44,7 +44,7 @@ func (p CosmWasmPool) GetAddress() sdk.AccAddress { } func (p CosmWasmPool) GetId() uint64 { - panic("CosmWasmPool.GetId not implemented") + return p.PoolId } func (p CosmWasmPool) GetSpreadFactor(ctx sdk.Context) osmomath.Dec { diff --git a/x/cosmwasmpool/pool_module.go b/x/cosmwasmpool/pool_module.go index c744519e64a..69b8351be67 100644 --- a/x/cosmwasmpool/pool_module.go +++ b/x/cosmwasmpool/pool_module.go @@ -110,6 +110,23 @@ func (k Keeper) GetPools(ctx sdk.Context) ([]poolmanagertypes.PoolI, error) { ) } +// GetPoolsSerializable retrieves all pool objects stored in the keeper. +// Because the Pool struct has a non-serializable wasmKeeper field, this method +// utilizes the CosmWasmPool struct directly instead, which allows it to be serialized +// in import/export genesis. +func (k Keeper) GetPoolsSerializable(ctx sdk.Context) ([]poolmanagertypes.PoolI, error) { + return osmoutils.GatherValuesFromStorePrefix( + ctx.KVStore(k.storeKey), types.PoolsKey, func(value []byte) (poolmanagertypes.PoolI, error) { + pool := model.CosmWasmPool{} + err := k.cdc.Unmarshal(value, &pool) + if err != nil { + return nil, err + } + return &pool, nil + }, + ) +} + // GetPoolsWithWasmKeeper behaves the same as GetPools, but it also sets the WasmKeeper field of the pool. func (k Keeper) GetPoolsWithWasmKeeper(ctx sdk.Context) ([]poolmanagertypes.PoolI, error) { return osmoutils.GatherValuesFromStorePrefix(