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.

645 lines
18 KiB

9 years ago
9 years ago
8 years ago
7 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
8 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
p2p: introduce peerConn to simplify peer creation (#1226) * expose AuthEnc in the P2P config if AuthEnc is true, dialed peers must have a node ID in the address and it must match the persistent pubkey from the secret handshake. Refs #1157 * fixes after my own review * fix docs * fix build failure ``` p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal ``` * p2p: introduce peerConn to simplify peer creation * Introduce `peerConn` containing the known fields of `peer` * `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked * Eliminates some mutable variables and makes the code flow better * Simplifies the `newXxxPeer` funcs * Use ID instead of PubKey where possible. * SetPubKeyFilter -> SetIDFilter * nodeInfo.Validate takes ID * remove peer.PubKey() * persistent node ids * fixes from review * test: use ip_plus_id.sh more * fix invalid memory panic during fast_sync test ``` 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e] 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0 xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0 xc420933e00, 0xc423f48801, 0x28, 0x2) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x 0, 0x0) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7 ```
7 years ago
p2p: introduce peerConn to simplify peer creation (#1226) * expose AuthEnc in the P2P config if AuthEnc is true, dialed peers must have a node ID in the address and it must match the persistent pubkey from the secret handshake. Refs #1157 * fixes after my own review * fix docs * fix build failure ``` p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal ``` * p2p: introduce peerConn to simplify peer creation * Introduce `peerConn` containing the known fields of `peer` * `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked * Eliminates some mutable variables and makes the code flow better * Simplifies the `newXxxPeer` funcs * Use ID instead of PubKey where possible. * SetPubKeyFilter -> SetIDFilter * nodeInfo.Validate takes ID * remove peer.PubKey() * persistent node ids * fixes from review * test: use ip_plus_id.sh more * fix invalid memory panic during fast_sync test ``` 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e] 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0 xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0 xc420933e00, 0xc423f48801, 0x28, 0x2) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x 0, 0x0) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7 ```
7 years ago
p2p: introduce peerConn to simplify peer creation (#1226) * expose AuthEnc in the P2P config if AuthEnc is true, dialed peers must have a node ID in the address and it must match the persistent pubkey from the secret handshake. Refs #1157 * fixes after my own review * fix docs * fix build failure ``` p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal ``` * p2p: introduce peerConn to simplify peer creation * Introduce `peerConn` containing the known fields of `peer` * `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked * Eliminates some mutable variables and makes the code flow better * Simplifies the `newXxxPeer` funcs * Use ID instead of PubKey where possible. * SetPubKeyFilter -> SetIDFilter * nodeInfo.Validate takes ID * remove peer.PubKey() * persistent node ids * fixes from review * test: use ip_plus_id.sh more * fix invalid memory panic during fast_sync test ``` 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e] 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0 xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0 xc420933e00, 0xc423f48801, 0x28, 0x2) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x 0, 0x0) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7 ```
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package pex
  2. import (
  3. "fmt"
  4. "reflect"
  5. "sort"
  6. "sync"
  7. "time"
  8. "github.com/tendermint/go-amino"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. "github.com/tendermint/tendermint/p2p"
  11. "github.com/tendermint/tendermint/p2p/conn"
  12. )
  13. type Peer = p2p.Peer
  14. const (
  15. // PexChannel is a channel for PEX messages
  16. PexChannel = byte(0x00)
  17. maxMsgSize = 1048576 // 1MB
  18. // ensure we have enough peers
  19. defaultEnsurePeersPeriod = 30 * time.Second
  20. defaultMinNumOutboundPeers = 10
  21. // Seed/Crawler constants
  22. // We want seeds to only advertise good peers. Therefore they should wait at
  23. // least as long as we expect it to take for a peer to become good before
  24. // disconnecting.
  25. // see consensus/reactor.go: blocksToContributeToBecomeGoodPeer
  26. // 10000 blocks assuming 1s blocks ~ 2.7 hours.
  27. defaultSeedDisconnectWaitPeriod = 3 * time.Hour
  28. defaultCrawlPeerInterval = 2 * time.Minute // don't redial for this. TODO: back-off. what for?
  29. defaultCrawlPeersPeriod = 30 * time.Second // check some peers every this
  30. maxAttemptsToDial = 16 // ~ 35h in total (last attempt - 18h)
  31. // if node connects to seed, it does not have any trusted peers.
  32. // Especially in the beginning, node should have more trusted peers than
  33. // untrusted.
  34. biasToSelectNewPeers = 30 // 70 to select good peers
  35. )
  36. // PEXReactor handles PEX (peer exchange) and ensures that an
  37. // adequate number of peers are connected to the switch.
  38. //
  39. // It uses `AddrBook` (address book) to store `NetAddress`es of the peers.
  40. //
  41. // ## Preventing abuse
  42. //
  43. // Only accept pexAddrsMsg from peers we sent a corresponding pexRequestMsg too.
  44. // Only accept one pexRequestMsg every ~defaultEnsurePeersPeriod.
  45. type PEXReactor struct {
  46. p2p.BaseReactor
  47. book AddrBook
  48. config *PEXReactorConfig
  49. ensurePeersPeriod time.Duration
  50. // maps to prevent abuse
  51. requestsSent *cmn.CMap // ID->struct{}: unanswered send requests
  52. lastReceivedRequests *cmn.CMap // ID->time.Time: last time peer requested from us
  53. attemptsToDial sync.Map // address (string) -> {number of attempts (int), last time dialed (time.Time)}
  54. }
  55. // PEXReactorConfig holds reactor specific configuration data.
  56. type PEXReactorConfig struct {
  57. // Seed/Crawler mode
  58. SeedMode bool
  59. // Seeds is a list of addresses reactor may use
  60. // if it can't connect to peers in the addrbook.
  61. Seeds []string
  62. // PrivatePeerIDs is a list of peer IDs, which must not be gossiped to other
  63. // peers.
  64. PrivatePeerIDs []string
  65. }
  66. type _attemptsToDial struct {
  67. number int
  68. lastDialed time.Time
  69. }
  70. // NewPEXReactor creates new PEX reactor.
  71. func NewPEXReactor(b AddrBook, config *PEXReactorConfig) *PEXReactor {
  72. r := &PEXReactor{
  73. book: b,
  74. config: config,
  75. ensurePeersPeriod: defaultEnsurePeersPeriod,
  76. requestsSent: cmn.NewCMap(),
  77. lastReceivedRequests: cmn.NewCMap(),
  78. }
  79. r.BaseReactor = *p2p.NewBaseReactor("PEXReactor", r)
  80. return r
  81. }
  82. // OnStart implements BaseService
  83. func (r *PEXReactor) OnStart() error {
  84. if err := r.BaseReactor.OnStart(); err != nil {
  85. return err
  86. }
  87. err := r.book.Start()
  88. if err != nil && err != cmn.ErrAlreadyStarted {
  89. return err
  90. }
  91. // return err if user provided a bad seed address
  92. if err := r.checkSeeds(); err != nil {
  93. return err
  94. }
  95. // Check if this node should run
  96. // in seed/crawler mode
  97. if r.config.SeedMode {
  98. go r.crawlPeersRoutine()
  99. } else {
  100. go r.ensurePeersRoutine()
  101. }
  102. return nil
  103. }
  104. // OnStop implements BaseService
  105. func (r *PEXReactor) OnStop() {
  106. r.BaseReactor.OnStop()
  107. r.book.Stop()
  108. }
  109. // GetChannels implements Reactor
  110. func (r *PEXReactor) GetChannels() []*conn.ChannelDescriptor {
  111. return []*conn.ChannelDescriptor{
  112. {
  113. ID: PexChannel,
  114. Priority: 1,
  115. SendQueueCapacity: 10,
  116. },
  117. }
  118. }
  119. // AddPeer implements Reactor by adding peer to the address book (if inbound)
  120. // or by requesting more addresses (if outbound).
  121. func (r *PEXReactor) AddPeer(p Peer) {
  122. if p.IsOutbound() {
  123. // For outbound peers, the address is already in the books -
  124. // either via DialPeersAsync or r.Receive.
  125. // Ask it for more peers if we need.
  126. if r.book.NeedMoreAddrs() {
  127. r.RequestAddrs(p)
  128. }
  129. } else {
  130. // For inbound peers, the peer is its own source,
  131. // and its NodeInfo has already been validated.
  132. // Let the ensurePeersRoutine handle asking for more
  133. // peers when we need - we don't trust inbound peers as much.
  134. addr := p.NodeInfo().NetAddress()
  135. if !isAddrPrivate(addr, r.config.PrivatePeerIDs) {
  136. err := r.book.AddAddress(addr, addr)
  137. if err != nil {
  138. r.Logger.Error("Failed to add new address", "err", err)
  139. }
  140. }
  141. }
  142. }
  143. // RemovePeer implements Reactor.
  144. func (r *PEXReactor) RemovePeer(p Peer, reason interface{}) {
  145. id := string(p.ID())
  146. r.requestsSent.Delete(id)
  147. r.lastReceivedRequests.Delete(id)
  148. }
  149. // Receive implements Reactor by handling incoming PEX messages.
  150. func (r *PEXReactor) Receive(chID byte, src Peer, msgBytes []byte) {
  151. msg, err := DecodeMessage(msgBytes)
  152. if err != nil {
  153. r.Logger.Error("Error decoding message", "src", src, "chId", chID, "msg", msg, "err", err, "bytes", msgBytes)
  154. r.Switch.StopPeerForError(src, err)
  155. return
  156. }
  157. r.Logger.Debug("Received message", "src", src, "chId", chID, "msg", msg)
  158. switch msg := msg.(type) {
  159. case *pexRequestMessage:
  160. // Check we're not receiving too many requests
  161. if err := r.receiveRequest(src); err != nil {
  162. r.Switch.StopPeerForError(src, err)
  163. return
  164. }
  165. // Seeds disconnect after sending a batch of addrs
  166. if r.config.SeedMode {
  167. r.SendAddrs(src, r.book.GetSelectionWithBias(biasToSelectNewPeers))
  168. r.Switch.StopPeerGracefully(src)
  169. } else {
  170. r.SendAddrs(src, r.book.GetSelection())
  171. }
  172. case *pexAddrsMessage:
  173. // If we asked for addresses, add them to the book
  174. if err := r.ReceiveAddrs(msg.Addrs, src); err != nil {
  175. r.Switch.StopPeerForError(src, err)
  176. return
  177. }
  178. default:
  179. r.Logger.Error(fmt.Sprintf("Unknown message type %v", reflect.TypeOf(msg)))
  180. }
  181. }
  182. func (r *PEXReactor) receiveRequest(src Peer) error {
  183. id := string(src.ID())
  184. v := r.lastReceivedRequests.Get(id)
  185. if v == nil {
  186. // initialize with empty time
  187. lastReceived := time.Time{}
  188. r.lastReceivedRequests.Set(id, lastReceived)
  189. return nil
  190. }
  191. lastReceived := v.(time.Time)
  192. if lastReceived.Equal(time.Time{}) {
  193. // first time gets a free pass. then we start tracking the time
  194. lastReceived = time.Now()
  195. r.lastReceivedRequests.Set(id, lastReceived)
  196. return nil
  197. }
  198. now := time.Now()
  199. if now.Sub(lastReceived) < r.ensurePeersPeriod/3 {
  200. return fmt.Errorf("Peer (%v) is sending too many PEX requests. Disconnecting", src.ID())
  201. }
  202. r.lastReceivedRequests.Set(id, now)
  203. return nil
  204. }
  205. // RequestAddrs asks peer for more addresses if we do not already
  206. // have a request out for this peer.
  207. func (r *PEXReactor) RequestAddrs(p Peer) {
  208. id := string(p.ID())
  209. if r.requestsSent.Has(id) {
  210. return
  211. }
  212. r.requestsSent.Set(id, struct{}{})
  213. p.Send(PexChannel, cdc.MustMarshalBinary(&pexRequestMessage{}))
  214. }
  215. // ReceiveAddrs adds the given addrs to the addrbook if theres an open
  216. // request for this peer and deletes the open request.
  217. // If there's no open request for the src peer, it returns an error.
  218. func (r *PEXReactor) ReceiveAddrs(addrs []*p2p.NetAddress, src Peer) error {
  219. id := string(src.ID())
  220. if !r.requestsSent.Has(id) {
  221. return cmn.NewError("Received unsolicited pexAddrsMessage")
  222. }
  223. r.requestsSent.Delete(id)
  224. srcAddr := src.NodeInfo().NetAddress()
  225. for _, netAddr := range addrs {
  226. if netAddr != nil && !isAddrPrivate(netAddr, r.config.PrivatePeerIDs) {
  227. err := r.book.AddAddress(netAddr, srcAddr)
  228. if err != nil {
  229. r.Logger.Error("Failed to add new address", "err", err)
  230. }
  231. }
  232. }
  233. return nil
  234. }
  235. // SendAddrs sends addrs to the peer.
  236. func (r *PEXReactor) SendAddrs(p Peer, netAddrs []*p2p.NetAddress) {
  237. p.Send(PexChannel, cdc.MustMarshalBinary(&pexAddrsMessage{Addrs: netAddrs}))
  238. }
  239. // SetEnsurePeersPeriod sets period to ensure peers connected.
  240. func (r *PEXReactor) SetEnsurePeersPeriod(d time.Duration) {
  241. r.ensurePeersPeriod = d
  242. }
  243. // Ensures that sufficient peers are connected. (continuous)
  244. func (r *PEXReactor) ensurePeersRoutine() {
  245. var (
  246. seed = cmn.NewRand()
  247. jitter = seed.Int63n(r.ensurePeersPeriod.Nanoseconds())
  248. )
  249. // Randomize first round of communication to avoid thundering herd.
  250. // If no potential peers are present directly start connecting so we guarantee
  251. // swift setup with the help of configured seeds.
  252. if r.hasPotentialPeers() {
  253. time.Sleep(time.Duration(jitter))
  254. }
  255. // fire once immediately.
  256. // ensures we dial the seeds right away if the book is empty
  257. r.ensurePeers()
  258. // fire periodically
  259. ticker := time.NewTicker(r.ensurePeersPeriod)
  260. for {
  261. select {
  262. case <-ticker.C:
  263. r.ensurePeers()
  264. case <-r.Quit():
  265. ticker.Stop()
  266. return
  267. }
  268. }
  269. }
  270. // ensurePeers ensures that sufficient peers are connected. (once)
  271. //
  272. // heuristic that we haven't perfected yet, or, perhaps is manually edited by
  273. // the node operator. It should not be used to compute what addresses are
  274. // already connected or not.
  275. func (r *PEXReactor) ensurePeers() {
  276. var (
  277. out, in, dial = r.Switch.NumPeers()
  278. numToDial = defaultMinNumOutboundPeers - (out + dial)
  279. )
  280. r.Logger.Info(
  281. "Ensure peers",
  282. "numOutPeers", out,
  283. "numInPeers", in,
  284. "numDialing", dial,
  285. "numToDial", numToDial,
  286. )
  287. if numToDial <= 0 {
  288. return
  289. }
  290. // bias to prefer more vetted peers when we have fewer connections.
  291. // not perfect, but somewhate ensures that we prioritize connecting to more-vetted
  292. // NOTE: range here is [10, 90]. Too high ?
  293. newBias := cmn.MinInt(out, 8)*10 + 10
  294. toDial := make(map[p2p.ID]*p2p.NetAddress)
  295. // Try maxAttempts times to pick numToDial addresses to dial
  296. maxAttempts := numToDial * 3
  297. for i := 0; i < maxAttempts && len(toDial) < numToDial; i++ {
  298. try := r.book.PickAddress(newBias)
  299. if try == nil {
  300. continue
  301. }
  302. if _, selected := toDial[try.ID]; selected {
  303. continue
  304. }
  305. if dialling := r.Switch.IsDialing(try.ID); dialling {
  306. continue
  307. }
  308. if connected := r.Switch.Peers().Has(try.ID); connected {
  309. continue
  310. }
  311. r.Logger.Info("Will dial address", "addr", try)
  312. toDial[try.ID] = try
  313. }
  314. // Dial picked addresses
  315. for _, addr := range toDial {
  316. go r.dialPeer(addr)
  317. }
  318. // If we need more addresses, pick a random peer and ask for more.
  319. if r.book.NeedMoreAddrs() {
  320. peers := r.Switch.Peers().List()
  321. peersCount := len(peers)
  322. if peersCount > 0 {
  323. peer := peers[cmn.RandInt()%peersCount] // nolint: gas
  324. r.Logger.Info("We need more addresses. Sending pexRequest to random peer", "peer", peer)
  325. r.RequestAddrs(peer)
  326. }
  327. }
  328. // If we are not connected to nor dialing anybody, fallback to dialing a seed.
  329. if out+in+dial+len(toDial) == 0 {
  330. r.Logger.Info("No addresses to dial nor connected peers. Falling back to seeds")
  331. r.dialSeeds()
  332. }
  333. }
  334. func (r *PEXReactor) dialPeer(addr *p2p.NetAddress) {
  335. var attempts int
  336. var lastDialed time.Time
  337. if lAttempts, attempted := r.attemptsToDial.Load(addr.DialString()); attempted {
  338. attempts = lAttempts.(_attemptsToDial).number
  339. lastDialed = lAttempts.(_attemptsToDial).lastDialed
  340. }
  341. if attempts > maxAttemptsToDial {
  342. r.Logger.Error("Reached max attempts to dial", "addr", addr, "attempts", attempts)
  343. r.book.MarkBad(addr)
  344. return
  345. }
  346. // exponential backoff if it's not our first attempt to dial given address
  347. if attempts > 0 {
  348. jitterSeconds := time.Duration(cmn.RandFloat64() * float64(time.Second)) // 1s == (1e9 ns)
  349. backoffDuration := jitterSeconds + ((1 << uint(attempts)) * time.Second)
  350. sinceLastDialed := time.Since(lastDialed)
  351. if sinceLastDialed < backoffDuration {
  352. r.Logger.Debug("Too early to dial", "addr", addr, "backoff_duration", backoffDuration, "last_dialed", lastDialed, "time_since", sinceLastDialed)
  353. return
  354. }
  355. }
  356. err := r.Switch.DialPeerWithAddress(addr, false)
  357. if err != nil {
  358. r.Logger.Error("Dialing failed", "addr", addr, "err", err, "attempts", attempts)
  359. // TODO: detect more "bad peer" scenarios
  360. if _, ok := err.(p2p.ErrSwitchAuthenticationFailure); ok {
  361. r.book.MarkBad(addr)
  362. r.attemptsToDial.Delete(addr.DialString())
  363. } else {
  364. r.book.MarkAttempt(addr)
  365. // FIXME: if the addr is going to be removed from the addrbook (hard to
  366. // tell at this point), we need to Delete it from attemptsToDial, not
  367. // record another attempt.
  368. // record attempt
  369. r.attemptsToDial.Store(addr.DialString(), _attemptsToDial{attempts + 1, time.Now()})
  370. }
  371. } else {
  372. // cleanup any history
  373. r.attemptsToDial.Delete(addr.DialString())
  374. }
  375. }
  376. // check seed addresses are well formed
  377. func (r *PEXReactor) checkSeeds() error {
  378. lSeeds := len(r.config.Seeds)
  379. if lSeeds == 0 {
  380. return nil
  381. }
  382. _, errs := p2p.NewNetAddressStrings(r.config.Seeds)
  383. for _, err := range errs {
  384. if err != nil {
  385. return err
  386. }
  387. }
  388. return nil
  389. }
  390. // randomly dial seeds until we connect to one or exhaust them
  391. func (r *PEXReactor) dialSeeds() {
  392. lSeeds := len(r.config.Seeds)
  393. if lSeeds == 0 {
  394. return
  395. }
  396. seedAddrs, _ := p2p.NewNetAddressStrings(r.config.Seeds)
  397. perm := cmn.RandPerm(lSeeds)
  398. // perm := r.Switch.rng.Perm(lSeeds)
  399. for _, i := range perm {
  400. // dial a random seed
  401. seedAddr := seedAddrs[i]
  402. err := r.Switch.DialPeerWithAddress(seedAddr, false)
  403. if err == nil {
  404. return
  405. }
  406. r.Switch.Logger.Error("Error dialing seed", "err", err, "seed", seedAddr)
  407. }
  408. r.Switch.Logger.Error("Couldn't connect to any seeds")
  409. }
  410. // AttemptsToDial returns the number of attempts to dial specific address. It
  411. // returns 0 if never attempted or successfully connected.
  412. func (r *PEXReactor) AttemptsToDial(addr *p2p.NetAddress) int {
  413. lAttempts, attempted := r.attemptsToDial.Load(addr.DialString())
  414. if attempted {
  415. return lAttempts.(_attemptsToDial).number
  416. }
  417. return 0
  418. }
  419. //----------------------------------------------------------
  420. // Explores the network searching for more peers. (continuous)
  421. // Seed/Crawler Mode causes this node to quickly disconnect
  422. // from peers, except other seed nodes.
  423. func (r *PEXReactor) crawlPeersRoutine() {
  424. // Do an initial crawl
  425. r.crawlPeers()
  426. // Fire periodically
  427. ticker := time.NewTicker(defaultCrawlPeersPeriod)
  428. for {
  429. select {
  430. case <-ticker.C:
  431. r.attemptDisconnects()
  432. r.crawlPeers()
  433. case <-r.Quit():
  434. return
  435. }
  436. }
  437. }
  438. // hasPotentialPeers indicates if there is a potential peer to connect to, by
  439. // consulting the Switch as well as the AddrBook.
  440. func (r *PEXReactor) hasPotentialPeers() bool {
  441. out, in, dial := r.Switch.NumPeers()
  442. return out+in+dial > 0 && len(r.book.ListOfKnownAddresses()) > 0
  443. }
  444. // crawlPeerInfo handles temporary data needed for the
  445. // network crawling performed during seed/crawler mode.
  446. type crawlPeerInfo struct {
  447. // The listening address of a potential peer we learned about
  448. Addr *p2p.NetAddress
  449. // The last time we attempt to reach this address
  450. LastAttempt time.Time
  451. // The last time we successfully reached this address
  452. LastSuccess time.Time
  453. }
  454. // oldestFirst implements sort.Interface for []crawlPeerInfo
  455. // based on the LastAttempt field.
  456. type oldestFirst []crawlPeerInfo
  457. func (of oldestFirst) Len() int { return len(of) }
  458. func (of oldestFirst) Swap(i, j int) { of[i], of[j] = of[j], of[i] }
  459. func (of oldestFirst) Less(i, j int) bool { return of[i].LastAttempt.Before(of[j].LastAttempt) }
  460. // getPeersToCrawl returns addresses of potential peers that we wish to validate.
  461. // NOTE: The status information is ordered as described above.
  462. func (r *PEXReactor) getPeersToCrawl() []crawlPeerInfo {
  463. var of oldestFirst
  464. // TODO: be more selective
  465. addrs := r.book.ListOfKnownAddresses()
  466. for _, addr := range addrs {
  467. if len(addr.ID()) == 0 {
  468. continue // dont use peers without id
  469. }
  470. of = append(of, crawlPeerInfo{
  471. Addr: addr.Addr,
  472. LastAttempt: addr.LastAttempt,
  473. LastSuccess: addr.LastSuccess,
  474. })
  475. }
  476. sort.Sort(of)
  477. return of
  478. }
  479. // crawlPeers will crawl the network looking for new peer addresses. (once)
  480. func (r *PEXReactor) crawlPeers() {
  481. peerInfos := r.getPeersToCrawl()
  482. now := time.Now()
  483. // Use addresses we know of to reach additional peers
  484. for _, pi := range peerInfos {
  485. // Do not attempt to connect with peers we recently dialed
  486. if now.Sub(pi.LastAttempt) < defaultCrawlPeerInterval {
  487. continue
  488. }
  489. // Otherwise, attempt to connect with the known address
  490. err := r.Switch.DialPeerWithAddress(pi.Addr, false)
  491. if err != nil {
  492. r.book.MarkAttempt(pi.Addr)
  493. continue
  494. }
  495. // Ask for more addresses
  496. peer := r.Switch.Peers().Get(pi.Addr.ID)
  497. r.RequestAddrs(peer)
  498. }
  499. }
  500. // attemptDisconnects checks if we've been with each peer long enough to disconnect
  501. func (r *PEXReactor) attemptDisconnects() {
  502. for _, peer := range r.Switch.Peers().List() {
  503. if peer.Status().Duration < defaultSeedDisconnectWaitPeriod {
  504. continue
  505. }
  506. if peer.IsPersistent() {
  507. continue
  508. }
  509. r.Switch.StopPeerGracefully(peer)
  510. }
  511. }
  512. // isAddrPrivate returns true if addr is private.
  513. func isAddrPrivate(addr *p2p.NetAddress, privatePeerIDs []string) bool {
  514. for _, id := range privatePeerIDs {
  515. if string(addr.ID) == id {
  516. return true
  517. }
  518. }
  519. return false
  520. }
  521. //-----------------------------------------------------------------------------
  522. // Messages
  523. // PexMessage is a primary type for PEX messages. Underneath, it could contain
  524. // either pexRequestMessage, or pexAddrsMessage messages.
  525. type PexMessage interface{}
  526. func RegisterPexMessage(cdc *amino.Codec) {
  527. cdc.RegisterInterface((*PexMessage)(nil), nil)
  528. cdc.RegisterConcrete(&pexRequestMessage{}, "tendermint/p2p/PexRequestMessage", nil)
  529. cdc.RegisterConcrete(&pexAddrsMessage{}, "tendermint/p2p/PexAddrsMessage", nil)
  530. }
  531. // DecodeMessage implements interface registered above.
  532. func DecodeMessage(bz []byte) (msg PexMessage, err error) {
  533. if len(bz) > maxMsgSize {
  534. return msg, fmt.Errorf("Msg exceeds max size (%d > %d)",
  535. len(bz), maxMsgSize)
  536. }
  537. err = cdc.UnmarshalBinary(bz, &msg)
  538. return
  539. }
  540. /*
  541. A pexRequestMessage requests additional peer addresses.
  542. */
  543. type pexRequestMessage struct {
  544. }
  545. func (m *pexRequestMessage) String() string {
  546. return "[pexRequest]"
  547. }
  548. /*
  549. A message with announced peer addresses.
  550. */
  551. type pexAddrsMessage struct {
  552. Addrs []*p2p.NetAddress
  553. }
  554. func (m *pexAddrsMessage) String() string {
  555. return fmt.Sprintf("[pexAddrs %v]", m.Addrs)
  556. }