forked from 0xPolygon/pbft-consensus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
state_test.go
504 lines (423 loc) · 12.2 KB
/
state_test.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package pbft
import (
"crypto/ecdsa"
"crypto/elliptic"
crand "crypto/rand"
"fmt"
mrand "math/rand"
"reflect"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func init() {
mrand.Seed(time.Now().UnixNano())
}
// Generate predefined number of validator node ids.
// Node id is generated with a given prefixNodeId, followed by underscore and an index.
func generateValidatorNodes(nodesCount int, prefixNodeId string) []NodeID {
validatorNodeIds := []NodeID{}
for i := 0; i < nodesCount; i++ {
validatorNodeIds = append(validatorNodeIds, NodeID(fmt.Sprintf("%s_%d", prefixNodeId, i)))
}
return validatorNodeIds
}
// Helper function which enables creation of MessageReq.
func createMessage(sender string, messageType MsgType, round ...uint64) *MessageReq {
seal := make([]byte, 2)
mrand.Read(seal)
r := uint64(0)
if len(round) > 0 {
r = round[0]
}
msg := &MessageReq{
From: NodeID(sender),
Type: messageType,
View: &View{Round: r},
Seal: seal,
Proposal: mockProposal,
}
return msg
}
func TestState_FaultyNodesCount(t *testing.T) {
cases := []struct {
TotalNodesCount, FaultyNodesCount int
}{
{0, 0},
{1, 0},
{2, 0},
{3, 0},
{4, 1},
{5, 1},
{6, 1},
{7, 2},
{8, 2},
{9, 2},
{10, 3},
{99, 32},
{100, 33},
}
for _, c := range cases {
s := newState()
s.validators = convertToMockValidatorSet(generateValidatorNodes(c.TotalNodesCount, "validator"))
assert.Equal(t, c.FaultyNodesCount, s.MaxFaultyNodes())
}
}
func Test_QuorumSize(t *testing.T) {
cases := []struct {
TotalNodesCount, QuorumSize int
}{
{1, 1},
{2, 1},
{3, 1},
{4, 3},
{5, 3},
{6, 3},
{7, 5},
{8, 5},
{9, 5},
{10, 7},
{100, 67},
}
for _, c := range cases {
assert.Equal(t, c.QuorumSize, QuorumSize(c.TotalNodesCount))
}
}
func TestState_ValidNodesCount(t *testing.T) {
cases := []struct {
TotalNodesCount, ValidNodesCount int
}{
{1, 0},
{2, 0},
{3, 0},
{4, 2},
{5, 2},
{6, 2},
{7, 4},
{8, 4},
{9, 4},
{10, 6},
{100, 66},
}
for _, c := range cases {
s := newState()
s.validators = convertToMockValidatorSet(generateValidatorNodes(c.TotalNodesCount, "validator"))
assert.Equal(t, c.ValidNodesCount, s.NumValid())
}
}
func TestState_AddMessages(t *testing.T) {
pool := newTesterAccountPool()
validatorIds := []string{"A", "B", "C", "D"}
pool.add(validatorIds...)
s := newState()
s.validators = pool.validatorSet()
// Send message from node which is not amongst validator nodes
s.addMessage(createMessage("E", MessageReq_Prepare))
assert.Empty(t, s.committed)
assert.Empty(t, s.prepared)
assert.Empty(t, s.roundMessages)
// -- test committed messages --
s.addMessage(pool.createMessage("A", MessageReq_Commit))
s.addMessage(pool.createMessage("B", MessageReq_Commit))
s.addMessage(pool.createMessage("B", MessageReq_Commit))
assert.Equal(t, 2, s.numCommitted())
// -- test prepare messages --
s.addMessage(pool.createMessage("C", MessageReq_Prepare))
s.addMessage(pool.createMessage("C", MessageReq_Prepare))
s.addMessage(pool.createMessage("D", MessageReq_Prepare))
assert.Equal(t, 2, s.numPrepared())
// -- test round change messages --
rounds := 2
for round := 0; round < rounds; round++ {
for i := 0; i < s.validators.Len(); i++ {
s.addMessage(pool.createMessage(validatorIds[i], MessageReq_RoundChange, uint64(round)))
}
}
for round := 0; round < rounds; round++ {
assert.Equal(t, rounds, len(s.roundMessages))
msgsPerRound := s.roundMessages[uint64(round)]
assert.Equal(t, s.validators.Len(), len(msgsPerRound))
}
}
func TestState_MaxRound_Found(t *testing.T) {
validatorsCount := 5
roundsCount := 6
s := newState()
validatorIds := make([]string, validatorsCount)
for i := 0; i < validatorsCount; i++ {
validatorId := fmt.Sprintf("validator_%d", i)
validatorIds[i] = validatorId
}
s.validators = newMockValidatorSet(validatorIds)
for round := 0; round < roundsCount; round++ {
if round%2 == 1 {
for _, validatorId := range validatorIds {
s.addMessage(createMessage(validatorId, MessageReq_RoundChange, uint64(round)))
}
} else {
s.addMessage(createMessage(validatorIds[mrand.Intn(validatorsCount)], MessageReq_RoundChange, uint64(round)))
}
}
maxRound, found := s.maxRound()
assert.Equal(t, uint64(5), maxRound)
assert.Equal(t, true, found)
}
func TestState_MaxRound_NotFound(t *testing.T) {
validatorsCount := 7
s := newState()
validatorIds := make([]string, validatorsCount)
for i := 0; i < validatorsCount; i++ {
validatorIds[i] = fmt.Sprintf("validator_%d", i)
}
s.validators = newMockValidatorSet(validatorIds)
// Send wrong message type from some validator, whereas roundMessages map is empty
s.addMessage(createMessage(validatorIds[0], MessageReq_Preprepare))
maxRound, found := s.maxRound()
assert.Equal(t, maxRound, uint64(0))
assert.Equal(t, found, false)
// Seed insufficient "RoundChange" messages count, so that maxRound isn't going to be found
for round := range validatorIds {
if round%2 == 0 {
// Each even round should populate more than one "RoundChange" messages, but just enough that we don't reach census (max faulty nodes+1)
for i := 0; i < s.MaxFaultyNodes(); i++ {
s.addMessage(createMessage(validatorIds[mrand.Intn(validatorsCount)], MessageReq_RoundChange, uint64(round)))
}
} else {
s.addMessage(createMessage(validatorIds[mrand.Intn(validatorsCount)], MessageReq_RoundChange, uint64(round)))
}
}
maxRound, found = s.maxRound()
assert.Equal(t, uint64(0), maxRound)
assert.Equal(t, false, found)
}
func TestState_AddRoundMessage(t *testing.T) {
s := newState()
s.validators = newMockValidatorSet([]string{"A", "B"})
roundMessageSize := s.AddRoundMessage(createMessage("A", MessageReq_Commit, 0))
assert.Equal(t, 0, roundMessageSize)
assert.Equal(t, 0, len(s.roundMessages))
s.AddRoundMessage(createMessage("A", MessageReq_RoundChange, 0))
s.AddRoundMessage(createMessage("A", MessageReq_RoundChange, 1))
s.AddRoundMessage(createMessage("A", MessageReq_RoundChange, 2))
roundMessageSize = s.AddRoundMessage(createMessage("B", MessageReq_RoundChange, 2))
assert.Equal(t, 2, roundMessageSize)
s.AddRoundMessage(createMessage("B", MessageReq_RoundChange, 3))
assert.Equal(t, 4, len(s.roundMessages))
assert.Empty(t, s.prepared)
assert.Empty(t, s.committed)
}
func TestState_addPrepared(t *testing.T) {
s := newState()
validatorIds := []string{"A", "B"}
s.validators = newMockValidatorSet(validatorIds)
s.addPrepared(createMessage("A", MessageReq_Commit))
assert.Equal(t, 0, len(s.prepared))
s.addPrepared(createMessage("A", MessageReq_Prepare))
s.addPrepared(createMessage("B", MessageReq_Prepare))
assert.Equal(t, len(validatorIds), len(s.prepared))
assert.Empty(t, s.committed)
assert.Empty(t, s.roundMessages)
}
func TestState_addCommitted(t *testing.T) {
s := newState()
validatorIds := []string{"A", "B"}
s.validators = newMockValidatorSet(validatorIds)
s.addCommitted(createMessage("A", MessageReq_Prepare))
assert.Empty(t, 0, s.committed)
s.addCommitted(createMessage("A", MessageReq_Commit))
s.addCommitted(createMessage("B", MessageReq_Commit))
assert.Equal(t, len(validatorIds), len(s.committed))
assert.Empty(t, s.prepared)
assert.Empty(t, s.roundMessages)
}
func TestState_Copy(t *testing.T) {
originalMsg := createMessage("A", MessageReq_Preprepare, 0)
copyMsg := originalMsg.Copy()
assert.NotSame(t, originalMsg, copyMsg)
assert.Equal(t, originalMsg, copyMsg)
}
func TestState_Lock_Unlock(t *testing.T) {
s := newState()
proposalData := make([]byte, 2)
mrand.Read(proposalData)
s.proposal = &Proposal{
Data: proposalData,
Time: time.Now(),
}
s.lock()
assert.True(t, s.locked)
assert.NotNil(t, s.proposal)
s.unlock()
assert.False(t, s.locked)
assert.Nil(t, s.proposal)
}
func TestState_GetSequence(t *testing.T) {
s := newState()
s.view = &View{Sequence: 3, Round: 0}
assert.True(t, s.GetSequence() == 3)
}
func TestState_getCommittedSeals(t *testing.T) {
pool := newTesterAccountPool()
pool.add("A", "B", "C", "D")
s := newState()
s.validators = pool.validatorSet()
s.addCommitted(createMessage("A", MessageReq_Commit))
s.addCommitted(createMessage("B", MessageReq_Commit))
committedSeals := s.getCommittedSeals()
assert.Len(t, committedSeals, 2)
assert.True(t, &committedSeals[0] != &s.committed["A"].Seal)
foundSeal := false
for _, seal := range committedSeals {
if reflect.DeepEqual(seal, s.committed["A"].Seal) {
foundSeal = true
}
}
assert.True(t, &committedSeals[0] != &s.committed["A"].Seal)
assert.True(t, foundSeal)
foundSeal = false
for _, seal := range committedSeals {
if reflect.DeepEqual(seal, s.committed["B"].Seal) {
foundSeal = true
}
}
assert.True(t, &committedSeals[1] != &s.committed["B"].Seal)
assert.True(t, foundSeal)
}
func TestMsgType_ToString(t *testing.T) {
expectedMapping := map[MsgType]string{
MessageReq_RoundChange: "RoundChange",
MessageReq_Preprepare: "Preprepare",
MessageReq_Commit: "Commit",
MessageReq_Prepare: "Prepare",
}
for msgType, expected := range expectedMapping {
assert.Equal(t, expected, msgType.String())
}
}
func TestPbftState_ToString(t *testing.T) {
expectedMapping := map[PbftState]string{
AcceptState: "AcceptState",
RoundChangeState: "RoundChangeState",
ValidateState: "ValidateState",
CommitState: "CommitState",
SyncState: "SyncState",
DoneState: "DoneState",
}
for pbftState, expected := range expectedMapping {
assert.Equal(t, expected, pbftState.String())
}
}
type signDelegate func([]byte) ([]byte, error)
type testerAccount struct {
alias string
priv *ecdsa.PrivateKey
signFn signDelegate
}
func (t *testerAccount) NodeID() NodeID {
return NodeID(t.alias)
}
func (t *testerAccount) Sign(b []byte) ([]byte, error) {
if t.signFn != nil {
return t.signFn(b)
}
return nil, nil
}
type testerAccountPool struct {
accounts []*testerAccount
}
func newTesterAccountPool(num ...int) *testerAccountPool {
t := &testerAccountPool{
accounts: []*testerAccount{},
}
if len(num) == 1 {
for i := 0; i < num[0]; i++ {
t.accounts = append(t.accounts, &testerAccount{
alias: strconv.Itoa(i),
priv: generateKey(),
})
}
}
return t
}
func (ap *testerAccountPool) add(accounts ...string) {
for _, account := range accounts {
if acct := ap.get(account); acct != nil {
continue
}
ap.accounts = append(ap.accounts, &testerAccount{
alias: account,
priv: generateKey(),
})
}
}
func (ap *testerAccountPool) get(name string) *testerAccount {
for _, account := range ap.accounts {
if account.alias == name {
return account
}
}
return nil
}
func (ap *testerAccountPool) validatorSet() ValidatorSet {
validatorIds := []NodeID{}
for _, acc := range ap.accounts {
validatorIds = append(validatorIds, NodeID(acc.alias))
}
return convertToMockValidatorSet(validatorIds)
}
// Helper function which enables creation of MessageReq.
// Note: sender needs to be seeded to the pool before invoking, otherwise senderId will be set to provided sender parameter.
func (ap *testerAccountPool) createMessage(sender string, messageType MsgType, round ...uint64) *MessageReq {
poolSender := ap.get(sender)
if poolSender != nil {
sender = poolSender.alias
}
return createMessage(sender, messageType, round...)
}
func generateKey() *ecdsa.PrivateKey {
prv, err := ecdsa.GenerateKey(elliptic.P384(), crand.Reader)
if err != nil {
panic(err)
}
return prv
}
func newMockValidatorSet(validatorIds []string) ValidatorSet {
validatorNodeIds := []NodeID{}
for _, id := range validatorIds {
validatorNodeIds = append(validatorNodeIds, NodeID(id))
}
return convertToMockValidatorSet(validatorNodeIds)
}
func convertToMockValidatorSet(validatorIds []NodeID) ValidatorSet {
validatorSet := valString(validatorIds)
return &validatorSet
}
type valString []NodeID
func (v *valString) CalcProposer(round uint64) NodeID {
seed := uint64(0)
offset := 0
// add last proposer
seed = uint64(offset) + round
pick := seed % uint64(v.Len())
return (*v)[pick]
}
func (v *valString) Index(id NodeID) int {
for i, currentId := range *v {
if currentId == id {
return i
}
}
return -1
}
func (v *valString) Includes(id NodeID) bool {
for _, currentId := range *v {
if currentId == id {
return true
}
}
return false
}
func (v *valString) Len() int {
return len(*v)
}