implement RemovePeer for PEXReactor

This commit is contained in:
Anton Kalyaev 2017-01-11 19:23:31 +04:00 committed by Anton Kaliaev
parent 3af7c67757
commit 37d5a2cf3e
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
2 changed files with 55 additions and 2 deletions

View File

@ -73,13 +73,16 @@ func (r *PEXReactor) AddPeer(p *Peer) {
r.RequestPEX(p)
}
} else { // For inbound connections, the peer is its own source
r.book.AddAddress(netAddr, netAddr)
addr := NewNetAddressString(p.ListenAddr)
r.book.AddAddress(addr, addr)
}
}
// RemovePeer implements Reactor
func (r *PEXReactor) RemovePeer(p *Peer, reason interface{}) {
// TODO
addr := NewNetAddressString(p.ListenAddr)
// addr will be ejected from the book
r.book.MarkBad(addr)
}
// Receive implements Reactor by handling incoming PEX messages.

50
pex_reactor_tests.go Normal file
View File

@ -0,0 +1,50 @@
package p2p
import (
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
. "github.com/tendermint/go-common"
)
func TestBasic(t *testing.T) {
book := NewAddrBook(createTempFileName("addrbook"), true)
r := NewPEXReactor(book)
assert.NotNil(t, r)
assert.NotEmpty(t, r.GetChannels())
}
func TestAddRemovePeer(t *testing.T) {
book := NewAddrBook(createTempFileName("addrbook"), true)
r := NewPEXReactor(book)
size := book.Size()
peer := createRandomPeer(false)
r.AddPeer(peer)
assert.Equal(t, size+1, book.Size())
r.RemovePeer(peer, "peer not available")
assert.Equal(t, size, book.Size())
outboundPeer := createRandomPeer(true)
r.AddPeer(outboundPeer)
assert.Equal(t, size, book.Size(), "size must not change")
r.RemovePeer(outboundPeer, "peer not available")
assert.Equal(t, size, book.Size(), "size must not change")
}
func createRandomPeer(outbound bool) *Peer {
return &Peer{
Key: RandStr(12),
NodeInfo: &NodeInfo{
RemoteAddr: Fmt("%v.%v.%v.%v:46656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256),
ListenAddr: Fmt("%v.%v.%v.%v:46656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256),
},
outbound: outbound,
}
}