10 "github.com/davecgh/go-spew/spew"
11 cfg "github.com/vapor/config"
12 dbm "github.com/vapor/database/leveldb"
13 "github.com/vapor/errors"
14 conn "github.com/vapor/p2p/connection"
15 "github.com/vapor/p2p/security"
16 "github.com/vapor/p2p/signlib"
24 testCfg = cfg.DefaultConfig()
28 Each peer has one `MConnection` (multiplex connection) instance.
30 __multiplex__ *noun* a system or signal involving simultaneous transmission of
31 several messages along a single channel of communication.
33 Each `MConnection` handles message transmission on multiple abstract communication
34 `Channel`s. Each channel has a globally unique byte id.
35 The byte id and the relative priorities of each `Channel` are configured upon
36 initialization of the connection.
38 There are two methods for sending messages:
39 func (m MConnection) Send(chID byte, msgBytes []byte) bool {}
40 func (m MConnection) TrySend(chID byte, msgBytes []byte}) bool {}
42 `Send(chID, msgBytes)` is a blocking call that waits until `msg` is
43 successfully queued for the channel with the given id byte `chID`, or until the
44 request times out. The message `msg` is serialized using Go-Amino.
46 `TrySend(chID, msgBytes)` is a nonblocking call that returns false if the
47 channel's queue is full.
49 Inbound message bytes are handled with an onReceive callback function.
51 type PeerMessage struct {
57 type TestReactor struct {
61 channels []*conn.ChannelDescriptor
64 msgsReceived map[byte][]PeerMessage
67 func NewTestReactor(channels []*conn.ChannelDescriptor, logMessages bool) *TestReactor {
70 logMessages: logMessages,
71 msgsReceived: make(map[byte][]PeerMessage),
73 tr.BaseReactor = *NewBaseReactor("TestReactor", tr)
78 // GetChannels implements Reactor
79 func (tr *TestReactor) GetChannels() []*conn.ChannelDescriptor {
83 // OnStart implements BaseService
84 func (tr *TestReactor) OnStart() error {
85 tr.BaseReactor.OnStart()
89 // OnStop implements BaseService
90 func (tr *TestReactor) OnStop() {
91 tr.BaseReactor.OnStop()
94 // AddPeer implements Reactor by sending our state to peer.
95 func (tr *TestReactor) AddPeer(peer *Peer) error {
99 // RemovePeer implements Reactor by removing peer from the pool.
100 func (tr *TestReactor) RemovePeer(peer *Peer, reason interface{}) {
103 // Receive implements Reactor by handling 4 types of messages (look below).
104 func (tr *TestReactor) Receive(chID byte, peer *Peer, msgBytes []byte) {
107 defer tr.mtx.Unlock()
108 tr.msgsReceived[chID] = append(tr.msgsReceived[chID], PeerMessage{peer.ID(), msgBytes, tr.msgsCounter})
113 func initSwitchFunc(sw *Switch) *Switch {
114 // Make two reactors of two channels each
115 sw.AddReactor("foo", NewTestReactor([]*conn.ChannelDescriptor{
116 {ID: byte(0x00), Priority: 10},
117 {ID: byte(0x01), Priority: 10},
119 sw.AddReactor("bar", NewTestReactor([]*conn.ChannelDescriptor{
120 {ID: byte(0x02), Priority: 10},
121 {ID: byte(0x03), Priority: 10},
128 func TestFiltersOutItself(t *testing.T) {
129 dirPath, err := ioutil.TempDir(".", "")
133 defer os.RemoveAll(dirPath)
135 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
138 cfg.P2P.ListenAddress = "127.0.1.1:0"
139 swPrivKey, err := signlib.NewPrivKey()
144 s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
148 rmdirPath, err := ioutil.TempDir(".", "")
152 defer os.RemoveAll(rmdirPath)
154 // simulate s1 having a public key and creating a remote peer with the same key
156 rpCfg.DBPath = rmdirPath
157 rp := &remotePeer{PrivKey: s1.nodePrivKey, Config: &rpCfg}
160 if err = s1.DialPeerWithAddress(rp.addr); errors.Root(err) != ErrConnectSelf {
164 //S1 dialing itself ip address
165 addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr())
167 if err := s1.DialPeerWithAddress(addr); errors.Root(err) != ErrConnectSelf {
172 func TestDialBannedPeer(t *testing.T) {
173 dirPath, err := ioutil.TempDir(".", "")
177 defer os.RemoveAll(dirPath)
179 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
182 cfg.P2P.ListenAddress = "127.0.1.1:0"
183 swPrivKey, err := signlib.NewPrivKey()
187 s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
191 rmdirPath, err := ioutil.TempDir(".", "")
195 defer os.RemoveAll(rmdirPath)
198 rpCfg.DBPath = rmdirPath
199 remotePrivKey, err := signlib.NewPrivKey()
204 rp := &remotePeer{PrivKey: remotePrivKey, Config: &rpCfg}
208 if ok := s1.security.IsBanned(rp.addr.IP.String(), security.LevelMsgIllegal, "test"); ok {
212 if err := s1.DialPeerWithAddress(rp.addr); errors.Root(err) != security.ErrConnectBannedPeer {
217 func TestDuplicateOutBoundPeer(t *testing.T) {
218 dirPath, err := ioutil.TempDir(".", "")
222 defer os.RemoveAll(dirPath)
224 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
227 cfg.P2P.ListenAddress = "127.0.1.1:0"
228 swPrivKey, err := signlib.NewPrivKey()
233 s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
237 rmdirPath, err := ioutil.TempDir(".", "")
241 defer os.RemoveAll(rmdirPath)
244 rpCfg.DBPath = rmdirPath
245 remotePrivKey, err := signlib.NewPrivKey()
250 rp := &remotePeer{PrivKey: remotePrivKey, Config: &rpCfg}
254 if err = s1.DialPeerWithAddress(rp.addr); err != nil {
258 if err = s1.DialPeerWithAddress(rp.addr); errors.Root(err) != ErrDuplicatePeer {
263 func TestDuplicateInBoundPeer(t *testing.T) {
264 dirPath, err := ioutil.TempDir(".", "")
268 defer os.RemoveAll(dirPath)
270 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
273 cfg.P2P.ListenAddress = "127.0.1.1:0"
274 swPrivKey, err := signlib.NewPrivKey()
278 s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
283 inpPrivKey, err := signlib.NewPrivKey()
288 inp := &inboundPeer{PrivKey: inpPrivKey, config: &inpCfg}
289 addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr())
296 inp1 := &inboundPeer{PrivKey: inp.PrivKey, config: &inp1Cfg}
299 time.Sleep(1 * time.Second)
300 if _, outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 1 {
301 t.Fatal("TestDuplicateInBoundPeer peer size error want 1, got:", outbound, inbound, dialing, spew.Sdump(s1.peers.lookup))
305 func TestAddInboundPeer(t *testing.T) {
306 dirPath, err := ioutil.TempDir(".", "")
310 defer os.RemoveAll(dirPath)
312 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
315 cfg.P2P.MaxNumPeers = 2
316 cfg.P2P.ListenAddress = "127.0.1.1:0"
317 swPrivKey, err := signlib.NewPrivKey()
321 s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
326 inpPrivKey, err := signlib.NewPrivKey()
331 inp := &inboundPeer{PrivKey: inpPrivKey, config: &inpCfg}
332 addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr())
339 rpPrivKey, err := signlib.NewPrivKey()
343 rp := &remotePeer{PrivKey: rpPrivKey, Config: &rpCfg}
347 if err := s1.DialPeerWithAddress(rp.addr); err != nil {
353 inp2PrivKey, err := signlib.NewPrivKey()
357 inp2 := &inboundPeer{PrivKey: inp2PrivKey, config: &inp2Cfg}
361 time.Sleep(1 * time.Second)
362 if _, outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 2 {
363 t.Fatal("TestAddInboundPeer peer size error want 2 got:", spew.Sdump(s1.peers.lookup))
367 func TestStopPeer(t *testing.T) {
368 dirPath, err := ioutil.TempDir(".", "")
372 defer os.RemoveAll(dirPath)
374 testDB := dbm.NewDB("testdb", "leveldb", dirPath)
377 cfg.P2P.MaxNumPeers = 2
378 cfg.P2P.ListenAddress = "127.0.1.1:0"
379 swPrivKey, err := signlib.NewPrivKey()
383 s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
388 inp2PrivKey, err := signlib.NewPrivKey()
392 inp := &inboundPeer{PrivKey: inp2PrivKey, config: &inpCfg}
393 addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr())
400 rpPrivKey, err := signlib.NewPrivKey()
405 rp := &remotePeer{PrivKey: rpPrivKey, Config: &rpCfg}
409 if err := s1.DialPeerWithAddress(rp.addr); err != nil {
412 time.Sleep(1 * time.Second)
413 if _, outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 2 {
414 t.Fatal("TestStopPeer peer size error want 2,got:", spew.Sdump(s1.peers.lookup))
417 s1.StopPeerGracefully(s1.peers.list[0].Key)
418 if _, outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 1 {
419 t.Fatal("TestStopPeer peer size error,want 1,got:", spew.Sdump(s1.peers.lookup))
422 s1.StopPeerForError(s1.peers.list[0], "stop for test")
423 if _, outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 0 {
424 t.Fatal("TestStopPeer peer size error,want 0, got:", spew.Sdump(s1.peers.lookup))