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

Method WriteHeader

core/headerchain.go:158–185  ·  view source on GitHub ↗

WriteHeader writes a header into the local chain, given that its parent is already known. If the total difficulty of the newly inserted header becomes greater than the current known TD, the canonical chain is re-routed. Note: This method is not concurrent-safe with inserting blocks simultaneously i

(header *types.Header)

Source from the content-addressed store, hash-verified

156// in two scenarios: pure-header mode of operation (light clients), or properly
157// separated header/block phases (non-archive clients).
158func (hc *HeaderChain) WriteHeader(header *types.Header) (status WriteStatus, err error) {
159 // Cache some values to prevent constant recalculation
160 var (
161 hash = header.Hash()
162 number = header.Number.Uint64()
163 )
164
165 rawdb.WriteHeader(hc.chainDb, header)
166
167 // If the height is higher than our known, add it to the canonical chain
168 if header.Number.Cmp(hc.CurrentHeader().Number) > 0 {
169 // Extend the canonical chain with the new header
170 rawdb.WriteCanonicalHash(hc.chainDb, hash, number)
171 rawdb.WriteHeadHeaderHash(hc.chainDb, hash)
172
173 hc.currentHeaderHash = hash
174 hc.currentHeader.Store(types.CopyHeader(header))
175
176 status = CanonStatTy
177 } else {
178 status = SideStatTy
179 }
180
181 hc.headerCache.Add(hash, header)
182 hc.numberCache.Add(hash, number)
183
184 return
185}
186
187// WhCallback is a callback function for inserting individual headers.
188// A callback is used for two reasons: first, in a LightChain, status should be

Callers 5

makeChainForBenchFunction · 0.80
testChainIndexerFunction · 0.80
InsertHeaderChainMethod · 0.80
writeHeaderMethod · 0.80
testHeaderChainImportFunction · 0.80

Calls 5

CurrentHeaderMethod · 0.95
Uint64Method · 0.80
CmpMethod · 0.80
HashMethod · 0.65
AddMethod · 0.65

Tested by 3

makeChainForBenchFunction · 0.64
testChainIndexerFunction · 0.64
testHeaderChainImportFunction · 0.64