-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpos_test.go
More file actions
161 lines (128 loc) · 4.03 KB
/
pos_test.go
File metadata and controls
161 lines (128 loc) · 4.03 KB
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
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package simplex_test
import (
"bytes"
"testing"
"time"
"github.com/ava-labs/simplex"
"github.com/ava-labs/simplex/testutil"
"github.com/stretchr/testify/require"
)
func TestPoS(t *testing.T) {
weights := map[int]uint64{
1: 2,
2: 2,
3: 5,
4: 1,
}
nodes := []simplex.NodeID{{1}, {2}, {3}, {4}}
posSigAggregator := &testutil.TestSignatureAggregator{
IsQuorumFunc: func(signatures []simplex.NodeID) bool {
var totalWeight uint64
for _, signer := range signatures {
totalWeight += weights[int(signer[0])]
}
return totalWeight > 6
},
}
testConf := &testutil.TestNodeConfig{SigAggregator: posSigAggregator, ReplicationEnabled: true}
net := testutil.NewControlledNetwork(t, nodes)
testutil.NewControlledSimplexNode(t, nodes[0], net, testConf)
testutil.NewControlledSimplexNode(t, nodes[1], net, testConf)
testutil.NewControlledSimplexNode(t, nodes[2], net, testConf)
testutil.NewControlledSimplexNode(t, nodes[3], net, testConf)
net.StartInstances()
defer net.StopInstances()
// Totally order 4 blocks, each proposed by a different node
for seq := uint64(0); seq < 5; seq++ {
net.TriggerLeaderBlockBuilder(seq)
for _, n := range net.Instances {
n.Storage.WaitForBlockCommit(seq)
}
}
for i := range net.Instances {
require.Equal(t, uint64(5), net.Instances[i].E.Metadata().Round)
}
// Next, disconnect the second node which has 20% of the stake
net.Disconnect(nodes[1])
net.AdvanceWithoutLeader(5, nodes[1])
// Re-connect it and ensure it catches up
net.Connect(nodes[1])
net.TriggerLeaderBlockBuilder(6)
for i, n := range net.Instances {
if i == 1 {
continue
}
n.Storage.WaitForBlockCommit(5)
}
net.Instances[1].Storage.WaitForBlockCommit(5)
net.TriggerLeaderBlockBuilder(7)
for _, n := range net.Instances {
n.Storage.WaitForBlockCommit(6)
}
net.TriggerLeaderBlockBuilder(8)
for _, n := range net.Instances {
n.Storage.WaitForBlockCommit(7)
}
// Ensure the leader is the second node
require.Equal(t, nodes[1], simplex.LeaderForRound(nodes, 9))
// Take down the first node and the last node, leave only the second and third node.
net.Disconnect(nodes[0])
net.Disconnect(nodes[3])
// At this point the second node is blacklisted, so we skip this round.
testutil.WaitToEnterRound(t, net.Instances[1].E, 10)
testutil.WaitToEnterRound(t, net.Instances[2].E, 10)
net.TriggerLeaderBlockBuilder(10)
for _, n := range net.Instances {
if bytes.Equal(n.E.ID, nodes[0]) || bytes.Equal(n.E.ID, nodes[3]) {
continue
}
n.Storage.WaitForBlockCommit(8)
}
// Bring up the two nodes back.
net.Connect(nodes[0])
net.Connect(nodes[3])
// Have the nodes advance to round 14 where the leader is 2.
for net.Instances[1].E.Metadata().Round != 14 && net.Instances[2].E.Metadata().Round != 14 {
for _, n := range net.Instances {
if bytes.Equal(n.E.ID, nodes[0]) || bytes.Equal(n.E.ID, nodes[3]) {
continue
}
n.BlockShouldBeBuilt()
n.AdvanceTime(n.E.EpochConfig.MaxProposalWait / 4)
}
time.Sleep(time.Millisecond * 100)
}
net.TriggerLeaderBlockBuilder(14)
for _, n := range net.Instances {
if bytes.Equal(n.E.ID, nodes[0]) || bytes.Equal(n.E.ID, nodes[3]) {
continue
}
n.Storage.WaitForBlockCommit(9)
}
// Ensure all nodes including those that were offline, now caught up with the latest round.
for _, n := range net.Instances {
testutil.WaitToEnterRound(t, n.E, 15)
}
// Now, disconnect the node with the highest stake (node 3) and observe the network is stuck
net.Disconnect(nodes[2])
net.TriggerLeaderBlockBuilder(15)
timedOut := make(map[int]struct{})
for len(timedOut) < 3 {
for i, n := range net.Instances {
if bytes.Equal(n.E.ID, nodes[2]) {
continue
}
n.BlockShouldBeBuilt()
n.AdvanceTime(n.E.EpochConfig.MaxProposalWait / 4)
if n.WAL.ContainsEmptyVote(15) {
timedOut[i] = struct{}{}
}
}
time.Sleep(time.Millisecond * 100)
}
for _, n := range net.Instances {
require.False(t, n.WAL.ContainsEmptyNotarization(15))
}
}