-
Notifications
You must be signed in to change notification settings - Fork 182
/
endblocker.go
105 lines (90 loc) · 3.22 KB
/
endblocker.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package gov
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/okex/exchain/x/gov/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/okex/exchain/x/common/perf"
"github.com/okex/exchain/x/gov/keeper"
)
// EndBlocker called every block, process inflation, update validator set.
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
logger := k.Logger(ctx)
seq := perf.GetPerf().OnEndBlockEnter(ctx, types.ModuleName)
defer perf.GetPerf().OnEndBlockExit(ctx, types.ModuleName, seq)
handleWaitingProposals(ctx, k, logger)
handleInActiveProposals(ctx, k, logger)
handleActiveProposals(ctx, k, logger)
}
// handle proposals which is executed on future block height appointed when it is submitted
func handleWaitingProposals(ctx sdk.Context, k keeper.Keeper, logger log.Logger) {
k.IterateWaitingProposalsQueue(ctx, uint64(ctx.BlockHeight()), func(proposal Proposal) bool {
handler := k.Router().GetRoute(proposal.ProposalRoute())
cacheCtx, writeCache := ctx.CacheContext()
err := handler(cacheCtx, &proposal)
if err != nil {
logger.Info(
fmt.Sprintf("proposal %d (%s) excute failed",
proposal.ProposalID,
proposal.GetTitle(),
),
)
} else {
logger.Info(
fmt.Sprintf("proposal %d (%s) excute successfully",
proposal.ProposalID,
proposal.GetTitle(),
),
)
writeCache()
}
return false
})
}
func handleInActiveProposals(ctx sdk.Context, k keeper.Keeper, logger log.Logger) {
// delete inactive proposal from store and its deposits
k.IterateInactiveProposalsQueue(ctx, ctx.BlockHeader().Time, func(proposal Proposal) bool {
k.DeleteProposal(ctx, proposal.ProposalID)
k.DistributeDeposits(ctx, proposal.ProposalID)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeInactiveProposal,
sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposal.ProposalID)),
sdk.NewAttribute(types.AttributeKeyProposalResult, types.AttributeValueProposalDropped),
),
)
logger.Info(
fmt.Sprintf("proposal %d (%s) didn't meet minimum deposit of %s (had only %s); deleted",
proposal.ProposalID,
proposal.GetTitle(),
k.GetDepositParams(ctx).MinDeposit,
proposal.TotalDeposit,
),
)
return false
})
}
func handleActiveProposals(ctx sdk.Context, k keeper.Keeper, logger log.Logger) {
// fetch active proposals whose voting periods have ended (are passed the block time)
k.IterateActiveProposalsQueue(ctx, ctx.BlockHeader().Time, func(proposal Proposal) bool {
status, distribute, tallyResults := keeper.Tally(ctx, k, proposal, true)
tagValue, logMsg := handleProposalAfterTally(ctx, k, &proposal, distribute, status)
proposal.FinalTallyResult = tallyResults
k.SetProposal(ctx, proposal)
k.RemoveFromActiveProposalQueue(ctx, proposal.ProposalID, proposal.VotingEndTime)
k.DeleteVotes(ctx, proposal.ProposalID)
logger.Info(
fmt.Sprintf("proposal %d (%s) tallied; result: %s",
proposal.ProposalID, proposal.GetTitle(), logMsg,
),
)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeActiveProposal,
sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposal.ProposalID)),
sdk.NewAttribute(types.AttributeKeyProposalResult, tagValue),
),
)
return false
})
}