Browse Source

revert 2710873

pull/456/head
Anton Kaliaev 8 years ago
parent
commit
17ec70fc09
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 27 additions and 27 deletions
  1. +3
    -13
      pex_reactor.go
  2. +24
    -14
      pex_reactor_test.go

+ 3
- 13
pex_reactor.go View File

@ -109,20 +109,10 @@ func (r *PEXReactor) AddPeer(p *Peer) {
} }
} }
// RemovePeer implements Reactor by removing peer from the address book.
//
// The peer will be proposed to us by other peers (PexAddrsMessage) or himself
// and we will add him again upon successful connection. Note that other peers
// will remove him too. The peer will need to send first requests to others by
// himself (he will have an addrbook or the seeds).
// RemovePeer implements Reactor.
func (r *PEXReactor) RemovePeer(p *Peer, reason interface{}) { func (r *PEXReactor) RemovePeer(p *Peer, reason interface{}) {
addr, err := NewNetAddressString(p.ListenAddr)
if err != nil {
// this should never happen
log.Error("Error in AddPeer: invalid peer address", "addr", p.ListenAddr, "error", err)
return
}
r.book.RemoveAddress(addr)
// If we aren't keeping track of local temp data for each peer here, then we
// don't have to do anything.
} }
// Receive implements Reactor by handling incoming PEX messages. // Receive implements Reactor by handling incoming PEX messages.


+ 24
- 14
pex_reactor_test.go View File

@ -14,20 +14,24 @@ import (
) )
func TestPEXReactorBasic(t *testing.T) { func TestPEXReactorBasic(t *testing.T) {
assert, require := assert.New(t), require.New(t)
dir, err := ioutil.TempDir("", "pex_reactor") dir, err := ioutil.TempDir("", "pex_reactor")
require.Nil(t, err)
require.Nil(err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
book := NewAddrBook(dir+"addrbook.json", true) book := NewAddrBook(dir+"addrbook.json", true)
r := NewPEXReactor(book) r := NewPEXReactor(book)
assert.NotNil(t, r)
assert.NotEmpty(t, r.GetChannels())
assert.NotNil(r)
assert.NotEmpty(r.GetChannels())
} }
func TestPEXReactorAddRemovePeer(t *testing.T) { func TestPEXReactorAddRemovePeer(t *testing.T) {
assert, require := assert.New(t), require.New(t)
dir, err := ioutil.TempDir("", "pex_reactor") dir, err := ioutil.TempDir("", "pex_reactor")
require.Nil(t, err)
require.Nil(err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
book := NewAddrBook(dir+"addrbook.json", true) book := NewAddrBook(dir+"addrbook.json", true)
@ -37,26 +41,28 @@ func TestPEXReactorAddRemovePeer(t *testing.T) {
peer := createRandomPeer(false) peer := createRandomPeer(false)
r.AddPeer(peer) r.AddPeer(peer)
assert.Equal(t, size+1, book.Size())
assert.Equal(size+1, book.Size())
r.RemovePeer(peer, "peer not available") r.RemovePeer(peer, "peer not available")
assert.Equal(t, size, book.Size())
assert.Equal(size+1, book.Size())
outboundPeer := createRandomPeer(true) outboundPeer := createRandomPeer(true)
r.AddPeer(outboundPeer) r.AddPeer(outboundPeer)
assert.Equal(t, size, book.Size(), "size must not change")
assert.Equal(size+1, book.Size(), "outbound peers should not be added to the address book")
r.RemovePeer(outboundPeer, "peer not available") r.RemovePeer(outboundPeer, "peer not available")
assert.Equal(t, size, book.Size(), "size must not change")
assert.Equal(size+1, book.Size())
} }
func TestPEXReactorRunning(t *testing.T) { func TestPEXReactorRunning(t *testing.T) {
require := require.New(t)
N := 3 N := 3
switches := make([]*Switch, N) switches := make([]*Switch, N)
dir, err := ioutil.TempDir("", "pex_reactor") dir, err := ioutil.TempDir("", "pex_reactor")
require.Nil(t, err)
require.Nil(err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
book := NewAddrBook(dir+"addrbook.json", false) book := NewAddrBook(dir+"addrbook.json", false)
@ -80,7 +86,7 @@ func TestPEXReactorRunning(t *testing.T) {
// start switches // start switches
for _, s := range switches { for _, s := range switches {
_, err := s.Start() // start switch and reactors _, err := s.Start() // start switch and reactors
require.Nil(t, err)
require.Nil(err)
} }
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
@ -100,8 +106,10 @@ func TestPEXReactorRunning(t *testing.T) {
} }
func TestPEXReactorReceive(t *testing.T) { func TestPEXReactorReceive(t *testing.T) {
assert, require := assert.New(t), require.New(t)
dir, err := ioutil.TempDir("", "pex_reactor") dir, err := ioutil.TempDir("", "pex_reactor")
require.Nil(t, err)
require.Nil(err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
book := NewAddrBook(dir+"addrbook.json", true) book := NewAddrBook(dir+"addrbook.json", true)
@ -114,15 +122,17 @@ func TestPEXReactorReceive(t *testing.T) {
addrs := []*NetAddress{netAddr} addrs := []*NetAddress{netAddr}
msg := wire.BinaryBytes(struct{ PexMessage }{&pexAddrsMessage{Addrs: addrs}}) msg := wire.BinaryBytes(struct{ PexMessage }{&pexAddrsMessage{Addrs: addrs}})
r.Receive(PexChannel, peer, msg) r.Receive(PexChannel, peer, msg)
assert.Equal(t, size+1, book.Size())
assert.Equal(size+1, book.Size())
msg = wire.BinaryBytes(struct{ PexMessage }{&pexRequestMessage{}}) msg = wire.BinaryBytes(struct{ PexMessage }{&pexRequestMessage{}})
r.Receive(PexChannel, peer, msg) r.Receive(PexChannel, peer, msg)
} }
func TestPEXReactorAbuseFromPeer(t *testing.T) { func TestPEXReactorAbuseFromPeer(t *testing.T) {
assert, require := assert.New(t), require.New(t)
dir, err := ioutil.TempDir("", "pex_reactor") dir, err := ioutil.TempDir("", "pex_reactor")
require.Nil(t, err)
require.Nil(err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
book := NewAddrBook(dir+"addrbook.json", true) book := NewAddrBook(dir+"addrbook.json", true)
@ -136,7 +146,7 @@ func TestPEXReactorAbuseFromPeer(t *testing.T) {
r.Receive(PexChannel, peer, msg) r.Receive(PexChannel, peer, msg)
} }
assert.True(t, r.ReachedMaxMsgCountForPeer(peer.ListenAddr))
assert.True(r.ReachedMaxMsgCountForPeer(peer.ListenAddr))
} }
func createRandomPeer(outbound bool) *Peer { func createRandomPeer(outbound bool) *Peer {


Loading…
Cancel
Save