Skip to content

Commit

Permalink
chore: refactor staking integration, sims and e2e tests (#8)
Browse files Browse the repository at this point in the history
* set min_self_delegation to 0 in TestAminoCodecFullDecodeAndEncode

* add WithdrawTokenizeShareRecordReward and WithdrawAllTokenizeShareRecordReward

* add methods to distribution/keeper

* register distribution msgs

* add SimulateMsgWithdrawTokenizeShareRecordReward

* LSM distribution queries

* LSM distr cli

* add BeforeTokenizeShareRecordRemoved hook

* add signers to proto distribution

* set signers correctly

* minimum refactor to build

* tag LSM test to be refactored

* Merge with feat/lsm/v0.47.x

tag LSM tests to be refactored

Fix nits

* nit

* comments more failing tests

* make protos

* Update x/staking/keeper/msg_server.go

Co-authored-by: Marius Poke <[email protected]>

* Update x/staking/keeper/msg_server.go

Co-authored-by: Marius Poke <[email protected]>

* add go.work and fix silent errors

* address comments

* refactor staking msg_server_tests.go - distrib hooks cause them to fail

* make integration tests pass

* clean up

* clean up

* refactor last integration

* nits

* revert deterministic tests change

* tests: update simulation randfees calc (#9)

* address min self delegation depreciation in tests

* refactor e2e tests and other nits

* tests: appease linter in randfees

---------

Co-authored-by: mpoke <[email protected]>
Co-authored-by: MSalopek <[email protected]>
  • Loading branch information
3 people authored Dec 8, 2023
1 parent e7220e3 commit b50f5a7
Show file tree
Hide file tree
Showing 21 changed files with 2,799 additions and 2,883 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ benchmark:
###############################################################################

golangci_lint_cmd=golangci-lint
golangci_version=v1.50.1
golangci_version=v1.55.2

lint:
@echo "--> Running linter"
Expand Down
2 changes: 1 addition & 1 deletion snapshots/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (s *Store) Save(
snapshotHasher := sha256.New()
chunkHasher := sha256.New()
for chunkBody := range chunks {
defer chunkBody.Close() //nolint:staticcheck
defer chunkBody.Close()
dir := s.pathSnapshot(height, format)
err = os.MkdirAll(dir, 0o755)
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions tests/e2e/staking/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,16 +914,19 @@ func (s *E2ETestSuite) TestGetCmdQueryParams() {
"with text output",
[]string{fmt.Sprintf("--%s=text", flags.FlagOutput)},
`bond_denom: stake
global_liquid_staking_cap: "1.000000000000000000"
historical_entries: 10000
max_entries: 7
max_validators: 100
min_commission_rate: "0.000000000000000000"
unbonding_time: 1814400s`,
unbonding_time: 1814400s
validator_bond_factor: "-1.000000000000000000"
validator_liquid_staking_cap: "1.000000000000000000"`,
},
{
"with json output",
[]string{fmt.Sprintf("--%s=json", flags.FlagOutput)},
`{"unbonding_time":"1814400s","max_validators":100,"max_entries":7,"historical_entries":10000,"bond_denom":"stake","min_commission_rate":"0.000000000000000000"}`,
`{"unbonding_time":"1814400s","max_validators":100,"max_entries":7,"historical_entries":10000,"bond_denom":"stake","min_commission_rate":"0.000000000000000000","validator_bond_factor":"-1.000000000000000000","global_liquid_staking_cap":"1.000000000000000000","validator_liquid_staking_cap":"1.000000000000000000"}`,
},
}
for _, tc := range testCases {
Expand Down Expand Up @@ -1224,7 +1227,7 @@ func (s *E2ETestSuite) TestNewRedelegateCmd() {
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
},
false, 31, &sdk.TxResponse{},
false, 3, &sdk.TxResponse{},
},
{
"valid transaction of delegate",
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/staking/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/require"

"cosmossdk.io/math"
"cosmossdk.io/simapp"
"github.com/cosmos/cosmos-sdk/codec"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
Expand Down Expand Up @@ -89,8 +90,8 @@ func createValidators(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers
return addrs, valAddrs, vals
}

func delegateCoinsFromAccount(ctx sdk.Context, app *simapp.SimApp, addr sdk.AccAddress, amount sdk.Int, val types.Validator) error {
_, err := app.StakingKeeper.Delegate(ctx, addr, amount, types.Unbonded, val, true)
func delegateCoinsFromAccount(ctx sdk.Context, sk keeper.Keeper, addr sdk.AccAddress, amount math.Int, val types.ValidatorI) error {
_, err := sk.Delegate(ctx, addr, amount, types.Unbonded, val.(types.Validator), true)

return err
}
170 changes: 170 additions & 0 deletions tests/integration/staking/keeper/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,173 @@ func TestUnbondingDelegationsMaxEntries(t *testing.T) {
require.True(math.IntEq(t, newBonded, oldBonded.SubRaw(1)))
require.True(math.IntEq(t, newNotBonded, oldNotBonded.AddRaw(1)))
}

func TestValidatorBondUndelegate(t *testing.T) {
_, app, ctx := createTestInput(t)

addrDels := simtestutil.AddTestAddrs(app.BankKeeper, app.StakingKeeper, ctx, 2, app.StakingKeeper.TokensFromConsensusPower(ctx, 10000))
addrVals := simtestutil.ConvertAddrsToValAddrs(addrDels)

startTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 10)

bondDenom := app.StakingKeeper.BondDenom(ctx)
notBondedPool := app.StakingKeeper.GetNotBondedPool(ctx)

require.NoError(t, banktestutil.FundModuleAccount(app.BankKeeper, ctx, notBondedPool.GetName(), sdk.NewCoins(sdk.NewCoin(bondDenom, startTokens))))
app.AccountKeeper.SetModuleAccount(ctx, notBondedPool)

// create a validator and a delegator to that validator
validator := testutil.NewValidator(t, addrVals[0], PKs[0])
validator.Status = types.Bonded
app.StakingKeeper.SetValidator(ctx, validator)

// set validator bond factor
params := app.StakingKeeper.GetParams(ctx)
params.ValidatorBondFactor = sdk.NewDec(1)
app.StakingKeeper.SetParams(ctx, params)

// convert to validator self-bond
msgServer := keeper.NewMsgServerImpl(app.StakingKeeper)

validator, _ = app.StakingKeeper.GetValidator(ctx, addrVals[0])
err := delegateCoinsFromAccount(ctx, *app.StakingKeeper, addrDels[0], startTokens, validator)
require.NoError(t, err)
_, err = msgServer.ValidatorBond(sdk.WrapSDKContext(ctx), &types.MsgValidatorBond{
DelegatorAddress: addrDels[0].String(),
ValidatorAddress: addrVals[0].String(),
})
require.NoError(t, err)

// tokenize share for 2nd account delegation
validator, _ = app.StakingKeeper.GetValidator(ctx, addrVals[0])
err = delegateCoinsFromAccount(ctx, *app.StakingKeeper, addrDels[1], startTokens, validator)
require.NoError(t, err)
tokenizeShareResp, err := msgServer.TokenizeShares(sdk.WrapSDKContext(ctx), &types.MsgTokenizeShares{
DelegatorAddress: addrDels[1].String(),
ValidatorAddress: addrVals[0].String(),
Amount: sdk.NewCoin(sdk.DefaultBondDenom, startTokens),
TokenizedShareOwner: addrDels[0].String(),
})
require.NoError(t, err)

// try undelegating
_, err = msgServer.Undelegate(sdk.WrapSDKContext(ctx), &types.MsgUndelegate{
DelegatorAddress: addrDels[0].String(),
ValidatorAddress: addrVals[0].String(),
Amount: sdk.NewCoin(sdk.DefaultBondDenom, startTokens),
})
require.Error(t, err)

// redeem full amount on 2nd account and try undelegation
validator, _ = app.StakingKeeper.GetValidator(ctx, addrVals[0])
err = delegateCoinsFromAccount(ctx, *app.StakingKeeper, addrDels[1], startTokens, validator)
require.NoError(t, err)
_, err = msgServer.RedeemTokensForShares(sdk.WrapSDKContext(ctx), &types.MsgRedeemTokensForShares{
DelegatorAddress: addrDels[1].String(),
Amount: tokenizeShareResp.Amount,
})
require.NoError(t, err)

// try undelegating
_, err = msgServer.Undelegate(sdk.WrapSDKContext(ctx), &types.MsgUndelegate{
DelegatorAddress: addrDels[0].String(),
ValidatorAddress: addrVals[0].String(),
Amount: sdk.NewCoin(sdk.DefaultBondDenom, startTokens),
})
require.NoError(t, err)

validator, _ = app.StakingKeeper.GetValidator(ctx, addrVals[0])
require.Equal(t, validator.ValidatorBondShares, sdk.ZeroDec())
}

func TestValidatorBondRedelegate(t *testing.T) {
_, app, ctx := createTestInput(t)

addrDels := simtestutil.AddTestAddrs(app.BankKeeper, app.StakingKeeper, ctx, 2, app.StakingKeeper.TokensFromConsensusPower(ctx, 10000))
addrVals := simtestutil.ConvertAddrsToValAddrs(addrDels)

startTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 10)

bondDenom := app.StakingKeeper.BondDenom(ctx)
notBondedPool := app.StakingKeeper.GetNotBondedPool(ctx)

startPoolToken := sdk.NewCoins(sdk.NewCoin(bondDenom, startTokens.Mul(sdk.NewInt(2))))
require.NoError(t, banktestutil.FundModuleAccount(app.BankKeeper, ctx, notBondedPool.GetName(), startPoolToken))
app.AccountKeeper.SetModuleAccount(ctx, notBondedPool)

// create a validator and a delegator to that validator
validator := testutil.NewValidator(t, addrVals[0], PKs[0])
validator.Status = types.Bonded
app.StakingKeeper.SetValidator(ctx, validator)
validator2 := testutil.NewValidator(t, addrVals[1], PKs[1])
validator.Status = types.Bonded
app.StakingKeeper.SetValidator(ctx, validator2)

// set validator bond factor
params := app.StakingKeeper.GetParams(ctx)
params.ValidatorBondFactor = sdk.NewDec(1)
app.StakingKeeper.SetParams(ctx, params)

// set total liquid stake
app.StakingKeeper.SetTotalLiquidStakedTokens(ctx, sdk.NewInt(100))

// delegate to each validator
validator, _ = app.StakingKeeper.GetValidator(ctx, addrVals[0])
err := delegateCoinsFromAccount(ctx, *app.StakingKeeper, addrDels[0], startTokens, validator)
require.NoError(t, err)

validator2, _ = app.StakingKeeper.GetValidator(ctx, addrVals[1])
err = delegateCoinsFromAccount(ctx, *app.StakingKeeper, addrDels[1], startTokens, validator2)
require.NoError(t, err)

// convert to validator self-bond
msgServer := keeper.NewMsgServerImpl(app.StakingKeeper)
_, err = msgServer.ValidatorBond(sdk.WrapSDKContext(ctx), &types.MsgValidatorBond{
DelegatorAddress: addrDels[0].String(),
ValidatorAddress: addrVals[0].String(),
})
require.NoError(t, err)

// tokenize share for 2nd account delegation
validator, _ = app.StakingKeeper.GetValidator(ctx, addrVals[0])
err = delegateCoinsFromAccount(ctx, *app.StakingKeeper, addrDels[1], startTokens, validator)
require.NoError(t, err)
tokenizeShareResp, err := msgServer.TokenizeShares(sdk.WrapSDKContext(ctx), &types.MsgTokenizeShares{
DelegatorAddress: addrDels[1].String(),
ValidatorAddress: addrVals[0].String(),
Amount: sdk.NewCoin(sdk.DefaultBondDenom, startTokens),
TokenizedShareOwner: addrDels[0].String(),
})
require.NoError(t, err)

// try undelegating
_, err = msgServer.BeginRedelegate(sdk.WrapSDKContext(ctx), &types.MsgBeginRedelegate{
DelegatorAddress: addrDels[0].String(),
ValidatorSrcAddress: addrVals[0].String(),
ValidatorDstAddress: addrVals[1].String(),
Amount: sdk.NewCoin(sdk.DefaultBondDenom, startTokens),
})
require.Error(t, err)

// redeem full amount on 2nd account and try undelegation
validator, _ = app.StakingKeeper.GetValidator(ctx, addrVals[0])
err = delegateCoinsFromAccount(ctx, *app.StakingKeeper, addrDels[1], startTokens, validator)
require.NoError(t, err)
_, err = msgServer.RedeemTokensForShares(sdk.WrapSDKContext(ctx), &types.MsgRedeemTokensForShares{
DelegatorAddress: addrDels[1].String(),
Amount: tokenizeShareResp.Amount,
})
require.NoError(t, err)

// try undelegating
_, err = msgServer.BeginRedelegate(sdk.WrapSDKContext(ctx), &types.MsgBeginRedelegate{
DelegatorAddress: addrDels[0].String(),
ValidatorSrcAddress: addrVals[0].String(),
ValidatorDstAddress: addrVals[1].String(),
Amount: sdk.NewCoin(sdk.DefaultBondDenom, startTokens),
})
require.NoError(t, err)

validator, _ = app.StakingKeeper.GetValidator(ctx, addrVals[0])
require.Equal(t, validator.ValidatorBondShares, sdk.ZeroDec())
}
66 changes: 42 additions & 24 deletions tests/integration/staking/keeper/determinstic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func (s *DeterministicTestSuite) SetupTest() {
)
s.Require().NoError(err)

// s.ctx = app.BaseApp.NewContext(false, tmproto.Header{Height: 1, Time: time.Now()}).WithGasMeter(sdk.NewInfiniteGasMeter())
s.ctx = app.BaseApp.NewContext(false, tmproto.Header{})

queryHelper := baseapp.NewQueryServerTestHelper(s.ctx, interfaceRegistry)
Expand Down Expand Up @@ -258,7 +257,8 @@ func (suite *DeterministicTestSuite) TestGRPCValidator() {
ValidatorAddr: val.OperatorAddress,
}

testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.Validator, 1915, false)
// NOTE: gas consumption delta changed from 1915 to 1933
testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.Validator, 1933, false)
}

func (suite *DeterministicTestSuite) TestGRPCValidators() {
Expand All @@ -281,7 +281,8 @@ func (suite *DeterministicTestSuite) TestGRPCValidators() {
suite.getStaticValidator()
suite.getStaticValidator2()

testdata.DeterministicIterations(suite.ctx, suite.Require(), &stakingtypes.QueryValidatorsRequest{}, suite.queryClient.Validators, 3525, false)
// NOTE: gas consumption delta changed from 3525 to 3597
testdata.DeterministicIterations(suite.ctx, suite.Require(), &stakingtypes.QueryValidatorsRequest{}, suite.queryClient.Validators, 3597, false)
}

func (suite *DeterministicTestSuite) TestGRPCValidatorDelegations() {
Expand Down Expand Up @@ -317,7 +318,8 @@ func (suite *DeterministicTestSuite) TestGRPCValidatorDelegations() {
ValidatorAddr: validator.OperatorAddress,
}

testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.ValidatorDelegations, 11985, false)
// NOTE: gas consumption delta changed from 11985 to 12615
testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.ValidatorDelegations, 12615, false)
}

func (suite *DeterministicTestSuite) TestGRPCValidatorUnbondingDelegations() {
Expand Down Expand Up @@ -390,7 +392,8 @@ func (suite *DeterministicTestSuite) TestGRPCDelegation() {
DelegatorAddr: delegator1,
}

testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.Delegation, 4635, false)
// NOTE: gas consumption delta changed from 4635 to 4845
testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.Delegation, 4845, false)
}

func (suite *DeterministicTestSuite) TestGRPCUnbondingDelegation() {
Expand Down Expand Up @@ -457,7 +460,8 @@ func (suite *DeterministicTestSuite) TestGRPCDelegatorDelegations() {
DelegatorAddr: delegator1,
}

testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.DelegatorDelegations, 4238, false)
// NOTE: gas consumption delta changed from 4238 to 4448
testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.DelegatorDelegations, 4448, false)
}

func (suite *DeterministicTestSuite) TestGRPCDelegatorValidator() {
Expand Down Expand Up @@ -488,7 +492,8 @@ func (suite *DeterministicTestSuite) TestGRPCDelegatorValidator() {
ValidatorAddr: validator.OperatorAddress,
}

testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.DelegatorValidator, 3563, false)
// NOTE: gas consumption delta changed from 3563 to 3581
testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.DelegatorValidator, 3581, false)
}

func (suite *DeterministicTestSuite) TestGRPCDelegatorUnbondingDelegations() {
Expand Down Expand Up @@ -579,7 +584,8 @@ func (suite *DeterministicTestSuite) TestGRPCHistoricalInfo() {
Height: height,
}

testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.HistoricalInfo, 1930, false)
// NOTE: gas consumption delta changed from 1930 to 1948
testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.HistoricalInfo, 1948, false)
}

func (suite *DeterministicTestSuite) TestGRPCDelegatorValidators() {
Expand Down Expand Up @@ -609,7 +615,9 @@ func (suite *DeterministicTestSuite) TestGRPCDelegatorValidators() {
suite.Require().NoError(err)

req := &stakingtypes.QueryDelegatorValidatorsRequest{DelegatorAddr: delegator1}
testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.DelegatorValidators, 3166, false)

// NOTE: gas consumption delta changed from 3166 to 3184
testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.DelegatorValidators, 3184, false)
}

func (suite *DeterministicTestSuite) TestGRPCPool() {
Expand All @@ -621,7 +629,9 @@ func (suite *DeterministicTestSuite) TestGRPCPool() {

suite.SetupTest() // reset
suite.getStaticValidator()
testdata.DeterministicIterations(suite.ctx, suite.Require(), &stakingtypes.QueryPoolRequest{}, suite.queryClient.Pool, 6185, false)

// NOTE: gas consumption delta changed from 6185 to 6377
testdata.DeterministicIterations(suite.ctx, suite.Require(), &stakingtypes.QueryPoolRequest{}, suite.queryClient.Pool, 6377, false)
}

func (suite *DeterministicTestSuite) TestGRPCRedelegations() {
Expand Down Expand Up @@ -683,18 +693,22 @@ func (suite *DeterministicTestSuite) TestGRPCRedelegations() {
DstValidatorAddr: validator2,
}

testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.Redelegations, 3920, false)
// NOTE: gas consumption delta changed from 3920 to 3938
testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.Redelegations, 3938, false)
}

func (suite *DeterministicTestSuite) TestGRPCParams() {
rapid.Check(suite.T(), func(t *rapid.T) {
params := stakingtypes.Params{
BondDenom: rapid.StringMatching(sdk.DefaultCoinDenomRegex()).Draw(t, "bond-denom"),
UnbondingTime: durationGenerator().Draw(t, "duration"),
MaxValidators: rapid.Uint32Min(1).Draw(t, "max-validators"),
MaxEntries: rapid.Uint32Min(1).Draw(t, "max-entries"),
HistoricalEntries: rapid.Uint32Min(1).Draw(t, "historical-entries"),
MinCommissionRate: sdk.NewDecWithPrec(rapid.Int64Range(0, 100).Draw(t, "commission"), 2),
BondDenom: rapid.StringMatching(sdk.DefaultCoinDenomRegex()).Draw(t, "bond-denom"),
UnbondingTime: durationGenerator().Draw(t, "duration"),
MaxValidators: rapid.Uint32Min(1).Draw(t, "max-validators"),
MaxEntries: rapid.Uint32Min(1).Draw(t, "max-entries"),
HistoricalEntries: rapid.Uint32Min(1).Draw(t, "historical-entries"),
MinCommissionRate: sdk.NewDecWithPrec(rapid.Int64Range(0, 100).Draw(t, "commission"), 2),
ValidatorBondFactor: sdk.NewDecWithPrec(rapid.Int64Range(0, 100).Draw(t, "bond-factor"), 2),
GlobalLiquidStakingCap: sdk.NewDecWithPrec(rapid.Int64Range(0, 100).Draw(t, "global-liquid-staking-cap"), 2),
ValidatorLiquidStakingCap: sdk.NewDecWithPrec(rapid.Int64Range(0, 100).Draw(t, "validator-liquid-staking-cap"), 2),
}

err := suite.stakingKeeper.SetParams(suite.ctx, params)
Expand All @@ -704,16 +718,20 @@ func (suite *DeterministicTestSuite) TestGRPCParams() {
})

params := stakingtypes.Params{
BondDenom: "denom",
UnbondingTime: time.Hour,
MaxValidators: 85,
MaxEntries: 5,
HistoricalEntries: 5,
MinCommissionRate: sdk.NewDecWithPrec(5, 2),
BondDenom: "denom",
UnbondingTime: time.Hour,
MaxValidators: 85,
MaxEntries: 5,
HistoricalEntries: 5,
MinCommissionRate: sdk.NewDecWithPrec(5, 2),
ValidatorBondFactor: sdk.NewDecWithPrec(18, 2),
GlobalLiquidStakingCap: sdk.NewDecWithPrec(11, 2),
ValidatorLiquidStakingCap: sdk.NewDecWithPrec(2, 2),
}

err := suite.stakingKeeper.SetParams(suite.ctx, params)
suite.Require().NoError(err)

testdata.DeterministicIterations(suite.ctx, suite.Require(), &stakingtypes.QueryParamsRequest{}, suite.queryClient.Params, 1114, false)
// NOTE: gas consumption delta changed from 1114 to 1291
testdata.DeterministicIterations(suite.ctx, suite.Require(), &stakingtypes.QueryParamsRequest{}, suite.queryClient.Params, 1291, false)
}
Loading

0 comments on commit b50f5a7

Please sign in to comment.