-
Notifications
You must be signed in to change notification settings - Fork 105
/
validator_set_update.go
192 lines (169 loc) · 6.97 KB
/
validator_set_update.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package keeper
import (
"fmt"
storetypes "cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/interchain-security/v5/x/ccv/provider/types"
)
// SetConsumerValidator sets provided consumer `validator` on the consumer chain with `chainID`
func (k Keeper) SetConsumerValidator(
ctx sdk.Context,
chainID string,
validator types.ConsumerValidator,
) {
store := ctx.KVStore(k.storeKey)
bz, err := validator.Marshal()
if err != nil {
panic(fmt.Errorf("failed to marshal ConsumerValidator: %w", err))
}
store.Set(types.ConsumerValidatorKey(chainID, validator.ProviderConsAddr), bz)
}
// DeleteConsumerValidator removes consumer validator with `providerAddr` address
func (k Keeper) DeleteConsumerValidator(
ctx sdk.Context,
chainID string,
providerConsAddr types.ProviderConsAddress,
) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.ConsumerValidatorKey(chainID, providerConsAddr.ToSdkConsAddr()))
}
// DeleteConsumerValSet deletes all the stored consumer validators for chain `chainID`
func (k Keeper) DeleteConsumerValSet(
ctx sdk.Context,
chainID string,
) {
store := ctx.KVStore(k.storeKey)
key := types.ChainIdWithLenKey(types.ConsumerValidatorBytePrefix, chainID)
iterator := storetypes.KVStorePrefixIterator(store, key)
var keysToDel [][]byte
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
keysToDel = append(keysToDel, iterator.Key())
}
for _, delKey := range keysToDel {
store.Delete(delKey)
}
}
// IsConsumerValidator returns `true` if the consumer validator with `providerAddr` exists for chain `chainID`
// and `false` otherwise
func (k Keeper) IsConsumerValidator(
ctx sdk.Context,
chainID string,
providerAddr types.ProviderConsAddress,
) bool {
store := ctx.KVStore(k.storeKey)
return store.Get(types.ConsumerValidatorKey(chainID, providerAddr.ToSdkConsAddr())) != nil
}
// GetConsumerValSet returns all the consumer validators for chain `chainID`
func (k Keeper) GetConsumerValSet(
ctx sdk.Context,
chainID string,
) (validators []types.ConsumerValidator) {
store := ctx.KVStore(k.storeKey)
key := types.ChainIdWithLenKey(types.ConsumerValidatorBytePrefix, chainID)
iterator := storetypes.KVStorePrefixIterator(store, key)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
iterator.Value()
var validator types.ConsumerValidator
if err := validator.Unmarshal(iterator.Value()); err != nil {
panic(fmt.Errorf("failed to unmarshal ConsumerValidator: %w", err))
}
validators = append(validators, validator)
}
return validators
}
// ComputeNextEpochConsumerValSet returns the next validator set that is responsible for validating consumer
// chain `chainID`, based on the bonded validators.
func (k Keeper) ComputeNextEpochConsumerValSet(
ctx sdk.Context,
chainID string,
bondedValidators []stakingtypes.Validator,
) []types.ConsumerValidator {
var nextValidators []types.ConsumerValidator
for _, val := range bondedValidators {
valoper, err := k.ValidatorAddressCodec().StringToBytes(val.GetOperator())
if err != nil {
panic(fmt.Errorf("could not decode validator operator: %w", err))
}
// get next voting power and the next consumer public key
nextPower, err := k.stakingKeeper.GetLastValidatorPower(ctx, valoper)
if err != nil {
panic(fmt.Errorf("could not get last validator power: %w", err))
}
consAddr, err := val.GetConsAddr()
if err != nil {
// this should never happen but is recoverable if we exclude this validator from the `nextValidators`
k.Logger(ctx).Error("could not get consensus address of validator",
"validator", val.GetOperator(),
"error", err)
continue
}
nextConsumerPublicKey, foundConsumerPublicKey := k.GetValidatorConsumerPubKey(ctx, chainID, types.NewProviderConsAddress(consAddr))
if !foundConsumerPublicKey {
// if no consumer key assigned then use the validator's key itself
k.Logger(ctx).Info("could not retrieve public key for validator on consumer chain because"+
" the validator did not assign a new consumer key",
"validator", val.GetOperator(),
"chainID", chainID)
nextConsumerPublicKey, err = val.CmtConsPublicKey()
if err != nil {
// this should never happen and might not be recoverable because without the public key
// we cannot generate a validator update
panic(fmt.Errorf("could not retrieve validator's (%+v) public key: %w", val, err))
}
}
nextValidator := types.ConsumerValidator{
ProviderConsAddr: consAddr,
Power: nextPower,
ConsumerPublicKey: &nextConsumerPublicKey,
}
nextValidators = append(nextValidators, nextValidator)
}
return nextValidators
}
// DiffValidators compares the current and the next epoch's consumer validators and returns the `ValidatorUpdate` diff
// needed by CometBFT to update the validator set on a chain.
func DiffValidators(
currentValidators []types.ConsumerValidator,
nextValidators []types.ConsumerValidator,
) []abci.ValidatorUpdate {
var updates []abci.ValidatorUpdate
isCurrentValidator := make(map[string]types.ConsumerValidator)
for _, val := range currentValidators {
isCurrentValidator[val.ConsumerPublicKey.String()] = val
}
isNextValidator := make(map[string]types.ConsumerValidator)
for _, val := range nextValidators {
isNextValidator[val.ConsumerPublicKey.String()] = val
}
for _, currentVal := range currentValidators {
if nextVal, found := isNextValidator[currentVal.ConsumerPublicKey.String()]; !found {
// this consumer public key does not appear in the next validators and hence we remove the validator
// with that consumer public key by creating an update with 0 power
updates = append(updates, abci.ValidatorUpdate{PubKey: *currentVal.ConsumerPublicKey, Power: 0})
} else if currentVal.Power != nextVal.Power {
// validator did not modify its consumer public key but has changed its voting power, so we
// have to create an update with the new power
updates = append(updates, abci.ValidatorUpdate{PubKey: *nextVal.ConsumerPublicKey, Power: nextVal.Power})
}
// else no update is needed because neither the consumer public key changed, nor the power of the validator
}
for _, nextVal := range nextValidators {
if _, found := isCurrentValidator[nextVal.ConsumerPublicKey.String()]; !found {
// this consumer public key does not exist in the current validators and hence we introduce this validator
updates = append(updates, abci.ValidatorUpdate{PubKey: *nextVal.ConsumerPublicKey, Power: nextVal.Power})
}
}
return updates
}
// SetConsumerValSet resets the current consumer validators with the `nextValidators` computed by
// `ComputeNextEpochConsumerValSet` and hence this method should only be called after `ComputeNextEpochConsumerValSet` has completed.
func (k Keeper) SetConsumerValSet(ctx sdk.Context, chainID string, nextValidators []types.ConsumerValidator) {
k.DeleteConsumerValSet(ctx, chainID)
for _, val := range nextValidators {
k.SetConsumerValidator(ctx, chainID, val)
}
}