You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

814 lines
20 KiB

9 years ago
9 years ago
9 years ago
  1. // Modified for Tendermint
  2. // Originally Copyright (c) 2013-2014 Conformal Systems LLC.
  3. // https://github.com/conformal/btcd/blob/master/LICENSE
  4. package p2p
  5. import (
  6. "encoding/binary"
  7. "encoding/json"
  8. "math"
  9. "math/rand"
  10. "net"
  11. "os"
  12. "sync"
  13. "time"
  14. . "github.com/tendermint/go-common"
  15. )
  16. const (
  17. // addresses under which the address manager will claim to need more addresses.
  18. needAddressThreshold = 1000
  19. // interval used to dump the address cache to disk for future use.
  20. dumpAddressInterval = time.Minute * 2
  21. // max addresses in each old address bucket.
  22. oldBucketSize = 64
  23. // buckets we split old addresses over.
  24. oldBucketCount = 64
  25. // max addresses in each new address bucket.
  26. newBucketSize = 64
  27. // buckets that we spread new addresses over.
  28. newBucketCount = 256
  29. // old buckets over which an address group will be spread.
  30. oldBucketsPerGroup = 4
  31. // new buckets over which an source address group will be spread.
  32. newBucketsPerGroup = 32
  33. // buckets a frequently seen new address may end up in.
  34. maxNewBucketsPerAddress = 4
  35. // days before which we assume an address has vanished
  36. // if we have not seen it announced in that long.
  37. numMissingDays = 30
  38. // tries without a single success before we assume an address is bad.
  39. numRetries = 3
  40. // max failures we will accept without a success before considering an address bad.
  41. maxFailures = 10
  42. // days since the last success before we will consider evicting an address.
  43. minBadDays = 7
  44. // % of total addresses known returned by GetSelection.
  45. getSelectionPercent = 23
  46. // min addresses that must be returned by GetSelection. Useful for bootstrapping.
  47. minGetSelection = 32
  48. // max addresses returned by GetSelection
  49. // NOTE: this must match "maxPexMessageSize"
  50. maxGetSelection = 250
  51. // current version of the on-disk format.
  52. serializationVersion = 1
  53. )
  54. /* AddrBook - concurrency safe peer address manager */
  55. type AddrBook struct {
  56. QuitService
  57. mtx sync.Mutex
  58. filePath string
  59. rand *rand.Rand
  60. key string
  61. ourAddrs map[string]*NetAddress
  62. addrLookup map[string]*knownAddress // new & old
  63. addrNew []map[string]*knownAddress
  64. addrOld []map[string]*knownAddress
  65. wg sync.WaitGroup
  66. nOld int
  67. nNew int
  68. }
  69. const (
  70. bucketTypeNew = 0x01
  71. bucketTypeOld = 0x02
  72. )
  73. // Use Start to begin processing asynchronous address updates.
  74. func NewAddrBook(filePath string) *AddrBook {
  75. am := &AddrBook{
  76. rand: rand.New(rand.NewSource(time.Now().UnixNano())),
  77. ourAddrs: make(map[string]*NetAddress),
  78. addrLookup: make(map[string]*knownAddress),
  79. filePath: filePath,
  80. }
  81. am.init()
  82. am.QuitService = *NewQuitService(log, "AddrBook", am)
  83. return am
  84. }
  85. // When modifying this, don't forget to update loadFromFile()
  86. func (a *AddrBook) init() {
  87. a.key = CRandHex(24) // 24/2 * 8 = 96 bits
  88. // New addr buckets
  89. a.addrNew = make([]map[string]*knownAddress, newBucketCount)
  90. for i := range a.addrNew {
  91. a.addrNew[i] = make(map[string]*knownAddress)
  92. }
  93. // Old addr buckets
  94. a.addrOld = make([]map[string]*knownAddress, oldBucketCount)
  95. for i := range a.addrOld {
  96. a.addrOld[i] = make(map[string]*knownAddress)
  97. }
  98. }
  99. func (a *AddrBook) OnStart() error {
  100. a.QuitService.OnStart()
  101. a.loadFromFile(a.filePath)
  102. a.wg.Add(1)
  103. go a.saveRoutine()
  104. return nil
  105. }
  106. func (a *AddrBook) OnStop() {
  107. a.QuitService.OnStop()
  108. a.wg.Wait()
  109. }
  110. func (a *AddrBook) AddOurAddress(addr *NetAddress) {
  111. a.mtx.Lock()
  112. defer a.mtx.Unlock()
  113. log.Info("Add our address to book", "addr", addr)
  114. a.ourAddrs[addr.String()] = addr
  115. }
  116. func (a *AddrBook) OurAddresses() []*NetAddress {
  117. addrs := []*NetAddress{}
  118. for _, addr := range a.ourAddrs {
  119. addrs = append(addrs, addr)
  120. }
  121. return addrs
  122. }
  123. func (a *AddrBook) AddAddress(addr *NetAddress, src *NetAddress) {
  124. a.mtx.Lock()
  125. defer a.mtx.Unlock()
  126. log.Info("Add address to book", "addr", addr, "src", src)
  127. a.addAddress(addr, src)
  128. }
  129. func (a *AddrBook) NeedMoreAddrs() bool {
  130. return a.Size() < needAddressThreshold
  131. }
  132. func (a *AddrBook) Size() int {
  133. a.mtx.Lock()
  134. defer a.mtx.Unlock()
  135. return a.size()
  136. }
  137. func (a *AddrBook) size() int {
  138. return a.nNew + a.nOld
  139. }
  140. // Pick an address to connect to with new/old bias.
  141. func (a *AddrBook) PickAddress(newBias int) *NetAddress {
  142. a.mtx.Lock()
  143. defer a.mtx.Unlock()
  144. if a.size() == 0 {
  145. return nil
  146. }
  147. if newBias > 100 {
  148. newBias = 100
  149. }
  150. if newBias < 0 {
  151. newBias = 0
  152. }
  153. // Bias between new and old addresses.
  154. oldCorrelation := math.Sqrt(float64(a.nOld)) * (100.0 - float64(newBias))
  155. newCorrelation := math.Sqrt(float64(a.nNew)) * float64(newBias)
  156. if (newCorrelation+oldCorrelation)*a.rand.Float64() < oldCorrelation {
  157. // pick random Old bucket.
  158. var bucket map[string]*knownAddress = nil
  159. for len(bucket) == 0 {
  160. bucket = a.addrOld[a.rand.Intn(len(a.addrOld))]
  161. }
  162. // pick a random ka from bucket.
  163. randIndex := a.rand.Intn(len(bucket))
  164. for _, ka := range bucket {
  165. if randIndex == 0 {
  166. return ka.Addr
  167. }
  168. randIndex--
  169. }
  170. PanicSanity("Should not happen")
  171. } else {
  172. // pick random New bucket.
  173. var bucket map[string]*knownAddress = nil
  174. for len(bucket) == 0 {
  175. bucket = a.addrNew[a.rand.Intn(len(a.addrNew))]
  176. }
  177. // pick a random ka from bucket.
  178. randIndex := a.rand.Intn(len(bucket))
  179. for _, ka := range bucket {
  180. if randIndex == 0 {
  181. return ka.Addr
  182. }
  183. randIndex--
  184. }
  185. PanicSanity("Should not happen")
  186. }
  187. return nil
  188. }
  189. func (a *AddrBook) MarkGood(addr *NetAddress) {
  190. a.mtx.Lock()
  191. defer a.mtx.Unlock()
  192. ka := a.addrLookup[addr.String()]
  193. if ka == nil {
  194. return
  195. }
  196. ka.markGood()
  197. if ka.isNew() {
  198. a.moveToOld(ka)
  199. }
  200. }
  201. func (a *AddrBook) MarkAttempt(addr *NetAddress) {
  202. a.mtx.Lock()
  203. defer a.mtx.Unlock()
  204. ka := a.addrLookup[addr.String()]
  205. if ka == nil {
  206. return
  207. }
  208. ka.markAttempt()
  209. }
  210. func (a *AddrBook) MarkBad(addr *NetAddress) {
  211. a.mtx.Lock()
  212. defer a.mtx.Unlock()
  213. ka := a.addrLookup[addr.String()]
  214. if ka == nil {
  215. return
  216. }
  217. // We currently just eject the address.
  218. // In the future, consider blacklisting.
  219. a.removeFromAllBuckets(ka)
  220. }
  221. /* Peer exchange */
  222. // GetSelection randomly selects some addresses (old & new). Suitable for peer-exchange protocols.
  223. func (a *AddrBook) GetSelection() []*NetAddress {
  224. a.mtx.Lock()
  225. defer a.mtx.Unlock()
  226. if a.size() == 0 {
  227. return nil
  228. }
  229. allAddr := make([]*NetAddress, a.size())
  230. i := 0
  231. for _, v := range a.addrLookup {
  232. allAddr[i] = v.Addr
  233. i++
  234. }
  235. numAddresses := MaxInt(
  236. MinInt(minGetSelection, len(allAddr)),
  237. len(allAddr)*getSelectionPercent/100)
  238. numAddresses = MinInt(maxGetSelection, numAddresses)
  239. // Fisher-Yates shuffle the array. We only need to do the first
  240. // `numAddresses' since we are throwing the rest.
  241. for i := 0; i < numAddresses; i++ {
  242. // pick a number between current index and the end
  243. j := rand.Intn(len(allAddr)-i) + i
  244. allAddr[i], allAddr[j] = allAddr[j], allAddr[i]
  245. }
  246. // slice off the limit we are willing to share.
  247. return allAddr[:numAddresses]
  248. }
  249. /* Loading & Saving */
  250. type addrBookJSON struct {
  251. Key string
  252. Addrs []*knownAddress
  253. }
  254. func (a *AddrBook) saveToFile(filePath string) {
  255. // Compile Addrs
  256. addrs := []*knownAddress{}
  257. for _, ka := range a.addrLookup {
  258. addrs = append(addrs, ka)
  259. }
  260. aJSON := &addrBookJSON{
  261. Key: a.key,
  262. Addrs: addrs,
  263. }
  264. jsonBytes, err := json.MarshalIndent(aJSON, "", "\t")
  265. if err != nil {
  266. log.Error("Failed to save AddrBook to file", "err", err)
  267. return
  268. }
  269. err = WriteFileAtomic(filePath, jsonBytes, 0644)
  270. if err != nil {
  271. log.Error("Failed to save AddrBook to file", "file", filePath, "error", err)
  272. }
  273. }
  274. // Returns false if file does not exist.
  275. // Panics if file is corrupt.
  276. func (a *AddrBook) loadFromFile(filePath string) bool {
  277. // If doesn't exist, do nothing.
  278. _, err := os.Stat(filePath)
  279. if os.IsNotExist(err) {
  280. return false
  281. }
  282. // Load addrBookJSON{}
  283. r, err := os.Open(filePath)
  284. if err != nil {
  285. PanicCrisis(Fmt("Error opening file %s: %v", filePath, err))
  286. }
  287. defer r.Close()
  288. aJSON := &addrBookJSON{}
  289. dec := json.NewDecoder(r)
  290. err = dec.Decode(aJSON)
  291. if err != nil {
  292. PanicCrisis(Fmt("Error reading file %s: %v", filePath, err))
  293. }
  294. // Restore all the fields...
  295. // Restore the key
  296. a.key = aJSON.Key
  297. // Restore .addrNew & .addrOld
  298. for _, ka := range aJSON.Addrs {
  299. for _, bucketIndex := range ka.Buckets {
  300. bucket := a.getBucket(ka.BucketType, bucketIndex)
  301. bucket[ka.Addr.String()] = ka
  302. }
  303. a.addrLookup[ka.Addr.String()] = ka
  304. if ka.BucketType == bucketTypeNew {
  305. a.nNew++
  306. } else {
  307. a.nOld++
  308. }
  309. }
  310. return true
  311. }
  312. /* Private methods */
  313. func (a *AddrBook) saveRoutine() {
  314. dumpAddressTicker := time.NewTicker(dumpAddressInterval)
  315. out:
  316. for {
  317. select {
  318. case <-dumpAddressTicker.C:
  319. log.Info("Saving AddrBook to file", "size", a.Size())
  320. a.saveToFile(a.filePath)
  321. case <-a.Quit:
  322. break out
  323. }
  324. }
  325. dumpAddressTicker.Stop()
  326. a.saveToFile(a.filePath)
  327. a.wg.Done()
  328. log.Notice("Address handler done")
  329. }
  330. func (a *AddrBook) getBucket(bucketType byte, bucketIdx int) map[string]*knownAddress {
  331. switch bucketType {
  332. case bucketTypeNew:
  333. return a.addrNew[bucketIdx]
  334. case bucketTypeOld:
  335. return a.addrOld[bucketIdx]
  336. default:
  337. PanicSanity("Should not happen")
  338. return nil
  339. }
  340. }
  341. // Adds ka to new bucket. Returns false if it couldn't do it cuz buckets full.
  342. // NOTE: currently it always returns true.
  343. func (a *AddrBook) addToNewBucket(ka *knownAddress, bucketIdx int) bool {
  344. // Sanity check
  345. if ka.isOld() {
  346. log.Warn(Fmt("Cannot add address already in old bucket to a new bucket: %v", ka))
  347. return false
  348. }
  349. addrStr := ka.Addr.String()
  350. bucket := a.getBucket(bucketTypeNew, bucketIdx)
  351. // Already exists?
  352. if _, ok := bucket[addrStr]; ok {
  353. return true
  354. }
  355. // Enforce max addresses.
  356. if len(bucket) > newBucketSize {
  357. log.Notice("new bucket is full, expiring old ")
  358. a.expireNew(bucketIdx)
  359. }
  360. // Add to bucket.
  361. bucket[addrStr] = ka
  362. if ka.addBucketRef(bucketIdx) == 1 {
  363. a.nNew++
  364. }
  365. // Ensure in addrLookup
  366. a.addrLookup[addrStr] = ka
  367. return true
  368. }
  369. // Adds ka to old bucket. Returns false if it couldn't do it cuz buckets full.
  370. func (a *AddrBook) addToOldBucket(ka *knownAddress, bucketIdx int) bool {
  371. // Sanity check
  372. if ka.isNew() {
  373. log.Warn(Fmt("Cannot add new address to old bucket: %v", ka))
  374. return false
  375. }
  376. if len(ka.Buckets) != 0 {
  377. log.Warn(Fmt("Cannot add already old address to another old bucket: %v", ka))
  378. return false
  379. }
  380. addrStr := ka.Addr.String()
  381. bucket := a.getBucket(bucketTypeNew, bucketIdx)
  382. // Already exists?
  383. if _, ok := bucket[addrStr]; ok {
  384. return true
  385. }
  386. // Enforce max addresses.
  387. if len(bucket) > oldBucketSize {
  388. return false
  389. }
  390. // Add to bucket.
  391. bucket[addrStr] = ka
  392. if ka.addBucketRef(bucketIdx) == 1 {
  393. a.nOld++
  394. }
  395. // Ensure in addrLookup
  396. a.addrLookup[addrStr] = ka
  397. return true
  398. }
  399. func (a *AddrBook) removeFromBucket(ka *knownAddress, bucketType byte, bucketIdx int) {
  400. if ka.BucketType != bucketType {
  401. log.Warn(Fmt("Bucket type mismatch: %v", ka))
  402. return
  403. }
  404. bucket := a.getBucket(bucketType, bucketIdx)
  405. delete(bucket, ka.Addr.String())
  406. if ka.removeBucketRef(bucketIdx) == 0 {
  407. if bucketType == bucketTypeNew {
  408. a.nNew--
  409. } else {
  410. a.nOld--
  411. }
  412. delete(a.addrLookup, ka.Addr.String())
  413. }
  414. }
  415. func (a *AddrBook) removeFromAllBuckets(ka *knownAddress) {
  416. for _, bucketIdx := range ka.Buckets {
  417. bucket := a.getBucket(ka.BucketType, bucketIdx)
  418. delete(bucket, ka.Addr.String())
  419. }
  420. ka.Buckets = nil
  421. if ka.BucketType == bucketTypeNew {
  422. a.nNew--
  423. } else {
  424. a.nOld--
  425. }
  426. delete(a.addrLookup, ka.Addr.String())
  427. }
  428. func (a *AddrBook) pickOldest(bucketType byte, bucketIdx int) *knownAddress {
  429. bucket := a.getBucket(bucketType, bucketIdx)
  430. var oldest *knownAddress
  431. for _, ka := range bucket {
  432. if oldest == nil || ka.LastAttempt.Before(oldest.LastAttempt) {
  433. oldest = ka
  434. }
  435. }
  436. return oldest
  437. }
  438. func (a *AddrBook) addAddress(addr, src *NetAddress) {
  439. if !addr.Routable() {
  440. log.Warn(Fmt("Cannot add non-routable address %v", addr))
  441. return
  442. }
  443. if _, ok := a.ourAddrs[addr.String()]; ok {
  444. // Ignore our own listener address.
  445. return
  446. }
  447. ka := a.addrLookup[addr.String()]
  448. if ka != nil {
  449. // Already old.
  450. if ka.isOld() {
  451. return
  452. }
  453. // Already in max new buckets.
  454. if len(ka.Buckets) == maxNewBucketsPerAddress {
  455. return
  456. }
  457. // The more entries we have, the less likely we are to add more.
  458. factor := int32(2 * len(ka.Buckets))
  459. if a.rand.Int31n(factor) != 0 {
  460. return
  461. }
  462. } else {
  463. ka = newKnownAddress(addr, src)
  464. }
  465. bucket := a.calcNewBucket(addr, src)
  466. a.addToNewBucket(ka, bucket)
  467. log.Notice("Added new address", "address", addr, "total", a.size())
  468. }
  469. // Make space in the new buckets by expiring the really bad entries.
  470. // If no bad entries are available we remove the oldest.
  471. func (a *AddrBook) expireNew(bucketIdx int) {
  472. for addrStr, ka := range a.addrNew[bucketIdx] {
  473. // If an entry is bad, throw it away
  474. if ka.isBad() {
  475. log.Notice(Fmt("expiring bad address %v", addrStr))
  476. a.removeFromBucket(ka, bucketTypeNew, bucketIdx)
  477. return
  478. }
  479. }
  480. // If we haven't thrown out a bad entry, throw out the oldest entry
  481. oldest := a.pickOldest(bucketTypeNew, bucketIdx)
  482. a.removeFromBucket(oldest, bucketTypeNew, bucketIdx)
  483. }
  484. // Promotes an address from new to old.
  485. // TODO: Move to old probabilistically.
  486. // The better a node is, the less likely it should be evicted from an old bucket.
  487. func (a *AddrBook) moveToOld(ka *knownAddress) {
  488. // Sanity check
  489. if ka.isOld() {
  490. log.Warn(Fmt("Cannot promote address that is already old %v", ka))
  491. return
  492. }
  493. if len(ka.Buckets) == 0 {
  494. log.Warn(Fmt("Cannot promote address that isn't in any new buckets %v", ka))
  495. return
  496. }
  497. // Remember one of the buckets in which ka is in.
  498. freedBucket := ka.Buckets[0]
  499. // Remove from all (new) buckets.
  500. a.removeFromAllBuckets(ka)
  501. // It's officially old now.
  502. ka.BucketType = bucketTypeOld
  503. // Try to add it to its oldBucket destination.
  504. oldBucketIdx := a.calcOldBucket(ka.Addr)
  505. added := a.addToOldBucket(ka, oldBucketIdx)
  506. if !added {
  507. // No room, must evict something
  508. oldest := a.pickOldest(bucketTypeOld, oldBucketIdx)
  509. a.removeFromBucket(oldest, bucketTypeOld, oldBucketIdx)
  510. // Find new bucket to put oldest in
  511. newBucketIdx := a.calcNewBucket(oldest.Addr, oldest.Src)
  512. added := a.addToNewBucket(oldest, newBucketIdx)
  513. // No space in newBucket either, just put it in freedBucket from above.
  514. if !added {
  515. added := a.addToNewBucket(oldest, freedBucket)
  516. if !added {
  517. log.Warn(Fmt("Could not migrate oldest %v to freedBucket %v", oldest, freedBucket))
  518. }
  519. }
  520. // Finally, add to bucket again.
  521. added = a.addToOldBucket(ka, oldBucketIdx)
  522. if !added {
  523. log.Warn(Fmt("Could not re-add ka %v to oldBucketIdx %v", ka, oldBucketIdx))
  524. }
  525. }
  526. }
  527. // doublesha256( key + sourcegroup +
  528. // int64(doublesha256(key + group + sourcegroup))%bucket_per_group ) % num_new_buckets
  529. func (a *AddrBook) calcNewBucket(addr, src *NetAddress) int {
  530. data1 := []byte{}
  531. data1 = append(data1, []byte(a.key)...)
  532. data1 = append(data1, []byte(groupKey(addr))...)
  533. data1 = append(data1, []byte(groupKey(src))...)
  534. hash1 := doubleSha256(data1)
  535. hash64 := binary.BigEndian.Uint64(hash1)
  536. hash64 %= newBucketsPerGroup
  537. var hashbuf [8]byte
  538. binary.BigEndian.PutUint64(hashbuf[:], hash64)
  539. data2 := []byte{}
  540. data2 = append(data2, []byte(a.key)...)
  541. data2 = append(data2, groupKey(src)...)
  542. data2 = append(data2, hashbuf[:]...)
  543. hash2 := doubleSha256(data2)
  544. return int(binary.BigEndian.Uint64(hash2) % newBucketCount)
  545. }
  546. // doublesha256( key + group +
  547. // int64(doublesha256(key + addr))%buckets_per_group ) % num_old_buckets
  548. func (a *AddrBook) calcOldBucket(addr *NetAddress) int {
  549. data1 := []byte{}
  550. data1 = append(data1, []byte(a.key)...)
  551. data1 = append(data1, []byte(addr.String())...)
  552. hash1 := doubleSha256(data1)
  553. hash64 := binary.BigEndian.Uint64(hash1)
  554. hash64 %= oldBucketsPerGroup
  555. var hashbuf [8]byte
  556. binary.BigEndian.PutUint64(hashbuf[:], hash64)
  557. data2 := []byte{}
  558. data2 = append(data2, []byte(a.key)...)
  559. data2 = append(data2, groupKey(addr)...)
  560. data2 = append(data2, hashbuf[:]...)
  561. hash2 := doubleSha256(data2)
  562. return int(binary.BigEndian.Uint64(hash2) % oldBucketCount)
  563. }
  564. // Return a string representing the network group of this address.
  565. // This is the /16 for IPv6, the /32 (/36 for he.net) for IPv6, the string
  566. // "local" for a local address and the string "unroutable for an unroutable
  567. // address.
  568. func groupKey(na *NetAddress) string {
  569. if na.Local() {
  570. return "local"
  571. }
  572. if !na.Routable() {
  573. return "unroutable"
  574. }
  575. if ipv4 := na.IP.To4(); ipv4 != nil {
  576. return (&net.IPNet{IP: na.IP, Mask: net.CIDRMask(16, 32)}).String()
  577. }
  578. if na.RFC6145() || na.RFC6052() {
  579. // last four bytes are the ip address
  580. ip := net.IP(na.IP[12:16])
  581. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  582. }
  583. if na.RFC3964() {
  584. ip := net.IP(na.IP[2:7])
  585. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  586. }
  587. if na.RFC4380() {
  588. // teredo tunnels have the last 4 bytes as the v4 address XOR
  589. // 0xff.
  590. ip := net.IP(make([]byte, 4))
  591. for i, byte := range na.IP[12:16] {
  592. ip[i] = byte ^ 0xff
  593. }
  594. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  595. }
  596. // OK, so now we know ourselves to be a IPv6 address.
  597. // bitcoind uses /32 for everything, except for Hurricane Electric's
  598. // (he.net) IP range, which it uses /36 for.
  599. bits := 32
  600. heNet := &net.IPNet{IP: net.ParseIP("2001:470::"),
  601. Mask: net.CIDRMask(32, 128)}
  602. if heNet.Contains(na.IP) {
  603. bits = 36
  604. }
  605. return (&net.IPNet{IP: na.IP, Mask: net.CIDRMask(bits, 128)}).String()
  606. }
  607. //-----------------------------------------------------------------------------
  608. /*
  609. knownAddress
  610. tracks information about a known network address that is used
  611. to determine how viable an address is.
  612. */
  613. type knownAddress struct {
  614. Addr *NetAddress
  615. Src *NetAddress
  616. Attempts int32
  617. LastAttempt time.Time
  618. LastSuccess time.Time
  619. BucketType byte
  620. Buckets []int
  621. }
  622. func newKnownAddress(addr *NetAddress, src *NetAddress) *knownAddress {
  623. return &knownAddress{
  624. Addr: addr,
  625. Src: src,
  626. Attempts: 0,
  627. LastAttempt: time.Now(),
  628. BucketType: bucketTypeNew,
  629. Buckets: nil,
  630. }
  631. }
  632. func (ka *knownAddress) isOld() bool {
  633. return ka.BucketType == bucketTypeOld
  634. }
  635. func (ka *knownAddress) isNew() bool {
  636. return ka.BucketType == bucketTypeNew
  637. }
  638. func (ka *knownAddress) markAttempt() {
  639. now := time.Now()
  640. ka.LastAttempt = now
  641. ka.Attempts += 1
  642. }
  643. func (ka *knownAddress) markGood() {
  644. now := time.Now()
  645. ka.LastAttempt = now
  646. ka.Attempts = 0
  647. ka.LastSuccess = now
  648. }
  649. func (ka *knownAddress) addBucketRef(bucketIdx int) int {
  650. for _, bucket := range ka.Buckets {
  651. if bucket == bucketIdx {
  652. log.Warn(Fmt("Bucket already exists in ka.Buckets: %v", ka))
  653. return -1
  654. }
  655. }
  656. ka.Buckets = append(ka.Buckets, bucketIdx)
  657. return len(ka.Buckets)
  658. }
  659. func (ka *knownAddress) removeBucketRef(bucketIdx int) int {
  660. buckets := []int{}
  661. for _, bucket := range ka.Buckets {
  662. if bucket != bucketIdx {
  663. buckets = append(buckets, bucket)
  664. }
  665. }
  666. if len(buckets) != len(ka.Buckets)-1 {
  667. log.Warn(Fmt("bucketIdx not found in ka.Buckets: %v", ka))
  668. return -1
  669. }
  670. ka.Buckets = buckets
  671. return len(ka.Buckets)
  672. }
  673. /*
  674. An address is bad if the address in question has not been tried in the last
  675. minute and meets one of the following criteria:
  676. 1) It claims to be from the future
  677. 2) It hasn't been seen in over a month
  678. 3) It has failed at least three times and never succeeded
  679. 4) It has failed ten times in the last week
  680. All addresses that meet these criteria are assumed to be worthless and not
  681. worth keeping hold of.
  682. */
  683. func (ka *knownAddress) isBad() bool {
  684. // Has been attempted in the last minute --> good
  685. if ka.LastAttempt.Before(time.Now().Add(-1 * time.Minute)) {
  686. return false
  687. }
  688. // Over a month old?
  689. if ka.LastAttempt.After(time.Now().Add(-1 * numMissingDays * time.Hour * 24)) {
  690. return true
  691. }
  692. // Never succeeded?
  693. if ka.LastSuccess.IsZero() && ka.Attempts >= numRetries {
  694. return true
  695. }
  696. // Hasn't succeeded in too long?
  697. if ka.LastSuccess.Before(time.Now().Add(-1*minBadDays*time.Hour*24)) &&
  698. ka.Attempts >= maxFailures {
  699. return true
  700. }
  701. return false
  702. }