forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_interhashes.go
432 lines (398 loc) · 12.5 KB
/
stage_interhashes.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package stagedsync
import (
"bytes"
"context"
"fmt"
"os"
"sort"
"time"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/changeset"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/common/etl"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"github.com/ledgerwatch/turbo-geth/eth/stagedsync/stages"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/log"
"github.com/ledgerwatch/turbo-geth/turbo/trie"
)
func SpawnIntermediateHashesStage(s *StageState, db ethdb.Database, checkRoot bool, tmpdir string, quit <-chan struct{}) error {
to, err := s.ExecutionAt(db)
if err != nil {
return err
}
if s.BlockNumber == to {
// we already did hash check for this block
// we don't do the obvious `if s.BlockNumber > to` to support reorgs more naturally
s.Done()
return nil
}
var tx ethdb.DbWithPendingMutations
var useExternalTx bool
if hasTx, ok := db.(ethdb.HasTx); ok && hasTx.Tx() != nil {
tx = db.(ethdb.DbWithPendingMutations)
useExternalTx = true
} else {
var err error
tx, err = db.Begin(context.Background(), ethdb.RW)
if err != nil {
return err
}
defer tx.Rollback()
}
hash, err := rawdb.ReadCanonicalHash(tx, to)
if err != nil {
return err
}
syncHeadHeader := rawdb.ReadHeader(tx, hash, to)
expectedRootHash := syncHeadHeader.Root
logPrefix := s.state.LogPrefix()
log.Info(fmt.Sprintf("[%s] Generating intermediate hashes", logPrefix), "from", s.BlockNumber, "to", to)
if s.BlockNumber == 0 {
if err := RegenerateIntermediateHashes(logPrefix, tx, checkRoot, tmpdir, expectedRootHash, quit); err != nil {
return err
}
} else {
if err := incrementIntermediateHashes(logPrefix, s, tx, to, checkRoot, tmpdir, expectedRootHash, quit); err != nil {
return err
}
}
if err := s.DoneAndUpdate(tx, to); err != nil {
return err
}
if !useExternalTx {
if _, err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func RegenerateIntermediateHashes(logPrefix string, db ethdb.Database, checkRoot bool, tmpdir string, expectedRootHash common.Hash, quit <-chan struct{}) error {
log.Info(fmt.Sprintf("[%s] Regeneration intermediate hashes started", logPrefix))
// Clear IH bucket
c := db.(ethdb.HasTx).Tx().Cursor(dbutils.IntermediateTrieHashBucket)
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
}
if err = c.Delete(k, v); err != nil {
return err
}
}
c.Close()
buf := etl.NewSortableBuffer(etl.BufferOptimalSize)
comparator := db.(ethdb.HasTx).Tx().Comparator(dbutils.IntermediateTrieHashBucket)
buf.SetComparator(comparator)
collector := etl.NewCollector(tmpdir, buf)
hashCollector := func(keyHex []byte, hash []byte) error {
if len(keyHex) == 0 {
return nil
}
if len(keyHex) > trie.IHDupKeyLen {
return collector.Collect(keyHex[:trie.IHDupKeyLen], append(keyHex[trie.IHDupKeyLen:], hash...))
}
return collector.Collect(keyHex, hash)
}
loader := trie.NewFlatDBTrieLoader(logPrefix, dbutils.CurrentStateBucket, dbutils.IntermediateTrieHashBucket)
if err := loader.Reset(trie.NewRetainList(0), hashCollector /* HashCollector */, false); err != nil {
return err
}
t := time.Now()
if hash, err := loader.CalcTrieRoot(db, quit); err == nil {
generationIHTook := time.Since(t)
if checkRoot && hash != expectedRootHash {
return fmt.Errorf("%s: wrong trie root: %x, expected (from header): %x", logPrefix, hash, expectedRootHash)
}
log.Debug("Collection finished",
"root hash", hash.Hex(),
"gen IH", generationIHTook,
)
} else {
return err
}
if err := collector.Load(logPrefix, db, dbutils.IntermediateTrieHashBucket, etl.IdentityLoadFunc, etl.TransformArgs{
Quit: quit,
Comparator: comparator,
}); err != nil {
return fmt.Errorf("%s: fail load data to bucket: %w", logPrefix, err)
}
log.Info(fmt.Sprintf("[%s] Regeneration ended", logPrefix))
return nil
}
type HashPromoter struct {
db ethdb.Database
ChangeSetBufSize uint64
TempDir string
quitCh <-chan struct{}
}
func NewHashPromoter(db ethdb.Database, quitCh <-chan struct{}) *HashPromoter {
return &HashPromoter{
db: db,
ChangeSetBufSize: 256 * 1024 * 1024,
TempDir: os.TempDir(),
quitCh: quitCh,
}
}
func (p *HashPromoter) Promote(logPrefix string, s *StageState, from, to uint64, storage bool, load etl.LoadFunc) error {
var changeSetBucket string
if storage {
changeSetBucket = dbutils.PlainStorageChangeSetBucket
} else {
changeSetBucket = dbutils.PlainAccountChangeSetBucket
}
log.Debug(fmt.Sprintf("[%s] Incremental state promotion of intermediate hashes", logPrefix), "from", from, "to", to, "csbucket", changeSetBucket)
startkey := dbutils.EncodeBlockNumber(from + 1)
decode := changeset.Mapper[changeSetBucket].Decode
extract := func(dbKey, dbValue []byte, next etl.ExtractNextFunc) error {
_, k, _ := decode(dbKey, dbValue)
newK, err := transformPlainStateKey(k)
if err != nil {
return err
}
return next(dbKey, newK, nil)
}
var l OldestAppearedLoad
l.innerLoadFunc = load
if err := etl.Transform(
logPrefix,
p.db,
changeSetBucket,
"",
p.TempDir,
extract,
l.LoadFunc,
etl.TransformArgs{
BufferType: etl.SortableOldestAppearedBuffer,
ExtractStartKey: startkey,
Quit: p.quitCh,
},
); err != nil {
return err
}
return nil
}
func (p *HashPromoter) Unwind(logPrefix string, s *StageState, u *UnwindState, storage bool, load etl.LoadFunc) error {
to := u.UnwindPoint
var changeSetBucket string
if storage {
changeSetBucket = dbutils.PlainStorageChangeSetBucket
} else {
changeSetBucket = dbutils.PlainAccountChangeSetBucket
}
log.Info(fmt.Sprintf("[%s] Unwinding of intermediate hashes", logPrefix), "from", s.BlockNumber, "to", to, "csbucket", changeSetBucket)
startkey := dbutils.EncodeBlockNumber(to + 1)
decode := changeset.Mapper[changeSetBucket].Decode
extract := func(dbKey, dbValue []byte, next etl.ExtractNextFunc) error {
_, k, _ := decode(dbKey, dbValue)
newK, err := transformPlainStateKey(k)
if err != nil {
return err
}
return next(k, newK, nil)
}
var l OldestAppearedLoad
l.innerLoadFunc = load
if err := etl.Transform(
logPrefix,
p.db,
changeSetBucket,
"",
p.TempDir,
extract,
l.LoadFunc,
etl.TransformArgs{
BufferType: etl.SortableOldestAppearedBuffer,
ExtractStartKey: startkey,
Quit: p.quitCh,
},
); err != nil {
return err
}
return nil
}
func incrementIntermediateHashes(logPrefix string, s *StageState, db ethdb.Database, to uint64, checkRoot bool, tmpdir string, expectedRootHash common.Hash, quit <-chan struct{}) error {
p := NewHashPromoter(db, quit)
p.TempDir = tmpdir
var exclude [][]byte
//ihFilter := trie.NewPrefixFilter()
collect := func(k []byte, _ []byte, _ etl.CurrentTableReader, _ etl.LoadNextFunc) error {
exclude = append(exclude, k)
//ihFilter.Add(k)
return nil
}
if err := p.Promote(logPrefix, s, s.BlockNumber, to, false /* storage */, collect); err != nil {
return err
}
if err := p.Promote(logPrefix, s, s.BlockNumber, to, true /* storage */, collect); err != nil {
return err
}
sort.Slice(exclude, func(i, j int) bool { return bytes.Compare(exclude[i], exclude[j]) < 0 })
unfurl := trie.NewRetainList(0)
for i := range exclude {
unfurl.AddKey(exclude[i])
}
buf := etl.NewSortableBuffer(etl.BufferOptimalSize)
comparator := db.(ethdb.HasTx).Tx().Comparator(dbutils.IntermediateTrieHashBucket)
buf.SetComparator(comparator)
collector := etl.NewCollector(tmpdir, buf)
hashCollector := func(keyHex []byte, hash []byte) error {
if len(keyHex) == 0 {
return nil
}
if len(keyHex) > trie.IHDupKeyLen {
return collector.Collect(keyHex[:trie.IHDupKeyLen], append(keyHex[trie.IHDupKeyLen:], hash...))
}
return collector.Collect(keyHex, hash)
}
loader := trie.NewFlatDBTrieLoader(logPrefix, dbutils.CurrentStateBucket, dbutils.IntermediateTrieHashBucket)
// hashCollector in the line below will collect deletes
if err := loader.Reset(unfurl, hashCollector, false); err != nil {
return err
}
t := time.Now()
hash, err := loader.CalcTrieRoot(db, quit)
if err != nil {
return err
}
generationIHTook := time.Since(t)
if checkRoot && hash != expectedRootHash {
return fmt.Errorf("%s: wrong trie root: %x, expected (from header): %x", logPrefix, hash, expectedRootHash)
}
log.Info(fmt.Sprintf("[%s] Collection finished", logPrefix),
"root hash", hash.Hex(),
"gen IH", generationIHTook,
)
if err := collector.Load(logPrefix, db,
dbutils.IntermediateTrieHashBucket,
etl.IdentityLoadFunc,
etl.TransformArgs{
Quit: quit,
Comparator: comparator,
},
); err != nil {
return err
}
return nil
}
func UnwindIntermediateHashesStage(u *UnwindState, s *StageState, db ethdb.Database, tmpdir string, quit <-chan struct{}) error {
hash, err := rawdb.ReadCanonicalHash(db, u.UnwindPoint)
if err != nil {
return fmt.Errorf("read canonical hash: %w", err)
}
syncHeadHeader := rawdb.ReadHeader(db, hash, u.UnwindPoint)
expectedRootHash := syncHeadHeader.Root
var tx ethdb.DbWithPendingMutations
var useExternalTx bool
if hasTx, ok := db.(ethdb.HasTx); ok && hasTx.Tx() != nil {
tx = db.(ethdb.DbWithPendingMutations)
useExternalTx = true
} else {
var err error
tx, err = db.Begin(context.Background(), ethdb.RW)
if err != nil {
return fmt.Errorf("open transcation: %w", err)
}
defer tx.Rollback()
}
logPrefix := s.state.LogPrefix()
if err := unwindIntermediateHashesStageImpl(logPrefix, u, s, tx, tmpdir, expectedRootHash, quit); err != nil {
return err
}
if err := u.Done(tx); err != nil {
return fmt.Errorf("%s: reset: %w", logPrefix, err)
}
if !useExternalTx {
if _, err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func unwindIntermediateHashesStageImpl(logPrefix string, u *UnwindState, s *StageState, db ethdb.Database, tmpdir string, expectedRootHash common.Hash, quit <-chan struct{}) error {
p := NewHashPromoter(db, quit)
p.TempDir = tmpdir
var exclude [][]byte
collect := func(k []byte, _ []byte, _ etl.CurrentTableReader, _ etl.LoadNextFunc) error {
exclude = append(exclude, k)
return nil
}
if err := p.Unwind(logPrefix, s, u, false /* storage */, collect); err != nil {
return err
}
if err := p.Unwind(logPrefix, s, u, true /* storage */, collect); err != nil {
return err
}
sort.Slice(exclude, func(i, j int) bool { return bytes.Compare(exclude[i], exclude[j]) < 0 })
unfurl := trie.NewRetainList(0)
for i := range exclude {
unfurl.AddKey(exclude[i])
}
buf := etl.NewSortableBuffer(etl.BufferOptimalSize)
comparator := db.(ethdb.HasTx).Tx().Comparator(dbutils.IntermediateTrieHashBucket)
buf.SetComparator(comparator)
collector := etl.NewCollector(tmpdir, buf)
hashCollector := func(keyHex []byte, hash []byte) error {
if len(keyHex) == 0 {
return nil
}
if len(keyHex) > trie.IHDupKeyLen {
return collector.Collect(keyHex[:trie.IHDupKeyLen], append(keyHex[trie.IHDupKeyLen:], hash...))
}
return collector.Collect(keyHex, hash)
}
loader := trie.NewFlatDBTrieLoader(logPrefix, dbutils.CurrentStateBucket, dbutils.IntermediateTrieHashBucket)
// hashCollector in the line below will collect deletes
if err := loader.Reset(unfurl, hashCollector, false); err != nil {
return err
}
t := time.Now()
hash, err := loader.CalcTrieRoot(db, quit)
if err != nil {
return fmt.Errorf("calcTrieRoot: %w", err)
}
generationIHTook := time.Since(t)
if hash != expectedRootHash {
return fmt.Errorf("%s: wrong trie root: %x, expected (from header): %x", logPrefix, hash, expectedRootHash)
}
log.Info(fmt.Sprintf("[%s] Collection finished", logPrefix),
"root hash", hash.Hex(),
"gen IH", generationIHTook,
)
if err := collector.Load(logPrefix, db,
dbutils.IntermediateTrieHashBucket,
etl.IdentityLoadFunc,
etl.TransformArgs{
Quit: quit,
Comparator: comparator,
},
); err != nil {
return err
}
return nil
}
func ResetHashState(db ethdb.Database) error {
if err := db.(ethdb.BucketsMigrator).ClearBuckets(
dbutils.CurrentStateBucket,
dbutils.ContractCodeBucket,
dbutils.IntermediateTrieHashBucket,
); err != nil {
return err
}
batch := db.NewBatch()
if err := stages.SaveStageProgress(batch, stages.IntermediateHashes, 0); err != nil {
return err
}
if err := stages.SaveStageUnwind(batch, stages.IntermediateHashes, 0); err != nil {
return err
}
if err := stages.SaveStageProgress(batch, stages.HashState, 0); err != nil {
return err
}
if err := stages.SaveStageUnwind(batch, stages.HashState, 0); err != nil {
return err
}
if _, err := batch.Commit(); err != nil {
return err
}
return nil
}