-
Notifications
You must be signed in to change notification settings - Fork 11
/
abci.go
74 lines (63 loc) · 2.2 KB
/
abci.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
package sync
import (
"fmt"
"github.com/celer-network/goutils/log"
"github.com/celer-network/sgn/x/sync/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking"
)
// EndBlocker called every block, process inflation, update validator set.
func EndBlocker(ctx sdk.Context, keeper Keeper) {
var tagValue string
validators := keeper.GetBondedValidators(ctx)
totalToken := sdk.ZeroInt()
validatorsByAddr := map[string]staking.Validator{}
for _, validator := range validators {
totalToken = totalToken.Add(validator.Tokens)
validatorsByAddr[validator.OperatorAddress.String()] = validator
}
threshold := keeper.GetTallyParams(ctx).Threshold.MulInt(totalToken).TruncateInt()
pullerReward := keeper.PullerReward(ctx)
activeChanges := keeper.GetActiveChanges(ctx)
for _, change := range activeChanges {
totalVote := sdk.ZeroInt()
for _, voter := range change.Voters {
v, ok := validatorsByAddr[voter.String()]
if !ok {
continue
}
totalVote = totalVote.Add(v.Tokens)
}
if totalVote.GTE(threshold) {
log.Infof("Change approved by majority. id: %d, type: %s, total vote: %s, threshold %s", change.ID, change.Type, totalVote, threshold)
applied := keeper.ApplyChange(ctx, change)
if applied {
if change.Rewardable {
initiatorEthAddr := validatorsByAddr[sdk.ValAddress(change.Initiator).String()].Description.Identity
keeper.AddPullerReward(ctx, initiatorEthAddr, pullerReward)
}
change.Status = StatusPassed
tagValue = types.AttributeValueChangePassed
} else {
change.Status = StatusFailed
tagValue = types.AttributeValueChangeFailed
}
}
if ctx.BlockTime().After(change.VotingEndTime) {
change.Status = StatusFailed
tagValue = types.AttributeValueChangeFailed
log.Infof("Change msg timeout, id: %d, type: %s", change.ID, change.Type)
}
if change.Status != StatusActive {
keeper.SetChange(ctx, change)
keeper.RemoveFromActiveChangeQueue(ctx, change.ID)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeActiveChange,
sdk.NewAttribute(types.AttributeKeyChangeID, fmt.Sprintf("%d", change.ID)),
sdk.NewAttribute(types.AttributeKeyChangeResult, tagValue),
),
)
}
}
}