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