SetHead rewinds the local chain to a new head. Everything above the new head will be deleted and the new one set.
(head uint64, delFn DeleteCallback)
| 409 | // SetHead rewinds the local chain to a new head. Everything above the new head |
| 410 | // will be deleted and the new one set. |
| 411 | func (hc *HeaderChain) SetHead(head uint64, delFn DeleteCallback) { |
| 412 | height := uint64(0) |
| 413 | |
| 414 | if hdr := hc.CurrentHeader(); hdr != nil { |
| 415 | height = hdr.Number.Uint64() |
| 416 | } |
| 417 | batch := hc.chainDb.NewBatch() |
| 418 | for hdr := hc.CurrentHeader(); hdr != nil && hdr.Number.Uint64() > head; hdr = hc.CurrentHeader() { |
| 419 | hash := hdr.Hash() |
| 420 | num := hdr.Number.Uint64() |
| 421 | if delFn != nil { |
| 422 | delFn(batch, hash, num) |
| 423 | } |
| 424 | rawdb.DeleteHeader(batch, hash, num) |
| 425 | |
| 426 | hc.currentHeader.Store(hc.GetHeader(hdr.ParentHash, hdr.Number.Uint64()-1)) |
| 427 | } |
| 428 | // Roll back the canonical chain numbering |
| 429 | for i := height; i > head; i-- { |
| 430 | rawdb.DeleteCanonicalHash(batch, i) |
| 431 | } |
| 432 | batch.Write() |
| 433 | |
| 434 | // Clear out any stale content from the caches |
| 435 | hc.headerCache.Purge() |
| 436 | hc.numberCache.Purge() |
| 437 | |
| 438 | if hc.CurrentHeader() == nil { |
| 439 | hc.currentHeader.Store(hc.genesisHeader) |
| 440 | } |
| 441 | hc.currentHeaderHash = hc.CurrentHeader().Hash() |
| 442 | |
| 443 | rawdb.WriteHeadHeaderHash(hc.chainDb, hc.currentHeaderHash) |
| 444 | } |
| 445 | |
| 446 | // SetGenesis sets a new genesis block header for the chain |
| 447 | func (hc *HeaderChain) SetGenesis(head *types.Header) { |