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.

551 lines
15 KiB

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