OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / addrmgr / knownaddress.go
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.
4
5 package addrmgr
6
7 import (
8         "time"
9
10         "github.com/btcsuite/btcd/wire"
11 )
12
13 // KnownAddress tracks information about a known network address that is used
14 // to determine how viable an address is.
15 type KnownAddress struct {
16         na          *wire.NetAddress
17         srcAddr     *wire.NetAddress
18         attempts    int
19         lastattempt time.Time
20         lastsuccess time.Time
21         tried       bool
22         refs        int // reference count of new buckets
23 }
24
25 // NetAddress returns the underlying wire.NetAddress associated with the
26 // known address.
27 func (ka *KnownAddress) NetAddress() *wire.NetAddress {
28         return ka.na
29 }
30
31 // LastAttempt returns the last time the known address was attempted.
32 func (ka *KnownAddress) LastAttempt() time.Time {
33         return ka.lastattempt
34 }
35
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 {
40         now := time.Now()
41         lastAttempt := now.Sub(ka.lastattempt)
42
43         if lastAttempt < 0 {
44                 lastAttempt = 0
45         }
46
47         c := 1.0
48
49         // Very recent attempts are less likely to be retried.
50         if lastAttempt < 10*time.Minute {
51                 c *= 0.01
52         }
53
54         // Failed attempts deprioritise.
55         for i := ka.attempts; i > 0; i-- {
56                 c /= 1.5
57         }
58
59         return c
60 }
61
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)) {
72                 return false
73         }
74
75         // From the future?
76         if ka.na.Timestamp.After(time.Now().Add(10 * time.Minute)) {
77                 return true
78         }
79
80         // Over a month old?
81         if ka.na.Timestamp.Before(time.Now().Add(-1 * numMissingDays * time.Hour * 24)) {
82                 return true
83         }
84
85         // Never succeeded?
86         if ka.lastsuccess.IsZero() && ka.attempts >= numRetries {
87                 return true
88         }
89
90         // Hasn't succeeded in too long?
91         if !ka.lastsuccess.After(time.Now().Add(-1*minBadDays*time.Hour*24)) &&
92                 ka.attempts >= maxFailures {
93                 return true
94         }
95
96         return false
97 }