forked from EscanBE/evermint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain_suite_staking.go
55 lines (42 loc) · 2.26 KB
/
chain_suite_staking.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package integration_test_util
import (
itutiltypes "github.com/servprotocolorg/serv/v12/integration_test_util/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
// TxPrepareContextWithdrawDelegatorAndValidatorReward prepares context for withdraw delegator and validator reward.
// It does create delegation, allocate reward, commit state and wait a few blocks for reward to increase.
func (suite *ChainIntegrationTestSuite) TxPrepareContextWithdrawDelegatorAndValidatorReward(delegator *itutiltypes.TestAccount, delegate uint8, waitXBlocks uint8) (valAddr sdk.ValAddress) {
validatorAddr := suite.GetValidatorAddress(1)
val := suite.ChainApp.StakingKeeper().Validator(suite.CurrentContext, validatorAddr)
valReward := suite.NewBaseCoin(1)
delegationAmount := suite.NewBaseCoin(int64(delegate))
distAcc := suite.ChainApp.DistributionKeeper().GetDistributionAccount(suite.CurrentContext)
suite.MintCoinToModuleAccount(distAcc, suite.NewBaseCoin(int64(int(delegate)*int(10+waitXBlocks))))
suite.ChainApp.AccountKeeper().SetModuleAccount(suite.CurrentContext, distAcc)
suite.MintCoin(delegator, delegationAmount)
suite.Commit()
msgDelegate := &stakingtypes.MsgDelegate{
DelegatorAddress: delegator.GetCosmosAddress().String(),
ValidatorAddress: validatorAddr.String(),
Amount: delegationAmount,
}
_, _, err := suite.DeliverTx(suite.CurrentContext, delegator, nil, msgDelegate)
suite.Require().NoError(err)
suite.Commit()
for c := 1; c <= int(waitXBlocks); c++ {
suite.ChainApp.DistributionKeeper().AllocateTokensToValidator(suite.CurrentContext, val, sdk.NewDecCoinsFromCoins(valReward))
suite.Commit()
}
return validatorAddr
}
// GetValidatorAddress returns the validator address of the validator with the given number.
// Due to there is a bug that the validator address is delivered from tendermint pubkey instead of cosmos pubkey in tendermint mode.
// So this function is used to correct the validator address in tendermint mode.
func (suite *ChainIntegrationTestSuite) GetValidatorAddress(number int) sdk.ValAddress {
validator := suite.ValidatorAccounts.Number(number)
if suite.HasTendermint() {
return sdk.ValAddress(validator.GetTmPubKey().Address())
}
return validator.GetValidatorAddress()
}