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)
| 156 | // in two scenarios: pure-header mode of operation (light clients), or properly |
| 157 | // separated header/block phases (non-archive clients). |
| 158 | func (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 |