processSection processes an entire section by calling backend functions while ensuring the continuity of the passed headers. Since the chain mutex is not held while processing, the continuity can be broken by a long reorg, in which case the function returns with an error.
(section uint64, lastHead common.Hash)
| 327 | // held while processing, the continuity can be broken by a long reorg, in which |
| 328 | // case the function returns with an error. |
| 329 | func (c *ChainIndexer) processSection(section uint64, lastHead common.Hash) (common.Hash, error) { |
| 330 | c.log.Debug("Processing new chain section", "section", section) |
| 331 | |
| 332 | // Reset and partial processing |
| 333 | |
| 334 | if err := c.backend.Reset(section, lastHead); err != nil { |
| 335 | c.setValidSections(0) |
| 336 | return common.Hash{}, err |
| 337 | } |
| 338 | |
| 339 | for number := section * c.sectionSize; number < (section+1)*c.sectionSize; number++ { |
| 340 | hash := rawdb.ReadCanonicalHash(c.chainDb, number) |
| 341 | if hash == (common.Hash{}) { |
| 342 | return common.Hash{}, fmt.Errorf("canonical block #%d unknown", number) |
| 343 | } |
| 344 | header := rawdb.ReadHeader(c.chainDb, hash, number) |
| 345 | if header == nil { |
| 346 | return common.Hash{}, fmt.Errorf("block #%d [%x…] not found", number, hash[:4]) |
| 347 | } else if header.ParentHash != lastHead { |
| 348 | return common.Hash{}, fmt.Errorf("chain reorged during section processing") |
| 349 | } |
| 350 | c.backend.Process(header) |
| 351 | lastHead = header.Hash() |
| 352 | } |
| 353 | if err := c.backend.Commit(); err != nil { |
| 354 | c.log.Error("Section commit failed", "error", err) |
| 355 | return common.Hash{}, err |
| 356 | } |
| 357 | return lastHead, nil |
| 358 | } |
| 359 | |
| 360 | // Sections returns the number of processed sections maintained by the indexer |
| 361 | // and also the information about the last header indexed for potential canonical |
no test coverage detected