forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_common.go
56 lines (44 loc) · 1.35 KB
/
test_common.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
package keeper // noalias
import (
"bytes"
sdk "github.com/shapeshift/cosmos-sdk/types"
"github.com/shapeshift/cosmos-sdk/x/staking/types"
)
// does a certain by-power index record exist
func ValidatorByPowerIndexExists(ctx sdk.Context, keeper *Keeper, power []byte) bool {
store := ctx.KVStore(keeper.storeKey)
return store.Has(power)
}
// update validator for testing
func TestingUpdateValidator(keeper *Keeper, ctx sdk.Context, validator types.Validator, apply bool) types.Validator {
keeper.SetValidator(ctx, validator)
// Remove any existing power key for validator.
store := ctx.KVStore(keeper.storeKey)
deleted := false
iterator := sdk.KVStorePrefixIterator(store, types.ValidatorsByPowerIndexKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
valAddr := types.ParseValidatorPowerRankKey(iterator.Key())
if bytes.Equal(valAddr, validator.GetOperator()) {
if deleted {
panic("found duplicate power index key")
} else {
deleted = true
}
store.Delete(iterator.Key())
}
}
keeper.SetValidatorByPowerIndex(ctx, validator)
if !apply {
ctx, _ = ctx.CacheContext()
}
_, err := keeper.ApplyAndReturnValidatorSetUpdates(ctx)
if err != nil {
panic(err)
}
validator, found := keeper.GetValidator(ctx, validator.GetOperator())
if !found {
panic("validator expected but not found")
}
return validator
}