From c4ba9751818eb201157f7ca4f1ecf0537782db33 Mon Sep 17 00:00:00 2001 From: Spoorthi Satheesha <9302666+spoo-bar@users.noreply.github.com> Date: Wed, 1 Mar 2023 10:59:09 +0000 Subject: [PATCH 1/6] replacing sdk mint module with our x/mint --- app/app.go | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/app/app.go b/app/app.go index e8f2f129..4501b277 100644 --- a/app/app.go +++ b/app/app.go @@ -60,9 +60,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/gov" govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" - "github.com/cosmos/cosmos-sdk/x/mint" - mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper" - minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" "github.com/cosmos/cosmos-sdk/x/params" paramsclient "github.com/cosmos/cosmos-sdk/x/params/client" paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper" @@ -99,9 +96,11 @@ import ( dbm "github.com/tendermint/tm-db" "github.com/archway-network/archway/wasmbinding" + "github.com/archway-network/archway/x/mint" + mintkeeper "github.com/archway-network/archway/x/mint/keeper" + minttypes "github.com/archway-network/archway/x/mint/types" "github.com/archway-network/archway/x/rewards" rewardsKeeper "github.com/archway-network/archway/x/rewards/keeper" - "github.com/archway-network/archway/x/rewards/mintbankkeeper" rewardsTypes "github.com/archway-network/archway/x/rewards/types" "github.com/archway-network/archway/x/tracking" trackingKeeper "github.com/archway-network/archway/x/tracking/keeper" @@ -523,10 +522,6 @@ func NewArchwayApp( appCodec, keys[minttypes.StoreKey], app.getSubspace(minttypes.ModuleName), - &stakingKeeper, - app.AccountKeeper, - mintbankkeeper.NewKeeper(app.BankKeeper, app.RewardsKeeper), - authtypes.FeeCollectorName, ) // The gov proposal types can be individually enabled @@ -565,7 +560,7 @@ func NewArchwayApp( bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), capability.NewAppModule(appCodec, *app.CapabilityKeeper), gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper), - mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper), + mint.NewAppModule(appCodec, app.MintKeeper), slashing.NewAppModule(appCodec, app.slashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper), @@ -696,7 +691,7 @@ func NewArchwayApp( feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry), authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry), gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper), - mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper), + mint.NewAppModule(appCodec, app.MintKeeper), staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper), distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), slashing.NewAppModule(appCodec, app.slashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), From 6fab8618007b73794ec73b9aa423b530521c3f66 Mon Sep 17 00:00:00 2001 From: Spoorthi Satheesha <9302666+spoo-bar@users.noreply.github.com> Date: Thu, 2 Mar 2023 13:04:55 +0000 Subject: [PATCH 2/6] replacing sdk/mint with x/mint --- app/app.go | 3 ++- app/sim_test.go | 2 +- app/test_helpers.go | 2 +- e2e/gastracking_test.go | 14 +++++--------- e2e/rewards_test.go | 17 ++++++++--------- e2e/testing/chain_options.go | 8 +++----- e2e/txfees_test.go | 13 ++++++------- wasmbinding/rewards/common_test.go | 4 ++-- x/mint/genesis.go | 8 +++++--- x/mint/keeper/common_test.go | 2 +- x/mint/keeper/keeper.go | 9 ++++++++- x/mint/keeper/minter.go | 11 +++++++++++ x/mint/types/expected_keepers.go | 11 +++++++++++ x/rewards/ante/fee_deduction_test.go | 2 +- x/rewards/keeper/common_test.go | 4 ++-- x/rewards/keeper/distribution_test.go | 2 +- x/rewards/keeper/invariants_test.go | 2 +- x/rewards/mintbankkeeper/keeper_test.go | 4 ++-- 18 files changed, 71 insertions(+), 47 deletions(-) create mode 100644 x/mint/keeper/minter.go diff --git a/app/app.go b/app/app.go index 4501b277..2d1d9d9b 100644 --- a/app/app.go +++ b/app/app.go @@ -522,6 +522,7 @@ func NewArchwayApp( appCodec, keys[minttypes.StoreKey], app.getSubspace(minttypes.ModuleName), + app.BankKeeper, ) // The gov proposal types can be individually enabled @@ -876,7 +877,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino paramsKeeper.Subspace(authtypes.ModuleName) paramsKeeper.Subspace(banktypes.ModuleName) paramsKeeper.Subspace(stakingtypes.ModuleName) - paramsKeeper.Subspace(minttypes.ModuleName) + paramsKeeper.Subspace(minttypes.ModuleName).WithKeyTable(minttypes.ParamKeyTable()) paramsKeeper.Subspace(distrtypes.ModuleName) paramsKeeper.Subspace(slashingtypes.ModuleName) paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govtypes.ParamKeyTable()) diff --git a/app/sim_test.go b/app/sim_test.go index 97efe19d..5f2a5926 100644 --- a/app/sim_test.go +++ b/app/sim_test.go @@ -7,6 +7,7 @@ import ( "path/filepath" "testing" + minttypes "github.com/archway-network/archway/x/mint/types" "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" @@ -20,7 +21,6 @@ import ( evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types" "github.com/cosmos/cosmos-sdk/x/feegrant" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" - minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" "github.com/cosmos/cosmos-sdk/x/simulation" slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" diff --git a/app/test_helpers.go b/app/test_helpers.go index a42d4e83..1787b298 100644 --- a/app/test_helpers.go +++ b/app/test_helpers.go @@ -9,8 +9,8 @@ import ( "testing" "time" + minttypes "github.com/archway-network/archway/x/mint/types" bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" - minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" diff --git a/e2e/gastracking_test.go b/e2e/gastracking_test.go index d669db4f..fedf1318 100644 --- a/e2e/gastracking_test.go +++ b/e2e/gastracking_test.go @@ -13,6 +13,7 @@ import ( e2eTesting "github.com/archway-network/archway/e2e/testing" "github.com/archway-network/archway/pkg" + mintTypes "github.com/archway-network/archway/x/mint/types" rewardsTypes "github.com/archway-network/archway/x/rewards/types" trackingTypes "github.com/archway-network/archway/x/tracking/types" ) @@ -28,6 +29,8 @@ func (s *E2ETestSuite) TestGasTrackingAndRewardsDistribution() { txFeeRebateRewardsRatio := sdk.NewDecWithPrec(5, 1) inflationRewardsRatio := sdk.NewDecWithPrec(5, 1) blockGasLimit := int64(10_000_000) + mintParams := mintTypes.DefaultParams() + mintParams.MinInflation = sdk.OneDec() // Setup (create new chain here with custom params) chain := e2eTesting.NewTestChain(s.T(), 1, @@ -35,11 +38,7 @@ func (s *E2ETestSuite) TestGasTrackingAndRewardsDistribution() { e2eTesting.WithInflationRewardsRatio(inflationRewardsRatio), e2eTesting.WithBlockGasLimit(blockGasLimit), // Artificially increase the minted inflation coin to get some rewards for the contract (otherwise contractOp gas / blockGasLimit ratio will be 0) - e2eTesting.WithMintParams( - sdk.NewDecWithPrec(8, 1), - sdk.NewDecWithPrec(8, 1), - 1000000, - ), + e2eTesting.WithMintParams(mintParams), // Set default Tx fee for non-manual transaction like Upload / Instantiate e2eTesting.WithDefaultFeeAmount("10000"), ) @@ -116,10 +115,7 @@ func (s *E2ETestSuite) TestGasTrackingAndRewardsDistribution() { { ctx := chain.GetContext() - mintKeeper := chain.GetApp().MintKeeper - mintParams := mintKeeper.GetParams(ctx) - - mintedCoin := chain.GetApp().MintKeeper.GetMinter(ctx).BlockProvision(mintParams) + mintedCoin, _ := chain.GetApp().MintKeeper.GetBlockProvisions(ctx) inflationRewards, _ := pkg.SplitCoins(sdk.NewCoins(mintedCoin), inflationRewardsRatio) s.Require().Len(inflationRewards, 1) blockInflationRewardsExpected = inflationRewards[0] diff --git a/e2e/rewards_test.go b/e2e/rewards_test.go index 8544392a..cbf9af2c 100644 --- a/e2e/rewards_test.go +++ b/e2e/rewards_test.go @@ -4,9 +4,9 @@ import ( "time" wasmdTypes "github.com/CosmWasm/wasmd/x/wasm/types" + mintTypes "github.com/archway-network/archway/x/mint/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/query" - mintTypes "github.com/cosmos/cosmos-sdk/x/mint/types" voterCustomTypes "github.com/archway-network/voter/src/pkg/archway/custom" voterTypes "github.com/archway-network/voter/src/types" @@ -25,6 +25,9 @@ func (s *E2ETestSuite) TestRewardsWithdrawProfitAndFees() { batchIncStep = 300 // withdraw batch size increment batchStartSize = 100 // withdraw batch size start value ) + mintParams := mintTypes.DefaultParams() + mintParams.MinInflation = sdk.MustNewDecFromStr("0.1") // 10% + mintParams.MaxInflation = sdk.MustNewDecFromStr("0.1") // 10% // Create a custom chain with "close to mainnet" params chain := e2eTesting.NewTestChain(s.T(), 1, @@ -41,11 +44,7 @@ func (s *E2ETestSuite) TestRewardsWithdrawProfitAndFees() { e2eTesting.WithTxFeeRebatesRewardsRatio(sdk.NewDecWithPrec(5, 1)), e2eTesting.WithInflationRewardsRatio(sdk.NewDecWithPrec(2, 1)), // Set constant inflation rate - e2eTesting.WithMintParams( - sdk.NewDecWithPrec(10, 2), // 10% - sdk.NewDecWithPrec(10, 2), // 10% - uint64(60*60*8766/1), // 1 seconds block time - ), + e2eTesting.WithMintParams(mintParams), ) trackingKeeper, rewardsKeeper := chain.GetApp().TrackingKeeper, chain.GetApp().RewardsKeeper chain.NextBlock(0) @@ -170,7 +169,7 @@ func (s *E2ETestSuite) TestRewardsWithdrawProfitAndFees() { } // Mint rewards coins - s.Require().NoError(chain.GetApp().MintKeeper.MintCoins(ctx, coinsToMint)) + s.Require().NoError(chain.GetApp().MintKeeper.MintCoins(ctx, mintTypes.ModuleName, coinsToMint)) s.Require().NoError(chain.GetApp().BankKeeper.SendCoinsFromModuleToModule(ctx, mintTypes.ModuleName, rewardsTypes.ContractRewardCollector, coinsToMint)) // Invariants check (just in case) @@ -273,7 +272,7 @@ func (s *E2ETestSuite) TestRewardsParamMaxWithdrawRecordsLimit() { } mintCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewIntFromUint64(rewardsTypes.MaxWithdrawRecordsParamLimit))) - s.Require().NoError(mintKeeper.MintCoins(ctx, mintCoins)) + s.Require().NoError(mintKeeper.MintCoins(ctx, mintTypes.ModuleName, mintCoins)) s.Require().NoError(bankKeeper.SendCoinsFromModuleToModule(ctx, mintTypes.ModuleName, rewardsTypes.ContractRewardCollector, mintCoins)) } @@ -347,7 +346,7 @@ func (s *E2ETestSuite) TestRewardsRecordsQueryLimit() { } mintCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewIntFromUint64(rewardsTypes.MaxRecordsQueryLimit))) - s.Require().NoError(mintKeeper.MintCoins(ctx, mintCoins)) + s.Require().NoError(mintKeeper.MintCoins(ctx, mintTypes.ModuleName, mintCoins)) s.Require().NoError(bankKeeper.SendCoinsFromModuleToModule(ctx, mintTypes.ModuleName, rewardsTypes.ContractRewardCollector, mintCoins)) recordsExpected = records diff --git a/e2e/testing/chain_options.go b/e2e/testing/chain_options.go index 25021772..49712704 100644 --- a/e2e/testing/chain_options.go +++ b/e2e/testing/chain_options.go @@ -1,9 +1,9 @@ package e2eTesting import ( + mintTypes "github.com/archway-network/archway/x/mint/types" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" - mintTypes "github.com/cosmos/cosmos-sdk/x/mint/types" abci "github.com/tendermint/tendermint/abci/types" "github.com/archway-network/archway/app" @@ -118,14 +118,12 @@ func WithTxFeeRebatesRewardsRatio(ratio sdk.Dec) TestChainGenesisOption { } // WithMintParams sets x/mint inflation calculation parameters. -func WithMintParams(inflationMin, inflationMax sdk.Dec, blocksPerYear uint64) TestChainGenesisOption { +func WithMintParams(mintParams mintTypes.Params) TestChainGenesisOption { return func(cdc codec.Codec, genesis app.GenesisState) { var mintGenesis mintTypes.GenesisState cdc.MustUnmarshalJSON(genesis[mintTypes.ModuleName], &mintGenesis) - mintGenesis.Params.InflationMin = inflationMin - mintGenesis.Params.InflationMax = inflationMax - mintGenesis.Params.BlocksPerYear = blocksPerYear + mintGenesis.Params = mintParams genesis[mintTypes.ModuleName] = cdc.MustMarshalJSON(&mintGenesis) } diff --git a/e2e/txfees_test.go b/e2e/txfees_test.go index 17fdb056..301a43e3 100644 --- a/e2e/txfees_test.go +++ b/e2e/txfees_test.go @@ -13,6 +13,7 @@ import ( voterTypes "github.com/archway-network/voter/src/types" e2eTesting "github.com/archway-network/archway/e2e/testing" + mintTypes "github.com/archway-network/archway/x/mint/types" rewardsTypes "github.com/archway-network/archway/x/rewards/types" ) @@ -34,6 +35,9 @@ func (s *E2ETestSuite) TestTxFees() { } return fmt.Sprintf("%8s", e2eTesting.HumanizeDecCoins(0, coin)) } + mintParams := mintTypes.DefaultParams() + mintParams.MinInflation = sdk.MustNewDecFromStr("0.1") // 10% (Archway mainnet param) + mintParams.MaxInflation = sdk.MustNewDecFromStr("0.1") // 10% (Archway mainnet param) // Create a custom chain with fixed inflation (10%) and 10M block gas limit chain := e2eTesting.NewTestChain(s.T(), 1, @@ -50,11 +54,7 @@ func (s *E2ETestSuite) TestTxFees() { e2eTesting.WithTxFeeRebatesRewardsRatio(sdk.NewDecWithPrec(5, 1)), // 50 % (Archway mainnet param) e2eTesting.WithInflationRewardsRatio(sdk.NewDecWithPrec(2, 1)), // 20 % (Archway mainnet param) // Set constant inflation rate - e2eTesting.WithMintParams( - sdk.NewDecWithPrec(10, 2), // 10% (Archway mainnet param) - sdk.NewDecWithPrec(10, 2), // 10% (Archway mainnet param) - uint64(60*60*8766/1), //1 seconds block time (Archway mainnet param) - ), + e2eTesting.WithMintParams(mintParams), ) // Check total supply @@ -99,8 +99,7 @@ func (s *E2ETestSuite) TestTxFees() { { ctx := chain.GetContext() - mintParams := chain.GetApp().MintKeeper.GetParams(ctx) - mintedCoin := chain.GetApp().MintKeeper.GetMinter(ctx).BlockProvision(mintParams) + mintedCoin, _ := chain.GetApp().MintKeeper.GetBlockProvisions(ctx) s.T().Logf("x/mint minted amount per block: %s", coinsToStr(mintedCoin)) } diff --git a/wasmbinding/rewards/common_test.go b/wasmbinding/rewards/common_test.go index 3d1951e6..fc8d4eff 100644 --- a/wasmbinding/rewards/common_test.go +++ b/wasmbinding/rewards/common_test.go @@ -6,8 +6,8 @@ import ( "time" "github.com/CosmWasm/wasmvm/types" + mintTypes "github.com/archway-network/archway/x/mint/types" sdk "github.com/cosmos/cosmos-sdk/types" - mintTypes "github.com/cosmos/cosmos-sdk/x/mint/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -241,7 +241,7 @@ func TestRewardsWASMBindings(t *testing.T) { keeper.GetState().RewardsRecord(ctx).CreateRewardsRecord(contractAddr, record1RewardsExpected, ctx.BlockHeight(), ctx.BlockTime()) keeper.GetState().RewardsRecord(ctx).CreateRewardsRecord(contractAddr, record2RewardsExpected, ctx.BlockHeight(), ctx.BlockTime()) keeper.GetState().RewardsRecord(ctx).CreateRewardsRecord(contractAddr, record3RewardsExpected, ctx.BlockHeight(), ctx.BlockTime()) - require.NoError(t, chain.GetApp().MintKeeper.MintCoins(ctx, recordsRewards)) + require.NoError(t, chain.GetApp().MintKeeper.MintCoins(ctx, mintTypes.ModuleName, recordsRewards)) require.NoError(t, chain.GetApp().BankKeeper.SendCoinsFromModuleToModule(ctx, mintTypes.ModuleName, rewardsTypes.ContractRewardCollector, recordsRewards)) // Query available rewards diff --git a/x/mint/genesis.go b/x/mint/genesis.go index 0c2a792b..a7149432 100644 --- a/x/mint/genesis.go +++ b/x/mint/genesis.go @@ -11,13 +11,15 @@ import ( func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { k.SetParams(ctx, genState.GetParams()) lbi := genState.GetLastBlockInfo() - if (lbi == types.LastBlockInfo{}) { - time := ctx.BlockTime() + if (lbi.Inflation == sdk.Dec{}) { lbi = types.LastBlockInfo{ Inflation: genState.Params.MinInflation, - Time: &time, } } + if lbi.Time == nil { + time := ctx.BlockTime() + lbi.Time = &time + } if err := k.SetLastBlockInfo(ctx, lbi); err != nil { panic(err) } diff --git a/x/mint/keeper/common_test.go b/x/mint/keeper/common_test.go index a53bb4f2..c7c25f5a 100644 --- a/x/mint/keeper/common_test.go +++ b/x/mint/keeper/common_test.go @@ -41,7 +41,7 @@ func SetupTestMintKeeper(t testing.TB) (keeper.Keeper, sdk.Context) { paramsKeeper.Subspace(types.ModuleName) subspace, _ := paramsKeeper.GetSubspace(types.ModuleName) - k := keeper.NewKeeper(marshaler, storeKey, subspace) + k := keeper.NewKeeper(marshaler, storeKey, subspace, nil) ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger()) return k, ctx diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index 83b4951e..70b9e63f 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -14,14 +14,16 @@ type Keeper struct { cdc codec.Codec paramStore paramTypes.Subspace storeKey sdk.StoreKey + bankKeeper types.BankKeeper } // NewKeeper creates a new Keeper instance. -func NewKeeper(cdc codec.Codec, storeKey sdk.StoreKey, ps paramTypes.Subspace) Keeper { +func NewKeeper(cdc codec.Codec, storeKey sdk.StoreKey, ps paramTypes.Subspace, bk types.BankKeeper) Keeper { return Keeper{ cdc: cdc, storeKey: storeKey, paramStore: ps, + bankKeeper: bk, } } @@ -29,3 +31,8 @@ func NewKeeper(cdc codec.Codec, storeKey sdk.StoreKey, ps paramTypes.Subspace) K func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", "x/"+types.ModuleName) } + +// MintCoins creates new coins from thin air and adds it to the given module account. +func (k Keeper) MintCoins(ctx sdk.Context, name string, amt sdk.Coins) error { + return k.bankKeeper.MintCoins(ctx, name, amt) +} diff --git a/x/mint/keeper/minter.go b/x/mint/keeper/minter.go new file mode 100644 index 00000000..72af9cbb --- /dev/null +++ b/x/mint/keeper/minter.go @@ -0,0 +1,11 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// GetBlockProvisions gets the tokens to be minted in the current block and returns the new inflation amount as well +func (k Keeper) GetBlockProvisions(ctx sdk.Context) (sdk.Coin, sdk.Dec) { + // todo: put the begin blocker mint amount calculation here + panic("unimplemented 👻") +} diff --git a/x/mint/types/expected_keepers.go b/x/mint/types/expected_keepers.go index ab1254f4..c51b85dd 100644 --- a/x/mint/types/expected_keepers.go +++ b/x/mint/types/expected_keepers.go @@ -1 +1,12 @@ package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// BankKeeper defines the contract needed to be fulfilled for banking and supply +// dependencies. +type BankKeeper interface { + // MintCoins makes the money printer go brrr and adds it to the module account. + MintCoins(ctx sdk.Context, name string, amt sdk.Coins) error +} diff --git a/x/rewards/ante/fee_deduction_test.go b/x/rewards/ante/fee_deduction_test.go index 0403958d..237c26fb 100644 --- a/x/rewards/ante/fee_deduction_test.go +++ b/x/rewards/ante/fee_deduction_test.go @@ -4,9 +4,9 @@ import ( "testing" wasmdTypes "github.com/CosmWasm/wasmd/x/wasm/types" + mintTypes "github.com/archway-network/archway/x/mint/types" sdk "github.com/cosmos/cosmos-sdk/types" authTypes "github.com/cosmos/cosmos-sdk/x/auth/types" - mintTypes "github.com/cosmos/cosmos-sdk/x/mint/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/x/rewards/keeper/common_test.go b/x/rewards/keeper/common_test.go index 317b6310..eed98d07 100644 --- a/x/rewards/keeper/common_test.go +++ b/x/rewards/keeper/common_test.go @@ -3,8 +3,8 @@ package keeper_test import ( "testing" + mintTypes "github.com/archway-network/archway/x/mint/types" sdk "github.com/cosmos/cosmos-sdk/types" - mintTypes "github.com/cosmos/cosmos-sdk/x/mint/types" "github.com/stretchr/testify/suite" e2eTesting "github.com/archway-network/archway/e2e/testing" @@ -42,7 +42,7 @@ func (s *KeeperTestSuite) SetupWithdrawTest(testData []withdrawTestRecordData) { // Mint rewards for the current record rewardsToMint := testRecord.Rewards - s.Require().NoError(s.chain.GetApp().MintKeeper.MintCoins(ctx, rewardsToMint)) + s.Require().NoError(s.chain.GetApp().MintKeeper.MintCoins(ctx, mintTypes.ModuleName, rewardsToMint)) s.Require().NoError(s.chain.GetApp().BankKeeper.SendCoinsFromModuleToModule(ctx, mintTypes.ModuleName, rewardsTypes.ContractRewardCollector, rewardsToMint)) // Create the record diff --git a/x/rewards/keeper/distribution_test.go b/x/rewards/keeper/distribution_test.go index d7274baf..3d69d982 100644 --- a/x/rewards/keeper/distribution_test.go +++ b/x/rewards/keeper/distribution_test.go @@ -4,8 +4,8 @@ import ( "testing" wasmdTypes "github.com/CosmWasm/wasmd/x/wasm/types" + mintTypes "github.com/archway-network/archway/x/mint/types" sdk "github.com/cosmos/cosmos-sdk/types" - mintTypes "github.com/cosmos/cosmos-sdk/x/mint/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/x/rewards/keeper/invariants_test.go b/x/rewards/keeper/invariants_test.go index cb21e1e5..803c20d1 100644 --- a/x/rewards/keeper/invariants_test.go +++ b/x/rewards/keeper/invariants_test.go @@ -4,8 +4,8 @@ import ( "testing" "time" + mintTypes "github.com/archway-network/archway/x/mint/types" sdk "github.com/cosmos/cosmos-sdk/types" - mintTypes "github.com/cosmos/cosmos-sdk/x/mint/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/x/rewards/mintbankkeeper/keeper_test.go b/x/rewards/mintbankkeeper/keeper_test.go index 7d88ddf6..f18935c6 100644 --- a/x/rewards/mintbankkeeper/keeper_test.go +++ b/x/rewards/mintbankkeeper/keeper_test.go @@ -3,10 +3,10 @@ package mintbankkeeper_test import ( "testing" + mintTypes "github.com/archway-network/archway/x/mint/types" sdk "github.com/cosmos/cosmos-sdk/types" authTypes "github.com/cosmos/cosmos-sdk/x/auth/types" distrTypes "github.com/cosmos/cosmos-sdk/x/distribution/types" - mintTypes "github.com/cosmos/cosmos-sdk/x/mint/types" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -134,7 +134,7 @@ func TestMintBankKeeper(t *testing.T) { transferCoins, err := sdk.ParseCoinsNormalized(tc.transferCoins) require.NoError(t, err) - require.NoError(t, chain.GetApp().MintKeeper.MintCoins(ctx, transferCoins)) + require.NoError(t, chain.GetApp().MintKeeper.MintCoins(ctx, mintTypes.ModuleName, transferCoins)) require.NoError(t, chain.GetApp().BankKeeper.SendCoinsFromModuleToModule(ctx, mintTypes.ModuleName, tc.srcModule, transferCoins)) // Remove rewards records which is created automagically From 6195e52a95549bfb4315e8227108a105376e52ee Mon Sep 17 00:00:00 2001 From: Spoorthi Satheesha <9302666+spoo-bar@users.noreply.github.com> Date: Thu, 2 Mar 2023 17:01:43 +0000 Subject: [PATCH 3/6] adding genesis Init/Export tests --- x/mint/genesis_test.go | 90 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/x/mint/genesis_test.go b/x/mint/genesis_test.go index 6301abd2..722a3866 100644 --- a/x/mint/genesis_test.go +++ b/x/mint/genesis_test.go @@ -1,3 +1,91 @@ package mint_test -// todo: add tests after app wiring: #289 +import ( + "testing" + "time" + + e2eTesting "github.com/archway-network/archway/e2e/testing" + "github.com/archway-network/archway/x/mint" + "github.com/archway-network/archway/x/mint/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/suite" +) + +// TestGenesisImportExport check genesis import/export. +func (s *KeeperTestSuite) TestGenesisImportExport() { + currentTime := time.Now() + ctx, k := s.chain.GetContext().WithBlockTime(currentTime), s.chain.GetApp().MintKeeper + inflation := sdk.MustNewDecFromStr("0.3") + + s.Run("Panic: invalid inflation info. should panic", func() { + initParams := types.DefaultParams() + genesisState := types.GenesisState{ + Params: initParams, + LastBlockInfo: types.LastBlockInfo{ + Inflation: sdk.MustNewDecFromStr("8"), + Time: ¤tTime, + }, + } + s.Panics(func() { mint.InitGenesis(ctx, k, genesisState) }) + }) + + s.Run("OK: check state matches init input. LastBlockInfo missing", func() { + initParams := types.DefaultParams() + initParams.MinInflation = sdk.MustNewDecFromStr("1") + genesisState := types.GenesisState{ + Params: initParams, + } + + mint.InitGenesis(ctx, k, genesisState) + + params := k.GetParams(ctx) + lbi, found := k.GetLastBlockInfo(ctx) + s.Require().EqualValues(initParams, params) + s.Require().True(found) + s.Require().EqualValues(params.MinInflation, lbi.Inflation) + s.Require().EqualValues(currentTime.UTC(), lbi.Time.UTC()) + }) + + s.Run("OK: check state matches init input", func() { + initParams := types.DefaultParams() + initParams.InflationChange = sdk.MustNewDecFromStr("0.123") + genesisState := types.GenesisState{ + Params: initParams, + LastBlockInfo: types.LastBlockInfo{ + Inflation: inflation, + Time: ¤tTime, + }, + } + + mint.InitGenesis(ctx, k, genesisState) + + params := k.GetParams(ctx) + lbi, found := k.GetLastBlockInfo(ctx) + s.Require().EqualValues(initParams, params) + s.Require().True(found) + s.Require().EqualValues(inflation, lbi.Inflation) + s.Require().EqualValues(currentTime.UTC(), lbi.Time.UTC()) + }) + + s.Run("OK: check export matches what we init", func() { + k.SetParams(ctx, types.DefaultParams()) + genesisState := mint.ExportGenesis(ctx, k) + s.Require().NotNil(genesisState) + s.Require().EqualValues(types.DefaultParams(), genesisState.Params) + s.Require().EqualValues(inflation, genesisState.LastBlockInfo.Inflation) + s.Require().EqualValues(currentTime.UTC(), genesisState.LastBlockInfo.Time.UTC()) + }) +} + +type KeeperTestSuite struct { + suite.Suite + chain *e2eTesting.TestChain +} + +func (s *KeeperTestSuite) SetupTest() { + s.chain = e2eTesting.NewTestChain(s.T(), 1) +} + +func TestMintKeeper(t *testing.T) { + suite.Run(t, new(KeeperTestSuite)) +} From ace40937ba2d0ba8d89406e83f1ae0b2aa3d30cc Mon Sep 17 00:00:00 2001 From: Spoorthi Satheesha <9302666+spoo-bar@users.noreply.github.com> Date: Fri, 3 Mar 2023 11:45:41 +0000 Subject: [PATCH 4/6] adding rewards module to default mint params --- x/mint/types/params.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/x/mint/types/params.go b/x/mint/types/params.go index 51481f2b..dbdbce1e 100644 --- a/x/mint/types/params.go +++ b/x/mint/types/params.go @@ -1,7 +1,7 @@ package types import ( - fmt "fmt" + "fmt" "time" sdk "github.com/cosmos/cosmos-sdk/types" @@ -21,7 +21,11 @@ var ( DefaultMaxBlockDuration time.Duration = time.Minute DefaultFeeCollectorRecipient InflationRecipient = InflationRecipient{ Recipient: authtypes.FeeCollectorName, - Ratio: sdk.OneDec(), + Ratio: sdk.MustNewDecFromStr("0.8"), + } + DefaultRewardsModule InflationRecipient = InflationRecipient{ + Recipient: "rewards", + Ratio: sdk.MustNewDecFromStr("0.2"), } ) @@ -60,7 +64,7 @@ func DefaultParams() Params { DefaultMaximumBonded, DefaultInflationChange, DefaultMaxBlockDuration, - []*InflationRecipient{&DefaultFeeCollectorRecipient}, + []*InflationRecipient{&DefaultFeeCollectorRecipient, &DefaultRewardsModule}, ) } From 54bf2a4fe3d585ef53daa66183f3f7a59a18d435 Mon Sep 17 00:00:00 2001 From: Spoorthi Satheesha <9302666+spoo-bar@users.noreply.github.com> Date: Fri, 3 Mar 2023 11:53:04 +0000 Subject: [PATCH 5/6] removing mintbankkeeper - will be handled by x/mint --- x/rewards/mintbankkeeper/keeper.go | 84 ---------- x/rewards/mintbankkeeper/keeper_test.go | 206 ------------------------ 2 files changed, 290 deletions(-) delete mode 100644 x/rewards/mintbankkeeper/keeper.go delete mode 100644 x/rewards/mintbankkeeper/keeper_test.go diff --git a/x/rewards/mintbankkeeper/keeper.go b/x/rewards/mintbankkeeper/keeper.go deleted file mode 100644 index c344900e..00000000 --- a/x/rewards/mintbankkeeper/keeper.go +++ /dev/null @@ -1,84 +0,0 @@ -package mintbankkeeper - -import ( - "fmt" - - sdk "github.com/cosmos/cosmos-sdk/types" - authTypes "github.com/cosmos/cosmos-sdk/x/auth/types" - mintTypes "github.com/cosmos/cosmos-sdk/x/mint/types" - - "github.com/archway-network/archway/pkg" - rewardsTypes "github.com/archway-network/archway/x/rewards/types" -) - -var _ mintTypes.BankKeeper = Keeper{} - -// RewardsKeeperExpected defines the expected interface for the x/rewards keeper. -type RewardsKeeperExpected interface { - InflationRewardsRatio(ctx sdk.Context) sdk.Dec - TrackInflationRewards(ctx sdk.Context, rewards sdk.Coin) - UpdateMinConsensusFee(ctx sdk.Context, inflationRewards sdk.Coin) -} - -// Keeper is the x/bank keeper decorator that is used by the x/mint module. -// Decorator is used to split inflation tokens between the rewards collector and the fee collector accounts. -type Keeper struct { - bankKeeper mintTypes.BankKeeper - rewardsKeeper RewardsKeeperExpected -} - -// NewKeeper creates a new Keeper instance. -func NewKeeper(bk mintTypes.BankKeeper, rk RewardsKeeperExpected) Keeper { - return Keeper{ - bankKeeper: bk, - rewardsKeeper: rk, - } -} - -// SendCoinsFromModuleToModule implements the mintTypes.BankKeeper interface. -func (k Keeper) SendCoinsFromModuleToModule(ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coins) error { - // Perform the split only if the recipient is fee collector (which for instance is always the case) and - // inflation rewards are enabled. - ratio := k.rewardsKeeper.InflationRewardsRatio(ctx) - if recipientModule != authTypes.FeeCollectorName || ratio.IsZero() { - return k.bankKeeper.SendCoinsFromModuleToModule(ctx, senderModule, recipientModule, amt) - } - - dappRewards, stakingRewards := pkg.SplitCoins(amt, ratio) - - // Send to the x/auth fee collector account - if !stakingRewards.Empty() { - if err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, senderModule, recipientModule, stakingRewards); err != nil { - return err - } - } - - // Send to the x/rewards account - if !dappRewards.Empty() { - if err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, senderModule, rewardsTypes.ContractRewardCollector, dappRewards); err != nil { - return err - } - } - - // Check that only one coin has been minted - if len(dappRewards) != 1 { - panic(fmt.Errorf("unexpected dApp rewards: %s", dappRewards)) - } - - // Track inflation rewards - k.rewardsKeeper.TrackInflationRewards(ctx, dappRewards[0]) - // Update the minimum consensus fee - k.rewardsKeeper.UpdateMinConsensusFee(ctx, dappRewards[0]) - - return nil -} - -// SendCoinsFromModuleToAccount implements the mintTypes.BankKeeper interface. -func (k Keeper) SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error { - return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, senderModule, recipientAddr, amt) -} - -// MintCoins implements the mintTypes.BankKeeper interface. -func (k Keeper) MintCoins(ctx sdk.Context, name string, amt sdk.Coins) error { - return k.bankKeeper.MintCoins(ctx, name, amt) -} diff --git a/x/rewards/mintbankkeeper/keeper_test.go b/x/rewards/mintbankkeeper/keeper_test.go deleted file mode 100644 index f18935c6..00000000 --- a/x/rewards/mintbankkeeper/keeper_test.go +++ /dev/null @@ -1,206 +0,0 @@ -package mintbankkeeper_test - -import ( - "testing" - - mintTypes "github.com/archway-network/archway/x/mint/types" - sdk "github.com/cosmos/cosmos-sdk/types" - authTypes "github.com/cosmos/cosmos-sdk/x/auth/types" - distrTypes "github.com/cosmos/cosmos-sdk/x/distribution/types" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - e2eTesting "github.com/archway-network/archway/e2e/testing" - "github.com/archway-network/archway/pkg" - "github.com/archway-network/archway/x/rewards/mintbankkeeper" - rewardsTypes "github.com/archway-network/archway/x/rewards/types" -) - -func TestMintBankKeeper(t *testing.T) { - type testCase struct { - name string - // Inputs - inflationRewardsRatio string // x/rewards inflation rewards ratio - blockMaxGas int64 // block max gas (consensus param) - srcModule string // source module name - dstModule string // destination module name - transferCoins string // coins to send [sdk.Coins] - // Expected outputs - errExpected bool - rewardRecordExpected bool // reward record expected to be created - dstBalanceDiffExpected string // expected destination module balance diff [sdk.Coins] - rewardsBalanceDiffExpected string // expected x/rewards module balance diff [sdk.Coins] - } - - testCases := []testCase{ - { - name: "OK: 1000stake: Mint -> FeeCollector with 0.6 ratio to Rewards", - // - inflationRewardsRatio: "0.6", - blockMaxGas: 1000, - srcModule: mintTypes.ModuleName, - dstModule: authTypes.FeeCollectorName, - transferCoins: "1000stake", - // - rewardRecordExpected: true, - dstBalanceDiffExpected: "400stake", - rewardsBalanceDiffExpected: "600stake", - }, - { - name: "OK: 45stake: Mint -> FeeCollector with 0.5 ratio to Rewards with Int truncated", - // - inflationRewardsRatio: "0.5", - blockMaxGas: 1000, - srcModule: mintTypes.ModuleName, - dstModule: authTypes.FeeCollectorName, - transferCoins: "45stake", - // - rewardRecordExpected: true, - dstBalanceDiffExpected: "23stake", - rewardsBalanceDiffExpected: "22stake", - }, - { - name: "OK: 100stake: Mint -> FeeCollector with 0.99 ratio to Rewards", - // - inflationRewardsRatio: "0.99", - blockMaxGas: 1000, - srcModule: mintTypes.ModuleName, - dstModule: authTypes.FeeCollectorName, - transferCoins: "100stake", - // - rewardRecordExpected: true, - dstBalanceDiffExpected: "1stake", - rewardsBalanceDiffExpected: "99stake", - }, - { - name: "OK: 100stake: Mint -> FeeCollector with 0.0 ratio to Rewards (no rewards)", - // - inflationRewardsRatio: "0", - blockMaxGas: 1000, - srcModule: mintTypes.ModuleName, - dstModule: authTypes.FeeCollectorName, - transferCoins: "100stake", - // - rewardRecordExpected: false, - dstBalanceDiffExpected: "100stake", - rewardsBalanceDiffExpected: "", - }, - { - name: "OK: 100stake: Mint -> FeeCollector with 0.01 ratio to Rewards (no block gas limit)", - // - inflationRewardsRatio: "0.01", - blockMaxGas: -1, - srcModule: mintTypes.ModuleName, - dstModule: authTypes.FeeCollectorName, - transferCoins: "100stake", - // - rewardRecordExpected: true, - dstBalanceDiffExpected: "99stake", - rewardsBalanceDiffExpected: "1stake", - }, - { - name: "OK: 100stake: Mint -> Distr (no x/rewards involved)", - // - inflationRewardsRatio: "0.5", - blockMaxGas: -1, - srcModule: mintTypes.ModuleName, - dstModule: distrTypes.ModuleName, - transferCoins: "100stake", - // - rewardRecordExpected: false, - dstBalanceDiffExpected: "100stake", - rewardsBalanceDiffExpected: "", - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - // Create chain - inflationRewardsRatio, err := sdk.NewDecFromStr(tc.inflationRewardsRatio) - require.NoError(t, err) - - chain := e2eTesting.NewTestChain(t, 1, - e2eTesting.WithInflationRewardsRatio(inflationRewardsRatio), - e2eTesting.WithBlockGasLimit(tc.blockMaxGas), - ) - ctx := chain.GetContext() - - // Fetch initial balances - srcBalanceBefore := chain.GetModuleBalance(tc.srcModule) - dstBalanceBefore := chain.GetModuleBalance(tc.dstModule) - rewardsBalanceBefore := chain.GetModuleBalance(rewardsTypes.ContractRewardCollector) - - // Mint funds for the source module - transferCoins, err := sdk.ParseCoinsNormalized(tc.transferCoins) - require.NoError(t, err) - - require.NoError(t, chain.GetApp().MintKeeper.MintCoins(ctx, mintTypes.ModuleName, transferCoins)) - require.NoError(t, chain.GetApp().BankKeeper.SendCoinsFromModuleToModule(ctx, mintTypes.ModuleName, tc.srcModule, transferCoins)) - - // Remove rewards records which is created automagically - chain.GetApp().RewardsKeeper.GetState().DeleteBlockRewardsCascade(ctx, ctx.BlockHeight()) - - // Transfer via keeper - k := mintbankkeeper.NewKeeper(chain.GetApp().BankKeeper, chain.GetApp().RewardsKeeper) - err = k.SendCoinsFromModuleToModule(ctx, tc.srcModule, tc.dstModule, transferCoins) - if tc.errExpected { - assert.Error(t, err) - return - } - require.NoError(t, err) - - // Check final balances - srcBalanceAfter := chain.GetModuleBalance(tc.srcModule) - dstBalanceAfter := chain.GetModuleBalance(tc.dstModule) - rewardsBalanceAfter := chain.GetModuleBalance(rewardsTypes.ContractRewardCollector) - - srcBalanceDiffReceived := srcBalanceBefore.Sub(srcBalanceAfter) // negative - dstBalanceDiffReceived := dstBalanceAfter.Sub(dstBalanceBefore) // positive - rewardsBalanceDiffReceived := rewardsBalanceAfter.Sub(rewardsBalanceBefore) // positive - - dstBalanceDiffExpected, err := sdk.ParseCoinsNormalized(tc.dstBalanceDiffExpected) - require.NoError(t, err) - rewardsDiffExpected, err := sdk.ParseCoinsNormalized(tc.rewardsBalanceDiffExpected) - require.NoError(t, err) - - assert.True(t, srcBalanceDiffReceived.IsZero()) - assert.Equal(t, dstBalanceDiffExpected.String(), dstBalanceDiffReceived.String()) - assert.Equal(t, rewardsDiffExpected.String(), rewardsBalanceDiffReceived.String()) - - // Check rewards record - rewardsRecordReceived, found := chain.GetApp().RewardsKeeper.GetState().BlockRewardsState(ctx).GetBlockRewards(ctx.BlockHeight()) - if !tc.rewardRecordExpected { - require.False(t, found) - return - } - require.True(t, found) - - maxGasExpected := uint64(0) - if tc.blockMaxGas > 0 { - maxGasExpected = uint64(tc.blockMaxGas) - } - - assert.Equal(t, ctx.BlockHeight(), rewardsRecordReceived.Height) - assert.Equal(t, rewardsDiffExpected.String(), rewardsRecordReceived.InflationRewards.String()) - assert.Equal(t, maxGasExpected, rewardsRecordReceived.MaxGas) - - // Check minimum consensus fee record - minConsFeeReceived, minConfFeeFound := chain.GetApp().RewardsKeeper.GetState().MinConsensusFee(ctx).GetFee() - if maxGasExpected == 0 || rewardsDiffExpected.IsZero() { - assert.False(t, minConfFeeFound) - } else { - require.True(t, minConfFeeFound) - - minConsFeeExpected := sdk.DecCoin{ - Denom: sdk.DefaultBondDenom, - Amount: rewardsDiffExpected[0].Amount.ToDec().Quo( - pkg.NewDecFromUint64(maxGasExpected).Mul( - chain.GetApp().RewardsKeeper.TxFeeRebateRatio(ctx).Sub(sdk.OneDec()), - ), - ).Neg(), - } - assert.Equal(t, minConsFeeExpected.String(), minConsFeeReceived.String()) - } - }) - } -} From 11029bda3e039a33e2a2805fded3a57218a3b731 Mon Sep 17 00:00:00 2001 From: Spoorthi Satheesha <9302666+spoo-bar@users.noreply.github.com> Date: Mon, 6 Mar 2023 17:43:44 +0000 Subject: [PATCH 6/6] fixing test panic --- e2e/gastracking_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/e2e/gastracking_test.go b/e2e/gastracking_test.go index b8f00ed5..45c7dd10 100644 --- a/e2e/gastracking_test.go +++ b/e2e/gastracking_test.go @@ -7,6 +7,7 @@ import ( wasmdTypes "github.com/CosmWasm/wasmd/x/wasm/types" sdk "github.com/cosmos/cosmos-sdk/types" + authTypes "github.com/cosmos/cosmos-sdk/x/auth/types" abci "github.com/tendermint/tendermint/abci/types" voterTypes "github.com/archway-network/voter/src/types" @@ -38,6 +39,7 @@ func (s *E2ETestSuite) TestGasTrackingAndRewardsDistribution() { e2eTesting.WithBlockGasLimit(blockGasLimit), // Artificially increase the minted inflation coin to get some rewards for the contract (otherwise contractOp gas / blockGasLimit ratio will be 0) e2eTesting.WithMintParams(mintParams), + e2eTesting.WithInflationDistributionRecipient(authTypes.FeeCollectorName, sdk.OneDec().Sub(inflationRewardsRatio)), e2eTesting.WithInflationDistributionRecipient(rewardsTypes.ModuleName, inflationRewardsRatio), // Set default Tx fee for non-manual transaction like Upload / Instantiate e2eTesting.WithDefaultFeeAmount("10000"),