forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_txpool.go
298 lines (269 loc) · 7.47 KB
/
stage_txpool.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
package stagedsync
import (
"context"
"encoding/binary"
"fmt"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/length"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/log/v3"
)
type TxPoolCfg struct {
db kv.RwDB
pool *core.TxPool
config core.TxPoolConfig
startFunc func()
}
func StageTxPoolCfg(db kv.RwDB, pool *core.TxPool, config core.TxPoolConfig, startFunc func()) TxPoolCfg {
return TxPoolCfg{
db: db,
pool: pool,
config: config,
startFunc: startFunc,
}
}
func SpawnTxPool(s *StageState, tx kv.RwTx, cfg TxPoolCfg, ctx context.Context) error {
quitCh := ctx.Done()
useExternalTx := tx != nil
if !useExternalTx {
var err error
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
to, err := s.ExecutionAt(tx)
if err != nil {
return err
}
logPrefix := s.LogPrefix()
if to < s.BlockNumber {
return fmt.Errorf("to (%d) < from (%d)", to, s.BlockNumber)
}
if cfg.pool != nil && !cfg.pool.IsStarted() {
log.Info(fmt.Sprintf("[%s] Starting tx pool after sync", logPrefix), "from", s.BlockNumber, "to", to)
headHash, err := rawdb.ReadCanonicalHash(tx, to)
if err != nil {
return err
}
headHeader := rawdb.ReadHeader(tx, headHash, to)
if err := cfg.pool.Start(headHeader.GasLimit, to); err != nil {
return fmt.Errorf(" start pool phase 1: %w", err)
}
if cfg.startFunc != nil {
cfg.startFunc()
}
}
if cfg.pool != nil && cfg.pool.IsStarted() && s.BlockNumber > 0 {
if err := incrementalTxPoolUpdate(logPrefix, s.BlockNumber, to, cfg.pool, tx, quitCh); err != nil {
return err
}
pending, queued := cfg.pool.Stats()
log.Info(fmt.Sprintf("[%s] Transaction stats", logPrefix), "pending", pending, "queued", queued)
}
if err := s.Update(tx, to); err != nil {
return err
}
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func incrementalTxPoolUpdate(logPrefix string, from, to uint64, pool *core.TxPool, tx kv.RwTx, quitCh <-chan struct{}) error {
headHash, err := rawdb.ReadCanonicalHash(tx, to)
if err != nil {
return err
}
headHeader := rawdb.ReadHeader(tx, headHash, to)
pool.ResetHead(headHeader.GasLimit, to)
canonical := make([]common.Hash, to-from)
currentHeaderIdx := uint64(0)
canonicals, err := tx.Cursor(kv.HeaderCanonical)
if err != nil {
return err
}
defer canonicals.Close()
for k, v, err := canonicals.Seek(dbutils.EncodeBlockNumber(from + 1)); k != nil; k, v, err = canonicals.Next() {
if err != nil {
return err
}
if err := libcommon.Stopped(quitCh); err != nil {
return err
}
if currentHeaderIdx >= to-from { // if header stage is ahead of body stage
break
}
copy(canonical[currentHeaderIdx][:], v)
currentHeaderIdx++
}
log.Trace(fmt.Sprintf("[%s] Read canonical hashes", logPrefix), "hashes", len(canonical))
bodies, err := tx.Cursor(kv.BlockBody)
if err != nil {
return err
}
defer bodies.Close()
for k, _, err := bodies.Seek(dbutils.EncodeBlockNumber(from + 1)); k != nil; k, _, err = bodies.Next() {
if err != nil {
return err
}
if err := libcommon.Stopped(quitCh); err != nil {
return err
}
blockNumber := binary.BigEndian.Uint64(k[:8])
blockHash := common.BytesToHash(k[8:])
if blockNumber > to {
break
}
if canonical[blockNumber-from-1] != blockHash {
// non-canonical case
continue
}
body := rawdb.ReadBodyWithTransactions(tx, blockHash, blockNumber)
for _, tx := range body.Transactions {
pool.RemoveTx(tx.Hash(), true /* outofbound */)
}
}
return nil
}
func UnwindTxPool(u *UnwindState, s *StageState, tx kv.RwTx, cfg TxPoolCfg, ctx context.Context) (err error) {
if u.UnwindPoint >= s.BlockNumber {
return nil
}
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
quitCh := ctx.Done()
logPrefix := s.LogPrefix()
if cfg.pool != nil && cfg.pool.IsStarted() {
if err := unwindTxPoolUpdate(logPrefix, u.UnwindPoint, s.BlockNumber, cfg.pool, tx, quitCh); err != nil {
return err
}
pending, queued := cfg.pool.Stats()
log.Info(fmt.Sprintf("[%s] Transaction stats", logPrefix), "pending", pending, "queued", queued)
}
if err := u.Done(tx); err != nil {
return err
}
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func unwindTxPoolUpdate(logPrefix string, from, to uint64, pool *core.TxPool, tx kv.RwTx, quitCh <-chan struct{}) error {
headHash, err := rawdb.ReadCanonicalHash(tx, from)
if err != nil {
return err
}
headHeader := rawdb.ReadHeader(tx, headHash, from)
pool.ResetHead(headHeader.GasLimit, from)
canonical := make([]common.Hash, to-from)
canonicals, err := tx.Cursor(kv.HeaderCanonical)
if err != nil {
return err
}
defer canonicals.Close()
for k, v, err := canonicals.Seek(dbutils.EncodeBlockNumber(from + 1)); k != nil; k, v, err = canonicals.Next() {
if err != nil {
return err
}
if err := libcommon.Stopped(quitCh); err != nil {
return err
}
blockNumber := binary.BigEndian.Uint64(k[:8])
if blockNumber > to {
break
}
copy(canonical[blockNumber-from-1][:], v)
}
log.Trace(fmt.Sprintf("[%s] Read canonical hashes", logPrefix), "hashes", len(canonical))
senders := make([][]common.Address, to-from+1)
sendersC, err := tx.Cursor(kv.Senders)
if err != nil {
return err
}
defer sendersC.Close()
for k, v, err := sendersC.Seek(dbutils.EncodeBlockNumber(from + 1)); k != nil; k, v, err = sendersC.Next() {
if err != nil {
return err
}
if err := libcommon.Stopped(quitCh); err != nil {
return err
}
blockNumber := binary.BigEndian.Uint64(k[:8])
blockHash := common.BytesToHash(k[8:])
if blockNumber > to {
break
}
if canonical[blockNumber-from-1] != blockHash {
// non-canonical case
continue
}
sendersArray := make([]common.Address, len(v)/length.Addr)
for i := 0; i < len(sendersArray); i++ {
copy(sendersArray[i][:], v[i*length.Addr:])
}
senders[blockNumber-from-1] = sendersArray
}
var txsToInject []types.Transaction
bodies, err := tx.Cursor(kv.BlockBody)
if err != nil {
return err
}
defer bodies.Close()
for k, _, err := bodies.Seek(dbutils.EncodeBlockNumber(from + 1)); k != nil; k, _, err = bodies.Next() {
if err != nil {
return err
}
if err := libcommon.Stopped(quitCh); err != nil {
return err
}
blockNumber := binary.BigEndian.Uint64(k[:8])
blockHash := common.BytesToHash(k[8:])
if blockNumber > to {
break
}
if canonical[blockNumber-from-1] != blockHash {
// non-canonical case
continue
}
body := rawdb.ReadBodyWithTransactions(tx, blockHash, blockNumber)
body.SendersToTxs(senders[blockNumber-from-1])
txsToInject = append(txsToInject, body.Transactions...)
}
//nolint:errcheck
log.Info(fmt.Sprintf("[%s] Injecting txs into the pool", logPrefix), "number", len(txsToInject))
pool.AddRemotesSync(txsToInject)
log.Info(fmt.Sprintf("[%s] Injection complete", logPrefix))
return nil
}
func PruneTxPool(s *PruneState, tx kv.RwTx, cfg TxPoolCfg, 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
}