forked from anthdm/hbbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
honey_badger_test.go
92 lines (82 loc) · 1.7 KB
/
honey_badger_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
package hbbft
import (
"encoding/gob"
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEngineAddTransaction(t *testing.T) {
cfg := Config{
N: 6,
ID: 0,
Nodes: []uint64{0, 1, 2, 3, 4, 5, 6},
}
e := NewHoneyBadger(cfg)
nTx := 100
for i := 0; i < nTx; i++ {
e.AddTransaction(&tx{uint64(i)})
}
assert.Equal(t, nTx, e.txBuffer.len())
}
type testNode struct {
hb *HoneyBadger
rpcCh <-chan RPC
transport Transport
}
func newTestNode(hb *HoneyBadger, tr Transport) *testNode {
return &testNode{
hb: hb,
transport: tr,
rpcCh: tr.Consume(),
}
}
func (n *testNode) run() {
for {
select {
case rpc := <-n.rpcCh:
switch t := rpc.Payload.(type) {
case HBMessage:
if err := n.hb.HandleMessage(rpc.NodeID, t.Epoch, t.Payload.(*ACSMessage)); err != nil {
log.Println(err)
continue
}
for _, msg := range n.hb.messageQue.messages() {
n.transport.SendMessage(n.hb.ID, msg.To, msg.Payload)
}
}
}
}
}
func (n *testNode) propose() error {
if err := n.hb.propose(); err != nil {
return err
}
for _, msg := range n.hb.messageQue.messages() {
n.transport.SendMessage(n.hb.ID, msg.To, msg.Payload)
}
return nil
}
func makeTestNodes(n int) []*testNode {
var (
transports = makeTransports(n)
nodes = make([]*testNode, len(transports))
)
connectTransports(transports)
for i, tr := range transports {
cfg := Config{
ID: uint64(i),
N: len(transports),
Nodes: makeids(n),
}
nodes[i] = newTestNode(NewHoneyBadger(cfg), tr)
nTx := 10000
for ii := 0; ii < nTx; ii++ {
nodes[i].hb.AddTransaction(&tx{uint64(ii)})
}
go nodes[i].run()
}
return nodes
}
func init() {
gob.Register(&tx{})
}