forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_txlookup.go
223 lines (205 loc) · 5.76 KB
/
stage_txlookup.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
package stagedsync
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"math/big"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/common/etl"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/ethdb/prune"
"github.com/ledgerwatch/erigon/rlp"
)
type TxLookupCfg struct {
db kv.RwDB
prune prune.Mode
tmpdir string
}
func StageTxLookupCfg(
db kv.RwDB,
prune prune.Mode,
tmpdir string,
) TxLookupCfg {
return TxLookupCfg{
db: db,
prune: prune,
tmpdir: tmpdir,
}
}
func SpawnTxLookup(s *StageState, tx kv.RwTx, cfg TxLookupCfg, ctx context.Context) (err error) {
quitCh := ctx.Done()
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
logPrefix := s.LogPrefix()
endBlock, err := s.ExecutionAt(tx)
if err != nil {
return err
}
startBlock := s.BlockNumber
pruneTo := cfg.prune.TxIndex.PruneTo(endBlock)
if startBlock < pruneTo {
startBlock = pruneTo
}
if startBlock > 0 {
startBlock++
}
startKey := dbutils.EncodeBlockNumber(startBlock)
if err = TxLookupTransform(logPrefix, tx, startKey, dbutils.EncodeBlockNumber(endBlock), quitCh, cfg); err != nil {
return err
}
if err = s.Update(tx, endBlock); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}
func TxLookupTransform(logPrefix string, tx kv.RwTx, startKey, endKey []byte, quitCh <-chan struct{}, cfg TxLookupCfg) error {
bigNum := new(big.Int)
return etl.Transform(logPrefix, tx, kv.HeaderCanonical, kv.TxLookup, cfg.tmpdir, func(k []byte, v []byte, next etl.ExtractNextFunc) error {
blocknum := binary.BigEndian.Uint64(k)
blockHash := common.BytesToHash(v)
body := rawdb.ReadBodyWithTransactions(tx, blockHash, blocknum)
if body == nil {
return fmt.Errorf("empty block body %d, hash %x", blocknum, v)
}
for _, txn := range body.Transactions {
if err := next(k, txn.Hash().Bytes(), bigNum.SetUint64(blocknum).Bytes()); err != nil {
return err
}
}
return nil
}, etl.IdentityLoadFunc, etl.TransformArgs{
Quit: quitCh,
ExtractStartKey: startKey,
ExtractEndKey: endKey,
LogDetailsExtract: func(k, v []byte) (additionalLogArguments []interface{}) {
return []interface{}{"block", binary.BigEndian.Uint64(k)}
},
})
}
func UnwindTxLookup(u *UnwindState, s *StageState, tx kv.RwTx, cfg TxLookupCfg, ctx context.Context) (err error) {
quitCh := ctx.Done()
if s.BlockNumber <= u.UnwindPoint {
return nil
}
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
if err := unwindTxLookup(u, s, tx, cfg, quitCh); err != nil {
return err
}
if err := u.Done(tx); err != nil {
return err
}
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func unwindTxLookup(u *UnwindState, s *StageState, tx kv.RwTx, cfg TxLookupCfg, quitCh <-chan struct{}) error {
reader := bytes.NewReader(nil)
logPrefix := s.LogPrefix()
return etl.Transform(logPrefix, tx, kv.BlockBody, kv.TxLookup, cfg.tmpdir, func(k, v []byte, next etl.ExtractNextFunc) error {
body := new(types.BodyForStorage)
reader.Reset(v)
if err := rlp.Decode(reader, body); err != nil {
return fmt.Errorf("rlp decode err: %w", err)
}
txs, err := rawdb.ReadTransactions(tx, body.BaseTxId, body.TxAmount)
if err != nil {
return err
}
for _, txn := range txs {
if err = next(k, txn.Hash().Bytes(), nil); err != nil {
return err
}
}
return nil
}, etl.IdentityLoadFunc, etl.TransformArgs{
Quit: quitCh,
ExtractStartKey: dbutils.EncodeBlockNumber(u.UnwindPoint + 1),
ExtractEndKey: dbutils.EncodeBlockNumber(s.BlockNumber),
LogDetailsExtract: func(k, v []byte) (additionalLogArguments []interface{}) {
return []interface{}{"block", binary.BigEndian.Uint64(k)}
},
})
}
func PruneTxLookup(s *PruneState, tx kv.RwTx, cfg TxLookupCfg, ctx context.Context) (err error) {
if !cfg.prune.TxIndex.Enabled() {
return nil
}
logPrefix := s.LogPrefix()
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
to := cfg.prune.TxIndex.PruneTo(s.ForwardProgress)
// Forward stage doesn't write anything before PruneTo point
// TODO: maybe need do binary search of values in db in this case
if s.PruneProgress != 0 {
if err = pruneTxLookup(tx, logPrefix, cfg.tmpdir, s, to, ctx); err != nil {
return err
}
}
if err = s.Done(tx); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}
func pruneTxLookup(tx kv.RwTx, logPrefix, tmpDir string, s *PruneState, pruneTo uint64, ctx context.Context) error {
reader := bytes.NewReader(nil)
return etl.Transform(logPrefix, tx, kv.BlockBody, kv.TxLookup, tmpDir, func(k, v []byte, next etl.ExtractNextFunc) error {
body := new(types.BodyForStorage)
reader.Reset(v)
if err := rlp.Decode(reader, body); err != nil {
return fmt.Errorf("rlp decode: %w", err)
}
txs, err := rawdb.ReadTransactions(tx, body.BaseTxId, body.TxAmount)
if err != nil {
return err
}
for _, txn := range txs {
if err := next(k, txn.Hash().Bytes(), nil); err != nil {
return err
}
}
return nil
}, etl.IdentityLoadFunc, etl.TransformArgs{
Quit: ctx.Done(),
ExtractStartKey: dbutils.EncodeBlockNumber(s.ForwardProgress),
ExtractEndKey: dbutils.EncodeBlockNumber(pruneTo),
LogDetailsExtract: func(k, v []byte) (additionalLogArguments []interface{}) {
return []interface{}{"block", binary.BigEndian.Uint64(k)}
},
})
}