-
Notifications
You must be signed in to change notification settings - Fork 72
/
main.go
306 lines (276 loc) · 9.19 KB
/
main.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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"math/big"
"os"
"path"
"time"
"github.com/pkg/profile"
"github.com/urfave/cli/v2"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/beacon"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
gstate "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
op_service "github.com/ethereum-optimism/optimism/op-service"
"github.com/ethereum-optimism/optimism/op-service/cliapp"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/opio"
)
var EnvPrefix = "OP_SIMULATE"
var (
RPCFlag = &cli.StringFlag{
Name: "rpc",
Usage: "RPC endpoint to fetch prestate from",
EnvVars: op_service.PrefixEnvVar(EnvPrefix, "RPC"),
Required: true,
}
TxFlag = &cli.StringFlag{
Name: "tx",
Usage: "Transaction hash to trace and simulate",
EnvVars: op_service.PrefixEnvVar(EnvPrefix, "TX"),
Required: true,
}
ProfFlag = &cli.BoolFlag{
Name: "profile",
Usage: "profile the tx processing",
EnvVars: op_service.PrefixEnvVar(EnvPrefix, "PROFILE"),
Required: false,
}
)
func main() {
flags := []cli.Flag{
RPCFlag, TxFlag, ProfFlag,
}
flags = append(flags, oplog.CLIFlags(EnvPrefix)...)
app := cli.NewApp()
app.Name = "op-simulate"
app.Usage = "Simulate a tx locally."
app.Description = "Fetch a tx from an RPC and simulate it locally."
app.Flags = cliapp.ProtectFlags(flags)
app.Action = mainAction
app.Writer = os.Stdout
app.ErrWriter = os.Stderr
err := app.Run(os.Args)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Application failed: %v", err)
os.Exit(1)
}
}
func mainAction(c *cli.Context) error {
ctx := opio.CancelOnInterrupt(c.Context)
logCfg := oplog.ReadCLIConfig(c)
logger := oplog.NewLogger(c.App.Writer, logCfg)
endpoint := c.String(RPCFlag.Name)
cl, err := rpc.DialContext(ctx, endpoint)
if err != nil {
return fmt.Errorf("failed to dial RPC %q: %w", endpoint, err)
}
txHashStr := c.String(TxFlag.Name)
var txHash common.Hash
if err := txHash.UnmarshalText([]byte(txHashStr)); err != nil {
return fmt.Errorf("invalid tx hash: %q", txHashStr)
}
prestatesDir := "."
if err := fetchPrestate(ctx, cl, prestatesDir, txHash); err != nil {
return fmt.Errorf("failed to prepare prestate: %w", err)
}
chainConfig, err := fetchChainConfig(ctx, cl)
if err != nil {
return fmt.Errorf("failed to get chain config: %w", err)
}
tx, err := fetchTx(ctx, cl, txHash)
if err != nil {
return fmt.Errorf("failed to get TX: %w", err)
}
rec, err := fetchReceipt(ctx, cl, txHash)
if err != nil {
return fmt.Errorf("failed to get receipt: %w", err)
}
header, err := fetchHeader(ctx, cl, rec.BlockHash)
if err != nil {
return fmt.Errorf("failed to get block header: %w", err)
}
doProfile := c.Bool(ProfFlag.Name)
if err := simulate(ctx, logger, chainConfig, prestateTraceFile(prestatesDir, txHash), tx, header, doProfile); err != nil {
return fmt.Errorf("failed to simulate tx: %w", err)
}
return nil
}
// TraceConfig is different than Geth TraceConfig, quicknode sin't flexible
type TraceConfig struct {
Config *vm.LogConfig
Tracer string `json:"tracer"`
Timeout *string `json:"timeout"`
// Config specific to given tracer. Note struct logger
// config are historically embedded in main object.
TracerConfig json.RawMessage
}
func prestateTraceFile(dir string, txHash common.Hash) string {
return path.Join(dir, "prestate_"+txHash.String()+".json")
}
func fetchPrestate(ctx context.Context, cl *rpc.Client, dir string, txHash common.Hash) error {
dest := prestateTraceFile(dir, txHash)
// check cache
_, err := os.Stat(dest)
if err == nil {
// already known file
return nil
}
if !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("failed to check prestate file %q: %w", dest, err)
}
var result json.RawMessage
if err := cl.CallContext(ctx, &result, "debug_traceTransaction", txHash, TraceConfig{
Config: &vm.LogConfig{
EnableMemory: false,
DisableStack: true,
DisableStorage: true,
EnableReturnData: false,
Debug: false,
Limit: 0,
Overrides: nil,
},
Tracer: "prestateTracer",
Timeout: nil,
TracerConfig: nil,
}); err != nil {
return fmt.Errorf("failed to retrieve prestate trace: %w", err)
}
if err := os.WriteFile(dest, result, 0644); err != nil {
return fmt.Errorf("failed to write prestate trace: %w", err)
}
return nil
}
func fetchChainConfig(ctx context.Context, cl *rpc.Client) (*params.ChainConfig, error) {
/* [Kroma: START]
// first try the chain-ID RPC, this is widely available on any RPC provider.
var idResult hexutil.Big
if err := cl.CallContext(ctx, &idResult, "eth_chainId"); err != nil {
return nil, fmt.Errorf("failed to retrieve chain ID: %w", err)
}
// if we recognize the chain ID, we can get the chain config
id := (*big.Int)(&idResult)
if id.IsUint64() {
cfg, err := params.LoadOPStackChainConfig(id.Uint64())
if err == nil {
return cfg, nil
}
// ignore error, try to fetch chain config in full
}
[Kroma: END] */
// if not already recognized, then fetch the chain config manually
var config params.ChainConfig
if err := cl.CallContext(ctx, &config, "eth_chainConfig"); err != nil {
return nil, fmt.Errorf("failed to retrieve chain config: %w", err)
}
return &config, nil
}
func fetchTx(ctx context.Context, cl *rpc.Client, txHash common.Hash) (*types.Transaction, error) {
tx, pending, err := ethclient.NewClient(cl).TransactionByHash(ctx, txHash)
if pending {
return nil, fmt.Errorf("tx %s is still pending", txHash)
}
return tx, err
}
func fetchReceipt(ctx context.Context, cl *rpc.Client, txHash common.Hash) (*types.Receipt, error) {
return ethclient.NewClient(cl).TransactionReceipt(ctx, txHash)
}
func fetchHeader(ctx context.Context, cl *rpc.Client, blockHash common.Hash) (*types.Header, error) {
return ethclient.NewClient(cl).HeaderByHash(ctx, blockHash)
}
type DumpAccount struct {
Balance hexutil.Big `json:"balance"`
Nonce uint64 `json:"nonce"`
Code hexutil.Bytes `json:"code,omitempty"`
Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
}
func readDump(prestatePath string) (map[common.Address]DumpAccount, error) {
f, err := os.Open(prestatePath)
if err != nil {
return nil, fmt.Errorf("failed to load state data: %w", err)
}
var out map[common.Address]DumpAccount
if err := json.NewDecoder(f).Decode(&out); err != nil {
return nil, fmt.Errorf("failed to decode state data: %w", err)
}
return out, nil
}
type simChainContext struct {
eng consensus.Engine
head *types.Header
}
func (d *simChainContext) Engine() consensus.Engine {
return d.eng
}
func (d *simChainContext) GetHeader(h common.Hash, n uint64) *types.Header {
if n == d.head.Number.Uint64() {
return d.head
}
panic(fmt.Errorf("header retrieval not supported, cannot fetch %s %d", h, n))
}
func simulate(ctx context.Context, logger log.Logger, conf *params.ChainConfig,
prestatePath string, tx *types.Transaction, header *types.Header, doProfile bool) error {
memDB := rawdb.NewMemoryDatabase()
stateDB := gstate.NewDatabase(memDB)
state, err := gstate.New(types.EmptyRootHash, stateDB, nil)
if err != nil {
return fmt.Errorf("failed to create in-memory state: %w", err)
}
dump, err := readDump(prestatePath)
if err != nil {
return fmt.Errorf("failed to read prestate: %w", err)
}
for addr, acc := range dump {
state.CreateAccount(addr)
state.SetBalance(addr, new(big.Int).Set((*big.Int)(&acc.Balance)))
state.SetNonce(addr, acc.Nonce)
state.SetCode(addr, acc.Code)
state.SetStorage(addr, acc.Storage)
}
// load prestate data into memory db state
_, err = state.Commit(header.Number.Uint64()-1, true)
if err != nil {
return fmt.Errorf("failed to write state data to underlying DB: %w", err)
}
rules := conf.Rules(header.Number, true, header.Time)
signer := types.MakeSigner(conf, header.Number, header.Time)
sender, err := signer.Sender(tx)
if err != nil {
return fmt.Errorf("failed to get tx sender: %w", err)
}
// prepare the state
precompiles := vm.ActivePrecompiles(rules)
state.Prepare(rules, sender, header.Coinbase, tx.To(), precompiles, tx.AccessList())
state.SetTxContext(tx.Hash(), 0)
cCtx := &simChainContext{eng: beacon.NewFaker(), head: header}
gp := core.GasPool(tx.Gas())
usedGas := uint64(0)
vmConfig := vm.Config{}
if doProfile {
prof := profile.Start(profile.NoShutdownHook, profile.ProfilePath("."), profile.CPUProfile)
defer prof.Stop()
}
// run the transaction
start := time.Now()
receipt, err := core.ApplyTransaction(conf, cCtx, &sender, &gp, state, header, tx, &usedGas, vmConfig)
if err != nil {
return fmt.Errorf("failed to apply tx: %w", err)
}
end := time.Since(start)
logger.Info("processed tx", "elapsed", end,
"ok", receipt.Status == types.ReceiptStatusSuccessful, "logs", len(receipt.Logs))
return nil
}