github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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 "crypto/rand" 22 "errors" 23 "fmt" 24 "net" 25 "time" 26 27 "github.com/intfoundation/intchain/log" 28 "github.com/intfoundation/intchain/p2p/discover" 29 "github.com/intfoundation/intchain/p2p/netutil" 30 ) 31 32 const ( 33 // This is the amount of time spent waiting in between 34 // redialing a certain node. 35 dialHistoryExpiration = 30 * time.Second 36 37 // Discovery lookups are throttled and can only run 38 // once every few seconds. 39 lookupInterval = 4 * time.Second 40 41 // If no peers are found for this amount of time, the initial bootnodes are 42 // attempted to be connected. 43 fallbackInterval = 20 * time.Second 44 45 // Endpoint resolution is throttled with bounded backoff. 46 initialResolveDelay = 60 * time.Second 47 maxResolveDelay = time.Hour 48 ) 49 50 // NodeDialer is used to connect to nodes in the network, typically by using 51 // an underlying net.Dialer but also using net.Pipe in tests 52 type NodeDialer interface { 53 Dial(*discover.Node) (net.Conn, error) 54 } 55 56 // TCPDialer implements the NodeDialer interface by using a net.Dialer to 57 // create TCP connections to nodes in the network 58 type TCPDialer struct { 59 *net.Dialer 60 } 61 62 // Dial creates a TCP connection to the node 63 func (t TCPDialer) Dial(dest *discover.Node) (net.Conn, error) { 64 addr := &net.TCPAddr{IP: dest.IP, Port: int(dest.TCP)} 65 return t.Dialer.Dial("tcp", addr.String()) 66 } 67 68 // dialstate schedules dials and discovery lookups. 69 // it get's a chance to compute new tasks on every iteration 70 // of the main loop in Server.run. 71 type dialstate struct { 72 maxDynDials int 73 ntab discoverTable 74 netrestrict *netutil.Netlist 75 76 lookupRunning bool 77 dialing map[discover.NodeID]connFlag 78 lookupBuf []*discover.Node // current discovery lookup results 79 randomNodes []*discover.Node // filled from Table 80 static map[discover.NodeID]*dialTask 81 hist *dialHistory 82 83 start time.Time // time when the dialer was first used 84 bootnodes []*discover.Node // default dials when there are no peers 85 } 86 87 type discoverTable interface { 88 Self() *discover.Node 89 Close() 90 Resolve(target discover.NodeID) *discover.Node 91 Lookup(target discover.NodeID) []*discover.Node 92 ReadRandomNodes([]*discover.Node) int 93 } 94 95 // the dial history remembers recent dials. 96 type dialHistory []pastDial 97 98 // pastDial is an entry in the dial history. 99 type pastDial struct { 100 id discover.NodeID 101 exp time.Time 102 } 103 104 type task interface { 105 Do(*Server) 106 } 107 108 // A dialTask is generated for each node that is dialed. Its 109 // fields cannot be accessed while the task is running. 110 type dialTask struct { 111 flags connFlag 112 dest *discover.Node 113 lastResolved time.Time 114 resolveDelay time.Duration 115 } 116 117 // discoverTask runs discovery table operations. 118 // Only one discoverTask is active at any time. 119 // discoverTask.Do performs a random lookup. 120 type discoverTask struct { 121 results []*discover.Node 122 } 123 124 // A waitExpireTask is generated if there are no other tasks 125 // to keep the loop in Server.run ticking. 126 type waitExpireTask struct { 127 time.Duration 128 } 129 130 func newDialState(static []*discover.Node, bootnodes []*discover.Node, ntab discoverTable, maxdyn int, netrestrict *netutil.Netlist) *dialstate { 131 s := &dialstate{ 132 maxDynDials: maxdyn, 133 ntab: ntab, 134 netrestrict: netrestrict, 135 static: make(map[discover.NodeID]*dialTask), 136 dialing: make(map[discover.NodeID]connFlag), 137 bootnodes: make([]*discover.Node, len(bootnodes)), 138 randomNodes: make([]*discover.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 *discover.Node) { 149 // This overwites 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 *discover.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[discover.NodeID]*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 *discover.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: int(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: int(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 *discover.Node, peers map[discover.NodeID]*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 s.ntab != nil && n.ID == s.ntab.Self().ID: 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.ID) 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: int(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 *discover.Node) error { 349 fd, err := srv.Dialer.Dial(dest) 350 if err != nil { 351 return &dialError{err} 352 } 353 mfd := newMeteredConn(fd, false) 354 return srv.SetupConn(mfd, t.flags, dest) 355 } 356 357 func (t *dialTask) String() string { 358 return fmt.Sprintf("%v %x %v:%d", t.flags, t.dest.ID[:8], t.dest.IP, t.dest.TCP) 359 } 360 361 func (t *discoverTask) Do(srv *Server) { 362 // newTasks generates a lookup task whenever dynamic dials are 363 // necessary. Lookups need to take some time, otherwise the 364 // event loop spins too fast. 365 next := srv.lastLookup.Add(lookupInterval) 366 if now := time.Now(); now.Before(next) { 367 time.Sleep(next.Sub(now)) 368 } 369 srv.lastLookup = time.Now() 370 var target discover.NodeID 371 rand.Read(target[:]) 372 t.results = srv.ntab.Lookup(target) 373 } 374 375 func (t *discoverTask) String() string { 376 s := "discovery lookup" 377 if len(t.results) > 0 { 378 s += fmt.Sprintf(" (%d results)", len(t.results)) 379 } 380 return s 381 } 382 383 func (t waitExpireTask) Do(*Server) { 384 time.Sleep(t.Duration) 385 } 386 func (t waitExpireTask) String() string { 387 return fmt.Sprintf("wait for dial hist expire (%v)", t.Duration) 388 } 389 390 // Use only these methods to access or modify dialHistory. 391 func (h dialHistory) min() pastDial { 392 return h[0] 393 } 394 func (h *dialHistory) add(id discover.NodeID, exp time.Time) { 395 heap.Push(h, pastDial{id, exp}) 396 397 } 398 func (h *dialHistory) remove(id discover.NodeID) bool { 399 for i, v := range *h { 400 if v.id == id { 401 heap.Remove(h, i) 402 return true 403 } 404 } 405 return false 406 } 407 func (h dialHistory) contains(id discover.NodeID) bool { 408 for _, v := range h { 409 if v.id == id { 410 return true 411 } 412 } 413 return false 414 } 415 func (h *dialHistory) expire(now time.Time) { 416 for h.Len() > 0 && h.min().exp.Before(now) { 417 heap.Pop(h) 418 } 419 } 420 421 // heap.Interface boilerplate 422 func (h dialHistory) Len() int { return len(h) } 423 func (h dialHistory) Less(i, j int) bool { return h[i].exp.Before(h[j].exp) } 424 func (h dialHistory) Swap(i, j int) { h[i], h[j] = h[j], h[i] } 425 func (h *dialHistory) Push(x interface{}) { 426 *h = append(*h, x.(pastDial)) 427 } 428 func (h *dialHistory) Pop() interface{} { 429 old := *h 430 n := len(old) 431 x := old[n-1] 432 *h = old[0 : n-1] 433 return x 434 }