Skip to content

Commit

Permalink
Merge pull request #360 from irisnet/dreamer/migration-params
Browse files Browse the repository at this point in the history
refactor: migration x/params to coinswap params
  • Loading branch information
aofengli committed Jun 12, 2023
2 parents 2768516 + 36b022a commit 8ac4d07
Show file tree
Hide file tree
Showing 60 changed files with 2,072 additions and 553 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/cosmos/cosmos-proto v1.0.0-beta.2
github.com/cosmos/cosmos-sdk v0.47.2
github.com/cosmos/gogoproto v1.4.8
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.5.3
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
Expand Down Expand Up @@ -78,6 +77,7 @@ require (
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/mock v1.6.0 // indirect
Expand Down
44 changes: 36 additions & 8 deletions modules/coinswap/client/testutil/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
"time"

"github.com/gogo/protobuf/proto"
"github.com/cosmos/gogoproto/proto"
"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -74,7 +74,11 @@ func (s *IntegrationTestSuite) TestCoinswap() {

fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.network.BondDenom, sdk.NewInt(10))).String()),
fmt.Sprintf(
"--%s=%s",
flags.FlagFees,
sdk.NewCoins(sdk.NewCoin(s.network.BondDenom, sdk.NewInt(10))).String(),
),
}

_ = tokentestutil.IssueTokenExec(s.T(), s.network, clientCtx, from.String(), args...)
Expand Down Expand Up @@ -127,7 +131,11 @@ func (s *IntegrationTestSuite) TestCoinswap() {

reqBz, err := val.ClientCtx.Codec.MarshalJSON(req)
s.Require().NoError(err)
res, err := testutil.PostRequest(fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", baseURL), "application/json", reqBz)
res, err := testutil.PostRequest(
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", baseURL),
"application/json",
reqBz,
)
s.Require().NoError(err)
var result tx.BroadcastTxResponse
err = val.ClientCtx.Codec.UnmarshalJSON(res, &result)
Expand Down Expand Up @@ -193,7 +201,11 @@ func (s *IntegrationTestSuite) TestCoinswap() {

reqBz, err = val.ClientCtx.Codec.MarshalJSON(req)
s.Require().NoError(err)
res, err = testutil.PostRequest(fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", baseURL), "application/json", reqBz)
res, err = testutil.PostRequest(
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", baseURL),
"application/json",
reqBz,
)
s.Require().NoError(err)
err = val.ClientCtx.Codec.UnmarshalJSON(res, &result)
s.Require().NoError(err)
Expand Down Expand Up @@ -257,7 +269,11 @@ func (s *IntegrationTestSuite) TestCoinswap() {

reqBz, err = val.ClientCtx.Codec.MarshalJSON(req)
s.Require().NoError(err)
_, err = testutil.PostRequest(fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", baseURL), "application/json", reqBz)
_, err = testutil.PostRequest(
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", baseURL),
"application/json",
reqBz,
)
s.Require().NoError(err)
err = val.ClientCtx.Codec.UnmarshalJSON(res, &result)
s.Require().NoError(err)
Expand Down Expand Up @@ -321,7 +337,11 @@ func (s *IntegrationTestSuite) TestCoinswap() {

reqBz, err = val.ClientCtx.Codec.MarshalJSON(req)
s.Require().NoError(err)
_, err = testutil.PostRequest(fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", baseURL), "application/json", reqBz)
_, err = testutil.PostRequest(
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", baseURL),
"application/json",
reqBz,
)
s.Require().NoError(err)
err = val.ClientCtx.Codec.UnmarshalJSON(res, &result)
s.Require().NoError(err)
Expand Down Expand Up @@ -380,7 +400,11 @@ func (s *IntegrationTestSuite) TestCoinswap() {

reqBz, err = val.ClientCtx.Codec.MarshalJSON(req)
s.Require().NoError(err)
_, err = testutil.PostRequest(fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", baseURL), "application/json", reqBz)
_, err = testutil.PostRequest(
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", baseURL),
"application/json",
reqBz,
)
s.Require().NoError(err)
err = val.ClientCtx.Codec.UnmarshalJSON(res, &result)
s.Require().NoError(err)
Expand Down Expand Up @@ -439,7 +463,11 @@ func (s *IntegrationTestSuite) TestCoinswap() {

reqBz, err = val.ClientCtx.Codec.MarshalJSON(req)
s.Require().NoError(err)
_, err = testutil.PostRequest(fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", baseURL), "application/json", reqBz)
_, err = testutil.PostRequest(
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs", baseURL),
"application/json",
reqBz,
)
s.Require().NoError(err)
err = val.ClientCtx.Codec.UnmarshalJSON(res, &result)
s.Require().NoError(err)
Expand Down
23 changes: 23 additions & 0 deletions modules/coinswap/exported/exported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package exported

import (
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

var (
NewParamSetPair = paramtypes.NewParamSetPair
)

type (
ParamSet = paramtypes.ParamSet
ParamSetPairs = paramtypes.ParamSetPairs

// Subspace defines an interface that implements the legacy x/params Subspace
// type.
//
// NOTE: This is used solely for migration of x/params managed parameters.
Subspace interface {
GetParamSet(ctx sdk.Context, ps ParamSet)
}
)
35 changes: 28 additions & 7 deletions modules/coinswap/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,22 @@ import (
var _ types.QueryServer = Keeper{}

// LiquidityPool returns the liquidity pool information of the denom
func (k Keeper) LiquidityPool(c context.Context, req *types.QueryLiquidityPoolRequest) (*types.QueryLiquidityPoolResponse, error) {
func (k Keeper) LiquidityPool(
c context.Context,
req *types.QueryLiquidityPoolRequest,
) (*types.QueryLiquidityPoolResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)
pool, exists := k.GetPoolByLptDenom(ctx, req.LptDenom)
if !exists {
return nil, sdkerrors.Wrapf(types.ErrReservePoolNotExists, "liquidity pool token: %s", req.LptDenom)
return nil, sdkerrors.Wrapf(
types.ErrReservePoolNotExists,
"liquidity pool token: %s",
req.LptDenom,
)
}

balances, err := k.GetPoolBalancesByLptDenom(ctx, pool.LptDenom)
Expand All @@ -51,7 +58,10 @@ func (k Keeper) LiquidityPool(c context.Context, req *types.QueryLiquidityPoolRe
return &res, nil
}

func (k Keeper) LiquidityPools(c context.Context, req *types.QueryLiquidityPoolsRequest) (*types.QueryLiquidityPoolsResponse, error) {
func (k Keeper) LiquidityPools(
c context.Context,
req *types.QueryLiquidityPoolsRequest,
) (*types.QueryLiquidityPoolsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
Expand All @@ -62,7 +72,7 @@ func (k Keeper) LiquidityPools(c context.Context, req *types.QueryLiquidityPools

store := ctx.KVStore(k.storeKey)
nftStore := prefix.NewStore(store, []byte(types.KeyPool))
pageRes, err := query.Paginate(nftStore, req.Pagination, func(key []byte, value []byte) error {
pageRes, err := query.Paginate(nftStore, req.Pagination, func(_ []byte, value []byte) error {
var pool types.Pool
k.cdc.MustUnmarshal(value, &pool)

Expand All @@ -75,9 +85,12 @@ func (k Keeper) LiquidityPools(c context.Context, req *types.QueryLiquidityPools
Id: pool.Id,
EscrowAddress: pool.EscrowAddress,
Standard: sdk.NewCoin(pool.StandardDenom, balances.AmountOf(pool.StandardDenom)),
Token: sdk.NewCoin(pool.CounterpartyDenom, balances.AmountOf(pool.CounterpartyDenom)),
Lpt: k.bk.GetSupply(ctx, pool.LptDenom),
Fee: params.Fee.String(),
Token: sdk.NewCoin(
pool.CounterpartyDenom,
balances.AmountOf(pool.CounterpartyDenom),
),
Lpt: k.bk.GetSupply(ctx, pool.LptDenom),
Fee: params.Fee.String(),
})
return nil
})
Expand All @@ -89,3 +102,11 @@ func (k Keeper) LiquidityPools(c context.Context, req *types.QueryLiquidityPools
Pools: pools,
}, nil
}

func (k Keeper) Params(
c context.Context,
req *types.QueryParamsRequest,
) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
return &types.QueryParamsResponse{Params: k.GetParams(ctx)}, nil
}
43 changes: 3 additions & 40 deletions modules/coinswap/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import (

sdkmath "cosmossdk.io/math"
"github.com/cometbft/cometbft/libs/log"
gogotypes "github.com/gogo/protobuf/types"

"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"

"github.com/irisnet/irismod/modules/coinswap/types"
)
Expand All @@ -24,8 +22,8 @@ type Keeper struct {
storeKey storetypes.StoreKey
bk types.BankKeeper
ak types.AccountKeeper
paramSpace paramstypes.Subspace
feeCollectorName string
authority string
blockedAddrs map[string]bool
}

Expand All @@ -36,30 +34,25 @@ type Keeper struct {
func NewKeeper(
cdc codec.BinaryCodec,
key storetypes.StoreKey,
paramSpace paramstypes.Subspace,
bk types.BankKeeper,
ak types.AccountKeeper,
blockedAddrs map[string]bool,
feeCollectorName string,
authority string,
) Keeper {
// ensure coinswap module account is set
if addr := ak.GetModuleAddress(types.ModuleName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}

// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}

return Keeper{
storeKey: key,
bk: bk,
ak: ak,
cdc: cdc,
paramSpace: paramSpace,
blockedAddrs: blockedAddrs,
feeCollectorName: feeCollectorName,
authority: authority,
}
}

Expand Down Expand Up @@ -659,33 +652,3 @@ func (k Keeper) removeUnilateralLiquidity(ctx sdk.Context,

return coins, k.bk.SendCoins(ctx, poolAddr, sender, coins)
}

// GetParams gets the parameters for the coinswap module.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
var swapParams types.Params
k.paramSpace.GetParamSet(ctx, &swapParams)
return swapParams
}

// SetParams sets the parameters for the coinswap module.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}

// SetStandardDenom sets the standard denom for the coinswap module.
func (k Keeper) SetStandardDenom(ctx sdk.Context, denom string) {
store := ctx.KVStore(k.storeKey)
denomWrap := gogotypes.StringValue{Value: denom}
bz := k.cdc.MustMarshal(&denomWrap)
store.Set(types.KeyStandardDenom, bz)
}

// GetStandardDenom returns the standard denom of the coinswap module.
func (k Keeper) GetStandardDenom(ctx sdk.Context) string {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyStandardDenom)

var denomWrap = gogotypes.StringValue{}
k.cdc.MustUnmarshal(bz, &denomWrap)
return denomWrap.Value
}
21 changes: 16 additions & 5 deletions modules/coinswap/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,41 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/irisnet/irismod/modules/coinswap/exported"
v1 "github.com/irisnet/irismod/modules/coinswap/migrations/v1"
v2 "github.com/irisnet/irismod/modules/coinswap/migrations/v2"
v3 "github.com/irisnet/irismod/modules/coinswap/migrations/v3"
v4 "github.com/irisnet/irismod/modules/coinswap/migrations/v4"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
k Keeper
k Keeper
legacySubspace exported.Subspace
}

// NewMigrator returns a new Migrator.
func NewMigrator(k Keeper) Migrator {
return Migrator{k: k}
func NewMigrator(k Keeper, legacySubspace exported.Subspace) Migrator {
return Migrator{k: k, legacySubspace: legacySubspace}
}

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v1.Migrate(ctx, m.k, m.k.bk, m.k.ak)
}

// Migrate1to2 migrates from version 2 to 3.
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v2.Migrate(ctx, m.k, m.k.paramSpace)
return v2.Migrate(ctx, m.k, m.legacySubspace)
}

// Migrate1to2 migrates from version 3 to 4.
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
return v3.Migrate(ctx, m.k, m.k.paramSpace)
return v3.Migrate(ctx, m.k, m.legacySubspace)
}

// Migrate1to2 migrates from version 4 to 5.
func (m Migrator) Migrate4to5(ctx sdk.Context) error {
return v4.Migrate(ctx, m.k, m.legacySubspace)
}
Loading

0 comments on commit 8ac4d07

Please sign in to comment.