This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
validator_status.go
106 lines (97 loc) · 4.68 KB
/
validator_status.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
106
package keeper
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
distr "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingexported "github.com/cosmos/cosmos-sdk/x/staking/exported"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/bandprotocol/bandchain/chain/x/oracle/types"
)
// valWithPower is an internal type to track validator with voting power inside of AllocateTokens.
type valWithPower struct {
val stakingexported.ValidatorI
power int64
}
// AllocateTokens allocates a portion of fee collected in the previous blocks to validators that
// that are actively performing oracle tasks. Note that this reward is also subjected to comm tax.
func (k Keeper) AllocateTokens(ctx sdk.Context, previousVotes []abci.VoteInfo) {
toReward := []valWithPower{}
totalPower := int64(0)
for _, vote := range previousVotes {
val := k.stakingKeeper.ValidatorByConsAddr(ctx, vote.Validator.Address)
if k.GetValidatorStatus(ctx, val.GetOperator()).IsActive {
toReward = append(toReward, valWithPower{val: val, power: vote.Validator.Power})
totalPower += vote.Validator.Power
}
}
if totalPower == 0 {
// No active validators performing oracle tasks, nothing needs to be done here.
return
}
feeCollector := k.supplyKeeper.GetModuleAccount(ctx, k.feeCollectorName)
totalFee := sdk.NewDecCoinsFromCoins(feeCollector.GetCoins()...)
// Compute the fee allocated for oracle module to distribute to active validators.
oracleRewardRatio := sdk.NewDecWithPrec(int64(k.GetParam(ctx, types.KeyOracleRewardPercentage)), 2)
oracleRewardInt, _ := totalFee.MulDecTruncate(oracleRewardRatio).TruncateDecimal()
// Transfer the oracle reward portion from fee collector to distr module.
err := k.supplyKeeper.SendCoinsFromModuleToModule(ctx, k.feeCollectorName, distr.ModuleName, oracleRewardInt)
if err != nil {
panic(err)
}
// Convert the transfered tokens back to DecCoins for internal distr allocations.
oracleReward := sdk.NewDecCoinsFromCoins(oracleRewardInt...)
remaining := oracleReward
rewardMultiplier := sdk.OneDec().Sub(k.distrKeeper.GetCommunityTax(ctx))
// Allocate non-community pool tokens to active validators weighted by voting power.
for _, each := range toReward {
powerFraction := sdk.NewDec(each.power).QuoTruncate(sdk.NewDec(totalPower))
reward := oracleReward.MulDecTruncate(rewardMultiplier).MulDecTruncate(powerFraction)
k.distrKeeper.AllocateTokensToValidator(ctx, each.val, reward)
remaining = remaining.Sub(reward)
}
// Allocate the remaining coins to the community pool.
feePool := k.distrKeeper.GetFeePool(ctx)
feePool.CommunityPool = feePool.CommunityPool.Add(remaining...)
k.distrKeeper.SetFeePool(ctx, feePool)
}
// GetValidatorStatus returns the validator status for the given validator. Note that validator
// status is default to [inactive, 0], so new validators start with inactive state.
func (k Keeper) GetValidatorStatus(ctx sdk.Context, val sdk.ValAddress) types.ValidatorStatus {
bz := ctx.KVStore(k.storeKey).Get(types.ValidatorStatusStoreKey(val))
if bz == nil {
return types.NewValidatorStatus(false, time.Time{})
}
var status types.ValidatorStatus
k.cdc.MustUnmarshalBinaryBare(bz, &status)
return status
}
// SetValidatorStatus sets the validator status for the given validator.
func (k Keeper) SetValidatorStatus(ctx sdk.Context, val sdk.ValAddress, status types.ValidatorStatus) {
ctx.KVStore(k.storeKey).Set(types.ValidatorStatusStoreKey(val), k.cdc.MustMarshalBinaryBare(status))
}
// Activate changes the given validator's status to active. Returns error if the validator is
// already active or was deactivated recently, as specified by InactivePenaltyDuration parameter.
func (k Keeper) Activate(ctx sdk.Context, val sdk.ValAddress) error {
status := k.GetValidatorStatus(ctx, val)
if status.IsActive {
return types.ErrValidatorAlreadyActive
}
penaltyDuration := time.Duration(k.GetParam(ctx, types.KeyInactivePenaltyDuration))
if !status.Since.IsZero() && status.Since.Add(penaltyDuration).After(ctx.BlockHeader().Time) {
return types.ErrTooSoonToActivate
}
k.SetValidatorStatus(ctx, val, types.NewValidatorStatus(true, ctx.BlockHeader().Time))
return nil
}
// MissReport changes the given validator's status to inactive. No-op if already inactive or
// if the validator was active after the time the request happened.
func (k Keeper) MissReport(ctx sdk.Context, val sdk.ValAddress, requestTime time.Time) {
status := k.GetValidatorStatus(ctx, val)
if status.IsActive && status.Since.Before(requestTime) {
k.SetValidatorStatus(ctx, val, types.NewValidatorStatus(false, ctx.BlockHeader().Time))
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeDeactivate,
sdk.NewAttribute(types.AttributeKeyValidator, val.String()),
))
}
}