forked from bnb-chain/bsc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_bsc_test.go
More file actions
256 lines (222 loc) · 7.36 KB
/
handler_bsc_test.go
File metadata and controls
256 lines (222 loc) · 7.36 KB
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
248
249
250
251
252
253
254
255
256
package eth
import (
"fmt"
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/protocols/bsc"
"github.com/ethereum/go-ethereum/eth/protocols/eth"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
)
type testBscHandler struct {
voteBroadcasts event.Feed
}
func (h *testBscHandler) Chain() *core.BlockChain { panic("no backing chain") }
func (h *testBscHandler) RunPeer(peer *bsc.Peer, handler bsc.Handler) error {
panic("not used in tests")
}
func (h *testBscHandler) PeerInfo(enode.ID) interface{} { panic("not used in tests") }
func (h *testBscHandler) Handle(peer *bsc.Peer, packet bsc.Packet) error {
switch packet := packet.(type) {
case *bsc.VotesPacket:
h.voteBroadcasts.Send(packet.Votes)
return nil
default:
panic(fmt.Sprintf("unexpected bsc packet type in tests: %T", packet))
}
}
func TestSendVotes68(t *testing.T) { testSendVotes(t, eth.ETH68) }
func testSendVotes(t *testing.T, protocol uint) {
t.Parallel()
// Create a message handler and fill the pool with big votes
handler := newTestHandler()
defer handler.close()
insert := make([]*types.VoteEnvelope, 100)
for index := range insert {
vote := types.VoteEnvelope{
VoteAddress: types.BLSPublicKey{},
Signature: types.BLSSignature{},
Data: &types.VoteData{
SourceNumber: uint64(0),
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))),
TargetNumber: uint64(index),
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(index)))),
},
}
insert[index] = &vote
go handler.votepool.PutVote(&vote)
}
time.Sleep(250 * time.Millisecond) // Wait until vote events get out of the system (can't use events, vote broadcaster races with peer join)
protos := []p2p.Protocol{
{
Name: "eth",
Version: eth.ETH68,
},
{
Name: "bsc",
Version: bsc.Bsc1,
},
}
caps := []p2p.Cap{
{
Name: "eth",
Version: eth.ETH68,
},
{
Name: "bsc",
Version: bsc.Bsc1,
},
}
// Create a source handler to send messages through and a sink peer to receive them
p2pEthSrc, p2pEthSink := p2p.MsgPipe()
defer p2pEthSrc.Close()
defer p2pEthSink.Close()
localEth := eth.NewPeer(protocol, p2p.NewPeerWithProtocols(enode.ID{1}, protos, "", caps), p2pEthSrc, nil)
remoteEth := eth.NewPeer(protocol, p2p.NewPeerWithProtocols(enode.ID{2}, protos, "", caps), p2pEthSink, nil)
defer localEth.Close()
defer remoteEth.Close()
p2pBscSrc, p2pBscSink := p2p.MsgPipe()
defer p2pBscSrc.Close()
defer p2pBscSink.Close()
localBsc := bsc.NewPeer(bsc.Bsc1, p2p.NewPeerWithProtocols(enode.ID{1}, protos, "", caps), p2pBscSrc)
remoteBsc := bsc.NewPeer(bsc.Bsc1, p2p.NewPeerWithProtocols(enode.ID{3}, protos, "", caps), p2pBscSink)
defer localBsc.Close()
defer remoteBsc.Close()
go func(p *bsc.Peer) {
(*bscHandler)(handler.handler).RunPeer(p, func(peer *bsc.Peer) error {
return bsc.Handle((*bscHandler)(handler.handler), peer)
})
}(localBsc)
time.Sleep(200 * time.Millisecond)
remoteBsc.Handshake()
time.Sleep(200 * time.Millisecond)
go func(p *eth.Peer) {
handler.handler.runEthPeer(p, func(peer *eth.Peer) error {
return eth.Handle((*ethHandler)(handler.handler), peer)
})
}(localEth)
// Run the handshake locally to avoid spinning up a source handler
var (
head = handler.chain.CurrentBlock()
td = handler.chain.GetTd(head.Hash(), head.Number.Uint64())
)
time.Sleep(200 * time.Millisecond)
if err := remoteEth.Handshake(1, handler.chain, eth.BlockRangeUpdatePacket{}, td, nil); err != nil {
t.Fatalf("failed to run protocol handshake: %d", err)
}
// After the handshake completes, the source handler should stream the sink
// the votes, subscribe to all inbound network events
backend := new(testBscHandler)
bcasts := make(chan []*types.VoteEnvelope)
bcastSub := backend.voteBroadcasts.Subscribe(bcasts)
defer bcastSub.Unsubscribe()
go bsc.Handle(backend, remoteBsc)
// Make sure we get all the votes on the correct channels
seen := make(map[common.Hash]struct{})
for len(seen) < len(insert) {
votes := <-bcasts
for _, vote := range votes {
if _, ok := seen[vote.Hash()]; ok {
t.Errorf("duplicate vote broadcast: %x", vote.Hash())
}
seen[vote.Hash()] = struct{}{}
}
}
for _, vote := range insert {
if _, ok := seen[vote.Hash()]; !ok {
t.Errorf("missing vote: %x", vote.Hash())
}
}
}
func TestRecvVotes68(t *testing.T) { testRecvVotes(t, eth.ETH68) }
func testRecvVotes(t *testing.T, protocol uint) {
t.Parallel()
// Create a message handler and fill the pool with big votes
handler := newTestHandler()
defer handler.close()
protos := []p2p.Protocol{
{
Name: "eth",
Version: eth.ETH68,
},
{
Name: "bsc",
Version: bsc.Bsc1,
},
}
caps := []p2p.Cap{
{
Name: "eth",
Version: eth.ETH68,
},
{
Name: "bsc",
Version: bsc.Bsc1,
},
}
// Create a source handler to send messages through and a sink peer to receive them
p2pEthSrc, p2pEthSink := p2p.MsgPipe()
defer p2pEthSrc.Close()
defer p2pEthSink.Close()
localEth := eth.NewPeer(protocol, p2p.NewPeerWithProtocols(enode.ID{1}, protos, "", caps), p2pEthSrc, nil)
remoteEth := eth.NewPeer(protocol, p2p.NewPeerWithProtocols(enode.ID{2}, protos, "", caps), p2pEthSink, nil)
defer localEth.Close()
defer remoteEth.Close()
p2pBscSrc, p2pBscSink := p2p.MsgPipe()
defer p2pBscSrc.Close()
defer p2pBscSink.Close()
localBsc := bsc.NewPeer(bsc.Bsc1, p2p.NewPeerWithProtocols(enode.ID{1}, protos, "", caps), p2pBscSrc)
remoteBsc := bsc.NewPeer(bsc.Bsc1, p2p.NewPeerWithProtocols(enode.ID{3}, protos, "", caps), p2pBscSink)
defer localBsc.Close()
defer remoteBsc.Close()
go func(p *bsc.Peer) {
(*bscHandler)(handler.handler).RunPeer(p, func(peer *bsc.Peer) error {
return bsc.Handle((*bscHandler)(handler.handler), peer)
})
}(localBsc)
time.Sleep(200 * time.Millisecond)
remoteBsc.Handshake()
time.Sleep(200 * time.Millisecond)
go func(p *eth.Peer) {
handler.handler.runEthPeer(p, func(peer *eth.Peer) error {
return eth.Handle((*ethHandler)(handler.handler), peer)
})
}(localEth)
// Run the handshake locally to avoid spinning up a source handler
var (
head = handler.chain.CurrentBlock()
td = handler.chain.GetTd(head.Hash(), head.Number.Uint64())
)
time.Sleep(200 * time.Millisecond)
if err := remoteEth.Handshake(1, handler.chain, eth.BlockRangeUpdatePacket{}, td, nil); err != nil {
t.Fatalf("failed to run protocol handshake: %d", err)
}
votesCh := make(chan core.NewVoteEvent)
sub := handler.votepool.SubscribeNewVoteEvent(votesCh)
defer sub.Unsubscribe()
// Send the vote to the sink and verify that it's added to the vote pool
vote := types.VoteEnvelope{
VoteAddress: types.BLSPublicKey{},
Signature: types.BLSSignature{},
Data: &types.VoteData{
SourceNumber: uint64(0),
SourceHash: common.BytesToHash(common.Hex2Bytes(string(rune(0)))),
TargetNumber: uint64(1),
TargetHash: common.BytesToHash(common.Hex2Bytes(string(rune(1)))),
},
}
remoteBsc.AsyncSendVotes([]*types.VoteEnvelope{&vote})
time.Sleep(100 * time.Millisecond)
select {
case event := <-votesCh:
if event.Vote.Hash() != vote.Hash() {
t.Errorf("added wrong vote hash: got %v, want %v", event.Vote.Hash(), vote.Hash())
}
case <-time.After(2 * time.Second):
t.Errorf("no NewVotesEvent received within 2 seconds")
}
}