forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_txlookup.go
108 lines (93 loc) · 3.31 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
package stagedsync
import (
"bytes"
"encoding/binary"
"fmt"
"math/big"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/common/etl"
"github.com/ledgerwatch/turbo-geth/core/rawdb"
"github.com/ledgerwatch/turbo-geth/core/types"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/rlp"
)
func SpawnTxLookup(s *StageState, db ethdb.Database, tmpdir string, quitCh <-chan struct{}) error {
var blockNum uint64
var startKey []byte
lastProcessedBlockNumber := s.BlockNumber
if lastProcessedBlockNumber > 0 {
blockNum = lastProcessedBlockNumber + 1
}
syncHeadNumber, err := s.ExecutionAt(db)
if err != nil {
return err
}
logPrefix := s.state.LogPrefix()
startKey = dbutils.HeaderHashKey(blockNum)
if err = TxLookupTransform(logPrefix, db, startKey, dbutils.HeaderHashKey(syncHeadNumber), quitCh, tmpdir); err != nil {
return err
}
return s.DoneAndUpdate(db, syncHeadNumber)
}
func TxLookupTransform(logPrefix string, db ethdb.Database, startKey, endKey []byte, quitCh <-chan struct{}, tmpdir string) error {
return etl.Transform(logPrefix, db, dbutils.HeaderPrefix, dbutils.TxLookupPrefix, tmpdir, func(k []byte, v []byte, next etl.ExtractNextFunc) error {
if !dbutils.CheckCanonicalKey(k) {
return nil
}
blocknum := binary.BigEndian.Uint64(k)
blockHash := common.BytesToHash(v)
body := rawdb.ReadBody(db, blockHash, blocknum)
if body == nil {
return fmt.Errorf("%s: tx lookup generation, empty block body %d, hash %x", logPrefix, blocknum, v)
}
blockNumBytes := new(big.Int).SetUint64(blocknum).Bytes()
for _, tx := range body.Transactions {
if err := next(k, tx.Hash().Bytes(), blockNumBytes); 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, db ethdb.Database, tmpdir string, quitCh <-chan struct{}) error {
collector := etl.NewCollector(tmpdir, etl.NewSortableBuffer(etl.BufferOptimalSize))
logPrefix := s.state.LogPrefix()
// Remove lookup entries for blocks between unwindPoint+1 and stage.BlockNumber
if err := db.Walk(dbutils.BlockBodyPrefix, dbutils.EncodeBlockNumber(u.UnwindPoint+1), 0, func(k, v []byte) (b bool, e error) {
if err := common.Stopped(quitCh); err != nil {
return false, err
}
blockNumber := binary.BigEndian.Uint64(k[:8])
if blockNumber > s.BlockNumber {
return false, nil
}
if err := common.Stopped(quitCh); err != nil {
return false, err
}
body := new(types.BodyForStorage)
if err := rlp.Decode(bytes.NewReader(v), body); err != nil {
return false, fmt.Errorf("%s, rlp decode err: %w", logPrefix, err)
}
txs, _ := rawdb.ReadTransactions(db, body.BaseTxId, body.TxAmount)
for _, tx := range txs {
if err := collector.Collect(tx.Hash().Bytes(), nil); err != nil {
return false, err
}
}
return true, nil
}); err != nil {
return err
}
if err := collector.Load(logPrefix, db, dbutils.TxLookupPrefix, etl.IdentityLoadFunc, etl.TransformArgs{Quit: quitCh}); err != nil {
return err
}
return u.Done(db)
}