1 // Copyright (c) 2013-2014 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
10 "github.com/btcsuite/btcd/wire"
13 // KnownAddress tracks information about a known network address that is used
14 // to determine how viable an address is.
15 type KnownAddress struct {
17 srcAddr *wire.NetAddress
22 refs int // reference count of new buckets
25 // NetAddress returns the underlying wire.NetAddress associated with the
27 func (ka *KnownAddress) NetAddress() *wire.NetAddress {
31 // LastAttempt returns the last time the known address was attempted.
32 func (ka *KnownAddress) LastAttempt() time.Time {
36 // chance returns the selection probability for a known address. The priority
37 // depends upon how recently the address has been seen, how recently it was last
38 // attempted and how often attempts to connect to it have failed.
39 func (ka *KnownAddress) chance() float64 {
41 lastAttempt := now.Sub(ka.lastattempt)
49 // Very recent attempts are less likely to be retried.
50 if lastAttempt < 10*time.Minute {
54 // Failed attempts deprioritise.
55 for i := ka.attempts; i > 0; i-- {
62 // isBad returns true if the address in question has not been tried in the last
63 // minute and meets one of the following criteria:
64 // 1) It claims to be from the future
65 // 2) It hasn't been seen in over a month
66 // 3) It has failed at least three times and never succeeded
67 // 4) It has failed ten times in the last week
68 // All addresses that meet these criteria are assumed to be worthless and not
69 // worth keeping hold of.
70 func (ka *KnownAddress) isBad() bool {
71 if ka.lastattempt.After(time.Now().Add(-1 * time.Minute)) {
76 if ka.na.Timestamp.After(time.Now().Add(10 * time.Minute)) {
81 if ka.na.Timestamp.Before(time.Now().Add(-1 * numMissingDays * time.Hour * 24)) {
86 if ka.lastsuccess.IsZero() && ka.attempts >= numRetries {
90 // Hasn't succeeded in too long?
91 if !ka.lastsuccess.After(time.Now().Add(-1*minBadDays*time.Hour*24)) &&
92 ka.attempts >= maxFailures {