/
generator.go
373 lines (311 loc) · 11.6 KB
/
generator.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package generator
import (
"fmt"
"time"
lite "github.com/tendermint/tendermint/lite2"
st "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/lite2/provider"
"github.com/tendermint/tendermint/types"
)
func generateNextBlocks(
numOfBlocks int,
state st.State,
privVals types.PrivValidatorsByAddress,
lastCommit *types.Commit,
valSetChanges ValSetChanges,
blockTime time.Time,
) ([]LiteBlock, []st.State, types.PrivValidatorsByAddress) {
var liteBlocks []LiteBlock
var states []st.State
valSetChanges = append(valSetChanges, valSetChanges[len(valSetChanges)-1])
for i := 0; i < numOfBlocks; i++ {
liteblock, st, _ := generateNextBlockWithNextValsUpdate(
state,
valSetChanges[i].PrivVals,
lastCommit,
valSetChanges[i+1].Validators,
nil,
blockTime,
)
liteBlocks = append(liteBlocks, liteblock)
state = st
lastCommit = liteblock.SignedHeader.Commit
states = append(states, state)
blockTime = blockTime.Add(5 * time.Second)
}
return liteBlocks, states, privVals
}
func makeLiteblocks(
valSetChanges ValSetChanges,
) ([]LiteBlock, []st.State, types.PrivValidatorsByAddress) {
signedHeader, state, _ := generateFirstBlockWithNextValsUpdate(
valSetChanges[0].Validators,
valSetChanges[0].PrivVals,
valSetChanges[1].Validators,
nil,
firstBlockTime,
)
firstBlock := []LiteBlock{
{
SignedHeader: signedHeader,
ValidatorSet: *state.LastValidators,
NextValidatorSet: *state.Validators,
},
}
lastCommit := signedHeader.Commit
numOfBlocks := len(valSetChanges) - 1
liteBlocks, states, privVals := generateNextBlocks(
numOfBlocks,
state,
valSetChanges[1].PrivVals,
lastCommit,
valSetChanges[1:],
thirdBlockTime,
)
liteBlocks = append(firstBlock, liteBlocks...)
stateSlice := []st.State{
state,
}
states = append(stateSlice, states...)
return liteBlocks, states, privVals
}
func generateMultiPeerBisectionCase(
description string,
primaryValSetChanges ValSetChanges,
alternativeValSetChanges ValSetChanges,
expectedBisections int32,
expectOutput string,
) (TestBisection, []st.State, types.PrivValidatorsByAddress, []st.State, types.PrivValidatorsByAddress) {
testBisection, statesPrimary, privValsPrimary := generateGeneralBisectionCase(
description,
primaryValSetChanges,
expectedBisections)
liteBlocks, statesAlternative, privValsAlternative := makeLiteblocks(alternativeValSetChanges)
testBisection.Witnesses[0] = MockProvider{}.New(liteBlocks[0].SignedHeader.Header.ChainID, liteBlocks)
testBisection.ExpectedOutput = expectOutput
return testBisection, statesPrimary, privValsPrimary, statesAlternative, privValsAlternative
}
func generateGeneralBisectionCase(
description string,
valSetChanges ValSetChanges,
expectedBisections int32,
) (TestBisection, []st.State, types.PrivValidatorsByAddress) {
liteBlocks, states, privVals := makeLiteblocks(valSetChanges)
primary := MockProvider{}.New(liteBlocks[0].SignedHeader.Header.ChainID, liteBlocks)
var witnesses []provider.Provider
witnesses = append([]provider.Provider{}, primary)
trustOptions := TrustOptions{}.make(liteBlocks[0].SignedHeader, TRUSTING_PERIOD, lite.DefaultTrustLevel)
heightToVerify := int64(len(valSetChanges))
testBisection := TestBisection{}.make(
description,
trustOptions,
primary,
witnesses,
heightToVerify,
now,
expectedOutputNoError,
expectedBisections,
)
return testBisection, states, privVals
}
// generateFirstBlock creates the first block of the chain
// with the given list of validators and timestamp
// Thus, It also calls the NewState() to initialize the state
// Returns the signedHeader and state after the first block is created
func generateFirstBlock(
vals []*types.Validator,
privVals types.PrivValidatorsByAddress,
now time.Time,
) (types.SignedHeader, st.State, types.PrivValidatorsByAddress) {
valSet := types.NewValidatorSet(vals)
state := NewState("test-chain-01", valSet, valSet)
return makeBlock(state, privVals, nil, now)
}
//TODO: Comment!
func generateFirstBlockWithNextValsUpdate(
vals []*types.Validator,
privVals types.PrivValidatorsByAddress,
nextVals []*types.Validator,
nextPrivVals types.PrivValidatorsByAddress,
now time.Time,
) (types.SignedHeader, st.State, types.PrivValidatorsByAddress) {
valSet := types.NewValidatorSet(vals)
nextValSet := types.NewValidatorSet(nextVals)
state := NewState("test-chain-01", valSet, nextValSet)
return makeBlock(state, privVals, nextPrivVals, now)
}
func makeBlock(
state st.State,
privVals types.PrivValidatorsByAddress,
nextPrivVals types.PrivValidatorsByAddress,
now time.Time,
) (types.SignedHeader, st.State, types.PrivValidatorsByAddress) {
txs := generateTxs()
evidences := generateEvidences()
lbh := state.LastBlockHeight + 1
proposer := state.Validators.Proposer.Address
// first block has a nil last commit
block, partSet := state.MakeBlock(lbh, txs, nil, evidences, proposer)
commit := generateCommit(block.Header, partSet, state.Validators, privVals, state.ChainID, now)
state, privVals = updateState(state, commit.BlockID, privVals, nextPrivVals)
signedHeader := types.SignedHeader{
Header: &block.Header,
Commit: commit,
}
return signedHeader, state, privVals
}
// Builds the Initial struct with given parameters
func generateInitial(signedHeader types.SignedHeader, nextValidatorSet types.ValidatorSet, trustingPeriod time.Duration, now time.Time) Initial {
return Initial{
SignedHeader: signedHeader,
NextValidatorSet: nextValidatorSet,
TrustingPeriod: trustingPeriod,
Now: now,
}
}
// This one generates a "next" block,
// i.e. given the first block, this function can be used to build up successive blocks
func generateNextBlock(state st.State, privVals types.PrivValidatorsByAddress, lastCommit *types.Commit, now time.Time) (LiteBlock, st.State, types.PrivValidatorsByAddress) {
txs := generateTxs()
evidences := generateEvidences()
lbh := state.LastBlockHeight + 1
proposer := state.Validators.Proposer.Address
block, partSet := state.MakeBlock(lbh, txs, lastCommit, evidences, proposer)
commit := generateCommit(block.Header, partSet, state.Validators, privVals, state.ChainID, now)
liteBlock := LiteBlock{
SignedHeader: types.SignedHeader{
Header: &block.Header,
Commit: commit,
},
ValidatorSet: *state.Validators.Copy(), // dereferencing pointer
NextValidatorSet: *state.NextValidators.Copy(), // dereferencing pointer
}
state, _ = updateState(state, commit.BlockID, privVals, nil)
return liteBlock, state, privVals
}
// Similar to generateNextBlock
// It also takes in new validators and privVals to be added to the NextValidatorSet
// Calls the UpdateWithChangeSet function on state.NextValidatorSet for the same
// Also, you can specify the number of vals to be deleted from it
func generateNextBlockWithNextValsUpdate(
state st.State,
privVals types.PrivValidatorsByAddress,
lastCommit *types.Commit,
newVals []*types.Validator,
newPrivVals types.PrivValidatorsByAddress,
now time.Time,
) (LiteBlock, st.State, types.PrivValidatorsByAddress) {
state.NextValidators = types.NewValidatorSet(newVals)
// state.NextValidators.IncrementProposerPriority(1)
txs := generateTxs()
evidences := generateEvidences()
lbh := state.LastBlockHeight + 1
proposer := state.Validators.Proposer.Address
block, partSet := state.MakeBlock(lbh, txs, lastCommit, evidences, proposer)
commit := generateCommit(block.Header, partSet, state.Validators, privVals, state.ChainID, now)
liteBlock := LiteBlock{
SignedHeader: types.SignedHeader{
Header: &block.Header,
Commit: commit,
},
ValidatorSet: *state.Validators.Copy(), // dereferencing pointer
NextValidatorSet: *state.NextValidators.Copy(), // dereferencing pointer
}
state, newPrivVals = updateState(state, commit.BlockID, privVals, newPrivVals)
return liteBlock, state, newPrivVals
}
// Builds a general case containing initial and one lite block in input
// TODO: change name to genInitialAndInput
func generateGeneralCase(
vals []*types.Validator,
privVals types.PrivValidatorsByAddress,
) (Initial, []LiteBlock, st.State, types.PrivValidatorsByAddress) {
var input []LiteBlock
signedHeader, state, privVals := generateFirstBlock(vals, privVals, firstBlockTime)
initial := generateInitial(signedHeader, *state.NextValidators, TRUSTING_PERIOD, now)
liteBlock, state, _ := generateNextBlock(state, privVals, signedHeader.Commit, secondBlockTime)
input = append(input, liteBlock)
return initial, input, state, privVals
}
func generateInitialAndInputSkipBlocks(
vals []*types.Validator,
privVals types.PrivValidatorsByAddress,
numOfBlocksToSkip int,
) (Initial, []LiteBlock, st.State, types.PrivValidatorsByAddress) {
var input []LiteBlock
signedHeader, state, privVals := generateFirstBlock(
vals,
privVals,
firstBlockTime,
)
initial := generateInitial(signedHeader, *state.NextValidators, TRUSTING_PERIOD, now)
blockTime := secondBlockTime
for i := 0; i <= numOfBlocksToSkip; i++ {
liteBlock, s, _ := generateNextBlock(state, privVals, signedHeader.Commit, blockTime)
blockTime = blockTime.Add(5 * time.Second)
state = s
if i == numOfBlocksToSkip {
input = append(input, liteBlock)
}
}
return initial, input, state, privVals
}
func generateAndMakeGeneralTestCase(description string, vals []*types.Validator, privVals types.PrivValidatorsByAddress, expectedOutput string) TestCase {
initial, input, _, _ := generateGeneralCase(vals, privVals)
return makeTestCase(description, initial, input, expectedOutput)
}
func generateAndMakeNextValsUpdateTestCase(
description string,
initialVals []*types.Validator,
initialPrivVals types.PrivValidatorsByAddress,
nextVals []*types.Validator,
nextPrivVals types.PrivValidatorsByAddress,
expectedOutput string,
) TestCase {
initial, input, _, _ := generateNextValsUpdateCase(initialVals, initialPrivVals, nextVals, nextPrivVals)
return makeTestCase(description, initial, input, expectedOutput)
}
// Builds a case where next validator set changes
// makes initial, and input with 2 lite blocks
func generateNextValsUpdateCase(
initialVals []*types.Validator,
initialPrivVals types.PrivValidatorsByAddress,
nextVals []*types.Validator,
nextPrivVals types.PrivValidatorsByAddress,
) (Initial, []LiteBlock, st.State, types.PrivValidatorsByAddress) {
var input []LiteBlock
signedHeader, state, privVals := generateFirstBlock(initialVals, initialPrivVals, firstBlockTime)
initial := generateInitial(signedHeader, *state.NextValidators, TRUSTING_PERIOD, now)
liteBlock, state, privVals := generateNextBlockWithNextValsUpdate(state, privVals, signedHeader.Commit, nextVals, nextPrivVals, secondBlockTime)
input = append(input, liteBlock)
liteBlock, state, _ = generateNextBlock(state, privVals, liteBlock.SignedHeader.Commit, thirdBlockTime)
input = append(input, liteBlock)
return initial, input, state, privVals
}
// UPDATE -> mutex on PartSet and functions take pointer to valSet - have to use a pointer
// generateCommit takes in header, partSet from Block that was created,
// validator set, privVals, chain ID and a timestamp to create
// and return a commit type
// To be called after MakeBlock()
func generateCommit(
header types.Header,
partSet *types.PartSet,
valSet *types.ValidatorSet,
privVals []types.PrivValidator,
chainID string,
now time.Time,
) *types.Commit {
blockID := types.BlockID{
Hash: header.Hash(),
PartsHeader: types.PartSetHeader{
Hash: partSet.Hash(),
Total: partSet.Total(),
},
}
voteSet := types.NewVoteSet(chainID, header.Height, 1, types.SignedMsgType(byte(types.PrecommitType)), valSet)
commit, err := types.MakeCommit(blockID, header.Height, 1, voteSet, privVals, now)
if err != nil {
fmt.Println(err)
}
return commit
}