forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 2
/
stage_hashstate.go
613 lines (551 loc) · 15.3 KB
/
stage_hashstate.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package stagedsync
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"os"
"runtime"
"time"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/length"
"github.com/ledgerwatch/erigon-lib/etl"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/changeset"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/log/v3"
)
type HashStateCfg struct {
db kv.RwDB
tmpDir string
}
func StageHashStateCfg(db kv.RwDB, tmpDir string) HashStateCfg {
return HashStateCfg{
db: db,
tmpDir: tmpDir,
}
}
func SpawnHashStateStage(s *StageState, tx kv.RwTx, cfg HashStateCfg, ctx context.Context) error {
useExternalTx := tx != nil
if !useExternalTx {
var err error
tx, err = cfg.db.BeginRw(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
}
logPrefix := s.LogPrefix()
to, err := s.ExecutionAt(tx)
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
return nil
}
if s.BlockNumber > to {
return fmt.Errorf("hashstate: promotion backwards from %d to %d", s.BlockNumber, to)
}
if to > s.BlockNumber+16 {
log.Info(fmt.Sprintf("[%s] Promoting plain state", logPrefix), "from", s.BlockNumber, "to", to)
}
if s.BlockNumber == 0 { // Initial hashing of the state is performed at the previous stage
if err := PromoteHashedStateCleanly(logPrefix, tx, cfg, ctx.Done()); err != nil {
return err
}
} else {
if err := promoteHashedStateIncrementally(logPrefix, s, s.BlockNumber, to, tx, cfg, ctx.Done()); err != nil {
return err
}
}
if err = s.Update(tx, to); err != nil {
return err
}
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func UnwindHashStateStage(u *UnwindState, s *StageState, tx kv.RwTx, cfg HashStateCfg, ctx context.Context) (err error) {
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
logPrefix := u.LogPrefix()
if err = unwindHashStateStageImpl(logPrefix, u, s, tx, cfg, ctx.Done()); err != nil {
return err
}
if err = u.Done(tx); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}
func unwindHashStateStageImpl(logPrefix string, u *UnwindState, s *StageState, tx kv.RwTx, cfg HashStateCfg, quit <-chan struct{}) error {
// Currently it does not require unwinding because it does not create any Intermediate Hash records
// and recomputes the state root from scratch
prom := NewPromoter(tx, quit)
prom.TempDir = cfg.tmpDir
if err := prom.Unwind(logPrefix, s, u, false /* storage */, true /* codes */); err != nil {
return err
}
if err := prom.Unwind(logPrefix, s, u, false /* storage */, false /* codes */); err != nil {
return err
}
if err := prom.Unwind(logPrefix, s, u, true /* storage */, false /* codes */); err != nil {
return err
}
return nil
}
func PromoteHashedStateCleanly(logPrefix string, db kv.RwTx, cfg HashStateCfg, quit <-chan struct{}) error {
if err := readPlainStateOnce(
logPrefix,
db,
cfg.tmpDir,
etl.IdentityLoadFunc,
quit,
); err != nil {
return err
}
return etl.Transform(
logPrefix,
db,
kv.PlainContractCode,
kv.ContractCode,
cfg.tmpDir,
keyTransformExtractFunc(transformContractCodeKey),
etl.IdentityLoadFunc,
etl.TransformArgs{
Quit: quit,
},
)
}
func readPlainStateOnce(
logPrefix string,
db kv.RwTx,
tmpdir string,
loadFunc etl.LoadFunc,
quit <-chan struct{},
) error {
bufferSize := etl.BufferOptimalSize
accCollector := etl.NewCollector(logPrefix, tmpdir, etl.NewSortableBuffer(bufferSize))
defer accCollector.Close()
storageCollector := etl.NewCollector(logPrefix, tmpdir, etl.NewSortableBuffer(bufferSize))
defer storageCollector.Close()
t := time.Now()
logEvery := time.NewTicker(30 * time.Second)
defer logEvery.Stop()
var m runtime.MemStats
c, err := db.Cursor(kv.PlainState)
if err != nil {
return err
}
defer c.Close()
convertAccFunc := func(key []byte) ([]byte, error) {
hash, err := common.HashData(key)
return hash[:], err
}
convertStorageFunc := func(key []byte) ([]byte, error) {
addrHash, err := common.HashData(key[:length.Addr])
if err != nil {
return nil, err
}
inc := binary.BigEndian.Uint64(key[length.Addr:])
secKey, err := common.HashData(key[length.Addr+length.Incarnation:])
if err != nil {
return nil, err
}
compositeKey := dbutils.GenerateCompositeStorageKey(addrHash, inc, secKey)
return compositeKey, nil
}
var startkey []byte
// reading kv.PlainState
for k, v, e := c.Seek(startkey); k != nil; k, v, e = c.Next() {
if e != nil {
return e
}
if err := libcommon.Stopped(quit); err != nil {
return err
}
if len(k) == 20 {
newK, err := convertAccFunc(k)
if err != nil {
return err
}
if err := accCollector.Collect(newK, v); err != nil {
return err
}
} else {
newK, err := convertStorageFunc(k)
if err != nil {
return err
}
if err := storageCollector.Collect(newK, v); err != nil {
return err
}
}
select {
default:
case <-logEvery.C:
runtime.ReadMemStats(&m)
log.Info(fmt.Sprintf("[%s] ETL [1/2] Extracting", logPrefix), "current key", fmt.Sprintf("%x...", k[:6]), "alloc", libcommon.ByteCount(m.Alloc), "sys", libcommon.ByteCount(m.Sys))
}
}
log.Trace(fmt.Sprintf("[%s] Extraction finished", logPrefix), "took", time.Since(t))
defer func(t time.Time) {
log.Trace(fmt.Sprintf("[%s] Load finished", logPrefix), "took", time.Since(t))
}(time.Now())
args := etl.TransformArgs{
Quit: quit,
}
if err := accCollector.Load(db, kv.HashedAccounts, loadFunc, args); err != nil {
return err
}
if err := storageCollector.Load(db, kv.HashedStorage, loadFunc, args); err != nil {
return err
}
return nil
}
func keyTransformExtractFunc(transformKey func([]byte) ([]byte, error)) etl.ExtractFunc {
return func(k, v []byte, next etl.ExtractNextFunc) error {
newK, err := transformKey(k)
if err != nil {
return err
}
return next(k, newK, v)
}
}
func transformPlainStateKey(key []byte) ([]byte, error) {
switch len(key) {
case length.Addr:
// account
hash, err := common.HashData(key)
return hash[:], err
case length.Addr + length.Incarnation + length.Hash:
// storage
addrHash, err := common.HashData(key[:length.Addr])
if err != nil {
return nil, err
}
inc := binary.BigEndian.Uint64(key[length.Addr:])
secKey, err := common.HashData(key[length.Addr+length.Incarnation:])
if err != nil {
return nil, err
}
compositeKey := dbutils.GenerateCompositeStorageKey(addrHash, inc, secKey)
return compositeKey, nil
default:
// no other keys are supported
return nil, fmt.Errorf("could not convert key from plain to hashed, unexpected len: %d", len(key))
}
}
func transformContractCodeKey(key []byte) ([]byte, error) {
if len(key) != length.Addr+length.Incarnation {
return nil, fmt.Errorf("could not convert code key from plain to hashed, unexpected len: %d", len(key))
}
address, incarnation := dbutils.PlainParseStoragePrefix(key)
addrHash, err := common.HashData(address[:])
if err != nil {
return nil, err
}
compositeKey := dbutils.GenerateStoragePrefix(addrHash[:], incarnation)
return compositeKey, nil
}
type OldestAppearedLoad struct {
innerLoadFunc etl.LoadFunc
lastKey bytes.Buffer
}
func (l *OldestAppearedLoad) LoadFunc(k, v []byte, table etl.CurrentTableReader, next etl.LoadNextFunc) error {
if bytes.Equal(k, l.lastKey.Bytes()) {
return nil
}
l.lastKey.Reset()
//nolint:errcheck
l.lastKey.Write(k)
return l.innerLoadFunc(k, v, table, next)
}
func NewPromoter(db kv.RwTx, quitCh <-chan struct{}) *Promoter {
return &Promoter{
db: db,
ChangeSetBufSize: 256 * 1024 * 1024,
TempDir: os.TempDir(),
quitCh: quitCh,
}
}
type Promoter struct {
db kv.RwTx
ChangeSetBufSize uint64
TempDir string
quitCh <-chan struct{}
}
func getExtractFunc(db kv.Tx, changeSetBucket string) etl.ExtractFunc {
decode := changeset.Mapper[changeSetBucket].Decode
return func(dbKey, dbValue []byte, next etl.ExtractNextFunc) error {
_, k, _, err := decode(dbKey, dbValue)
if err != nil {
return err
}
// ignoring value un purpose, we want the latest one and it is in PlainState
value, err := db.GetOne(kv.PlainState, k)
if err != nil {
return err
}
newK, err := transformPlainStateKey(k)
if err != nil {
return err
}
return next(dbKey, newK, value)
}
}
func getExtractCode(db kv.Tx, changeSetBucket string) etl.ExtractFunc {
decode := changeset.Mapper[changeSetBucket].Decode
return func(dbKey, dbValue []byte, next etl.ExtractNextFunc) error {
_, k, _, err := decode(dbKey, dbValue)
if err != nil {
return err
}
value, err := db.GetOne(kv.PlainState, k)
if err != nil {
return err
}
if len(value) == 0 {
return nil
}
incarnation, err := accounts.DecodeIncarnationFromStorage(value)
if err != nil {
return err
}
if incarnation == 0 {
return nil
}
plainKey := dbutils.PlainGenerateStoragePrefix(k, incarnation)
var codeHash []byte
codeHash, err = db.GetOne(kv.PlainContractCode, plainKey)
if err != nil {
return fmt.Errorf("getFromPlainCodesAndLoad for %x, inc %d: %w", plainKey, incarnation, err)
}
if codeHash == nil {
return nil
}
newK, err := transformContractCodeKey(plainKey)
if err != nil {
return err
}
return next(dbKey, newK, codeHash)
}
}
func getUnwindExtractStorage(changeSetBucket string) etl.ExtractFunc {
decode := changeset.Mapper[changeSetBucket].Decode
return func(dbKey, dbValue []byte, next etl.ExtractNextFunc) error {
_, k, v, err := decode(dbKey, dbValue)
if err != nil {
return err
}
newK, err := transformPlainStateKey(k)
if err != nil {
return err
}
return next(dbKey, newK, v)
}
}
func getUnwindExtractAccounts(db kv.Tx, changeSetBucket string) etl.ExtractFunc {
decode := changeset.Mapper[changeSetBucket].Decode
return func(dbKey, dbValue []byte, next etl.ExtractNextFunc) error {
_, k, v, err := decode(dbKey, dbValue)
if err != nil {
return err
}
newK, err := transformPlainStateKey(k)
if err != nil {
return err
}
if len(v) == 0 {
return next(dbKey, newK, v)
}
var acc accounts.Account
if err = acc.DecodeForStorage(v); err != nil {
return err
}
if !(acc.Incarnation > 0 && acc.IsEmptyCodeHash()) {
return next(dbKey, newK, v)
}
if codeHash, err := db.GetOne(kv.ContractCode, dbutils.GenerateStoragePrefix(newK, acc.Incarnation)); err == nil {
copy(acc.CodeHash[:], codeHash)
} else {
return fmt.Errorf("adjusting codeHash for ks %x, inc %d: %w", newK, acc.Incarnation, err)
}
value := make([]byte, acc.EncodingLengthForStorage())
acc.EncodeForStorage(value)
return next(dbKey, newK, value)
}
}
func getCodeUnwindExtractFunc(db kv.Tx, changeSetBucket string) etl.ExtractFunc {
decode := changeset.Mapper[changeSetBucket].Decode
return func(dbKey, dbValue []byte, next etl.ExtractNextFunc) error {
_, k, v, err := decode(dbKey, dbValue)
if err != nil {
return err
}
if len(v) == 0 {
return nil
}
var (
newK []byte
codeHash []byte
)
incarnation, err := accounts.DecodeIncarnationFromStorage(v)
if err != nil {
return err
}
if incarnation == 0 {
return nil
}
plainKey := dbutils.PlainGenerateStoragePrefix(k, incarnation)
codeHash, err = db.GetOne(kv.PlainContractCode, plainKey)
if err != nil {
return fmt.Errorf("getCodeUnwindExtractFunc: %w, key=%x", err, plainKey)
}
newK, err = transformContractCodeKey(plainKey)
if err != nil {
return err
}
return next(dbKey, newK, codeHash)
}
}
func (p *Promoter) Promote(logPrefix string, s *StageState, from, to uint64, storage bool, codes bool) error {
var changeSetBucket string
if storage {
changeSetBucket = kv.StorageChangeSet
} else {
changeSetBucket = kv.AccountChangeSet
}
if to > from+16 {
log.Info(fmt.Sprintf("[%s] Incremental promotion started", logPrefix), "from", from, "to", to, "codes", codes, "csbucket", changeSetBucket)
}
startkey := dbutils.EncodeBlockNumber(from + 1)
var l OldestAppearedLoad
l.innerLoadFunc = etl.IdentityLoadFunc
var loadBucket string
var extract etl.ExtractFunc
if codes {
loadBucket = kv.ContractCode
extract = getExtractCode(p.db, changeSetBucket)
} else {
if storage {
loadBucket = kv.HashedStorage
} else {
loadBucket = kv.HashedAccounts
}
extract = getExtractFunc(p.db, changeSetBucket)
}
if err := etl.Transform(
logPrefix,
p.db,
changeSetBucket,
loadBucket,
p.TempDir,
extract,
etl.IdentityLoadFunc,
etl.TransformArgs{
BufferType: etl.SortableOldestAppearedBuffer,
ExtractStartKey: startkey,
Quit: p.quitCh,
},
); err != nil {
return err
}
return nil
}
func (p *Promoter) Unwind(logPrefix string, s *StageState, u *UnwindState, storage bool, codes bool) error {
var changeSetBucket string
if storage {
changeSetBucket = kv.StorageChangeSet
} else {
changeSetBucket = kv.AccountChangeSet
}
from := s.BlockNumber
to := u.UnwindPoint
log.Info(fmt.Sprintf("[%s] Unwinding started", logPrefix), "from", from, "to", to, "storage", storage, "codes", codes)
startkey := dbutils.EncodeBlockNumber(to + 1)
var l OldestAppearedLoad
var loadBucket string
var extractFunc etl.ExtractFunc
if codes {
loadBucket = kv.ContractCode
extractFunc = getCodeUnwindExtractFunc(p.db, changeSetBucket)
l.innerLoadFunc = etl.IdentityLoadFunc
} else {
l.innerLoadFunc = etl.IdentityLoadFunc
if storage {
loadBucket = kv.HashedStorage
extractFunc = getUnwindExtractStorage(changeSetBucket)
} else {
loadBucket = kv.HashedAccounts
extractFunc = getUnwindExtractAccounts(p.db, changeSetBucket)
}
}
return etl.Transform(
logPrefix,
p.db,
changeSetBucket,
loadBucket,
p.TempDir,
extractFunc,
l.LoadFunc,
etl.TransformArgs{
BufferType: etl.SortableOldestAppearedBuffer,
ExtractStartKey: startkey,
Quit: p.quitCh,
LogDetailsExtract: func(k, v []byte) (additionalLogArguments []interface{}) {
return []interface{}{"progress", etl.ProgressFromKey(k)}
},
LogDetailsLoad: func(k, v []byte) (additionalLogArguments []interface{}) {
return []interface{}{"progress", etl.ProgressFromKey(k) + 50} // loading is the second stage, from 50..100
},
},
)
}
func promoteHashedStateIncrementally(logPrefix string, s *StageState, from, to uint64, db kv.RwTx, cfg HashStateCfg, quit <-chan struct{}) error {
prom := NewPromoter(db, quit)
prom.TempDir = cfg.tmpDir
if err := prom.Promote(logPrefix, s, from, to, false /* storage */, true /* codes */); err != nil {
return err
}
if err := prom.Promote(logPrefix, s, from, to, false /* storage */, false /* codes */); err != nil {
return err
}
if err := prom.Promote(logPrefix, s, from, to, true /* storage */, false /* codes */); err != nil {
return err
}
return nil
}
func PruneHashStateStage(s *PruneState, tx kv.RwTx, cfg HashStateCfg, ctx context.Context) (err error) {
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}