Skip to content

Commit

Permalink
fix: cosmwasmpool state export (backport #6666) (#6683)
Browse files Browse the repository at this point in the history
* 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 d4582d4)

# Conflicts:
#	CHANGELOG.md
#	x/cosmwasmpool/genesis.go
#	x/cosmwasmpool/genesis_test.go

* fix merge conflicts

---------

Co-authored-by: Adam Tucker <adam@osmosis.team>
Co-authored-by: Adam Tucker <adamleetucker@outlook.com>
  • Loading branch information
3 people authored Oct 12, 2023
1 parent a08ff69 commit 73be89f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 39 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ 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
* [#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

## v19.2.0

Expand Down
43 changes: 17 additions & 26 deletions x/cosmwasmpool/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,25 @@ func (k *Keeper) InitGenesis(ctx sdk.Context, gen *types.GenesisState, unpacker
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
params := k.GetParams(ctx)

// TODO: We remove this as there is an issue with resolving the
// type url for the cosmwasm pools.
// panic: unable to resolve type URL /
//pools, err := k.GetPools(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)
//}
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)
}

return &types.GenesisState{
Params: params,
// TODO: This is likely because amino is being used directly
// (instead of codec.LegacyAmino which is preferred) or
// UnpackInterfacesMessage is not defined for some type which
// contains a protobuf Any either directly or via one of its members.
// To see a stacktrace of where the error is coming from,
// set the var Debug = true in codec/types/compat.go
// Pools: poolAnys,
Pools: poolAnys,
}
}
27 changes: 15 additions & 12 deletions x/cosmwasmpool/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,21 @@ func (s *PoolModuleSuite) TestInitGenesis() {
s.Require().Equal(expectedTotalLiquidity.String(), liquidity.String())
}

// TODO: Fix this test when fixing genesis export functionality
//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)
//}
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())
}
}

func (s *PoolModuleSuite) TestMarshalUnmarshalGenesis() {
s.Setup()
Expand Down
2 changes: 1 addition & 1 deletion x/cosmwasmpool/model/store_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions x/cosmwasmpool/pool_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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