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