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.

177 lines
4.1 KiB

7 years ago
7 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
7 years ago
7 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
7 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
7 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
7 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
p2p: seed mode refactoring (#3011) ListOfKnownAddresses is removed panic if addrbook size is less than zero CrawlPeers does not attempt to connect to existing or peers we're currently dialing various perf. fixes improved tests (though not complete) move IsDialingOrExistingAddress check into DialPeerWithAddress (Fixes #2716) * addrbook: preallocate memory when saving addrbook to file * addrbook: remove oldestFirst struct and check for ID * oldestFirst replaced with sort.Slice * ID is now mandatory, so no need to check * addrbook: remove ListOfKnownAddresses GetSelection is used instead in seed mode. * addrbook: panic if size is less than 0 * rewrite addrbook#saveToFile to not use a counter * test AttemptDisconnects func * move IsDialingOrExistingAddress check into DialPeerWithAddress * save and cleanup crawl peer data * get rid of DefaultSeedDisconnectWaitPeriod * make linter happy * fix TestPEXReactorSeedMode * fix comment * add a changelog entry * Apply suggestions from code review Co-Authored-By: melekes <anton.kalyaev@gmail.com> * rename ErrDialingOrExistingAddress to ErrCurrentlyDialingOrExistingAddress * lowercase errors * do not persist seed data pros: - no extra files - less IO cons: - if the node crashes, seed might crawl a peer too soon * fixes after Ethan's review * add a changelog entry * we should only consult Switch about peers checking addrbook size does not make sense since only PEX reactor uses it for dialing peers! https://github.com/tendermint/tendermint/pull/3011#discussion_r270948875
6 years ago
  1. package p2p
  2. import (
  3. "fmt"
  4. "net"
  5. "github.com/tendermint/tendermint/types"
  6. )
  7. // ErrFilterTimeout indicates that a filter operation timed out.
  8. type ErrFilterTimeout struct{}
  9. func (e ErrFilterTimeout) Error() string {
  10. return "filter timed out"
  11. }
  12. // ErrRejected indicates that a Peer was rejected carrying additional
  13. // information as to the reason.
  14. type ErrRejected struct {
  15. addr NetAddress
  16. conn net.Conn
  17. err error
  18. id types.NodeID
  19. isAuthFailure bool
  20. isDuplicate bool
  21. isFiltered bool
  22. isIncompatible bool
  23. isNodeInfoInvalid bool
  24. isSelf bool
  25. }
  26. // Addr returns the NetAddress for the rejected Peer.
  27. func (e ErrRejected) Addr() NetAddress {
  28. return e.addr
  29. }
  30. func (e ErrRejected) Error() string {
  31. if e.isAuthFailure {
  32. return fmt.Sprintf("auth failure: %s", e.err)
  33. }
  34. if e.isDuplicate {
  35. if e.conn != nil {
  36. return fmt.Sprintf(
  37. "duplicate CONN<%s>",
  38. e.conn.RemoteAddr().String(),
  39. )
  40. }
  41. if e.id != "" {
  42. return fmt.Sprintf("duplicate ID<%v>", e.id)
  43. }
  44. }
  45. if e.isFiltered {
  46. if e.conn != nil {
  47. return fmt.Sprintf(
  48. "filtered CONN<%s>: %s",
  49. e.conn.RemoteAddr().String(),
  50. e.err,
  51. )
  52. }
  53. if e.id != "" {
  54. return fmt.Sprintf("filtered ID<%v>: %s", e.id, e.err)
  55. }
  56. }
  57. if e.isIncompatible {
  58. return fmt.Sprintf("incompatible: %s", e.err)
  59. }
  60. if e.isNodeInfoInvalid {
  61. return fmt.Sprintf("invalid NodeInfo: %s", e.err)
  62. }
  63. if e.isSelf {
  64. return fmt.Sprintf("self ID<%v>", e.id)
  65. }
  66. return fmt.Sprintf("%s", e.err)
  67. }
  68. // IsAuthFailure when Peer authentication was unsuccessful.
  69. func (e ErrRejected) IsAuthFailure() bool { return e.isAuthFailure }
  70. // IsDuplicate when Peer ID or IP are present already.
  71. func (e ErrRejected) IsDuplicate() bool { return e.isDuplicate }
  72. // IsFiltered when Peer ID or IP was filtered.
  73. func (e ErrRejected) IsFiltered() bool { return e.isFiltered }
  74. // IsIncompatible when Peer NodeInfo is not compatible with our own.
  75. func (e ErrRejected) IsIncompatible() bool { return e.isIncompatible }
  76. // IsNodeInfoInvalid when the sent NodeInfo is not valid.
  77. func (e ErrRejected) IsNodeInfoInvalid() bool { return e.isNodeInfoInvalid }
  78. // IsSelf when Peer is our own node.
  79. func (e ErrRejected) IsSelf() bool { return e.isSelf }
  80. // ErrSwitchDuplicatePeerID to be raised when a peer is connecting with a known
  81. // ID.
  82. type ErrSwitchDuplicatePeerID struct {
  83. ID types.NodeID
  84. }
  85. func (e ErrSwitchDuplicatePeerID) Error() string {
  86. return fmt.Sprintf("duplicate peer ID %v", e.ID)
  87. }
  88. // ErrSwitchDuplicatePeerIP to be raised whena a peer is connecting with a known
  89. // IP.
  90. type ErrSwitchDuplicatePeerIP struct {
  91. IP net.IP
  92. }
  93. func (e ErrSwitchDuplicatePeerIP) Error() string {
  94. return fmt.Sprintf("duplicate peer IP %v", e.IP.String())
  95. }
  96. // ErrSwitchConnectToSelf to be raised when trying to connect to itself.
  97. type ErrSwitchConnectToSelf struct {
  98. Addr *NetAddress
  99. }
  100. func (e ErrSwitchConnectToSelf) Error() string {
  101. return fmt.Sprintf("connect to self: %v", e.Addr)
  102. }
  103. type ErrSwitchAuthenticationFailure struct {
  104. Dialed *NetAddress
  105. Got types.NodeID
  106. }
  107. func (e ErrSwitchAuthenticationFailure) Error() string {
  108. return fmt.Sprintf(
  109. "failed to authenticate peer. Dialed %v, but got peer with ID %s",
  110. e.Dialed,
  111. e.Got,
  112. )
  113. }
  114. // ErrTransportClosed is raised when the Transport has been closed.
  115. type ErrTransportClosed struct{}
  116. func (e ErrTransportClosed) Error() string {
  117. return "transport has been closed"
  118. }
  119. //-------------------------------------------------------------------
  120. type ErrNetAddressNoID struct {
  121. Addr string
  122. }
  123. func (e ErrNetAddressNoID) Error() string {
  124. return fmt.Sprintf("address (%s) does not contain ID", e.Addr)
  125. }
  126. type ErrNetAddressInvalid struct {
  127. Addr string
  128. Err error
  129. }
  130. func (e ErrNetAddressInvalid) Error() string {
  131. return fmt.Sprintf("invalid address (%s): %v", e.Addr, e.Err)
  132. }
  133. // ErrCurrentlyDialingOrExistingAddress indicates that we're currently
  134. // dialing this address or it belongs to an existing peer.
  135. type ErrCurrentlyDialingOrExistingAddress struct {
  136. Addr string
  137. }
  138. func (e ErrCurrentlyDialingOrExistingAddress) Error() string {
  139. return fmt.Sprintf("connection with %s has been established or dialed", e.Addr)
  140. }