github.com/clem109/go-ethereum@v1.8.3-0.20180316121352-fe6cf00f480a/eth/downloader/peer.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 // Contains the active peer-set of the downloader, maintaining both failures 18 // as well as reputation metrics to prioritize the block retrievals. 19 20 package downloader 21 22 import ( 23 "errors" 24 "fmt" 25 "math" 26 "math/big" 27 "sort" 28 "sync" 29 "sync/atomic" 30 "time" 31 32 "github.com/ethereum/go-ethereum/common" 33 "github.com/ethereum/go-ethereum/event" 34 "github.com/ethereum/go-ethereum/log" 35 ) 36 37 const ( 38 maxLackingHashes = 4096 // Maximum number of entries allowed on the list or lacking items 39 measurementImpact = 0.1 // The impact a single measurement has on a peer's final throughput value. 40 ) 41 42 var ( 43 errAlreadyFetching = errors.New("already fetching blocks from peer") 44 errAlreadyRegistered = errors.New("peer is already registered") 45 errNotRegistered = errors.New("peer is not registered") 46 ) 47 48 // peerConnection represents an active peer from which hashes and blocks are retrieved. 49 type peerConnection struct { 50 id string // Unique identifier of the peer 51 52 headerIdle int32 // Current header activity state of the peer (idle = 0, active = 1) 53 blockIdle int32 // Current block activity state of the peer (idle = 0, active = 1) 54 receiptIdle int32 // Current receipt activity state of the peer (idle = 0, active = 1) 55 stateIdle int32 // Current node data activity state of the peer (idle = 0, active = 1) 56 57 headerThroughput float64 // Number of headers measured to be retrievable per second 58 blockThroughput float64 // Number of blocks (bodies) measured to be retrievable per second 59 receiptThroughput float64 // Number of receipts measured to be retrievable per second 60 stateThroughput float64 // Number of node data pieces measured to be retrievable per second 61 62 rtt time.Duration // Request round trip time to track responsiveness (QoS) 63 64 headerStarted time.Time // Time instance when the last header fetch was started 65 blockStarted time.Time // Time instance when the last block (body) fetch was started 66 receiptStarted time.Time // Time instance when the last receipt fetch was started 67 stateStarted time.Time // Time instance when the last node data fetch was started 68 69 lacking map[common.Hash]struct{} // Set of hashes not to request (didn't have previously) 70 71 peer Peer 72 73 version int // Eth protocol version number to switch strategies 74 log log.Logger // Contextual logger to add extra infos to peer logs 75 lock sync.RWMutex 76 } 77 78 // LightPeer encapsulates the methods required to synchronise with a remote light peer. 79 type LightPeer interface { 80 Head() (common.Hash, *big.Int) 81 RequestHeadersByHash(common.Hash, int, int, bool) error 82 RequestHeadersByNumber(uint64, int, int, bool) error 83 } 84 85 // Peer encapsulates the methods required to synchronise with a remote full peer. 86 type Peer interface { 87 LightPeer 88 RequestBodies([]common.Hash) error 89 RequestReceipts([]common.Hash) error 90 RequestNodeData([]common.Hash) error 91 } 92 93 // lightPeerWrapper wraps a LightPeer struct, stubbing out the Peer-only methods. 94 type lightPeerWrapper struct { 95 peer LightPeer 96 } 97 98 func (w *lightPeerWrapper) Head() (common.Hash, *big.Int) { return w.peer.Head() } 99 func (w *lightPeerWrapper) RequestHeadersByHash(h common.Hash, amount int, skip int, reverse bool) error { 100 return w.peer.RequestHeadersByHash(h, amount, skip, reverse) 101 } 102 func (w *lightPeerWrapper) RequestHeadersByNumber(i uint64, amount int, skip int, reverse bool) error { 103 return w.peer.RequestHeadersByNumber(i, amount, skip, reverse) 104 } 105 func (w *lightPeerWrapper) RequestBodies([]common.Hash) error { 106 panic("RequestBodies not supported in light client mode sync") 107 } 108 func (w *lightPeerWrapper) RequestReceipts([]common.Hash) error { 109 panic("RequestReceipts not supported in light client mode sync") 110 } 111 func (w *lightPeerWrapper) RequestNodeData([]common.Hash) error { 112 panic("RequestNodeData not supported in light client mode sync") 113 } 114 115 // newPeerConnection creates a new downloader peer. 116 func newPeerConnection(id string, version int, peer Peer, logger log.Logger) *peerConnection { 117 return &peerConnection{ 118 id: id, 119 lacking: make(map[common.Hash]struct{}), 120 121 peer: peer, 122 123 version: version, 124 log: logger, 125 } 126 } 127 128 // Reset clears the internal state of a peer entity. 129 func (p *peerConnection) Reset() { 130 p.lock.Lock() 131 defer p.lock.Unlock() 132 133 atomic.StoreInt32(&p.headerIdle, 0) 134 atomic.StoreInt32(&p.blockIdle, 0) 135 atomic.StoreInt32(&p.receiptIdle, 0) 136 atomic.StoreInt32(&p.stateIdle, 0) 137 138 p.headerThroughput = 0 139 p.blockThroughput = 0 140 p.receiptThroughput = 0 141 p.stateThroughput = 0 142 143 p.lacking = make(map[common.Hash]struct{}) 144 } 145 146 // FetchHeaders sends a header retrieval request to the remote peer. 147 func (p *peerConnection) FetchHeaders(from uint64, count int) error { 148 // Sanity check the protocol version 149 if p.version < 62 { 150 panic(fmt.Sprintf("header fetch [eth/62+] requested on eth/%d", p.version)) 151 } 152 // Short circuit if the peer is already fetching 153 if !atomic.CompareAndSwapInt32(&p.headerIdle, 0, 1) { 154 return errAlreadyFetching 155 } 156 p.headerStarted = time.Now() 157 158 // Issue the header retrieval request (absolut upwards without gaps) 159 go p.peer.RequestHeadersByNumber(from, count, 0, false) 160 161 return nil 162 } 163 164 // FetchBodies sends a block body retrieval request to the remote peer. 165 func (p *peerConnection) FetchBodies(request *fetchRequest) error { 166 // Sanity check the protocol version 167 if p.version < 62 { 168 panic(fmt.Sprintf("body fetch [eth/62+] requested on eth/%d", p.version)) 169 } 170 // Short circuit if the peer is already fetching 171 if !atomic.CompareAndSwapInt32(&p.blockIdle, 0, 1) { 172 return errAlreadyFetching 173 } 174 p.blockStarted = time.Now() 175 176 // Convert the header set to a retrievable slice 177 hashes := make([]common.Hash, 0, len(request.Headers)) 178 for _, header := range request.Headers { 179 hashes = append(hashes, header.Hash()) 180 } 181 go p.peer.RequestBodies(hashes) 182 183 return nil 184 } 185 186 // FetchReceipts sends a receipt retrieval request to the remote peer. 187 func (p *peerConnection) FetchReceipts(request *fetchRequest) error { 188 // Sanity check the protocol version 189 if p.version < 63 { 190 panic(fmt.Sprintf("body fetch [eth/63+] requested on eth/%d", p.version)) 191 } 192 // Short circuit if the peer is already fetching 193 if !atomic.CompareAndSwapInt32(&p.receiptIdle, 0, 1) { 194 return errAlreadyFetching 195 } 196 p.receiptStarted = time.Now() 197 198 // Convert the header set to a retrievable slice 199 hashes := make([]common.Hash, 0, len(request.Headers)) 200 for _, header := range request.Headers { 201 hashes = append(hashes, header.Hash()) 202 } 203 go p.peer.RequestReceipts(hashes) 204 205 return nil 206 } 207 208 // FetchNodeData sends a node state data retrieval request to the remote peer. 209 func (p *peerConnection) FetchNodeData(hashes []common.Hash) error { 210 // Sanity check the protocol version 211 if p.version < 63 { 212 panic(fmt.Sprintf("node data fetch [eth/63+] requested on eth/%d", p.version)) 213 } 214 // Short circuit if the peer is already fetching 215 if !atomic.CompareAndSwapInt32(&p.stateIdle, 0, 1) { 216 return errAlreadyFetching 217 } 218 p.stateStarted = time.Now() 219 220 go p.peer.RequestNodeData(hashes) 221 222 return nil 223 } 224 225 // SetHeadersIdle sets the peer to idle, allowing it to execute new header retrieval 226 // requests. Its estimated header retrieval throughput is updated with that measured 227 // just now. 228 func (p *peerConnection) SetHeadersIdle(delivered int) { 229 p.setIdle(p.headerStarted, delivered, &p.headerThroughput, &p.headerIdle) 230 } 231 232 // SetBlocksIdle sets the peer to idle, allowing it to execute new block retrieval 233 // requests. Its estimated block retrieval throughput is updated with that measured 234 // just now. 235 func (p *peerConnection) SetBlocksIdle(delivered int) { 236 p.setIdle(p.blockStarted, delivered, &p.blockThroughput, &p.blockIdle) 237 } 238 239 // SetBodiesIdle sets the peer to idle, allowing it to execute block body retrieval 240 // requests. Its estimated body retrieval throughput is updated with that measured 241 // just now. 242 func (p *peerConnection) SetBodiesIdle(delivered int) { 243 p.setIdle(p.blockStarted, delivered, &p.blockThroughput, &p.blockIdle) 244 } 245 246 // SetReceiptsIdle sets the peer to idle, allowing it to execute new receipt 247 // retrieval requests. Its estimated receipt retrieval throughput is updated 248 // with that measured just now. 249 func (p *peerConnection) SetReceiptsIdle(delivered int) { 250 p.setIdle(p.receiptStarted, delivered, &p.receiptThroughput, &p.receiptIdle) 251 } 252 253 // SetNodeDataIdle sets the peer to idle, allowing it to execute new state trie 254 // data retrieval requests. Its estimated state retrieval throughput is updated 255 // with that measured just now. 256 func (p *peerConnection) SetNodeDataIdle(delivered int) { 257 p.setIdle(p.stateStarted, delivered, &p.stateThroughput, &p.stateIdle) 258 } 259 260 // setIdle sets the peer to idle, allowing it to execute new retrieval requests. 261 // Its estimated retrieval throughput is updated with that measured just now. 262 func (p *peerConnection) setIdle(started time.Time, delivered int, throughput *float64, idle *int32) { 263 // Irrelevant of the scaling, make sure the peer ends up idle 264 defer atomic.StoreInt32(idle, 0) 265 266 p.lock.Lock() 267 defer p.lock.Unlock() 268 269 // If nothing was delivered (hard timeout / unavailable data), reduce throughput to minimum 270 if delivered == 0 { 271 *throughput = 0 272 return 273 } 274 // Otherwise update the throughput with a new measurement 275 elapsed := time.Since(started) + 1 // +1 (ns) to ensure non-zero divisor 276 measured := float64(delivered) / (float64(elapsed) / float64(time.Second)) 277 278 *throughput = (1-measurementImpact)*(*throughput) + measurementImpact*measured 279 p.rtt = time.Duration((1-measurementImpact)*float64(p.rtt) + measurementImpact*float64(elapsed)) 280 281 p.log.Trace("Peer throughput measurements updated", 282 "hps", p.headerThroughput, "bps", p.blockThroughput, 283 "rps", p.receiptThroughput, "sps", p.stateThroughput, 284 "miss", len(p.lacking), "rtt", p.rtt) 285 } 286 287 // HeaderCapacity retrieves the peers header download allowance based on its 288 // previously discovered throughput. 289 func (p *peerConnection) HeaderCapacity(targetRTT time.Duration) int { 290 p.lock.RLock() 291 defer p.lock.RUnlock() 292 293 return int(math.Min(1+math.Max(1, p.headerThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxHeaderFetch))) 294 } 295 296 // BlockCapacity retrieves the peers block download allowance based on its 297 // previously discovered throughput. 298 func (p *peerConnection) BlockCapacity(targetRTT time.Duration) int { 299 p.lock.RLock() 300 defer p.lock.RUnlock() 301 302 return int(math.Min(1+math.Max(1, p.blockThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxBlockFetch))) 303 } 304 305 // ReceiptCapacity retrieves the peers receipt download allowance based on its 306 // previously discovered throughput. 307 func (p *peerConnection) ReceiptCapacity(targetRTT time.Duration) int { 308 p.lock.RLock() 309 defer p.lock.RUnlock() 310 311 return int(math.Min(1+math.Max(1, p.receiptThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxReceiptFetch))) 312 } 313 314 // NodeDataCapacity retrieves the peers state download allowance based on its 315 // previously discovered throughput. 316 func (p *peerConnection) NodeDataCapacity(targetRTT time.Duration) int { 317 p.lock.RLock() 318 defer p.lock.RUnlock() 319 320 return int(math.Min(1+math.Max(1, p.stateThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxStateFetch))) 321 } 322 323 // MarkLacking appends a new entity to the set of items (blocks, receipts, states) 324 // that a peer is known not to have (i.e. have been requested before). If the 325 // set reaches its maximum allowed capacity, items are randomly dropped off. 326 func (p *peerConnection) MarkLacking(hash common.Hash) { 327 p.lock.Lock() 328 defer p.lock.Unlock() 329 330 for len(p.lacking) >= maxLackingHashes { 331 for drop := range p.lacking { 332 delete(p.lacking, drop) 333 break 334 } 335 } 336 p.lacking[hash] = struct{}{} 337 } 338 339 // Lacks retrieves whether the hash of a blockchain item is on the peers lacking 340 // list (i.e. whether we know that the peer does not have it). 341 func (p *peerConnection) Lacks(hash common.Hash) bool { 342 p.lock.RLock() 343 defer p.lock.RUnlock() 344 345 _, ok := p.lacking[hash] 346 return ok 347 } 348 349 // peerSet represents the collection of active peer participating in the chain 350 // download procedure. 351 type peerSet struct { 352 peers map[string]*peerConnection 353 newPeerFeed event.Feed 354 peerDropFeed event.Feed 355 lock sync.RWMutex 356 } 357 358 // newPeerSet creates a new peer set top track the active download sources. 359 func newPeerSet() *peerSet { 360 return &peerSet{ 361 peers: make(map[string]*peerConnection), 362 } 363 } 364 365 // SubscribeNewPeers subscribes to peer arrival events. 366 func (ps *peerSet) SubscribeNewPeers(ch chan<- *peerConnection) event.Subscription { 367 return ps.newPeerFeed.Subscribe(ch) 368 } 369 370 // SubscribePeerDrops subscribes to peer departure events. 371 func (ps *peerSet) SubscribePeerDrops(ch chan<- *peerConnection) event.Subscription { 372 return ps.peerDropFeed.Subscribe(ch) 373 } 374 375 // Reset iterates over the current peer set, and resets each of the known peers 376 // to prepare for a next batch of block retrieval. 377 func (ps *peerSet) Reset() { 378 ps.lock.RLock() 379 defer ps.lock.RUnlock() 380 381 for _, peer := range ps.peers { 382 peer.Reset() 383 } 384 } 385 386 // Register injects a new peer into the working set, or returns an error if the 387 // peer is already known. 388 // 389 // The method also sets the starting throughput values of the new peer to the 390 // average of all existing peers, to give it a realistic chance of being used 391 // for data retrievals. 392 func (ps *peerSet) Register(p *peerConnection) error { 393 // Retrieve the current median RTT as a sane default 394 p.rtt = ps.medianRTT() 395 396 // Register the new peer with some meaningful defaults 397 ps.lock.Lock() 398 if _, ok := ps.peers[p.id]; ok { 399 ps.lock.Unlock() 400 return errAlreadyRegistered 401 } 402 if len(ps.peers) > 0 { 403 p.headerThroughput, p.blockThroughput, p.receiptThroughput, p.stateThroughput = 0, 0, 0, 0 404 405 for _, peer := range ps.peers { 406 peer.lock.RLock() 407 p.headerThroughput += peer.headerThroughput 408 p.blockThroughput += peer.blockThroughput 409 p.receiptThroughput += peer.receiptThroughput 410 p.stateThroughput += peer.stateThroughput 411 peer.lock.RUnlock() 412 } 413 p.headerThroughput /= float64(len(ps.peers)) 414 p.blockThroughput /= float64(len(ps.peers)) 415 p.receiptThroughput /= float64(len(ps.peers)) 416 p.stateThroughput /= float64(len(ps.peers)) 417 } 418 ps.peers[p.id] = p 419 ps.lock.Unlock() 420 421 ps.newPeerFeed.Send(p) 422 return nil 423 } 424 425 // Unregister removes a remote peer from the active set, disabling any further 426 // actions to/from that particular entity. 427 func (ps *peerSet) Unregister(id string) error { 428 ps.lock.Lock() 429 p, ok := ps.peers[id] 430 if !ok { 431 defer ps.lock.Unlock() 432 return errNotRegistered 433 } 434 delete(ps.peers, id) 435 ps.lock.Unlock() 436 437 ps.peerDropFeed.Send(p) 438 return nil 439 } 440 441 // Peer retrieves the registered peer with the given id. 442 func (ps *peerSet) Peer(id string) *peerConnection { 443 ps.lock.RLock() 444 defer ps.lock.RUnlock() 445 446 return ps.peers[id] 447 } 448 449 // Len returns if the current number of peers in the set. 450 func (ps *peerSet) Len() int { 451 ps.lock.RLock() 452 defer ps.lock.RUnlock() 453 454 return len(ps.peers) 455 } 456 457 // AllPeers retrieves a flat list of all the peers within the set. 458 func (ps *peerSet) AllPeers() []*peerConnection { 459 ps.lock.RLock() 460 defer ps.lock.RUnlock() 461 462 list := make([]*peerConnection, 0, len(ps.peers)) 463 for _, p := range ps.peers { 464 list = append(list, p) 465 } 466 return list 467 } 468 469 // HeaderIdlePeers retrieves a flat list of all the currently header-idle peers 470 // within the active peer set, ordered by their reputation. 471 func (ps *peerSet) HeaderIdlePeers() ([]*peerConnection, int) { 472 idle := func(p *peerConnection) bool { 473 return atomic.LoadInt32(&p.headerIdle) == 0 474 } 475 throughput := func(p *peerConnection) float64 { 476 p.lock.RLock() 477 defer p.lock.RUnlock() 478 return p.headerThroughput 479 } 480 return ps.idlePeers(62, 64, idle, throughput) 481 } 482 483 // BodyIdlePeers retrieves a flat list of all the currently body-idle peers within 484 // the active peer set, ordered by their reputation. 485 func (ps *peerSet) BodyIdlePeers() ([]*peerConnection, int) { 486 idle := func(p *peerConnection) bool { 487 return atomic.LoadInt32(&p.blockIdle) == 0 488 } 489 throughput := func(p *peerConnection) float64 { 490 p.lock.RLock() 491 defer p.lock.RUnlock() 492 return p.blockThroughput 493 } 494 return ps.idlePeers(62, 64, idle, throughput) 495 } 496 497 // ReceiptIdlePeers retrieves a flat list of all the currently receipt-idle peers 498 // within the active peer set, ordered by their reputation. 499 func (ps *peerSet) ReceiptIdlePeers() ([]*peerConnection, int) { 500 idle := func(p *peerConnection) bool { 501 return atomic.LoadInt32(&p.receiptIdle) == 0 502 } 503 throughput := func(p *peerConnection) float64 { 504 p.lock.RLock() 505 defer p.lock.RUnlock() 506 return p.receiptThroughput 507 } 508 return ps.idlePeers(63, 64, idle, throughput) 509 } 510 511 // NodeDataIdlePeers retrieves a flat list of all the currently node-data-idle 512 // peers within the active peer set, ordered by their reputation. 513 func (ps *peerSet) NodeDataIdlePeers() ([]*peerConnection, int) { 514 idle := func(p *peerConnection) bool { 515 return atomic.LoadInt32(&p.stateIdle) == 0 516 } 517 throughput := func(p *peerConnection) float64 { 518 p.lock.RLock() 519 defer p.lock.RUnlock() 520 return p.stateThroughput 521 } 522 return ps.idlePeers(63, 64, idle, throughput) 523 } 524 525 // idlePeers retrieves a flat list of all currently idle peers satisfying the 526 // protocol version constraints, using the provided function to check idleness. 527 // The resulting set of peers are sorted by their measure throughput. 528 func (ps *peerSet) idlePeers(minProtocol, maxProtocol int, idleCheck func(*peerConnection) bool, throughput func(*peerConnection) float64) ([]*peerConnection, int) { 529 ps.lock.RLock() 530 defer ps.lock.RUnlock() 531 532 idle, total := make([]*peerConnection, 0, len(ps.peers)), 0 533 for _, p := range ps.peers { 534 if p.version >= minProtocol && p.version <= maxProtocol { 535 if idleCheck(p) { 536 idle = append(idle, p) 537 } 538 total++ 539 } 540 } 541 for i := 0; i < len(idle); i++ { 542 for j := i + 1; j < len(idle); j++ { 543 if throughput(idle[i]) < throughput(idle[j]) { 544 idle[i], idle[j] = idle[j], idle[i] 545 } 546 } 547 } 548 return idle, total 549 } 550 551 // medianRTT returns the median RTT of the peerset, considering only the tuning 552 // peers if there are more peers available. 553 func (ps *peerSet) medianRTT() time.Duration { 554 // Gather all the currnetly measured round trip times 555 ps.lock.RLock() 556 defer ps.lock.RUnlock() 557 558 rtts := make([]float64, 0, len(ps.peers)) 559 for _, p := range ps.peers { 560 p.lock.RLock() 561 rtts = append(rtts, float64(p.rtt)) 562 p.lock.RUnlock() 563 } 564 sort.Float64s(rtts) 565 566 median := rttMaxEstimate 567 if qosTuningPeers <= len(rtts) { 568 median = time.Duration(rtts[qosTuningPeers/2]) // Median of our tuning peers 569 } else if len(rtts) > 0 { 570 median = time.Duration(rtts[len(rtts)/2]) // Median of our connected peers (maintain even like this some baseline qos) 571 } 572 // Restrict the RTT into some QoS defaults, irrelevant of true RTT 573 if median < rttMinEstimate { 574 median = rttMinEstimate 575 } 576 if median > rttMaxEstimate { 577 median = rttMaxEstimate 578 } 579 return median 580 }