2014-07-11 02:03:34 +02:00
|
|
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package addrmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2014-07-11 02:03:34 +02:00
|
|
|
)
|
|
|
|
|
2015-01-28 16:44:56 +01:00
|
|
|
// KnownAddress tracks information about a known network address that is used
|
2014-07-11 02:03:34 +02:00
|
|
|
// to determine how viable an address is.
|
2015-01-28 16:44:56 +01:00
|
|
|
type KnownAddress struct {
|
2015-02-05 22:16:39 +01:00
|
|
|
na *wire.NetAddress
|
|
|
|
srcAddr *wire.NetAddress
|
2014-07-11 02:03:34 +02:00
|
|
|
attempts int
|
|
|
|
lastattempt time.Time
|
|
|
|
lastsuccess time.Time
|
|
|
|
tried bool
|
|
|
|
refs int // reference count of new buckets
|
|
|
|
}
|
|
|
|
|
2015-02-05 22:16:39 +01:00
|
|
|
// NetAddress returns the underlying wire.NetAddress associated with the
|
2014-07-11 02:03:34 +02:00
|
|
|
// known address.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (ka *KnownAddress) NetAddress() *wire.NetAddress {
|
2014-07-11 02:03:34 +02:00
|
|
|
return ka.na
|
|
|
|
}
|
|
|
|
|
|
|
|
// LastAttempt returns the last time the known address was attempted.
|
2015-01-28 16:44:56 +01:00
|
|
|
func (ka *KnownAddress) LastAttempt() time.Time {
|
2014-07-11 02:03:34 +02:00
|
|
|
return ka.lastattempt
|
|
|
|
}
|
|
|
|
|
|
|
|
// chance returns the selection probability for a known address. The priority
|
|
|
|
// depends upon how recently the address has been seen, how recently it was last
|
|
|
|
// attempted and how often attempts to connect to it have failed.
|
2015-01-28 16:44:56 +01:00
|
|
|
func (ka *KnownAddress) chance() float64 {
|
2014-07-11 02:03:34 +02:00
|
|
|
now := time.Now()
|
|
|
|
lastSeen := now.Sub(ka.na.Timestamp)
|
|
|
|
lastAttempt := now.Sub(ka.lastattempt)
|
|
|
|
|
|
|
|
if lastSeen < 0 {
|
|
|
|
lastSeen = 0
|
|
|
|
}
|
|
|
|
if lastAttempt < 0 {
|
|
|
|
lastAttempt = 0
|
|
|
|
}
|
|
|
|
|
2015-04-08 21:52:36 +02:00
|
|
|
c := 1.0
|
2014-07-11 02:03:34 +02:00
|
|
|
|
|
|
|
// Very recent attempts are less likely to be retried.
|
2015-03-29 19:43:55 +02:00
|
|
|
if lastAttempt < 10*time.Minute {
|
2014-07-11 02:03:34 +02:00
|
|
|
c *= 0.01
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failed attempts deprioritise.
|
2015-03-29 19:43:55 +02:00
|
|
|
for i := ka.attempts; i > 0; i-- {
|
2014-07-11 02:03:34 +02:00
|
|
|
c /= 1.5
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// isBad returns true if the address in question has not been tried in the last
|
|
|
|
// minute and meets one of the following criteria:
|
|
|
|
// 1) It claims to be from the future
|
|
|
|
// 2) It hasn't been seen in over a month
|
|
|
|
// 3) It has failed at least three times and never succeeded
|
|
|
|
// 4) It has failed ten times in the last week
|
|
|
|
// All addresses that meet these criteria are assumed to be worthless and not
|
|
|
|
// worth keeping hold of.
|
2015-01-28 16:44:56 +01:00
|
|
|
func (ka *KnownAddress) isBad() bool {
|
2014-07-11 02:03:34 +02:00
|
|
|
if ka.lastattempt.After(time.Now().Add(-1 * time.Minute)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// From the future?
|
|
|
|
if ka.na.Timestamp.After(time.Now().Add(10 * time.Minute)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Over a month old?
|
2015-03-29 19:43:55 +02:00
|
|
|
if ka.na.Timestamp.Before(time.Now().Add(-1 * numMissingDays * time.Hour * 24)) {
|
2014-07-11 02:03:34 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Never succeeded?
|
|
|
|
if ka.lastsuccess.IsZero() && ka.attempts >= numRetries {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hasn't succeeded in too long?
|
|
|
|
if !ka.lastsuccess.After(time.Now().Add(-1*minBadDays*time.Hour*24)) &&
|
|
|
|
ka.attempts >= maxFailures {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|