forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_headers.go
288 lines (265 loc) · 9.58 KB
/
stage_headers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package stagedsync
import (
"fmt"
"math/big"
mrand "math/rand"
"time"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/consensus"
"github.com/ledgerwatch/turbo-geth/core"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"github.com/ledgerwatch/turbo-geth/core/types"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/log"
"github.com/ledgerwatch/turbo-geth/params"
"github.com/ledgerwatch/turbo-geth/rlp"
)
func SpawnHeaderDownloadStage(s *StageState, u Unwinder, d DownloaderGlue, headersFetchers []func() error) error {
err := d.SpawnHeaderDownloadStage(headersFetchers, s, u)
if err == nil {
s.Done()
}
return err
}
// Implements consensus.ChainReader
type ChainReader struct {
config *params.ChainConfig
db ethdb.Database
}
// Config retrieves the blockchain's chain configuration.
func (cr ChainReader) Config() *params.ChainConfig {
return cr.config
}
// CurrentHeader retrieves the current header from the local chain.
func (cr ChainReader) CurrentHeader() *types.Header {
hash := rawdb.ReadHeadHeaderHash(cr.db)
number := rawdb.ReadHeaderNumber(cr.db, hash)
return rawdb.ReadHeader(cr.db, hash, *number)
}
// GetHeader retrieves a block header from the database by hash and number.
func (cr ChainReader) GetHeader(hash common.Hash, number uint64) *types.Header {
return rawdb.ReadHeader(cr.db, hash, number)
}
// GetHeaderByNumber retrieves a block header from the database by number.
func (cr ChainReader) GetHeaderByNumber(number uint64) *types.Header {
hash, err := rawdb.ReadCanonicalHash(cr.db, number)
if err != nil {
log.Error("ReadCanonicalHash failed", "err", err)
return nil
}
return rawdb.ReadHeader(cr.db, hash, number)
}
// GetHeaderByHash retrieves a block header from the database by its hash.
func (cr ChainReader) GetHeaderByHash(hash common.Hash) *types.Header {
number := rawdb.ReadHeaderNumber(cr.db, hash)
return rawdb.ReadHeader(cr.db, hash, *number)
}
// GetBlock retrieves a block from the database by hash and number.
func (cr ChainReader) GetBlock(hash common.Hash, number uint64) *types.Block {
return rawdb.ReadBlock(cr.db, hash, number)
}
func VerifyHeaders(db ethdb.Database, headers []*types.Header, config *params.ChainConfig, engine consensus.Engine, checkFreq int) error {
// Generate the list of seal verification requests, and start the parallel verifier
seals := make([]bool, len(headers))
if checkFreq != 0 {
// In case of checkFreq == 0 all seals are left false.
for i := 0; i < len(seals)/checkFreq; i++ {
index := i * checkFreq
if index >= len(seals) {
index = len(seals) - 1
}
seals[index] = true
}
// Last should always be verified to avoid junk.
seals[len(seals)-1] = true
}
cancel, results := engine.VerifyHeaders(ChainReader{config, db}, headers, seals)
defer cancel()
// Iterate over the headers and ensure they all check out
for i := 0; i < len(headers); i++ {
// Otherwise wait for headers checks and ensure they pass
if err := <-results; err != nil {
return err
}
}
return nil
}
func InsertHeaderChain(logPrefix string, db ethdb.Database, headers []*types.Header) (bool, bool, uint64, error) {
start := time.Now()
// ignore headers that we already have
alreadyCanonicalIndex := 0
for _, h := range headers {
number := h.Number.Uint64()
ch, err := rawdb.ReadCanonicalHash(db, number)
if err != nil {
return false, false, 0, fmt.Errorf("[%s] %w", logPrefix, err)
}
if h.Hash() == ch {
alreadyCanonicalIndex++
} else {
break
}
// If the header is a banned one, straight out abort
if core.BadHashes[h.Hash()] {
log.Error(fmt.Sprintf(`[%s]
########## BAD BLOCK #########
Number: %v
Hash: 0x%x
Error: %v
##############################
`, logPrefix, h.Number, h.Hash(), core.ErrBlacklistedHash))
return false, false, 0, core.ErrBlacklistedHash
}
}
headers = headers[alreadyCanonicalIndex:]
if len(headers) < 1 {
return false, false, 0, nil
}
if rawdb.ReadHeader(db, headers[0].ParentHash, headers[0].Number.Uint64()-1) == nil {
return false, false, 0, fmt.Errorf("%s: unknown parent %x", logPrefix, headers[0].ParentHash)
}
parentTd, err := rawdb.ReadTd(db, headers[0].ParentHash, headers[0].Number.Uint64()-1)
if err != nil {
return false, false, 0, fmt.Errorf("[%s] %w", logPrefix, err)
}
externTd := new(big.Int).Set(parentTd)
for i, header := range headers {
if i > 0 {
if header.ParentHash != headers[i-1].Hash() {
return false, false, 0, fmt.Errorf("%s: broken chain", logPrefix)
}
}
externTd = externTd.Add(externTd, header.Difficulty)
}
headHash := rawdb.ReadHeadHeaderHash(db)
headNumber := rawdb.ReadHeaderNumber(db, headHash)
localTd, err := rawdb.ReadTd(db, headHash, *headNumber)
if err != nil {
return false, false, 0, err
}
lastHeader := headers[len(headers)-1]
// If the total difficulty is higher than our known, add it to the canonical chain
// Second clause in the if statement reduces the vulnerability to selfish mining.
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
newCanonical := externTd.Cmp(localTd) > 0
if !newCanonical && externTd.Cmp(localTd) == 0 {
if lastHeader.Number.Uint64() < *headNumber {
newCanonical = true
} else if lastHeader.Number.Uint64() == *headNumber {
newCanonical = mrand.Float64() < 0.5
}
}
var deepFork bool // Whether the forkBlock is outside this header chain segment
ch, err := rawdb.ReadCanonicalHash(db, headers[0].Number.Uint64()-1)
if err != nil {
return false, false, 0, fmt.Errorf("[%s] %w", logPrefix, err)
}
if newCanonical && headers[0].ParentHash != ch {
deepFork = true
}
var forkBlockNumber uint64
var fork bool // Set to true if forkBlockNumber is initialised
ignored := 0
batch := db.NewBatch()
// Do a full insert if pre-checks passed
td := new(big.Int).Set(parentTd)
for _, header := range headers {
// we always add header difficulty to TD, because next blocks might
// be inserted and we need the right value for them
td = td.Add(td, header.Difficulty)
if !newCanonical && rawdb.ReadHeaderNumber(batch, header.Hash()) != nil {
// We cannot ignore blocks if they cause reorg
ignored++
continue
}
number := header.Number.Uint64()
ch, err := rawdb.ReadCanonicalHash(batch, number)
if err != nil {
return false, false, 0, fmt.Errorf("[%s] %w", logPrefix, err)
}
hashesMatch := header.Hash() == ch
if newCanonical && !deepFork && !fork && !hashesMatch {
forkBlockNumber = number - 1
fork = true
} else if newCanonical && hashesMatch {
forkBlockNumber = number
fork = true
}
if newCanonical {
if err = rawdb.WriteCanonicalHash(batch, header.Hash(), header.Number.Uint64()); err != nil {
return false, false, 0, fmt.Errorf("[%s] %w", logPrefix, err)
}
}
data, err := rlp.EncodeToBytes(header)
if err != nil {
return false, false, 0, fmt.Errorf("[%s] Failed to RLP encode header: %w", logPrefix, err)
}
if err := rawdb.WriteTd(batch, header.Hash(), header.Number.Uint64(), td); err != nil {
return false, false, 0, fmt.Errorf("[%s] Failed to WriteTd: %w", logPrefix, err)
}
if err := batch.Put(dbutils.HeaderPrefix, dbutils.HeaderKey(number, header.Hash()), data); err != nil {
return false, false, 0, fmt.Errorf("[%s] Failed to store header: %w", logPrefix, err)
}
}
if deepFork {
forkHeader := rawdb.ReadHeader(batch, headers[0].ParentHash, headers[0].Number.Uint64()-1)
forkBlockNumber = forkHeader.Number.Uint64() - 1
forkHash := forkHeader.ParentHash
for {
ch, err := rawdb.ReadCanonicalHash(batch, forkBlockNumber)
if err != nil {
return false, false, 0, fmt.Errorf("[%s] %w", logPrefix, err)
}
if forkHash == ch {
break
}
if err = rawdb.WriteCanonicalHash(batch, forkHash, forkBlockNumber); err != nil {
return false, false, 0, err
}
forkHeader = rawdb.ReadHeader(batch, forkHash, forkBlockNumber)
forkBlockNumber = forkHeader.Number.Uint64() - 1
forkHash = forkHeader.ParentHash
}
err = rawdb.WriteCanonicalHash(batch, headers[0].ParentHash, headers[0].Number.Uint64()-1)
if err != nil {
return false, false, 0, err
}
}
reorg := newCanonical && forkBlockNumber < *headNumber
if reorg {
// Delete any canonical number assignments above the new head
for i := lastHeader.Number.Uint64() + 1; i <= *headNumber; i++ {
err = rawdb.DeleteCanonicalHash(batch, i)
if err != nil {
return false, false, 0, fmt.Errorf("[%s] %w", logPrefix, err)
}
}
}
if newCanonical {
encoded := dbutils.EncodeBlockNumber(lastHeader.Number.Uint64())
if err := batch.Put(dbutils.HeaderNumberPrefix, lastHeader.Hash().Bytes(), encoded); err != nil {
return false, false, 0, fmt.Errorf("[%s] Failed to store hash to number mapping: %w", logPrefix, err)
}
rawdb.WriteHeadHeaderHash(batch, lastHeader.Hash())
}
if _, err := batch.Commit(); err != nil {
return false, false, 0, fmt.Errorf("%s: write header markers into disk: %w", logPrefix, err)
}
// Report some public statistics so the user has a clue what's going on
ctx := []interface{}{
"count", len(headers), "elapsed", common.PrettyDuration(time.Since(start)),
"number", lastHeader.Number, "hash", lastHeader.Hash(),
}
if timestamp := time.Unix(int64(lastHeader.Time), 0); time.Since(timestamp) > time.Minute {
ctx = append(ctx, []interface{}{"age", common.PrettyAge(timestamp)}...)
}
if ignored > 0 {
ctx = append(ctx, []interface{}{"ignored", ignored}...)
}
if reorg {
ctx = append(ctx, []interface{}{"reorg", reorg, "forkBlockNumber", forkBlockNumber}...)
}
log.Info(fmt.Sprintf("[%s] Imported new block headers", logPrefix), ctx...)
return newCanonical, reorg, forkBlockNumber, nil
}