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.

138 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. ka.LastBanTime = time.Now().Add(banTime)
  50. }
  51. func (ka *knownAddress) isBanned() bool {
  52. return ka.LastBanTime.After(time.Now())
  53. }
  54. func (ka *knownAddress) addBucketRef(bucketIdx int) int {
  55. for _, bucket := range ka.Buckets {
  56. if bucket == bucketIdx {
  57. // TODO refactor to return error?
  58. // log.Warn(Fmt("Bucket already exists in ka.Buckets: %v", ka))
  59. return -1
  60. }
  61. }
  62. ka.Buckets = append(ka.Buckets, bucketIdx)
  63. return len(ka.Buckets)
  64. }
  65. func (ka *knownAddress) removeBucketRef(bucketIdx int) int {
  66. buckets := []int{}
  67. for _, bucket := range ka.Buckets {
  68. if bucket != bucketIdx {
  69. buckets = append(buckets, bucket)
  70. }
  71. }
  72. if len(buckets) != len(ka.Buckets)-1 {
  73. // TODO refactor to return error?
  74. // log.Warn(Fmt("bucketIdx not found in ka.Buckets: %v", ka))
  75. return -1
  76. }
  77. ka.Buckets = buckets
  78. return len(ka.Buckets)
  79. }
  80. /*
  81. An address is bad if the address in question is a New address, has not been tried in the last
  82. minute, and meets one of the following criteria:
  83. 1) It claims to be from the future
  84. 2) It hasn't been seen in over a week
  85. 3) It has failed at least three times and never succeeded
  86. 4) It has failed ten times in the last week
  87. All addresses that meet these criteria are assumed to be worthless and not
  88. worth keeping hold of.
  89. */
  90. func (ka *knownAddress) isBad() bool {
  91. // Is Old --> good
  92. if ka.BucketType == bucketTypeOld {
  93. return false
  94. }
  95. // Has been attempted in the last minute --> good
  96. if ka.LastAttempt.After(time.Now().Add(-1 * time.Minute)) {
  97. return false
  98. }
  99. // TODO: From the future?
  100. // Too old?
  101. // TODO: should be a timestamp of last seen, not just last attempt
  102. if ka.LastAttempt.Before(time.Now().Add(-1 * numMissingDays * time.Hour * 24)) {
  103. return true
  104. }
  105. // Never succeeded?
  106. if ka.LastSuccess.IsZero() && ka.Attempts >= numRetries {
  107. return true
  108. }
  109. // Hasn't succeeded in too long?
  110. if ka.LastSuccess.Before(time.Now().Add(-1*minBadDays*time.Hour*24)) &&
  111. ka.Attempts >= maxFailures {
  112. return true
  113. }
  114. return false
  115. }