Skip to content

Commit

Permalink
test: add some tally tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Aug 30, 2024
1 parent 21a6030 commit 2416aee
Show file tree
Hide file tree
Showing 7 changed files with 428 additions and 29 deletions.
60 changes: 41 additions & 19 deletions x/gov/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,34 @@ func getTestProposal() []sdk.Msg {
}
}

type mocks struct {
acctKeeper *govtestutil.MockAccountKeeper
bankKeeper *govtestutil.MockBankKeeper
stakingKeeper *govtestutil.MockStakingKeeper
}

func mockAccountKeeperExpectations(ctx sdk.Context, m mocks) {
m.acctKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(govAcct).AnyTimes()
m.acctKeeper.EXPECT().GetModuleAccount(gomock.Any(), types.ModuleName).Return(authtypes.NewEmptyModuleAccount(types.ModuleName)).AnyTimes()
}

func mockDefaultExpectations(ctx sdk.Context, m mocks) {
mockAccountKeeperExpectations(ctx, m)
trackMockBalances(m.bankKeeper)
m.stakingKeeper.EXPECT().TokensFromConsensusPower(ctx, gomock.Any()).DoAndReturn(func(ctx sdk.Context, power int64) math.Int {
return sdk.TokensFromConsensusPower(power, math.NewIntFromUint64(1000000))
}).AnyTimes()

m.stakingKeeper.EXPECT().BondDenom(ctx).Return("stake").AnyTimes()
m.stakingKeeper.EXPECT().IterateBondedValidatorsByPower(gomock.Any(), gomock.Any()).AnyTimes()
m.stakingKeeper.EXPECT().IterateDelegations(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
m.stakingKeeper.EXPECT().TotalBondedTokens(gomock.Any()).Return(math.NewInt(10000000)).AnyTimes()
}

// setupGovKeeper creates a govKeeper as well as all its dependencies.
func setupGovKeeper(t *testing.T) (
func setupGovKeeper(t *testing.T, expectations ...func(sdk.Context, mocks)) (
*keeper.Keeper,
*govtestutil.MockAccountKeeper,
*govtestutil.MockBankKeeper,
*govtestutil.MockStakingKeeper,
mocks,
moduletestutil.TestEncodingConfig,
sdk.Context,
) {
Expand All @@ -69,23 +91,23 @@ func setupGovKeeper(t *testing.T) (

// gomock initializations
ctrl := gomock.NewController(t)
acctKeeper := govtestutil.NewMockAccountKeeper(ctrl)
bankKeeper := govtestutil.NewMockBankKeeper(ctrl)
stakingKeeper := govtestutil.NewMockStakingKeeper(ctrl)
acctKeeper.EXPECT().GetModuleAddress(types.ModuleName).Return(govAcct).AnyTimes()
acctKeeper.EXPECT().GetModuleAccount(gomock.Any(), types.ModuleName).Return(authtypes.NewEmptyModuleAccount(types.ModuleName)).AnyTimes()
trackMockBalances(bankKeeper)
stakingKeeper.EXPECT().TokensFromConsensusPower(ctx, gomock.Any()).DoAndReturn(func(ctx sdk.Context, power int64) math.Int {
return sdk.TokensFromConsensusPower(power, math.NewIntFromUint64(1000000))
}).AnyTimes()
stakingKeeper.EXPECT().BondDenom(ctx).Return("stake").AnyTimes()
stakingKeeper.EXPECT().IterateBondedValidatorsByPower(gomock.Any(), gomock.Any()).AnyTimes()
stakingKeeper.EXPECT().IterateDelegations(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
stakingKeeper.EXPECT().TotalBondedTokens(gomock.Any()).Return(math.NewInt(10000000)).AnyTimes()
m := mocks{
acctKeeper: govtestutil.NewMockAccountKeeper(ctrl),
bankKeeper: govtestutil.NewMockBankKeeper(ctrl),
stakingKeeper: govtestutil.NewMockStakingKeeper(ctrl),
}
if len(expectations) == 0 {
mockDefaultExpectations(ctx, m)
} else {
for _, exp := range expectations {
exp(ctx, m)
}
}

// Gov keeper initializations
govKeeper := keeper.NewKeeper(encCfg.Codec, key, acctKeeper, bankKeeper, stakingKeeper, msr, types.DefaultConfig(), govAcct.String())
govKeeper := keeper.NewKeeper(encCfg.Codec, key, m.acctKeeper, m.bankKeeper, m.stakingKeeper, msr, types.DefaultConfig(), govAcct.String())
govKeeper.SetProposalID(ctx, 1)

govRouter := v1beta1.NewRouter() // Also register legacy gov handlers to test them too.
govRouter.AddRoute(types.RouterKey, v1beta1.ProposalHandler)
govKeeper.SetLegacyRouter(govRouter)
Expand All @@ -96,7 +118,7 @@ func setupGovKeeper(t *testing.T) (
v1.RegisterMsgServer(msr, keeper.NewMsgServerImpl(govKeeper))
banktypes.RegisterMsgServer(msr, nil) // Nil is fine here as long as we never execute the proposal's Msgs.

return govKeeper, acctKeeper, bankKeeper, stakingKeeper, encCfg, ctx
return govKeeper, m, encCfg, ctx
}

// trackMockBalances sets up expected calls on the Mock BankKeeper, and also
Expand Down
5 changes: 3 additions & 2 deletions x/gov/keeper/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const (
)

func TestDeposits(t *testing.T) {
govKeeper, _, bankKeeper, stakingKeeper, _, ctx := setupGovKeeper(t)
govKeeper, mocks, _, ctx := setupGovKeeper(t)
bankKeeper, stakingKeeper := mocks.bankKeeper, mocks.stakingKeeper
trackMockBalances(bankKeeper)
TestAddrs := simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, ctx, 2, sdk.NewInt(10000000))

Expand Down Expand Up @@ -193,7 +194,7 @@ func TestValidateInitialDeposit(t *testing.T) {

for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
govKeeper, _, _, _, _, ctx := setupGovKeeper(t)
govKeeper, _, _, ctx := setupGovKeeper(t)

params := v1.DefaultParams()
params.MinDeposit = tc.minDeposit
Expand Down
3 changes: 2 additions & 1 deletion x/gov/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func (h *MockGovHooksReceiver) AfterProposalVotingPeriodEnded(ctx sdk.Context, p

func TestHooks(t *testing.T) {
minDeposit := v1.DefaultParams().MinDeposit
govKeeper, _, bankKeeper, stakingKeeper, _, ctx := setupGovKeeper(t)
govKeeper, mocks, _, ctx := setupGovKeeper(t)
bankKeeper, stakingKeeper := mocks.bankKeeper, mocks.stakingKeeper
addrs := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 1, minDeposit[0].Amount)

govHooksReceiver := MockGovHooksReceiver{}
Expand Down
7 changes: 4 additions & 3 deletions x/gov/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func (suite *KeeperTestSuite) SetupSuite() {
}

func (suite *KeeperTestSuite) reset() {
govKeeper, acctKeeper, bankKeeper, stakingKeeper, encCfg, ctx := setupGovKeeper(suite.T())
govKeeper, mocks, encCfg, ctx := setupGovKeeper(suite.T())
acctKeeper, bankKeeper, stakingKeeper := mocks.acctKeeper, mocks.bankKeeper, mocks.stakingKeeper

// Populate the gov account with some coins, as the TestProposal we have
// is a MsgSend from the gov account.
Expand Down Expand Up @@ -72,7 +73,7 @@ func (suite *KeeperTestSuite) reset() {
}

func TestIncrementProposalNumber(t *testing.T) {
govKeeper, _, _, _, _, ctx := setupGovKeeper(t)
govKeeper, _, _, ctx := setupGovKeeper(t)

tp := TestProposal
_, err := govKeeper.SubmitProposal(ctx, tp, "", "test", "summary", sdk.AccAddress("cosmos1ghekyjucln7y67ntx7cf27m9dpuxxemn4c8g4r"))
Expand All @@ -92,7 +93,7 @@ func TestIncrementProposalNumber(t *testing.T) {
}

func TestProposalQueues(t *testing.T) {
govKeeper, _, _, _, _, ctx := setupGovKeeper(t)
govKeeper, _, _, ctx := setupGovKeeper(t)

// create test proposals
tp := TestProposal
Expand Down
5 changes: 3 additions & 2 deletions x/gov/keeper/tally.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ func (keeper Keeper) Tally(ctx sdk.Context, proposal v1.Proposal) (passes bool,

// TODO: Upgrade the spec to cover all of these cases & remove pseudocode.
// If there is no staked coins, the proposal fails
if keeper.sk.TotalBondedTokens(ctx).IsZero() {
totalBondedTokens := keeper.sk.TotalBondedTokens(ctx)
if totalBondedTokens.IsZero() {
return false, false, tallyResults
}

// If there is not enough quorum of votes, the proposal fails
percentVoting := totalVotingPower.Quo(sdk.NewDecFromInt(keeper.sk.TotalBondedTokens(ctx)))
percentVoting := totalVotingPower.Quo(sdk.NewDecFromInt(totalBondedTokens))
quorum, _ := sdk.NewDecFromStr(params.Quorum)
if percentVoting.LT(quorum) {
return false, params.BurnVoteQuorum, tallyResults
Expand Down
Loading

0 comments on commit 2416aee

Please sign in to comment.