mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-25 15:55:18 +00:00
614d4e40fe
* 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>
195 lines
6.4 KiB
Go
195 lines
6.4 KiB
Go
package keeper_test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
|
tmtime "github.com/cometbft/cometbft/types/time"
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
sdkmath "cosmossdk.io/math"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
|
|
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
|
"github.com/kava-labs/kava/app"
|
|
"github.com/kava-labs/kava/x/savings/keeper"
|
|
"github.com/kava-labs/kava/x/savings/types"
|
|
)
|
|
|
|
// Test suite used for all keeper tests
|
|
type KeeperTestSuite struct {
|
|
suite.Suite
|
|
keeper keeper.Keeper
|
|
app app.TestApp
|
|
ctx sdk.Context
|
|
addrs []sdk.AccAddress
|
|
}
|
|
|
|
// The default state used by each test
|
|
func (suite *KeeperTestSuite) SetupTest() {
|
|
config := sdk.GetConfig()
|
|
app.SetBech32AddressPrefixes(config)
|
|
|
|
tApp := app.NewTestApp()
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
|
|
tApp.InitializeFromGenesisStates()
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(1)
|
|
keeper := tApp.GetSavingsKeeper()
|
|
suite.app = tApp
|
|
suite.ctx = ctx
|
|
suite.keeper = keeper
|
|
suite.addrs = addrs
|
|
|
|
stakingParams := stakingtypes.DefaultParams()
|
|
stakingParams.BondDenom = "ukava"
|
|
suite.app.GetStakingKeeper().SetParams(suite.ctx, stakingParams)
|
|
}
|
|
|
|
func (suite *KeeperTestSuite) TestGetSetDeleteDeposit() {
|
|
dep := types.NewDeposit(sdk.AccAddress("test"), sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(100))))
|
|
|
|
_, f := suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"))
|
|
suite.Require().False(f)
|
|
|
|
suite.keeper.SetDeposit(suite.ctx, dep)
|
|
|
|
testDeposit, f := suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"))
|
|
suite.Require().True(f)
|
|
suite.Require().Equal(dep, testDeposit)
|
|
|
|
suite.Require().NotPanics(func() { suite.keeper.DeleteDeposit(suite.ctx, dep) })
|
|
_, f = suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"))
|
|
suite.Require().False(f)
|
|
}
|
|
|
|
func (suite *KeeperTestSuite) TestIterateDeposits() {
|
|
for i := 0; i < 5; i++ {
|
|
dep := types.NewDeposit(sdk.AccAddress("test"+fmt.Sprint(i)), sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(100))))
|
|
suite.Require().NotPanics(func() { suite.keeper.SetDeposit(suite.ctx, dep) })
|
|
}
|
|
var deposits []types.Deposit
|
|
suite.keeper.IterateDeposits(suite.ctx, func(d types.Deposit) bool {
|
|
deposits = append(deposits, d)
|
|
return false
|
|
})
|
|
suite.Require().Equal(5, len(deposits))
|
|
}
|
|
|
|
func (suite *KeeperTestSuite) getAccountCoins(acc authtypes.AccountI) sdk.Coins {
|
|
bk := suite.app.GetBankKeeper()
|
|
return bk.GetAllBalances(suite.ctx, acc.GetAddress())
|
|
}
|
|
|
|
func (suite *KeeperTestSuite) getAccount(addr sdk.AccAddress) authtypes.AccountI {
|
|
ak := suite.app.GetAccountKeeper()
|
|
return ak.GetAccount(suite.ctx, addr)
|
|
}
|
|
|
|
func (suite *KeeperTestSuite) getAccountAtCtx(addr sdk.AccAddress, ctx sdk.Context) authtypes.AccountI {
|
|
ak := suite.app.GetAccountKeeper()
|
|
return ak.GetAccount(ctx, addr)
|
|
}
|
|
|
|
func (suite *KeeperTestSuite) getModuleAccount(name string) authtypes.ModuleAccountI {
|
|
ak := suite.app.GetAccountKeeper()
|
|
return ak.GetModuleAccount(suite.ctx, name)
|
|
}
|
|
|
|
func (suite *KeeperTestSuite) getModuleAccountAtCtx(name string, ctx sdk.Context) authtypes.ModuleAccountI {
|
|
ak := suite.app.GetAccountKeeper()
|
|
return ak.GetModuleAccount(ctx, name)
|
|
}
|
|
|
|
func TestKeeperTestSuite(t *testing.T) {
|
|
suite.Run(t, new(KeeperTestSuite))
|
|
}
|
|
|
|
// CreateAccount creates a new account from the provided balance and address
|
|
func (suite *KeeperTestSuite) CreateAccountWithAddress(addr sdk.AccAddress, initialBalance sdk.Coins) authtypes.AccountI {
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
acc := ak.NewAccountWithAddress(suite.ctx, addr)
|
|
ak.SetAccount(suite.ctx, acc)
|
|
|
|
err := suite.app.FundAccount(suite.ctx, acc.GetAddress(), initialBalance)
|
|
suite.Require().NoError(err)
|
|
|
|
return acc
|
|
}
|
|
|
|
// CreateVestingAccount creates a new vesting account. `vestingBalance` should be a fraction of `initialBalance`.
|
|
func (suite *KeeperTestSuite) CreateVestingAccountWithAddress(addr sdk.AccAddress, initialBalance sdk.Coins, vestingBalance sdk.Coins) authtypes.AccountI {
|
|
if vestingBalance.IsAnyGT(initialBalance) {
|
|
panic("vesting balance must be less than initial balance")
|
|
}
|
|
acc := suite.CreateAccountWithAddress(addr, initialBalance)
|
|
bacc := acc.(*authtypes.BaseAccount)
|
|
|
|
periods := vestingtypes.Periods{
|
|
vestingtypes.Period{
|
|
Length: 31556952,
|
|
Amount: vestingBalance,
|
|
},
|
|
}
|
|
vacc := vestingtypes.NewPeriodicVestingAccount(bacc, vestingBalance, suite.ctx.BlockTime().Unix(), periods)
|
|
suite.app.GetAccountKeeper().SetAccount(suite.ctx, vacc)
|
|
return vacc
|
|
}
|
|
|
|
func (suite *KeeperTestSuite) deliverMsgCreateValidator(ctx sdk.Context, address sdk.ValAddress, selfDelegation sdk.Coin) error {
|
|
msg, err := stakingtypes.NewMsgCreateValidator(
|
|
address,
|
|
ed25519.GenPrivKey().PubKey(),
|
|
selfDelegation,
|
|
stakingtypes.Description{},
|
|
stakingtypes.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
|
|
sdkmath.NewInt(1e6),
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
msgServer := stakingkeeper.NewMsgServerImpl(suite.app.GetStakingKeeper())
|
|
_, err = msgServer.CreateValidator(sdk.WrapSDKContext(suite.ctx), msg)
|
|
return err
|
|
}
|
|
|
|
// CreateNewUnbondedValidator creates a new validator in the staking module.
|
|
// New validators are unbonded until the end blocker is run.
|
|
func (suite *KeeperTestSuite) CreateNewUnbondedValidator(addr sdk.ValAddress, selfDelegation sdkmath.Int) stakingtypes.Validator {
|
|
// Create a validator
|
|
err := suite.deliverMsgCreateValidator(suite.ctx, addr, sdk.NewCoin("ukava", selfDelegation))
|
|
suite.Require().NoError(err)
|
|
|
|
// New validators are created in an unbonded state. Note if the end blocker is run later this validator could become bonded.
|
|
|
|
validator, found := suite.app.GetStakingKeeper().GetValidator(suite.ctx, addr)
|
|
suite.Require().True(found)
|
|
return validator
|
|
}
|
|
|
|
// CreateDelegation delegates tokens to a validator.
|
|
func (suite *KeeperTestSuite) CreateDelegation(valAddr sdk.ValAddress, delegator sdk.AccAddress, amount sdkmath.Int) sdk.Dec {
|
|
sk := suite.app.GetStakingKeeper()
|
|
|
|
stakingDenom := sk.BondDenom(suite.ctx)
|
|
msg := stakingtypes.NewMsgDelegate(
|
|
delegator,
|
|
valAddr,
|
|
sdk.NewCoin(stakingDenom, amount),
|
|
)
|
|
|
|
msgServer := stakingkeeper.NewMsgServerImpl(sk)
|
|
_, err := msgServer.Delegate(sdk.WrapSDKContext(suite.ctx), msg)
|
|
suite.Require().NoError(err)
|
|
|
|
del, found := sk.GetDelegation(suite.ctx, delegator, valAddr)
|
|
suite.Require().True(found)
|
|
return del.Shares
|
|
}
|