-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.go
247 lines (203 loc) · 5.36 KB
/
integration_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
package qbft
import (
"fmt"
"log"
"math/rand"
"os"
"sort"
"testing"
"time"
)
type simTransport func(from, to NodeID) time.Duration
func TestIntegration(t *testing.T) {
simTransport := func(from, to NodeID) time.Duration {
// create a random time duration between 500 and 4500 milliseconds
// to simulate the network latency
return time.Duration(500+rand.Intn(2500)) * time.Millisecond
}
c := newIntegrationCluster(3, simTransport)
var done bool
var steps int
for steps = 0; steps < 10000 && !done; steps++ {
c.runStep()
done = c.isDone()
}
if !done {
t.Fatal("not done")
}
t.Logf("Num of steps: %d", steps)
}
type integrationCluster struct {
nodes map[NodeID]*integrationNode
messages []*integrationMessage
timestamp uint64
simTransport simTransport
valSet ValidatorSet
}
func newIntegrationCluster(num int, transport simTransport) *integrationCluster {
ic := &integrationCluster{
nodes: map[NodeID]*integrationNode{},
messages: []*integrationMessage{},
timestamp: 0,
simTransport: transport,
}
ids := []NodeID{}
for i := 0; i < num; i++ {
ids = append(ids, NodeID(fmt.Sprintf("node-%d", i)))
}
accPool := newTesterAccountPool()
for _, id := range ids {
accPool.add(string(id))
}
valSet := accPool.validatorSet()
ic.valSet = valSet
for _, id := range ids {
node := &integrationNode{
id: id,
height: 0,
valSet: valSet,
}
qbft := New(id,
WithSigner(&mockSigner{node: id}),
WithTransport(ic),
WithRoundTimeout(5*time.Second),
WithTimer(&integrationTimer{node: id, ic: ic}),
WithLogger(log.New(os.Stdout, string(id)+": ", 0)),
)
node.qbft = qbft
ic.nodes[id] = node
}
// set the backend now once everything has been initialized
for _, node := range ic.nodes {
node.qbft.SetBackend(node)
}
// emit the first proposal since that one does not have any
// external event that dispatches the action
proposer := valSet.CalculateProposer(0)
if err := ic.nodes[proposer].qbft.buildInitialProposal(); err != nil {
panic(err)
}
return ic
}
func (i *integrationCluster) SetTimeout(id NodeID, timeout time.Duration) {
msg := &integrationMessage{
timestamp: i.timestamp + uint64(timeout.Milliseconds()),
msg: nil,
receiver: id,
}
i.pushMessage(msg)
}
func (ic *integrationCluster) popMessage() *integrationMessage {
if len(ic.messages) == 0 {
return nil
}
var pop *integrationMessage
pop, ic.messages = ic.messages[0], ic.messages[1:]
return pop
}
func (ic *integrationCluster) pushMessage(msg *integrationMessage) {
fmt.Printf("Add message to=%s timestamp=%d (%v)\n", msg.receiver, msg.timestamp, msg)
// append the message and sort the queue
ic.messages = append(ic.messages, msg)
sort.Slice(ic.messages, func(i, j int) bool {
return ic.messages[i].timestamp < ic.messages[j].timestamp
})
}
type integrationTimer struct {
node NodeID
ic *integrationCluster
}
func (i *integrationTimer) TimeCh() <-chan time.Time {
panic("not required")
}
func (i *integrationTimer) SetTimeout(n time.Duration) {
i.ic.SetTimeout(i.node, n)
}
func (i *integrationCluster) Recv() chan *QBFTMessageWithRecipient {
panic("not required")
}
func (i *integrationCluster) Send(rawMsg *QBFTMessageWithRecipient) error {
// send the message to every queue
for _, node := range i.nodes {
if node.id == rawMsg.Sender {
continue
}
latency := time.Duration(0)
if i.simTransport != nil {
latency = i.simTransport(rawMsg.Sender, node.id)
}
msg := &integrationMessage{
timestamp: i.timestamp + uint64(latency.Milliseconds()),
msg: rawMsg,
receiver: node.id,
}
i.pushMessage(msg)
}
return nil
}
func (i *integrationCluster) isDone() bool {
num := 0
for _, node := range i.nodes {
if node.sealedProposal != nil {
num++
}
}
quorum, _ := getQuorumNumbers(i.valSet.VotingPower())
return num >= int(quorum)
}
func (i *integrationCluster) runStep() {
msg := i.popMessage()
if msg == nil {
// simulation is done?
return
}
/*
if msg.msg != nil {
fmt.Printf("Run step (MSG): receiver=%s timestamp=%d type=%s round=%d %d (%v)\n", msg.receiver, msg.timestamp, msg.msg.Message.typ(), msg.msg.Message.Round(), i.nodes[msg.receiver].qbft.state.round, msg)
} else {
fmt.Printf("Run step (TIMEOUT): receiver=%s timestamp=%d %d (%v)\n", msg.receiver, msg.timestamp, i.nodes[msg.receiver].qbft.state.round, msg)
}
*/
// all the messages generated during this interation
// will take as reference this timestamp
i.timestamp = msg.timestamp
node := i.nodes[msg.receiver]
if node.sealedProposal != nil {
return
}
node.qbft.handleMessage(msg.msg)
}
type integrationMessage struct {
timestamp uint64
msg *QBFTMessageWithRecipient
receiver NodeID
}
type integrationNode struct {
qbft *QBFT
id NodeID
height uint64
valSet ValidatorSet
sealedProposal *SealedProposal
}
func (i *integrationNode) Height() uint64 {
return i.height
}
func (i *integrationNode) ValidatorSet() ValidatorSet {
return i.valSet
}
func (i *integrationNode) Insert(p *SealedProposal) error {
i.sealedProposal = p
return nil
}
func (i *integrationNode) BuildProposal(round uint64) (*Block, []byte, error) {
block := &Block{
RoundNumber: round,
Height: i.height,
Body: []byte{0x1, 0x2},
Proposer: i.id,
}
return block, nil, nil
}
func (i *integrationNode) ValidateProposal(*Proposal) error {
return nil
}