Browse Source

mvp blacklist alg

pull/4548/head
Callum Michael Waters 5 years ago
parent
commit
7466f35345
2 changed files with 51 additions and 5 deletions
  1. +39
    -5
      p2p/pex/addrbook.go
  2. +12
    -0
      p2p/pex/known_address.go

+ 39
- 5
p2p/pex/addrbook.go View File

@ -22,8 +22,9 @@ import (
)
const (
bucketTypeNew = 0x01
bucketTypeOld = 0x02
bucketTypeNew = 0x01
bucketTypeOld = 0x02
defaultBanTime = 24 * time.Hour
)
// AddrBook is an address book used for tracking peers
@ -87,6 +88,7 @@ type addrBook struct {
ourAddrs map[string]struct{}
privateIDs map[p2p.ID]struct{}
addrLookup map[p2p.ID]*knownAddress // new & old
badPeers map[p2p.ID]*knownAddress // blacklisted peers
bucketsOld []map[string]*knownAddress
bucketsNew []map[string]*knownAddress
nOld int
@ -108,6 +110,7 @@ func NewAddrBook(filePath string, routabilityStrict bool) AddrBook {
ourAddrs: make(map[string]struct{}),
privateIDs: make(map[p2p.ID]struct{}),
addrLookup: make(map[p2p.ID]*knownAddress),
badPeers: make(map[p2p.ID]*knownAddress),
filePath: filePath,
routabilityStrict: routabilityStrict,
}
@ -233,6 +236,7 @@ func (a *addrBook) HasAddress(addr *p2p.NetAddress) bool {
// NeedMoreAddrs implements AddrBook - returns true if there are not have enough addresses in the book.
func (a *addrBook) NeedMoreAddrs() bool {
a.ReinstateBadPeers()
return a.Size() < needAddressThreshold
}
@ -324,10 +328,24 @@ func (a *addrBook) MarkAttempt(addr *p2p.NetAddress) {
ka.markAttempt()
}
// MarkBad implements AddrBook. Currently it just ejects the address.
// TODO: black list for some amount of time
// MarkBad implements AddrBook. Kicks address out from book, places
// the address in the badPeers pool.
func (a *addrBook) MarkBad(addr *p2p.NetAddress) {
a.RemoveAddress(addr)
if a.addBadPeer(addr) {
a.RemoveAddress(addr)
}
}
func (a *addrBook) ReinstateBadPeers() {
for _, ka := range a.badPeers {
if !ka.isBanned(defaultBanTime) {
a.mtx.Lock()
bucket := a.calcNewBucket(ka.Addr, ka.Src)
a.addToNewBucket(ka, bucket)
delete(a.badPeers, ka.ID())
a.mtx.Unlock()
}
}
}
// GetSelection implements AddrBook.
@ -725,6 +743,22 @@ func (a *addrBook) moveToOld(ka *knownAddress) {
}
}
func (a *addrBook) addBadPeer(addr *p2p.NetAddress) bool {
a.mtx.Lock()
defer a.mtx.Unlock()
// check it exists in addrbook
ka := a.addrLookup[addr.ID]
// check address is not already there
if _, alreadyBadPeer := a.badPeers[addr.ID]; ka != nil && !alreadyBadPeer {
// add to bad peer list
ka.ban()
a.badPeers[addr.ID] = ka
return true
}
return false
}
//---------------------------------------------------------------------
// calculate bucket placements


+ 12
- 0
p2p/pex/known_address.go View File

@ -16,6 +16,7 @@ type knownAddress struct {
BucketType byte `json:"bucket_type"`
LastAttempt time.Time `json:"last_attempt"`
LastSuccess time.Time `json:"last_success"`
LastBanTime time.Time `json:"last_ban_time"`
}
func newKnownAddress(addr *p2p.NetAddress, src *p2p.NetAddress) *knownAddress {
@ -54,6 +55,17 @@ func (ka *knownAddress) markGood() {
ka.LastSuccess = now
}
func (ka *knownAddress) ban() {
ka.LastBanTime = time.Now()
}
func (ka *knownAddress) isBanned(banTime time.Duration) bool {
if ka.LastBanTime.Add(banTime).Before(time.Now()) {
return false
}
return true
}
func (ka *knownAddress) addBucketRef(bucketIdx int) int {
for _, bucket := range ka.Buckets {
if bucket == bucketIdx {


Loading…
Cancel
Save