From 8abde1de7584c652026d345f2cff70486aa2f2d4 Mon Sep 17 00:00:00 2001 From: 170210 Date: Fri, 10 May 2024 14:11:33 +0900 Subject: [PATCH 01/14] chore: add logic validation & tests Signed-off-by: 170210 --- Makefile | 2 +- x/fswap/keeper/expected_keepers.go | 1 + x/fswap/keeper/keeper.go | 8 +++ x/fswap/keeper/keeper_test.go | 66 +++++++++++++++------- x/fswap/testutil/expected_keepers_mocks.go | 39 ++++++++----- 5 files changed, 81 insertions(+), 35 deletions(-) diff --git a/Makefile b/Makefile index 8504265a2a..1245af80fe 100644 --- a/Makefile +++ b/Makefile @@ -144,7 +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 + mockgen -source=x/fswap/keeper/expected_keepers.go -package testutil -destination x/fswap/testutil/expected_keepers_mocks.go mockgen -source=x/fbridge/types/expected_keepers.go -package testutil -destination x/fbridge/testutil/expected_keepers_mocks.go .PHONY: mocks diff --git a/x/fswap/keeper/expected_keepers.go b/x/fswap/keeper/expected_keepers.go index 08934e1c3c..908ca160ac 100644 --- a/x/fswap/keeper/expected_keepers.go +++ b/x/fswap/keeper/expected_keepers.go @@ -13,6 +13,7 @@ type ( IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error GetDenomMetaData(ctx sdk.Context, denom string) (banktypes.Metadata, bool) SetDenomMetaData(ctx sdk.Context, denomMetaData banktypes.Metadata) + HasSupply(ctx sdk.Context, denom string) bool MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error } diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 6b153150a1..6257d3dfe2 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -125,6 +125,14 @@ func (k Keeper) SetSwap(ctx sdk.Context, swap types.Swap, toDenomMetadata bank.M return types.ErrCanNotHaveMoreSwap.Wrapf("cannot make more swaps, max swaps is %d", k.config.MaxSwaps) } + if swap.ToDenom != toDenomMetadata.Base { + return sdkerrors.ErrInvalidRequest.Wrap("toDenom should be existed in metadata") + } + + if !k.HasSupply(ctx, swap.FromDenom) { + return sdkerrors.ErrInvalidRequest.Wrap("fromDenom should be existed in chain") + } + if isNewSwap { swapped := types.Swapped{ FromCoinAmount: sdk.Coin{ diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index 63774bef84..ee14860b15 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -4,9 +4,6 @@ import ( "context" "testing" - "github.com/stretchr/testify/suite" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/Finschia/finschia-sdk/crypto/keys/secp256k1" "github.com/Finschia/finschia-sdk/simapp" "github.com/Finschia/finschia-sdk/testutil/testdata" @@ -14,8 +11,12 @@ import ( sdkerrors "github.com/Finschia/finschia-sdk/types/errors" bank "github.com/Finschia/finschia-sdk/x/bank/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" minttypes "github.com/Finschia/finschia-sdk/x/mint/types" + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/suite" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" ) type KeeperTestSuite struct { @@ -82,8 +83,8 @@ func (s *KeeperTestSuite) SetupTest() { DenomUnits: []*bank.DenomUnit{ {Denom: s.swap.ToDenom, Exponent: 0}, }, - Base: "dummy", - Display: "dummycoin", + Base: "todenom", + Display: "todenomcoin", Name: "DUMMY", Symbol: "DUM", } @@ -189,27 +190,56 @@ func (s *KeeperTestSuite) TestSwap() { } func (s *KeeperTestSuite) TestSetSwap() { + ctrl := gomock.NewController(s.T()) + defer ctrl.Finish() + + bankKeeper := testutil.NewMockBankKeeper(ctrl) + bankKeeper.EXPECT().HasSupply(gomock.Any(), "fromdenom").Return(true).AnyTimes() + bankKeeper.EXPECT().HasSupply(gomock.Any(), gomock.Any()).Return(false).AnyTimes() + bankKeeper.EXPECT().GetDenomMetaData(gomock.Any(), "todenom").Return(bank.Metadata{}, false).AnyTimes() + bankKeeper.EXPECT().GetDenomMetaData(gomock.Any(), gomock.Any()).Return(s.toDenomMetadata, true).AnyTimes() + bankKeeper.EXPECT().SetDenomMetaData(gomock.Any(), s.toDenomMetadata).Times(1) + s.keeper.BankKeeper = bankKeeper + testCases := map[string]struct { - swap types.Swap - toDenomMeta bank.Metadata - existingMetadata bool - expectedError error + swap types.Swap + toDenomMeta bank.Metadata + expectedError error }{ "valid": { types.Swap{ - FromDenom: "fromD", - ToDenom: "toD", + FromDenom: "fromdenom", + ToDenom: "todenom", AmountCapForToDenom: sdk.OneInt(), SwapRate: sdk.OneDec(), }, s.toDenomMetadata, - false, nil, }, + "from-denom does not exist": { + types.Swap{ + FromDenom: "fakedenom", + ToDenom: "todenom", + AmountCapForToDenom: sdk.OneInt(), + SwapRate: sdk.OneDec(), + }, + s.toDenomMetadata, + sdkerrors.ErrInvalidRequest, + }, + "to-denom does not equal with metadata": { + types.Swap{ + FromDenom: "fromdenom", + ToDenom: "fakedenom", + AmountCapForToDenom: sdk.OneInt(), + SwapRate: sdk.OneDec(), + }, + s.toDenomMetadata, + sdkerrors.ErrInvalidRequest, + }, "to-denom metadata change not allowed": { types.Swap{ - FromDenom: "fromD", - ToDenom: "toD", + FromDenom: "fromdenom", + ToDenom: "change", AmountCapForToDenom: sdk.OneInt(), SwapRate: sdk.OneDec(), }, @@ -221,7 +251,6 @@ func (s *KeeperTestSuite) TestSetSwap() { Name: s.toDenomMetadata.Name, Symbol: s.toDenomMetadata.Symbol, }, - true, sdkerrors.ErrInvalidRequest, }, } @@ -229,12 +258,7 @@ func (s *KeeperTestSuite) TestSetSwap() { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() err := s.keeper.SetSwap(ctx, tc.swap, s.toDenomMetadata) - if tc.existingMetadata { - err := s.keeper.SetSwap(ctx, tc.swap, s.toDenomMetadata) - s.Require().ErrorIs(err, tc.expectedError) - } else { - s.Require().ErrorIs(err, tc.expectedError) - } + s.Require().ErrorIs(err, tc.expectedError) }) } } diff --git a/x/fswap/testutil/expected_keepers_mocks.go b/x/fswap/testutil/expected_keepers_mocks.go index 8718d93937..de503f6f08 100644 --- a/x/fswap/testutil/expected_keepers_mocks.go +++ b/x/fswap/testutil/expected_keepers_mocks.go @@ -7,9 +7,8 @@ package testutil import ( reflect "reflect" - types "github.com/Finschia/finschia-sdk/snapshots/types" - types0 "github.com/Finschia/finschia-sdk/types" - types1 "github.com/Finschia/finschia-sdk/x/bank/types" + types "github.com/Finschia/finschia-sdk/types" + types0 "github.com/Finschia/finschia-sdk/x/bank/types" gomock "github.com/golang/mock/gomock" ) @@ -37,7 +36,7 @@ func (m *MockBankKeeper) EXPECT() *MockBankKeeperMockRecorder { } // BurnCoins mocks base method. -func (m *MockBankKeeper) BurnCoins(ctx types0.Context, moduleName string, amt types0.Coins) error { +func (m *MockBankKeeper) BurnCoins(ctx types.Context, moduleName string, amt types.Coins) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "BurnCoins", ctx, moduleName, amt) ret0, _ := ret[0].(error) @@ -51,10 +50,10 @@ func (mr *MockBankKeeperMockRecorder) BurnCoins(ctx, moduleName, amt interface{} } // GetBalance mocks base method. -func (m *MockBankKeeper) GetBalance(ctx types0.Context, addr types0.AccAddress, denom string) types0.Coin { +func (m *MockBankKeeper) GetBalance(ctx types.Context, addr types.AccAddress, denom string) types.Coin { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetBalance", ctx, addr, denom) - ret0, _ := ret[0].(types0.Coin) + ret0, _ := ret[0].(types.Coin) return ret0 } @@ -65,10 +64,10 @@ func (mr *MockBankKeeperMockRecorder) GetBalance(ctx, addr, denom interface{}) * } // GetDenomMetaData mocks base method. -func (m *MockBankKeeper) GetDenomMetaData(ctx types0.Context, denom string) (types.Metadata, bool) { +func (m *MockBankKeeper) GetDenomMetaData(ctx types.Context, denom string) (types0.Metadata, bool) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetDenomMetaData", ctx, denom) - ret0, _ := ret[0].(types.Metadata) + ret0, _ := ret[0].(types0.Metadata) ret1, _ := ret[1].(bool) return ret0, ret1 } @@ -79,8 +78,22 @@ func (mr *MockBankKeeperMockRecorder) GetDenomMetaData(ctx, denom interface{}) * return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDenomMetaData", reflect.TypeOf((*MockBankKeeper)(nil).GetDenomMetaData), ctx, denom) } +// HasSupply mocks base method. +func (m *MockBankKeeper) HasSupply(ctx types.Context, denom string) bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "HasSupply", ctx, denom) + ret0, _ := ret[0].(bool) + return ret0 +} + +// HasSupply indicates an expected call of HasSupply. +func (mr *MockBankKeeperMockRecorder) HasSupply(ctx, denom interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HasSupply", reflect.TypeOf((*MockBankKeeper)(nil).HasSupply), ctx, denom) +} + // IsSendEnabledCoins mocks base method. -func (m *MockBankKeeper) IsSendEnabledCoins(ctx types0.Context, coins ...types0.Coin) error { +func (m *MockBankKeeper) IsSendEnabledCoins(ctx types.Context, coins ...types.Coin) error { m.ctrl.T.Helper() varargs := []interface{}{ctx} for _, a := range coins { @@ -99,7 +112,7 @@ func (mr *MockBankKeeperMockRecorder) IsSendEnabledCoins(ctx interface{}, coins } // MintCoins mocks base method. -func (m *MockBankKeeper) MintCoins(ctx types0.Context, moduleName string, amt types0.Coins) error { +func (m *MockBankKeeper) MintCoins(ctx types.Context, moduleName string, amt types.Coins) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "MintCoins", ctx, moduleName, amt) ret0, _ := ret[0].(error) @@ -113,7 +126,7 @@ func (mr *MockBankKeeperMockRecorder) MintCoins(ctx, moduleName, amt interface{} } // SendCoinsFromAccountToModule mocks base method. -func (m *MockBankKeeper) SendCoinsFromAccountToModule(ctx types0.Context, senderAddr types0.AccAddress, recipientModule string, amt types0.Coins) error { +func (m *MockBankKeeper) SendCoinsFromAccountToModule(ctx types.Context, senderAddr types.AccAddress, recipientModule string, amt types.Coins) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SendCoinsFromAccountToModule", ctx, senderAddr, recipientModule, amt) ret0, _ := ret[0].(error) @@ -127,7 +140,7 @@ func (mr *MockBankKeeperMockRecorder) SendCoinsFromAccountToModule(ctx, senderAd } // SendCoinsFromModuleToAccount mocks base method. -func (m *MockBankKeeper) SendCoinsFromModuleToAccount(ctx types0.Context, senderModule string, recipientAddr types0.AccAddress, amt types0.Coins) error { +func (m *MockBankKeeper) SendCoinsFromModuleToAccount(ctx types.Context, senderModule string, recipientAddr types.AccAddress, amt types.Coins) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SendCoinsFromModuleToAccount", ctx, senderModule, recipientAddr, amt) ret0, _ := ret[0].(error) @@ -141,7 +154,7 @@ func (mr *MockBankKeeperMockRecorder) SendCoinsFromModuleToAccount(ctx, senderMo } // SetDenomMetaData mocks base method. -func (m *MockBankKeeper) SetDenomMetaData(ctx types0.Context, denomMetaData types1.Metadata) { +func (m *MockBankKeeper) SetDenomMetaData(ctx types.Context, denomMetaData types0.Metadata) { m.ctrl.T.Helper() m.ctrl.Call(m, "SetDenomMetaData", ctx, denomMetaData) } From 1a4ad851e8e64b1fe2ba632db6d88fa06dc21197 Mon Sep 17 00:00:00 2001 From: 170210 Date: Mon, 13 May 2024 14:25:13 +0900 Subject: [PATCH 02/14] test: add genesisState tests Signed-off-by: 170210 --- x/fswap/keeper/genesis_test.go | 38 ++++++++-- x/fswap/types/genesis.go | 31 +++++--- x/fswap/types/genesis_test.go | 129 ++++++++++++++++++++++++++++++++- 3 files changed, 177 insertions(+), 21 deletions(-) diff --git a/x/fswap/keeper/genesis_test.go b/x/fswap/keeper/genesis_test.go index c4fa1a0e99..1df6f28c75 100644 --- a/x/fswap/keeper/genesis_test.go +++ b/x/fswap/keeper/genesis_test.go @@ -3,19 +3,45 @@ package keeper_test import ( "fmt" + sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/fswap/types" ) func (s *KeeperTestSuite) TestInitAndExportGenesis() { ctx, _ := s.ctx.CacheContext() - defaultGenesis := types.DefaultGenesis() - err := s.keeper.InitGenesis(ctx, defaultGenesis) + testSwapRate, _ := sdk.NewDecFromStr("1234567890") + testGenesis := &types.GenesisState{ + Swaps: []types.Swap{ + { + FromDenom: "aaa", + ToDenom: "bbb", + AmountCapForToDenom: sdk.NewInt(1234567890000), + SwapRate: testSwapRate, + }, + }, + SwapStats: types.SwapStats{ + SwapCount: 1, + }, + Swappeds: []types.Swapped{ + { + FromCoinAmount: sdk.Coin{ + Denom: "aaa", + Amount: sdk.ZeroInt(), + }, + ToCoinAmount: sdk.Coin{ + Denom: "bbb", + Amount: sdk.ZeroInt(), + }, + }, + }, + } + err := s.keeper.InitGenesis(ctx, testGenesis) s.Require().NoError(err) exportGenesis := s.keeper.ExportGenesis(ctx) fmt.Println(len(exportGenesis.GetSwaps())) - s.Require().Equal(defaultGenesis, exportGenesis) - s.Require().Equal(defaultGenesis.GetSwaps(), exportGenesis.GetSwaps()) - s.Require().Equal(defaultGenesis.GetSwapStats(), exportGenesis.GetSwapStats()) - s.Require().Equal(defaultGenesis.GetSwappeds(), exportGenesis.GetSwappeds()) + s.Require().Equal(testGenesis, exportGenesis) + s.Require().Equal(testGenesis.GetSwaps(), exportGenesis.GetSwaps()) + s.Require().Equal(testGenesis.GetSwapStats(), exportGenesis.GetSwapStats()) + s.Require().Equal(testGenesis.GetSwappeds(), exportGenesis.GetSwappeds()) } diff --git a/x/fswap/types/genesis.go b/x/fswap/types/genesis.go index e12f0dfdf8..1ff0d61b89 100644 --- a/x/fswap/types/genesis.go +++ b/x/fswap/types/genesis.go @@ -29,22 +29,10 @@ func AuthorityCandidates() []sdk.AccAddress { // Validate performs basic genesis state validation returning an error upon any failure. func (gs *GenesisState) Validate() error { - for _, swap := range gs.GetSwaps() { - if err := swap.ValidateBasic(); err != nil { - return err - } - } - if err := gs.SwapStats.ValidateBasic(); err != nil { return err } - for _, swapped := range gs.GetSwappeds() { - if err := swapped.ValidateBasic(); err != nil { - return err - } - } - if len(gs.GetSwaps()) != len(gs.GetSwappeds()) { return ErrInvalidState.Wrap("number of swaps does not match number of Swappeds") } @@ -53,5 +41,24 @@ func (gs *GenesisState) Validate() error { return ErrInvalidState.Wrap("number of swaps does not match swap count in SwapStats") } + swappeds := gs.GetSwappeds() + for i, swap := range gs.GetSwaps() { + swapped := swappeds[i] + if err := swap.ValidateBasic(); err != nil { + return err + } + if err := swapped.ValidateBasic(); err != nil { + return err + } + if swap.FromDenom != swapped.FromCoinAmount.Denom { + return ErrInvalidState.Wrap("FromCoin in swap and swapped do not correspond") + } + if swap.ToDenom != swapped.ToCoinAmount.Denom { + return ErrInvalidState.Wrap("ToCoin in swap and swapped do not correspond") + } + if swap.AmountCapForToDenom.LT(swapped.ToCoinAmount.Amount) { + return ErrInvalidState.Wrap("AmountCapForToDenom cannot be exceeded") + } + } return nil } diff --git a/x/fswap/types/genesis_test.go b/x/fswap/types/genesis_test.go index 29308ea519..d07f8b046b 100644 --- a/x/fswap/types/genesis_test.go +++ b/x/fswap/types/genesis_test.go @@ -3,16 +3,42 @@ package types_test import ( "testing" - "github.com/stretchr/testify/require" - + sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/fswap/types" + "github.com/stretchr/testify/require" ) -// todo: add tests func TestGenesisStateValidate(t *testing.T) { + testSwapRate, _ := sdk.NewDecFromStr("1234567890") + exampleGenesis := &types.GenesisState{ + Swaps: []types.Swap{ + { + FromDenom: "aaa", + ToDenom: "bbb", + AmountCapForToDenom: sdk.NewInt(1234567890000), + SwapRate: testSwapRate, + }, + }, + SwapStats: types.SwapStats{ + SwapCount: 1, + }, + Swappeds: []types.Swapped{ + { + FromCoinAmount: sdk.Coin{ + Denom: "aaa", + Amount: sdk.ZeroInt(), + }, + ToCoinAmount: sdk.Coin{ + Denom: "bbb", + Amount: sdk.ZeroInt(), + }, + }, + }, + } for _, tc := range []struct { desc string genState *types.GenesisState + modify func(*types.GenesisState) valid bool }{ { @@ -20,8 +46,105 @@ func TestGenesisStateValidate(t *testing.T) { genState: types.DefaultGenesis(), valid: true, }, + { + desc: "example is valid", + genState: exampleGenesis, + valid: true, + }, + { + desc: "SwapCount is nagative in SwapStats is invalid", + genState: exampleGenesis, + modify: func(gs *types.GenesisState) { + gs.SwapStats.SwapCount = -1 + }, + valid: false, + }, + { + desc: "number of swaps does not match number of Swappeds", + genState: exampleGenesis, + modify: func(gs *types.GenesisState) { + gs.Swaps = append(gs.Swaps, types.Swap{}) + }, + valid: false, + }, + { + desc: "number of swaps does not match number of Swappeds", + genState: exampleGenesis, + modify: func(gs *types.GenesisState) { + gs.Swaps = append(gs.Swaps, types.Swap{}) + gs.Swappeds = append(gs.Swappeds, types.Swapped{}) + }, + valid: false, + }, + { + desc: "fromDenom=toDenom in Swap is invalid", + genState: exampleGenesis, + modify: func(gs *types.GenesisState) { + gs.Swaps[0].ToDenom = "aaa" + }, + valid: false, + }, + { + desc: "AmountCapForToDenom=0 in Swap is invalid", + genState: exampleGenesis, + modify: func(gs *types.GenesisState) { + gs.Swaps[0].AmountCapForToDenom = sdk.ZeroInt() + }, + valid: false, + }, + { + desc: "SwapRate=0 in Swap is invalid", + genState: exampleGenesis, + modify: func(gs *types.GenesisState) { + gs.Swaps[0].SwapRate = sdk.ZeroDec() + }, + valid: false, + }, + { + desc: "FromCoinAmount is nagative in Swappeds is invalid", + genState: exampleGenesis, + modify: func(gs *types.GenesisState) { + gs.Swappeds[0].FromCoinAmount.Amount = sdk.NewInt(-1) + }, + valid: false, + }, + { + desc: "ToCoinAmount is nagative in Swappeds is invalid", + genState: exampleGenesis, + modify: func(gs *types.GenesisState) { + gs.Swappeds[0].ToCoinAmount.Amount = sdk.NewInt(-1) + }, + valid: false, + }, + { + desc: "FromCoin in swap and swapped do not correspond", + genState: exampleGenesis, + modify: func(gs *types.GenesisState) { + gs.Swappeds[0].FromCoinAmount.Denom = "ccc" + }, + valid: false, + }, + { + desc: "ToCoin in swap and swapped do not correspond", + genState: exampleGenesis, + modify: func(gs *types.GenesisState) { + gs.Swappeds[0].ToCoinAmount.Denom = "ccc" + }, + valid: false, + }, + { + desc: "AmountCapForToDenom has been exceeded is invalid", + genState: exampleGenesis, + modify: func(gs *types.GenesisState) { + gs.Swappeds[0].ToCoinAmount.Amount = sdk.NewInt(12345678900000000) + }, + valid: false, + }, } { t.Run(tc.desc, func(t *testing.T) { + if tc.modify != nil { + tc.modify(tc.genState) + } err := tc.genState.Validate() if tc.valid { require.NoError(t, err) From 8e14cdbdcf7317a759e72e745d2075ae68def5ca Mon Sep 17 00:00:00 2001 From: 170210 Date: Mon, 13 May 2024 14:36:41 +0900 Subject: [PATCH 03/14] chore: update CHANGLOG.md Signed-off-by: 170210 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04ff53deb7..e9334fbdbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/fbridge) [\#1366](https://github.com/Finschia/finschia-sdk/pull/1366) Set target denom as module parameters * (x/fbridge) [\#1369](https://github.com/Finschia/finschia-sdk/pull/1369) Add the event of `SetBridgeStatus` * (x/fswap) [\#1372](https://github.com/Finschia/finschia-sdk/pull/1372) support message based proposals +* (x/fswap) [\#1382](https://github.com/Finschia/finschia-sdk/pull/1382) add validation & unit tests in fswap module ### Bug Fixes * chore(deps) [\#1141](https://github.com/Finschia/finschia-sdk/pull/1141) Bump github.com/cosmos/ledger-cosmos-go from 0.12.2 to 0.13.2 to fix ledger signing issue From 484b3627d0d6249feaa0893753448dda491fe7c5 Mon Sep 17 00:00:00 2001 From: 170210 Date: Mon, 13 May 2024 14:43:08 +0900 Subject: [PATCH 04/14] chore: fix lint Signed-off-by: 170210 --- x/fswap/keeper/keeper_test.go | 7 ++++--- x/fswap/types/genesis_test.go | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index ee14860b15..b958d23fc7 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -4,6 +4,10 @@ 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/crypto/keys/secp256k1" "github.com/Finschia/finschia-sdk/simapp" "github.com/Finschia/finschia-sdk/testutil/testdata" @@ -14,9 +18,6 @@ import ( "github.com/Finschia/finschia-sdk/x/fswap/testutil" "github.com/Finschia/finschia-sdk/x/fswap/types" minttypes "github.com/Finschia/finschia-sdk/x/mint/types" - "github.com/golang/mock/gomock" - "github.com/stretchr/testify/suite" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" ) type KeeperTestSuite struct { diff --git a/x/fswap/types/genesis_test.go b/x/fswap/types/genesis_test.go index d07f8b046b..26a66e43bd 100644 --- a/x/fswap/types/genesis_test.go +++ b/x/fswap/types/genesis_test.go @@ -3,9 +3,10 @@ package types_test import ( "testing" + "github.com/stretchr/testify/require" + sdk "github.com/Finschia/finschia-sdk/types" "github.com/Finschia/finschia-sdk/x/fswap/types" - "github.com/stretchr/testify/require" ) func TestGenesisStateValidate(t *testing.T) { From cb6d6243b70c558f05da5b6db1f65f2a61a8082b Mon Sep 17 00:00:00 2001 From: 170210 Date: Mon, 13 May 2024 16:45:10 +0900 Subject: [PATCH 05/14] chore: add newKeeper test Signed-off-by: 170210 --- simapp/app.go | 4 +- x/fswap/keeper/expected_keepers.go | 27 +++++------ x/fswap/keeper/keeper.go | 16 +++++-- x/fswap/keeper/keeper_test.go | 54 ++++++++++++++++++++++ x/fswap/module.go | 7 ++- x/fswap/testutil/expected_keepers_mocks.go | 37 +++++++++++++++ 6 files changed, 123 insertions(+), 22 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 956a46626f..b4be6a11c3 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -342,7 +342,7 @@ func NewSimApp( app.AuthzKeeper = authzkeeper.NewKeeper(keys[authzkeeper.StoreKey], appCodec, app.BaseApp.MsgServiceRouter()) fswapConfig := fswaptypes.DefaultConfig() - app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], fswapConfig, fswaptypes.DefaultAuthority().String(), app.BankKeeper) + app.FswapKeeper = fswapkeeper.NewKeeper(appCodec, keys[fswaptypes.StoreKey], fswapConfig, fswaptypes.DefaultAuthority().String(), app.AccountKeeper, app.BankKeeper) // register the proposal types govRouter := govtypes.NewRouter() @@ -403,7 +403,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.BankKeeper), + fswap.NewAppModule(appCodec, app.FswapKeeper, app.AccountKeeper, app.BankKeeper), fbridgemodule.NewAppModule(appCodec, app.FbridgeKeeper), ) diff --git a/x/fswap/keeper/expected_keepers.go b/x/fswap/keeper/expected_keepers.go index 908ca160ac..405bb09753 100644 --- a/x/fswap/keeper/expected_keepers.go +++ b/x/fswap/keeper/expected_keepers.go @@ -5,16 +5,17 @@ import ( banktypes "github.com/Finschia/finschia-sdk/x/bank/types" ) -type ( - BankKeeper interface { - GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin - SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error - SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error - IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error - GetDenomMetaData(ctx sdk.Context, denom string) (banktypes.Metadata, bool) - SetDenomMetaData(ctx sdk.Context, denomMetaData banktypes.Metadata) - HasSupply(ctx sdk.Context, denom string) bool - MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error - BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error - } -) +type AccountKeeper interface { + GetModuleAddress(moduleName string) sdk.AccAddress +} +type BankKeeper interface { + GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin + SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error + SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error + IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error + GetDenomMetaData(ctx sdk.Context, denom string) (banktypes.Metadata, bool) + SetDenomMetaData(ctx sdk.Context, denomMetaData banktypes.Metadata) + HasSupply(ctx sdk.Context, denom string) bool + MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error + BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error +} diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index 6257d3dfe2..bfd6bbf9a9 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -15,18 +15,23 @@ import ( ) type Keeper struct { - cdc codec.BinaryCodec - storeKey storetypes.StoreKey - config types.Config - authority string + cdc codec.BinaryCodec + storeKey storetypes.StoreKey + config types.Config + authority string + authkeeper AccountKeeper BankKeeper } -func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, config types.Config, authority string, bk BankKeeper) Keeper { +func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, config types.Config, authority string, ak AccountKeeper, bk BankKeeper) Keeper { if _, err := sdk.AccAddressFromBech32(authority); err != nil { panic("authority is not a valid acc address") } + if addr := ak.GetModuleAddress(types.ModuleName); addr == nil { + panic("fbridge module account has not been set") + } + found := false for _, addr := range types.AuthorityCandidates() { if authority == addr.String() { @@ -44,6 +49,7 @@ func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, config types storeKey, config, authority, + ak, bk, } } diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index b958d23fc7..31f2b43706 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/golang/mock/gomock" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -13,10 +14,13 @@ import ( "github.com/Finschia/finschia-sdk/testutil/testdata" sdk "github.com/Finschia/finschia-sdk/types" sdkerrors "github.com/Finschia/finschia-sdk/types/errors" + authtypes "github.com/Finschia/finschia-sdk/x/auth/types" bank "github.com/Finschia/finschia-sdk/x/bank/types" + "github.com/Finschia/finschia-sdk/x/foundation" "github.com/Finschia/finschia-sdk/x/fswap/keeper" "github.com/Finschia/finschia-sdk/x/fswap/testutil" "github.com/Finschia/finschia-sdk/x/fswap/types" + govtypes "github.com/Finschia/finschia-sdk/x/gov/types" minttypes "github.com/Finschia/finschia-sdk/x/mint/types" ) @@ -115,6 +119,56 @@ func TestKeeperTestSuite(t *testing.T) { suite.Run(t, &KeeperTestSuite{}) } +func TestNewKeeper(t *testing.T) { + app := simapp.Setup(false) + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + authKeeper := testutil.NewMockAccountKeeper(ctrl) + testCases := map[string]struct { + malleate func() + isPanic bool + }{ + "fswap module account has not been set": { + malleate: func() { + authKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(nil).Times(1) + keeper.NewKeeper(app.AppCodec(), sdk.NewKVStoreKey(types.StoreKey), types.DefaultConfig(), types.DefaultAuthority().String(), authKeeper, app.BankKeeper) + }, + isPanic: true, + }, + "fswap authority must be the gov or foundation module account": { + malleate: func() { + authKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(authtypes.NewModuleAddress(types.ModuleName)).Times(1) + keeper.NewKeeper(app.AppCodec(), sdk.NewKVStoreKey(types.StoreKey), types.DefaultConfig(), authtypes.NewModuleAddress("invalid").String(), authKeeper, app.BankKeeper) + }, + isPanic: true, + }, + "success - gov authority": { + malleate: func() { + authKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(authtypes.NewModuleAddress(types.ModuleName)).Times(1) + keeper.NewKeeper(app.AppCodec(), sdk.NewKVStoreKey(types.StoreKey), types.DefaultConfig(), authtypes.NewModuleAddress(govtypes.ModuleName).String(), authKeeper, app.BankKeeper) + }, + isPanic: false, + }, + "success - foundation authority": { + malleate: func() { + authKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(authtypes.NewModuleAddress(types.ModuleName)).Times(1) + keeper.NewKeeper(app.AppCodec(), sdk.NewKVStoreKey(types.StoreKey), types.DefaultConfig(), authtypes.NewModuleAddress(foundation.ModuleName).String(), authKeeper, app.BankKeeper) + }, + isPanic: false, + }, + } + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + if tc.isPanic { + require.Panics(t, tc.malleate) + } else { + tc.malleate() + } + }) + } +} + func (s *KeeperTestSuite) TestSwap() { swap2ExpectedAmount, ok := sdk.NewIntFromString("296159312000000") s.Require().True(ok) diff --git a/x/fswap/module.go b/x/fswap/module.go index d561bbe296..ffc02c58f3 100644 --- a/x/fswap/module.go +++ b/x/fswap/module.go @@ -86,18 +86,21 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command { type AppModule struct { AppModuleBasic - keeper keeper.Keeper - bankKeeper keeper.BankKeeper + keeper keeper.Keeper + accountKeeper keeper.AccountKeeper + bankKeeper keeper.BankKeeper } func NewAppModule( cdc codec.Codec, keeper keeper.Keeper, + accountKeeper keeper.AccountKeeper, bankKeeper keeper.BankKeeper, ) AppModule { return AppModule{ AppModuleBasic: NewAppModuleBasic(cdc), keeper: keeper, + accountKeeper: accountKeeper, bankKeeper: bankKeeper, } } diff --git a/x/fswap/testutil/expected_keepers_mocks.go b/x/fswap/testutil/expected_keepers_mocks.go index de503f6f08..b0173b1dc2 100644 --- a/x/fswap/testutil/expected_keepers_mocks.go +++ b/x/fswap/testutil/expected_keepers_mocks.go @@ -12,6 +12,43 @@ import ( gomock "github.com/golang/mock/gomock" ) +// MockAccountKeeper is a mock of AccountKeeper interface. +type MockAccountKeeper struct { + ctrl *gomock.Controller + recorder *MockAccountKeeperMockRecorder +} + +// MockAccountKeeperMockRecorder is the mock recorder for MockAccountKeeper. +type MockAccountKeeperMockRecorder struct { + mock *MockAccountKeeper +} + +// NewMockAccountKeeper creates a new mock instance. +func NewMockAccountKeeper(ctrl *gomock.Controller) *MockAccountKeeper { + mock := &MockAccountKeeper{ctrl: ctrl} + mock.recorder = &MockAccountKeeperMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockAccountKeeper) EXPECT() *MockAccountKeeperMockRecorder { + return m.recorder +} + +// GetModuleAddress mocks base method. +func (m *MockAccountKeeper) GetModuleAddress(moduleName string) types.AccAddress { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetModuleAddress", moduleName) + ret0, _ := ret[0].(types.AccAddress) + return ret0 +} + +// GetModuleAddress indicates an expected call of GetModuleAddress. +func (mr *MockAccountKeeperMockRecorder) GetModuleAddress(moduleName interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModuleAddress", reflect.TypeOf((*MockAccountKeeper)(nil).GetModuleAddress), moduleName) +} + // MockBankKeeper is a mock of BankKeeper interface. type MockBankKeeper struct { ctrl *gomock.Controller From bf8eb978321424c375848ce5b6f027052aa71dee Mon Sep 17 00:00:00 2001 From: 170210 Date: Tue, 14 May 2024 14:51:21 +0900 Subject: [PATCH 06/14] fix: fix for comment Signed-off-by: 170210 --- x/fswap/keeper/keeper.go | 6 +----- x/fswap/keeper/keeper_test.go | 12 +----------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index bfd6bbf9a9..eb92bb5fe6 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -29,7 +29,7 @@ func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, config types } if addr := ak.GetModuleAddress(types.ModuleName); addr == nil { - panic("fbridge module account has not been set") + panic("fswap module account has not been set") } found := false @@ -131,10 +131,6 @@ func (k Keeper) SetSwap(ctx sdk.Context, swap types.Swap, toDenomMetadata bank.M return types.ErrCanNotHaveMoreSwap.Wrapf("cannot make more swaps, max swaps is %d", k.config.MaxSwaps) } - if swap.ToDenom != toDenomMetadata.Base { - return sdkerrors.ErrInvalidRequest.Wrap("toDenom should be existed in metadata") - } - if !k.HasSupply(ctx, swap.FromDenom) { return sdkerrors.ErrInvalidRequest.Wrap("fromDenom should be existed in chain") } diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index 31f2b43706..cd1f3a38ad 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -281,16 +281,6 @@ func (s *KeeperTestSuite) TestSetSwap() { s.toDenomMetadata, sdkerrors.ErrInvalidRequest, }, - "to-denom does not equal with metadata": { - types.Swap{ - FromDenom: "fromdenom", - ToDenom: "fakedenom", - AmountCapForToDenom: sdk.OneInt(), - SwapRate: sdk.OneDec(), - }, - s.toDenomMetadata, - sdkerrors.ErrInvalidRequest, - }, "to-denom metadata change not allowed": { types.Swap{ FromDenom: "fromdenom", @@ -312,7 +302,7 @@ func (s *KeeperTestSuite) TestSetSwap() { for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() - err := s.keeper.SetSwap(ctx, tc.swap, s.toDenomMetadata) + err := s.keeper.SetSwap(ctx, tc.swap, tc.toDenomMeta) s.Require().ErrorIs(err, tc.expectedError) }) } From 7e0df2c3a3ea809e7cd5cb20e6b9981fcd0d9699 Mon Sep 17 00:00:00 2001 From: 170210 Date: Fri, 17 May 2024 16:55:32 +0900 Subject: [PATCH 07/14] fix: fix test Signed-off-by: 170210 --- x/fswap/keeper/keeper_test.go | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index 6c10f779a2..0e0a8b473f 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -278,7 +278,7 @@ func (s *KeeperTestSuite) TestSetSwap() { Attributes: []abci.EventAttribute{ { Key: []byte("swap"), - Value: []uint8{0x7b, 0x22, 0x66, 0x72, 0x6f, 0x6d, 0x5f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x66, 0x72, 0x6f, 0x6d, 0x44, 0x22, 0x2c, 0x22, 0x74, 0x6f, 0x5f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x74, 0x6f, 0x44, 0x22, 0x2c, 0x22, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x5f, 0x63, 0x61, 0x70, 0x5f, 0x66, 0x6f, 0x72, 0x5f, 0x74, 0x6f, 0x5f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x31, 0x22, 0x2c, 0x22, 0x73, 0x77, 0x61, 0x70, 0x5f, 0x72, 0x61, 0x74, 0x65, 0x22, 0x3a, 0x22, 0x31, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x22, 0x7d}, + Value: []uint8{0x7b, 0x22, 0x66, 0x72, 0x6f, 0x6d, 0x5f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x66, 0x72, 0x6f, 0x6d, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x2c, 0x22, 0x74, 0x6f, 0x5f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x74, 0x6f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x2c, 0x22, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x5f, 0x63, 0x61, 0x70, 0x5f, 0x66, 0x6f, 0x72, 0x5f, 0x74, 0x6f, 0x5f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x31, 0x22, 0x2c, 0x22, 0x73, 0x77, 0x61, 0x70, 0x5f, 0x72, 0x61, 0x74, 0x65, 0x22, 0x3a, 0x22, 0x31, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x22, 0x7d}, Index: false, }, }, @@ -288,7 +288,7 @@ func (s *KeeperTestSuite) TestSetSwap() { Attributes: []abci.EventAttribute{ { Key: []byte("metadata"), - Value: []uint8{0x7b, 0x22, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x3a, 0x22, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x6d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x2d, 0x63, 0x6f, 0x69, 0x6e, 0x22, 0x2c, 0x22, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x5f, 0x75, 0x6e, 0x69, 0x74, 0x73, 0x22, 0x3a, 0x5b, 0x7b, 0x22, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x74, 0x6f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x2c, 0x22, 0x65, 0x78, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x22, 0x3a, 0x30, 0x2c, 0x22, 0x61, 0x6c, 0x69, 0x61, 0x73, 0x65, 0x73, 0x22, 0x3a, 0x5b, 0x5d, 0x7d, 0x5d, 0x2c, 0x22, 0x62, 0x61, 0x73, 0x65, 0x22, 0x3a, 0x22, 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x22, 0x2c, 0x22, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x22, 0x3a, 0x22, 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x63, 0x6f, 0x69, 0x6e, 0x22, 0x2c, 0x22, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3a, 0x22, 0x44, 0x55, 0x4d, 0x4d, 0x59, 0x22, 0x2c, 0x22, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x22, 0x3a, 0x22, 0x44, 0x55, 0x4d, 0x22, 0x7d}, + Value: []uint8{0x7b, 0x22, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x3a, 0x22, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x6d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x2d, 0x63, 0x6f, 0x69, 0x6e, 0x22, 0x2c, 0x22, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x5f, 0x75, 0x6e, 0x69, 0x74, 0x73, 0x22, 0x3a, 0x5b, 0x7b, 0x22, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x74, 0x6f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x2c, 0x22, 0x65, 0x78, 0x70, 0x6f, 0x6e, 0x65, 0x6e, 0x74, 0x22, 0x3a, 0x30, 0x2c, 0x22, 0x61, 0x6c, 0x69, 0x61, 0x73, 0x65, 0x73, 0x22, 0x3a, 0x5b, 0x5d, 0x7d, 0x5d, 0x2c, 0x22, 0x62, 0x61, 0x73, 0x65, 0x22, 0x3a, 0x22, 0x74, 0x6f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x2c, 0x22, 0x64, 0x69, 0x73, 0x70, 0x6c, 0x61, 0x79, 0x22, 0x3a, 0x22, 0x74, 0x6f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x63, 0x6f, 0x69, 0x6e, 0x22, 0x2c, 0x22, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3a, 0x22, 0x44, 0x55, 0x4d, 0x4d, 0x59, 0x22, 0x2c, 0x22, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x22, 0x3a, 0x22, 0x44, 0x55, 0x4d, 0x22, 0x7d}, Index: false, }, }, @@ -306,16 +306,6 @@ func (s *KeeperTestSuite) TestSetSwap() { sdkerrors.ErrInvalidRequest, sdk.Events{}, }, - "from-denom does not exist": { - types.Swap{ - FromDenom: "fakedenom", - ToDenom: "todenom", - AmountCapForToDenom: sdk.OneInt(), - SwapRate: sdk.OneDec(), - }, - s.toDenomMetadata, - sdkerrors.ErrInvalidRequest, - }, "to-denom metadata change not allowed": { types.Swap{ FromDenom: "fromdenom", @@ -339,7 +329,10 @@ func (s *KeeperTestSuite) TestSetSwap() { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() err := s.keeper.SetSwap(ctx, tc.swap, tc.toDenomMeta) - s.Require().ErrorIs(err, tc.expectedError) + if tc.expectedError != nil { + s.Require().ErrorIs(err, tc.expectedError) + return + } events := ctx.EventManager().Events() s.Require().Equal(tc.expectedEvents, events) }) From b5b105c326fafceea72dc1a78f9048ead1647709 Mon Sep 17 00:00:00 2001 From: 170210 Date: Fri, 17 May 2024 18:18:01 +0900 Subject: [PATCH 08/14] fix: fix for comments Signed-off-by: 170210 --- x/fswap/keeper/expected_keepers.go | 1 - x/fswap/keeper/keeper.go | 2 +- x/fswap/keeper/keeper_test.go | 49 ++++++++++++++++++---- x/fswap/testutil/expected_keepers_mocks.go | 14 ------- 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/x/fswap/keeper/expected_keepers.go b/x/fswap/keeper/expected_keepers.go index 405bb09753..5c7ad92fc4 100644 --- a/x/fswap/keeper/expected_keepers.go +++ b/x/fswap/keeper/expected_keepers.go @@ -15,7 +15,6 @@ type BankKeeper interface { IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error GetDenomMetaData(ctx sdk.Context, denom string) (banktypes.Metadata, bool) SetDenomMetaData(ctx sdk.Context, denomMetaData banktypes.Metadata) - HasSupply(ctx sdk.Context, denom string) bool MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error } diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index bc183e1577..a83f62fd50 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -131,7 +131,7 @@ func (k Keeper) SetSwap(ctx sdk.Context, swap types.Swap, toDenomMetadata bank.M return types.ErrCanNotHaveMoreSwap.Wrapf("cannot make more swaps, max swaps is %d", k.config.MaxSwaps) } - if !k.HasSupply(ctx, swap.FromDenom) { + if _, ok := k.GetDenomMetaData(ctx, swap.FromDenom); !ok { return sdkerrors.ErrInvalidRequest.Wrap("fromDenom should be existed in chain") } diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index 0e0a8b473f..63ef7c32df 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -248,18 +248,12 @@ func (s *KeeperTestSuite) TestSwap() { func (s *KeeperTestSuite) TestSetSwap() { ctrl := gomock.NewController(s.T()) defer ctrl.Finish() - bankKeeper := testutil.NewMockBankKeeper(ctrl) - bankKeeper.EXPECT().HasSupply(gomock.Any(), "fromdenom").Return(true).AnyTimes() - bankKeeper.EXPECT().HasSupply(gomock.Any(), gomock.Any()).Return(false).AnyTimes() - bankKeeper.EXPECT().GetDenomMetaData(gomock.Any(), "todenom").Return(bank.Metadata{}, false).AnyTimes() - bankKeeper.EXPECT().GetDenomMetaData(gomock.Any(), gomock.Any()).Return(s.toDenomMetadata, true).AnyTimes() - bankKeeper.EXPECT().SetDenomMetaData(gomock.Any(), s.toDenomMetadata).Times(1) - s.keeper.BankKeeper = bankKeeper testCases := map[string]struct { swap types.Swap toDenomMeta bank.Metadata + malleate func() expectedError error expectedEvents sdk.Events }{ @@ -271,6 +265,11 @@ func (s *KeeperTestSuite) TestSetSwap() { SwapRate: sdk.OneDec(), }, s.toDenomMetadata, + func() { + bankKeeper.EXPECT().GetDenomMetaData(gomock.Any(), "fromdenom").Return(bank.Metadata{}, true).Times(1) + bankKeeper.EXPECT().GetDenomMetaData(gomock.Any(), "todenom").Return(bank.Metadata{}, false).Times(1) + bankKeeper.EXPECT().SetDenomMetaData(gomock.Any(), s.toDenomMetadata).Times(1) + }, nil, sdk.Events{ sdk.Event{ @@ -295,6 +294,32 @@ func (s *KeeperTestSuite) TestSetSwap() { }, }, }, + "to-denom metadata has been stored": { + types.Swap{ + FromDenom: "fromdenom", + ToDenom: "todenom", + AmountCapForToDenom: sdk.OneInt(), + SwapRate: sdk.OneDec(), + }, + s.toDenomMetadata, + func() { + bankKeeper.EXPECT().GetDenomMetaData(gomock.Any(), "fromdenom").Return(bank.Metadata{}, true).Times(1) + bankKeeper.EXPECT().GetDenomMetaData(gomock.Any(), "todenom").Return(s.toDenomMetadata, true).Times(1) + }, + nil, + sdk.Events{ + sdk.Event{ + Type: "lbm.fswap.v1.EventSetSwap", + Attributes: []abci.EventAttribute{ + { + Key: []byte("swap"), + Value: []uint8{0x7b, 0x22, 0x66, 0x72, 0x6f, 0x6d, 0x5f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x66, 0x72, 0x6f, 0x6d, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x2c, 0x22, 0x74, 0x6f, 0x5f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x74, 0x6f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x2c, 0x22, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x5f, 0x63, 0x61, 0x70, 0x5f, 0x66, 0x6f, 0x72, 0x5f, 0x74, 0x6f, 0x5f, 0x64, 0x65, 0x6e, 0x6f, 0x6d, 0x22, 0x3a, 0x22, 0x31, 0x22, 0x2c, 0x22, 0x73, 0x77, 0x61, 0x70, 0x5f, 0x72, 0x61, 0x74, 0x65, 0x22, 0x3a, 0x22, 0x31, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x22, 0x7d}, + Index: false, + }, + }, + }, + }, + }, "from-denom does not exist": { types.Swap{ FromDenom: "fakedenom", @@ -303,6 +328,9 @@ func (s *KeeperTestSuite) TestSetSwap() { SwapRate: sdk.OneDec(), }, s.toDenomMetadata, + func() { + bankKeeper.EXPECT().GetDenomMetaData(gomock.Any(), "fakedenom").Return(bank.Metadata{}, false).Times(1) + }, sdkerrors.ErrInvalidRequest, sdk.Events{}, }, @@ -321,6 +349,10 @@ func (s *KeeperTestSuite) TestSetSwap() { Name: s.toDenomMetadata.Name, Symbol: s.toDenomMetadata.Symbol, }, + func() { + bankKeeper.EXPECT().GetDenomMetaData(gomock.Any(), "fromdenom").Return(bank.Metadata{}, true).Times(1) + bankKeeper.EXPECT().GetDenomMetaData(gomock.Any(), "change").Return(s.toDenomMetadata, true).Times(1) + }, sdkerrors.ErrInvalidRequest, sdk.Events{}, }, @@ -328,6 +360,9 @@ func (s *KeeperTestSuite) TestSetSwap() { for name, tc := range testCases { s.Run(name, func() { ctx, _ := s.ctx.CacheContext() + tc.malleate() + s.keeper.BankKeeper = bankKeeper + err := s.keeper.SetSwap(ctx, tc.swap, tc.toDenomMeta) if tc.expectedError != nil { s.Require().ErrorIs(err, tc.expectedError) diff --git a/x/fswap/testutil/expected_keepers_mocks.go b/x/fswap/testutil/expected_keepers_mocks.go index b0173b1dc2..7693d4093f 100644 --- a/x/fswap/testutil/expected_keepers_mocks.go +++ b/x/fswap/testutil/expected_keepers_mocks.go @@ -115,20 +115,6 @@ func (mr *MockBankKeeperMockRecorder) GetDenomMetaData(ctx, denom interface{}) * return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetDenomMetaData", reflect.TypeOf((*MockBankKeeper)(nil).GetDenomMetaData), ctx, denom) } -// HasSupply mocks base method. -func (m *MockBankKeeper) HasSupply(ctx types.Context, denom string) bool { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "HasSupply", ctx, denom) - ret0, _ := ret[0].(bool) - return ret0 -} - -// HasSupply indicates an expected call of HasSupply. -func (mr *MockBankKeeperMockRecorder) HasSupply(ctx, denom interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HasSupply", reflect.TypeOf((*MockBankKeeper)(nil).HasSupply), ctx, denom) -} - // IsSendEnabledCoins mocks base method. func (m *MockBankKeeper) IsSendEnabledCoins(ctx types.Context, coins ...types.Coin) error { m.ctrl.T.Helper() From 52bbde520d512cda13c50504d249a3ec70b61444 Mon Sep 17 00:00:00 2001 From: 170210 Date: Mon, 20 May 2024 18:50:07 +0900 Subject: [PATCH 09/14] fix: fix failed tests Signed-off-by: 170210 --- x/fswap/keeper/keeper_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/x/fswap/keeper/keeper_test.go b/x/fswap/keeper/keeper_test.go index 63ef7c32df..4d3b68eb06 100644 --- a/x/fswap/keeper/keeper_test.go +++ b/x/fswap/keeper/keeper_test.go @@ -94,6 +94,17 @@ func (s *KeeperTestSuite) SetupTest() { Name: "DUMMY", Symbol: "DUM", } + fromDenom := bank.Metadata{ + Description: "This is metadata for from-coin", + DenomUnits: []*bank.DenomUnit{ + {Denom: "fromdenom", Exponent: 0}, + }, + Base: "fromdenom", + Display: "fromdenomcoin", + Name: "DUMMY", + Symbol: "DUM", + } + app.BankKeeper.SetDenomMetaData(s.ctx, fromDenom) s.createAccountsWithInitBalance(app) app.AccountKeeper.GetModuleAccount(s.ctx, types.ModuleName) } From 7e2c647d6e07698010a81ac7cf9d6f3f75eb55b1 Mon Sep 17 00:00:00 2001 From: 170210 Date: Tue, 21 May 2024 14:14:51 +0900 Subject: [PATCH 10/14] fix: fix for comment Signed-off-by: 170210 --- x/fswap/keeper/keeper.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/x/fswap/keeper/keeper.go b/x/fswap/keeper/keeper.go index a83f62fd50..c2b5f1e1cc 100644 --- a/x/fswap/keeper/keeper.go +++ b/x/fswap/keeper/keeper.go @@ -15,11 +15,10 @@ import ( ) type Keeper struct { - cdc codec.BinaryCodec - storeKey storetypes.StoreKey - config types.Config - authority string - authkeeper AccountKeeper + cdc codec.BinaryCodec + storeKey storetypes.StoreKey + config types.Config + authority string BankKeeper } @@ -49,7 +48,6 @@ func NewKeeper(cdc codec.BinaryCodec, storeKey storetypes.StoreKey, config types storeKey, config, authority, - ak, bk, } } From 56e3b095532978c1beecd0be60f1674fbfe3511c Mon Sep 17 00:00:00 2001 From: 170210 Date: Tue, 21 May 2024 14:25:16 +0900 Subject: [PATCH 11/14] fix: remove unused keeper in appmodule Signed-off-by: 170210 --- simapp/app.go | 2 +- x/fswap/module.go | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index b4be6a11c3..62c24388af 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -403,7 +403,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), + fswap.NewAppModule(appCodec, app.FswapKeeper, app.BankKeeper), fbridgemodule.NewAppModule(appCodec, app.FbridgeKeeper), ) diff --git a/x/fswap/module.go b/x/fswap/module.go index 7531b7096c..acca5b9976 100644 --- a/x/fswap/module.go +++ b/x/fswap/module.go @@ -89,21 +89,18 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command { type AppModule struct { AppModuleBasic - keeper keeper.Keeper - accountKeeper keeper.AccountKeeper - bankKeeper keeper.BankKeeper + keeper keeper.Keeper + bankKeeper keeper.BankKeeper } func NewAppModule( cdc codec.Codec, keeper keeper.Keeper, - accountKeeper keeper.AccountKeeper, bankKeeper keeper.BankKeeper, ) AppModule { return AppModule{ AppModuleBasic: NewAppModuleBasic(cdc), keeper: keeper, - accountKeeper: accountKeeper, bankKeeper: bankKeeper, } } From 5345557145da8ea71c91198d652919539e61ce8a Mon Sep 17 00:00:00 2001 From: 170210 Date: Tue, 21 May 2024 14:55:55 +0900 Subject: [PATCH 12/14] fix: fix for merge (FSwapQueryTestSuite Setup up) Signed-off-by: 170210 --- x/fswap/keeper/grpc_query_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/x/fswap/keeper/grpc_query_test.go b/x/fswap/keeper/grpc_query_test.go index f141459d09..b0c02d7f2e 100644 --- a/x/fswap/keeper/grpc_query_test.go +++ b/x/fswap/keeper/grpc_query_test.go @@ -65,6 +65,17 @@ func (s *FSwapQueryTestSuite) SetupTest() { Symbol: "DUM", } + fromDenom := bank.Metadata{ + Description: "This is metadata for from-coin", + DenomUnits: []*bank.DenomUnit{ + {Denom: s.fromDenom, Exponent: 0}, + }, + Base: s.fromDenom, + Display: "fromcoin", + Name: "FROM", + Symbol: "FROM", + } + s.app.BankKeeper.SetDenomMetaData(s.ctx, fromDenom) err = s.keeper.SetSwap(s.ctx, s.swap, s.toDenomMetadata) s.Require().NoError(err) } From 20d392e81224182f15093a4fd1cf7571b59c8929 Mon Sep 17 00:00:00 2001 From: 170210 Date: Tue, 21 May 2024 15:48:42 +0900 Subject: [PATCH 13/14] fix: fix for codecov Signed-off-by: 170210 --- x/fswap/types/genesis_test.go | 95 +++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 38 deletions(-) diff --git a/x/fswap/types/genesis_test.go b/x/fswap/types/genesis_test.go index 26a66e43bd..fa8c2901ac 100644 --- a/x/fswap/types/genesis_test.go +++ b/x/fswap/types/genesis_test.go @@ -10,32 +10,6 @@ import ( ) func TestGenesisStateValidate(t *testing.T) { - testSwapRate, _ := sdk.NewDecFromStr("1234567890") - exampleGenesis := &types.GenesisState{ - Swaps: []types.Swap{ - { - FromDenom: "aaa", - ToDenom: "bbb", - AmountCapForToDenom: sdk.NewInt(1234567890000), - SwapRate: testSwapRate, - }, - }, - SwapStats: types.SwapStats{ - SwapCount: 1, - }, - Swappeds: []types.Swapped{ - { - FromCoinAmount: sdk.Coin{ - Denom: "aaa", - Amount: sdk.ZeroInt(), - }, - ToCoinAmount: sdk.Coin{ - Denom: "bbb", - Amount: sdk.ZeroInt(), - }, - }, - }, - } for _, tc := range []struct { desc string genState *types.GenesisState @@ -49,12 +23,12 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "example is valid", - genState: exampleGenesis, + genState: newExampleGenesis(), valid: true, }, { desc: "SwapCount is nagative in SwapStats is invalid", - genState: exampleGenesis, + genState: newExampleGenesis(), modify: func(gs *types.GenesisState) { gs.SwapStats.SwapCount = -1 }, @@ -62,7 +36,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "number of swaps does not match number of Swappeds", - genState: exampleGenesis, + genState: newExampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swaps = append(gs.Swaps, types.Swap{}) }, @@ -70,7 +44,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "number of swaps does not match number of Swappeds", - genState: exampleGenesis, + genState: newExampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swaps = append(gs.Swaps, types.Swap{}) gs.Swappeds = append(gs.Swappeds, types.Swapped{}) @@ -79,7 +53,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "fromDenom=toDenom in Swap is invalid", - genState: exampleGenesis, + genState: newExampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swaps[0].ToDenom = "aaa" }, @@ -87,7 +61,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "AmountCapForToDenom=0 in Swap is invalid", - genState: exampleGenesis, + genState: newExampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swaps[0].AmountCapForToDenom = sdk.ZeroInt() }, @@ -95,7 +69,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "SwapRate=0 in Swap is invalid", - genState: exampleGenesis, + genState: newExampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swaps[0].SwapRate = sdk.ZeroDec() }, @@ -103,7 +77,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "FromCoinAmount is nagative in Swappeds is invalid", - genState: exampleGenesis, + genState: newExampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swappeds[0].FromCoinAmount.Amount = sdk.NewInt(-1) }, @@ -111,15 +85,31 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "ToCoinAmount is nagative in Swappeds is invalid", - genState: exampleGenesis, + genState: newExampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swappeds[0].ToCoinAmount.Amount = sdk.NewInt(-1) }, valid: false, }, + { + desc: "Swap in Swaps is invalid", + genState: newExampleGenesis(), + modify: func(gs *types.GenesisState) { + gs.Swaps[0].FromDenom = "" + }, + valid: false, + }, + { + desc: "Swapped in Swappeds is invalide ", + genState: newExampleGenesis(), + modify: func(gs *types.GenesisState) { + gs.Swappeds[0].FromCoinAmount.Denom = "" + }, + valid: false, + }, { desc: "FromCoin in swap and swapped do not correspond", - genState: exampleGenesis, + genState: newExampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swappeds[0].FromCoinAmount.Denom = "ccc" }, @@ -127,7 +117,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "ToCoin in swap and swapped do not correspond", - genState: exampleGenesis, + genState: newExampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swappeds[0].ToCoinAmount.Denom = "ccc" }, @@ -135,7 +125,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "AmountCapForToDenom has been exceeded is invalid", - genState: exampleGenesis, + genState: newExampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swappeds[0].ToCoinAmount.Amount = sdk.NewInt(12345678900000000) }, @@ -155,3 +145,32 @@ func TestGenesisStateValidate(t *testing.T) { }) } } + +func newExampleGenesis() *types.GenesisState { + testSwapRate, _ := sdk.NewDecFromStr("1234567890") + return &types.GenesisState{ + Swaps: []types.Swap{ + { + FromDenom: "aaa", + ToDenom: "bbb", + AmountCapForToDenom: sdk.NewInt(1234567890000), + SwapRate: testSwapRate, + }, + }, + SwapStats: types.SwapStats{ + SwapCount: 1, + }, + Swappeds: []types.Swapped{ + { + FromCoinAmount: sdk.Coin{ + Denom: "aaa", + Amount: sdk.ZeroInt(), + }, + ToCoinAmount: sdk.Coin{ + Denom: "bbb", + Amount: sdk.ZeroInt(), + }, + }, + }, + } +} From 1f01fc9a779d0382098fae04461f2cfc880e4578 Mon Sep 17 00:00:00 2001 From: 170210 Date: Tue, 21 May 2024 16:03:45 +0900 Subject: [PATCH 14/14] fix: fix for comment Signed-off-by: 170210 --- x/fswap/types/genesis_test.go | 86 +++++++++++++++++------------------ 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/x/fswap/types/genesis_test.go b/x/fswap/types/genesis_test.go index fa8c2901ac..fc2ed9a781 100644 --- a/x/fswap/types/genesis_test.go +++ b/x/fswap/types/genesis_test.go @@ -10,6 +10,35 @@ import ( ) func TestGenesisStateValidate(t *testing.T) { + exampleGenesis := func() *types.GenesisState { + testSwapRate, _ := sdk.NewDecFromStr("1234567890") + return &types.GenesisState{ + Swaps: []types.Swap{ + { + FromDenom: "aaa", + ToDenom: "bbb", + AmountCapForToDenom: sdk.NewInt(1234567890000), + SwapRate: testSwapRate, + }, + }, + SwapStats: types.SwapStats{ + SwapCount: 1, + }, + Swappeds: []types.Swapped{ + { + FromCoinAmount: sdk.Coin{ + Denom: "aaa", + Amount: sdk.ZeroInt(), + }, + ToCoinAmount: sdk.Coin{ + Denom: "bbb", + Amount: sdk.ZeroInt(), + }, + }, + }, + } + } + for _, tc := range []struct { desc string genState *types.GenesisState @@ -23,12 +52,12 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "example is valid", - genState: newExampleGenesis(), + genState: exampleGenesis(), valid: true, }, { desc: "SwapCount is nagative in SwapStats is invalid", - genState: newExampleGenesis(), + genState: exampleGenesis(), modify: func(gs *types.GenesisState) { gs.SwapStats.SwapCount = -1 }, @@ -36,7 +65,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "number of swaps does not match number of Swappeds", - genState: newExampleGenesis(), + genState: exampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swaps = append(gs.Swaps, types.Swap{}) }, @@ -44,7 +73,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "number of swaps does not match number of Swappeds", - genState: newExampleGenesis(), + genState: exampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swaps = append(gs.Swaps, types.Swap{}) gs.Swappeds = append(gs.Swappeds, types.Swapped{}) @@ -53,7 +82,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "fromDenom=toDenom in Swap is invalid", - genState: newExampleGenesis(), + genState: exampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swaps[0].ToDenom = "aaa" }, @@ -61,7 +90,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "AmountCapForToDenom=0 in Swap is invalid", - genState: newExampleGenesis(), + genState: exampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swaps[0].AmountCapForToDenom = sdk.ZeroInt() }, @@ -69,7 +98,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "SwapRate=0 in Swap is invalid", - genState: newExampleGenesis(), + genState: exampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swaps[0].SwapRate = sdk.ZeroDec() }, @@ -77,7 +106,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "FromCoinAmount is nagative in Swappeds is invalid", - genState: newExampleGenesis(), + genState: exampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swappeds[0].FromCoinAmount.Amount = sdk.NewInt(-1) }, @@ -85,7 +114,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "ToCoinAmount is nagative in Swappeds is invalid", - genState: newExampleGenesis(), + genState: exampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swappeds[0].ToCoinAmount.Amount = sdk.NewInt(-1) }, @@ -93,7 +122,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "Swap in Swaps is invalid", - genState: newExampleGenesis(), + genState: exampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swaps[0].FromDenom = "" }, @@ -101,7 +130,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "Swapped in Swappeds is invalide ", - genState: newExampleGenesis(), + genState: exampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swappeds[0].FromCoinAmount.Denom = "" }, @@ -109,7 +138,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "FromCoin in swap and swapped do not correspond", - genState: newExampleGenesis(), + genState: exampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swappeds[0].FromCoinAmount.Denom = "ccc" }, @@ -117,7 +146,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "ToCoin in swap and swapped do not correspond", - genState: newExampleGenesis(), + genState: exampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swappeds[0].ToCoinAmount.Denom = "ccc" }, @@ -125,7 +154,7 @@ func TestGenesisStateValidate(t *testing.T) { }, { desc: "AmountCapForToDenom has been exceeded is invalid", - genState: newExampleGenesis(), + genState: exampleGenesis(), modify: func(gs *types.GenesisState) { gs.Swappeds[0].ToCoinAmount.Amount = sdk.NewInt(12345678900000000) }, @@ -145,32 +174,3 @@ func TestGenesisStateValidate(t *testing.T) { }) } } - -func newExampleGenesis() *types.GenesisState { - testSwapRate, _ := sdk.NewDecFromStr("1234567890") - return &types.GenesisState{ - Swaps: []types.Swap{ - { - FromDenom: "aaa", - ToDenom: "bbb", - AmountCapForToDenom: sdk.NewInt(1234567890000), - SwapRate: testSwapRate, - }, - }, - SwapStats: types.SwapStats{ - SwapCount: 1, - }, - Swappeds: []types.Swapped{ - { - FromCoinAmount: sdk.Coin{ - Denom: "aaa", - Amount: sdk.ZeroInt(), - }, - ToCoinAmount: sdk.Coin{ - Denom: "bbb", - Amount: sdk.ZeroInt(), - }, - }, - }, - } -}