forked from anthdm/hbbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbc_test.go
246 lines (220 loc) · 5.48 KB
/
rbc_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
package hbbft
import (
"log"
"sync"
"testing"
"github.com/klauspost/reedsolomon"
"github.com/stretchr/testify/assert"
)
// Test RBC where 1 node will not provide its value. We use 4 nodes that will
// tolerate 1 faulty node. The 3 good nodes should be able te reconstruct the
// proposed value just fine.
func TestRBC1FaultyNode(t *testing.T) {
var (
n = 4
pid = 0
resCh = make(chan bcResult, 3)
value = []byte("not a normal looking payload")
faultyNode = uint64(3)
)
nodes := makeRBCNodes(n, pid, resCh)
var wg sync.WaitGroup
wg.Add(n - 1)
// Start a routine that will collect the results from all the good nodes.
// In this case 3 results should be equal to the proposed value.
go func() {
for {
res := <-resCh
// Test that we really have a faulty node.
assert.NotEqual(t, res.nodeID, faultyNode)
assert.Equal(t, value, res.value)
wg.Done()
}
}()
// Faulty node will not provide its input.
nodes[faultyNode].faulty = true
assert.Nil(t, nodes[pid].inputValue(value))
wg.Wait()
}
// Test RBC with 4 good nodes in the network. We expect all 4 nodes to output
// the proposed value.
func TestRBC4GoodNodes(t *testing.T) {
var (
n = 4
pid = 0
resCh = make(chan bcResult)
value = []byte("not a normal looking payload")
)
nodes := makeRBCNodes(n, pid, resCh)
var wg sync.WaitGroup
wg.Add(n)
// Start a routine that will collect the results from all the nodes. In this
// case we expect all 4 nodes to ouput the proposed value.
go func() {
for {
res := <-resCh
assert.Equal(t, value, res.value)
wg.Done()
}
}()
assert.Nil(t, nodes[pid].inputValue(value))
wg.Wait()
}
func TestRBCInputValue(t *testing.T) {
rbc := NewRBC(Config{
N: 4,
}, 0)
reqs, err := rbc.InputValue([]byte("this is a test string"))
assert.Nil(t, err)
assert.Equal(t, rbc.N-1, len(reqs))
assert.Equal(t, 1, len(rbc.Messages()))
assert.Equal(t, 0, len(rbc.messages))
}
func TestNewReliableBroadcast(t *testing.T) {
assertState := func(t *testing.T, rb *RBC, cfg Config) {
assert.NotNil(t, rb.enc)
assert.NotNil(t, rb.recvEchos)
assert.NotNil(t, rb.recvReadys)
assert.Equal(t, rb.numParityShards, cfg.F*2)
assert.Equal(t, rb.numDataShards, cfg.N-rb.numParityShards)
assert.Equal(t, 0, len(rb.messages))
}
cfg := Config{N: 4, F: 1}
rb := NewRBC(cfg, 0)
assertState(t, rb, cfg)
cfg = Config{N: 18, F: 4}
rb = NewRBC(cfg, 0)
assertState(t, rb, cfg)
cfg = Config{N: 100, F: 10}
rb = NewRBC(cfg, 0)
assertState(t, rb, cfg)
}
func TestRBCOutputIsNilAfterConsuming(t *testing.T) {
rbc := NewRBC(Config{N: 4}, 0)
output := []byte("a")
rbc.output = output
assert.Equal(t, output, rbc.Output())
assert.Nil(t, rbc.Output())
}
func TestRBCMessagesIsEmptyAfterConsuming(t *testing.T) {
rbc := NewRBC(Config{N: 4}, 0)
rbc.messages = []*BroadcastMessage{&BroadcastMessage{}}
assert.Equal(t, 1, len(rbc.Messages()))
assert.Equal(t, 0, len(rbc.Messages()))
}
func TestMakeShards(t *testing.T) {
var (
data = []byte("this is a very normal string.")
nP = 2
nD = 4
)
for i := 0; i < 10; i++ {
nP += i
nD += i
enc, err := reedsolomon.New(nD, nP)
assert.Nil(t, err)
shards, err := makeShards(enc, data)
assert.Nil(t, err)
assert.Equal(t, nP+nD, len(shards))
}
}
func TestMakeProofRequests(t *testing.T) {
var (
data = []byte("this is a very normal string.")
nP = 2
nD = 4
)
enc, err := reedsolomon.New(nD, nP)
assert.Nil(t, err)
shards, err := makeShards(enc, data)
assert.Nil(t, err)
assert.Equal(t, nP+nD, len(shards))
reqs, err := makeProofRequests(shards)
assert.Nil(t, err)
assert.Equal(t, nP+nD, len(reqs))
for _, r := range reqs {
assert.True(t, validateProof(r))
}
}
type bcResult struct {
nodeID uint64
value []byte
}
// simple engine to test RBC independently.
type testRBCEngine struct {
faulty bool
rbc *RBC
rpcCh <-chan RPC
resCh chan bcResult
transport Transport
}
func newTestRBCEngine(resCh chan bcResult, rbc *RBC, tr Transport) *testRBCEngine {
return &testRBCEngine{
rbc: rbc,
rpcCh: tr.Consume(),
resCh: resCh,
transport: tr,
}
}
func (e *testRBCEngine) run() {
for {
select {
case rpc := <-e.rpcCh:
err := e.rbc.HandleMessage(rpc.NodeID, rpc.Payload.(*BroadcastMessage))
if err != nil {
log.Println(err)
continue
}
for _, msg := range e.rbc.Messages() {
e.transport.Broadcast(e.rbc.ID, msg)
}
if output := e.rbc.Output(); output != nil {
// Faulty node will refuse to send its produced output, causing
// potential disturb of conensus liveness.
if e.faulty {
continue
}
e.resCh <- bcResult{
nodeID: e.rbc.ID,
value: output,
}
}
}
}
}
func (e *testRBCEngine) inputValue(data []byte) error {
reqs, err := e.rbc.InputValue(data)
if err != nil {
return err
}
msgs := make([]interface{}, len(reqs))
for i := 0; i < len(reqs); i++ {
msgs[i] = reqs[i]
}
e.transport.SendProofMessages(e.rbc.ID, msgs)
return nil
}
func makeRBCNodes(n, pid int, resCh chan bcResult) []*testRBCEngine {
transports := makeTransports(n)
connectTransports(transports)
nodes := make([]*testRBCEngine, len(transports))
for i, tr := range transports {
cfg := Config{
ID: uint64(i),
N: len(transports),
}
nodes[i] = newTestRBCEngine(resCh, NewRBC(cfg, uint64(pid)), tr)
go nodes[i].run()
}
return nodes
}
func connectTransports(tt []Transport) {
for i := 0; i < len(tt); i++ {
for ii := 0; ii < len(tt); ii++ {
if ii == i {
continue
}
tt[i].Connect(tt[ii].Addr(), tt[ii])
}
}
}