github.com/gojoychain/go-geth@v1.8.22/p2p/dial.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package p2p 18 19 import ( 20 "container/heap" 21 "errors" 22 "fmt" 23 "net" 24 "time" 25 26 "github.com/ethereum/go-ethereum/log" 27 "github.com/ethereum/go-ethereum/p2p/enode" 28 "github.com/ethereum/go-ethereum/p2p/netutil" 29 ) 30 31 const ( 32 // This is the amount of time spent waiting in between 33 // redialing a certain node. 34 dialHistoryExpiration = 30 * time.Second 35 36 // Discovery lookups are throttled and can only run 37 // once every few seconds. 38 lookupInterval = 4 * time.Second 39 40 // If no peers are found for this amount of time, the initial bootnodes are 41 // attempted to be connected. 42 fallbackInterval = 20 * time.Second 43 44 // Endpoint resolution is throttled with bounded backoff. 45 initialResolveDelay = 60 * time.Second 46 maxResolveDelay = time.Hour 47 ) 48 49 // NodeDialer is used to connect to nodes in the network, typically by using 50 // an underlying net.Dialer but also using net.Pipe in tests 51 type NodeDialer interface { 52 Dial(*enode.Node) (net.Conn, error) 53 } 54 55 // TCPDialer implements the NodeDialer interface by using a net.Dialer to 56 // create TCP connections to nodes in the network 57 type TCPDialer struct { 58 *net.Dialer 59 } 60 61 // Dial creates a TCP connection to the node 62 func (t TCPDialer) Dial(dest *enode.Node) (net.Conn, error) { 63 addr := &net.TCPAddr{IP: dest.IP(), Port: dest.TCP()} 64 return t.Dialer.Dial("tcp", addr.String()) 65 } 66 67 // dialstate schedules dials and discovery lookups. 68 // it get's a chance to compute new tasks on every iteration 69 // of the main loop in Server.run. 70 type dialstate struct { 71 maxDynDials int 72 ntab discoverTable 73 netrestrict *netutil.Netlist 74 self enode.ID 75 76 lookupRunning bool 77 dialing map[enode.ID]connFlag 78 lookupBuf []*enode.Node // current discovery lookup results 79 randomNodes []*enode.Node // filled from Table 80 static map[enode.ID]*dialTask 81 hist *dialHistory 82 83 start time.Time // time when the dialer was first used 84 bootnodes []*enode.Node // default dials when there are no peers 85 } 86 87 type discoverTable interface { 88 Close() 89 Resolve(*enode.Node) *enode.Node 90 LookupRandom() []*enode.Node 91 ReadRandomNodes([]*enode.Node) int 92 } 93 94 // the dial history remembers recent dials. 95 type dialHistory []pastDial 96 97 // pastDial is an entry in the dial history. 98 type pastDial struct { 99 id enode.ID 100 exp time.Time 101 } 102 103 type task interface { 104 Do(*Server) 105 } 106 107 // A dialTask is generated for each node that is dialed. Its 108 // fields cannot be accessed while the task is running. 109 type dialTask struct { 110 flags connFlag 111 dest *enode.Node 112 lastResolved time.Time 113 resolveDelay time.Duration 114 } 115 116 // discoverTask runs discovery table operations. 117 // Only one discoverTask is active at any time. 118 // discoverTask.Do performs a random lookup. 119 type discoverTask struct { 120 results []*enode.Node 121 } 122 123 // A waitExpireTask is generated if there are no other tasks 124 // to keep the loop in Server.run ticking. 125 type waitExpireTask struct { 126 time.Duration 127 } 128 129 func newDialState(self enode.ID, static []*enode.Node, bootnodes []*enode.Node, ntab discoverTable, maxdyn int, netrestrict *netutil.Netlist) *dialstate { 130 s := &dialstate{ 131 maxDynDials: maxdyn, 132 ntab: ntab, 133 self: self, 134 netrestrict: netrestrict, 135 static: make(map[enode.ID]*dialTask), 136 dialing: make(map[enode.ID]connFlag), 137 bootnodes: make([]*enode.Node, len(bootnodes)), 138 randomNodes: make([]*enode.Node, maxdyn/2), 139 hist: new(dialHistory), 140 } 141 copy(s.bootnodes, bootnodes) 142 for _, n := range static { 143 s.addStatic(n) 144 } 145 return s 146 } 147 148 func (s *dialstate) addStatic(n *enode.Node) { 149 // This overwrites the task instead of updating an existing 150 // entry, giving users the opportunity to force a resolve operation. 151 s.static[n.ID()] = &dialTask{flags: staticDialedConn, dest: n} 152 } 153 154 func (s *dialstate) removeStatic(n *enode.Node) { 155 // This removes a task so future attempts to connect will not be made. 156 delete(s.static, n.ID()) 157 // This removes a previous dial timestamp so that application 158 // can force a server to reconnect with chosen peer immediately. 159 s.hist.remove(n.ID()) 160 } 161 162 func (s *dialstate) newTasks(nRunning int, peers map[enode.ID]*Peer, now time.Time) []task { 163 if s.start.IsZero() { 164 s.start = now 165 } 166 167 var newtasks []task 168 addDial := func(flag connFlag, n *enode.Node) bool { 169 if err := s.checkDial(n, peers); err != nil { 170 log.Trace("Skipping dial candidate", "id", n.ID(), "addr", &net.TCPAddr{IP: n.IP(), Port: n.TCP()}, "err", err) 171 return false 172 } 173 s.dialing[n.ID()] = flag 174 newtasks = append(newtasks, &dialTask{flags: flag, dest: n}) 175 return true 176 } 177 178 // Compute number of dynamic dials necessary at this point. 179 needDynDials := s.maxDynDials 180 for _, p := range peers { 181 if p.rw.is(dynDialedConn) { 182 needDynDials-- 183 } 184 } 185 for _, flag := range s.dialing { 186 if flag&dynDialedConn != 0 { 187 needDynDials-- 188 } 189 } 190 191 // Expire the dial history on every invocation. 192 s.hist.expire(now) 193 194 // Create dials for static nodes if they are not connected. 195 for id, t := range s.static { 196 err := s.checkDial(t.dest, peers) 197 switch err { 198 case errNotWhitelisted, errSelf: 199 log.Warn("Removing static dial candidate", "id", t.dest.ID, "addr", &net.TCPAddr{IP: t.dest.IP(), Port: t.dest.TCP()}, "err", err) 200 delete(s.static, t.dest.ID()) 201 case nil: 202 s.dialing[id] = t.flags 203 newtasks = append(newtasks, t) 204 } 205 } 206 // If we don't have any peers whatsoever, try to dial a random bootnode. This 207 // scenario is useful for the testnet (and private networks) where the discovery 208 // table might be full of mostly bad peers, making it hard to find good ones. 209 if len(peers) == 0 && len(s.bootnodes) > 0 && needDynDials > 0 && now.Sub(s.start) > fallbackInterval { 210 bootnode := s.bootnodes[0] 211 s.bootnodes = append(s.bootnodes[:0], s.bootnodes[1:]...) 212 s.bootnodes = append(s.bootnodes, bootnode) 213 214 if addDial(dynDialedConn, bootnode) { 215 needDynDials-- 216 } 217 } 218 // Use random nodes from the table for half of the necessary 219 // dynamic dials. 220 randomCandidates := needDynDials / 2 221 if randomCandidates > 0 { 222 n := s.ntab.ReadRandomNodes(s.randomNodes) 223 for i := 0; i < randomCandidates && i < n; i++ { 224 if addDial(dynDialedConn, s.randomNodes[i]) { 225 needDynDials-- 226 } 227 } 228 } 229 // Create dynamic dials from random lookup results, removing tried 230 // items from the result buffer. 231 i := 0 232 for ; i < len(s.lookupBuf) && needDynDials > 0; i++ { 233 if addDial(dynDialedConn, s.lookupBuf[i]) { 234 needDynDials-- 235 } 236 } 237 s.lookupBuf = s.lookupBuf[:copy(s.lookupBuf, s.lookupBuf[i:])] 238 // Launch a discovery lookup if more candidates are needed. 239 if len(s.lookupBuf) < needDynDials && !s.lookupRunning { 240 s.lookupRunning = true 241 newtasks = append(newtasks, &discoverTask{}) 242 } 243 244 // Launch a timer to wait for the next node to expire if all 245 // candidates have been tried and no task is currently active. 246 // This should prevent cases where the dialer logic is not ticked 247 // because there are no pending events. 248 if nRunning == 0 && len(newtasks) == 0 && s.hist.Len() > 0 { 249 t := &waitExpireTask{s.hist.min().exp.Sub(now)} 250 newtasks = append(newtasks, t) 251 } 252 return newtasks 253 } 254 255 var ( 256 errSelf = errors.New("is self") 257 errAlreadyDialing = errors.New("already dialing") 258 errAlreadyConnected = errors.New("already connected") 259 errRecentlyDialed = errors.New("recently dialed") 260 errNotWhitelisted = errors.New("not contained in netrestrict whitelist") 261 ) 262 263 func (s *dialstate) checkDial(n *enode.Node, peers map[enode.ID]*Peer) error { 264 _, dialing := s.dialing[n.ID()] 265 switch { 266 case dialing: 267 return errAlreadyDialing 268 case peers[n.ID()] != nil: 269 return errAlreadyConnected 270 case n.ID() == s.self: 271 return errSelf 272 case s.netrestrict != nil && !s.netrestrict.Contains(n.IP()): 273 return errNotWhitelisted 274 case s.hist.contains(n.ID()): 275 return errRecentlyDialed 276 } 277 return nil 278 } 279 280 func (s *dialstate) taskDone(t task, now time.Time) { 281 switch t := t.(type) { 282 case *dialTask: 283 s.hist.add(t.dest.ID(), now.Add(dialHistoryExpiration)) 284 delete(s.dialing, t.dest.ID()) 285 case *discoverTask: 286 s.lookupRunning = false 287 s.lookupBuf = append(s.lookupBuf, t.results...) 288 } 289 } 290 291 func (t *dialTask) Do(srv *Server) { 292 if t.dest.Incomplete() { 293 if !t.resolve(srv) { 294 return 295 } 296 } 297 err := t.dial(srv, t.dest) 298 if err != nil { 299 log.Trace("Dial error", "task", t, "err", err) 300 // Try resolving the ID of static nodes if dialing failed. 301 if _, ok := err.(*dialError); ok && t.flags&staticDialedConn != 0 { 302 if t.resolve(srv) { 303 t.dial(srv, t.dest) 304 } 305 } 306 } 307 } 308 309 // resolve attempts to find the current endpoint for the destination 310 // using discovery. 311 // 312 // Resolve operations are throttled with backoff to avoid flooding the 313 // discovery network with useless queries for nodes that don't exist. 314 // The backoff delay resets when the node is found. 315 func (t *dialTask) resolve(srv *Server) bool { 316 if srv.ntab == nil { 317 log.Debug("Can't resolve node", "id", t.dest.ID, "err", "discovery is disabled") 318 return false 319 } 320 if t.resolveDelay == 0 { 321 t.resolveDelay = initialResolveDelay 322 } 323 if time.Since(t.lastResolved) < t.resolveDelay { 324 return false 325 } 326 resolved := srv.ntab.Resolve(t.dest) 327 t.lastResolved = time.Now() 328 if resolved == nil { 329 t.resolveDelay *= 2 330 if t.resolveDelay > maxResolveDelay { 331 t.resolveDelay = maxResolveDelay 332 } 333 log.Debug("Resolving node failed", "id", t.dest.ID, "newdelay", t.resolveDelay) 334 return false 335 } 336 // The node was found. 337 t.resolveDelay = initialResolveDelay 338 t.dest = resolved 339 log.Debug("Resolved node", "id", t.dest.ID, "addr", &net.TCPAddr{IP: t.dest.IP(), Port: t.dest.TCP()}) 340 return true 341 } 342 343 type dialError struct { 344 error 345 } 346 347 // dial performs the actual connection attempt. 348 func (t *dialTask) dial(srv *Server, dest *enode.Node) error { 349 fd, err := srv.Dialer.Dial(dest) 350 if err != nil { 351 return &dialError{err} 352 } 353 mfd := newMeteredConn(fd, false, dest.IP()) 354 return srv.SetupConn(mfd, t.flags, dest) 355 } 356 357 func (t *dialTask) String() string { 358 id := t.dest.ID() 359 return fmt.Sprintf("%v %x %v:%d", t.flags, id[:8], t.dest.IP(), t.dest.TCP()) 360 } 361 362 func (t *discoverTask) Do(srv *Server) { 363 // newTasks generates a lookup task whenever dynamic dials are 364 // necessary. Lookups need to take some time, otherwise the 365 // event loop spins too fast. 366 next := srv.lastLookup.Add(lookupInterval) 367 if now := time.Now(); now.Before(next) { 368 time.Sleep(next.Sub(now)) 369 } 370 srv.lastLookup = time.Now() 371 t.results = srv.ntab.LookupRandom() 372 } 373 374 func (t *discoverTask) String() string { 375 s := "discovery lookup" 376 if len(t.results) > 0 { 377 s += fmt.Sprintf(" (%d results)", len(t.results)) 378 } 379 return s 380 } 381 382 func (t waitExpireTask) Do(*Server) { 383 time.Sleep(t.Duration) 384 } 385 func (t waitExpireTask) String() string { 386 return fmt.Sprintf("wait for dial hist expire (%v)", t.Duration) 387 } 388 389 // Use only these methods to access or modify dialHistory. 390 func (h dialHistory) min() pastDial { 391 return h[0] 392 } 393 func (h *dialHistory) add(id enode.ID, exp time.Time) { 394 heap.Push(h, pastDial{id, exp}) 395 396 } 397 func (h *dialHistory) remove(id enode.ID) bool { 398 for i, v := range *h { 399 if v.id == id { 400 heap.Remove(h, i) 401 return true 402 } 403 } 404 return false 405 } 406 func (h dialHistory) contains(id enode.ID) bool { 407 for _, v := range h { 408 if v.id == id { 409 return true 410 } 411 } 412 return false 413 } 414 func (h *dialHistory) expire(now time.Time) { 415 for h.Len() > 0 && h.min().exp.Before(now) { 416 heap.Pop(h) 417 } 418 } 419 420 // heap.Interface boilerplate 421 func (h dialHistory) Len() int { return len(h) } 422 func (h dialHistory) Less(i, j int) bool { return h[i].exp.Before(h[j].exp) } 423 func (h dialHistory) Swap(i, j int) { h[i], h[j] = h[j], h[i] } 424 func (h *dialHistory) Push(x interface{}) { 425 *h = append(*h, x.(pastDial)) 426 } 427 func (h *dialHistory) Pop() interface{} { 428 old := *h 429 n := len(old) 430 x := old[n-1] 431 *h = old[0 : n-1] 432 return x 433 }