0g-chain/x/community/testutil/disable_inflation.go
Draco 614d4e40fe
Update cosmos-sdk to v0.47.7 (#1811)
* Update cometbft, cosmos, ethermint, and ibc-go

* Replace github.com/tendermint/tendermint by github.com/cometbft/cometbft

* Replace github.com/tendermint/tm-db by github.com/cometbft/cometbft-db

* Replace gogo/protobuf with cosmos/gogoproto & simapp replacement

* Replace cosmos-sdk/simapp/helpers with cosmos-sdk/testutil/sims

* Remove no longer used simulations

* Replace ibchost with ibcexported
See https://github.com/cosmos/ibc-go/blob/v7.2.2/docs/migrations/v6-to-v7.md#ibc-module-constants

* Add new consensus params keeper

* Add consensus keeper to blockers

* Fix keeper and module issues in app.go

* Add IsSendEnabledCoins and update SetParams interface changes

* Fix protobuf build for cosmos 47 (#1800)

* fix cp errors by using -f; fix lint by only linting our proto dir;
and use proofs.proto directly from ics23 for ibc-go v7

* run proto-all; commit updated third party deps and swagger changes

* regenerate proto files

* use correct gocosmos build plugin for buf

* re-gen all protobuf files to update paths for new gocosmos plugin

* update protoc and buf to latest versions

* fix staking keeper issues in app.go

* update tally handler for gov changes

* chain id fix and flag fixes

* update deps for cometbft 47.7 upgrade

* remove all module legacy queriers

* update stakingKeeper to pointer

* Replace ModuleCdc from govv1beta1 to govcodec

* remove simulations

* abci.LastCommitInfo → abci.CommitInfo

* Remove unused code in keys.go

* simapp.MakeTestEncodingConfig -> moduletestutil.MakeTestEncodingConfi

* Fix chain id issues in tests

* Fix remaining unit test issues

* Update changelog for upgrade

* Fix e2e tests using updated kvtool

* Update protonet to v47 compatible genesis

* Bump cometbft-db to v0.9.1-kava.1

* Update kvtool

* Remove extra changelog

* Fix merged rocksdb issues

* go mod cleanup

* Bump cometbft-db to v9 and go to 1.21

* Bump rocksdb version to v8.10.0

* Update kvtool to latest version

* Update gin to v1.9.0

* Use ibctm.ModuleName in app_test

* Fallback to genesis chain id instead of client toml

* Remove all simulations

* Fix cdp migrations issue with v47

* Update dependencies to correct tags

---------

Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
2024-02-06 17:54:10 -05:00

204 lines
8.1 KiB
Go

package testutil
import (
"time"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtime "github.com/cometbft/cometbft/types/time"
sdk "github.com/cosmos/cosmos-sdk/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/stretchr/testify/suite"
sdkmath "cosmossdk.io/math"
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/x/community"
"github.com/kava-labs/kava/x/community/keeper"
"github.com/kava-labs/kava/x/community/types"
kavadisttypes "github.com/kava-labs/kava/x/kavadist/types"
)
type testFunc func(sdk.Context, keeper.Keeper)
// Test suite used for all abci inflation tests
type disableInflationTestSuite struct {
suite.Suite
App app.TestApp
Ctx sdk.Context
Keeper keeper.Keeper
genesisMintState *minttypes.GenesisState
genesisKavadistState *kavadisttypes.GenesisState
genesisDistrState *distrtypes.GenesisState
testFunc testFunc
}
func NewDisableInflationTestSuite(tf testFunc) *disableInflationTestSuite {
suite := &disableInflationTestSuite{}
suite.testFunc = tf
return suite
}
// The default state used by each test
func (suite *disableInflationTestSuite) SetupTest() {
app.SetSDKConfig()
tApp := app.NewTestApp()
suite.App = tApp
suite.Ctx = suite.App.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
suite.Keeper = suite.App.GetCommunityKeeper()
// Set up x/mint and x/kavadist gen state
mintGen := minttypes.DefaultGenesisState()
mintGen.Params.InflationMax = sdk.NewDecWithPrec(595, 3)
mintGen.Params.InflationMin = sdk.NewDecWithPrec(595, 3)
suite.genesisMintState = mintGen
kavadistGen := kavadisttypes.DefaultGenesisState()
kavadistGen.Params.Active = true
suite.genesisKavadistState = kavadistGen
distrGen := distrtypes.DefaultGenesisState()
distrGen.Params.CommunityTax = sdk.MustNewDecFromStr("0.949500000000000000")
suite.genesisDistrState = distrGen
appCodec := tApp.AppCodec()
suite.App.InitializeFromGenesisStates(
app.GenesisState{minttypes.ModuleName: appCodec.MustMarshalJSON(mintGen)},
app.GenesisState{kavadisttypes.ModuleName: appCodec.MustMarshalJSON(kavadistGen)},
app.GenesisState{distrtypes.ModuleName: appCodec.MustMarshalJSON(distrGen)},
)
}
func (suite *disableInflationTestSuite) TestDisableInflation() {
validateState := func(upgraded bool, expectedDisableTime time.Time, originalStakingRewards sdkmath.LegacyDec, setStakingRewards sdkmath.LegacyDec, msg string) {
params, found := suite.Keeper.GetParams(suite.Ctx)
suite.Require().True(found)
mintParams := suite.App.GetMintKeeper().GetParams(suite.Ctx)
kavadistParams := suite.App.GetKavadistKeeper().GetParams(suite.Ctx)
distrParams := suite.App.GetDistrKeeper().GetParams(suite.Ctx)
disableTimeMsg := "expected inflation disable time to match"
expectedMintState := suite.genesisMintState
expectedKavadistState := suite.genesisKavadistState
expectedDistrState := suite.genesisDistrState
expectedStakingRewards := originalStakingRewards
msgSuffix := "before upgrade"
// The state expected after upgrade time is reached
if upgraded {
// Disable upgrade time is reset when run.
//
// This allows the time to be set and run again if required.
// In addition, with zero time not upgrading, achieves idempotence
// without extra logic or state.
expectedDisableTime = time.Time{}
disableTimeMsg = "expected inflation disable time to be reset"
expectedStakingRewards = setStakingRewards
expectedMintState.Params.InflationMin = sdk.ZeroDec()
expectedMintState.Params.InflationMax = sdk.ZeroDec()
expectedKavadistState.Params.Active = false
expectedDistrState.Params.CommunityTax = sdk.ZeroDec()
msgSuffix = "after upgrade"
suite.Require().NoError(
app.EventsContains(
suite.Ctx.EventManager().Events(),
sdk.NewEvent(
types.EventTypeInflationStop,
sdk.NewAttribute(
types.AttributeKeyInflationDisableTime,
suite.Ctx.BlockTime().Format(time.RFC3339),
),
),
))
}
suite.Require().Equal(expectedMintState.Params.InflationMin, mintParams.InflationMin, msg+": expected mint inflation min to match state "+msgSuffix)
suite.Require().Equal(expectedMintState.Params.InflationMax, mintParams.InflationMax, msg+": expected mint inflation max to match state "+msgSuffix)
suite.Require().Equal(expectedKavadistState.Params.Active, kavadistParams.Active, msg+":expected kavadist active flag match state "+msgSuffix)
suite.Require().Equal(expectedDistrState.Params.CommunityTax, distrParams.CommunityTax, msg+":expected x/distribution community tax to match state "+msgSuffix)
suite.Require().Equal(expectedDisableTime, params.UpgradeTimeDisableInflation, msg+": "+disableTimeMsg)
// we always check staking rewards per second matches the passed in expectation
suite.Require().Equal(expectedStakingRewards, params.StakingRewardsPerSecond, msg+": "+"staking rewards per second to match "+msgSuffix)
// we don't modify or zero out the initial rewards per second for upgrade time
suite.Require().Equal(setStakingRewards, params.UpgradeTimeSetStakingRewardsPerSecond, msg+": "+"set staking rewards per second to match "+msgSuffix)
}
blockTime := suite.Ctx.BlockTime()
testCases := []struct {
name string
upgradeTime time.Time
setStakingRewards sdkmath.LegacyDec
shouldUpgrade bool
}{
{"zero upgrade time -- should not upgrade", time.Time{}, sdkmath.LegacyNewDec(1001), false},
{"upgrade time in future -- should not upgrade", blockTime.Add(1 * time.Second), sdkmath.LegacyNewDec(1002), false},
{"upgrade time in past -- should upgrade", blockTime.Add(-1 * time.Second), sdkmath.LegacyNewDec(1003), true},
{"upgrade time equal to block time -- should upgrade", blockTime, sdkmath.LegacyNewDec(1004), true},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
params, found := suite.Keeper.GetParams(suite.Ctx)
suite.Require().True(found)
// these should not match in order to assure assertions test correct behavior
suite.Require().NotEqual(params.StakingRewardsPerSecond, tc.setStakingRewards, "set staking rewards can not match initial staking rewards")
// ensure state is as we expect before running upgrade or updating time
validateState(false, time.Time{}, params.StakingRewardsPerSecond, params.UpgradeTimeSetStakingRewardsPerSecond, "initial state")
// set inflation disable time
params.UpgradeTimeDisableInflation = tc.upgradeTime
// set upgrade time set staking rewards per second
params.UpgradeTimeSetStakingRewardsPerSecond = tc.setStakingRewards
suite.Keeper.SetParams(suite.Ctx, params)
// run test function
suite.testFunc(suite.Ctx, suite.Keeper)
// run assertions to ensure upgrade did or did not run
validateState(tc.shouldUpgrade, tc.upgradeTime, params.StakingRewardsPerSecond, tc.setStakingRewards, "first begin blocker run")
// test idempotence only if upgrade should have been ran
if tc.shouldUpgrade {
// reset mint and kavadist state to their initial values
err := suite.App.GetMintKeeper().SetParams(suite.Ctx, suite.genesisMintState.Params)
suite.Require().NoError(err)
suite.App.GetKavadistKeeper().SetParams(suite.Ctx, suite.genesisKavadistState.Params)
// modify staking rewards per second to ensure they are not overridden again
params, found := suite.Keeper.GetParams(suite.Ctx)
suite.Require().True(found)
params.StakingRewardsPerSecond = params.StakingRewardsPerSecond.Add(sdkmath.LegacyOneDec())
suite.Keeper.SetParams(suite.Ctx, params)
// run begin blocker again
community.BeginBlocker(suite.Ctx, suite.Keeper)
// ensure begin blocker is idempotent and never runs twice
validateState(false, time.Time{}, params.StakingRewardsPerSecond, tc.setStakingRewards, "second begin blocker run")
}
})
}
}
func (suite *disableInflationTestSuite) TestPanicsOnMissingParameters() {
suite.SetupTest()
store := suite.Ctx.KVStore(suite.App.GetKVStoreKey(types.StoreKey))
store.Delete(types.ParamsKey)
suite.PanicsWithValue("invalid state: module parameters not found", func() {
suite.testFunc(suite.Ctx, suite.Keeper)
})
}