github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/eth/fetcher/fetcher.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 fetcher contains the block announcement based synchronisation. 18 package fetcher 19 20 import ( 21 "errors" 22 "fmt" 23 "math/rand" 24 "time" 25 26 "github.com/atheioschain/go-atheios/common" 27 "github.com/atheioschain/go-atheios/core" 28 "github.com/atheioschain/go-atheios/core/types" 29 "github.com/atheioschain/go-atheios/logger" 30 "github.com/atheioschain/go-atheios/logger/glog" 31 "gopkg.in/karalabe/cookiejar.v2/collections/prque" 32 ) 33 34 const ( 35 arriveTimeout = 500 * time.Millisecond // Time allowance before an announced block is explicitly requested 36 gatherSlack = 100 * time.Millisecond // Interval used to collate almost-expired announces with fetches 37 fetchTimeout = 5 * time.Second // Maximum allotted time to return an explicitly requested block 38 maxUncleDist = 7 // Maximum allowed backward distance from the chain head 39 maxQueueDist = 32 // Maximum allowed distance from the chain head to queue 40 hashLimit = 256 // Maximum number of unique blocks a peer may have announced 41 blockLimit = 64 // Maximum number of unique blocks a peer may have delivered 42 ) 43 44 var ( 45 errTerminated = errors.New("terminated") 46 ) 47 48 // blockRetrievalFn is a callback type for retrieving a block from the local chain. 49 type blockRetrievalFn func(common.Hash) *types.Block 50 51 // headerRequesterFn is a callback type for sending a header retrieval request. 52 type headerRequesterFn func(common.Hash) error 53 54 // bodyRequesterFn is a callback type for sending a body retrieval request. 55 type bodyRequesterFn func([]common.Hash) error 56 57 // blockValidatorFn is a callback type to verify a block's header for fast propagation. 58 type blockValidatorFn func(block *types.Block, parent *types.Block) error 59 60 // blockBroadcasterFn is a callback type for broadcasting a block to connected peers. 61 type blockBroadcasterFn func(block *types.Block, propagate bool) 62 63 // chainHeightFn is a callback type to retrieve the current chain height. 64 type chainHeightFn func() uint64 65 66 // chainInsertFn is a callback type to insert a batch of blocks into the local chain. 67 type chainInsertFn func(types.Blocks) (int, error) 68 69 // peerDropFn is a callback type for dropping a peer detected as malicious. 70 type peerDropFn func(id string) 71 72 // announce is the hash notification of the availability of a new block in the 73 // network. 74 type announce struct { 75 hash common.Hash // Hash of the block being announced 76 number uint64 // Number of the block being announced (0 = unknown | old protocol) 77 header *types.Header // Header of the block partially reassembled (new protocol) 78 time time.Time // Timestamp of the announcement 79 80 origin string // Identifier of the peer originating the notification 81 82 fetchHeader headerRequesterFn // [eth/62] Fetcher function to retrieve the header of an announced block 83 fetchBodies bodyRequesterFn // [eth/62] Fetcher function to retrieve the body of an announced block 84 } 85 86 // headerFilterTask represents a batch of headers needing fetcher filtering. 87 type headerFilterTask struct { 88 headers []*types.Header // Collection of headers to filter 89 time time.Time // Arrival time of the headers 90 } 91 92 // headerFilterTask represents a batch of block bodies (transactions and uncles) 93 // needing fetcher filtering. 94 type bodyFilterTask struct { 95 transactions [][]*types.Transaction // Collection of transactions per block bodies 96 uncles [][]*types.Header // Collection of uncles per block bodies 97 time time.Time // Arrival time of the blocks' contents 98 } 99 100 // inject represents a schedules import operation. 101 type inject struct { 102 origin string 103 block *types.Block 104 } 105 106 // Fetcher is responsible for accumulating block announcements from various peers 107 // and scheduling them for retrieval. 108 type Fetcher struct { 109 // Various event channels 110 notify chan *announce 111 inject chan *inject 112 113 blockFilter chan chan []*types.Block 114 headerFilter chan chan *headerFilterTask 115 bodyFilter chan chan *bodyFilterTask 116 117 done chan common.Hash 118 quit chan struct{} 119 120 // Announce states 121 announces map[string]int // Per peer announce counts to prevent memory exhaustion 122 announced map[common.Hash][]*announce // Announced blocks, scheduled for fetching 123 fetching map[common.Hash]*announce // Announced blocks, currently fetching 124 fetched map[common.Hash][]*announce // Blocks with headers fetched, scheduled for body retrieval 125 completing map[common.Hash]*announce // Blocks with headers, currently body-completing 126 127 // Block cache 128 queue *prque.Prque // Queue containing the import operations (block number sorted) 129 queues map[string]int // Per peer block counts to prevent memory exhaustion 130 queued map[common.Hash]*inject // Set of already queued blocks (to dedup imports) 131 132 // Callbacks 133 getBlock blockRetrievalFn // Retrieves a block from the local chain 134 validateBlock blockValidatorFn // Checks if a block's headers have a valid proof of work 135 broadcastBlock blockBroadcasterFn // Broadcasts a block to connected peers 136 chainHeight chainHeightFn // Retrieves the current chain's height 137 insertChain chainInsertFn // Injects a batch of blocks into the chain 138 dropPeer peerDropFn // Drops a peer for misbehaving 139 140 // Testing hooks 141 announceChangeHook func(common.Hash, bool) // Method to call upon adding or deleting a hash from the announce list 142 queueChangeHook func(common.Hash, bool) // Method to call upon adding or deleting a block from the import queue 143 fetchingHook func([]common.Hash) // Method to call upon starting a block (eth/61) or header (eth/62) fetch 144 completingHook func([]common.Hash) // Method to call upon starting a block body fetch (eth/62) 145 importedHook func(*types.Block) // Method to call upon successful block import (both eth/61 and eth/62) 146 } 147 148 // New creates a block fetcher to retrieve blocks based on hash announcements. 149 func New(getBlock blockRetrievalFn, validateBlock blockValidatorFn, broadcastBlock blockBroadcasterFn, chainHeight chainHeightFn, insertChain chainInsertFn, dropPeer peerDropFn) *Fetcher { 150 return &Fetcher{ 151 notify: make(chan *announce), 152 inject: make(chan *inject), 153 blockFilter: make(chan chan []*types.Block), 154 headerFilter: make(chan chan *headerFilterTask), 155 bodyFilter: make(chan chan *bodyFilterTask), 156 done: make(chan common.Hash), 157 quit: make(chan struct{}), 158 announces: make(map[string]int), 159 announced: make(map[common.Hash][]*announce), 160 fetching: make(map[common.Hash]*announce), 161 fetched: make(map[common.Hash][]*announce), 162 completing: make(map[common.Hash]*announce), 163 queue: prque.New(), 164 queues: make(map[string]int), 165 queued: make(map[common.Hash]*inject), 166 getBlock: getBlock, 167 validateBlock: validateBlock, 168 broadcastBlock: broadcastBlock, 169 chainHeight: chainHeight, 170 insertChain: insertChain, 171 dropPeer: dropPeer, 172 } 173 } 174 175 // Start boots up the announcement based synchroniser, accepting and processing 176 // hash notifications and block fetches until termination requested. 177 func (f *Fetcher) Start() { 178 go f.loop() 179 } 180 181 // Stop terminates the announcement based synchroniser, canceling all pending 182 // operations. 183 func (f *Fetcher) Stop() { 184 close(f.quit) 185 } 186 187 // Notify announces the fetcher of the potential availability of a new block in 188 // the network. 189 func (f *Fetcher) Notify(peer string, hash common.Hash, number uint64, time time.Time, 190 headerFetcher headerRequesterFn, bodyFetcher bodyRequesterFn) error { 191 block := &announce{ 192 hash: hash, 193 number: number, 194 time: time, 195 origin: peer, 196 fetchHeader: headerFetcher, 197 fetchBodies: bodyFetcher, 198 } 199 select { 200 case f.notify <- block: 201 return nil 202 case <-f.quit: 203 return errTerminated 204 } 205 } 206 207 // Enqueue tries to fill gaps the the fetcher's future import queue. 208 func (f *Fetcher) Enqueue(peer string, block *types.Block) error { 209 op := &inject{ 210 origin: peer, 211 block: block, 212 } 213 select { 214 case f.inject <- op: 215 return nil 216 case <-f.quit: 217 return errTerminated 218 } 219 } 220 221 // FilterHeaders extracts all the headers that were explicitly requested by the fetcher, 222 // returning those that should be handled differently. 223 func (f *Fetcher) FilterHeaders(headers []*types.Header, time time.Time) []*types.Header { 224 glog.V(logger.Detail).Infof("[eth/62] filtering %d headers", len(headers)) 225 226 // Send the filter channel to the fetcher 227 filter := make(chan *headerFilterTask) 228 229 select { 230 case f.headerFilter <- filter: 231 case <-f.quit: 232 return nil 233 } 234 // Request the filtering of the header list 235 select { 236 case filter <- &headerFilterTask{headers: headers, time: time}: 237 case <-f.quit: 238 return nil 239 } 240 // Retrieve the headers remaining after filtering 241 select { 242 case task := <-filter: 243 return task.headers 244 case <-f.quit: 245 return nil 246 } 247 } 248 249 // FilterBodies extracts all the block bodies that were explicitly requested by 250 // the fetcher, returning those that should be handled differently. 251 func (f *Fetcher) FilterBodies(transactions [][]*types.Transaction, uncles [][]*types.Header, time time.Time) ([][]*types.Transaction, [][]*types.Header) { 252 glog.V(logger.Detail).Infof("[eth/62] filtering %d:%d bodies", len(transactions), len(uncles)) 253 254 // Send the filter channel to the fetcher 255 filter := make(chan *bodyFilterTask) 256 257 select { 258 case f.bodyFilter <- filter: 259 case <-f.quit: 260 return nil, nil 261 } 262 // Request the filtering of the body list 263 select { 264 case filter <- &bodyFilterTask{transactions: transactions, uncles: uncles, time: time}: 265 case <-f.quit: 266 return nil, nil 267 } 268 // Retrieve the bodies remaining after filtering 269 select { 270 case task := <-filter: 271 return task.transactions, task.uncles 272 case <-f.quit: 273 return nil, nil 274 } 275 } 276 277 // Loop is the main fetcher loop, checking and processing various notification 278 // events. 279 func (f *Fetcher) loop() { 280 // Iterate the block fetching until a quit is requested 281 fetchTimer := time.NewTimer(0) 282 completeTimer := time.NewTimer(0) 283 284 for { 285 // Clean up any expired block fetches 286 for hash, announce := range f.fetching { 287 if time.Since(announce.time) > fetchTimeout { 288 f.forgetHash(hash) 289 } 290 } 291 // Import any queued blocks that could potentially fit 292 height := f.chainHeight() 293 for !f.queue.Empty() { 294 op := f.queue.PopItem().(*inject) 295 if f.queueChangeHook != nil { 296 f.queueChangeHook(op.block.Hash(), false) 297 } 298 // If too high up the chain or phase, continue later 299 number := op.block.NumberU64() 300 if number > height+1 { 301 f.queue.Push(op, -float32(op.block.NumberU64())) 302 if f.queueChangeHook != nil { 303 f.queueChangeHook(op.block.Hash(), true) 304 } 305 break 306 } 307 // Otherwise if fresh and still unknown, try and import 308 hash := op.block.Hash() 309 if number+maxUncleDist < height || f.getBlock(hash) != nil { 310 f.forgetBlock(hash) 311 continue 312 } 313 f.insert(op.origin, op.block) 314 } 315 // Wait for an outside event to occur 316 select { 317 case <-f.quit: 318 // Fetcher terminating, abort all operations 319 return 320 321 case notification := <-f.notify: 322 // A block was announced, make sure the peer isn't DOSing us 323 propAnnounceInMeter.Mark(1) 324 325 count := f.announces[notification.origin] + 1 326 if count > hashLimit { 327 glog.V(logger.Debug).Infof("Peer %s: exceeded outstanding announces (%d)", notification.origin, hashLimit) 328 propAnnounceDOSMeter.Mark(1) 329 break 330 } 331 // If we have a valid block number, check that it's potentially useful 332 if notification.number > 0 { 333 if dist := int64(notification.number) - int64(f.chainHeight()); dist < -maxUncleDist || dist > maxQueueDist { 334 glog.V(logger.Debug).Infof("[eth/62] Peer %s: discarded announcement #%d [%x…], distance %d", notification.origin, notification.number, notification.hash[:4], dist) 335 propAnnounceDropMeter.Mark(1) 336 break 337 } 338 } 339 // All is well, schedule the announce if block's not yet downloading 340 if _, ok := f.fetching[notification.hash]; ok { 341 break 342 } 343 if _, ok := f.completing[notification.hash]; ok { 344 break 345 } 346 f.announces[notification.origin] = count 347 f.announced[notification.hash] = append(f.announced[notification.hash], notification) 348 if f.announceChangeHook != nil && len(f.announced[notification.hash]) == 1 { 349 f.announceChangeHook(notification.hash, true) 350 } 351 if len(f.announced) == 1 { 352 f.rescheduleFetch(fetchTimer) 353 } 354 355 case op := <-f.inject: 356 // A direct block insertion was requested, try and fill any pending gaps 357 propBroadcastInMeter.Mark(1) 358 f.enqueue(op.origin, op.block) 359 360 case hash := <-f.done: 361 // A pending import finished, remove all traces of the notification 362 f.forgetHash(hash) 363 f.forgetBlock(hash) 364 365 case <-fetchTimer.C: 366 // At least one block's timer ran out, check for needing retrieval 367 request := make(map[string][]common.Hash) 368 369 for hash, announces := range f.announced { 370 if time.Since(announces[0].time) > arriveTimeout-gatherSlack { 371 // Pick a random peer to retrieve from, reset all others 372 announce := announces[rand.Intn(len(announces))] 373 f.forgetHash(hash) 374 375 // If the block still didn't arrive, queue for fetching 376 if f.getBlock(hash) == nil { 377 request[announce.origin] = append(request[announce.origin], hash) 378 f.fetching[hash] = announce 379 } 380 } 381 } 382 // Send out all block header requests 383 for peer, hashes := range request { 384 if glog.V(logger.Detail) && len(hashes) > 0 { 385 list := "[" 386 for _, hash := range hashes { 387 list += fmt.Sprintf("%x…, ", hash[:4]) 388 } 389 list = list[:len(list)-2] + "]" 390 glog.V(logger.Detail).Infof("[eth/62] Peer %s: fetching headers %s", peer, list) 391 } 392 // Create a closure of the fetch and schedule in on a new thread 393 fetchHeader, hashes := f.fetching[hashes[0]].fetchHeader, hashes 394 go func() { 395 if f.fetchingHook != nil { 396 f.fetchingHook(hashes) 397 } 398 for _, hash := range hashes { 399 headerFetchMeter.Mark(1) 400 fetchHeader(hash) // Suboptimal, but protocol doesn't allow batch header retrievals 401 } 402 }() 403 } 404 // Schedule the next fetch if blocks are still pending 405 f.rescheduleFetch(fetchTimer) 406 407 case <-completeTimer.C: 408 // At least one header's timer ran out, retrieve everything 409 request := make(map[string][]common.Hash) 410 411 for hash, announces := range f.fetched { 412 // Pick a random peer to retrieve from, reset all others 413 announce := announces[rand.Intn(len(announces))] 414 f.forgetHash(hash) 415 416 // If the block still didn't arrive, queue for completion 417 if f.getBlock(hash) == nil { 418 request[announce.origin] = append(request[announce.origin], hash) 419 f.completing[hash] = announce 420 } 421 } 422 // Send out all block body requests 423 for peer, hashes := range request { 424 if glog.V(logger.Detail) && len(hashes) > 0 { 425 list := "[" 426 for _, hash := range hashes { 427 list += fmt.Sprintf("%x…, ", hash[:4]) 428 } 429 list = list[:len(list)-2] + "]" 430 431 glog.V(logger.Detail).Infof("[eth/62] Peer %s: fetching bodies %s", peer, list) 432 } 433 // Create a closure of the fetch and schedule in on a new thread 434 if f.completingHook != nil { 435 f.completingHook(hashes) 436 } 437 bodyFetchMeter.Mark(int64(len(hashes))) 438 go f.completing[hashes[0]].fetchBodies(hashes) 439 } 440 // Schedule the next fetch if blocks are still pending 441 f.rescheduleComplete(completeTimer) 442 443 case filter := <-f.headerFilter: 444 // Headers arrived from a remote peer. Extract those that were explicitly 445 // requested by the fetcher, and return everything else so it's delivered 446 // to other parts of the system. 447 var task *headerFilterTask 448 select { 449 case task = <-filter: 450 case <-f.quit: 451 return 452 } 453 headerFilterInMeter.Mark(int64(len(task.headers))) 454 455 // Split the batch of headers into unknown ones (to return to the caller), 456 // known incomplete ones (requiring body retrievals) and completed blocks. 457 unknown, incomplete, complete := []*types.Header{}, []*announce{}, []*types.Block{} 458 for _, header := range task.headers { 459 hash := header.Hash() 460 461 // Filter fetcher-requested headers from other synchronisation algorithms 462 if announce := f.fetching[hash]; announce != nil && f.fetched[hash] == nil && f.completing[hash] == nil && f.queued[hash] == nil { 463 // If the delivered header does not match the promised number, drop the announcer 464 if header.Number.Uint64() != announce.number { 465 glog.V(logger.Detail).Infof("[eth/62] Peer %s: invalid block number for [%x…]: announced %d, provided %d", announce.origin, header.Hash().Bytes()[:4], announce.number, header.Number.Uint64()) 466 f.dropPeer(announce.origin) 467 f.forgetHash(hash) 468 continue 469 } 470 // Only keep if not imported by other means 471 if f.getBlock(hash) == nil { 472 announce.header = header 473 announce.time = task.time 474 475 // If the block is empty (header only), short circuit into the final import queue 476 if header.TxHash == types.DeriveSha(types.Transactions{}) && header.UncleHash == types.CalcUncleHash([]*types.Header{}) { 477 glog.V(logger.Detail).Infof("[eth/62] Peer %s: block #%d [%x…] empty, skipping body retrieval", announce.origin, header.Number.Uint64(), header.Hash().Bytes()[:4]) 478 479 block := types.NewBlockWithHeader(header) 480 block.ReceivedAt = task.time 481 482 complete = append(complete, block) 483 f.completing[hash] = announce 484 continue 485 } 486 // Otherwise add to the list of blocks needing completion 487 incomplete = append(incomplete, announce) 488 } else { 489 glog.V(logger.Detail).Infof("[eth/62] Peer %s: block #%d [%x…] already imported, discarding header", announce.origin, header.Number.Uint64(), header.Hash().Bytes()[:4]) 490 f.forgetHash(hash) 491 } 492 } else { 493 // Fetcher doesn't know about it, add to the return list 494 unknown = append(unknown, header) 495 } 496 } 497 headerFilterOutMeter.Mark(int64(len(unknown))) 498 select { 499 case filter <- &headerFilterTask{headers: unknown, time: task.time}: 500 case <-f.quit: 501 return 502 } 503 // Schedule the retrieved headers for body completion 504 for _, announce := range incomplete { 505 hash := announce.header.Hash() 506 if _, ok := f.completing[hash]; ok { 507 continue 508 } 509 f.fetched[hash] = append(f.fetched[hash], announce) 510 if len(f.fetched) == 1 { 511 f.rescheduleComplete(completeTimer) 512 } 513 } 514 // Schedule the header-only blocks for import 515 for _, block := range complete { 516 if announce := f.completing[block.Hash()]; announce != nil { 517 f.enqueue(announce.origin, block) 518 } 519 } 520 521 case filter := <-f.bodyFilter: 522 // Block bodies arrived, extract any explicitly requested blocks, return the rest 523 var task *bodyFilterTask 524 select { 525 case task = <-filter: 526 case <-f.quit: 527 return 528 } 529 bodyFilterInMeter.Mark(int64(len(task.transactions))) 530 531 blocks := []*types.Block{} 532 for i := 0; i < len(task.transactions) && i < len(task.uncles); i++ { 533 // Match up a body to any possible completion request 534 matched := false 535 536 for hash, announce := range f.completing { 537 if f.queued[hash] == nil { 538 txnHash := types.DeriveSha(types.Transactions(task.transactions[i])) 539 uncleHash := types.CalcUncleHash(task.uncles[i]) 540 541 if txnHash == announce.header.TxHash && uncleHash == announce.header.UncleHash { 542 // Mark the body matched, reassemble if still unknown 543 matched = true 544 545 if f.getBlock(hash) == nil { 546 block := types.NewBlockWithHeader(announce.header).WithBody(task.transactions[i], task.uncles[i]) 547 block.ReceivedAt = task.time 548 549 blocks = append(blocks, block) 550 } else { 551 f.forgetHash(hash) 552 } 553 } 554 } 555 } 556 if matched { 557 task.transactions = append(task.transactions[:i], task.transactions[i+1:]...) 558 task.uncles = append(task.uncles[:i], task.uncles[i+1:]...) 559 i-- 560 continue 561 } 562 } 563 564 bodyFilterOutMeter.Mark(int64(len(task.transactions))) 565 select { 566 case filter <- task: 567 case <-f.quit: 568 return 569 } 570 // Schedule the retrieved blocks for ordered import 571 for _, block := range blocks { 572 if announce := f.completing[block.Hash()]; announce != nil { 573 f.enqueue(announce.origin, block) 574 } 575 } 576 } 577 } 578 } 579 580 // rescheduleFetch resets the specified fetch timer to the next announce timeout. 581 func (f *Fetcher) rescheduleFetch(fetch *time.Timer) { 582 // Short circuit if no blocks are announced 583 if len(f.announced) == 0 { 584 return 585 } 586 // Otherwise find the earliest expiring announcement 587 earliest := time.Now() 588 for _, announces := range f.announced { 589 if earliest.After(announces[0].time) { 590 earliest = announces[0].time 591 } 592 } 593 fetch.Reset(arriveTimeout - time.Since(earliest)) 594 } 595 596 // rescheduleComplete resets the specified completion timer to the next fetch timeout. 597 func (f *Fetcher) rescheduleComplete(complete *time.Timer) { 598 // Short circuit if no headers are fetched 599 if len(f.fetched) == 0 { 600 return 601 } 602 // Otherwise find the earliest expiring announcement 603 earliest := time.Now() 604 for _, announces := range f.fetched { 605 if earliest.After(announces[0].time) { 606 earliest = announces[0].time 607 } 608 } 609 complete.Reset(gatherSlack - time.Since(earliest)) 610 } 611 612 // enqueue schedules a new future import operation, if the block to be imported 613 // has not yet been seen. 614 func (f *Fetcher) enqueue(peer string, block *types.Block) { 615 hash := block.Hash() 616 617 // Ensure the peer isn't DOSing us 618 count := f.queues[peer] + 1 619 if count > blockLimit { 620 glog.V(logger.Debug).Infof("Peer %s: discarded block #%d [%x…], exceeded allowance (%d)", peer, block.NumberU64(), hash.Bytes()[:4], blockLimit) 621 propBroadcastDOSMeter.Mark(1) 622 f.forgetHash(hash) 623 return 624 } 625 // Discard any past or too distant blocks 626 if dist := int64(block.NumberU64()) - int64(f.chainHeight()); dist < -maxUncleDist || dist > maxQueueDist { 627 glog.V(logger.Debug).Infof("Peer %s: discarded block #%d [%x…], distance %d", peer, block.NumberU64(), hash.Bytes()[:4], dist) 628 propBroadcastDropMeter.Mark(1) 629 f.forgetHash(hash) 630 return 631 } 632 // Schedule the block for future importing 633 if _, ok := f.queued[hash]; !ok { 634 op := &inject{ 635 origin: peer, 636 block: block, 637 } 638 f.queues[peer] = count 639 f.queued[hash] = op 640 f.queue.Push(op, -float32(block.NumberU64())) 641 if f.queueChangeHook != nil { 642 f.queueChangeHook(op.block.Hash(), true) 643 } 644 if glog.V(logger.Debug) { 645 glog.Infof("Peer %s: queued block #%d [%x…], total %v", peer, block.NumberU64(), hash.Bytes()[:4], f.queue.Size()) 646 } 647 } 648 } 649 650 // insert spawns a new goroutine to run a block insertion into the chain. If the 651 // block's number is at the same height as the current import phase, if updates 652 // the phase states accordingly. 653 func (f *Fetcher) insert(peer string, block *types.Block) { 654 hash := block.Hash() 655 656 // Run the import on a new thread 657 glog.V(logger.Debug).Infof("Peer %s: importing block #%d [%x…]", peer, block.NumberU64(), hash[:4]) 658 go func() { 659 defer func() { f.done <- hash }() 660 661 // If the parent's unknown, abort insertion 662 parent := f.getBlock(block.ParentHash()) 663 if parent == nil { 664 glog.V(logger.Debug).Infof("Peer %s: parent [%x…] of block #%d [%x…] unknown", peer, block.ParentHash().Bytes()[:4], block.NumberU64(), hash[:4]) 665 return 666 } 667 // Quickly validate the header and propagate the block if it passes 668 switch err := f.validateBlock(block, parent); err { 669 case nil: 670 // All ok, quickly propagate to our peers 671 propBroadcastOutTimer.UpdateSince(block.ReceivedAt) 672 go f.broadcastBlock(block, true) 673 674 case core.BlockFutureErr: 675 // Weird future block, don't fail, but neither propagate 676 677 default: 678 // Something went very wrong, drop the peer 679 glog.V(logger.Debug).Infof("Peer %s: block #%d [%x…] verification failed: %v", peer, block.NumberU64(), hash[:4], err) 680 f.dropPeer(peer) 681 return 682 } 683 // Run the actual import and log any issues 684 if _, err := f.insertChain(types.Blocks{block}); err != nil { 685 glog.V(logger.Warn).Infof("Peer %s: block #%d [%x…] import failed: %v", peer, block.NumberU64(), hash[:4], err) 686 return 687 } 688 // If import succeeded, broadcast the block 689 propAnnounceOutTimer.UpdateSince(block.ReceivedAt) 690 go f.broadcastBlock(block, false) 691 692 // Invoke the testing hook if needed 693 if f.importedHook != nil { 694 f.importedHook(block) 695 } 696 }() 697 } 698 699 // forgetHash removes all traces of a block announcement from the fetcher's 700 // internal state. 701 func (f *Fetcher) forgetHash(hash common.Hash) { 702 // Remove all pending announces and decrement DOS counters 703 for _, announce := range f.announced[hash] { 704 f.announces[announce.origin]-- 705 if f.announces[announce.origin] == 0 { 706 delete(f.announces, announce.origin) 707 } 708 } 709 delete(f.announced, hash) 710 if f.announceChangeHook != nil { 711 f.announceChangeHook(hash, false) 712 } 713 // Remove any pending fetches and decrement the DOS counters 714 if announce := f.fetching[hash]; announce != nil { 715 f.announces[announce.origin]-- 716 if f.announces[announce.origin] == 0 { 717 delete(f.announces, announce.origin) 718 } 719 delete(f.fetching, hash) 720 } 721 722 // Remove any pending completion requests and decrement the DOS counters 723 for _, announce := range f.fetched[hash] { 724 f.announces[announce.origin]-- 725 if f.announces[announce.origin] == 0 { 726 delete(f.announces, announce.origin) 727 } 728 } 729 delete(f.fetched, hash) 730 731 // Remove any pending completions and decrement the DOS counters 732 if announce := f.completing[hash]; announce != nil { 733 f.announces[announce.origin]-- 734 if f.announces[announce.origin] == 0 { 735 delete(f.announces, announce.origin) 736 } 737 delete(f.completing, hash) 738 } 739 } 740 741 // forgetBlock removes all traces of a queued block from the fetcher's internal 742 // state. 743 func (f *Fetcher) forgetBlock(hash common.Hash) { 744 if insert := f.queued[hash]; insert != nil { 745 f.queues[insert.origin]-- 746 if f.queues[insert.origin] == 0 { 747 delete(f.queues, insert.origin) 748 } 749 delete(f.queued, hash) 750 } 751 }