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.

680 lines
19 KiB

p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
  1. package p2p
  2. import (
  3. "fmt"
  4. "math"
  5. "sync"
  6. "time"
  7. "github.com/tendermint/tendermint/config"
  8. cmn "github.com/tendermint/tendermint/libs/common"
  9. "github.com/tendermint/tendermint/p2p/conn"
  10. )
  11. const (
  12. // wait a random amount of time from this interval
  13. // before dialing peers or reconnecting to help prevent DoS
  14. dialRandomizerIntervalMilliseconds = 3000
  15. // repeatedly try to reconnect for a few minutes
  16. // ie. 5 * 20 = 100s
  17. reconnectAttempts = 20
  18. reconnectInterval = 5 * time.Second
  19. // then move into exponential backoff mode for ~1day
  20. // ie. 3**10 = 16hrs
  21. reconnectBackOffAttempts = 10
  22. reconnectBackOffBaseSeconds = 3
  23. )
  24. // MConnConfig returns an MConnConfig with fields updated
  25. // from the P2PConfig.
  26. func MConnConfig(cfg *config.P2PConfig) conn.MConnConfig {
  27. mConfig := conn.DefaultMConnConfig()
  28. mConfig.FlushThrottle = cfg.FlushThrottleTimeout
  29. mConfig.SendRate = cfg.SendRate
  30. mConfig.RecvRate = cfg.RecvRate
  31. mConfig.MaxPacketMsgPayloadSize = cfg.MaxPacketMsgPayloadSize
  32. return mConfig
  33. }
  34. //-----------------------------------------------------------------------------
  35. // An AddrBook represents an address book from the pex package, which is used
  36. // to store peer addresses.
  37. type AddrBook interface {
  38. AddAddress(addr *NetAddress, src *NetAddress) error
  39. AddOurAddress(*NetAddress)
  40. OurAddress(*NetAddress) bool
  41. MarkGood(*NetAddress)
  42. RemoveAddress(*NetAddress)
  43. HasAddress(*NetAddress) bool
  44. Save()
  45. }
  46. // PeerFilterFunc to be implemented by filter hooks after a new Peer has been
  47. // fully setup.
  48. type PeerFilterFunc func(IPeerSet, Peer) error
  49. //-----------------------------------------------------------------------------
  50. // Switch handles peer connections and exposes an API to receive incoming messages
  51. // on `Reactors`. Each `Reactor` is responsible for handling incoming messages of one
  52. // or more `Channels`. So while sending outgoing messages is typically performed on the peer,
  53. // incoming messages are received on the reactor.
  54. type Switch struct {
  55. cmn.BaseService
  56. config *config.P2PConfig
  57. reactors map[string]Reactor
  58. chDescs []*conn.ChannelDescriptor
  59. reactorsByCh map[byte]Reactor
  60. peers *PeerSet
  61. dialing *cmn.CMap
  62. reconnecting *cmn.CMap
  63. nodeInfo NodeInfo // our node info
  64. nodeKey *NodeKey // our node privkey
  65. addrBook AddrBook
  66. transport Transport
  67. filterTimeout time.Duration
  68. peerFilters []PeerFilterFunc
  69. rng *cmn.Rand // seed for randomizing dial times and orders
  70. metrics *Metrics
  71. }
  72. // SwitchOption sets an optional parameter on the Switch.
  73. type SwitchOption func(*Switch)
  74. // NewSwitch creates a new Switch with the given config.
  75. func NewSwitch(
  76. cfg *config.P2PConfig,
  77. transport Transport,
  78. options ...SwitchOption,
  79. ) *Switch {
  80. sw := &Switch{
  81. config: cfg,
  82. reactors: make(map[string]Reactor),
  83. chDescs: make([]*conn.ChannelDescriptor, 0),
  84. reactorsByCh: make(map[byte]Reactor),
  85. peers: NewPeerSet(),
  86. dialing: cmn.NewCMap(),
  87. reconnecting: cmn.NewCMap(),
  88. metrics: NopMetrics(),
  89. transport: transport,
  90. filterTimeout: defaultFilterTimeout,
  91. }
  92. // Ensure we have a completely undeterministic PRNG.
  93. sw.rng = cmn.NewRand()
  94. sw.BaseService = *cmn.NewBaseService(nil, "P2P Switch", sw)
  95. for _, option := range options {
  96. option(sw)
  97. }
  98. return sw
  99. }
  100. // SwitchFilterTimeout sets the timeout used for peer filters.
  101. func SwitchFilterTimeout(timeout time.Duration) SwitchOption {
  102. return func(sw *Switch) { sw.filterTimeout = timeout }
  103. }
  104. // SwitchPeerFilters sets the filters for rejection of new peers.
  105. func SwitchPeerFilters(filters ...PeerFilterFunc) SwitchOption {
  106. return func(sw *Switch) { sw.peerFilters = filters }
  107. }
  108. // WithMetrics sets the metrics.
  109. func WithMetrics(metrics *Metrics) SwitchOption {
  110. return func(sw *Switch) { sw.metrics = metrics }
  111. }
  112. //---------------------------------------------------------------------
  113. // Switch setup
  114. // AddReactor adds the given reactor to the switch.
  115. // NOTE: Not goroutine safe.
  116. func (sw *Switch) AddReactor(name string, reactor Reactor) Reactor {
  117. // Validate the reactor.
  118. // No two reactors can share the same channel.
  119. reactorChannels := reactor.GetChannels()
  120. for _, chDesc := range reactorChannels {
  121. chID := chDesc.ID
  122. if sw.reactorsByCh[chID] != nil {
  123. cmn.PanicSanity(fmt.Sprintf("Channel %X has multiple reactors %v & %v", chID, sw.reactorsByCh[chID], reactor))
  124. }
  125. sw.chDescs = append(sw.chDescs, chDesc)
  126. sw.reactorsByCh[chID] = reactor
  127. }
  128. sw.reactors[name] = reactor
  129. reactor.SetSwitch(sw)
  130. return reactor
  131. }
  132. // Reactors returns a map of reactors registered on the switch.
  133. // NOTE: Not goroutine safe.
  134. func (sw *Switch) Reactors() map[string]Reactor {
  135. return sw.reactors
  136. }
  137. // Reactor returns the reactor with the given name.
  138. // NOTE: Not goroutine safe.
  139. func (sw *Switch) Reactor(name string) Reactor {
  140. return sw.reactors[name]
  141. }
  142. // SetNodeInfo sets the switch's NodeInfo for checking compatibility and handshaking with other nodes.
  143. // NOTE: Not goroutine safe.
  144. func (sw *Switch) SetNodeInfo(nodeInfo NodeInfo) {
  145. sw.nodeInfo = nodeInfo
  146. }
  147. // NodeInfo returns the switch's NodeInfo.
  148. // NOTE: Not goroutine safe.
  149. func (sw *Switch) NodeInfo() NodeInfo {
  150. return sw.nodeInfo
  151. }
  152. // SetNodeKey sets the switch's private key for authenticated encryption.
  153. // NOTE: Not goroutine safe.
  154. func (sw *Switch) SetNodeKey(nodeKey *NodeKey) {
  155. sw.nodeKey = nodeKey
  156. }
  157. //---------------------------------------------------------------------
  158. // Service start/stop
  159. // OnStart implements BaseService. It starts all the reactors and peers.
  160. func (sw *Switch) OnStart() error {
  161. // Start reactors
  162. for _, reactor := range sw.reactors {
  163. err := reactor.Start()
  164. if err != nil {
  165. return cmn.ErrorWrap(err, "failed to start %v", reactor)
  166. }
  167. }
  168. // Start accepting Peers.
  169. go sw.acceptRoutine()
  170. return nil
  171. }
  172. // OnStop implements BaseService. It stops all peers and reactors.
  173. func (sw *Switch) OnStop() {
  174. // Stop peers
  175. for _, p := range sw.peers.List() {
  176. sw.transport.Cleanup(p)
  177. p.Stop()
  178. if sw.peers.Remove(p) {
  179. sw.metrics.Peers.Add(float64(-1))
  180. }
  181. }
  182. // Stop reactors
  183. sw.Logger.Debug("Switch: Stopping reactors")
  184. for _, reactor := range sw.reactors {
  185. reactor.Stop()
  186. }
  187. }
  188. //---------------------------------------------------------------------
  189. // Peers
  190. // Broadcast runs a go routine for each attempted send, which will block trying
  191. // to send for defaultSendTimeoutSeconds. Returns a channel which receives
  192. // success values for each attempted send (false if times out). Channel will be
  193. // closed once msg bytes are sent to all peers (or time out).
  194. //
  195. // NOTE: Broadcast uses goroutines, so order of broadcast may not be preserved.
  196. func (sw *Switch) Broadcast(chID byte, msgBytes []byte) chan bool {
  197. successChan := make(chan bool, len(sw.peers.List()))
  198. sw.Logger.Debug("Broadcast", "channel", chID, "msgBytes", fmt.Sprintf("%X", msgBytes))
  199. var wg sync.WaitGroup
  200. for _, peer := range sw.peers.List() {
  201. wg.Add(1)
  202. go func(peer Peer) {
  203. defer wg.Done()
  204. success := peer.Send(chID, msgBytes)
  205. successChan <- success
  206. }(peer)
  207. }
  208. go func() {
  209. wg.Wait()
  210. close(successChan)
  211. }()
  212. return successChan
  213. }
  214. // NumPeers returns the count of outbound/inbound and outbound-dialing peers.
  215. func (sw *Switch) NumPeers() (outbound, inbound, dialing int) {
  216. peers := sw.peers.List()
  217. for _, peer := range peers {
  218. if peer.IsOutbound() {
  219. outbound++
  220. } else {
  221. inbound++
  222. }
  223. }
  224. dialing = sw.dialing.Size()
  225. return
  226. }
  227. // MaxNumOutboundPeers returns a maximum number of outbound peers.
  228. func (sw *Switch) MaxNumOutboundPeers() int {
  229. return sw.config.MaxNumOutboundPeers
  230. }
  231. // Peers returns the set of peers that are connected to the switch.
  232. func (sw *Switch) Peers() IPeerSet {
  233. return sw.peers
  234. }
  235. // StopPeerForError disconnects from a peer due to external error.
  236. // If the peer is persistent, it will attempt to reconnect.
  237. // TODO: make record depending on reason.
  238. func (sw *Switch) StopPeerForError(peer Peer, reason interface{}) {
  239. sw.Logger.Error("Stopping peer for error", "peer", peer, "err", reason)
  240. sw.stopAndRemovePeer(peer, reason)
  241. if peer.IsPersistent() {
  242. addr := peer.OriginalAddr()
  243. if addr == nil {
  244. // FIXME: persistent peers can't be inbound right now.
  245. // self-reported address for inbound persistent peers
  246. addr = peer.NodeInfo().NetAddress()
  247. }
  248. go sw.reconnectToPeer(addr)
  249. }
  250. }
  251. // StopPeerGracefully disconnects from a peer gracefully.
  252. // TODO: handle graceful disconnects.
  253. func (sw *Switch) StopPeerGracefully(peer Peer) {
  254. sw.Logger.Info("Stopping peer gracefully")
  255. sw.stopAndRemovePeer(peer, nil)
  256. }
  257. func (sw *Switch) stopAndRemovePeer(peer Peer, reason interface{}) {
  258. if sw.peers.Remove(peer) {
  259. sw.metrics.Peers.Add(float64(-1))
  260. }
  261. sw.transport.Cleanup(peer)
  262. peer.Stop()
  263. for _, reactor := range sw.reactors {
  264. reactor.RemovePeer(peer, reason)
  265. }
  266. }
  267. // reconnectToPeer tries to reconnect to the addr, first repeatedly
  268. // with a fixed interval, then with exponential backoff.
  269. // If no success after all that, it stops trying, and leaves it
  270. // to the PEX/Addrbook to find the peer with the addr again
  271. // NOTE: this will keep trying even if the handshake or auth fails.
  272. // TODO: be more explicit with error types so we only retry on certain failures
  273. // - ie. if we're getting ErrDuplicatePeer we can stop
  274. // because the addrbook got us the peer back already
  275. func (sw *Switch) reconnectToPeer(addr *NetAddress) {
  276. if sw.reconnecting.Has(string(addr.ID)) {
  277. return
  278. }
  279. sw.reconnecting.Set(string(addr.ID), addr)
  280. defer sw.reconnecting.Delete(string(addr.ID))
  281. start := time.Now()
  282. sw.Logger.Info("Reconnecting to peer", "addr", addr)
  283. for i := 0; i < reconnectAttempts; i++ {
  284. if !sw.IsRunning() {
  285. return
  286. }
  287. if sw.IsDialingOrExistingAddress(addr) {
  288. sw.Logger.Debug("Peer connection has been established or dialed while we waiting next try", "addr", addr)
  289. return
  290. }
  291. err := sw.DialPeerWithAddress(addr, true)
  292. if err == nil {
  293. return // success
  294. }
  295. sw.Logger.Info("Error reconnecting to peer. Trying again", "tries", i, "err", err, "addr", addr)
  296. // sleep a set amount
  297. sw.randomSleep(reconnectInterval)
  298. continue
  299. }
  300. sw.Logger.Error("Failed to reconnect to peer. Beginning exponential backoff",
  301. "addr", addr, "elapsed", time.Since(start))
  302. for i := 0; i < reconnectBackOffAttempts; i++ {
  303. if !sw.IsRunning() {
  304. return
  305. }
  306. // sleep an exponentially increasing amount
  307. sleepIntervalSeconds := math.Pow(reconnectBackOffBaseSeconds, float64(i))
  308. sw.randomSleep(time.Duration(sleepIntervalSeconds) * time.Second)
  309. err := sw.DialPeerWithAddress(addr, true)
  310. if err == nil {
  311. return // success
  312. }
  313. sw.Logger.Info("Error reconnecting to peer. Trying again", "tries", i, "err", err, "addr", addr)
  314. }
  315. sw.Logger.Error("Failed to reconnect to peer. Giving up", "addr", addr, "elapsed", time.Since(start))
  316. }
  317. // SetAddrBook allows to set address book on Switch.
  318. func (sw *Switch) SetAddrBook(addrBook AddrBook) {
  319. sw.addrBook = addrBook
  320. }
  321. // MarkPeerAsGood marks the given peer as good when it did something useful
  322. // like contributed to consensus.
  323. func (sw *Switch) MarkPeerAsGood(peer Peer) {
  324. if sw.addrBook != nil {
  325. sw.addrBook.MarkGood(peer.NodeInfo().NetAddress())
  326. }
  327. }
  328. //---------------------------------------------------------------------
  329. // Dialing
  330. // DialPeersAsync dials a list of peers asynchronously in random order (optionally, making them persistent).
  331. // Used to dial peers from config on startup or from unsafe-RPC (trusted sources).
  332. // TODO: remove addrBook arg since it's now set on the switch
  333. func (sw *Switch) DialPeersAsync(addrBook AddrBook, peers []string, persistent bool) error {
  334. netAddrs, errs := NewNetAddressStrings(peers)
  335. // only log errors, dial correct addresses
  336. for _, err := range errs {
  337. sw.Logger.Error("Error in peer's address", "err", err)
  338. }
  339. ourAddr := sw.nodeInfo.NetAddress()
  340. // TODO: this code feels like it's in the wrong place.
  341. // The integration tests depend on the addrBook being saved
  342. // right away but maybe we can change that. Recall that
  343. // the addrBook is only written to disk every 2min
  344. if addrBook != nil {
  345. // add peers to `addrBook`
  346. for _, netAddr := range netAddrs {
  347. // do not add our address or ID
  348. if !netAddr.Same(ourAddr) {
  349. if err := addrBook.AddAddress(netAddr, ourAddr); err != nil {
  350. sw.Logger.Error("Can't add peer's address to addrbook", "err", err)
  351. }
  352. }
  353. }
  354. // Persist some peers to disk right away.
  355. // NOTE: integration tests depend on this
  356. addrBook.Save()
  357. }
  358. // permute the list, dial them in random order.
  359. perm := sw.rng.Perm(len(netAddrs))
  360. for i := 0; i < len(perm); i++ {
  361. go func(i int) {
  362. j := perm[i]
  363. addr := netAddrs[j]
  364. if addr.Same(ourAddr) {
  365. sw.Logger.Debug("Ignore attempt to connect to ourselves", "addr", addr, "ourAddr", ourAddr)
  366. return
  367. }
  368. sw.randomSleep(0)
  369. if sw.IsDialingOrExistingAddress(addr) {
  370. sw.Logger.Debug("Ignore attempt to connect to an existing peer", "addr", addr)
  371. return
  372. }
  373. err := sw.DialPeerWithAddress(addr, persistent)
  374. if err != nil {
  375. switch err.(type) {
  376. case ErrSwitchConnectToSelf, ErrSwitchDuplicatePeerID:
  377. sw.Logger.Debug("Error dialing peer", "err", err)
  378. default:
  379. sw.Logger.Error("Error dialing peer", "err", err)
  380. }
  381. }
  382. }(i)
  383. }
  384. return nil
  385. }
  386. // DialPeerWithAddress dials the given peer and runs sw.addPeer if it connects and authenticates successfully.
  387. // If `persistent == true`, the switch will always try to reconnect to this peer if the connection ever fails.
  388. func (sw *Switch) DialPeerWithAddress(addr *NetAddress, persistent bool) error {
  389. sw.dialing.Set(string(addr.ID), addr)
  390. defer sw.dialing.Delete(string(addr.ID))
  391. return sw.addOutboundPeerWithConfig(addr, sw.config, persistent)
  392. }
  393. // sleep for interval plus some random amount of ms on [0, dialRandomizerIntervalMilliseconds]
  394. func (sw *Switch) randomSleep(interval time.Duration) {
  395. r := time.Duration(sw.rng.Int63n(dialRandomizerIntervalMilliseconds)) * time.Millisecond
  396. time.Sleep(r + interval)
  397. }
  398. // IsDialingOrExistingAddress returns true if switch has a peer with the given
  399. // address or dialing it at the moment.
  400. func (sw *Switch) IsDialingOrExistingAddress(addr *NetAddress) bool {
  401. return sw.dialing.Has(string(addr.ID)) ||
  402. sw.peers.Has(addr.ID) ||
  403. (!sw.config.AllowDuplicateIP && sw.peers.HasIP(addr.IP))
  404. }
  405. func (sw *Switch) acceptRoutine() {
  406. for {
  407. p, err := sw.transport.Accept(peerConfig{
  408. chDescs: sw.chDescs,
  409. onPeerError: sw.StopPeerForError,
  410. reactorsByCh: sw.reactorsByCh,
  411. metrics: sw.metrics,
  412. })
  413. if err != nil {
  414. switch err := err.(type) {
  415. case ErrRejected:
  416. if err.IsSelf() {
  417. // Remove the given address from the address book and add to our addresses
  418. // to avoid dialing in the future.
  419. addr := err.Addr()
  420. sw.addrBook.RemoveAddress(&addr)
  421. sw.addrBook.AddOurAddress(&addr)
  422. }
  423. sw.Logger.Info(
  424. "Inbound Peer rejected",
  425. "err", err,
  426. "numPeers", sw.peers.Size(),
  427. )
  428. continue
  429. case *ErrTransportClosed:
  430. sw.Logger.Error(
  431. "Stopped accept routine, as transport is closed",
  432. "numPeers", sw.peers.Size(),
  433. )
  434. default:
  435. sw.Logger.Error(
  436. "Accept on transport errored",
  437. "err", err,
  438. "numPeers", sw.peers.Size(),
  439. )
  440. // We could instead have a retry loop around the acceptRoutine,
  441. // but that would need to stop and let the node shutdown eventually.
  442. // So might as well panic and let process managers restart the node.
  443. // There's no point in letting the node run without the acceptRoutine,
  444. // since it won't be able to accept new connections.
  445. panic(fmt.Errorf("accept routine exited: %v", err))
  446. }
  447. break
  448. }
  449. // Ignore connection if we already have enough peers.
  450. _, in, _ := sw.NumPeers()
  451. if in >= sw.config.MaxNumInboundPeers {
  452. sw.Logger.Info(
  453. "Ignoring inbound connection: already have enough inbound peers",
  454. "address", p.NodeInfo().NetAddress().String(),
  455. "have", in,
  456. "max", sw.config.MaxNumInboundPeers,
  457. )
  458. sw.transport.Cleanup(p)
  459. continue
  460. }
  461. if err := sw.addPeer(p); err != nil {
  462. sw.transport.Cleanup(p)
  463. if p.IsRunning() {
  464. _ = p.Stop()
  465. }
  466. sw.Logger.Info(
  467. "Ignoring inbound connection: error while adding peer",
  468. "err", err,
  469. "id", p.ID(),
  470. )
  471. }
  472. }
  473. }
  474. // dial the peer; make secret connection; authenticate against the dialed ID;
  475. // add the peer.
  476. // if dialing fails, start the reconnect loop. If handhsake fails, its over.
  477. // If peer is started succesffuly, reconnectLoop will start when
  478. // StopPeerForError is called
  479. func (sw *Switch) addOutboundPeerWithConfig(
  480. addr *NetAddress,
  481. cfg *config.P2PConfig,
  482. persistent bool,
  483. ) error {
  484. sw.Logger.Info("Dialing peer", "address", addr)
  485. // XXX(xla): Remove the leakage of test concerns in implementation.
  486. if cfg.TestDialFail {
  487. go sw.reconnectToPeer(addr)
  488. return fmt.Errorf("dial err (peerConfig.DialFail == true)")
  489. }
  490. p, err := sw.transport.Dial(*addr, peerConfig{
  491. chDescs: sw.chDescs,
  492. onPeerError: sw.StopPeerForError,
  493. persistent: persistent,
  494. reactorsByCh: sw.reactorsByCh,
  495. metrics: sw.metrics,
  496. })
  497. if err != nil {
  498. switch e := err.(type) {
  499. case ErrRejected:
  500. if e.IsSelf() {
  501. // Remove the given address from the address book and add to our addresses
  502. // to avoid dialing in the future.
  503. sw.addrBook.RemoveAddress(addr)
  504. sw.addrBook.AddOurAddress(addr)
  505. return err
  506. }
  507. }
  508. // retry persistent peers after
  509. // any dial error besides IsSelf()
  510. if persistent {
  511. go sw.reconnectToPeer(addr)
  512. }
  513. return err
  514. }
  515. if err := sw.addPeer(p); err != nil {
  516. sw.transport.Cleanup(p)
  517. if p.IsRunning() {
  518. _ = p.Stop()
  519. }
  520. return err
  521. }
  522. return nil
  523. }
  524. func (sw *Switch) filterPeer(p Peer) error {
  525. // Avoid duplicate
  526. if sw.peers.Has(p.ID()) {
  527. return ErrRejected{id: p.ID(), isDuplicate: true}
  528. }
  529. errc := make(chan error, len(sw.peerFilters))
  530. for _, f := range sw.peerFilters {
  531. go func(f PeerFilterFunc, p Peer, errc chan<- error) {
  532. errc <- f(sw.peers, p)
  533. }(f, p, errc)
  534. }
  535. for i := 0; i < cap(errc); i++ {
  536. select {
  537. case err := <-errc:
  538. if err != nil {
  539. return ErrRejected{id: p.ID(), err: err, isFiltered: true}
  540. }
  541. case <-time.After(sw.filterTimeout):
  542. return ErrFilterTimeout{}
  543. }
  544. }
  545. return nil
  546. }
  547. // addPeer starts up the Peer and adds it to the Switch. Error is returned if
  548. // the peer is filtered out or failed to start or can't be added.
  549. func (sw *Switch) addPeer(p Peer) error {
  550. if err := sw.filterPeer(p); err != nil {
  551. return err
  552. }
  553. p.SetLogger(sw.Logger.With("peer", p.NodeInfo().NetAddress()))
  554. // Handle the shut down case where the switch has stopped but we're
  555. // concurrently trying to add a peer.
  556. if !sw.IsRunning() {
  557. // XXX should this return an error or just log and terminate?
  558. sw.Logger.Error("Won't start a peer - switch is not running", "peer", p)
  559. return nil
  560. }
  561. // Start the peer's send/recv routines.
  562. // Must start it before adding it to the peer set
  563. // to prevent Start and Stop from being called concurrently.
  564. err := p.Start()
  565. if err != nil {
  566. // Should never happen
  567. sw.Logger.Error("Error starting peer", "err", err, "peer", p)
  568. return err
  569. }
  570. // Add the peer to PeerSet. Do this before starting the reactors
  571. // so that if Receive errors, we will find the peer and remove it.
  572. // Add should not err since we already checked peers.Has().
  573. if err := sw.peers.Add(p); err != nil {
  574. return err
  575. }
  576. sw.metrics.Peers.Add(float64(1))
  577. // Start all the reactor protocols on the peer.
  578. for _, reactor := range sw.reactors {
  579. reactor.AddPeer(p)
  580. }
  581. sw.Logger.Info("Added peer", "peer", p)
  582. return nil
  583. }