0g-chain/x/incentive/keeper/rewards_savings_accum_test.go

164 lines
4.6 KiB
Go
Raw Normal View History

package keeper_test
import (
"testing"
"time"
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 22:54:10 +00:00
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
savingskeeper "github.com/kava-labs/kava/x/savings/keeper"
savingstypes "github.com/kava-labs/kava/x/savings/types"
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/x/incentive/keeper"
"github.com/kava-labs/kava/x/incentive/testutil"
"github.com/kava-labs/kava/x/incentive/types"
)
// Test suite used for all keeper tests
type SavingsRewardsTestSuite struct {
suite.Suite
keeper keeper.Keeper
savingsKeeper savingskeeper.Keeper
app app.TestApp
ctx sdk.Context
genesisTime time.Time
addrs []sdk.AccAddress
}
// SetupTest is run automatically before each suite test
func (suite *SavingsRewardsTestSuite) SetupTest() {
config := sdk.GetConfig()
app.SetBech32AddressPrefixes(config)
_, allAddrs := app.GeneratePrivKeyAddressPairs(10)
suite.addrs = allAddrs[:5]
suite.genesisTime = time.Date(2020, 12, 15, 14, 0, 0, 0, time.UTC)
}
func (suite *SavingsRewardsTestSuite) SetupApp() {
suite.app = app.NewTestApp()
suite.keeper = suite.app.GetIncentiveKeeper()
suite.savingsKeeper = suite.app.GetSavingsKeeper()
suite.ctx = suite.app.NewContext(true, tmproto.Header{Height: 1, Time: suite.genesisTime})
}
func (suite *SavingsRewardsTestSuite) SetupWithGenState(authBuilder *app.AuthBankGenesisBuilder, incentBuilder testutil.IncentiveGenesisBuilder,
savingsGenesis savingstypes.GenesisState,
) {
suite.SetupApp()
suite.app.InitializeFromGenesisStatesWithTime(
suite.genesisTime,
authBuilder.BuildMarshalled(suite.app.AppCodec()),
app.GenesisState{savingstypes.ModuleName: suite.app.AppCodec().MustMarshalJSON(&savingsGenesis)},
incentBuilder.BuildMarshalled(suite.app.AppCodec()),
)
}
func (suite *SavingsRewardsTestSuite) TestAccumulateSavingsRewards() {
type args struct {
deposit sdk.Coin
rewardsPerSecond sdk.Coins
timeElapsed int
expectedRewardIndexes types.RewardIndexes
}
type test struct {
name string
args args
}
testCases := []test{
{
"7 seconds",
args{
deposit: c("ukava", 1_000_000),
rewardsPerSecond: cs(c("hard", 122354)),
timeElapsed: 7,
expectedRewardIndexes: types.RewardIndexes{
types.NewRewardIndex("hard", d("0.856478000000000000")),
},
},
},
{
"1 day",
args{
deposit: c("ukava", 1_000_000),
rewardsPerSecond: cs(c("hard", 122354)),
timeElapsed: 86400,
expectedRewardIndexes: types.RewardIndexes{
types.NewRewardIndex("hard", d("10571.385600000000000000")),
},
},
},
{
"0 seconds",
args{
deposit: c("ukava", 1_000_000),
rewardsPerSecond: cs(c("hard", 122354)),
timeElapsed: 0,
expectedRewardIndexes: types.RewardIndexes{
types.NewRewardIndex("hard", d("0.0")),
},
},
},
{
"multiple reward coins",
args{
deposit: c("ukava", 1_000_000),
rewardsPerSecond: cs(c("hard", 122354), c("bnb", 567889)),
timeElapsed: 7,
expectedRewardIndexes: types.RewardIndexes{
types.NewRewardIndex("bnb", d("3.97522300000000000")),
types.NewRewardIndex("hard", d("0.856478000000000000")),
},
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
params := savingstypes.NewParams(
[]string{"ukava"},
)
deposits := savingstypes.Deposits{
savingstypes.NewDeposit(
suite.addrs[0],
sdk.NewCoins(tc.args.deposit),
),
}
savingsGenesis := savingstypes.NewGenesisState(params, deposits)
authBuilder := app.NewAuthBankGenesisBuilder().
WithSimpleAccount(suite.addrs[0], cs(c("ukava", 1e9))).
WithSimpleModuleAccount(savingstypes.ModuleName, sdk.NewCoins(tc.args.deposit))
incentBuilder := testutil.NewIncentiveGenesisBuilder().
WithGenesisTime(suite.genesisTime).
WithSimpleSavingsRewardPeriod(tc.args.deposit.Denom, tc.args.rewardsPerSecond)
suite.SetupWithGenState(authBuilder, incentBuilder, savingsGenesis)
// Set up chain context at future time
runAtTime := suite.ctx.BlockTime().Add(time.Duration(int(time.Second) * tc.args.timeElapsed))
runCtx := suite.ctx.WithBlockTime(runAtTime)
rewardPeriods, found := suite.keeper.GetSavingsRewardPeriods(runCtx, tc.args.deposit.Denom)
suite.Require().True(found)
suite.keeper.AccumulateSavingsRewards(runCtx, rewardPeriods)
rewardIndexes, _ := suite.keeper.GetSavingsRewardIndexes(runCtx, tc.args.deposit.Denom)
suite.Require().Equal(tc.args.expectedRewardIndexes, rewardIndexes)
})
}
}
func TestSavingsRewardsTestSuite(t *testing.T) {
suite.Run(t, new(SavingsRewardsTestSuite))
}