From 871ec94075b50d2b5014a17d4431144b8c6bc4e5 Mon Sep 17 00:00:00 2001 From: Kitipong Sirirueangsakul Date: Wed, 13 Sep 2023 16:03:47 +0700 Subject: [PATCH] fix test --- x/globalfee/feechecker/feechecker_test.go | 548 +++++++++++----------- x/globalfee/genesis_test.go | 70 --- x/globalfee/keeper/genesis.go | 20 + x/globalfee/keeper/genesis_test.go | 81 ++++ x/globalfee/keeper/grpc_query.go | 6 +- x/globalfee/keeper/grpc_query_test.go | 124 ++--- x/globalfee/module.go | 37 +- x/globalfee/types/params.go | 7 + 8 files changed, 473 insertions(+), 420 deletions(-) create mode 100644 x/globalfee/keeper/genesis.go create mode 100644 x/globalfee/keeper/genesis_test.go diff --git a/x/globalfee/feechecker/feechecker_test.go b/x/globalfee/feechecker/feechecker_test.go index fbdab3c27..b0085bbca 100644 --- a/x/globalfee/feechecker/feechecker_test.go +++ b/x/globalfee/feechecker/feechecker_test.go @@ -1,276 +1,276 @@ package feechecker_test -// import ( -// "math" -// "testing" - -// sdk "github.com/cosmos/cosmos-sdk/types" -// "github.com/stretchr/testify/suite" - -// "github.com/bandprotocol/chain/v2/testing/testapp" -// "github.com/bandprotocol/chain/v2/x/globalfee/feechecker" -// "github.com/bandprotocol/chain/v2/x/oracle/types" -// "github.com/cosmos/cosmos-sdk/x/authz" -// ) - -// var ( -// BasicCalldata = []byte("BASIC_CALLDATA") -// BasicClientID = "BASIC_CLIENT_ID" -// ) - -// type StubTx struct { -// sdk.Tx -// sdk.FeeTx -// Msgs []sdk.Msg -// GasPrices sdk.DecCoins -// } - -// func (st *StubTx) GetMsgs() []sdk.Msg { -// return st.Msgs -// } - -// func (st *StubTx) ValidateBasic() error { -// return nil -// } - -// func (st *StubTx) GetGas() uint64 { -// return 1000000 -// } - -// func (st *StubTx) GetFee() sdk.Coins { -// fees := make(sdk.Coins, len(st.GasPrices)) - -// // Determine the fees by multiplying each gas prices -// glDec := sdk.NewDec(int64(st.GetGas())) -// for i, gp := range st.GasPrices { -// fee := gp.Amount.Mul(glDec) -// fees[i] = sdk.NewCoin(gp.Denom, fee.Ceil().RoundInt()) -// } - -// return fees -// } - -// type FeeCheckerTestSuite struct { -// suite.Suite -// FeeChecker feechecker.FeeChecker -// ctx sdk.Context -// requestId types.RequestID -// } - -// func (suite *FeeCheckerTestSuite) SetupTest() { -// app, ctx, oracleKeeper := testapp.CreateTestInput(true) -// suite.ctx = ctx.WithBlockHeight(999). -// WithIsCheckTx(true). -// WithMinGasPrices(sdk.DecCoins{{Denom: "uband", Amount: sdk.NewDecWithPrec(1, 4)}}) - -// oracleKeeper.GrantReporter(suite.ctx, testapp.Validators[0].ValAddress, testapp.Alice.Address) - -// req := types.NewRequest( -// 1, -// BasicCalldata, -// []sdk.ValAddress{testapp.Validators[0].ValAddress}, -// 1, -// 1, -// testapp.ParseTime(0), -// "", -// nil, -// nil, -// 0, -// ) -// suite.requestId = oracleKeeper.AddRequest(suite.ctx, req) - -// suite.FeeChecker = feechecker.NewFeeChecker( -// &oracleKeeper, -// &app.GlobalfeeKeeper, -// app.StakingKeeper, -// ) -// } - -// func (suite *FeeCheckerTestSuite) TestValidRawReport() { -// msgs := []sdk.Msg{types.NewMsgReportData(suite.requestId, []types.RawReport{}, testapp.Validators[0].ValAddress)} -// stubTx := &StubTx{Msgs: msgs} - -// // test - check report tx -// isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) -// suite.Require().True(isReportTx) - -// // test - check tx fee with min gas prices -// fee, priority, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) -// suite.Require().NoError(err) -// suite.Require().Equal(sdk.Coins{}, fee) -// suite.Require().Equal(int64(math.MaxInt64), priority) -// } - -// func (suite *FeeCheckerTestSuite) TestNotValidRawReport() { -// msgs := []sdk.Msg{types.NewMsgReportData(1, []types.RawReport{}, testapp.Alice.ValAddress)} -// stubTx := &StubTx{Msgs: msgs} - -// // test - check report tx -// isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) -// suite.Require().False(isReportTx) - -// // test - check tx fee with min gas prices -// _, _, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) -// suite.Require().Error(err) -// } - -// func (suite *FeeCheckerTestSuite) TestValidReport() { -// reportMsgs := []sdk.Msg{ -// types.NewMsgReportData(suite.requestId, []types.RawReport{}, testapp.Validators[0].ValAddress), -// } -// authzMsg := authz.NewMsgExec(testapp.Alice.Address, reportMsgs) -// stubTx := &StubTx{Msgs: []sdk.Msg{&authzMsg}} - -// // test - check report tx -// isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) -// suite.Require().True(isReportTx) - -// // test - check tx fee with min gas prices -// fee, priority, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) -// suite.Require().NoError(err) -// suite.Require().Equal(sdk.Coins{}, fee) -// suite.Require().Equal(int64(math.MaxInt64), priority) -// } - -// func (suite *FeeCheckerTestSuite) TestNoAuthzReport() { -// reportMsgs := []sdk.Msg{ -// types.NewMsgReportData(suite.requestId, []types.RawReport{}, testapp.Validators[0].ValAddress), -// } -// authzMsg := authz.NewMsgExec(testapp.Bob.Address, reportMsgs) -// stubTx := &StubTx{Msgs: []sdk.Msg{&authzMsg}, GasPrices: sdk.NewDecCoins(sdk.NewDecCoin("uband", sdk.NewInt(1)))} - -// // test - check report tx -// isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) -// suite.Require().False(isReportTx) - -// // test - check tx fee with min gas prices -// _, _, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) -// suite.Require().NoError(err) -// } - -// func (suite *FeeCheckerTestSuite) TestNotValidReport() { -// reportMsgs := []sdk.Msg{ -// types.NewMsgReportData(suite.requestId+1, []types.RawReport{}, testapp.Validators[0].ValAddress), -// } -// authzMsg := authz.NewMsgExec(testapp.Alice.Address, reportMsgs) -// stubTx := &StubTx{Msgs: []sdk.Msg{&authzMsg}} - -// // test - check report tx -// isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) -// suite.Require().False(isReportTx) - -// // test - check tx fee with min gas prices -// _, _, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) -// suite.Require().Error(err) -// } - -// func (suite *FeeCheckerTestSuite) TestNotReportMsg() { -// requestMsg := types.NewMsgRequestData( -// 1, -// BasicCalldata, -// 1, -// 1, -// BasicClientID, -// testapp.Coins100000000uband, -// testapp.TestDefaultPrepareGas, -// testapp.TestDefaultExecuteGas, -// testapp.FeePayer.Address, -// ) -// stubTx := &StubTx{ -// Msgs: []sdk.Msg{requestMsg}, -// GasPrices: sdk.NewDecCoins( -// sdk.NewDecCoinFromDec("uaaaa", sdk.NewDecWithPrec(100, 3)), -// sdk.NewDecCoinFromDec("uaaab", sdk.NewDecWithPrec(1, 3)), -// sdk.NewDecCoinFromDec("uaaac", sdk.NewDecWithPrec(0, 3)), -// sdk.NewDecCoinFromDec("uband", sdk.NewDecWithPrec(3, 3)), -// sdk.NewDecCoinFromDec("uccca", sdk.NewDecWithPrec(0, 3)), -// sdk.NewDecCoinFromDec("ucccb", sdk.NewDecWithPrec(1, 3)), -// sdk.NewDecCoinFromDec("ucccc", sdk.NewDecWithPrec(100, 3)), -// ), -// } - -// // test - check report tx -// isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) -// suite.Require().False(isReportTx) - -// // test - check tx fee with min gas prices -// fee, priority, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) -// suite.Require().NoError(err) -// suite.Require().Equal(stubTx.GetFee(), fee) -// suite.Require().Equal(int64(30), priority) -// } - -// func (suite *FeeCheckerTestSuite) TestReportMsgAndOthersTypeMsgInTheSameAuthzMsgs() { -// reportMsg := types.NewMsgReportData(suite.requestId, []types.RawReport{}, testapp.Validators[0].ValAddress) -// requestMsg := types.NewMsgRequestData( -// 1, -// BasicCalldata, -// 1, -// 1, -// BasicClientID, -// testapp.Coins100000000uband, -// testapp.TestDefaultPrepareGas, -// testapp.TestDefaultExecuteGas, -// testapp.FeePayer.Address, -// ) -// msgs := []sdk.Msg{reportMsg, requestMsg} -// authzMsg := authz.NewMsgExec(testapp.Alice.Address, msgs) -// stubTx := &StubTx{Msgs: []sdk.Msg{&authzMsg}, GasPrices: sdk.NewDecCoins(sdk.NewDecCoin("uband", sdk.NewInt(1)))} - -// // test - check report tx -// isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) -// suite.Require().False(isReportTx) - -// // test - check tx fee with min gas prices -// fee, priority, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) -// suite.Require().NoError(err) -// suite.Require().Equal(stubTx.GetFee(), fee) -// suite.Require().Equal(int64(10000), priority) -// } - -// func (suite *FeeCheckerTestSuite) TestReportMsgAndOthersTypeMsgInTheSameTx() { -// reportMsg := types.NewMsgReportData(suite.requestId, []types.RawReport{}, testapp.Validators[0].ValAddress) -// requestMsg := types.NewMsgRequestData( -// 1, -// BasicCalldata, -// 1, -// 1, -// BasicClientID, -// testapp.Coins100000000uband, -// testapp.TestDefaultPrepareGas, -// testapp.TestDefaultExecuteGas, -// testapp.FeePayer.Address, -// ) -// stubTx := &StubTx{ -// Msgs: []sdk.Msg{reportMsg, requestMsg}, -// GasPrices: sdk.NewDecCoins(sdk.NewDecCoin("uband", sdk.NewInt(1))), -// } - -// // test - check report tx -// isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) -// suite.Require().False(isReportTx) - -// // test - check tx fee with min gas prices -// fee, priority, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) -// suite.Require().NoError(err) -// suite.Require().Equal(stubTx.GetFee(), fee) -// suite.Require().Equal(int64(10000), priority) -// } - -// func (suite *FeeCheckerTestSuite) TestGetBondDenom() { -// denom := suite.FeeChecker.GetBondDenom(suite.ctx) -// suite.Require().Equal("uband", denom) -// } - -// func (suite *FeeCheckerTestSuite) TestDefaultZeroGlobalFee() { -// coins, err := suite.FeeChecker.DefaultZeroGlobalFee(suite.ctx) - -// suite.Require().Equal(1, len(coins)) -// suite.Require().Equal("uband", coins[0].Denom) -// suite.Require().Equal(sdk.NewDec(0), coins[0].Amount) -// suite.Require().NoError(err) -// } - -// func TestFeeCheckerTestSuite(t *testing.T) { -// suite.Run(t, new(FeeCheckerTestSuite)) -// } +import ( + "math" + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/suite" + + "github.com/bandprotocol/chain/v2/testing/testapp" + "github.com/bandprotocol/chain/v2/x/globalfee/feechecker" + "github.com/bandprotocol/chain/v2/x/oracle/types" + "github.com/cosmos/cosmos-sdk/x/authz" +) + +var ( + BasicCalldata = []byte("BASIC_CALLDATA") + BasicClientID = "BASIC_CLIENT_ID" +) + +type StubTx struct { + sdk.Tx + sdk.FeeTx + Msgs []sdk.Msg + GasPrices sdk.DecCoins +} + +func (st *StubTx) GetMsgs() []sdk.Msg { + return st.Msgs +} + +func (st *StubTx) ValidateBasic() error { + return nil +} + +func (st *StubTx) GetGas() uint64 { + return 1000000 +} + +func (st *StubTx) GetFee() sdk.Coins { + fees := make(sdk.Coins, len(st.GasPrices)) + + // Determine the fees by multiplying each gas prices + glDec := sdk.NewDec(int64(st.GetGas())) + for i, gp := range st.GasPrices { + fee := gp.Amount.Mul(glDec) + fees[i] = sdk.NewCoin(gp.Denom, fee.Ceil().RoundInt()) + } + + return fees +} + +type FeeCheckerTestSuite struct { + suite.Suite + FeeChecker feechecker.FeeChecker + ctx sdk.Context + requestId types.RequestID +} + +func (suite *FeeCheckerTestSuite) SetupTest() { + app, ctx, oracleKeeper := testapp.CreateTestInput(true) + suite.ctx = ctx.WithBlockHeight(999). + WithIsCheckTx(true). + WithMinGasPrices(sdk.DecCoins{{Denom: "uband", Amount: sdk.NewDecWithPrec(1, 4)}}) + + oracleKeeper.GrantReporter(suite.ctx, testapp.Validators[0].ValAddress, testapp.Alice.Address) + + req := types.NewRequest( + 1, + BasicCalldata, + []sdk.ValAddress{testapp.Validators[0].ValAddress}, + 1, + 1, + testapp.ParseTime(0), + "", + nil, + nil, + 0, + ) + suite.requestId = oracleKeeper.AddRequest(suite.ctx, req) + + suite.FeeChecker = feechecker.NewFeeChecker( + &oracleKeeper, + &app.GlobalfeeKeeper, + app.StakingKeeper, + ) +} + +func (suite *FeeCheckerTestSuite) TestValidRawReport() { + msgs := []sdk.Msg{types.NewMsgReportData(suite.requestId, []types.RawReport{}, testapp.Validators[0].ValAddress)} + stubTx := &StubTx{Msgs: msgs} + + // test - check report tx + isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) + suite.Require().True(isReportTx) + + // test - check tx fee with min gas prices + fee, priority, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) + suite.Require().NoError(err) + suite.Require().Equal(sdk.Coins{}, fee) + suite.Require().Equal(int64(math.MaxInt64), priority) +} + +func (suite *FeeCheckerTestSuite) TestNotValidRawReport() { + msgs := []sdk.Msg{types.NewMsgReportData(1, []types.RawReport{}, testapp.Alice.ValAddress)} + stubTx := &StubTx{Msgs: msgs} + + // test - check report tx + isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) + suite.Require().False(isReportTx) + + // test - check tx fee with min gas prices + _, _, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) + suite.Require().Error(err) +} + +func (suite *FeeCheckerTestSuite) TestValidReport() { + reportMsgs := []sdk.Msg{ + types.NewMsgReportData(suite.requestId, []types.RawReport{}, testapp.Validators[0].ValAddress), + } + authzMsg := authz.NewMsgExec(testapp.Alice.Address, reportMsgs) + stubTx := &StubTx{Msgs: []sdk.Msg{&authzMsg}} + + // test - check report tx + isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) + suite.Require().True(isReportTx) + + // test - check tx fee with min gas prices + fee, priority, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) + suite.Require().NoError(err) + suite.Require().Equal(sdk.Coins{}, fee) + suite.Require().Equal(int64(math.MaxInt64), priority) +} + +func (suite *FeeCheckerTestSuite) TestNoAuthzReport() { + reportMsgs := []sdk.Msg{ + types.NewMsgReportData(suite.requestId, []types.RawReport{}, testapp.Validators[0].ValAddress), + } + authzMsg := authz.NewMsgExec(testapp.Bob.Address, reportMsgs) + stubTx := &StubTx{Msgs: []sdk.Msg{&authzMsg}, GasPrices: sdk.NewDecCoins(sdk.NewDecCoin("uband", sdk.NewInt(1)))} + + // test - check report tx + isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) + suite.Require().False(isReportTx) + + // test - check tx fee with min gas prices + _, _, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) + suite.Require().NoError(err) +} + +func (suite *FeeCheckerTestSuite) TestNotValidReport() { + reportMsgs := []sdk.Msg{ + types.NewMsgReportData(suite.requestId+1, []types.RawReport{}, testapp.Validators[0].ValAddress), + } + authzMsg := authz.NewMsgExec(testapp.Alice.Address, reportMsgs) + stubTx := &StubTx{Msgs: []sdk.Msg{&authzMsg}} + + // test - check report tx + isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) + suite.Require().False(isReportTx) + + // test - check tx fee with min gas prices + _, _, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) + suite.Require().Error(err) +} + +func (suite *FeeCheckerTestSuite) TestNotReportMsg() { + requestMsg := types.NewMsgRequestData( + 1, + BasicCalldata, + 1, + 1, + BasicClientID, + testapp.Coins100000000uband, + testapp.TestDefaultPrepareGas, + testapp.TestDefaultExecuteGas, + testapp.FeePayer.Address, + ) + stubTx := &StubTx{ + Msgs: []sdk.Msg{requestMsg}, + GasPrices: sdk.NewDecCoins( + sdk.NewDecCoinFromDec("uaaaa", sdk.NewDecWithPrec(100, 3)), + sdk.NewDecCoinFromDec("uaaab", sdk.NewDecWithPrec(1, 3)), + sdk.NewDecCoinFromDec("uaaac", sdk.NewDecWithPrec(0, 3)), + sdk.NewDecCoinFromDec("uband", sdk.NewDecWithPrec(3, 3)), + sdk.NewDecCoinFromDec("uccca", sdk.NewDecWithPrec(0, 3)), + sdk.NewDecCoinFromDec("ucccb", sdk.NewDecWithPrec(1, 3)), + sdk.NewDecCoinFromDec("ucccc", sdk.NewDecWithPrec(100, 3)), + ), + } + + // test - check report tx + isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) + suite.Require().False(isReportTx) + + // test - check tx fee with min gas prices + fee, priority, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) + suite.Require().NoError(err) + suite.Require().Equal(stubTx.GetFee(), fee) + suite.Require().Equal(int64(30), priority) +} + +func (suite *FeeCheckerTestSuite) TestReportMsgAndOthersTypeMsgInTheSameAuthzMsgs() { + reportMsg := types.NewMsgReportData(suite.requestId, []types.RawReport{}, testapp.Validators[0].ValAddress) + requestMsg := types.NewMsgRequestData( + 1, + BasicCalldata, + 1, + 1, + BasicClientID, + testapp.Coins100000000uband, + testapp.TestDefaultPrepareGas, + testapp.TestDefaultExecuteGas, + testapp.FeePayer.Address, + ) + msgs := []sdk.Msg{reportMsg, requestMsg} + authzMsg := authz.NewMsgExec(testapp.Alice.Address, msgs) + stubTx := &StubTx{Msgs: []sdk.Msg{&authzMsg}, GasPrices: sdk.NewDecCoins(sdk.NewDecCoin("uband", sdk.NewInt(1)))} + + // test - check report tx + isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) + suite.Require().False(isReportTx) + + // test - check tx fee with min gas prices + fee, priority, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) + suite.Require().NoError(err) + suite.Require().Equal(stubTx.GetFee(), fee) + suite.Require().Equal(int64(10000), priority) +} + +func (suite *FeeCheckerTestSuite) TestReportMsgAndOthersTypeMsgInTheSameTx() { + reportMsg := types.NewMsgReportData(suite.requestId, []types.RawReport{}, testapp.Validators[0].ValAddress) + requestMsg := types.NewMsgRequestData( + 1, + BasicCalldata, + 1, + 1, + BasicClientID, + testapp.Coins100000000uband, + testapp.TestDefaultPrepareGas, + testapp.TestDefaultExecuteGas, + testapp.FeePayer.Address, + ) + stubTx := &StubTx{ + Msgs: []sdk.Msg{reportMsg, requestMsg}, + GasPrices: sdk.NewDecCoins(sdk.NewDecCoin("uband", sdk.NewInt(1))), + } + + // test - check report tx + isReportTx := suite.FeeChecker.CheckReportTx(suite.ctx, stubTx) + suite.Require().False(isReportTx) + + // test - check tx fee with min gas prices + fee, priority, err := suite.FeeChecker.CheckTxFeeWithMinGasPrices(suite.ctx, stubTx) + suite.Require().NoError(err) + suite.Require().Equal(stubTx.GetFee(), fee) + suite.Require().Equal(int64(10000), priority) +} + +func (suite *FeeCheckerTestSuite) TestGetBondDenom() { + denom := suite.FeeChecker.GetBondDenom(suite.ctx) + suite.Require().Equal("uband", denom) +} + +func (suite *FeeCheckerTestSuite) TestDefaultZeroGlobalFee() { + coins, err := suite.FeeChecker.DefaultZeroGlobalFee(suite.ctx) + + suite.Require().Equal(1, len(coins)) + suite.Require().Equal("uband", coins[0].Denom) + suite.Require().Equal(sdk.NewDec(0), coins[0].Amount) + suite.Require().NoError(err) +} + +func TestFeeCheckerTestSuite(t *testing.T) { + suite.Run(t, new(FeeCheckerTestSuite)) +} diff --git a/x/globalfee/genesis_test.go b/x/globalfee/genesis_test.go index 559126332..78dd07b41 100644 --- a/x/globalfee/genesis_test.go +++ b/x/globalfee/genesis_test.go @@ -2,22 +2,10 @@ package globalfee import ( "testing" - "time" - dbm "github.com/cometbft/cometbft-db" - "github.com/cometbft/cometbft/libs/log" - tmproto "github.com/cometbft/cometbft/proto/tendermint/types" - "github.com/cosmos/cosmos-sdk/store" - storetypes "github.com/cosmos/cosmos-sdk/store/types" - sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - - "github.com/bandprotocol/chain/v2/x/globalfee/keeper" - "github.com/bandprotocol/chain/v2/x/globalfee/types" ) func TestDefaultGenesis(t *testing.T) { @@ -73,61 +61,3 @@ func TestValidateGenesis(t *testing.T) { }) } } - -func TestInitExportGenesis(t *testing.T) { - specs := map[string]struct { - src string - exp types.GenesisState - }{ - "single fee": { - src: `{"params":{"minimum_gas_prices":[{"denom":"ALX", "amount":"1"}]}}`, - exp: types.GenesisState{ - Params: types.Params{MinimumGasPrices: sdk.NewDecCoins(sdk.NewDecCoin("ALX", sdk.NewInt(1)))}, - }, - }, - "multiple fee options": { - src: `{"params":{"minimum_gas_prices":[{"denom":"ALX", "amount":"1"}, {"denom":"BLX", "amount":"0.001"}]}}`, - exp: types.GenesisState{ - Params: types.Params{MinimumGasPrices: sdk.NewDecCoins(sdk.NewDecCoin("ALX", sdk.NewInt(1)), - sdk.NewDecCoinFromDec("BLX", sdk.NewDecWithPrec(1, 3)))}, - }, - }, - "no fee set": { - src: `{"params":{}}`, - exp: types.GenesisState{Params: types.Params{MinimumGasPrices: sdk.DecCoins{}}}, - }, - } - for name, spec := range specs { - t.Run(name, func(t *testing.T) { - ctx, encCfg, keeper := setupTestStore(t) - m := NewAppModule(keeper) - m.InitGenesis(ctx, encCfg.Codec, []byte(spec.src)) - gotJSON := m.ExportGenesis(ctx, encCfg.Codec) - var got types.GenesisState - require.NoError(t, encCfg.Codec.UnmarshalJSON(gotJSON, &got)) - assert.Equal(t, spec.exp, got, string(gotJSON)) - }) - } -} - -func setupTestStore(t *testing.T) (sdk.Context, moduletestutil.TestEncodingConfig, keeper.Keeper) { - db := dbm.NewMemDB() - ms := store.NewCommitMultiStore(db) - encCfg := moduletestutil.MakeTestEncodingConfig() - storeKey := sdk.NewKVStoreKey(types.StoreKey) - ms.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db) - require.NoError(t, ms.LoadLatestVersion()) - - globalfeeKeeper := keeper.NewKeeper( - encCfg.Codec, - storeKey, - authtypes.NewModuleAddress(govtypes.ModuleName).String(), - ) - - ctx := sdk.NewContext(ms, tmproto.Header{ - Height: 1234567, - Time: time.Date(2020, time.April, 22, 12, 0, 0, 0, time.UTC), - }, false, log.NewNopLogger()) - - return ctx, encCfg, globalfeeKeeper -} diff --git a/x/globalfee/keeper/genesis.go b/x/globalfee/keeper/genesis.go new file mode 100644 index 000000000..330402d92 --- /dev/null +++ b/x/globalfee/keeper/genesis.go @@ -0,0 +1,20 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/bandprotocol/chain/v2/x/globalfee/types" +) + +// InitGenesis new globalfee genesis +func (keeper Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) { + if err := keeper.SetParams(ctx, data.Params); err != nil { + panic(err) + } +} + +// ExportGenesis returns a GenesisState for a given context. +func (keeper Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { + params := keeper.GetParams(ctx) + return types.NewGenesisState(params) +} diff --git a/x/globalfee/keeper/genesis_test.go b/x/globalfee/keeper/genesis_test.go new file mode 100644 index 000000000..c24285b1d --- /dev/null +++ b/x/globalfee/keeper/genesis_test.go @@ -0,0 +1,81 @@ +package keeper_test + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/codec" + storetypes "github.com/cosmos/cosmos-sdk/store/types" + "github.com/cosmos/cosmos-sdk/testutil" + sdk "github.com/cosmos/cosmos-sdk/types" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + "github.com/stretchr/testify/suite" + + "github.com/bandprotocol/chain/v2/x/globalfee" + "github.com/bandprotocol/chain/v2/x/globalfee/keeper" + "github.com/bandprotocol/chain/v2/x/globalfee/types" +) + +type GenesisTestSuite struct { + suite.Suite + + sdkCtx sdk.Context + keeper keeper.Keeper + cdc codec.BinaryCodec + key *storetypes.KVStoreKey +} + +func TestGenesisTestSuite(t *testing.T) { + suite.Run(t, new(GenesisTestSuite)) +} + +func (s *GenesisTestSuite) SetupTest() { + key := sdk.NewKVStoreKey(types.StoreKey) + testCtx := testutil.DefaultContextWithDB(s.T(), key, sdk.NewTransientStoreKey("transient_test")) + encCfg := moduletestutil.MakeTestEncodingConfig(globalfee.AppModuleBasic{}) + + // gomock initializations + s.cdc = codec.NewProtoCodec(encCfg.InterfaceRegistry) + s.sdkCtx = testCtx.Ctx + s.key = key + + s.keeper = keeper.NewKeeper(s.cdc, key, authtypes.NewModuleAddress(govtypes.ModuleName).String()) +} + +func (s *GenesisTestSuite) TestImportExportGenesis() { + specs := map[string]struct { + src string + exp types.GenesisState + }{ + "single fee": { + src: `{"params":{"minimum_gas_prices":[{"denom":"ALX", "amount":"1"}]}}`, + exp: types.GenesisState{ + Params: types.Params{MinimumGasPrices: sdk.NewDecCoins(sdk.NewDecCoin("ALX", sdk.NewInt(1)))}, + }, + }, + "multiple fee options": { + src: `{"params":{"minimum_gas_prices":[{"denom":"ALX", "amount":"1"}, {"denom":"BLX", "amount":"0.001"}]}}`, + exp: types.GenesisState{ + Params: types.Params{MinimumGasPrices: sdk.NewDecCoins(sdk.NewDecCoin("ALX", sdk.NewInt(1)), + sdk.NewDecCoinFromDec("BLX", sdk.NewDecWithPrec(1, 3)))}, + }, + }, + "no fee set": { + src: `{"params":{}}`, + exp: types.GenesisState{Params: types.Params{MinimumGasPrices: nil}}, + }, + } + for name, spec := range specs { + s.Run(name, func() { + genesisState := &spec.exp + s.keeper.InitGenesis(s.sdkCtx, genesisState) + + params := s.keeper.GetParams(s.sdkCtx) + s.Require().Equal(genesisState.Params, params) + + genesisState2 := s.keeper.ExportGenesis(s.sdkCtx) + s.Require().Equal(genesisState, genesisState2) + }) + } +} diff --git a/x/globalfee/keeper/grpc_query.go b/x/globalfee/keeper/grpc_query.go index 7d55656c0..e3306ee21 100644 --- a/x/globalfee/keeper/grpc_query.go +++ b/x/globalfee/keeper/grpc_query.go @@ -15,11 +15,9 @@ type Querier struct { } // Params return parameters of globalfee module -func (q Querier) Params( - stdCtx context.Context, - _ *types.QueryParamsRequest, -) (*types.QueryParamsResponse, error) { +func (q Querier) Params(stdCtx context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) { ctx := sdk.UnwrapSDKContext(stdCtx) + return &types.QueryParamsResponse{ Params: q.GetParams(ctx), }, nil diff --git a/x/globalfee/keeper/grpc_query_test.go b/x/globalfee/keeper/grpc_query_test.go index db240dc1b..39aa29be7 100644 --- a/x/globalfee/keeper/grpc_query_test.go +++ b/x/globalfee/keeper/grpc_query_test.go @@ -1,59 +1,73 @@ -package keeper +package keeper_test -// import ( -// "testing" +import ( + "testing" -// sdk "github.com/cosmos/cosmos-sdk/types" -// paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" -// "github.com/stretchr/testify/assert" -// "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/testutil" + sdk "github.com/cosmos/cosmos-sdk/types" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" -// "github.com/bandprotocol/chain/v2/x/globalfee/types" -// ) + "github.com/bandprotocol/chain/v2/x/globalfee/keeper" + "github.com/bandprotocol/chain/v2/x/globalfee/types" +) -// func TestQueryMinimumGasPrices(t *testing.T) { -// specs := map[string]struct { -// setupStore func(ctx sdk.Context, s paramtypes.Subspace) -// expMin sdk.DecCoins -// }{ -// "one coin": { -// setupStore: func(ctx sdk.Context, s paramtypes.Subspace) { -// s.SetParamSet(ctx, &types.Params{ -// MinimumGasPrices: sdk.NewDecCoins(sdk.NewDecCoin("ALX", sdk.OneInt())), -// }) -// }, -// expMin: sdk.NewDecCoins(sdk.NewDecCoin("ALX", sdk.OneInt())), -// }, -// "multiple coins": { -// setupStore: func(ctx sdk.Context, s paramtypes.Subspace) { -// s.SetParamSet(ctx, &types.Params{ -// MinimumGasPrices: sdk.NewDecCoins( -// sdk.NewDecCoin("ALX", sdk.OneInt()), -// sdk.NewDecCoin("BLX", sdk.NewInt(2)), -// ), -// }) -// }, -// expMin: sdk.NewDecCoins(sdk.NewDecCoin("ALX", sdk.OneInt()), sdk.NewDecCoin("BLX", sdk.NewInt(2))), -// }, -// "no min gas price set": { -// setupStore: func(ctx sdk.Context, s paramtypes.Subspace) { -// s.SetParamSet(ctx, &types.Params{}) -// }, -// }, -// "no param set": { -// setupStore: func(ctx sdk.Context, s paramtypes.Subspace) { -// }, -// }, -// } -// for name, spec := range specs { -// t.Run(name, func(t *testing.T) { -// ctx, _, subspace := setupTestStore(t) -// spec.setupStore(ctx, subspace) -// q := NewGrpcQuerier(subspace) -// gotResp, gotErr := q.MinimumGasPrices(sdk.WrapSDKContext(ctx), nil) -// require.NoError(t, gotErr) -// require.NotNil(t, gotResp) -// assert.Equal(t, spec.expMin, gotResp.MinimumGasPrices) -// }) -// } -// } +func TestQueryParams(t *testing.T) { + specs := map[string]struct { + setupStore func(ctx sdk.Context, k keeper.Keeper) + expMin sdk.DecCoins + }{ + "one coin": { + setupStore: func(ctx sdk.Context, k keeper.Keeper) { + k.SetParams(ctx, types.Params{ + MinimumGasPrices: sdk.NewDecCoins(sdk.NewDecCoin("ALX", sdk.OneInt())), + }) + }, + expMin: sdk.NewDecCoins(sdk.NewDecCoin("ALX", sdk.OneInt())), + }, + "multiple coins": { + setupStore: func(ctx sdk.Context, k keeper.Keeper) { + k.SetParams(ctx, types.Params{ + MinimumGasPrices: sdk.NewDecCoins( + sdk.NewDecCoin("ALX", sdk.OneInt()), + sdk.NewDecCoin("BLX", sdk.NewInt(2)), + ), + }) + }, + expMin: sdk.NewDecCoins(sdk.NewDecCoin("ALX", sdk.OneInt()), sdk.NewDecCoin("BLX", sdk.NewInt(2))), + }, + "no min gas price set": { + setupStore: func(ctx sdk.Context, k keeper.Keeper) { + k.SetParams(ctx, types.Params{}) + }, + }, + "no param set": { + setupStore: func(ctx sdk.Context, k keeper.Keeper) { + }, + }, + } + for name, spec := range specs { + t.Run(name, func(t *testing.T) { + encCfg := moduletestutil.MakeTestEncodingConfig() + key := sdk.NewKVStoreKey(types.StoreKey) + ctx := testutil.DefaultContextWithDB(t, key, sdk.NewTransientStoreKey("transient_test")).Ctx + + k := keeper.NewKeeper( + encCfg.Codec, + key, + authtypes.NewModuleAddress(govtypes.ModuleName).String(), + ) + + q := keeper.Querier{Keeper: k} + spec.setupStore(ctx, k) + gotResp, gotErr := q.Params(sdk.WrapSDKContext(ctx), nil) + + require.NoError(t, gotErr) + require.NotNil(t, gotResp) + assert.Equal(t, spec.expMin, gotResp.Params.MinimumGasPrices) + }) + } +} diff --git a/x/globalfee/module.go b/x/globalfee/module.go index 503b1447d..0a130f8b7 100644 --- a/x/globalfee/module.go +++ b/x/globalfee/module.go @@ -20,7 +20,7 @@ import ( ) // ConsensusVersion defines the current x/globalfee module consensus version. -const ConsensusVersion = 2 +const ConsensusVersion = 1 var ( _ module.AppModuleBasic = AppModuleBasic{} @@ -35,10 +35,18 @@ func (a AppModuleBasic) Name() string { return types.ModuleName } +// RegisterLegacyAminoCodec registers the mint module's types on the given LegacyAmino codec. +func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + types.RegisterLegacyAminoCodec(cdc) +} + +// RegisterInterfaces registers the module's interface types +func (b AppModuleBasic) RegisterInterfaces(r codectypes.InterfaceRegistry) { + types.RegisterInterfaces(r) +} + func (a AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { - return cdc.MustMarshalJSON(&types.GenesisState{ - Params: types.DefaultParams(), - }) + return cdc.MustMarshalJSON(types.DefaultGenesisState()) } func (a AppModuleBasic) ValidateGenesis( @@ -51,14 +59,12 @@ func (a AppModuleBasic) ValidateGenesis( if err != nil { return err } + if err := data.Params.ValidateBasic(); err != nil { return sdkerrors.Wrap(err, "params") } - return nil -} -func (a AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) { - types.RegisterInterfaces(registry) + return nil } func (a AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { @@ -73,12 +79,9 @@ func (a AppModuleBasic) GetQueryCmd() *cobra.Command { return cli.GetQueryCmd() } -func (a AppModuleBasic) RegisterLegacyAminoCodec(amino *codec.LegacyAmino) { - types.RegisterLegacyAminoCodec(amino) -} - type AppModule struct { AppModuleBasic + keeper keeper.Keeper } @@ -94,14 +97,14 @@ func (a AppModule) InitGenesis( ) []abci.ValidatorUpdate { var genesisState types.GenesisState marshaler.MustUnmarshalJSON(message, &genesisState) - a.keeper.SetParams(ctx, genesisState.Params) - return nil + + a.keeper.InitGenesis(ctx, &genesisState) + return []abci.ValidatorUpdate{} } func (a AppModule) ExportGenesis(ctx sdk.Context, marshaler codec.JSONCodec) json.RawMessage { - var genState types.GenesisState - genState.Params = a.keeper.GetParams(ctx) - return marshaler.MustMarshalJSON(&genState) + genState := a.keeper.ExportGenesis(ctx) + return marshaler.MustMarshalJSON(genState) } func (a AppModule) RegisterInvariants(registry sdk.InvariantRegistry) { diff --git a/x/globalfee/types/params.go b/x/globalfee/types/params.go index d864c4a83..403e3c2df 100644 --- a/x/globalfee/types/params.go +++ b/x/globalfee/types/params.go @@ -5,6 +5,13 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) +// NewParams returns Params instance with the given values. +func NewParams(minimumGasPrices sdk.DecCoins) Params { + return Params{ + MinimumGasPrices: minimumGasPrices, + } +} + // DefaultParams returns default parameters func DefaultParams() Params { return Params{MinimumGasPrices: sdk.DecCoins{}}