github.com/phillinzzz/newBsc@v1.1.6/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 "math" 25 "math/big" 26 "sort" 27 "sync" 28 "sync/atomic" 29 "time" 30 31 "github.com/phillinzzz/newBsc/common" 32 "github.com/phillinzzz/newBsc/eth/protocols/eth" 33 "github.com/phillinzzz/newBsc/event" 34 "github.com/phillinzzz/newBsc/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 uint // 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 uint, peer Peer, logger log.Logger) *peerConnection { 117 return &peerConnection{ 118 id: id, 119 lacking: make(map[common.Hash]struct{}), 120 peer: peer, 121 version: version, 122 log: logger, 123 } 124 } 125 126 // Reset clears the internal state of a peer entity. 127 func (p *peerConnection) Reset() { 128 p.lock.Lock() 129 defer p.lock.Unlock() 130 131 atomic.StoreInt32(&p.headerIdle, 0) 132 atomic.StoreInt32(&p.blockIdle, 0) 133 atomic.StoreInt32(&p.receiptIdle, 0) 134 atomic.StoreInt32(&p.stateIdle, 0) 135 136 p.headerThroughput = 0 137 p.blockThroughput = 0 138 p.receiptThroughput = 0 139 p.stateThroughput = 0 140 141 p.lacking = make(map[common.Hash]struct{}) 142 } 143 144 // FetchHeaders sends a header retrieval request to the remote peer. 145 func (p *peerConnection) FetchHeaders(from uint64, count int) error { 146 // Short circuit if the peer is already fetching 147 if !atomic.CompareAndSwapInt32(&p.headerIdle, 0, 1) { 148 return errAlreadyFetching 149 } 150 p.headerStarted = time.Now() 151 152 // Issue the header retrieval request (absolute upwards without gaps) 153 154 go p.peer.RequestHeadersByNumber(from, count, 0, false) 155 return nil 156 } 157 158 // FetchBodies sends a block body retrieval request to the remote peer. 159 func (p *peerConnection) FetchBodies(request *fetchRequest) error { 160 // Short circuit if the peer is already fetching 161 if !atomic.CompareAndSwapInt32(&p.blockIdle, 0, 1) { 162 return errAlreadyFetching 163 } 164 p.blockStarted = time.Now() 165 166 go func() { 167 // Convert the header set to a retrievable slice 168 hashes := make([]common.Hash, 0, len(request.Headers)) 169 for _, header := range request.Headers { 170 hashes = append(hashes, header.Hash()) 171 } 172 p.peer.RequestBodies(hashes) 173 }() 174 175 return nil 176 } 177 178 // FetchReceipts sends a receipt retrieval request to the remote peer. 179 func (p *peerConnection) FetchReceipts(request *fetchRequest) error { 180 // Short circuit if the peer is already fetching 181 if !atomic.CompareAndSwapInt32(&p.receiptIdle, 0, 1) { 182 return errAlreadyFetching 183 } 184 p.receiptStarted = time.Now() 185 186 go func() { 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 p.peer.RequestReceipts(hashes) 193 }() 194 195 return nil 196 } 197 198 // FetchNodeData sends a node state data retrieval request to the remote peer. 199 func (p *peerConnection) FetchNodeData(hashes []common.Hash) error { 200 // Short circuit if the peer is already fetching 201 if !atomic.CompareAndSwapInt32(&p.stateIdle, 0, 1) { 202 return errAlreadyFetching 203 } 204 p.stateStarted = time.Now() 205 go p.peer.RequestNodeData(hashes) 206 207 return nil 208 } 209 210 // SetHeadersIdle sets the peer to idle, allowing it to execute new header retrieval 211 // requests. Its estimated header retrieval throughput is updated with that measured 212 // just now. 213 func (p *peerConnection) SetHeadersIdle(delivered int, deliveryTime time.Time) { 214 p.setIdle(deliveryTime.Sub(p.headerStarted), delivered, &p.headerThroughput, &p.headerIdle) 215 } 216 217 // SetBodiesIdle sets the peer to idle, allowing it to execute block body retrieval 218 // requests. Its estimated body retrieval throughput is updated with that measured 219 // just now. 220 func (p *peerConnection) SetBodiesIdle(delivered int, deliveryTime time.Time) { 221 p.setIdle(deliveryTime.Sub(p.blockStarted), delivered, &p.blockThroughput, &p.blockIdle) 222 } 223 224 // SetReceiptsIdle sets the peer to idle, allowing it to execute new receipt 225 // retrieval requests. Its estimated receipt retrieval throughput is updated 226 // with that measured just now. 227 func (p *peerConnection) SetReceiptsIdle(delivered int, deliveryTime time.Time) { 228 p.setIdle(deliveryTime.Sub(p.receiptStarted), delivered, &p.receiptThroughput, &p.receiptIdle) 229 } 230 231 // SetNodeDataIdle sets the peer to idle, allowing it to execute new state trie 232 // data retrieval requests. Its estimated state retrieval throughput is updated 233 // with that measured just now. 234 func (p *peerConnection) SetNodeDataIdle(delivered int, deliveryTime time.Time) { 235 p.setIdle(deliveryTime.Sub(p.stateStarted), delivered, &p.stateThroughput, &p.stateIdle) 236 } 237 238 // setIdle sets the peer to idle, allowing it to execute new retrieval requests. 239 // Its estimated retrieval throughput is updated with that measured just now. 240 func (p *peerConnection) setIdle(elapsed time.Duration, delivered int, throughput *float64, idle *int32) { 241 // Irrelevant of the scaling, make sure the peer ends up idle 242 defer atomic.StoreInt32(idle, 0) 243 244 p.lock.Lock() 245 defer p.lock.Unlock() 246 247 // If nothing was delivered (hard timeout / unavailable data), reduce throughput to minimum 248 if delivered == 0 { 249 *throughput = 0 250 return 251 } 252 // Otherwise update the throughput with a new measurement 253 if elapsed <= 0 { 254 elapsed = 1 // +1 (ns) to ensure non-zero divisor 255 } 256 measured := float64(delivered) / (float64(elapsed) / float64(time.Second)) 257 258 *throughput = (1-measurementImpact)*(*throughput) + measurementImpact*measured 259 p.rtt = time.Duration((1-measurementImpact)*float64(p.rtt) + measurementImpact*float64(elapsed)) 260 261 p.log.Trace("Peer throughput measurements updated", 262 "hps", p.headerThroughput, "bps", p.blockThroughput, 263 "rps", p.receiptThroughput, "sps", p.stateThroughput, 264 "miss", len(p.lacking), "rtt", p.rtt) 265 } 266 267 // HeaderCapacity retrieves the peers header download allowance based on its 268 // previously discovered throughput. 269 func (p *peerConnection) HeaderCapacity(targetRTT time.Duration) int { 270 p.lock.RLock() 271 defer p.lock.RUnlock() 272 273 return int(math.Min(1+math.Max(1, p.headerThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxHeaderFetch))) 274 } 275 276 // BlockCapacity retrieves the peers block download allowance based on its 277 // previously discovered throughput. 278 func (p *peerConnection) BlockCapacity(targetRTT time.Duration) int { 279 p.lock.RLock() 280 defer p.lock.RUnlock() 281 282 return int(math.Min(1+math.Max(1, p.blockThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxBlockFetch))) 283 } 284 285 // ReceiptCapacity retrieves the peers receipt download allowance based on its 286 // previously discovered throughput. 287 func (p *peerConnection) ReceiptCapacity(targetRTT time.Duration) int { 288 p.lock.RLock() 289 defer p.lock.RUnlock() 290 291 return int(math.Min(1+math.Max(1, p.receiptThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxReceiptFetch))) 292 } 293 294 // NodeDataCapacity retrieves the peers state download allowance based on its 295 // previously discovered throughput. 296 func (p *peerConnection) NodeDataCapacity(targetRTT time.Duration) int { 297 p.lock.RLock() 298 defer p.lock.RUnlock() 299 300 return int(math.Min(1+math.Max(1, p.stateThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxStateFetch))) 301 } 302 303 // MarkLacking appends a new entity to the set of items (blocks, receipts, states) 304 // that a peer is known not to have (i.e. have been requested before). If the 305 // set reaches its maximum allowed capacity, items are randomly dropped off. 306 func (p *peerConnection) MarkLacking(hash common.Hash) { 307 p.lock.Lock() 308 defer p.lock.Unlock() 309 310 for len(p.lacking) >= maxLackingHashes { 311 for drop := range p.lacking { 312 delete(p.lacking, drop) 313 break 314 } 315 } 316 p.lacking[hash] = struct{}{} 317 } 318 319 // Lacks retrieves whether the hash of a blockchain item is on the peers lacking 320 // list (i.e. whether we know that the peer does not have it). 321 func (p *peerConnection) Lacks(hash common.Hash) bool { 322 p.lock.RLock() 323 defer p.lock.RUnlock() 324 325 _, ok := p.lacking[hash] 326 return ok 327 } 328 329 // peerSet represents the collection of active peer participating in the chain 330 // download procedure. 331 type peerSet struct { 332 peers map[string]*peerConnection 333 newPeerFeed event.Feed 334 peerDropFeed event.Feed 335 lock sync.RWMutex 336 } 337 338 // newPeerSet creates a new peer set top track the active download sources. 339 func newPeerSet() *peerSet { 340 return &peerSet{ 341 peers: make(map[string]*peerConnection), 342 } 343 } 344 345 // SubscribeNewPeers subscribes to peer arrival events. 346 func (ps *peerSet) SubscribeNewPeers(ch chan<- *peerConnection) event.Subscription { 347 return ps.newPeerFeed.Subscribe(ch) 348 } 349 350 // SubscribePeerDrops subscribes to peer departure events. 351 func (ps *peerSet) SubscribePeerDrops(ch chan<- *peerConnection) event.Subscription { 352 return ps.peerDropFeed.Subscribe(ch) 353 } 354 355 // Reset iterates over the current peer set, and resets each of the known peers 356 // to prepare for a next batch of block retrieval. 357 func (ps *peerSet) Reset() { 358 ps.lock.RLock() 359 defer ps.lock.RUnlock() 360 361 for _, peer := range ps.peers { 362 peer.Reset() 363 } 364 } 365 366 // Register injects a new peer into the working set, or returns an error if the 367 // peer is already known. 368 // 369 // The method also sets the starting throughput values of the new peer to the 370 // average of all existing peers, to give it a realistic chance of being used 371 // for data retrievals. 372 func (ps *peerSet) Register(p *peerConnection) error { 373 // Retrieve the current median RTT as a sane default 374 p.rtt = ps.medianRTT() 375 376 // Register the new peer with some meaningful defaults 377 ps.lock.Lock() 378 if _, ok := ps.peers[p.id]; ok { 379 ps.lock.Unlock() 380 return errAlreadyRegistered 381 } 382 if len(ps.peers) > 0 { 383 p.headerThroughput, p.blockThroughput, p.receiptThroughput, p.stateThroughput = 0, 0, 0, 0 384 385 for _, peer := range ps.peers { 386 peer.lock.RLock() 387 p.headerThroughput += peer.headerThroughput 388 p.blockThroughput += peer.blockThroughput 389 p.receiptThroughput += peer.receiptThroughput 390 p.stateThroughput += peer.stateThroughput 391 peer.lock.RUnlock() 392 } 393 p.headerThroughput /= float64(len(ps.peers)) 394 p.blockThroughput /= float64(len(ps.peers)) 395 p.receiptThroughput /= float64(len(ps.peers)) 396 p.stateThroughput /= float64(len(ps.peers)) 397 } 398 ps.peers[p.id] = p 399 ps.lock.Unlock() 400 401 ps.newPeerFeed.Send(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 p, ok := ps.peers[id] 410 if !ok { 411 ps.lock.Unlock() 412 return errNotRegistered 413 } 414 delete(ps.peers, id) 415 ps.lock.Unlock() 416 417 ps.peerDropFeed.Send(p) 418 return nil 419 } 420 421 // Peer retrieves the registered peer with the given id. 422 func (ps *peerSet) Peer(id string) *peerConnection { 423 ps.lock.RLock() 424 defer ps.lock.RUnlock() 425 426 return ps.peers[id] 427 } 428 429 // Len returns if the current number of peers in the set. 430 func (ps *peerSet) Len() int { 431 ps.lock.RLock() 432 defer ps.lock.RUnlock() 433 434 return len(ps.peers) 435 } 436 437 // AllPeers retrieves a flat list of all the peers within the set. 438 func (ps *peerSet) AllPeers() []*peerConnection { 439 ps.lock.RLock() 440 defer ps.lock.RUnlock() 441 442 list := make([]*peerConnection, 0, len(ps.peers)) 443 for _, p := range ps.peers { 444 list = append(list, p) 445 } 446 return list 447 } 448 449 // HeaderIdlePeers retrieves a flat list of all the currently header-idle peers 450 // within the active peer set, ordered by their reputation. 451 func (ps *peerSet) HeaderIdlePeers() ([]*peerConnection, int) { 452 idle := func(p *peerConnection) bool { 453 return atomic.LoadInt32(&p.headerIdle) == 0 454 } 455 throughput := func(p *peerConnection) float64 { 456 p.lock.RLock() 457 defer p.lock.RUnlock() 458 return p.headerThroughput 459 } 460 return ps.idlePeers(eth.ETH65, eth.ETH67, idle, throughput) 461 } 462 463 // BodyIdlePeers retrieves a flat list of all the currently body-idle peers within 464 // the active peer set, ordered by their reputation. 465 func (ps *peerSet) BodyIdlePeers() ([]*peerConnection, int) { 466 idle := func(p *peerConnection) bool { 467 return atomic.LoadInt32(&p.blockIdle) == 0 468 } 469 throughput := func(p *peerConnection) float64 { 470 p.lock.RLock() 471 defer p.lock.RUnlock() 472 return p.blockThroughput 473 } 474 return ps.idlePeers(eth.ETH65, eth.ETH67, idle, throughput) 475 } 476 477 // ReceiptIdlePeers retrieves a flat list of all the currently receipt-idle peers 478 // within the active peer set, ordered by their reputation. 479 func (ps *peerSet) ReceiptIdlePeers() ([]*peerConnection, int) { 480 idle := func(p *peerConnection) bool { 481 return atomic.LoadInt32(&p.receiptIdle) == 0 482 } 483 throughput := func(p *peerConnection) float64 { 484 p.lock.RLock() 485 defer p.lock.RUnlock() 486 return p.receiptThroughput 487 } 488 return ps.idlePeers(eth.ETH65, eth.ETH67, idle, throughput) 489 } 490 491 // NodeDataIdlePeers retrieves a flat list of all the currently node-data-idle 492 // peers within the active peer set, ordered by their reputation. 493 func (ps *peerSet) NodeDataIdlePeers() ([]*peerConnection, int) { 494 idle := func(p *peerConnection) bool { 495 return atomic.LoadInt32(&p.stateIdle) == 0 496 } 497 throughput := func(p *peerConnection) float64 { 498 p.lock.RLock() 499 defer p.lock.RUnlock() 500 return p.stateThroughput 501 } 502 return ps.idlePeers(eth.ETH65, eth.ETH67, idle, throughput) 503 } 504 505 // idlePeers retrieves a flat list of all currently idle peers satisfying the 506 // protocol version constraints, using the provided function to check idleness. 507 // The resulting set of peers are sorted by their measure throughput. 508 func (ps *peerSet) idlePeers(minProtocol, maxProtocol uint, idleCheck func(*peerConnection) bool, throughput func(*peerConnection) float64) ([]*peerConnection, int) { 509 ps.lock.RLock() 510 defer ps.lock.RUnlock() 511 512 idle, total := make([]*peerConnection, 0, len(ps.peers)), 0 513 tps := make([]float64, 0, len(ps.peers)) 514 for _, p := range ps.peers { 515 if p.version >= minProtocol && p.version <= maxProtocol { 516 if idleCheck(p) { 517 idle = append(idle, p) 518 tps = append(tps, throughput(p)) 519 } 520 total++ 521 } 522 } 523 // And sort them 524 sortPeers := &peerThroughputSort{idle, tps} 525 sort.Sort(sortPeers) 526 return sortPeers.p, total 527 } 528 529 // medianRTT returns the median RTT of the peerset, considering only the tuning 530 // peers if there are more peers available. 531 func (ps *peerSet) medianRTT() time.Duration { 532 // Gather all the currently measured round trip times 533 ps.lock.RLock() 534 defer ps.lock.RUnlock() 535 536 rtts := make([]float64, 0, len(ps.peers)) 537 for _, p := range ps.peers { 538 p.lock.RLock() 539 rtts = append(rtts, float64(p.rtt)) 540 p.lock.RUnlock() 541 } 542 sort.Float64s(rtts) 543 544 median := rttMaxEstimate 545 if qosTuningPeers <= len(rtts) { 546 median = time.Duration(rtts[qosTuningPeers/2]) // Median of our tuning peers 547 } else if len(rtts) > 0 { 548 median = time.Duration(rtts[len(rtts)/2]) // Median of our connected peers (maintain even like this some baseline qos) 549 } 550 // Restrict the RTT into some QoS defaults, irrelevant of true RTT 551 if median < rttMinEstimate { 552 median = rttMinEstimate 553 } 554 if median > rttMaxEstimate { 555 median = rttMaxEstimate 556 } 557 return median 558 } 559 560 // peerThroughputSort implements the Sort interface, and allows for 561 // sorting a set of peers by their throughput 562 // The sorted data is with the _highest_ throughput first 563 type peerThroughputSort struct { 564 p []*peerConnection 565 tp []float64 566 } 567 568 func (ps *peerThroughputSort) Len() int { 569 return len(ps.p) 570 } 571 572 func (ps *peerThroughputSort) Less(i, j int) bool { 573 return ps.tp[i] > ps.tp[j] 574 } 575 576 func (ps *peerThroughputSort) Swap(i, j int) { 577 ps.p[i], ps.p[j] = ps.p[j], ps.p[i] 578 ps.tp[i], ps.tp[j] = ps.tp[j], ps.tp[i] 579 }