-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
118 lines (98 loc) · 2.78 KB
/
validator.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
107
108
109
110
111
112
113
114
115
116
117
118
package types
import (
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
)
func (v Validator) GetAddressBytes() ([]byte, error) {
_, addr, err := bech32.DecodeAndConvert(v.ValoperAddress)
return addr, err
}
// check to see if two validator instances are equal. Used in testing.
// func (v Validator) IsEqual(other Validator) bool {
// if v.ValoperAddress != other.ValoperAddress {
// return false
// }
//
// if !v.CommissionRate.Equal(other.CommissionRate) {
// return false
// }
//
// if !v.DelegatorShares.Equal(other.DelegatorShares) {
// return false
// }
//
// if !v.VotingPower.Equal(other.VotingPower) {
// return false
// }
// return true
// }
func (v Validator) SharesToTokens(shares sdk.Dec) sdkmath.Int {
if v.DelegatorShares.IsNil() || v.DelegatorShares.IsZero() {
return sdk.ZeroInt()
}
return sdk.NewDecFromInt(v.VotingPower).Quo(v.DelegatorShares).Mul(shares).TruncateInt()
}
func (di DelegatorIntent) AddOrdinal(multiplier sdk.Dec, intents ValidatorIntents) DelegatorIntent {
if len(intents) == 0 {
return di
}
if len(di.Intents) == 0 {
di.Intents = make(ValidatorIntents, 0)
}
ordinalized := di.Ordinalize(multiplier)
OUTER:
for _, i := range intents.Sort() {
for jdx, j := range ordinalized.SortedIntents() {
if i.ValoperAddress == j.ValoperAddress {
ordinalized.Intents[jdx].Weight = j.Weight.Add(i.Weight)
continue OUTER
}
}
ordinalized.Intents = append(ordinalized.Intents, i)
}
// we may have appended above, so resort intents.
ordinalized.SortedIntents()
return ordinalized.Normalize()
}
func (di DelegatorIntent) IntentForValoper(valoper string) (*ValidatorIntent, bool) {
for _, intent := range di.Intents {
if intent.ValoperAddress == valoper {
return intent, true
}
}
return nil, false
}
func (di DelegatorIntent) MustIntentForValoper(valoper string) *ValidatorIntent {
intent, found := di.IntentForValoper(valoper)
if !found {
panic("intent not found")
}
return intent
}
func (di DelegatorIntent) Normalize() DelegatorIntent {
summedWeight := sdk.ZeroDec()
// cached sorted intents as we don't modify in the first iteration.
sortedIntents := di.SortedIntents()
for _, i := range sortedIntents {
summedWeight = summedWeight.Add(i.Weight)
}
// zero summed weight, we should panic here, something is very wrong...
if summedWeight.IsZero() {
return di
}
for idx, i := range sortedIntents {
di.Intents[idx].Weight = i.Weight.QuoTruncate(summedWeight)
}
return di
}
func (di DelegatorIntent) Ordinalize(multiple sdk.Dec) DelegatorIntent {
for idx, i := range di.SortedIntents() {
di.Intents[idx].Weight = i.Weight.Mul(multiple)
}
return di
}
func (di *DelegatorIntent) SortedIntents() ValidatorIntents {
di.Intents = di.Intents.Sort()
return di.Intents
}