-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstage_finish.go
154 lines (135 loc) · 3.45 KB
/
stage_finish.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
package stagedsync
import (
"bytes"
"context"
"encoding/binary"
"reflect"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/rlp"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/log/v3"
)
type FinishCfg struct {
db kv.RwDB
tmpDir string
log log.Logger
}
func StageFinishCfg(db kv.RwDB, tmpDir string, logger log.Logger) FinishCfg {
return FinishCfg{
db: db,
log: logger,
tmpDir: tmpDir,
}
}
func FinishForward(s *StageState, tx kv.RwTx, cfg FinishCfg, initialCycle bool) error {
useExternalTx := tx != nil
if !useExternalTx {
var err error
tx, err = cfg.db.BeginRw(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
}
var executionAt uint64
var err error
if executionAt, err = s.ExecutionAt(tx); err != nil {
return err
}
if executionAt <= s.BlockNumber {
return nil
}
rawdb.WriteHeadBlockHash(tx, rawdb.ReadHeadHeaderHash(tx))
err = s.Update(tx, executionAt)
if err != nil {
return err
}
if initialCycle {
if err := params.SetErigonVersion(tx, params.VersionKeyFinished); err != nil {
return err
}
}
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func UnwindFinish(u *UnwindState, tx kv.RwTx, cfg FinishCfg, 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 err = u.Done(tx); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}
func PruneFinish(u *PruneState, tx kv.RwTx, cfg FinishCfg, 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
}
func NotifyNewHeaders(ctx context.Context, finishStageBeforeSync uint64, finishStageAfterSync uint64, unwindTo *uint64, notifier ChainEventNotifier, tx kv.Tx) error {
if notifier == nil {
log.Trace("RPC Daemon notification channel not set. No headers notifications will be sent")
return nil
}
// Notify all headers we have (either canonical or not) in a maximum range span of 1024
var notifyFrom uint64
if unwindTo != nil && *unwindTo != 0 && (*unwindTo) < finishStageBeforeSync {
notifyFrom = *unwindTo
} else {
heightSpan := finishStageAfterSync - finishStageBeforeSync
if heightSpan > 1024 {
heightSpan = 1024
}
notifyFrom = finishStageAfterSync - heightSpan
}
notifyFrom++
startKey := make([]byte, reflect.TypeOf(notifyFrom).Size()+32)
var notifyTo uint64
binary.BigEndian.PutUint64(startKey, notifyFrom)
if err := tx.ForEach(kv.Headers, startKey, func(k, headerRLP []byte) error {
if len(headerRLP) == 0 {
return nil
}
header := new(types.Header)
if err := rlp.Decode(bytes.NewReader(headerRLP), header); err != nil {
log.Error("Invalid block header RLP", "err", err)
return err
}
notifyTo = header.Number.Uint64()
notifier.OnNewHeader(header)
return libcommon.Stopped(ctx.Done())
}); err != nil {
log.Error("RPC Daemon notification failed", "error", err)
return err
}
log.Info("RPC Daemon notified of new headers", "from", notifyFrom-1, "to", notifyTo)
return nil
}