add public RemoveAddress API
after discussion with @ebuchman (https://github.com/tendermint/go-p2p/pull/10#discussion_r96471729)
This commit is contained in:
parent
324293f4cb
commit
cf18bf2966
10
addrbook.go
10
addrbook.go
|
@ -252,15 +252,21 @@ func (a *AddrBook) MarkAttempt(addr *NetAddress) {
|
|||
ka.markAttempt()
|
||||
}
|
||||
|
||||
// MarkBad currently just ejects the address. In the future, consider
|
||||
// blacklisting.
|
||||
func (a *AddrBook) MarkBad(addr *NetAddress) {
|
||||
a.RemoveAddress(addr)
|
||||
}
|
||||
|
||||
// RemoveAddress removes the address from the book.
|
||||
func (a *AddrBook) RemoveAddress(addr *NetAddress) {
|
||||
a.mtx.Lock()
|
||||
defer a.mtx.Unlock()
|
||||
ka := a.addrLookup[addr.String()]
|
||||
if ka == nil {
|
||||
return
|
||||
}
|
||||
// We currently just eject the address.
|
||||
// In the future, consider blacklisting.
|
||||
log.Info("Remove address from book", "addr", addr)
|
||||
a.removeFromAllBuckets(ka)
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,9 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func createTempFileName(prefix string) string {
|
||||
f, err := ioutil.TempFile("", prefix)
|
||||
if err != nil {
|
||||
|
@ -148,3 +151,19 @@ func randIPv4Address(t *testing.T) *NetAddress {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddrBookRemoveAddress(t *testing.T) {
|
||||
fname := createTempFileName("addrbook_test")
|
||||
book := NewAddrBook(fname, true)
|
||||
|
||||
addr := randIPv4Address()
|
||||
book.AddAddress(addr, addr)
|
||||
assert.Equal(t, 1, book.Size())
|
||||
|
||||
book.RemoveAddress(addr)
|
||||
assert.Equal(t, 0, book.Size())
|
||||
|
||||
nonExistingAddr := randIPv4Address()
|
||||
book.RemoveAddress(nonExistingAddr)
|
||||
assert.Equal(t, 0, book.Size())
|
||||
}
|
||||
|
|
|
@ -111,10 +111,14 @@ 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).
|
||||
func (r *PEXReactor) RemovePeer(p *Peer, reason interface{}) {
|
||||
addr := NewNetAddressString(p.ListenAddr)
|
||||
// addr will be ejected from the book
|
||||
r.book.MarkBad(addr)
|
||||
r.book.RemoveAddress(addr)
|
||||
}
|
||||
|
||||
// Receive implements Reactor by handling incoming PEX messages.
|
||||
|
|
Loading…
Reference in New Issue