17
17
package core
18
18
19
19
import (
20
- crand "crypto/rand"
21
20
"errors"
22
21
"fmt"
23
- "math"
24
22
"math/big"
25
- mrand "math/rand"
26
23
"sync/atomic"
27
24
"time"
28
25
@@ -43,53 +40,48 @@ const (
43
40
numberCacheLimit = 2048
44
41
)
45
42
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.
49
45
//
50
46
// HeaderChain is responsible for maintaining the header chain including the
51
47
// header query and updating.
52
48
//
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:
56
50
//
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.
59
59
type HeaderChain struct {
60
60
config * params.ChainConfig
61
61
chainDb ethdb.Database
62
62
genesisHeader * types.Header
63
63
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)
66
66
67
67
headerCache * lru.Cache [common.Hash , * types.Header ]
68
68
tdCache * lru.Cache [common.Hash , * big.Int ] // most recent total difficulties
69
69
numberCache * lru.Cache [common.Hash , uint64 ] // most recent block numbers
70
70
71
71
procInterrupt func () bool
72
-
73
- rand * mrand.Rand
74
- engine consensus.Engine
72
+ engine consensus.Engine
75
73
}
76
74
77
75
// NewHeaderChain creates a new HeaderChain structure. ProcInterrupt points
78
76
// to the parent's interrupt semaphore.
79
77
func 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
- }
85
78
hc := & HeaderChain {
86
79
config : config ,
87
80
chainDb : chainDb ,
88
81
headerCache : lru.NewCache [common.Hash , * types.Header ](headerCacheLimit ),
89
82
tdCache : lru.NewCache [common.Hash , * big.Int ](tdCacheLimit ),
90
83
numberCache : lru.NewCache [common.Hash , uint64 ](numberCacheLimit ),
91
84
procInterrupt : procInterrupt ,
92
- rand : mrand .New (mrand .NewSource (seed .Int64 ())),
93
85
engine : engine ,
94
86
}
95
87
hc .genesisHeader = hc .GetHeaderByNumber (0 )
@@ -525,7 +517,7 @@ func (hc *HeaderChain) GetCanonicalHash(number uint64) common.Hash {
525
517
// CurrentHeader retrieves the current head header of the canonical chain. The
526
518
// header is retrieved from the HeaderChain's internal cache.
527
519
func (hc * HeaderChain ) CurrentHeader () * types.Header {
528
- return hc .currentHeader .Load ().( * types. Header )
520
+ return hc .currentHeader .Load ()
529
521
}
530
522
531
523
// SetCurrentHeader sets the in-memory head header marker of the canonical chan
0 commit comments