This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtcp_peer_test.go
220 lines (191 loc) · 5.19 KB
/
tcp_peer_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
package agent
import (
"bytes"
"crypto/ecdsa"
"crypto/rand"
"encoding/binary"
"encoding/hex"
io "io"
"log"
"net"
"net/http"
_ "net/http/pprof"
"sync"
"testing"
"time"
"github.com/BDLS-bft/bdls"
"github.com/BDLS-bft/bdls/crypto/blake2b"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
)
// init will listen for 6060 while debugging
func init() {
go func() {
log.Println(http.ListenAndServe("0.0.0.0:6060", nil))
}()
}
type testParam struct {
numPeers int
numParticipants int
stopHeight int
expectedLatency time.Duration
}
func TestTCPPeer(t *testing.T) {
var params = []testParam{
{
numPeers: 20,
numParticipants: 20,
stopHeight: 5,
expectedLatency: 100 * time.Millisecond,
},
{
numPeers: 20,
numParticipants: 20,
stopHeight: 5,
expectedLatency: 200 * time.Millisecond,
},
{
numPeers: 20,
numParticipants: 20,
stopHeight: 5,
expectedLatency: 300 * time.Millisecond,
},
{
numPeers: 20,
numParticipants: 20,
stopHeight: 5,
expectedLatency: 500 * time.Millisecond,
},
{
numPeers: 20,
numParticipants: 20,
stopHeight: 5,
expectedLatency: 1000 * time.Millisecond,
},
}
for i := 0; i < len(params); i++ {
t.Logf("-=-=- TESTING CASE: [%v/%v] -=-=-", i+1, len(params))
testConsensus(t, ¶ms[i])
}
}
func testConsensus(t *testing.T, param *testParam) {
t.Logf("PARAMETERS: %+v", spew.Sprintf("%+v", param))
var participants []*ecdsa.PrivateKey
var coords []bdls.Identity
for i := 0; i < param.numParticipants; i++ {
privateKey, err := ecdsa.GenerateKey(bdls.S256Curve, rand.Reader)
if err != nil {
t.Fatal(err)
}
participants = append(participants, privateKey)
coords = append(coords, bdls.DefaultPubKeyToIdentity(&privateKey.PublicKey))
}
// consensus for one height
consensusOneHeight := func(currentHeight uint64) {
// randomize participants, fisher yates shuffle
n := uint32(len(participants))
for i := n - 1; i > 0; i-- {
var j uint32
binary.Read(rand.Reader, binary.LittleEndian, &j)
j = j % (i + 1)
participants[i], participants[j] = participants[j], participants[i]
}
// created a locked consensus object
var all []*bdls.Consensus
// same epoch
epoch := time.Now()
// create numPeer peers
for i := 0; i < param.numPeers; i++ {
// initiate config
config := new(bdls.Config)
config.Epoch = epoch
config.CurrentHeight = currentHeight
config.PrivateKey = participants[i] // randomized participants
config.Participants = coords // keep all pubkeys
// should replace with real function
config.StateCompare = func(a bdls.State, b bdls.State) int { return bytes.Compare(a, b) }
config.StateValidate = func(a bdls.State) bool { return true }
// consensus
consensus, err := bdls.NewConsensus(config)
assert.Nil(t, err)
consensus.SetLatency(param.expectedLatency)
all = append(all, consensus)
}
// establish full connected mesh with tcp_peer
numConns := 0
agents := make([]*TCPAgent, len(all))
for i := 0; i < len(all); i++ {
agents[i] = NewTCPAgent(all[i], participants[i])
}
for i := 0; i < len(all); i++ {
for j := 0; j < len(all); j++ {
if i != j {
c1, c2 := net.Pipe() // in memory duplex pipe to connection i & j
p1 := NewTCPPeer(c1, agents[i])
p2 := NewTCPPeer(c2, agents[j])
ok := agents[i].AddPeer(p1)
assert.True(t, ok)
ok = agents[j].AddPeer(p2)
assert.True(t, ok)
numConns += 2
// auth public key
p1.InitiatePublicKeyAuthentication()
p2.InitiatePublicKeyAuthentication()
}
}
}
<-time.After(2 * time.Second)
// make sure authentication completed
for i := 0; i < len(all); i++ {
for _, peer := range agents[i].peers {
peer.Lock()
assert.Equal(t, peer.localAuthState, localChallengeAccepted)
assert.Equal(t, peer.peerAuthStatus, peerAuthenticated)
peer.Unlock()
}
}
// after all connections have established, start updater,
// this must be done after connection establishement
// to prevent from missing <decide> messages
for i := 0; i < len(all); i++ {
agents[i].Update()
}
var wg sync.WaitGroup
wg.Add(param.numPeers)
// selected random peers
for k := range agents {
go func(i int) {
agent := agents[i]
defer wg.Done()
data := make([]byte, 1024)
io.ReadFull(rand.Reader, data)
agent.Propose(data)
for {
newHeight, newRound, newState := agent.GetLatestState()
if newHeight > currentHeight {
now := time.Now()
// only one peer print the decide
if i == 0 {
h := blake2b.Sum256(newState)
t.Logf("%v <decide> at height:%v round:%v hash:%v", now.Format("15:04:05"), newHeight, newRound, hex.EncodeToString(h[:]))
}
return
}
// wait
<-time.After(20 * time.Millisecond)
}
}(k)
}
// wait for all peers exit
wg.Wait()
// close all peers when waitgroup exit
for k := range agents {
agents[k].Close()
}
}
// loop to stopHeight
for i := 0; i < param.stopHeight; i++ {
consensusOneHeight(uint64(i))
}
t.Logf("consensus stopped at height:%v for %v peers %v participants", param.stopHeight, param.numPeers, param.numParticipants)
}