/
mock_tendermint.go
286 lines (242 loc) · 7.98 KB
/
mock_tendermint.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package simulation
import (
"fmt"
"math/rand"
"sort"
"time"
"github.com/cosmos/cosmos-sdk/types/simulation"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"
"golang.org/x/exp/maps"
markov "github.com/merlins-labs/merlins/v17/simulation/simtypes/transitionmatrix"
)
type mockValidator struct {
val abci.ValidatorUpdate
livenessState int
}
func (mv mockValidator) String() string {
return fmt.Sprintf("mockValidator{%s power:%v state:%v}",
mv.val.PubKey.String(),
mv.val.Power,
mv.livenessState)
}
type mockValidators map[string]mockValidator
// get mockValidators from abci validators
func newMockValidators(r *rand.Rand, abciVals []abci.ValidatorUpdate, params Params) mockValidators {
validators := make(mockValidators)
for _, validator := range abciVals {
str := fmt.Sprintf("%X", validator.PubKey.GetEd25519())
liveliness := markov.GetMemberOfInitialState(r, params.InitialLivenessWeightings())
validators[str] = mockValidator{
val: validator,
livenessState: liveliness,
}
}
return validators
}
func (mv mockValidators) Clone() mockValidators {
validators := make(mockValidators, len(mv))
keys := mv.getKeys()
for _, k := range keys {
validators[k] = mv[k]
}
return validators
}
// TODO describe usage
func (mv mockValidators) getKeys() []string {
keys := maps.Keys(mv)
sort.Strings(keys)
return keys
}
// randomProposer picks a random proposer from the current validator set
func (mv mockValidators) randomProposer(r *rand.Rand) crypto.PubKey {
keys := mv.getKeys()
if len(keys) == 0 {
return nil
}
key := keys[r.Intn(len(keys))]
proposer := mv[key].val
pk, err := cryptoenc.PubKeyFromProto(proposer.PubKey)
if err != nil { //nolint:wsl
panic(err)
}
return pk
}
func (mv mockValidators) toTmProtoValidators(proposerPubKey crypto.PubKey) (tmtypes.ValidatorSet, error) {
var tmProtoValSet tmproto.ValidatorSet
var tmTypesValSet *tmtypes.ValidatorSet
// iterate through current validators and add them to the TM ValidatorSet struct
for _, key := range mv.getKeys() {
var validator tmproto.Validator
mapVal := mv[key]
validator.PubKey = mapVal.val.PubKey
currentPubKey, err := cryptoenc.PubKeyFromProto(mapVal.val.PubKey)
if err != nil {
return *tmTypesValSet, err
}
validator.Address = currentPubKey.Address()
tmProtoValSet.Validators = append(tmProtoValSet.Validators, &validator)
}
// set the proposer chosen earlier as the validator set block proposer
var proposerVal tmtypes.Validator
proposerVal.PubKey = proposerPubKey
proposerVal.Address = proposerPubKey.Address()
blockProposer, err := proposerVal.ToProto()
if err != nil {
return *tmTypesValSet, err
}
tmProtoValSet.Proposer = blockProposer
// create a validatorSet type from the tmproto created earlier
tmTypesValSet, err = tmtypes.ValidatorSetFromProto(&tmProtoValSet)
return *tmTypesValSet, err
}
// updateValidators mimics Tendermint's update logic.
func updateValidators(
r *rand.Rand,
params simulation.Params,
current map[string]mockValidator,
updates []abci.ValidatorUpdate,
event func(route, op, evResult string),
) (map[string]mockValidator, error) {
nextSet := mockValidators(current).Clone()
// Count the number of validators that are about to be kicked
kickedValidators := 0
for _, update := range updates {
str := fmt.Sprintf("%X", update.PubKey.GetEd25519())
if update.Power == 0 {
if _, ok := nextSet[str]; !ok {
return nil, fmt.Errorf("tried to delete a nonexistent validator: %s", str)
}
kickedValidators++
event("end_block", "validator_updates", "kicked")
} else if _, ok := nextSet[str]; ok {
// validator already exists, update weight
nextSet[str] = mockValidator{update, nextSet[str].livenessState}
event("end_block", "validator_updates", "updated")
} else {
// Set this new validator
nextSet[str] = mockValidator{
update,
markov.GetMemberOfInitialState(r, params.InitialLivenessWeightings()),
}
event("end_block", "validator_updates", "added")
}
}
// Check if all the validators are about to be kicked, if so, don't perform the deletions
if kickedValidators == len(nextSet) {
return nextSet, nil
}
// Perform the deletions for validators that are to be kicked
for _, update := range updates {
str := fmt.Sprintf("%X", update.PubKey.GetEd25519())
if update.Power == 0 {
delete(nextSet, str)
}
}
return nextSet, nil
}
// RandomRequestBeginBlock generates a list of signing validators according to
// the provided list of validators, signing fraction, and evidence fraction
func RandomRequestBeginBlock(r *rand.Rand, params Params,
validators mockValidators, pastTimes []time.Time,
pastVoteInfos [][]abci.VoteInfo,
event func(route, op, evResult string), header tmproto.Header,
) abci.RequestBeginBlock {
if len(validators) == 0 {
return abci.RequestBeginBlock{
Header: header,
}
}
voteInfos := randomVoteInfos(r, params, validators)
evidence := randomDoubleSignEvidence(r, params, validators, pastTimes, pastVoteInfos, event, header, voteInfos)
return abci.RequestBeginBlock{
Header: header,
LastCommitInfo: abci.LastCommitInfo{
Votes: voteInfos,
},
ByzantineValidators: evidence,
}
}
func randomVoteInfos(r *rand.Rand, simParams Params, validators mockValidators,
) []abci.VoteInfo {
voteInfos := make([]abci.VoteInfo, len(validators))
for i, key := range validators.getKeys() {
mVal := validators[key]
mVal.livenessState = simParams.LivenessTransitionMatrix().NextState(r, mVal.livenessState)
signed := true
if mVal.livenessState == 1 {
// spotty connection, 50% probability of success
// See https://github.com/golang/go/issues/23804#issuecomment-365370418
// for reasoning behind computing like this
signed = r.Int63()%2 == 0
} else if mVal.livenessState == 2 {
// offline
signed = false
}
// TODO: Do we want to log any data to statsdb here?
pubkey, err := cryptoenc.PubKeyFromProto(mVal.val.PubKey)
if err != nil {
panic(err)
}
voteInfos[i] = abci.VoteInfo{
Validator: abci.Validator{
Address: pubkey.Address(),
Power: mVal.val.Power,
},
SignedLastBlock: signed,
}
}
return voteInfos
}
func randomDoubleSignEvidence(r *rand.Rand, params Params,
validators mockValidators, pastTimes []time.Time,
pastVoteInfos [][]abci.VoteInfo,
event func(route, op, evResult string), header tmproto.Header, voteInfos []abci.VoteInfo,
) []abci.Evidence {
evidence := []abci.Evidence{}
// return if no past times or if only 10 validators remaining in the active set
if len(pastTimes) == 0 {
return evidence
}
var n float64 = 1
// TODO: Change this to be markov based & clean this up
// Right now we incrementally lower the evidence fraction to make
// it less likely to jail many validators in one run.
// We should also add some method of including new validators into the set
// instead of being stuck with the ones we start with during initialization.
for r.Float64() < (params.EvidenceFraction() / n) {
// if only one validator remaining, don't jail any more validators
if len(voteInfos)-int(n) <= 0 {
return nil
}
height := header.Height
time := header.Time
vals := voteInfos
if r.Float64() < params.PastEvidenceFraction() && header.Height > 1 {
height = int64(r.Intn(int(header.Height)-1)) + 1 // Tendermint starts at height 1
// array indices offset by one
time = pastTimes[height-1]
vals = pastVoteInfos[height-1]
}
validator := vals[r.Intn(len(vals))].Validator
var totalVotingPower int64
for _, val := range vals {
totalVotingPower += val.Validator.Power
}
evidence = append(evidence,
abci.Evidence{
Type: abci.EvidenceType_DUPLICATE_VOTE,
Validator: validator,
Height: height,
Time: time,
TotalVotingPower: totalVotingPower,
},
)
event("begin_block", "evidence", "ok")
n++
}
return evidence
}