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.

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