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.

129 lines
3.1 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
  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. }
  17. func newKnownAddress(addr *p2p.NetAddress, src *p2p.NetAddress) *knownAddress {
  18. return &knownAddress{
  19. Addr: addr,
  20. Src: src,
  21. Attempts: 0,
  22. LastAttempt: time.Now(),
  23. BucketType: bucketTypeNew,
  24. Buckets: nil,
  25. }
  26. }
  27. func (ka *knownAddress) ID() p2p.ID {
  28. return ka.Addr.ID
  29. }
  30. func (ka *knownAddress) isOld() bool {
  31. return ka.BucketType == bucketTypeOld
  32. }
  33. func (ka *knownAddress) isNew() bool {
  34. return ka.BucketType == bucketTypeNew
  35. }
  36. func (ka *knownAddress) markAttempt() {
  37. now := time.Now()
  38. ka.LastAttempt = now
  39. ka.Attempts++
  40. }
  41. func (ka *knownAddress) markGood() {
  42. now := time.Now()
  43. ka.LastAttempt = now
  44. ka.Attempts = 0
  45. ka.LastSuccess = now
  46. }
  47. func (ka *knownAddress) addBucketRef(bucketIdx int) int {
  48. for _, bucket := range ka.Buckets {
  49. if bucket == bucketIdx {
  50. // TODO refactor to return error?
  51. // log.Warn(Fmt("Bucket already exists in ka.Buckets: %v", ka))
  52. return -1
  53. }
  54. }
  55. ka.Buckets = append(ka.Buckets, bucketIdx)
  56. return len(ka.Buckets)
  57. }
  58. func (ka *knownAddress) removeBucketRef(bucketIdx int) int {
  59. buckets := []int{}
  60. for _, bucket := range ka.Buckets {
  61. if bucket != bucketIdx {
  62. buckets = append(buckets, bucket)
  63. }
  64. }
  65. if len(buckets) != len(ka.Buckets)-1 {
  66. // TODO refactor to return error?
  67. // log.Warn(Fmt("bucketIdx not found in ka.Buckets: %v", ka))
  68. return -1
  69. }
  70. ka.Buckets = buckets
  71. return len(ka.Buckets)
  72. }
  73. /*
  74. An address is bad if the address in question is a New address, has not been tried in the last
  75. minute, and meets one of the following criteria:
  76. 1) It claims to be from the future
  77. 2) It hasn't been seen in over a week
  78. 3) It has failed at least three times and never succeeded
  79. 4) It has failed ten times in the last week
  80. All addresses that meet these criteria are assumed to be worthless and not
  81. worth keeping hold of.
  82. */
  83. func (ka *knownAddress) isBad() bool {
  84. // Is Old --> good
  85. if ka.BucketType == bucketTypeOld {
  86. return false
  87. }
  88. // Has been attempted in the last minute --> good
  89. if ka.LastAttempt.After(time.Now().Add(-1 * time.Minute)) {
  90. return false
  91. }
  92. // TODO: From the future?
  93. // Too old?
  94. // TODO: should be a timestamp of last seen, not just last attempt
  95. if ka.LastAttempt.Before(time.Now().Add(-1 * numMissingDays * time.Hour * 24)) {
  96. return true
  97. }
  98. // Never succeeded?
  99. if ka.LastSuccess.IsZero() && ka.Attempts >= numRetries {
  100. return true
  101. }
  102. // Hasn't succeeded in too long?
  103. if ka.LastSuccess.Before(time.Now().Add(-1*minBadDays*time.Hour*24)) &&
  104. ka.Attempts >= maxFailures {
  105. return true
  106. }
  107. return false
  108. }