MCPcopy Create free account
hub / github.com/CPChain/chain / SetHead

Method SetHead

core/headerchain.go:411–444  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

409// SetHead rewinds the local chain to a new head. Everything above the new head
410// will be deleted and the new one set.
411func (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
447func (hc *HeaderChain) SetGenesis(head *types.Header) {

Callers

nothing calls this directly

Calls 6

CurrentHeaderMethod · 0.95
GetHeaderMethod · 0.95
WriteMethod · 0.95
Uint64Method · 0.80
NewBatchMethod · 0.65
HashMethod · 0.65

Tested by

no test coverage detected