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 9 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 genesis and params
170210 marked this conversation as resolved.
Show resolved Hide resolved

### 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
15 changes: 15 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,9 @@ func NewSimApp(
// If evidence needs to be handled for the app, set routes in router here and seal
app.EvidenceKeeper = *evidenceKeeper

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

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

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
Expand Down Expand Up @@ -383,6 +392,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 +420,7 @@ func NewSimApp(
vestingtypes.ModuleName,
token.ModuleName,
collection.ModuleName,
fswaptypes.ModuleName,
)
app.mm.SetOrderEndBlockers(
crisistypes.ModuleName,
Expand All @@ -431,6 +442,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 +470,7 @@ func NewSimApp(
vestingtypes.ModuleName,
token.ModuleName,
collection.ModuleName,
fswaptypes.ModuleName,
)

// Uncomment if you want to set a custom migration order here.
Expand Down Expand Up @@ -723,5 +736,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(crisistypes.ModuleName)
paramsKeeper.Subspace(foundation.ModuleName)

paramsKeeper.Subspace(fswaptypes.ModuleName)

return paramsKeeper
}
3 changes: 2 additions & 1 deletion x/ERRORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@

|Error Name|Codespace|Code|Description|
|:-|:-|:-|:-|
|ErrSample|fswap|1100|sample error|
|ErrParamsNotFound|fswap|1100|params does not exist|
|ErrSwappedNotFound|fswap|1101|swapped 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.DecCoin {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte{types.TotalSupplyKey})
var totalSupply sdk.DecCoin
if bz == nil {
panic(types.ErrParamsNotFound)

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#L37

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

func (k Keeper) SetTotalSupply(ctx sdk.Context, totalSupply sdk.DecCoin) 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#L47

Added line #L47 was not covered by tests
}
store.Set([]byte{types.TotalSupplyKey}, bz)
return nil
}
31 changes: 31 additions & 0 deletions x/fswap/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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, bk types.BankKeeper, 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
}
totalOldCoinsSupply := bk.GetSupply(ctx, types.DefaultOldCoins).Amount
totalNewCoinsSupply := types.DefaultSwapRate.MulInt(totalOldCoinsSupply)
totalNewCoins := sdk.NewDecCoinFromDec(genState.Params.NewCoinDenom, totalNewCoinsSupply)
if err := k.SetTotalSupply(ctx, totalNewCoins); err != nil {
0Tech marked this conversation as resolved.
Show resolved Hide resolved
panic(err)

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

View check run for this annotation

Codecov / codecov/patch

x/fswap/keeper/genesis.go#L21

Added line #L21 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),
}
}
20 changes: 20 additions & 0 deletions x/fswap/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package keeper_test

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

func (s *KeeperTestSuite) TestInitAndExportGenesis() {
s.keeper.InitGenesis(s.sdkCtx, s.bankKeeper, *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)

gotTotalSupply := s.keeper.GetTotalSupply(s.sdkCtx)
totalNewCoinsSupply := types.DefaultSwapRate.MulInt(testutil.MockOldCoin.Amount)
expectedCoin := sdk.NewDecCoinFromDec(types.DefaultNewCoinDenom, totalNewCoinsSupply)
s.Require().Equal(expectedCoin, gotTotalSupply)
}
6 changes: 3 additions & 3 deletions x/fswap/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ import (
type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
storeKey storetypes.StoreKey
}
)

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

Expand Down
44 changes: 44 additions & 0 deletions x/fswap/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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)
bankKeeper.EXPECT().GetSupply(gomock.Any(), "cony").Return(testutil.MockOldCoin)
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) NewCoinDenom(ctx sdk.Context) string {
params := k.GetParams(ctx)
return params.NewCoinDenom

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
}
9 changes: 5 additions & 4 deletions x/fswap/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.EndBlockAppModule = AppModule{}
)

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -144,14 +145,14 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.Ra
// Initialize global index to index in genesis state
cdc.MustUnmarshalJSON(gs, &genState)

InitGenesis(ctx, am.keeper, genState)
am.keeper.InitGenesis(ctx, am.bankKeeper, genState)

return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the capability module's exported genesis state as raw JSON bytes.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
genState := ExportGenesis(ctx, am.keeper)
genState := am.keeper.ExportGenesis(ctx)
return cdc.MustMarshalJSON(genState)
}

Expand Down
Loading
Loading