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.

140 lines
3.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package pex
  2. import (
  3. "time"
  4. "github.com/tendermint/tendermint/p2p"
  5. )
  6. // knownAddress tracks information about a known network address
  7. // that is used to determine how viable an address is.
  8. type knownAddress struct {
  9. Addr *p2p.NetAddress `json:"addr"`
  10. Src *p2p.NetAddress `json:"src"`
  11. Buckets []int `json:"buckets"`
  12. Attempts int32 `json:"attempts"`
  13. BucketType byte `json:"bucket_type"`
  14. LastAttempt time.Time `json:"last_attempt"`
  15. LastSuccess time.Time `json:"last_success"`
  16. LastBanTime time.Time `json:"last_ban_time"`
  17. }
  18. func newKnownAddress(addr *p2p.NetAddress, src *p2p.NetAddress) *knownAddress {
  19. return &knownAddress{
  20. Addr: addr,
  21. Src: src,
  22. Attempts: 0,
  23. LastAttempt: time.Now(),
  24. BucketType: bucketTypeNew,
  25. Buckets: nil,
  26. }
  27. }
  28. func (ka *knownAddress) ID() p2p.ID {
  29. return ka.Addr.ID
  30. }
  31. func (ka *knownAddress) isOld() bool {
  32. return ka.BucketType == bucketTypeOld
  33. }
  34. func (ka *knownAddress) isNew() bool {
  35. return ka.BucketType == bucketTypeNew
  36. }
  37. func (ka *knownAddress) markAttempt() {
  38. now := time.Now()
  39. ka.LastAttempt = now
  40. ka.Attempts++
  41. }
  42. func (ka *knownAddress) markGood() {
  43. now := time.Now()
  44. ka.LastAttempt = now
  45. ka.Attempts = 0
  46. ka.LastSuccess = now
  47. }
  48. func (ka *knownAddress) ban(banTime time.Duration) {
  49. if ka.LastBanTime.Before(time.Now().Add(banTime)) {
  50. ka.LastBanTime = time.Now().Add(banTime)
  51. }
  52. }
  53. func (ka *knownAddress) isBanned() bool {
  54. return ka.LastBanTime.After(time.Now())
  55. }
  56. func (ka *knownAddress) addBucketRef(bucketIdx int) int {
  57. for _, bucket := range ka.Buckets {
  58. if bucket == bucketIdx {
  59. // TODO refactor to return error?
  60. // log.Warn(Fmt("Bucket already exists in ka.Buckets: %v", ka))
  61. return -1
  62. }
  63. }
  64. ka.Buckets = append(ka.Buckets, bucketIdx)
  65. return len(ka.Buckets)
  66. }
  67. func (ka *knownAddress) removeBucketRef(bucketIdx int) int {
  68. buckets := []int{}
  69. for _, bucket := range ka.Buckets {
  70. if bucket != bucketIdx {
  71. buckets = append(buckets, bucket)
  72. }
  73. }
  74. if len(buckets) != len(ka.Buckets)-1 {
  75. // TODO refactor to return error?
  76. // log.Warn(Fmt("bucketIdx not found in ka.Buckets: %v", ka))
  77. return -1
  78. }
  79. ka.Buckets = buckets
  80. return len(ka.Buckets)
  81. }
  82. /*
  83. An address is bad if the address in question is a New address, has not been tried in the last
  84. minute, and meets one of the following criteria:
  85. 1) It claims to be from the future
  86. 2) It hasn't been seen in over a week
  87. 3) It has failed at least three times and never succeeded
  88. 4) It has failed ten times in the last week
  89. All addresses that meet these criteria are assumed to be worthless and not
  90. worth keeping hold of.
  91. */
  92. func (ka *knownAddress) isBad() bool {
  93. // Is Old --> good
  94. if ka.BucketType == bucketTypeOld {
  95. return false
  96. }
  97. // Has been attempted in the last minute --> good
  98. if ka.LastAttempt.After(time.Now().Add(-1 * time.Minute)) {
  99. return false
  100. }
  101. // TODO: From the future?
  102. // Too old?
  103. // TODO: should be a timestamp of last seen, not just last attempt
  104. if ka.LastAttempt.Before(time.Now().Add(-1 * numMissingDays * time.Hour * 24)) {
  105. return true
  106. }
  107. // Never succeeded?
  108. if ka.LastSuccess.IsZero() && ka.Attempts >= numRetries {
  109. return true
  110. }
  111. // Hasn't succeeded in too long?
  112. if ka.LastSuccess.Before(time.Now().Add(-1*minBadDays*time.Hour*24)) &&
  113. ka.Attempts >= maxFailures {
  114. return true
  115. }
  116. return false
  117. }