cosmos-sdk/x/distribution/abci_test.go

108 lines
4.3 KiB
Go
Raw Normal View History

test: verify logic for proposer reward (#11517) ## Description Closes: #9161 I have added a test to confirm (or not) the proposer reward bug from the above issue. It seems that there is no bug, and it behaves as it should. The relevant logic that ensure that is here: - https://github.com/cosmos/cosmos-sdk/blob/7fc7b3f6ff82eb5ede52881778114f6b38bd7dfa/x/distribution/keeper/allocation.go#L56 - https://github.com/cosmos/cosmos-sdk/blob/7fc7b3f6ff82eb5ede52881778114f6b38bd7dfa/x/distribution/keeper/allocation.go#L67 I am still opening this PR as adding this test can't do wrong. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2022-04-04 08:05:05 -07:00
package distribution_test
import (
"testing"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)
const (
totalValidators = 6
lazyValidatorIdx = 2
power = 100 / totalValidators
)
var (
valTokens = sdk.TokensFromConsensusPower(50, sdk.DefaultPowerReduction)
validatorCommissionRates = stakingtypes.NewCommissionRates(sdk.OneDec(), sdk.OneDec(), sdk.OneDec())
)
type validator struct {
addr sdk.ValAddress
pubkey cryptotypes.PubKey
votes []abci.VoteInfo
}
// Context in https://github.com/cosmos/cosmos-sdk/issues/9161
func TestVerifyProposerRewardAssignement(t *testing.T) {
app := simapp.Setup(t, false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
addrs := simapp.AddTestAddrsIncremental(app, ctx, totalValidators, valTokens)
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
tstaking.Commission = validatorCommissionRates
// create validators
validators := make([]validator, totalValidators-1)
for i := range validators {
validators[i].addr = sdk.ValAddress(addrs[i])
validators[i].pubkey = ed25519.GenPrivKey().PubKey()
validators[i].votes = make([]abci.VoteInfo, totalValidators)
tstaking.CreateValidatorWithValPower(validators[i].addr, validators[i].pubkey, power, true)
}
app.EndBlock(abci.RequestEndBlock{})
require.NotEmpty(t, app.Commit())
// verify validators lists
require.Len(t, app.StakingKeeper.GetAllValidators(ctx), totalValidators)
for i, val := range validators {
// verify all validator exists
require.NotNil(t, app.StakingKeeper.ValidatorByConsAddr(ctx, sdk.GetConsAddress(val.pubkey)))
// populate last commit info
voteInfos := []abci.VoteInfo{}
for _, val2 := range validators {
voteInfos = append(voteInfos, abci.VoteInfo{
Validator: abci.Validator{
Address: sdk.GetConsAddress(val2.pubkey),
Power: power,
},
SignedLastBlock: true,
})
}
// have this validator only submit the minimum amount of pre-commits
if i == lazyValidatorIdx {
for j := totalValidators * 2 / 3; j < len(voteInfos); j++ {
voteInfos[j].SignedLastBlock = false
}
}
validators[i].votes = voteInfos
}
// previous block submitted by validator n-1 (with 100% previous commits) and proposed by lazy validator
app.BeginBlock(abci.RequestBeginBlock{
Header: tmproto.Header{Height: app.LastBlockHeight() + 1, ProposerAddress: sdk.GetConsAddress(validators[lazyValidatorIdx].pubkey)},
LastCommitInfo: abci.LastCommitInfo{Votes: validators[lazyValidatorIdx-1].votes},
})
require.NotEmpty(t, app.Commit())
// previous block submitted by lazy validator (with 67% previous commits) and proposed by validator n+1
app.BeginBlock(abci.RequestBeginBlock{
Header: tmproto.Header{Height: app.LastBlockHeight() + 1, ProposerAddress: sdk.GetConsAddress(validators[lazyValidatorIdx+1].pubkey)},
LastCommitInfo: abci.LastCommitInfo{Votes: validators[lazyValidatorIdx].votes},
})
require.NotEmpty(t, app.Commit())
// previous block submitted by validator n+1 (with 100% previous commits) and proposed by validator n+2
app.BeginBlock(abci.RequestBeginBlock{
Header: tmproto.Header{Height: app.LastBlockHeight() + 1, ProposerAddress: sdk.GetConsAddress(validators[lazyValidatorIdx+2].pubkey)},
LastCommitInfo: abci.LastCommitInfo{Votes: validators[lazyValidatorIdx+1].votes},
})
require.NotEmpty(t, app.Commit())
rewardsValidatorBeforeLazyValidator := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, validators[lazyValidatorIdx+1].addr)
rewardsLazyValidator := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, validators[lazyValidatorIdx].addr)
rewardsValidatorAfterLazyValidator := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, validators[lazyValidatorIdx+1].addr)
require.True(t, rewardsLazyValidator[0].Amount.LT(rewardsValidatorAfterLazyValidator[0].Amount))
require.Equal(t, rewardsValidatorBeforeLazyValidator, rewardsValidatorAfterLazyValidator)
}