-
Notifications
You must be signed in to change notification settings - Fork 2
/
main5.go
221 lines (209 loc) · 6.89 KB
/
main5.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
package main
import (
"bytes"
"context"
"fmt"
"github.com/c2h5oh/datasize"
"github.com/ledgerwatch/erigon-lib/kv"
"path"
"starlink-world/erigon-evm/common"
"starlink-world/erigon-evm/common/dbutils"
"starlink-world/erigon-evm/core/rawdb"
"starlink-world/erigon-evm/core/types"
"starlink-world/erigon-evm/ethdb/cbor"
kv2 "starlink-world/erigon-evm/kv/mdbx"
"starlink-world/erigon-evm/log"
"starlink-world/erigon-evm/rlp"
)
//将mdbx里边的数据写入到文件中,参考eth的文件存储方式,注意,仅仅存储Receipts,Senders,BlockBody,EthTx(其实为区块的body),Headers,HeaderCanonical
var (
datadir = "/Volumes/1T/erigon"
fromBlockNumber = uint64(0)
stopBlockNumber = uint64(2000000)
chaindata = path.Join(datadir, "chaindata")
databaseVerbosity = int(2)
referenceChaindata string
block, pruneTo, unwind uint64
unwindEvery uint64
freezer = "/Volumes/1T/chaindata"
batchSizeStr = "512M"
reset bool
bucket string
toChaindata string
migration string
integrityFast = true
integritySlow bool
file string
HeimdallURL string
txtrace bool // Whether to trace the execution (should only be used together eith `block`)
pruneFlag = "hrtc"
pruneH, pruneR, pruneT, pruneC uint64
pruneHBefore, pruneRBefore uint64
pruneTBefore, pruneCBefore uint64
experiments []string
chain string // Which chain to use (mainnet, ropsten, rinkeby, goerli, etc.)
syncmodeStr string
emptySliceByte, _ = rlp.EncodeToBytes([]interface{}{})
receiptSliceByte, _ = rlp.EncodeToBytes([]interface{}{})
)
func init() {
receipts := make(types.Receipts, 0)
buf := bytes.NewBuffer(make([]byte, 0, 1024))
if err := cbor.Marshal(buf, receipts); err != nil {
log.Error("receipt unmarshal failed", "err", err)
}
receiptSliceByte = buf.Bytes()
}
func openKV(label kv.Label, logger log.Logger, path string, exclusive bool) kv.RwDB {
opts := kv2.NewMDBX(logger).Freezer(freezer).NoBaseFreezerReadable(true).NoSnapFreezerReadable(true).Path(path).Label(label)
if exclusive {
opts = opts.Exclusive()
}
if databaseVerbosity != -1 {
opts = opts.DBVerbosity(kv.DBVerbosityLvl(databaseVerbosity))
}
return opts.MustOpen()
}
func openDB(path string, logger log.Logger, applyMigrations bool) kv.RwDB {
label := kv.ChainDB
db := openKV(label, logger, path, false)
return db
}
func must(err error) {
if err != nil {
panic(err)
}
}
func rewriteBlock(db kv.RwTx, number uint64) error {
hash, err := rawdb.ReadCanonicalHash(db, number)
if err != nil {
return fmt.Errorf("requested non-canonical hash %x.", hash)
}
headerData := rawdb.ReadHeaderRLP(db, hash, number)
if len(headerData) == 0 {
return fmt.Errorf("requested header hash %x.", hash)
}
header := new(types.Header)
if err := rlp.Decode(bytes.NewReader(headerData), header); err != nil {
return fmt.Errorf("requested error header hash %x.", hash)
}
storageBodyRLP := rawdb.ReadStorageBodyRLP(db, hash, number)
if len(storageBodyRLP) == 0 {
return fmt.Errorf("requested storage body rlp hash %x.", hash)
}
bodyForStorage := new(types.BodyForStorage)
if err := rlp.DecodeBytes(storageBodyRLP, bodyForStorage); err != nil {
return fmt.Errorf("requested error storage body rlp hash %x.", hash)
}
body := new(types.Body)
body.Uncles = bodyForStorage.Uncles
if bodyForStorage.TxAmount < 2 {
panic(fmt.Sprintf("block body hash too few txs amount: %d, %d", number, bodyForStorage.TxAmount))
}
baseTxId, txAmount := bodyForStorage.BaseTxId+1, bodyForStorage.TxAmount-2
transactions, err := rawdb.CanonicalTransactions(db, baseTxId, txAmount)
if err != nil {
log.Error("failed ReadTransactionByHash", "hash", hash, "block", number, "err", err)
return nil
}
receiptsData, err := db.GetOne(kv.Receipts, dbutils.HeaderKey(number, hash))
if err != nil {
log.Error("ReadHeaderRLP failed", "err", err)
}
if len(receiptsData) == 0 {
receiptsData = receiptSliceByte
}
numberBytes := dbutils.EncodeBlockNumber(number)
if len(transactions) == 0 {
if err := db.Put(kv.EthTx, numberBytes, emptySliceByte); err != nil {
return err
}
if err := db.Put(kv.Receipts, numberBytes, receiptsData); err != nil {
return err
}
if err := db.Put(kv.Senders, numberBytes, emptySliceByte); err != nil {
return err
}
if err := db.Put(kv.BlockBody, numberBytes, storageBodyRLP); err != nil {
return err
}
if err := db.Put(kv.Headers, numberBytes, headerData); err != nil {
return err
}
if err := db.Put(kv.HeaderCanonical, numberBytes, hash[:]); err != nil {
return err
}
return db.Commit()
}
senders, err := db.GetOne(kv.Senders, dbutils.BlockBodyKey(number, hash))
if err != nil {
return fmt.Errorf("readSenders failed: %w", err)
}
if len(senders)/length.Addr != len(transactions) {
return fmt.Errorf("requested senders non-canonical hash %x.", hash)
}
if err := db.Put(kv.Receipts, numberBytes, receiptsData); err != nil {
return err
}
if err := db.Put(kv.Senders, numberBytes, senders); err != nil {
return err
}
if err := db.Put(kv.BlockBody, numberBytes, storageBodyRLP); err != nil {
return err
}
if err := db.Put(kv.EthTx, numberBytes, rlp.MustEncodeToBytes(transactions)); err != nil {
return err
}
if err := db.Put(kv.Headers, numberBytes, headerData); err != nil {
return err
}
if err := db.Put(kv.HeaderCanonical, numberBytes, hash[:]); err != nil {
return err
}
return db.Commit()
}
func main() {
logger := log.New()
db := openDB(chaindata, logger, true)
defer db.Close()
var batchSize datasize.ByteSize
must(batchSize.UnmarshalText([]byte(batchSizeStr)))
if fromBlockNumber > stopBlockNumber {
return
}
logPrefix := "excuted"
//f, _ := os.Create("cpu_main5.prof")
//g, _ := os.Create("mem_main5.prof")
//pprof.StartCPUProfile(f)
//defer pprof.StopCPUProfile()
//defer pprof.WriteHeapProfile(g)
switch db.(type) {
case *kv2.MdbxKV:
v := db.(*kv2.MdbxKV)
old, err := v.Ancients()
if err != nil {
panic(err)
return
}
i := fromBlockNumber
if old >= i && old > 0 {
i = old
}
for ; i <= stopBlockNumber; i++ {
wtx, err := db.BeginRw(context.Background())
if err != nil {
wtx.Rollback()
panic(err)
}
if err := rewriteBlock(wtx, i); err != nil {
wtx.Rollback()
panic(err)
return
}
}
log.Info(fmt.Sprintf("[%s] Completed on", logPrefix), "block", fromBlockNumber)
default:
return
}
return
}