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: implement fswap module's genesis and params #1339

Merged
merged 27 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c5d92f9
feat: implement genesis related functions
170210 Apr 18, 2024
7063c9b
chore: use mockgen
170210 Apr 19, 2024
27b9b2a
chore: add tests
170210 Apr 19, 2024
3afadd4
fix: add subspace in paramsKeeper
170210 Apr 19, 2024
31d3829
fix: fix lint
170210 Apr 19, 2024
f63ce4d
fix: fix for test errors
170210 Apr 19, 2024
6d59aa8
fix: fix lint
170210 Apr 19, 2024
96da1de
fix: fix appmodulebasic
170210 Apr 19, 2024
065f4f2
chore: update CHANGLOG.md
170210 Apr 19, 2024
0423ac7
fix: use configuration instead of params
170210 Apr 23, 2024
8fcbab8
chore: refactor new&default fswapped
170210 Apr 23, 2024
c91259f
fix: add total_supply into genesisStatus & remove calculate logic
170210 Apr 23, 2024
d6f2ce9
fix: fix lint
170210 Apr 23, 2024
d0f4841
fix: fix module_simulation
170210 Apr 23, 2024
3cd7acb
fix: set swappableNewCoinDenom as params
170210 Apr 24, 2024
c2a9b61
fix: change total_supply name to swappableNewCoinAmount
170210 Apr 24, 2024
f0be4eb
fix: fix lint
170210 Apr 24, 2024
e42e064
fix: use io/collection to generate key
170210 Apr 24, 2024
09562a4
Revert "fix: use io/collection to generate key"
170210 Apr 24, 2024
688d489
fix: change sdk.Coin to sdk.Int
170210 Apr 24, 2024
a9ca68f
fix: fix lint
170210 Apr 24, 2024
722e95b
fix: remote unnecessary codes
170210 Apr 24, 2024
160f953
fix: add validate for swappable new coin
170210 Apr 24, 2024
eff7746
fix: add randomizedGenState
170210 Apr 24, 2024
bd076f9
fix: fix error doc
170210 Apr 24, 2024
61f09fa
fix: fix lint
170210 Apr 24, 2024
9d583c3
fix: change proto message fields' name
170210 Apr 24, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Features
* (consensus) [\#1178](https://github.com/Finschia/finschia-sdk/pull/1178) change the consensus from Ostracon to Tendermint v0.34.24
* (x/fswap) [\#1336](https://github.com/Finschia/finschia-sdk/pull/1336) Initialize fswap module
* (x/fswap) [\#1339](https://github.com/Finschia/finschia-sdk/pull/1339) Implement fswap module's genesis

### Improvements
* (docs) [\#1120](https://github.com/Finschia/finschia-sdk/pull/1120) Update links in x/foundation README.md
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ mocks: $(MOCKS_DIR)
mockgen -package mocks -destination tests/mocks/grpc_server.go github.com/gogo/protobuf/grpc Server
mockgen -package mocks -destination tests/mocks/tendermint_tendermint_libs_log_DB.go github.com/tendermint/tendermint/libs/log Logger
mockgen -source=x/stakingplus/expected_keepers.go -package testutil -destination x/stakingplus/testutil/expected_keepers_mocks.go
mockgen -source=x/fswap/types/expected_keepers.go -package testutil -destination x/fswap/testutil/expected_keepers_mocks.go
.PHONY: mocks

$(MOCKS_DIR):
Expand Down
2 changes: 1 addition & 1 deletion docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12791,7 +12791,7 @@ Params defines the parameters for the module.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `new_coin_denom` | [string](#string) | | new denomination for new coin after swap |
| `swappable_new_coin_amount` | [cosmos.base.v1beta1.Coin](#cosmos.base.v1beta1.Coin) | | |



Expand Down
2 changes: 1 addition & 1 deletion proto/lbm/fswap/v1/fswap.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types";
import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";

message Swapped{
message Swapped {
cosmos.base.v1beta1.Coin old_coin_amount = 1
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"];
cosmos.base.v1beta1.Coin new_coin_amount = 2
Expand Down
6 changes: 3 additions & 3 deletions proto/lbm/fswap/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ package lbm.fswap.v1;
option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types";

import "gogoproto/gogo.proto";
import "lbm/fswap/v1/params.proto";
import "lbm/fswap/v1/fswap.proto";
import "lbm/fswap/v1/params.proto";

// GenesisState defines the fswap module's genesis state.
message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
Swapped swapped =2 [(gogoproto.nullable) = false];
Params params = 1 [(gogoproto.nullable) = false];
Swapped swapped = 2 [(gogoproto.nullable) = false];
}
7 changes: 4 additions & 3 deletions proto/lbm/fswap/v1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ package lbm.fswap.v1;
option go_package = "github.com/Finschia/finschia-sdk/x/fswap/types";

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";

// Params defines the parameters for the module.
message Params {
option (gogoproto.goproto_stringer) = false;
// new denomination for new coin after swap
string new_coin_denom = 1;
}
cosmos.base.v1beta1.Coin swappable_new_coin_amount = 1
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/Finschia/finschia-sdk/types.Coin"];
}
14 changes: 14 additions & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ import (
foundationclient "github.com/Finschia/finschia-sdk/x/foundation/client"
foundationkeeper "github.com/Finschia/finschia-sdk/x/foundation/keeper"
foundationmodule "github.com/Finschia/finschia-sdk/x/foundation/module"
"github.com/Finschia/finschia-sdk/x/fswap"
fswapkeeper "github.com/Finschia/finschia-sdk/x/fswap/keeper"
fswaptypes "github.com/Finschia/finschia-sdk/x/fswap/types"
"github.com/Finschia/finschia-sdk/x/genutil"
genutiltypes "github.com/Finschia/finschia-sdk/x/genutil/types"
"github.com/Finschia/finschia-sdk/x/gov"
Expand Down Expand Up @@ -138,6 +141,7 @@ var (
vesting.AppModuleBasic{},
tokenmodule.AppModuleBasic{},
collectionmodule.AppModuleBasic{},
fswap.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -197,6 +201,7 @@ type SimApp struct {
ClassKeeper classkeeper.Keeper
TokenKeeper tokenkeeper.Keeper
CollectionKeeper collectionkeeper.Keeper
FswapKeeper fswapkeeper.Keeper

// the module manager
mm *module.Manager
Expand Down Expand Up @@ -250,6 +255,7 @@ func NewSimApp(
token.StoreKey,
collection.StoreKey,
authzkeeper.StoreKey,
fswaptypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
// NOTE: The testingkey is just mounted for testing purposes. Actual applications should
Expand Down Expand Up @@ -352,6 +358,10 @@ func NewSimApp(
// If evidence needs to be handled for the app, set routes in router here and seal
app.EvidenceKeeper = *evidenceKeeper

/**** Phase 1 ****/
fswapConfig := fswaptypes.DefaultConfig()
app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], app.AccountKeeper, app.BankKeeper, fswapConfig)

/**** Module Options ****/

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
Expand Down Expand Up @@ -383,6 +393,7 @@ func NewSimApp(
tokenmodule.NewAppModule(appCodec, app.TokenKeeper),
collectionmodule.NewAppModule(appCodec, app.CollectionKeeper),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
fswap.NewAppModule(appCodec, app.FswapKeeper, app.AccountKeeper, app.BankKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -410,6 +421,7 @@ func NewSimApp(
vestingtypes.ModuleName,
token.ModuleName,
collection.ModuleName,
fswaptypes.ModuleName,
)
app.mm.SetOrderEndBlockers(
crisistypes.ModuleName,
Expand All @@ -431,6 +443,7 @@ func NewSimApp(
foundation.ModuleName,
token.ModuleName,
collection.ModuleName,
fswaptypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -458,6 +471,7 @@ func NewSimApp(
vestingtypes.ModuleName,
token.ModuleName,
collection.ModuleName,
fswaptypes.ModuleName,
)

// Uncomment if you want to set a custom migration order here.
Expand Down
4 changes: 3 additions & 1 deletion x/ERRORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@

|Error Name|Codespace|Code|Description|
|:-|:-|:-|:-|
|ErrSample|fswap|1100|sample error|
|ErrParamsNotFound|fswap|1100|params does not exist|
|ErrSwappedNotFound|fswap|1101|swapped does not exist|
|ErrTotalSupplyNotFound|fswap|1102|swappable new coin amount does not exist|

>You can also find detailed information in the following Errors.go files:
* [fswap/types/errors.go](fswap/types/errors.go)
Expand Down
24 changes: 0 additions & 24 deletions x/fswap/genesis.go

This file was deleted.

1 change: 0 additions & 1 deletion x/fswap/genesis_test.go

This file was deleted.

51 changes: 51 additions & 0 deletions x/fswap/keeper/fswap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package keeper

import (
sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/x/fswap/types"
)

// GetSwapped get all parameters as types.Swapped
func (k Keeper) GetSwapped(ctx sdk.Context) types.Swapped {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte{types.SwappedKey})
var swapped types.Swapped

if bz == nil {
panic(types.ErrSwappedNotFound)

Check warning on line 15 in x/fswap/keeper/fswap.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/fswap.go#L15

Added line #L15 was not covered by tests
}
k.cdc.MustUnmarshal(bz, &swapped)
return swapped
}

// SetSwapped set the types.Swapped
func (k Keeper) SetSwapped(ctx sdk.Context, swapped types.Swapped) error {
store := ctx.KVStore(k.storeKey)
bz, err := k.cdc.Marshal(&swapped)
if err != nil {
return err

Check warning on line 26 in x/fswap/keeper/fswap.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/fswap.go#L26

Added line #L26 was not covered by tests
}
store.Set([]byte{types.SwappedKey}, bz)
return nil
}

func (k Keeper) GetTotalSupply(ctx sdk.Context) sdk.Coin {
store := ctx.KVStore(k.storeKey)
170210 marked this conversation as resolved.
Show resolved Hide resolved
bz := store.Get([]byte{types.SwappableNewCoinAmountKey})
var totalSupply sdk.Coin
if bz == nil {
panic(types.ErrTotalSupplyNotFound)

Check warning on line 37 in x/fswap/keeper/fswap.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/fswap.go#L32-L37

Added lines #L32 - L37 were not covered by tests
}
k.cdc.MustUnmarshal(bz, &totalSupply)
return totalSupply

Check warning on line 40 in x/fswap/keeper/fswap.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/fswap.go#L39-L40

Added lines #L39 - L40 were not covered by tests
}

func (k Keeper) SetTotalSupply(ctx sdk.Context, totalSupply sdk.Coin) error {
store := ctx.KVStore(k.storeKey)
bz, err := k.cdc.Marshal(&totalSupply)
if err != nil {
return err

Check warning on line 47 in x/fswap/keeper/fswap.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/fswap.go#L43-L47

Added lines #L43 - L47 were not covered by tests
}
store.Set([]byte{types.SwappableNewCoinAmountKey}, bz)
return nil

Check warning on line 50 in x/fswap/keeper/fswap.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/fswap.go#L49-L50

Added lines #L49 - L50 were not covered by tests
}
25 changes: 25 additions & 0 deletions x/fswap/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package keeper

import (
sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/x/fswap/types"
)

// InitGenesis initializes the module's state from a provided genesis
// state.
func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
if err := k.SetParams(ctx, genState.Params); err != nil {
panic(err)

Check warning on line 12 in x/fswap/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/genesis.go#L12

Added line #L12 was not covered by tests
}
if err := k.SetSwapped(ctx, genState.Swapped); err != nil {
panic(err)

Check warning on line 15 in x/fswap/keeper/genesis.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/genesis.go#L15

Added line #L15 was not covered by tests
}
}

// ExportGenesis returns the capability module's exported genesis.
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
return &types.GenesisState{
Params: k.GetParams(ctx),
Swapped: k.GetSwapped(ctx),
}
}
13 changes: 13 additions & 0 deletions x/fswap/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package keeper_test

import (
"github.com/Finschia/finschia-sdk/x/fswap/types"
)

func (s *KeeperTestSuite) TestInitAndExportGenesis() {
s.keeper.InitGenesis(s.sdkCtx, *types.DefaultGenesis())
got := s.keeper.ExportGenesis(s.sdkCtx)
s.Require().NotNil(got)
s.Require().Equal(types.DefaultParams(), got.Params)
s.Require().Equal(types.DefaultSwapped(), got.Swapped)
}
9 changes: 6 additions & 3 deletions x/fswap/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@ import (
type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
storeKey storetypes.StoreKey
config types.Config
}
)

func NewKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
ak types.AccountKeeper,
bk types.BankKeeper,
storeKey storetypes.StoreKey,
config types.Config,
) Keeper {
return Keeper{
cdc: cdc,
storeKey: storeKey,
accountKeeper: ak,
bankKeeper: bk,
storeKey: storeKey,
config: config,
}
}

Expand Down
43 changes: 43 additions & 0 deletions x/fswap/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package keeper_test

import (
"context"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/Finschia/finschia-sdk/simapp"
sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/x/fswap/keeper"
"github.com/Finschia/finschia-sdk/x/fswap/testutil"
"github.com/Finschia/finschia-sdk/x/fswap/types"
)

type KeeperTestSuite struct {
suite.Suite
sdkCtx sdk.Context
goCtx context.Context
keeper keeper.Keeper
bankKeeper types.BankKeeper

msgServer types.MsgServer
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}

func (s *KeeperTestSuite) SetupTest() {
ctrl := gomock.NewController(s.T())
bankKeeper := testutil.NewMockBankKeeper(ctrl)
s.bankKeeper = bankKeeper
checkTx := false
app := simapp.Setup(checkTx)
s.sdkCtx = app.BaseApp.NewContext(checkTx, tmproto.Header{})
s.goCtx = sdk.WrapSDKContext(s.sdkCtx)
s.keeper = app.FswapKeeper

s.msgServer = keeper.NewMsgServerImpl(s.keeper)
}
24 changes: 21 additions & 3 deletions x/fswap/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,28 @@

// GetParams get all parameters as types.Params
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams()
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte{types.ParamsKey})
var params types.Params
if bz == nil {
panic(types.ErrParamsNotFound)

Check warning on line 14 in x/fswap/keeper/params.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/params.go#L14

Added line #L14 was not covered by tests
}
k.cdc.MustUnmarshal(bz, &params)
return params
}

// SetParams set the params
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
// k.paramstore.SetParamSet(ctx, &params)
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error {
store := ctx.KVStore(k.storeKey)
bz, err := k.cdc.Marshal(&params)
if err != nil {
return err

Check warning on line 25 in x/fswap/keeper/params.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/params.go#L25

Added line #L25 was not covered by tests
}
store.Set([]byte{types.ParamsKey}, bz)
return nil
}

func (k Keeper) SwappableNewCoinAmount(ctx sdk.Context) sdk.Coin {
params := k.GetParams(ctx)
return params.SwappableNewCoinAmount

Check warning on line 33 in x/fswap/keeper/params.go

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/params.go#L31-L33

Added lines #L31 - L33 were not covered by tests
}
1 change: 0 additions & 1 deletion x/fswap/keeper/params_test.go

This file was deleted.

Loading
Loading