2019-02-08 18:33:06 -08:00
|
|
|
package gov
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/tags"
|
|
|
|
)
|
|
|
|
|
2019-06-04 11:38:11 -07:00
|
|
|
// EndBlocker called every block, process inflation, update validator set
|
2019-04-05 13:20:58 -07:00
|
|
|
func EndBlocker(ctx sdk.Context, keeper Keeper) sdk.Tags {
|
2019-04-15 20:58:06 -07:00
|
|
|
logger := keeper.Logger(ctx)
|
2019-02-08 18:33:06 -08:00
|
|
|
resTags := sdk.NewTags()
|
|
|
|
|
2019-06-04 11:38:11 -07:00
|
|
|
// delete inactive proposal from store and its deposits
|
|
|
|
keeper.IterateInactiveProposalsQueue(ctx, ctx.BlockHeader().Time, func(proposal Proposal) bool {
|
|
|
|
keeper.DeleteProposal(ctx, proposal.ProposalID)
|
|
|
|
keeper.DeleteDeposits(ctx, proposal.ProposalID)
|
2019-02-08 18:33:06 -08:00
|
|
|
|
2019-06-04 11:38:11 -07:00
|
|
|
resTags = resTags.AppendTag(tags.ProposalID, fmt.Sprintf("%d", proposal.ProposalID))
|
2019-02-08 18:33:06 -08:00
|
|
|
resTags = resTags.AppendTag(tags.ProposalResult, tags.ActionProposalDropped)
|
|
|
|
|
|
|
|
logger.Info(
|
|
|
|
fmt.Sprintf("proposal %d (%s) didn't meet minimum deposit of %s (had only %s); deleted",
|
2019-06-04 11:38:11 -07:00
|
|
|
proposal.ProposalID,
|
|
|
|
proposal.GetTitle(),
|
2019-02-08 18:33:06 -08:00
|
|
|
keeper.GetDepositParams(ctx).MinDeposit,
|
2019-06-04 11:38:11 -07:00
|
|
|
proposal.TotalDeposit,
|
2019-02-08 18:33:06 -08:00
|
|
|
),
|
|
|
|
)
|
2019-06-04 11:38:11 -07:00
|
|
|
return false
|
|
|
|
})
|
2019-02-08 18:33:06 -08:00
|
|
|
|
|
|
|
// fetch active proposals whose voting periods have ended (are passed the block time)
|
2019-06-04 11:38:11 -07:00
|
|
|
keeper.IterateActiveProposalsQueue(ctx, ctx.BlockHeader().Time, func(proposal Proposal) bool {
|
2019-04-30 09:31:38 -07:00
|
|
|
var tagValue, logMsg string
|
|
|
|
|
2019-06-04 11:38:11 -07:00
|
|
|
passes, burnDeposits, tallyResults := tally(ctx, keeper, proposal)
|
|
|
|
|
2019-05-20 07:13:50 -07:00
|
|
|
if burnDeposits {
|
2019-06-04 11:38:11 -07:00
|
|
|
keeper.DeleteDeposits(ctx, proposal.ProposalID)
|
2019-05-20 07:13:50 -07:00
|
|
|
} else {
|
2019-06-04 11:38:11 -07:00
|
|
|
keeper.RefundDeposits(ctx, proposal.ProposalID)
|
2019-05-20 07:13:50 -07:00
|
|
|
}
|
2019-04-30 09:31:38 -07:00
|
|
|
|
2019-05-20 07:13:50 -07:00
|
|
|
if passes {
|
2019-06-04 11:38:11 -07:00
|
|
|
handler := keeper.router.GetRoute(proposal.ProposalRoute())
|
2019-04-30 09:31:38 -07:00
|
|
|
cacheCtx, writeCache := ctx.CacheContext()
|
|
|
|
|
|
|
|
// The proposal handler may execute state mutating logic depending
|
|
|
|
// on the proposal content. If the handler fails, no state mutation
|
|
|
|
// is written and the error message is logged.
|
2019-06-04 11:38:11 -07:00
|
|
|
err := handler(cacheCtx, proposal.Content)
|
2019-04-30 09:31:38 -07:00
|
|
|
if err == nil {
|
2019-06-04 11:38:11 -07:00
|
|
|
proposal.Status = StatusPassed
|
2019-04-30 09:31:38 -07:00
|
|
|
tagValue = tags.ActionProposalPassed
|
|
|
|
logMsg = "passed"
|
|
|
|
|
|
|
|
// write state to the underlying multi-store
|
|
|
|
writeCache()
|
|
|
|
} else {
|
2019-06-04 11:38:11 -07:00
|
|
|
proposal.Status = StatusFailed
|
2019-04-30 09:31:38 -07:00
|
|
|
tagValue = tags.ActionProposalFailed
|
|
|
|
logMsg = fmt.Sprintf("passed, but failed on execution: %s", err.ABCILog())
|
|
|
|
}
|
2019-02-08 18:33:06 -08:00
|
|
|
} else {
|
2019-06-04 11:38:11 -07:00
|
|
|
proposal.Status = StatusRejected
|
2019-02-08 18:33:06 -08:00
|
|
|
tagValue = tags.ActionProposalRejected
|
2019-04-30 09:31:38 -07:00
|
|
|
logMsg = "rejected"
|
2019-02-08 18:33:06 -08:00
|
|
|
}
|
|
|
|
|
2019-06-04 11:38:11 -07:00
|
|
|
proposal.FinalTallyResult = tallyResults
|
2019-04-30 09:31:38 -07:00
|
|
|
|
2019-06-04 11:38:11 -07:00
|
|
|
keeper.SetProposal(ctx, proposal)
|
|
|
|
keeper.RemoveFromActiveProposalQueue(ctx, proposal.ProposalID, proposal.VotingEndTime)
|
2019-02-08 18:33:06 -08:00
|
|
|
|
|
|
|
logger.Info(
|
|
|
|
fmt.Sprintf(
|
2019-04-30 09:31:38 -07:00
|
|
|
"proposal %d (%s) tallied; result: %s",
|
2019-06-04 11:38:11 -07:00
|
|
|
proposal.ProposalID, proposal.GetTitle(), logMsg,
|
2019-02-08 18:33:06 -08:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2019-06-04 11:38:11 -07:00
|
|
|
resTags = resTags.AppendTag(tags.ProposalID, fmt.Sprintf("%d", proposal.ProposalID))
|
2019-02-08 18:33:06 -08:00
|
|
|
resTags = resTags.AppendTag(tags.ProposalResult, tagValue)
|
2019-06-04 11:38:11 -07:00
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
2019-02-08 18:33:06 -08:00
|
|
|
|
2019-04-05 13:20:58 -07:00
|
|
|
return resTags
|
2019-02-08 18:33:06 -08:00
|
|
|
}
|