forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_verkle_trie.go
110 lines (103 loc) · 2.82 KB
/
stage_verkle_trie.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
package stagedsync
import (
"context"
"fmt"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/erigon/cmd/verkle/verkletrie"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
)
func SpawnVerkleTrie(s *StageState, u Unwinder, tx kv.RwTx, cfg TrieCfg, ctx context.Context, logger log.Logger) (libcommon.Hash, error) {
var err error
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return libcommon.Hash{}, err
}
defer tx.Rollback()
}
from := uint64(0)
if s.BlockNumber > 0 {
from = s.BlockNumber + 1
}
to, err := s.ExecutionAt(tx)
if err != nil {
return libcommon.Hash{}, err
}
verkleWriter := verkletrie.NewVerkleTreeWriter(tx, cfg.tmpDir, logger)
if err := verkletrie.IncrementAccount(tx, tx, 10, verkleWriter, from, to, cfg.tmpDir); err != nil {
return libcommon.Hash{}, err
}
var newRoot libcommon.Hash
if newRoot, err = verkletrie.IncrementStorage(tx, tx, 10, verkleWriter, from, to, cfg.tmpDir); err != nil {
return libcommon.Hash{}, err
}
if cfg.checkRoot {
header := rawdb.ReadHeaderByNumber(tx, to)
if header.Root != newRoot {
return libcommon.Hash{}, fmt.Errorf("invalid verkle root, header has %x, computed: %x", header.Root, newRoot)
}
}
if err := s.Update(tx, to); err != nil {
return libcommon.Hash{}, err
}
if err := stages.SaveStageProgress(tx, stages.VerkleTrie, to); err != nil {
return libcommon.Hash{}, err
}
if !useExternalTx {
return newRoot, tx.Commit()
}
return newRoot, nil
}
func UnwindVerkleTrie(u *UnwindState, s *StageState, tx kv.RwTx, cfg TrieCfg, ctx context.Context, logger log.Logger) (err error) {
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
from := u.UnwindPoint + 1
to, err := s.ExecutionAt(tx)
if err != nil {
return err
}
verkleWriter := verkletrie.NewVerkleTreeWriter(tx, cfg.tmpDir, logger)
if err := verkletrie.IncrementAccount(tx, tx, 10, verkleWriter, from, to, cfg.tmpDir); err != nil {
return err
}
if _, err = verkletrie.IncrementStorage(tx, tx, 10, verkleWriter, from, to, cfg.tmpDir); err != nil {
return err
}
if err := s.Update(tx, from); err != nil {
return err
}
if err := stages.SaveStageProgress(tx, stages.VerkleTrie, from); err != nil {
return err
}
if !useExternalTx {
return tx.Commit()
}
return nil
}
func PruneVerkleTries(s *PruneState, tx kv.RwTx, cfg TrieCfg, ctx context.Context) (err error) {
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
s.Done(tx)
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}