-
Notifications
You must be signed in to change notification settings - Fork 200
/
test_utils.go
157 lines (129 loc) · 5.31 KB
/
test_utils.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
// nolint
package types
import (
sdkmath "cosmossdk.io/math"
"math"
"math/rand"
"time"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cometbft/cometbft/crypto/secp256k1"
tmprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
)
// OracleDecPrecision nolint
const OracleDecPrecision = 8
// GenerateRandomTestCase nolint
func GenerateRandomTestCase() (rates []float64, valAddrs []sdk.ValAddress, stakingKeeper DummyStakingKeeper) {
valAddrs = []sdk.ValAddress{}
mockValidators := []MockValidator{}
base := math.Pow10(OracleDecPrecision)
rand.Seed(int64(time.Now().Nanosecond()))
numInputs := 10 + (rand.Int() % 100)
for i := 0; i < numInputs; i++ {
rate := float64(int64(rand.Float64()*base)) / base
rates = append(rates, rate)
pubKey := secp256k1.GenPrivKey().PubKey()
valAddr := sdk.ValAddress(pubKey.Address())
valAddrs = append(valAddrs, valAddr)
power := rand.Int63()%1000 + 1
mockValidator := NewMockValidator(valAddr, power)
mockValidators = append(mockValidators, mockValidator)
}
stakingKeeper = NewDummyStakingKeeper(mockValidators)
return
}
var _ StakingKeeper = DummyStakingKeeper{}
// DummyStakingKeeper dummy staking keeper to test ballot
type DummyStakingKeeper struct {
validators []MockValidator
}
// NewDummyStakingKeeper returns new DummyStakingKeeper instance
func NewDummyStakingKeeper(validators []MockValidator) DummyStakingKeeper {
return DummyStakingKeeper{
validators: validators,
}
}
// Validators nolint
func (sk DummyStakingKeeper) Validators() []MockValidator {
return sk.validators
}
// Validator nolint
func (sk DummyStakingKeeper) Validator(ctx sdk.Context, address sdk.ValAddress) stakingtypes.ValidatorI {
for _, validator := range sk.validators {
if validator.GetOperator().Equals(address) {
return validator
}
}
return nil
}
// TotalBondedTokens nolint
func (DummyStakingKeeper) TotalBondedTokens(_ sdk.Context) sdk.Int {
return sdk.ZeroInt()
}
// Slash nolint
func (DummyStakingKeeper) Slash(sdk.Context, sdk.ConsAddress, int64, int64, sdk.Dec) sdkmath.Int {
return sdkmath.ZeroInt()
}
// ValidatorsPowerStoreIterator nolint
func (DummyStakingKeeper) ValidatorsPowerStoreIterator(ctx sdk.Context) sdk.Iterator {
return sdk.KVStoreReversePrefixIterator(nil, nil)
}
// Jail nolint
func (DummyStakingKeeper) Jail(sdk.Context, sdk.ConsAddress) {
}
// GetLastValidatorPower nolint
func (sk DummyStakingKeeper) GetLastValidatorPower(ctx sdk.Context, operator sdk.ValAddress) (power int64) {
return sk.Validator(ctx, operator).GetConsensusPower(sdk.DefaultPowerReduction)
}
// MaxValidators returns the maximum amount of bonded validators
func (DummyStakingKeeper) MaxValidators(sdk.Context) uint32 {
return 100
}
// PowerReduction - is the amount of staking tokens required for 1 unit of consensus-engine power
func (DummyStakingKeeper) PowerReduction(ctx sdk.Context) (res sdk.Int) {
res = sdk.DefaultPowerReduction
return
}
// MockValidator nolint
type MockValidator struct {
power int64
valOperAddr sdk.ValAddress
}
var _ stakingtypes.ValidatorI = MockValidator{}
func (MockValidator) IsJailed() bool { return false }
func (MockValidator) GetMoniker() string { return "" }
func (MockValidator) GetStatus() stakingtypes.BondStatus { return stakingtypes.Bonded }
func (MockValidator) IsBonded() bool { return true }
func (MockValidator) IsUnbonded() bool { return false }
func (MockValidator) IsUnbonding() bool { return false }
func (v MockValidator) GetOperator() sdk.ValAddress { return v.valOperAddr }
func (MockValidator) ConsPubKey() (cryptotypes.PubKey, error) { return nil, nil }
func (MockValidator) TmConsPublicKey() (tmprotocrypto.PublicKey, error) {
return tmprotocrypto.PublicKey{}, nil
}
func (MockValidator) GetConsAddr() (sdk.ConsAddress, error) { return nil, nil }
func (v MockValidator) GetTokens() sdk.Int {
return sdk.TokensFromConsensusPower(v.power, sdk.DefaultPowerReduction)
}
func (v MockValidator) GetBondedTokens() sdk.Int {
return sdk.TokensFromConsensusPower(v.power, sdk.DefaultPowerReduction)
}
func (v MockValidator) GetConsensusPower(powerReduction sdk.Int) int64 { return v.power }
func (v *MockValidator) SetConsensusPower(power int64) { v.power = power }
func (v MockValidator) GetCommission() sdk.Dec { return sdk.ZeroDec() }
func (v MockValidator) GetMinSelfDelegation() sdk.Int { return sdk.OneInt() }
func (v MockValidator) GetDelegatorShares() sdk.Dec { return sdk.NewDec(v.power) }
func (v MockValidator) TokensFromShares(sdk.Dec) sdk.Dec { return sdk.ZeroDec() }
func (v MockValidator) TokensFromSharesTruncated(sdk.Dec) sdk.Dec { return sdk.ZeroDec() }
func (v MockValidator) TokensFromSharesRoundUp(sdk.Dec) sdk.Dec { return sdk.ZeroDec() }
func (v MockValidator) SharesFromTokens(amt sdk.Int) (sdk.Dec, error) { return sdk.ZeroDec(), nil }
func (v MockValidator) SharesFromTokensTruncated(amt sdk.Int) (sdk.Dec, error) {
return sdk.ZeroDec(), nil
}
func NewMockValidator(valAddr sdk.ValAddress, power int64) MockValidator {
return MockValidator{
power: power,
valOperAddr: valAddr,
}
}