forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_senders.go
386 lines (348 loc) · 10 KB
/
stage_senders.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
package stagedsync
import (
"context"
"encoding/binary"
"errors"
"fmt"
"math"
"sync"
"time"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon-lib/common/length"
"github.com/ledgerwatch/erigon-lib/etl"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/core/rawdb/blockio"
"github.com/ledgerwatch/erigon/turbo/services"
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/secp256k1"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/common/debug"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/ethdb/prune"
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
"github.com/ledgerwatch/erigon/turbo/stages/headerdownload"
)
type SendersCfg struct {
db kv.RwDB
batchSize int
blockSize int
bufferSize int
numOfGoroutines int
readChLen int
badBlockHalt bool
tmpdir string
prune prune.Mode
chainConfig *chain.Config
blockRetire *snapshotsync.BlockRetire
hd *headerdownload.HeaderDownload
blockReader services.FullBlockReader
blockWriter *blockio.BlockWriter
}
func StageSendersCfg(db kv.RwDB, chainCfg *chain.Config, badBlockHalt bool, tmpdir string, prune prune.Mode, br *snapshotsync.BlockRetire, blockWriter *blockio.BlockWriter, blockReader services.FullBlockReader, hd *headerdownload.HeaderDownload) SendersCfg {
const sendersBatchSize = 10000
const sendersBlockSize = 4096
return SendersCfg{
db: db,
batchSize: sendersBatchSize,
blockSize: sendersBlockSize,
bufferSize: (sendersBlockSize * 10 / 20) * 10000, // 20*4096
numOfGoroutines: secp256k1.NumOfContexts(), // we can only be as parallels as our crypto library supports,
readChLen: 4,
badBlockHalt: badBlockHalt,
tmpdir: tmpdir,
chainConfig: chainCfg,
prune: prune,
blockRetire: br,
hd: hd,
blockReader: blockReader,
blockWriter: blockWriter,
}
}
func SpawnRecoverSendersStage(cfg SendersCfg, s *StageState, u Unwinder, tx kv.RwTx, toBlock uint64, ctx context.Context, logger log.Logger) error {
if cfg.blockRetire != nil && cfg.blockRetire.Snapshots() != nil && cfg.blockRetire.Snapshots().Cfg().Enabled && s.BlockNumber < cfg.blockRetire.Snapshots().BlocksAvailable() {
s.BlockNumber = cfg.blockRetire.Snapshots().BlocksAvailable()
}
quitCh := ctx.Done()
useExternalTx := tx != nil
if !useExternalTx {
var err error
tx, err = cfg.db.BeginRw(context.Background())
if err != nil {
return err
}
defer tx.Rollback()
}
prevStageProgress, errStart := stages.GetStageProgress(tx, stages.Bodies)
if errStart != nil {
return errStart
}
var to = prevStageProgress
if toBlock > 0 {
to = cmp.Min(prevStageProgress, toBlock)
}
if to < s.BlockNumber {
return nil
}
logPrefix := s.LogPrefix()
if to > s.BlockNumber+16 {
logger.Info(fmt.Sprintf("[%s] Started", logPrefix), "from", s.BlockNumber, "to", to)
}
logEvery := time.NewTicker(30 * time.Second)
defer logEvery.Stop()
startFrom := s.BlockNumber + 1
jobs := make(chan *senderRecoveryJob, cfg.batchSize)
out := make(chan *senderRecoveryJob, cfg.batchSize)
wg := new(sync.WaitGroup)
wg.Add(cfg.numOfGoroutines)
ctx, cancelWorkers := context.WithCancel(context.Background())
defer cancelWorkers()
for i := 0; i < cfg.numOfGoroutines; i++ {
go func(threadNo int) {
defer debug.LogPanic()
defer wg.Done()
// each goroutine gets it's own crypto context to make sure they are really parallel
recoverSenders(ctx, logPrefix, secp256k1.ContextForThread(threadNo), cfg.chainConfig, jobs, out, quitCh)
}(i)
}
collectorSenders := etl.NewCollector(logPrefix, cfg.tmpdir, etl.NewSortableBuffer(etl.BufferOptimalSize), logger)
defer collectorSenders.Close()
errCh := make(chan senderRecoveryError)
go func() {
defer debug.LogPanic()
defer close(errCh)
defer cancelWorkers()
var ok bool
var j *senderRecoveryJob
for {
select {
case <-quitCh:
return
case <-logEvery.C:
n := s.BlockNumber
if j != nil {
n += uint64(j.index)
}
logger.Info(fmt.Sprintf("[%s] Recovery", logPrefix), "block_number", n, "ch", fmt.Sprintf("%d/%d", len(jobs), cap(jobs)))
case j, ok = <-out:
if !ok {
return
}
if j.err != nil {
errCh <- senderRecoveryError{err: j.err, blockNumber: j.blockNumber, blockHash: j.blockHash}
return
}
k := make([]byte, 4)
binary.BigEndian.PutUint32(k, uint32(j.index))
index := int(binary.BigEndian.Uint32(k))
if err := collectorSenders.Collect(dbutils.BlockBodyKey(s.BlockNumber+uint64(index)+1, j.blockHash), j.senders); err != nil {
errCh <- senderRecoveryError{err: j.err}
return
}
}
}
}()
var minBlockNum uint64 = math.MaxUint64
var minBlockHash libcommon.Hash
var minBlockErr error
handleRecoverErr := func(recErr senderRecoveryError) error {
if recErr.blockHash == (libcommon.Hash{}) {
return recErr.err
}
if recErr.blockNumber < minBlockNum {
minBlockNum = recErr.blockNumber
minBlockHash = recErr.blockHash
minBlockErr = recErr.err
}
return nil
}
bodiesC, err := tx.Cursor(kv.HeaderCanonical)
if err != nil {
return err
}
defer bodiesC.Close()
Loop:
for k, v, err := bodiesC.Seek(hexutility.EncodeTs(startFrom)); k != nil; k, v, err = bodiesC.Next() {
if err != nil {
return err
}
if err := libcommon.Stopped(quitCh); err != nil {
return err
}
blockNumber := binary.BigEndian.Uint64(k)
blockHash := libcommon.BytesToHash(v)
if blockNumber > to {
break
}
has, err := cfg.blockReader.HasSenders(ctx, tx, blockHash, blockNumber)
if err != nil {
return err
}
if has {
continue
}
var body *types.Body
if body, err = cfg.blockReader.BodyWithTransactions(ctx, tx, blockHash, blockNumber); err != nil {
return err
}
if body == nil {
logger.Warn(fmt.Sprintf("[%s] ReadBodyWithTransactions can't find block", logPrefix), "num", blockNumber, "hash", blockHash)
continue
}
select {
case recoveryErr := <-errCh:
if recoveryErr.err != nil {
cancelWorkers()
if err := handleRecoverErr(recoveryErr); err != nil {
return err
}
break Loop
}
case jobs <- &senderRecoveryJob{body: body, key: k, blockNumber: blockNumber, blockHash: blockHash, index: int(blockNumber - s.BlockNumber - 1)}:
}
}
close(jobs)
wg.Wait()
close(out)
for recoveryErr := range errCh {
if recoveryErr.err != nil {
cancelWorkers()
if err := handleRecoverErr(recoveryErr); err != nil {
return err
}
}
}
if minBlockErr != nil {
logger.Error(fmt.Sprintf("[%s] Error recovering senders for block %d %x): %v", logPrefix, minBlockNum, minBlockHash, minBlockErr))
if cfg.badBlockHalt {
return minBlockErr
}
minHeader := rawdb.ReadHeader(tx, minBlockHash, minBlockNum)
if cfg.hd != nil {
cfg.hd.ReportBadHeaderPoS(minBlockHash, minHeader.ParentHash)
}
if to > s.BlockNumber {
u.UnwindTo(minBlockNum-1, minBlockHash)
}
} else {
if err := collectorSenders.Load(tx, kv.Senders, etl.IdentityLoadFunc, etl.TransformArgs{
Quit: quitCh,
LogDetailsLoad: func(k, v []byte) (additionalLogArguments []interface{}) {
return []interface{}{"block", binary.BigEndian.Uint64(k)}
},
}); 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
}
type senderRecoveryError struct {
err error
blockNumber uint64
blockHash libcommon.Hash
}
type senderRecoveryJob struct {
body *types.Body
key []byte
senders []byte
blockHash libcommon.Hash
blockNumber uint64
index int
err error
}
func recoverSenders(ctx context.Context, logPrefix string, cryptoContext *secp256k1.Context, config *chain.Config, in, out chan *senderRecoveryJob, quit <-chan struct{}) {
var job *senderRecoveryJob
var ok bool
for {
select {
case job, ok = <-in:
if !ok {
return
}
if job == nil {
return
}
case <-ctx.Done():
return
case <-quit:
return
}
body := job.body
signer := types.MakeSigner(config, job.blockNumber)
job.senders = make([]byte, len(body.Transactions)*length.Addr)
for i, tx := range body.Transactions {
from, err := signer.SenderWithContext(cryptoContext, tx)
if err != nil {
job.err = fmt.Errorf("%s: error recovering sender for tx=%x, %w", logPrefix, tx.Hash(), err)
break
}
copy(job.senders[i*length.Addr:], from[:])
}
// prevent sending to close channel
if err := libcommon.Stopped(quit); err != nil {
job.err = err
} else if err = libcommon.Stopped(ctx.Done()); err != nil {
job.err = err
}
out <- job
if errors.Is(job.err, libcommon.ErrStopped) {
return
}
}
}
func UnwindSendersStage(s *UnwindState, tx kv.RwTx, cfg SendersCfg, 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 = s.Done(tx); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}
func PruneSendersStage(s *PruneState, tx kv.RwTx, cfg SendersCfg, ctx context.Context) (err error) {
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
sn := cfg.blockRetire.Snapshots()
if sn.Cfg().Enabled {
// noop. in this case senders will be deleted by BlockRetire.PruneAncientBlocks after data-freezing.
} else if cfg.prune.TxIndex.Enabled() {
to := cfg.prune.TxIndex.PruneTo(s.ForwardProgress)
if err = rawdb.PruneTable(tx, kv.Senders, to, ctx, 100); err != nil {
return err
}
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}