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