forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stage_postexec.go
84 lines (71 loc) · 1.7 KB
/
stage_postexec.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
package stagedsync
import (
"context"
"fmt"
"github.com/ledgerwatch/erigon-lib/kv"
)
// PostExec stage is run after execution stage to peform extra verifications that are only possible when state is available.
// It is used for consensus engines which keep validators inside smart contracts (Bor, AuRa)
type PostExecCfg struct {
db kv.RwDB
borDb kv.RwDB
}
func StagePostExecCfg(db kv.RwDB, borDb kv.RwDB) PostExecCfg {
return PostExecCfg{
db: db,
borDb: borDb,
}
}
func SpawnPostExecStage(s *StageState, tx kv.RwTx, cfg PostExecCfg, 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 err = s.Update(tx, to); err != nil {
return err
}
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func UnwindPostExecStage(u *UnwindState, s *StageState, tx kv.RwTx, cfg PostExecCfg, 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 = u.Done(tx); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}