forked from 0xPolygon/pbft-consensus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathe2e_partition_test.go
101 lines (85 loc) · 2.97 KB
/
e2e_partition_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
package e2e
import (
"math"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestE2E_Partition_OneMajority(t *testing.T) {
const nodesCnt = 5
hook := newPartitionTransport(300 * time.Millisecond)
c := newPBFTCluster(t, "majority_partition", "prt", nodesCnt, hook)
c.Start()
defer c.Stop()
err := c.WaitForHeight(5, 1*time.Minute)
assert.NoError(t, err)
// create two partitions.
majorityPartition := []string{"prt_0", "prt_1", "prt_2"}
minorityPartition := []string{"prt_3", "prt_4"}
hook.Partition(majorityPartition, minorityPartition)
// only the majority partition will be able to sync
err = c.WaitForHeight(10, 1*time.Minute, majorityPartition)
assert.NoError(t, err)
// the partition with two nodes is stuck
c.IsStuck(10*time.Second, minorityPartition)
// reset all partitions
hook.Reset()
allNodes := make([]string, len(c.nodes))
for i, node := range c.Nodes() {
allNodes[i] = node.name
}
// all nodes should be able to sync
err = c.WaitForHeight(15, 1*time.Minute, allNodes)
assert.NoError(t, err)
}
func TestE2E_Partition_MajorityCanValidate(t *testing.T) {
const nodesCnt = 7 // N=3F + 1, F = 2
hook := newPartitionTransport(300 * time.Millisecond)
c := newPBFTCluster(t, "majority_partition", "prt", nodesCnt, hook)
limit := int(math.Floor(nodesCnt*2.0/3.0)) + 1 // 2F+1 nodes can Validate
for _, node := range c.Nodes() {
node.setFaultyNode(node.name >= "prt_"+strconv.Itoa(limit))
}
c.Start()
defer c.Stop()
names := generateNodeNames(0, limit, "prt_")
err := c.WaitForHeight(4, 1*time.Minute, names)
assert.NoError(t, err)
// restart minority and wait to sync
for _, node := range c.Nodes() {
if node.name >= "prt_"+strconv.Itoa(limit) {
node.Restart()
}
}
err = c.WaitForHeight(4, 1*time.Minute)
assert.NoError(t, err)
}
func TestE2E_Partition_MajorityCantValidate(t *testing.T) {
const nodesCnt = 7 // N=3F + 1, F = 2
hook := newPartitionTransport(300 * time.Millisecond)
c := newPBFTCluster(t, "majority_partition", "prt", nodesCnt, hook)
limit := int(math.Floor(nodesCnt * 2.0 / 3.0)) // + 1 removed because 2F+1 nodes is majority
for _, node := range c.Nodes() {
node.setFaultyNode(node.name < "prt_"+strconv.Itoa(limit))
}
c.Start()
defer c.Stop()
names := generateNodeNames(limit, nodesCnt, "prt_")
err := c.WaitForHeight(3, 1*time.Minute, names)
assert.Errorf(t, err, "Height reached for minority of nodes")
}
func TestE2E_Partition_BigMajorityCantValidate(t *testing.T) {
const nodesCnt = 100
hook := newPartitionTransport(300 * time.Millisecond)
c := newPBFTCluster(t, "majority_partition", "prt", nodesCnt, hook)
limit := int(math.Floor(nodesCnt * 2.0 / 3.0)) // + 1 removed because 2F+1 nodes is majority
for _, node := range c.Nodes() {
node.setFaultyNode(node.name <= "prt_"+strconv.Itoa(limit))
}
c.Start()
defer c.Stop()
nodeNames := generateNodeNames(limit, nodesCnt, "prt_")
err := c.WaitForHeight(8, 1*time.Minute, nodeNames)
assert.Errorf(t, err, "Height reached for minority of nodes")
}