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

Function NewHeaderChain

core/headerchain.go:77–115  ·  view source on GitHub ↗

NewHeaderChain creates a new HeaderChain structure. getValidator should return the parent's validator procInterrupt points to the parent's interrupt semaphore wg points to the parent's shutdown wait group

(chainDb database.Database, config *configs.ChainConfig, engine consensus.Engine, procInterrupt func() bool)

Source from the content-addressed store, hash-verified

75// procInterrupt points to the parent's interrupt semaphore
76// wg points to the parent's shutdown wait group
77func NewHeaderChain(chainDb database.Database, config *configs.ChainConfig, engine consensus.Engine, procInterrupt func() bool) (*HeaderChain, error) {
78 headerCache, _ := lru.New(headerCacheLimit)
79 numberCache, _ := lru.New(numberCacheLimit)
80
81 // Seed a fast but crypto originating random generator
82 seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
83 if err != nil {
84 return nil, err
85 }
86
87 hc := &HeaderChain{
88 config: config,
89 chainDb: chainDb,
90 headerCache: headerCache,
91 numberCache: numberCache,
92 procInterrupt: procInterrupt,
93 rand: mrand.New(mrand.NewSource(seed.Int64())),
94 engine: engine,
95 }
96
97 hc.genesisHeader = hc.GetHeaderByNumber(0)
98 if hc.genesisHeader == nil {
99 return nil, ErrNoGenesis
100 }
101
102 hc.currentHeader.Store(hc.genesisHeader)
103 if head := rawdb.ReadHeadBlockHash(chainDb); head != (common.Hash{}) {
104 if chead := hc.GetHeaderByHash(head); chead != nil {
105 hc.currentHeader.Store(chead)
106 }
107 }
108 hc.currentHeaderHash = hc.CurrentHeader().Hash()
109
110 if currentHead := hc.CurrentHeader(); currentHead != nil {
111 hc.SetKnownHead(currentHead.Hash(), currentHead.Number.Uint64())
112 }
113
114 return hc, nil
115}
116
117// KnownHead returns hash and number of current head block (maybe not in local chain)
118func (hc *HeaderChain) KnownHead() (common.Hash, uint64) {

Callers 1

NewBlockChainFunction · 0.85

Calls 7

GetHeaderByNumberMethod · 0.95
GetHeaderByHashMethod · 0.95
CurrentHeaderMethod · 0.95
SetKnownHeadMethod · 0.95
Int64Method · 0.80
Uint64Method · 0.80
HashMethod · 0.65

Tested by

no test coverage detected