forked from pokt-network/pocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotstuff_test.go
243 lines (206 loc) · 7.14 KB
/
hotstuff_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
package consensus_tests
import (
"fmt"
"testing"
"time"
"github.com/benbjohnson/clock"
"github.com/pokt-network/pocket/consensus"
typesCons "github.com/pokt-network/pocket/consensus/types"
"github.com/pokt-network/pocket/shared/modules"
"github.com/stretchr/testify/require"
)
func TestHotstuff4Nodes1BlockHappyPath(t *testing.T) {
clockMock := clock.NewMock()
// Test configs
numNodes := 4
runtimeMgrs := GenerateNodeRuntimeMgrs(t, numNodes, clockMock)
go timeReminder(clockMock, 100*time.Millisecond)
// Create & start test pocket nodes
testChannel := make(modules.EventsChannel, 100)
pocketNodes := CreateTestConsensusPocketNodes(t, runtimeMgrs, testChannel)
StartAllTestPocketNodes(t, pocketNodes)
// Debug message to start consensus by triggering first view change
for _, pocketNode := range pocketNodes {
TriggerNextView(t, pocketNode)
}
advanceTime(clockMock, 10*time.Millisecond)
// NewRound
newRoundMessages, err := WaitForNetworkConsensusMessages(t, clockMock, testChannel, consensus.NewRound, consensus.Propose, numNodes, 1000)
require.NoError(t, err)
for nodeId, pocketNode := range pocketNodes {
nodeState := GetConsensusNodeState(pocketNode)
assertNodeConsensusView(t, nodeId,
typesCons.ConsensusNodeState{
Height: 1,
Step: uint8(consensus.NewRound),
Round: 0,
},
nodeState)
require.Equal(t, false, nodeState.IsLeader)
}
for _, message := range newRoundMessages {
P2PBroadcast(t, pocketNodes, message)
}
// Leader election is deterministic for now, so we know its NodeId
// TODO(olshansky): Use seeding for deterministic leader election in unit tests.
leaderId := typesCons.NodeId(2)
leader := pocketNodes[leaderId]
advanceTime(clockMock, 10*time.Millisecond)
// Prepare
prepareProposal, err := WaitForNetworkConsensusMessages(t, clockMock, testChannel, consensus.Prepare, consensus.Propose, 1, 1000)
require.NoError(t, err)
for nodeId, pocketNode := range pocketNodes {
nodeState := GetConsensusNodeState(pocketNode)
assertNodeConsensusView(t, nodeId,
typesCons.ConsensusNodeState{
Height: 1,
Step: uint8(consensus.Prepare),
Round: 0,
},
nodeState)
require.Equal(t, leaderId, nodeState.LeaderId, fmt.Sprintf("%d should be the current leader", leaderId))
}
for _, message := range prepareProposal {
P2PBroadcast(t, pocketNodes, message)
}
advanceTime(clockMock, 10*time.Millisecond)
// Precommit
prepareVotes, err := WaitForNetworkConsensusMessages(t, clockMock, testChannel, consensus.Prepare, consensus.Vote, numNodes, 1000)
require.NoError(t, err)
for _, vote := range prepareVotes {
P2PSend(t, leader, vote)
}
advanceTime(clockMock, 10*time.Millisecond)
preCommitProposal, err := WaitForNetworkConsensusMessages(t, clockMock, testChannel, consensus.PreCommit, consensus.Propose, 1, 1000)
require.NoError(t, err)
for nodeId, pocketNode := range pocketNodes {
nodeState := GetConsensusNodeState(pocketNode)
assertNodeConsensusView(t, nodeId,
typesCons.ConsensusNodeState{
Height: 1,
Step: uint8(consensus.PreCommit),
Round: 0,
},
nodeState)
require.Equal(t, leaderId, nodeState.LeaderId, fmt.Sprintf("%d should be the current leader", leaderId))
}
for _, message := range preCommitProposal {
P2PBroadcast(t, pocketNodes, message)
}
advanceTime(clockMock, 10*time.Millisecond)
// Commit
preCommitVotes, err := WaitForNetworkConsensusMessages(t, clockMock, testChannel, consensus.PreCommit, consensus.Vote, numNodes, 1000)
require.NoError(t, err)
for _, vote := range preCommitVotes {
P2PSend(t, leader, vote)
}
advanceTime(clockMock, 10*time.Millisecond)
commitProposal, err := WaitForNetworkConsensusMessages(t, clockMock, testChannel, consensus.Commit, consensus.Propose, 1, 1000)
require.NoError(t, err)
for nodeId, pocketNode := range pocketNodes {
nodeState := GetConsensusNodeState(pocketNode)
assertNodeConsensusView(t, nodeId,
typesCons.ConsensusNodeState{
Height: 1,
Step: uint8(consensus.Commit),
Round: 0,
},
nodeState)
require.Equal(t, leaderId, nodeState.LeaderId, fmt.Sprintf("%d should be the current leader", leaderId))
}
for _, message := range commitProposal {
P2PBroadcast(t, pocketNodes, message)
}
advanceTime(clockMock, 10*time.Millisecond)
// Decide
commitVotes, err := WaitForNetworkConsensusMessages(t, clockMock, testChannel, consensus.Commit, consensus.Vote, numNodes, 1000)
require.NoError(t, err)
for _, vote := range commitVotes {
P2PSend(t, leader, vote)
}
advanceTime(clockMock, 10*time.Millisecond)
decideProposal, err := WaitForNetworkConsensusMessages(t, clockMock, testChannel, consensus.Decide, consensus.Propose, 1, 1000)
require.NoError(t, err)
for pocketId, pocketNode := range pocketNodes {
nodeState := GetConsensusNodeState(pocketNode)
// Leader has already committed the block and hence moved to the next height.
if pocketId == leaderId {
assertNodeConsensusView(t, pocketId,
typesCons.ConsensusNodeState{
Height: 2,
Step: uint8(consensus.NewRound),
Round: 0,
},
nodeState)
require.Equal(t, nodeState.LeaderId, typesCons.NodeId(0), "Leader should be empty")
continue
}
assertNodeConsensusView(t, pocketId,
typesCons.ConsensusNodeState{
Height: 1,
Step: uint8(consensus.Decide),
Round: 0,
},
nodeState)
require.Equal(t, leaderId, nodeState.LeaderId, fmt.Sprintf("%d should be the current leader", leaderId))
}
for _, message := range decideProposal {
P2PBroadcast(t, pocketNodes, message)
}
advanceTime(clockMock, 10*time.Millisecond)
// Block has been committed and new round has begun
_, err = WaitForNetworkConsensusMessages(t, clockMock, testChannel, consensus.NewRound, consensus.Propose, numNodes, 1000)
require.NoError(t, err)
for pocketId, pocketNode := range pocketNodes {
nodeState := GetConsensusNodeState(pocketNode)
assertNodeConsensusView(t, pocketId,
typesCons.ConsensusNodeState{
Height: 2,
Step: uint8(consensus.NewRound),
Round: 0,
},
nodeState)
require.Equal(t, nodeState.LeaderId, typesCons.NodeId(0), "Leader should be empty")
}
}
/*
func TestHotstuff4Nodes1Byzantine1Block(t *testing.T) {
t.Skip() // TODO: Implement
}
func TestHotstuff4Nodes2Byzantine1Block(t *testing.T) {
t.Skip() // TODO: Implement
}
func TestHotstuff4Nodes1BlockNetworkPartition(t *testing.T) {
t.Skip() // TODO: Implement
}
func TestHotstuff4Nodes1Block4Rounds(t *testing.T) {
t.Skip() // TODO: Implement
}
func TestHotstuff4Nodes2Blocks(t *testing.T) {
t.Skip() // TODO: Implement
}
func TestHotstuff4Nodes2NewNodes1Block(t *testing.T) {
t.Skip() // TODO: Implement
}
func TestHotstuff4Nodes2DroppedNodes1Block(t *testing.T) {
t.Skip() // TODO: Implement
}
func TestHotstuff4NodesFailOnPrepare(t *testing.T) {
t.Skip() // TODO: Implement
}
func TestHotstuff4NodesFailOnPrecommit(t *testing.T) {
t.Skip() // TODO: Implement
}
func TestHotstuff4NodesFailOnCommit(t *testing.T) {
t.Skip() // TODO: Implement
}
func TestHotstuff4NodesFailOnDecide(t *testing.T) {
t.Skip() // TODO: Implement
}
func TestHotstuffValidatorWithLockedQC(t *testing.T) {
t.Skip() // TODO: Implement
}
func TestHotstuffValidatorWithLockedQCMissingNewRoundMsg(t *testing.T) {
t.Skip() // TODO: Implement
}
*/