forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefault_stages.go
392 lines (375 loc) · 15.5 KB
/
default_stages.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
package stagedsync
import (
"context"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/log/v3"
)
func DefaultStages(ctx context.Context, snapshots SnapshotsCfg, headers HeadersCfg, cumulativeIndex CumulativeIndexCfg, blockHashCfg BlockHashesCfg, bodies BodiesCfg, senders SendersCfg, exec ExecuteBlockCfg, hashState HashStateCfg, trieCfg TrieCfg, history HistoryCfg, logIndex LogIndexCfg, callTraces CallTracesCfg, txLookup TxLookupCfg, finish FinishCfg, test bool) []*Stage {
return []*Stage{
{
ID: stages.Snapshots,
Description: "Download snapshots",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
if badBlockUnwind {
return nil
}
return SpawnStageSnapshots(s, ctx, tx, snapshots, firstCycle, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return nil
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return SnapshotsPrune(p, firstCycle, snapshots, ctx, tx)
},
},
{
ID: stages.Headers,
Description: "Download headers",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
if badBlockUnwind {
return nil
}
return SpawnStageHeaders(s, u, ctx, tx, headers, firstCycle, test, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return HeadersUnwind(u, s, tx, headers, test)
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return HeadersPrune(p, tx, headers, ctx)
},
},
{
ID: stages.CumulativeIndex,
Description: "Write Cumulative Index",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return SpawnStageCumulativeIndex(cumulativeIndex, s, tx, ctx, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindCumulativeIndexStage(u, cumulativeIndex, tx, ctx)
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return PruneCumulativeIndexStage(p, tx, ctx)
},
},
{
ID: stages.BlockHashes,
Description: "Write block hashes",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return SpawnBlockHashStage(s, tx, blockHashCfg, ctx, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindBlockHashStage(u, tx, blockHashCfg, ctx)
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return PruneBlockHashStage(p, tx, blockHashCfg, ctx)
},
},
{
ID: stages.Bodies,
Description: "Download block bodies",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return BodiesForward(s, u, ctx, tx, bodies, test, firstCycle, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindBodiesStage(u, tx, bodies, ctx)
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return PruneBodiesStage(p, tx, bodies, ctx)
},
},
{
ID: stages.Senders,
Description: "Recover senders from tx signatures",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return SpawnRecoverSendersStage(senders, s, u, tx, 0, ctx, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindSendersStage(u, tx, senders, ctx)
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return PruneSendersStage(p, tx, senders, ctx)
},
},
{
ID: stages.Execution,
Description: "Execute blocks w/o hash checks",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return SpawnExecuteBlocksStage(s, u, tx, 0, ctx, exec, firstCycle, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindExecutionStage(u, s, tx, ctx, exec, firstCycle, logger)
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return PruneExecutionStage(p, tx, exec, ctx, firstCycle)
},
},
{
ID: stages.HashState,
Description: "Hash the key in the state",
Disabled: bodies.historyV3 && ethconfig.EnableHistoryV4InTest,
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return SpawnHashStateStage(s, tx, hashState, ctx, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindHashStateStage(u, s, tx, hashState, ctx, logger)
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return PruneHashStateStage(p, tx, hashState, ctx)
},
},
{
ID: stages.IntermediateHashes,
Description: "Generate intermediate hashes and computing state root",
Disabled: bodies.historyV3 && ethconfig.EnableHistoryV4InTest,
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
if exec.chainConfig.IsPrague(0) {
_, err := SpawnVerkleTrie(s, u, tx, trieCfg, ctx, logger)
return err
}
_, err := SpawnIntermediateHashesStage(s, u, tx, trieCfg, ctx, logger)
return err
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
if exec.chainConfig.IsPrague(0) {
return UnwindVerkleTrie(u, s, tx, trieCfg, ctx, logger)
}
return UnwindIntermediateHashesStage(u, s, tx, trieCfg, ctx, logger)
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return PruneIntermediateHashesStage(p, tx, trieCfg, ctx)
},
},
{
ID: stages.CallTraces,
Description: "Generate call traces index",
DisabledDescription: "Work In Progress",
Disabled: bodies.historyV3,
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return SpawnCallTraces(s, tx, callTraces, ctx, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindCallTraces(u, s, tx, callTraces, ctx, logger)
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return PruneCallTraces(p, tx, callTraces, ctx, logger)
},
},
{
ID: stages.AccountHistoryIndex,
Description: "Generate account history index",
Disabled: bodies.historyV3,
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return SpawnAccountHistoryIndex(s, tx, history, ctx, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindAccountHistoryIndex(u, s, tx, history, ctx)
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return PruneAccountHistoryIndex(p, tx, history, ctx, logger)
},
},
{
ID: stages.StorageHistoryIndex,
Description: "Generate storage history index",
Disabled: bodies.historyV3,
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return SpawnStorageHistoryIndex(s, tx, history, ctx, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindStorageHistoryIndex(u, s, tx, history, ctx)
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return PruneStorageHistoryIndex(p, tx, history, ctx, logger)
},
},
{
ID: stages.LogIndex,
Description: "Generate receipt logs index",
Disabled: bodies.historyV3,
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return SpawnLogIndex(s, tx, logIndex, ctx, 0, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindLogIndex(u, s, tx, logIndex, ctx)
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return PruneLogIndex(p, tx, logIndex, ctx, logger)
},
},
{
ID: stages.TxLookup,
Description: "Generate tx lookup index",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return SpawnTxLookup(s, tx, 0 /* toBlock */, txLookup, ctx, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindTxLookup(u, s, tx, txLookup, ctx, logger)
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return PruneTxLookup(p, tx, txLookup, ctx, firstCycle, logger)
},
},
{
ID: stages.Finish,
Description: "Final: update current block for the RPC API",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, _ Unwinder, tx kv.RwTx, logger log.Logger) error {
return FinishForward(s, tx, finish, firstCycle)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindFinish(u, tx, finish, ctx)
},
Prune: func(firstCycle bool, p *PruneState, tx kv.RwTx, logger log.Logger) error {
return PruneFinish(p, tx, finish, ctx)
},
},
}
}
// StateStages are all stages necessary for basic unwind and stage computation, it is primarily used to process side forks and memory execution.
func StateStages(ctx context.Context, headers HeadersCfg, bodies BodiesCfg, blockHashCfg BlockHashesCfg, senders SendersCfg, exec ExecuteBlockCfg, hashState HashStateCfg, trieCfg TrieCfg) []*Stage {
return []*Stage{
{
ID: stages.Headers,
Description: "Download headers",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return nil
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return HeadersUnwind(u, s, tx, headers, false)
},
},
{
ID: stages.Bodies,
Description: "Download block bodies",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return BodiesForward(s, u, ctx, tx, bodies, false, false, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindBodiesStage(u, tx, bodies, ctx)
},
},
{
ID: stages.BlockHashes,
Description: "Write block hashes",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return SpawnBlockHashStage(s, tx, blockHashCfg, ctx, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindBlockHashStage(u, tx, blockHashCfg, ctx)
},
},
{
ID: stages.Senders,
Description: "Recover senders from tx signatures",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return SpawnRecoverSendersStage(senders, s, u, tx, 0, ctx, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindSendersStage(u, tx, senders, ctx)
},
},
{
ID: stages.Execution,
Description: "Execute blocks w/o hash checks",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return SpawnExecuteBlocksStage(s, u, tx, 0, ctx, exec, firstCycle, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindExecutionStage(u, s, tx, ctx, exec, firstCycle, logger)
},
},
{
ID: stages.HashState,
Description: "Hash the key in the state",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
return SpawnHashStateStage(s, tx, hashState, ctx, logger)
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindHashStateStage(u, s, tx, hashState, ctx, logger)
},
},
{
ID: stages.IntermediateHashes,
Description: "Generate intermediate hashes and computing state root",
Forward: func(firstCycle bool, badBlockUnwind bool, s *StageState, u Unwinder, tx kv.RwTx, logger log.Logger) error {
_, err := SpawnIntermediateHashesStage(s, u, tx, trieCfg, ctx, logger)
return err
},
Unwind: func(firstCycle bool, u *UnwindState, s *StageState, tx kv.RwTx, logger log.Logger) error {
return UnwindIntermediateHashesStage(u, s, tx, trieCfg, ctx, logger)
},
},
}
}
var DefaultForwardOrder = UnwindOrder{
stages.Snapshots,
stages.Headers,
stages.BlockHashes,
stages.Bodies,
// Stages below don't use Internet
stages.Senders,
stages.Execution,
stages.Translation,
stages.HashState,
stages.IntermediateHashes,
stages.CallTraces,
stages.AccountHistoryIndex,
stages.StorageHistoryIndex,
stages.LogIndex,
stages.TxLookup,
stages.Finish,
}
// UnwindOrder represents the order in which the stages needs to be unwound.
// The unwind order is important and not always just stages going backwards.
// Let's say, there is tx pool can be unwound only after execution.
// It's ok to remove some stage from here to disable only unwind of stage
type UnwindOrder []stages.SyncStage
type PruneOrder []stages.SyncStage
var DefaultUnwindOrder = UnwindOrder{
stages.Finish,
stages.TxLookup,
stages.LogIndex,
stages.StorageHistoryIndex,
stages.AccountHistoryIndex,
stages.CallTraces,
// Unwinding of IHashes needs to happen after unwinding HashState
stages.HashState,
stages.IntermediateHashes,
stages.Translation,
stages.Execution,
stages.Senders,
stages.Bodies,
stages.BlockHashes,
stages.Headers,
}
var StateUnwindOrder = UnwindOrder{
// Unwinding of IHashes needs to happen after unwinding HashState
stages.HashState,
stages.IntermediateHashes,
stages.Execution,
stages.Senders,
stages.Bodies,
stages.BlockHashes,
stages.Headers,
}
var DefaultPruneOrder = PruneOrder{
stages.Finish,
stages.TxLookup,
stages.LogIndex,
stages.StorageHistoryIndex,
stages.AccountHistoryIndex,
stages.CallTraces,
// Unwinding of IHashes needs to happen after unwinding HashState
stages.HashState,
stages.IntermediateHashes,
stages.Translation,
stages.Execution,
stages.Senders,
stages.Bodies,
stages.BlockHashes,
stages.Headers,
stages.Snapshots,
}
var MiningUnwindOrder = UnwindOrder{} // nothing to unwind in mining - because mining does not commit db changes
var MiningPruneOrder = PruneOrder{} // nothing to unwind in mining - because mining does not commit db changes