github.com/devfans/go-ethereum@v1.5.10-0.20170326212234-7419d0c38291/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/ethereum/go-ethereum/log" 28 "github.com/ethereum/go-ethereum/p2p/discover" 29 "github.com/ethereum/go-ethereum/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 // Endpoint resolution is throttled with bounded backoff. 42 initialResolveDelay = 60 * time.Second 43 maxResolveDelay = time.Hour 44 ) 45 46 // dialstate schedules dials and discovery lookups. 47 // it get's a chance to compute new tasks on every iteration 48 // of the main loop in Server.run. 49 type dialstate struct { 50 maxDynDials int 51 ntab discoverTable 52 netrestrict *netutil.Netlist 53 54 lookupRunning bool 55 dialing map[discover.NodeID]connFlag 56 lookupBuf []*discover.Node // current discovery lookup results 57 randomNodes []*discover.Node // filled from Table 58 static map[discover.NodeID]*dialTask 59 hist *dialHistory 60 } 61 62 type discoverTable interface { 63 Self() *discover.Node 64 Close() 65 Resolve(target discover.NodeID) *discover.Node 66 Lookup(target discover.NodeID) []*discover.Node 67 ReadRandomNodes([]*discover.Node) int 68 } 69 70 // the dial history remembers recent dials. 71 type dialHistory []pastDial 72 73 // pastDial is an entry in the dial history. 74 type pastDial struct { 75 id discover.NodeID 76 exp time.Time 77 } 78 79 type task interface { 80 Do(*Server) 81 } 82 83 // A dialTask is generated for each node that is dialed. Its 84 // fields cannot be accessed while the task is running. 85 type dialTask struct { 86 flags connFlag 87 dest *discover.Node 88 lastResolved time.Time 89 resolveDelay time.Duration 90 } 91 92 // discoverTask runs discovery table operations. 93 // Only one discoverTask is active at any time. 94 // discoverTask.Do performs a random lookup. 95 type discoverTask struct { 96 results []*discover.Node 97 } 98 99 // A waitExpireTask is generated if there are no other tasks 100 // to keep the loop in Server.run ticking. 101 type waitExpireTask struct { 102 time.Duration 103 } 104 105 func newDialState(static []*discover.Node, ntab discoverTable, maxdyn int, netrestrict *netutil.Netlist) *dialstate { 106 s := &dialstate{ 107 maxDynDials: maxdyn, 108 ntab: ntab, 109 netrestrict: netrestrict, 110 static: make(map[discover.NodeID]*dialTask), 111 dialing: make(map[discover.NodeID]connFlag), 112 randomNodes: make([]*discover.Node, maxdyn/2), 113 hist: new(dialHistory), 114 } 115 for _, n := range static { 116 s.addStatic(n) 117 } 118 return s 119 } 120 121 func (s *dialstate) addStatic(n *discover.Node) { 122 // This overwites the task instead of updating an existing 123 // entry, giving users the opportunity to force a resolve operation. 124 s.static[n.ID] = &dialTask{flags: staticDialedConn, dest: n} 125 } 126 127 func (s *dialstate) removeStatic(n *discover.Node) { 128 // This removes a task so future attempts to connect will not be made. 129 delete(s.static, n.ID) 130 } 131 132 func (s *dialstate) newTasks(nRunning int, peers map[discover.NodeID]*Peer, now time.Time) []task { 133 var newtasks []task 134 addDial := func(flag connFlag, n *discover.Node) bool { 135 if err := s.checkDial(n, peers); err != nil { 136 log.Trace("Skipping dial candidate", "id", n.ID, "addr", &net.TCPAddr{IP: n.IP, Port: int(n.TCP)}, "err", err) 137 return false 138 } 139 s.dialing[n.ID] = flag 140 newtasks = append(newtasks, &dialTask{flags: flag, dest: n}) 141 return true 142 } 143 144 // Compute number of dynamic dials necessary at this point. 145 needDynDials := s.maxDynDials 146 for _, p := range peers { 147 if p.rw.is(dynDialedConn) { 148 needDynDials-- 149 } 150 } 151 for _, flag := range s.dialing { 152 if flag&dynDialedConn != 0 { 153 needDynDials-- 154 } 155 } 156 157 // Expire the dial history on every invocation. 158 s.hist.expire(now) 159 160 // Create dials for static nodes if they are not connected. 161 for id, t := range s.static { 162 err := s.checkDial(t.dest, peers) 163 switch err { 164 case errNotWhitelisted, errSelf: 165 log.Warn("Removing static dial candidate", "id", t.dest.ID, "addr", &net.TCPAddr{IP: t.dest.IP, Port: int(t.dest.TCP)}, "err", err) 166 delete(s.static, t.dest.ID) 167 case nil: 168 s.dialing[id] = t.flags 169 newtasks = append(newtasks, t) 170 } 171 } 172 173 // Use random nodes from the table for half of the necessary 174 // dynamic dials. 175 randomCandidates := needDynDials / 2 176 if randomCandidates > 0 { 177 n := s.ntab.ReadRandomNodes(s.randomNodes) 178 for i := 0; i < randomCandidates && i < n; i++ { 179 if addDial(dynDialedConn, s.randomNodes[i]) { 180 needDynDials-- 181 } 182 } 183 } 184 // Create dynamic dials from random lookup results, removing tried 185 // items from the result buffer. 186 i := 0 187 for ; i < len(s.lookupBuf) && needDynDials > 0; i++ { 188 if addDial(dynDialedConn, s.lookupBuf[i]) { 189 needDynDials-- 190 } 191 } 192 s.lookupBuf = s.lookupBuf[:copy(s.lookupBuf, s.lookupBuf[i:])] 193 // Launch a discovery lookup if more candidates are needed. 194 if len(s.lookupBuf) < needDynDials && !s.lookupRunning { 195 s.lookupRunning = true 196 newtasks = append(newtasks, &discoverTask{}) 197 } 198 199 // Launch a timer to wait for the next node to expire if all 200 // candidates have been tried and no task is currently active. 201 // This should prevent cases where the dialer logic is not ticked 202 // because there are no pending events. 203 if nRunning == 0 && len(newtasks) == 0 && s.hist.Len() > 0 { 204 t := &waitExpireTask{s.hist.min().exp.Sub(now)} 205 newtasks = append(newtasks, t) 206 } 207 return newtasks 208 } 209 210 var ( 211 errSelf = errors.New("is self") 212 errAlreadyDialing = errors.New("already dialing") 213 errAlreadyConnected = errors.New("already connected") 214 errRecentlyDialed = errors.New("recently dialed") 215 errNotWhitelisted = errors.New("not contained in netrestrict whitelist") 216 ) 217 218 func (s *dialstate) checkDial(n *discover.Node, peers map[discover.NodeID]*Peer) error { 219 _, dialing := s.dialing[n.ID] 220 switch { 221 case dialing: 222 return errAlreadyDialing 223 case peers[n.ID] != nil: 224 return errAlreadyConnected 225 case s.ntab != nil && n.ID == s.ntab.Self().ID: 226 return errSelf 227 case s.netrestrict != nil && !s.netrestrict.Contains(n.IP): 228 return errNotWhitelisted 229 case s.hist.contains(n.ID): 230 return errRecentlyDialed 231 } 232 return nil 233 } 234 235 func (s *dialstate) taskDone(t task, now time.Time) { 236 switch t := t.(type) { 237 case *dialTask: 238 s.hist.add(t.dest.ID, now.Add(dialHistoryExpiration)) 239 delete(s.dialing, t.dest.ID) 240 case *discoverTask: 241 s.lookupRunning = false 242 s.lookupBuf = append(s.lookupBuf, t.results...) 243 } 244 } 245 246 func (t *dialTask) Do(srv *Server) { 247 if t.dest.Incomplete() { 248 if !t.resolve(srv) { 249 return 250 } 251 } 252 success := t.dial(srv, t.dest) 253 // Try resolving the ID of static nodes if dialing failed. 254 if !success && t.flags&staticDialedConn != 0 { 255 if t.resolve(srv) { 256 t.dial(srv, t.dest) 257 } 258 } 259 } 260 261 // resolve attempts to find the current endpoint for the destination 262 // using discovery. 263 // 264 // Resolve operations are throttled with backoff to avoid flooding the 265 // discovery network with useless queries for nodes that don't exist. 266 // The backoff delay resets when the node is found. 267 func (t *dialTask) resolve(srv *Server) bool { 268 if srv.ntab == nil { 269 log.Debug("Can't resolve node", "id", t.dest.ID, "err", "discovery is disabled") 270 return false 271 } 272 if t.resolveDelay == 0 { 273 t.resolveDelay = initialResolveDelay 274 } 275 if time.Since(t.lastResolved) < t.resolveDelay { 276 return false 277 } 278 resolved := srv.ntab.Resolve(t.dest.ID) 279 t.lastResolved = time.Now() 280 if resolved == nil { 281 t.resolveDelay *= 2 282 if t.resolveDelay > maxResolveDelay { 283 t.resolveDelay = maxResolveDelay 284 } 285 log.Debug("Resolving node failed", "id", t.dest.ID, "newdelay", t.resolveDelay) 286 return false 287 } 288 // The node was found. 289 t.resolveDelay = initialResolveDelay 290 t.dest = resolved 291 log.Debug("Resolved node", "id", t.dest.ID, "addr", &net.TCPAddr{IP: t.dest.IP, Port: int(t.dest.TCP)}) 292 return true 293 } 294 295 // dial performs the actual connection attempt. 296 func (t *dialTask) dial(srv *Server, dest *discover.Node) bool { 297 addr := &net.TCPAddr{IP: dest.IP, Port: int(dest.TCP)} 298 fd, err := srv.Dialer.Dial("tcp", addr.String()) 299 if err != nil { 300 log.Trace("Dial error", "task", t, "err", err) 301 return false 302 } 303 mfd := newMeteredConn(fd, false) 304 srv.setupConn(mfd, t.flags, dest) 305 return true 306 } 307 308 func (t *dialTask) String() string { 309 return fmt.Sprintf("%v %x %v:%d", t.flags, t.dest.ID[:8], t.dest.IP, t.dest.TCP) 310 } 311 312 func (t *discoverTask) Do(srv *Server) { 313 // newTasks generates a lookup task whenever dynamic dials are 314 // necessary. Lookups need to take some time, otherwise the 315 // event loop spins too fast. 316 next := srv.lastLookup.Add(lookupInterval) 317 if now := time.Now(); now.Before(next) { 318 time.Sleep(next.Sub(now)) 319 } 320 srv.lastLookup = time.Now() 321 var target discover.NodeID 322 rand.Read(target[:]) 323 t.results = srv.ntab.Lookup(target) 324 } 325 326 func (t *discoverTask) String() string { 327 s := "discovery lookup" 328 if len(t.results) > 0 { 329 s += fmt.Sprintf(" (%d results)", len(t.results)) 330 } 331 return s 332 } 333 334 func (t waitExpireTask) Do(*Server) { 335 time.Sleep(t.Duration) 336 } 337 func (t waitExpireTask) String() string { 338 return fmt.Sprintf("wait for dial hist expire (%v)", t.Duration) 339 } 340 341 // Use only these methods to access or modify dialHistory. 342 func (h dialHistory) min() pastDial { 343 return h[0] 344 } 345 func (h *dialHistory) add(id discover.NodeID, exp time.Time) { 346 heap.Push(h, pastDial{id, exp}) 347 } 348 func (h dialHistory) contains(id discover.NodeID) bool { 349 for _, v := range h { 350 if v.id == id { 351 return true 352 } 353 } 354 return false 355 } 356 func (h *dialHistory) expire(now time.Time) { 357 for h.Len() > 0 && h.min().exp.Before(now) { 358 heap.Pop(h) 359 } 360 } 361 362 // heap.Interface boilerplate 363 func (h dialHistory) Len() int { return len(h) } 364 func (h dialHistory) Less(i, j int) bool { return h[i].exp.Before(h[j].exp) } 365 func (h dialHistory) Swap(i, j int) { h[i], h[j] = h[j], h[i] } 366 func (h *dialHistory) Push(x interface{}) { 367 *h = append(*h, x.(pastDial)) 368 } 369 func (h *dialHistory) Pop() interface{} { 370 old := *h 371 n := len(old) 372 x := old[n-1] 373 *h = old[0 : n-1] 374 return x 375 }