Browse Source

add public RemoveAddress API

after discussion with @ebuchman (https://github.com/tendermint/go-p2p/pull/10#discussion_r96471729)
pull/456/head
Anton Kaliaev 8 years ago
parent
commit
cf18bf2966
No known key found for this signature in database GPG Key ID: 7B6881D965918214
3 changed files with 33 additions and 4 deletions
  1. +8
    -2
      addrbook.go
  2. +19
    -0
      addrbook_test.go
  3. +6
    -2
      pex_reactor.go

+ 8
- 2
addrbook.go View File

@ -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)
}


+ 19
- 0
addrbook_test.go View File

@ -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())
}

+ 6
- 2
pex_reactor.go View File

@ -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…
Cancel
Save