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.

573 lines
14 KiB

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
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
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
8 years ago
8 years ago
8 years ago
8 years ago
  1. package p2p
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/rand"
  6. "net"
  7. "time"
  8. crypto "github.com/tendermint/go-crypto"
  9. "github.com/tendermint/log15"
  10. cfg "github.com/tendermint/tendermint/config"
  11. cmn "github.com/tendermint/tmlibs/common"
  12. )
  13. const (
  14. reconnectAttempts = 30
  15. reconnectInterval = 3 * time.Second
  16. )
  17. type Reactor interface {
  18. cmn.Service // Start, Stop
  19. SetSwitch(*Switch)
  20. GetChannels() []*ChannelDescriptor
  21. AddPeer(peer *Peer)
  22. RemovePeer(peer *Peer, reason interface{})
  23. Receive(chID byte, peer *Peer, msgBytes []byte)
  24. }
  25. //--------------------------------------
  26. type BaseReactor struct {
  27. cmn.BaseService // Provides Start, Stop, .Quit
  28. Switch *Switch
  29. }
  30. func NewBaseReactor(log log15.Logger, name string, impl Reactor) *BaseReactor {
  31. return &BaseReactor{
  32. BaseService: *cmn.NewBaseService(log, name, impl),
  33. Switch: nil,
  34. }
  35. }
  36. func (br *BaseReactor) SetSwitch(sw *Switch) {
  37. br.Switch = sw
  38. }
  39. func (_ *BaseReactor) GetChannels() []*ChannelDescriptor { return nil }
  40. func (_ *BaseReactor) AddPeer(peer *Peer) {}
  41. func (_ *BaseReactor) RemovePeer(peer *Peer, reason interface{}) {}
  42. func (_ *BaseReactor) Receive(chID byte, peer *Peer, msgBytes []byte) {}
  43. //-----------------------------------------------------------------------------
  44. /*
  45. The `Switch` handles peer connections and exposes an API to receive incoming messages
  46. on `Reactors`. Each `Reactor` is responsible for handling incoming messages of one
  47. or more `Channels`. So while sending outgoing messages is typically performed on the peer,
  48. incoming messages are received on the reactor.
  49. */
  50. type Switch struct {
  51. cmn.BaseService
  52. config *cfg.P2PConfig
  53. peerConfig *PeerConfig
  54. listeners []Listener
  55. reactors map[string]Reactor
  56. chDescs []*ChannelDescriptor
  57. reactorsByCh map[byte]Reactor
  58. peers *PeerSet
  59. dialing *cmn.CMap
  60. nodeInfo *NodeInfo // our node info
  61. nodePrivKey crypto.PrivKeyEd25519 // our node privkey
  62. filterConnByAddr func(net.Addr) error
  63. filterConnByPubKey func(crypto.PubKeyEd25519) error
  64. }
  65. var (
  66. ErrSwitchDuplicatePeer = errors.New("Duplicate peer")
  67. ErrSwitchMaxPeersPerIPRange = errors.New("IP range has too many peers")
  68. )
  69. func NewSwitch(config *cfg.P2PConfig) *Switch {
  70. sw := &Switch{
  71. config: config,
  72. peerConfig: DefaultPeerConfig(),
  73. reactors: make(map[string]Reactor),
  74. chDescs: make([]*ChannelDescriptor, 0),
  75. reactorsByCh: make(map[byte]Reactor),
  76. peers: NewPeerSet(),
  77. dialing: cmn.NewCMap(),
  78. nodeInfo: nil,
  79. }
  80. sw.BaseService = *cmn.NewBaseService(log, "P2P Switch", sw)
  81. return sw
  82. }
  83. // Not goroutine safe.
  84. func (sw *Switch) AddReactor(name string, reactor Reactor) Reactor {
  85. // Validate the reactor.
  86. // No two reactors can share the same channel.
  87. reactorChannels := reactor.GetChannels()
  88. for _, chDesc := range reactorChannels {
  89. chID := chDesc.ID
  90. if sw.reactorsByCh[chID] != nil {
  91. cmn.PanicSanity(fmt.Sprintf("Channel %X has multiple reactors %v & %v", chID, sw.reactorsByCh[chID], reactor))
  92. }
  93. sw.chDescs = append(sw.chDescs, chDesc)
  94. sw.reactorsByCh[chID] = reactor
  95. }
  96. sw.reactors[name] = reactor
  97. reactor.SetSwitch(sw)
  98. return reactor
  99. }
  100. // Not goroutine safe.
  101. func (sw *Switch) Reactors() map[string]Reactor {
  102. return sw.reactors
  103. }
  104. // Not goroutine safe.
  105. func (sw *Switch) Reactor(name string) Reactor {
  106. return sw.reactors[name]
  107. }
  108. // Not goroutine safe.
  109. func (sw *Switch) AddListener(l Listener) {
  110. sw.listeners = append(sw.listeners, l)
  111. }
  112. // Not goroutine safe.
  113. func (sw *Switch) Listeners() []Listener {
  114. return sw.listeners
  115. }
  116. // Not goroutine safe.
  117. func (sw *Switch) IsListening() bool {
  118. return len(sw.listeners) > 0
  119. }
  120. // Not goroutine safe.
  121. func (sw *Switch) SetNodeInfo(nodeInfo *NodeInfo) {
  122. sw.nodeInfo = nodeInfo
  123. }
  124. // Not goroutine safe.
  125. func (sw *Switch) NodeInfo() *NodeInfo {
  126. return sw.nodeInfo
  127. }
  128. // Not goroutine safe.
  129. // NOTE: Overwrites sw.nodeInfo.PubKey
  130. func (sw *Switch) SetNodePrivKey(nodePrivKey crypto.PrivKeyEd25519) {
  131. sw.nodePrivKey = nodePrivKey
  132. if sw.nodeInfo != nil {
  133. sw.nodeInfo.PubKey = nodePrivKey.PubKey().Unwrap().(crypto.PubKeyEd25519)
  134. }
  135. }
  136. // Switch.Start() starts all the reactors, peers, and listeners.
  137. func (sw *Switch) OnStart() error {
  138. sw.BaseService.OnStart()
  139. // Start reactors
  140. for _, reactor := range sw.reactors {
  141. _, err := reactor.Start()
  142. if err != nil {
  143. return err
  144. }
  145. }
  146. // Start peers
  147. for _, peer := range sw.peers.List() {
  148. sw.startInitPeer(peer)
  149. }
  150. // Start listeners
  151. for _, listener := range sw.listeners {
  152. go sw.listenerRoutine(listener)
  153. }
  154. return nil
  155. }
  156. func (sw *Switch) OnStop() {
  157. sw.BaseService.OnStop()
  158. // Stop listeners
  159. for _, listener := range sw.listeners {
  160. listener.Stop()
  161. }
  162. sw.listeners = nil
  163. // Stop peers
  164. for _, peer := range sw.peers.List() {
  165. peer.Stop()
  166. sw.peers.Remove(peer)
  167. }
  168. // Stop reactors
  169. for _, reactor := range sw.reactors {
  170. reactor.Stop()
  171. }
  172. }
  173. // NOTE: This performs a blocking handshake before the peer is added.
  174. // CONTRACT: If error is returned, peer is nil, and conn is immediately closed.
  175. func (sw *Switch) AddPeer(peer *Peer) error {
  176. if err := sw.FilterConnByAddr(peer.Addr()); err != nil {
  177. return err
  178. }
  179. if err := sw.FilterConnByPubKey(peer.PubKey()); err != nil {
  180. return err
  181. }
  182. if err := peer.HandshakeTimeout(sw.nodeInfo, time.Duration(sw.peerConfig.HandshakeTimeout*time.Second)); err != nil {
  183. return err
  184. }
  185. // Avoid self
  186. if sw.nodeInfo.PubKey.Equals(peer.PubKey().Wrap()) {
  187. return errors.New("Ignoring connection from self")
  188. }
  189. // Check version, chain id
  190. if err := sw.nodeInfo.CompatibleWith(peer.NodeInfo); err != nil {
  191. return err
  192. }
  193. // Add the peer to .peers
  194. // ignore if duplicate or if we already have too many for that IP range
  195. if err := sw.peers.Add(peer); err != nil {
  196. log.Notice("Ignoring peer", "error", err, "peer", peer)
  197. peer.Stop()
  198. return err
  199. }
  200. // Start peer
  201. if sw.IsRunning() {
  202. sw.startInitPeer(peer)
  203. }
  204. log.Notice("Added peer", "peer", peer)
  205. return nil
  206. }
  207. func (sw *Switch) FilterConnByAddr(addr net.Addr) error {
  208. if sw.filterConnByAddr != nil {
  209. return sw.filterConnByAddr(addr)
  210. }
  211. return nil
  212. }
  213. func (sw *Switch) FilterConnByPubKey(pubkey crypto.PubKeyEd25519) error {
  214. if sw.filterConnByPubKey != nil {
  215. return sw.filterConnByPubKey(pubkey)
  216. }
  217. return nil
  218. }
  219. func (sw *Switch) SetAddrFilter(f func(net.Addr) error) {
  220. sw.filterConnByAddr = f
  221. }
  222. func (sw *Switch) SetPubKeyFilter(f func(crypto.PubKeyEd25519) error) {
  223. sw.filterConnByPubKey = f
  224. }
  225. func (sw *Switch) startInitPeer(peer *Peer) {
  226. peer.Start() // spawn send/recv routines
  227. for _, reactor := range sw.reactors {
  228. reactor.AddPeer(peer)
  229. }
  230. }
  231. // Dial a list of seeds asynchronously in random order
  232. func (sw *Switch) DialSeeds(addrBook *AddrBook, seeds []string) error {
  233. netAddrs, err := NewNetAddressStrings(seeds)
  234. if err != nil {
  235. return err
  236. }
  237. if addrBook != nil {
  238. // add seeds to `addrBook`
  239. ourAddrS := sw.nodeInfo.ListenAddr
  240. ourAddr, _ := NewNetAddressString(ourAddrS)
  241. for _, netAddr := range netAddrs {
  242. // do not add ourselves
  243. if netAddr.Equals(ourAddr) {
  244. continue
  245. }
  246. addrBook.AddAddress(netAddr, ourAddr)
  247. }
  248. addrBook.Save()
  249. }
  250. // permute the list, dial them in random order.
  251. perm := rand.Perm(len(netAddrs))
  252. for i := 0; i < len(perm); i++ {
  253. go func(i int) {
  254. time.Sleep(time.Duration(rand.Int63n(3000)) * time.Millisecond)
  255. j := perm[i]
  256. sw.dialSeed(netAddrs[j])
  257. }(i)
  258. }
  259. return nil
  260. }
  261. func (sw *Switch) dialSeed(addr *NetAddress) {
  262. peer, err := sw.DialPeerWithAddress(addr, true)
  263. if err != nil {
  264. log.Error("Error dialing seed", "error", err)
  265. return
  266. } else {
  267. log.Notice("Connected to seed", "peer", peer)
  268. }
  269. }
  270. func (sw *Switch) DialPeerWithAddress(addr *NetAddress, persistent bool) (*Peer, error) {
  271. sw.dialing.Set(addr.IP.String(), addr)
  272. defer sw.dialing.Delete(addr.IP.String())
  273. peer, err := newOutboundPeerWithConfig(addr, sw.reactorsByCh, sw.chDescs, sw.StopPeerForError, sw.nodePrivKey, sw.peerConfig)
  274. if err != nil {
  275. log.Info("Failed dialing peer", "address", addr, "error", err)
  276. return nil, err
  277. }
  278. if persistent {
  279. peer.makePersistent()
  280. }
  281. err = sw.AddPeer(peer)
  282. if err != nil {
  283. log.Info("Failed adding peer", "address", addr, "error", err)
  284. peer.CloseConn()
  285. return nil, err
  286. }
  287. log.Notice("Dialed and added peer", "address", addr, "peer", peer)
  288. return peer, nil
  289. }
  290. func (sw *Switch) IsDialing(addr *NetAddress) bool {
  291. return sw.dialing.Has(addr.IP.String())
  292. }
  293. // Broadcast runs a go routine for each attempted send, which will block
  294. // trying to send for defaultSendTimeoutSeconds. Returns a channel
  295. // which receives success values for each attempted send (false if times out)
  296. // NOTE: Broadcast uses goroutines, so order of broadcast may not be preserved.
  297. func (sw *Switch) Broadcast(chID byte, msg interface{}) chan bool {
  298. successChan := make(chan bool, len(sw.peers.List()))
  299. log.Debug("Broadcast", "channel", chID, "msg", msg)
  300. for _, peer := range sw.peers.List() {
  301. go func(peer *Peer) {
  302. success := peer.Send(chID, msg)
  303. successChan <- success
  304. }(peer)
  305. }
  306. return successChan
  307. }
  308. // Returns the count of outbound/inbound and outbound-dialing peers.
  309. func (sw *Switch) NumPeers() (outbound, inbound, dialing int) {
  310. peers := sw.peers.List()
  311. for _, peer := range peers {
  312. if peer.outbound {
  313. outbound++
  314. } else {
  315. inbound++
  316. }
  317. }
  318. dialing = sw.dialing.Size()
  319. return
  320. }
  321. func (sw *Switch) Peers() IPeerSet {
  322. return sw.peers
  323. }
  324. // Disconnect from a peer due to external error, retry if it is a persistent peer.
  325. // TODO: make record depending on reason.
  326. func (sw *Switch) StopPeerForError(peer *Peer, reason interface{}) {
  327. addr := NewNetAddress(peer.Addr())
  328. log.Notice("Stopping peer for error", "peer", peer, "error", reason)
  329. sw.stopAndRemovePeer(peer, reason)
  330. if peer.IsPersistent() {
  331. go func() {
  332. log.Notice("Reconnecting to peer", "peer", peer)
  333. for i := 1; i < reconnectAttempts; i++ {
  334. if !sw.IsRunning() {
  335. return
  336. }
  337. peer, err := sw.DialPeerWithAddress(addr, true)
  338. if err != nil {
  339. if i == reconnectAttempts {
  340. log.Notice("Error reconnecting to peer. Giving up", "tries", i, "error", err)
  341. return
  342. }
  343. log.Notice("Error reconnecting to peer. Trying again", "tries", i, "error", err)
  344. time.Sleep(reconnectInterval)
  345. continue
  346. }
  347. log.Notice("Reconnected to peer", "peer", peer)
  348. return
  349. }
  350. }()
  351. }
  352. }
  353. // Disconnect from a peer gracefully.
  354. // TODO: handle graceful disconnects.
  355. func (sw *Switch) StopPeerGracefully(peer *Peer) {
  356. log.Notice("Stopping peer gracefully")
  357. sw.stopAndRemovePeer(peer, nil)
  358. }
  359. func (sw *Switch) stopAndRemovePeer(peer *Peer, reason interface{}) {
  360. sw.peers.Remove(peer)
  361. peer.Stop()
  362. for _, reactor := range sw.reactors {
  363. reactor.RemovePeer(peer, reason)
  364. }
  365. }
  366. func (sw *Switch) listenerRoutine(l Listener) {
  367. for {
  368. inConn, ok := <-l.Connections()
  369. if !ok {
  370. break
  371. }
  372. // ignore connection if we already have enough
  373. maxPeers := sw.config.MaxNumPeers
  374. if maxPeers <= sw.peers.Size() {
  375. log.Info("Ignoring inbound connection: already have enough peers", "address", inConn.RemoteAddr().String(), "numPeers", sw.peers.Size(), "max", maxPeers)
  376. continue
  377. }
  378. // New inbound connection!
  379. err := sw.addPeerWithConnectionAndConfig(inConn, sw.peerConfig)
  380. if err != nil {
  381. log.Notice("Ignoring inbound connection: error while adding peer", "address", inConn.RemoteAddr().String(), "error", err)
  382. continue
  383. }
  384. // NOTE: We don't yet have the listening port of the
  385. // remote (if they have a listener at all).
  386. // The peerHandshake will handle that
  387. }
  388. // cleanup
  389. }
  390. //-----------------------------------------------------------------------------
  391. type SwitchEventNewPeer struct {
  392. Peer *Peer
  393. }
  394. type SwitchEventDonePeer struct {
  395. Peer *Peer
  396. Error interface{}
  397. }
  398. //------------------------------------------------------------------
  399. // Switches connected via arbitrary net.Conn; useful for testing
  400. // Returns n switches, connected according to the connect func.
  401. // If connect==Connect2Switches, the switches will be fully connected.
  402. // initSwitch defines how the ith switch should be initialized (ie. with what reactors).
  403. // NOTE: panics if any switch fails to start.
  404. func MakeConnectedSwitches(cfg *cfg.P2PConfig, n int, initSwitch func(int, *Switch) *Switch, connect func([]*Switch, int, int)) []*Switch {
  405. switches := make([]*Switch, n)
  406. for i := 0; i < n; i++ {
  407. switches[i] = makeSwitch(cfg, i, "testing", "123.123.123", initSwitch)
  408. }
  409. if err := StartSwitches(switches); err != nil {
  410. panic(err)
  411. }
  412. for i := 0; i < n; i++ {
  413. for j := i; j < n; j++ {
  414. connect(switches, i, j)
  415. }
  416. }
  417. return switches
  418. }
  419. var PanicOnAddPeerErr = false
  420. // Will connect switches i and j via net.Pipe()
  421. // Blocks until a conection is established.
  422. // NOTE: caller ensures i and j are within bounds
  423. func Connect2Switches(switches []*Switch, i, j int) {
  424. switchI := switches[i]
  425. switchJ := switches[j]
  426. c1, c2 := net.Pipe()
  427. doneCh := make(chan struct{})
  428. go func() {
  429. err := switchI.addPeerWithConnection(c1)
  430. if PanicOnAddPeerErr && err != nil {
  431. panic(err)
  432. }
  433. doneCh <- struct{}{}
  434. }()
  435. go func() {
  436. err := switchJ.addPeerWithConnection(c2)
  437. if PanicOnAddPeerErr && err != nil {
  438. panic(err)
  439. }
  440. doneCh <- struct{}{}
  441. }()
  442. <-doneCh
  443. <-doneCh
  444. }
  445. func StartSwitches(switches []*Switch) error {
  446. for _, s := range switches {
  447. _, err := s.Start() // start switch and reactors
  448. if err != nil {
  449. return err
  450. }
  451. }
  452. return nil
  453. }
  454. func makeSwitch(cfg *cfg.P2PConfig, i int, network, version string, initSwitch func(int, *Switch) *Switch) *Switch {
  455. privKey := crypto.GenPrivKeyEd25519()
  456. // new switch, add reactors
  457. // TODO: let the config be passed in?
  458. s := initSwitch(i, NewSwitch(cfg))
  459. s.SetNodeInfo(&NodeInfo{
  460. PubKey: privKey.PubKey().Unwrap().(crypto.PubKeyEd25519),
  461. Moniker: cmn.Fmt("switch%d", i),
  462. Network: network,
  463. Version: version,
  464. RemoteAddr: cmn.Fmt("%v:%v", network, rand.Intn(64512)+1023),
  465. ListenAddr: cmn.Fmt("%v:%v", network, rand.Intn(64512)+1023),
  466. })
  467. s.SetNodePrivKey(privKey)
  468. return s
  469. }
  470. func (sw *Switch) addPeerWithConnection(conn net.Conn) error {
  471. peer, err := newInboundPeer(conn, sw.reactorsByCh, sw.chDescs, sw.StopPeerForError, sw.nodePrivKey)
  472. if err != nil {
  473. conn.Close()
  474. return err
  475. }
  476. if err = sw.AddPeer(peer); err != nil {
  477. conn.Close()
  478. return err
  479. }
  480. return nil
  481. }
  482. func (sw *Switch) addPeerWithConnectionAndConfig(conn net.Conn, config *PeerConfig) error {
  483. peer, err := newInboundPeerWithConfig(conn, sw.reactorsByCh, sw.chDescs, sw.StopPeerForError, sw.nodePrivKey, config)
  484. if err != nil {
  485. conn.Close()
  486. return err
  487. }
  488. if err = sw.AddPeer(peer); err != nil {
  489. conn.Close()
  490. return err
  491. }
  492. return nil
  493. }