github.com/annchain/OG@v0.0.9/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 "github.com/annchain/OG/p2p/netutil" 25 "github.com/annchain/OG/p2p/onode" 26 "github.com/sirupsen/logrus" 27 "net" 28 "time" 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(*onode.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 *onode.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 onode.ID 75 76 lookupRunning bool 77 dialing map[onode.ID]connFlag 78 lookupBuf []*onode.Node // current discovery lookup results 79 randomNodes []*onode.Node // filled from Table 80 static map[onode.ID]*dialTask 81 hist *dialHistory 82 83 start time.Time // time when the dialer was first used 84 bootnodes []*onode.Node // default dials when there are no peers 85 } 86 87 type discoverTable interface { 88 //Self() *onode.Node 89 Close() 90 Resolve(target *onode.Node) *onode.Node 91 LookupRandom() []*onode.Node 92 ReadRandomNodes([]*onode.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 onode.ID 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 *onode.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 []*onode.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(self onode.ID, static []*onode.Node, bootnodes []*onode.Node, ntab discoverTable, maxdyn int, netrestrict *netutil.Netlist) *dialstate { 131 s := &dialstate{ 132 maxDynDials: maxdyn, 133 ntab: ntab, 134 netrestrict: netrestrict, 135 self: self, 136 static: make(map[onode.ID]*dialTask), 137 dialing: make(map[onode.ID]connFlag), 138 bootnodes: make([]*onode.Node, len(bootnodes)), 139 randomNodes: make([]*onode.Node, maxdyn/2), 140 hist: new(dialHistory), 141 } 142 copy(s.bootnodes, bootnodes) 143 for _, n := range static { 144 s.addStatic(n) 145 } 146 return s 147 } 148 149 func (s *dialstate) addStatic(n *onode.Node) { 150 // This overwrites the task instead of updating an existing 151 // entry, giving users the opportunity to force a resolve operation. 152 s.static[n.ID()] = &dialTask{flags: staticDialedConn, dest: n} 153 } 154 155 func (s *dialstate) removeStatic(n *onode.Node) { 156 // This removes a task so future attempts to connect will not be made. 157 delete(s.static, n.ID()) 158 // This removes a previous dial timestamp so that application 159 // can force a server to reconnect with chosen peer immediately. 160 s.hist.remove(n.ID()) 161 } 162 163 func (s *dialstate) newTasks(nRunning int, peers map[onode.ID]*Peer, now time.Time) []task { 164 if s.start.IsZero() { 165 s.start = now 166 } 167 168 var newtasks []task 169 addDial := func(flag connFlag, n *onode.Node) bool { 170 if err := s.checkDial(n, peers); err != nil { 171 log.WithFields(logrus.Fields{"id": n.ID(), "addr": &net.TCPAddr{IP: n.IP(), Port: n.TCP()}}). 172 WithError(err). 173 Trace("Skipping dial candidate") 174 return false 175 } 176 s.dialing[n.ID()] = flag 177 newtasks = append(newtasks, &dialTask{flags: flag, dest: n}) 178 return true 179 } 180 181 // Compute number of dynamic dials necessary at this point. 182 needDynDials := s.maxDynDials 183 for _, p := range peers { 184 if p.rw.is(dynDialedConn) { 185 needDynDials-- 186 } 187 } 188 for _, flag := range s.dialing { 189 if flag&dynDialedConn != 0 { 190 needDynDials-- 191 } 192 } 193 194 // Expire the dial history on every invocation. 195 s.hist.expire(now) 196 197 // Create dials for static nodes if they are not connected. 198 for id, t := range s.static { 199 err := s.checkDial(t.dest, peers) 200 switch err { 201 case errNotWhitelisted, errSelf: 202 log.WithFields(logrus.Fields{"id": t.dest.ID().TerminalString(), "addr": &net.TCPAddr{IP: t.dest.IP(), Port: t.dest.TCP()}}). 203 WithError(err). 204 Debug("Removing static dial candidate") 205 delete(s.static, t.dest.ID()) 206 case nil: 207 s.dialing[id] = t.flags 208 newtasks = append(newtasks, t) 209 } 210 } 211 // If we don't have any peers whatsoever, try to dial a random bootnode. This 212 // scenario is useful for the testnet (and private networks) where the discovery 213 // table might be full of mostly bad peers, making it hard to find good ones. 214 if len(peers) == 0 && len(s.bootnodes) > 0 && needDynDials > 0 && now.Sub(s.start) > fallbackInterval { 215 bootnode := s.bootnodes[0] 216 s.bootnodes = append(s.bootnodes[:0], s.bootnodes[1:]...) 217 s.bootnodes = append(s.bootnodes, bootnode) 218 219 if addDial(dynDialedConn, bootnode) { 220 needDynDials-- 221 } 222 } 223 // Use random nodes from the table for half of the necessary 224 // dynamic dials. 225 randomCandidates := needDynDials / 2 226 if randomCandidates > 0 { 227 n := s.ntab.ReadRandomNodes(s.randomNodes) 228 for i := 0; i < randomCandidates && i < n; i++ { 229 if addDial(dynDialedConn, s.randomNodes[i]) { 230 needDynDials-- 231 } 232 } 233 } 234 // Create dynamic dials from random lookup results, removing tried 235 // items from the result buffer. 236 i := 0 237 for ; i < len(s.lookupBuf) && needDynDials > 0; i++ { 238 if addDial(dynDialedConn, s.lookupBuf[i]) { 239 needDynDials-- 240 } 241 } 242 s.lookupBuf = s.lookupBuf[:copy(s.lookupBuf, s.lookupBuf[i:])] 243 // Launch a discovery lookup if more candidates are needed. 244 if len(s.lookupBuf) < needDynDials && !s.lookupRunning { 245 s.lookupRunning = true 246 newtasks = append(newtasks, &discoverTask{}) 247 } 248 249 // Launch a timer to wait for the next node to expire if all 250 // candidates have been tried and no task is currently active. 251 // This should prevent cases where the dialer logic is not ticked 252 // because there are no pending events. 253 if nRunning == 0 && len(newtasks) == 0 && s.hist.Len() > 0 { 254 t := &waitExpireTask{s.hist.min().exp.Sub(now)} 255 newtasks = append(newtasks, t) 256 } 257 return newtasks 258 } 259 260 var ( 261 errSelf = errors.New("is self") 262 errAlreadyDialing = errors.New("already dialing") 263 errAlreadyConnected = errors.New("already connected") 264 errRecentlyDialed = errors.New("recently dialed") 265 errNotWhitelisted = errors.New("not contained in netrestrict whitelist") 266 ) 267 268 func (s *dialstate) checkDial(n *onode.Node, peers map[onode.ID]*Peer) error { 269 _, dialing := s.dialing[n.ID()] 270 switch { 271 case dialing: 272 return errAlreadyDialing 273 case peers[n.ID()] != nil: 274 return errAlreadyConnected 275 case s.ntab != nil && n.ID() == s.self: 276 return errSelf 277 case s.netrestrict != nil && !s.netrestrict.Contains(n.IP()): 278 return errNotWhitelisted 279 case s.hist.contains(n.ID()): 280 return errRecentlyDialed 281 } 282 return nil 283 } 284 285 func (s *dialstate) taskDone(t task, now time.Time) { 286 switch t := t.(type) { 287 case *dialTask: 288 s.hist.add(t.dest.ID(), now.Add(dialHistoryExpiration)) 289 delete(s.dialing, t.dest.ID()) 290 case *discoverTask: 291 s.lookupRunning = false 292 s.lookupBuf = append(s.lookupBuf, t.results...) 293 } 294 } 295 296 func (t *dialTask) Do(srv *Server) { 297 if t.dest.Incomplete() { 298 if !t.resolve(srv) { 299 return 300 } 301 } 302 err := t.dial(srv, t.dest) 303 if err != nil { 304 log.WithField("task", t).WithError(err).Trace("Dial error") 305 // Try resolving the ID of static nodes if dialing failed. 306 if _, ok := err.(*dialError); ok && t.flags&staticDialedConn != 0 { 307 if t.resolve(srv) { 308 t.dial(srv, t.dest) 309 } 310 } 311 } 312 } 313 314 // resolve attempts to find the current endpoint for the destination 315 // using discovery. 316 // 317 // Resolve operations are throttled with backoff to avoid flooding the 318 // discovery network with useless queries for nodes that don't exist. 319 // The backoff delay resets when the node is found. 320 func (t *dialTask) resolve(srv *Server) bool { 321 if srv.ntab == nil { 322 log.WithFields(logrus.Fields{"id": t.dest.ID}). 323 Debug("Can't resolve node: discovery is disabled") 324 return false 325 } 326 if t.resolveDelay == 0 { 327 t.resolveDelay = initialResolveDelay 328 } 329 if time.Since(t.lastResolved) < t.resolveDelay { 330 return false 331 } 332 resolved := srv.ntab.Resolve(t.dest) 333 t.lastResolved = time.Now() 334 if resolved == nil { 335 t.resolveDelay *= 2 336 if t.resolveDelay > maxResolveDelay { 337 t.resolveDelay = maxResolveDelay 338 } 339 log.WithFields(logrus.Fields{"id": t.dest.ID, "newdelay": t.resolveDelay}).Warn("Resolving node failed") 340 return false 341 } 342 // The node was found. 343 t.resolveDelay = initialResolveDelay 344 t.dest = resolved 345 log.WithFields(logrus.Fields{"id": t.dest.ID().TerminalString(), "addr": &net.TCPAddr{IP: t.dest.IP(), Port: t.dest.TCP()}}). 346 Debug("Resolved node") 347 return true 348 } 349 350 type dialError struct { 351 error 352 } 353 354 // dial performs the actual connection attempt. 355 func (t *dialTask) dial(srv *Server, dest *onode.Node) error { 356 fd, err := srv.Dialer.Dial(dest) 357 if err != nil { 358 return &dialError{err} 359 } 360 mfd := newMeteredConn(fd, false, dest.IP()) 361 return srv.SetupConn(mfd, t.flags, dest) 362 } 363 364 func (t *dialTask) String() string { 365 id := t.dest.ID() 366 return fmt.Sprintf("%v %x %v:%d", t.flags, id[:8], t.dest.IP(), t.dest.TCP()) 367 } 368 369 func (t *discoverTask) Do(srv *Server) { 370 // newTasks generates a lookup task whenever dynamic dials are 371 // necessary. Lookups need to take some time, otherwise the 372 // event loop spins too fast. 373 next := srv.lastLookup.Add(lookupInterval) 374 if now := time.Now(); now.Before(next) { 375 time.Sleep(next.Sub(now)) 376 } 377 srv.lastLookup = time.Now() 378 var target onode.ID 379 rand.Read(target[:]) 380 t.results = srv.ntab.LookupRandom() 381 } 382 383 func (t *discoverTask) String() string { 384 s := "discovery lookup" 385 if len(t.results) > 0 { 386 s += fmt.Sprintf(" (%d results)", len(t.results)) 387 } 388 return s 389 } 390 391 func (t waitExpireTask) Do(*Server) { 392 time.Sleep(t.Duration) 393 } 394 func (t waitExpireTask) String() string { 395 return fmt.Sprintf("wait for dial hist expire (%v)", t.Duration) 396 } 397 398 // Use only these methods to access or modify dialHistory. 399 func (h dialHistory) min() pastDial { 400 return h[0] 401 } 402 func (h *dialHistory) add(id onode.ID, exp time.Time) { 403 heap.Push(h, pastDial{id, exp}) 404 405 } 406 func (h *dialHistory) remove(id onode.ID) bool { 407 for i, v := range *h { 408 if v.id == id { 409 heap.Remove(h, i) 410 return true 411 } 412 } 413 return false 414 } 415 func (h dialHistory) contains(id onode.ID) bool { 416 for _, v := range h { 417 if v.id == id { 418 return true 419 } 420 } 421 return false 422 } 423 func (h *dialHistory) expire(now time.Time) { 424 for h.Len() > 0 && h.min().exp.Before(now) { 425 heap.Pop(h) 426 } 427 } 428 429 // heap.Interface boilerplate 430 func (h dialHistory) Len() int { return len(h) } 431 func (h dialHistory) Less(i, j int) bool { return h[i].exp.Before(h[j].exp) } 432 func (h dialHistory) Swap(i, j int) { h[i], h[j] = h[j], h[i] } 433 func (h *dialHistory) Push(x interface{}) { 434 *h = append(*h, x.(pastDial)) 435 } 436 func (h *dialHistory) Pop() interface{} { 437 old := *h 438 n := len(old) 439 x := old[n-1] 440 *h = old[0 : n-1] 441 return x 442 }