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.

628 lines
17 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. "bytes"
  4. "fmt"
  5. "math/rand"
  6. "reflect"
  7. "sort"
  8. "sync"
  9. "time"
  10. "github.com/pkg/errors"
  11. wire "github.com/tendermint/go-wire"
  12. cmn "github.com/tendermint/tmlibs/common"
  13. "github.com/tendermint/tendermint/p2p"
  14. "github.com/tendermint/tendermint/p2p/conn"
  15. )
  16. type Peer = p2p.Peer
  17. const (
  18. // PexChannel is a channel for PEX messages
  19. PexChannel = byte(0x00)
  20. maxPexMessageSize = 1048576 // 1MB
  21. // ensure we have enough peers
  22. defaultEnsurePeersPeriod = 30 * time.Second
  23. defaultMinNumOutboundPeers = 10
  24. // Seed/Crawler constants
  25. // TODO:
  26. // We want seeds to only advertise good peers.
  27. // Peers are marked by external mechanisms.
  28. // We need a config value that can be set to be
  29. // on the order of how long it would take before a good
  30. // peer is marked good.
  31. defaultSeedDisconnectWaitPeriod = 2 * time.Minute // disconnect after this
  32. defaultCrawlPeerInterval = 2 * time.Minute // dont redial for this. TODO: back-off
  33. defaultCrawlPeersPeriod = 30 * time.Second // check some peers every this
  34. maxAttemptsToDial = 16 // ~ 35h in total (last attempt - 18h)
  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. }
  63. type _attemptsToDial struct {
  64. number int
  65. lastDialed time.Time
  66. }
  67. // NewPEXReactor creates new PEX reactor.
  68. func NewPEXReactor(b AddrBook, config *PEXReactorConfig) *PEXReactor {
  69. r := &PEXReactor{
  70. book: b,
  71. config: config,
  72. ensurePeersPeriod: defaultEnsurePeersPeriod,
  73. requestsSent: cmn.NewCMap(),
  74. lastReceivedRequests: cmn.NewCMap(),
  75. }
  76. r.BaseReactor = *p2p.NewBaseReactor("PEXReactor", r)
  77. return r
  78. }
  79. // OnStart implements BaseService
  80. func (r *PEXReactor) OnStart() error {
  81. if err := r.BaseReactor.OnStart(); err != nil {
  82. return err
  83. }
  84. err := r.book.Start()
  85. if err != nil && err != cmn.ErrAlreadyStarted {
  86. return err
  87. }
  88. // return err if user provided a bad seed address
  89. if err := r.checkSeeds(); err != nil {
  90. return err
  91. }
  92. // Check if this node should run
  93. // in seed/crawler mode
  94. if r.config.SeedMode {
  95. go r.crawlPeersRoutine()
  96. } else {
  97. go r.ensurePeersRoutine()
  98. }
  99. return nil
  100. }
  101. // OnStop implements BaseService
  102. func (r *PEXReactor) OnStop() {
  103. r.BaseReactor.OnStop()
  104. r.book.Stop()
  105. }
  106. // GetChannels implements Reactor
  107. func (r *PEXReactor) GetChannels() []*conn.ChannelDescriptor {
  108. return []*conn.ChannelDescriptor{
  109. {
  110. ID: PexChannel,
  111. Priority: 1,
  112. SendQueueCapacity: 10,
  113. },
  114. }
  115. }
  116. // AddPeer implements Reactor by adding peer to the address book (if inbound)
  117. // or by requesting more addresses (if outbound).
  118. func (r *PEXReactor) AddPeer(p Peer) {
  119. if p.IsOutbound() {
  120. // For outbound peers, the address is already in the books -
  121. // either via DialPeersAsync or r.Receive.
  122. // Ask it for more peers if we need.
  123. if r.book.NeedMoreAddrs() {
  124. r.RequestAddrs(p)
  125. }
  126. } else {
  127. // For inbound peers, the peer is its own source,
  128. // and its NodeInfo has already been validated.
  129. // Let the ensurePeersRoutine handle asking for more
  130. // peers when we need - we don't trust inbound peers as much.
  131. addr := p.NodeInfo().NetAddress()
  132. r.book.AddAddress(addr, addr)
  133. }
  134. }
  135. // RemovePeer implements Reactor.
  136. func (r *PEXReactor) RemovePeer(p Peer, reason interface{}) {
  137. id := string(p.ID())
  138. r.requestsSent.Delete(id)
  139. r.lastReceivedRequests.Delete(id)
  140. }
  141. // Receive implements Reactor by handling incoming PEX messages.
  142. func (r *PEXReactor) Receive(chID byte, src Peer, msgBytes []byte) {
  143. _, msg, err := DecodeMessage(msgBytes)
  144. if err != nil {
  145. r.Logger.Error("Error decoding message", "err", err)
  146. return
  147. }
  148. r.Logger.Debug("Received message", "src", src, "chId", chID, "msg", msg)
  149. switch msg := msg.(type) {
  150. case *pexRequestMessage:
  151. // Check we're not receiving too many requests
  152. if err := r.receiveRequest(src); err != nil {
  153. r.Switch.StopPeerForError(src, err)
  154. return
  155. }
  156. // Seeds disconnect after sending a batch of addrs
  157. if r.config.SeedMode {
  158. // TODO: should we be more selective ?
  159. r.SendAddrs(src, r.book.GetSelection())
  160. r.Switch.StopPeerGracefully(src)
  161. } else {
  162. r.SendAddrs(src, r.book.GetSelection())
  163. }
  164. case *pexAddrsMessage:
  165. // If we asked for addresses, add them to the book
  166. if err := r.ReceiveAddrs(msg.Addrs, src); err != nil {
  167. r.Switch.StopPeerForError(src, err)
  168. return
  169. }
  170. default:
  171. r.Logger.Error(fmt.Sprintf("Unknown message type %v", reflect.TypeOf(msg)))
  172. }
  173. }
  174. func (r *PEXReactor) receiveRequest(src Peer) error {
  175. id := string(src.ID())
  176. v := r.lastReceivedRequests.Get(id)
  177. if v == nil {
  178. // initialize with empty time
  179. lastReceived := time.Time{}
  180. r.lastReceivedRequests.Set(id, lastReceived)
  181. return nil
  182. }
  183. lastReceived := v.(time.Time)
  184. if lastReceived.Equal(time.Time{}) {
  185. // first time gets a free pass. then we start tracking the time
  186. lastReceived = time.Now()
  187. r.lastReceivedRequests.Set(id, lastReceived)
  188. return nil
  189. }
  190. now := time.Now()
  191. if now.Sub(lastReceived) < r.ensurePeersPeriod/3 {
  192. return fmt.Errorf("Peer (%v) is sending too many PEX requests. Disconnecting", src.ID())
  193. }
  194. r.lastReceivedRequests.Set(id, now)
  195. return nil
  196. }
  197. // RequestAddrs asks peer for more addresses if we do not already
  198. // have a request out for this peer.
  199. func (r *PEXReactor) RequestAddrs(p Peer) {
  200. id := string(p.ID())
  201. if r.requestsSent.Has(id) {
  202. return
  203. }
  204. r.requestsSent.Set(id, struct{}{})
  205. p.Send(PexChannel, struct{ PexMessage }{&pexRequestMessage{}})
  206. }
  207. // ReceiveAddrs adds the given addrs to the addrbook if theres an open
  208. // request for this peer and deletes the open request.
  209. // If there's no open request for the src peer, it returns an error.
  210. func (r *PEXReactor) ReceiveAddrs(addrs []*p2p.NetAddress, src Peer) error {
  211. id := string(src.ID())
  212. if !r.requestsSent.Has(id) {
  213. return errors.New("Received unsolicited pexAddrsMessage")
  214. }
  215. r.requestsSent.Delete(id)
  216. srcAddr := src.NodeInfo().NetAddress()
  217. for _, netAddr := range addrs {
  218. if netAddr != nil {
  219. r.book.AddAddress(netAddr, srcAddr)
  220. }
  221. }
  222. return nil
  223. }
  224. // SendAddrs sends addrs to the peer.
  225. func (r *PEXReactor) SendAddrs(p Peer, netAddrs []*p2p.NetAddress) {
  226. p.Send(PexChannel, struct{ PexMessage }{&pexAddrsMessage{Addrs: netAddrs}})
  227. }
  228. // SetEnsurePeersPeriod sets period to ensure peers connected.
  229. func (r *PEXReactor) SetEnsurePeersPeriod(d time.Duration) {
  230. r.ensurePeersPeriod = d
  231. }
  232. // Ensures that sufficient peers are connected. (continuous)
  233. func (r *PEXReactor) ensurePeersRoutine() {
  234. var (
  235. seed = rand.New(rand.NewSource(time.Now().UnixNano()))
  236. jitter = seed.Int63n(r.ensurePeersPeriod.Nanoseconds())
  237. )
  238. // Randomize first round of communication to avoid thundering herd.
  239. // If no potential peers are present directly start connecting so we guarantee
  240. // swift setup with the help of configured seeds.
  241. if r.hasPotentialPeers() {
  242. time.Sleep(time.Duration(jitter))
  243. }
  244. // fire once immediately.
  245. // ensures we dial the seeds right away if the book is empty
  246. r.ensurePeers()
  247. // fire periodically
  248. ticker := time.NewTicker(r.ensurePeersPeriod)
  249. for {
  250. select {
  251. case <-ticker.C:
  252. r.ensurePeers()
  253. case <-r.Quit():
  254. ticker.Stop()
  255. return
  256. }
  257. }
  258. }
  259. // ensurePeers ensures that sufficient peers are connected. (once)
  260. //
  261. // heuristic that we haven't perfected yet, or, perhaps is manually edited by
  262. // the node operator. It should not be used to compute what addresses are
  263. // already connected or not.
  264. func (r *PEXReactor) ensurePeers() {
  265. var (
  266. out, in, dial = r.Switch.NumPeers()
  267. numToDial = defaultMinNumOutboundPeers - (out + dial)
  268. )
  269. r.Logger.Info(
  270. "Ensure peers",
  271. "numOutPeers", out,
  272. "numInPeers", in,
  273. "numDialing", dial,
  274. "numToDial", numToDial,
  275. )
  276. if numToDial <= 0 {
  277. return
  278. }
  279. // bias to prefer more vetted peers when we have fewer connections.
  280. // not perfect, but somewhate ensures that we prioritize connecting to more-vetted
  281. // NOTE: range here is [10, 90]. Too high ?
  282. newBias := cmn.MinInt(out, 8)*10 + 10
  283. toDial := make(map[p2p.ID]*p2p.NetAddress)
  284. // Try maxAttempts times to pick numToDial addresses to dial
  285. maxAttempts := numToDial * 3
  286. for i := 0; i < maxAttempts && len(toDial) < numToDial; i++ {
  287. try := r.book.PickAddress(newBias)
  288. if try == nil {
  289. continue
  290. }
  291. if _, selected := toDial[try.ID]; selected {
  292. continue
  293. }
  294. if dialling := r.Switch.IsDialing(try.ID); dialling {
  295. continue
  296. }
  297. if connected := r.Switch.Peers().Has(try.ID); connected {
  298. continue
  299. }
  300. r.Logger.Info("Will dial address", "addr", try)
  301. toDial[try.ID] = try
  302. }
  303. // Dial picked addresses
  304. for _, addr := range toDial {
  305. go r.dialPeer(addr)
  306. }
  307. // If we need more addresses, pick a random peer and ask for more.
  308. if r.book.NeedMoreAddrs() {
  309. peers := r.Switch.Peers().List()
  310. peersCount := len(peers)
  311. if peersCount > 0 {
  312. peer := peers[rand.Int()%peersCount] // nolint: gas
  313. r.Logger.Info("We need more addresses. Sending pexRequest to random peer", "peer", peer)
  314. r.RequestAddrs(peer)
  315. }
  316. }
  317. // If we are not connected to nor dialing anybody, fallback to dialing a seed.
  318. if out+in+dial+len(toDial) == 0 {
  319. r.Logger.Info("No addresses to dial nor connected peers. Falling back to seeds")
  320. r.dialSeeds()
  321. }
  322. }
  323. func (r *PEXReactor) dialPeer(addr *p2p.NetAddress) {
  324. var attempts int
  325. var lastDialed time.Time
  326. if lAttempts, attempted := r.attemptsToDial.Load(addr.DialString()); attempted {
  327. attempts = lAttempts.(_attemptsToDial).number
  328. lastDialed = lAttempts.(_attemptsToDial).lastDialed
  329. }
  330. if attempts > maxAttemptsToDial {
  331. r.Logger.Error("Reached max attempts to dial", "addr", addr, "attempts", attempts)
  332. r.book.MarkBad(addr)
  333. return
  334. }
  335. // exponential backoff if it's not our first attempt to dial given address
  336. if attempts > 0 {
  337. jitterSeconds := time.Duration(rand.Float64() * float64(time.Second)) // 1s == (1e9 ns)
  338. backoffDuration := jitterSeconds + ((1 << uint(attempts)) * time.Second)
  339. sinceLastDialed := time.Since(lastDialed)
  340. if sinceLastDialed < backoffDuration {
  341. r.Logger.Debug("Too early to dial", "addr", addr, "backoff_duration", backoffDuration, "last_dialed", lastDialed, "time_since", sinceLastDialed)
  342. return
  343. }
  344. }
  345. err := r.Switch.DialPeerWithAddress(addr, false)
  346. if err != nil {
  347. r.Logger.Error("Dialing failed", "addr", addr, "err", err, "attempts", attempts)
  348. // TODO: detect more "bad peer" scenarios
  349. if _, ok := err.(p2p.ErrSwitchAuthenticationFailure); ok {
  350. r.book.MarkBad(addr)
  351. } else {
  352. r.book.MarkAttempt(addr)
  353. }
  354. // record attempt
  355. r.attemptsToDial.Store(addr.DialString(), _attemptsToDial{attempts + 1, time.Now()})
  356. } else {
  357. // cleanup any history
  358. r.attemptsToDial.Delete(addr.DialString())
  359. }
  360. }
  361. // check seed addresses are well formed
  362. func (r *PEXReactor) checkSeeds() error {
  363. lSeeds := len(r.config.Seeds)
  364. if lSeeds == 0 {
  365. return nil
  366. }
  367. _, errs := p2p.NewNetAddressStrings(r.config.Seeds)
  368. for _, err := range errs {
  369. if err != nil {
  370. return err
  371. }
  372. }
  373. return nil
  374. }
  375. // randomly dial seeds until we connect to one or exhaust them
  376. func (r *PEXReactor) dialSeeds() {
  377. lSeeds := len(r.config.Seeds)
  378. if lSeeds == 0 {
  379. return
  380. }
  381. seedAddrs, _ := p2p.NewNetAddressStrings(r.config.Seeds)
  382. perm := rand.Perm(lSeeds)
  383. // perm := r.Switch.rng.Perm(lSeeds)
  384. for _, i := range perm {
  385. // dial a random seed
  386. seedAddr := seedAddrs[i]
  387. err := r.Switch.DialPeerWithAddress(seedAddr, false)
  388. if err == nil {
  389. return
  390. }
  391. r.Switch.Logger.Error("Error dialing seed", "err", err, "seed", seedAddr)
  392. }
  393. r.Switch.Logger.Error("Couldn't connect to any seeds")
  394. }
  395. // AttemptsToDial returns the number of attempts to dial specific address. It
  396. // returns 0 if never attempted or successfully connected.
  397. func (r *PEXReactor) AttemptsToDial(addr *p2p.NetAddress) int {
  398. lAttempts, attempted := r.attemptsToDial.Load(addr.DialString())
  399. if attempted {
  400. return lAttempts.(_attemptsToDial).number
  401. } else {
  402. return 0
  403. }
  404. }
  405. //----------------------------------------------------------
  406. // Explores the network searching for more peers. (continuous)
  407. // Seed/Crawler Mode causes this node to quickly disconnect
  408. // from peers, except other seed nodes.
  409. func (r *PEXReactor) crawlPeersRoutine() {
  410. // Do an initial crawl
  411. r.crawlPeers()
  412. // Fire periodically
  413. ticker := time.NewTicker(defaultCrawlPeersPeriod)
  414. for {
  415. select {
  416. case <-ticker.C:
  417. r.attemptDisconnects()
  418. r.crawlPeers()
  419. case <-r.Quit():
  420. return
  421. }
  422. }
  423. }
  424. // hasPotentialPeers indicates if there is a potential peer to connect to, by
  425. // consulting the Switch as well as the AddrBook.
  426. func (r *PEXReactor) hasPotentialPeers() bool {
  427. out, in, dial := r.Switch.NumPeers()
  428. return out+in+dial > 0 && len(r.book.ListOfKnownAddresses()) > 0
  429. }
  430. // crawlPeerInfo handles temporary data needed for the
  431. // network crawling performed during seed/crawler mode.
  432. type crawlPeerInfo struct {
  433. // The listening address of a potential peer we learned about
  434. Addr *p2p.NetAddress
  435. // The last time we attempt to reach this address
  436. LastAttempt time.Time
  437. // The last time we successfully reached this address
  438. LastSuccess time.Time
  439. }
  440. // oldestFirst implements sort.Interface for []crawlPeerInfo
  441. // based on the LastAttempt field.
  442. type oldestFirst []crawlPeerInfo
  443. func (of oldestFirst) Len() int { return len(of) }
  444. func (of oldestFirst) Swap(i, j int) { of[i], of[j] = of[j], of[i] }
  445. func (of oldestFirst) Less(i, j int) bool { return of[i].LastAttempt.Before(of[j].LastAttempt) }
  446. // getPeersToCrawl returns addresses of potential peers that we wish to validate.
  447. // NOTE: The status information is ordered as described above.
  448. func (r *PEXReactor) getPeersToCrawl() []crawlPeerInfo {
  449. var of oldestFirst
  450. // TODO: be more selective
  451. addrs := r.book.ListOfKnownAddresses()
  452. for _, addr := range addrs {
  453. if len(addr.ID()) == 0 {
  454. continue // dont use peers without id
  455. }
  456. of = append(of, crawlPeerInfo{
  457. Addr: addr.Addr,
  458. LastAttempt: addr.LastAttempt,
  459. LastSuccess: addr.LastSuccess,
  460. })
  461. }
  462. sort.Sort(of)
  463. return of
  464. }
  465. // crawlPeers will crawl the network looking for new peer addresses. (once)
  466. func (r *PEXReactor) crawlPeers() {
  467. peerInfos := r.getPeersToCrawl()
  468. now := time.Now()
  469. // Use addresses we know of to reach additional peers
  470. for _, pi := range peerInfos {
  471. // Do not attempt to connect with peers we recently dialed
  472. if now.Sub(pi.LastAttempt) < defaultCrawlPeerInterval {
  473. continue
  474. }
  475. // Otherwise, attempt to connect with the known address
  476. err := r.Switch.DialPeerWithAddress(pi.Addr, false)
  477. if err != nil {
  478. r.book.MarkAttempt(pi.Addr)
  479. continue
  480. }
  481. }
  482. // Crawl the connected peers asking for more addresses
  483. for _, pi := range peerInfos {
  484. // We will wait a minimum period of time before crawling peers again
  485. if now.Sub(pi.LastAttempt) >= defaultCrawlPeerInterval {
  486. peer := r.Switch.Peers().Get(pi.Addr.ID)
  487. if peer != nil {
  488. r.RequestAddrs(peer)
  489. }
  490. }
  491. }
  492. }
  493. // attemptDisconnects checks if we've been with each peer long enough to disconnect
  494. func (r *PEXReactor) attemptDisconnects() {
  495. for _, peer := range r.Switch.Peers().List() {
  496. status := peer.Status()
  497. if status.Duration < defaultSeedDisconnectWaitPeriod {
  498. continue
  499. }
  500. if peer.IsPersistent() {
  501. continue
  502. }
  503. r.Switch.StopPeerGracefully(peer)
  504. }
  505. }
  506. //-----------------------------------------------------------------------------
  507. // Messages
  508. const (
  509. msgTypeRequest = byte(0x01)
  510. msgTypeAddrs = byte(0x02)
  511. )
  512. // PexMessage is a primary type for PEX messages. Underneath, it could contain
  513. // either pexRequestMessage, or pexAddrsMessage messages.
  514. type PexMessage interface{}
  515. var _ = wire.RegisterInterface(
  516. struct{ PexMessage }{},
  517. wire.ConcreteType{&pexRequestMessage{}, msgTypeRequest},
  518. wire.ConcreteType{&pexAddrsMessage{}, msgTypeAddrs},
  519. )
  520. // DecodeMessage implements interface registered above.
  521. func DecodeMessage(bz []byte) (msgType byte, msg PexMessage, err error) {
  522. msgType = bz[0]
  523. n := new(int)
  524. r := bytes.NewReader(bz)
  525. msg = wire.ReadBinary(struct{ PexMessage }{}, r, maxPexMessageSize, n, &err).(struct{ PexMessage }).PexMessage
  526. return
  527. }
  528. /*
  529. A pexRequestMessage requests additional peer addresses.
  530. */
  531. type pexRequestMessage struct {
  532. }
  533. func (m *pexRequestMessage) String() string {
  534. return "[pexRequest]"
  535. }
  536. /*
  537. A message with announced peer addresses.
  538. */
  539. type pexAddrsMessage struct {
  540. Addrs []*p2p.NetAddress
  541. }
  542. func (m *pexAddrsMessage) String() string {
  543. return fmt.Sprintf("[pexAddrs %v]", m.Addrs)
  544. }