1717package core
1818
1919import (
20- crand "crypto/rand"
2120 "errors"
2221 "fmt"
23- "math"
2422 "math/big"
25- mrand "math/rand"
2623 "sync/atomic"
2724 "time"
2825
@@ -43,53 +40,48 @@ const (
4340 numberCacheLimit = 2048
4441)
4542
46- // HeaderChain implements the basic block header chain logic that is shared by
47- // core.BlockChain and light.LightChain. It is not usable in itself, only as
48- // a part of either structure.
43+ // HeaderChain implements the basic block header chain logic. It is not usable
44+ // in itself, but rather an internal structure of core.Blockchain.
4945//
5046// HeaderChain is responsible for maintaining the header chain including the
5147// header query and updating.
5248//
53- // The components maintained by headerchain includes: (1) total difficulty
54- // (2) header (3) block hash -> number mapping (4) canonical number -> hash mapping
55- // and (5) head header flag.
49+ // The data components maintained by HeaderChain include:
5650//
57- // It is not thread safe either, the encapsulating chain structures should do
58- // the necessary mutex locking/unlocking.
51+ // - total difficulty
52+ // - header
53+ // - block hash -> number mapping
54+ // - canonical number -> hash mapping
55+ // - head header flag.
56+ //
57+ // It is not thread safe, the encapsulating chain structures should do the
58+ // necessary mutex locking/unlocking.
5959type HeaderChain struct {
6060 config * params.ChainConfig
6161 chainDb ethdb.Database
6262 genesisHeader * types.Header
6363
64- currentHeader atomic.Value // Current head of the header chain (may be above the block chain!)
65- currentHeaderHash common.Hash // Hash of the current head of the header chain (prevent recomputing all the time)
64+ currentHeader atomic.Pointer [types. Header ] // Current head of the header chain (maybe above the block chain!)
65+ currentHeaderHash common.Hash // Hash of the current head of the header chain (prevent recomputing all the time)
6666
6767 headerCache * lru.Cache [common.Hash , * types.Header ]
6868 tdCache * lru.Cache [common.Hash , * big.Int ] // most recent total difficulties
6969 numberCache * lru.Cache [common.Hash , uint64 ] // most recent block numbers
7070
7171 procInterrupt func () bool
72-
73- rand * mrand.Rand
74- engine consensus.Engine
72+ engine consensus.Engine
7573}
7674
7775// NewHeaderChain creates a new HeaderChain structure. ProcInterrupt points
7876// to the parent's interrupt semaphore.
7977func NewHeaderChain (chainDb ethdb.Database , config * params.ChainConfig , engine consensus.Engine , procInterrupt func () bool ) (* HeaderChain , error ) {
80- // Seed a fast but crypto originating random generator
81- seed , err := crand .Int (crand .Reader , big .NewInt (math .MaxInt64 ))
82- if err != nil {
83- return nil , err
84- }
8578 hc := & HeaderChain {
8679 config : config ,
8780 chainDb : chainDb ,
8881 headerCache : lru.NewCache [common.Hash , * types.Header ](headerCacheLimit ),
8982 tdCache : lru.NewCache [common.Hash , * big.Int ](tdCacheLimit ),
9083 numberCache : lru.NewCache [common.Hash , uint64 ](numberCacheLimit ),
9184 procInterrupt : procInterrupt ,
92- rand : mrand .New (mrand .NewSource (seed .Int64 ())),
9385 engine : engine ,
9486 }
9587 hc .genesisHeader = hc .GetHeaderByNumber (0 )
@@ -525,7 +517,7 @@ func (hc *HeaderChain) GetCanonicalHash(number uint64) common.Hash {
525517// CurrentHeader retrieves the current head header of the canonical chain. The
526518// header is retrieved from the HeaderChain's internal cache.
527519func (hc * HeaderChain ) CurrentHeader () * types.Header {
528- return hc .currentHeader .Load ().( * types. Header )
520+ return hc .currentHeader .Load ()
529521}
530522
531523// SetCurrentHeader sets the in-memory head header marker of the canonical chan
0 commit comments