@@ -7,100 +7,85 @@ import (
77 "github.com/cosmos/cosmos-sdk/x/gov/tags"
88)
99
10- // Called every block, process inflation, update validator set
10+ // EndBlocker called every block, process inflation, update validator set
1111func EndBlocker (ctx sdk.Context , keeper Keeper ) sdk.Tags {
1212 logger := keeper .Logger (ctx )
1313 resTags := sdk .NewTags ()
1414
15- inactiveIterator := keeper . InactiveProposalQueueIterator ( ctx , ctx . BlockHeader (). Time )
16- defer inactiveIterator . Close ()
17- for ; inactiveIterator . Valid (); inactiveIterator . Next () {
18- var proposalID uint64
15+ // delete inactive proposal from store and its deposits
16+ keeper . IterateInactiveProposalsQueue ( ctx , ctx . BlockHeader (). Time , func ( proposal Proposal ) bool {
17+ keeper . DeleteProposal ( ctx , proposal . ProposalID )
18+ keeper . DeleteDeposits ( ctx , proposal . ProposalID )
1919
20- keeper .cdc .MustUnmarshalBinaryLengthPrefixed (inactiveIterator .Value (), & proposalID )
21- inactiveProposal , ok := keeper .GetProposal (ctx , proposalID )
22- if ! ok {
23- panic (fmt .Sprintf ("proposal %d does not exist" , proposalID ))
24- }
25-
26- keeper .DeleteProposal (ctx , proposalID )
27- keeper .DeleteDeposits (ctx , proposalID ) // delete any associated deposits (burned)
28-
29- resTags = resTags .AppendTag (tags .ProposalID , fmt .Sprintf ("%d" , proposalID ))
20+ resTags = resTags .AppendTag (tags .ProposalID , fmt .Sprintf ("%d" , proposal .ProposalID ))
3021 resTags = resTags .AppendTag (tags .ProposalResult , tags .ActionProposalDropped )
3122
3223 logger .Info (
3324 fmt .Sprintf ("proposal %d (%s) didn't meet minimum deposit of %s (had only %s); deleted" ,
34- inactiveProposal .ProposalID ,
35- inactiveProposal .GetTitle (),
25+ proposal .ProposalID ,
26+ proposal .GetTitle (),
3627 keeper .GetDepositParams (ctx ).MinDeposit ,
37- inactiveProposal .TotalDeposit ,
28+ proposal .TotalDeposit ,
3829 ),
3930 )
40- }
31+ return false
32+ })
4133
4234 // fetch active proposals whose voting periods have ended (are passed the block time)
43- activeIterator := keeper .ActiveProposalQueueIterator (ctx , ctx .BlockHeader ().Time )
44- defer activeIterator .Close ()
45- for ; activeIterator .Valid (); activeIterator .Next () {
46- var proposalID uint64
47-
48- keeper .cdc .MustUnmarshalBinaryLengthPrefixed (activeIterator .Value (), & proposalID )
49- activeProposal , ok := keeper .GetProposal (ctx , proposalID )
50- if ! ok {
51- panic (fmt .Sprintf ("proposal %d does not exist" , proposalID ))
52- }
53- passes , burnDeposits , tallyResults := tally (ctx , keeper , activeProposal )
54-
35+ keeper .IterateActiveProposalsQueue (ctx , ctx .BlockHeader ().Time , func (proposal Proposal ) bool {
5536 var tagValue , logMsg string
5637
38+ passes , burnDeposits , tallyResults := tally (ctx , keeper , proposal )
39+
5740 if burnDeposits {
58- keeper .DeleteDeposits (ctx , activeProposal .ProposalID )
41+ keeper .DeleteDeposits (ctx , proposal .ProposalID )
5942 } else {
60- keeper .RefundDeposits (ctx , activeProposal .ProposalID )
43+ keeper .RefundDeposits (ctx , proposal .ProposalID )
6144 }
6245
6346 if passes {
64- handler := keeper .router .GetRoute (activeProposal .ProposalRoute ())
47+ handler := keeper .router .GetRoute (proposal .ProposalRoute ())
6548 cacheCtx , writeCache := ctx .CacheContext ()
6649
6750 // The proposal handler may execute state mutating logic depending
6851 // on the proposal content. If the handler fails, no state mutation
6952 // is written and the error message is logged.
70- err := handler (cacheCtx , activeProposal .Content )
53+ err := handler (cacheCtx , proposal .Content )
7154 if err == nil {
72- activeProposal .Status = StatusPassed
55+ proposal .Status = StatusPassed
7356 tagValue = tags .ActionProposalPassed
7457 logMsg = "passed"
7558
7659 // write state to the underlying multi-store
7760 writeCache ()
7861 } else {
79- activeProposal .Status = StatusFailed
62+ proposal .Status = StatusFailed
8063 tagValue = tags .ActionProposalFailed
8164 logMsg = fmt .Sprintf ("passed, but failed on execution: %s" , err .ABCILog ())
8265 }
8366 } else {
84- activeProposal .Status = StatusRejected
67+ proposal .Status = StatusRejected
8568 tagValue = tags .ActionProposalRejected
8669 logMsg = "rejected"
8770 }
8871
89- activeProposal .FinalTallyResult = tallyResults
72+ proposal .FinalTallyResult = tallyResults
9073
91- keeper .SetProposal (ctx , activeProposal )
92- keeper .RemoveFromActiveProposalQueue (ctx , activeProposal . VotingEndTime , activeProposal . ProposalID )
74+ keeper .SetProposal (ctx , proposal )
75+ keeper .RemoveFromActiveProposalQueue (ctx , proposal . ProposalID , proposal . VotingEndTime )
9376
9477 logger .Info (
9578 fmt .Sprintf (
9679 "proposal %d (%s) tallied; result: %s" ,
97- activeProposal .ProposalID , activeProposal .GetTitle (), logMsg ,
80+ proposal .ProposalID , proposal .GetTitle (), logMsg ,
9881 ),
9982 )
10083
101- resTags = resTags .AppendTag (tags .ProposalID , fmt .Sprintf ("%d" , proposalID ))
84+ resTags = resTags .AppendTag (tags .ProposalID , fmt .Sprintf ("%d" , proposal . ProposalID ))
10285 resTags = resTags .AppendTag (tags .ProposalResult , tagValue )
103- }
86+
87+ return false
88+ })
10489
10590 return resTags
10691}
0 commit comments