Skip to content

Commit

Permalink
fix: emergency poll should be finished if voting period is over (#302)
Browse files Browse the repository at this point in the history
* emergency poll still need to be tallyed at normal schedule

* execute handleTallyResult if a proposal is expired

* write test

* fix lint

* handle emergency in different way

* bump go version for lint

* clear comment

* cap next tally time to voting end time

* simplify the condition
  • Loading branch information
beer-1 authored Nov 12, 2024
1 parent f7a3f0e commit 329e6e3
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.22
go-version: 1.23
check-latest: true
- uses: technote-space/[email protected]
id: git_diff
Expand Down Expand Up @@ -72,7 +72,7 @@ jobs:
- name: Setup go
uses: actions/setup-go@v5
with:
go-version: 1.22
go-version: 1.23
check-latest: true
- run: |
go mod tidy
Expand Down
9 changes: 8 additions & 1 deletion x/gov/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,15 @@ func EndBlocker(ctx sdk.Context, k *keeper.Keeper) error {
return false, err
}

if !quorumReached {
// schedule the next tally only if quorum is not reached and voting period is not over
if !quorumReached && proposal.VotingEndTime.After(ctx.BlockTime()) {
nextTallyTime := ctx.BlockTime().Add(params.EmergencyTallyInterval)

// if the next tally time is after the voting end time, set it to the voting end time
if nextTallyTime.After(*proposal.VotingEndTime) {
nextTallyTime = *proposal.VotingEndTime
}

if err = k.EmergencyProposalsQueue.Set(ctx, collections.Join(nextTallyTime, proposal.Id), proposal.Id); err != nil {
return false, err
}
Expand Down
49 changes: 49 additions & 0 deletions x/gov/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,52 @@ func TestTickSingleProposal(t *testing.T) {
})
}
}

func TestEmergencyProposal_Rejected_VotingPeriodOver(t *testing.T) {
app := createAppWithSimpleValidators(t)
ctx := app.BaseApp.NewContext(false)
initTime := ctx.BlockHeader().Time

govMsgSvr := keeper.NewMsgServerImpl(app.GovKeeper)
propMsg := createTextProposalMsg(t, emergencyMinDeposit[0].Amount.Int64(), false)
_, err := govMsgSvr.SubmitProposal(ctx, propMsg)
require.NoError(t, err)

newHeader := ctx.BlockHeader()
newHeader.Time = ctx.BlockHeader().Time.Add(time.Minute)
ctx = ctx.WithBlockHeader(newHeader)

proposal, err := app.GovKeeper.Proposals.Get(ctx, 1)
require.NoError(t, err)
require.True(t, proposal.Emergency)
require.True(t, proposal.EmergencyStartTime.Equal(ctx.BlockTime().Add(-time.Minute)))
require.True(t, proposal.EmergencyNextTallyTime.Equal(ctx.BlockTime().Add(emergencyTallyInterval-time.Minute)))
require.True(t, proposal.SubmitTime.Equal(initTime))
require.True(t, proposal.DepositEndTime.Equal(initTime.Add(depositPeriod)))
require.Equal(t, proposal.Status, v1.StatusVotingPeriod)
require.True(t, proposal.VotingStartTime.Equal(ctx.BlockTime().Add(-time.Minute)))
require.True(t, proposal.VotingEndTime.Equal(ctx.BlockTime().Add(votingPeriod-time.Minute)))

// not enough votes

newHeader = ctx.BlockHeader()
newHeader.Time = ctx.BlockHeader().Time.Add(time.Minute)
ctx = ctx.WithBlockHeader(newHeader)

err = gov.EndBlocker(ctx, app.GovKeeper)
require.NoError(t, err)
proposal, err = app.GovKeeper.Proposals.Get(ctx, 1)
require.NoError(t, err)
require.Equal(t, proposal.Status, v1.StatusVotingPeriod)

// Voting period is over; the proposal should be finished
newHeader = ctx.BlockHeader()
newHeader.Time = ctx.BlockHeader().Time.Add(votingPeriod)
ctx = ctx.WithBlockHeader(newHeader)

err = gov.EndBlocker(ctx, app.GovKeeper)
require.NoError(t, err)
proposal, err = app.GovKeeper.Proposals.Get(ctx, 1)
require.NoError(t, err)
require.Equal(t, proposal.Status, v1.StatusRejected)
}
6 changes: 3 additions & 3 deletions x/gov/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var (
emergencyTallyInterval = time.Minute * 10
)

func createDefaultApp(t *testing.T) *initiaapp.InitiaApp {
func createDefaultApp(_ *testing.T) *initiaapp.InitiaApp {
app := initiaapp.SetupWithGenesisAccounts(nil, authtypes.GenesisAccounts{
&authtypes.BaseAccount{Address: addrs[0].String()},
},
Expand Down Expand Up @@ -124,7 +124,7 @@ func createTextProposalMsg(t *testing.T, initialTokenAmount int64, expedited boo
return newProposalMsg
}

func createDepositMsg(t *testing.T, depositor sdk.AccAddress, proposalID uint64, amount sdk.Coins) *v1.MsgDeposit {
func createDepositMsg(_ *testing.T, depositor sdk.AccAddress, proposalID uint64, amount sdk.Coins) *v1.MsgDeposit {
newDepositMsg := v1.NewMsgDeposit(
depositor,
proposalID,
Expand All @@ -133,7 +133,7 @@ func createDepositMsg(t *testing.T, depositor sdk.AccAddress, proposalID uint64,
return newDepositMsg
}

func createVoteMsg(t *testing.T, voter sdk.AccAddress, proposalID uint64, option v1.VoteOption) *v1.MsgVote {
func createVoteMsg(_ *testing.T, voter sdk.AccAddress, proposalID uint64, option v1.VoteOption) *v1.MsgVote {
newVoteMsg := v1.NewMsgVote(
voter,
proposalID,
Expand Down
3 changes: 3 additions & 0 deletions x/gov/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ func (p Params) Validate(ac address.Codec) error {
if p.EmergencyTallyInterval.Seconds() <= 0 {
return fmt.Errorf("emergency tally interval must be positive: %s", p.EmergencyTallyInterval)
}
if p.EmergencyTallyInterval.Seconds() >= p.VotingPeriod.Seconds() {
return fmt.Errorf("emergency tally interval %s must be strictly less than the voting period %s", p.EmergencyTallyInterval, p.VotingPeriod)
}

if minEmergencyDeposit := sdk.Coins(p.EmergencyMinDeposit); !minEmergencyDeposit.IsAllGTE(p.ExpeditedMinDeposit) {
return fmt.Errorf("emergency minimum deposit must be greater than or equal to minimum deposit")
Expand Down
3 changes: 3 additions & 0 deletions x/gov/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func Test_Params(t *testing.T) {
params.EmergencyTallyInterval = 0
require.Error(t, params.Validate(ac))

params.EmergencyTallyInterval = params.VotingPeriod
require.Error(t, params.Validate(ac))

params.EmergencyTallyInterval = _emergencyTallyInterval

_minDeposit := params.MinDeposit
Expand Down

0 comments on commit 329e6e3

Please sign in to comment.