-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
176 lines (164 loc) · 5.23 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
package main
import (
"context"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum-optimism/optimism/op-service/opio"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/rpc"
"github.com/status-im/keycard-go/hexutils"
"github.com/urfave/cli/v2"
"keroro520/bench/core"
"keroro520/bench/spec/simplecall"
"keroro520/bench/spec/simpletransfer"
"keroro520/bench/util"
"os"
)
func main() {
app := &cli.App{
Commands: []*cli.Command{
{
Name: "export",
Usage: "Export transactions based on the chain",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "http.rpc-url",
Usage: "The HTTP RPC endpoint of the existing chain",
Required: true,
},
&cli.StringFlag{
Name: "engine.rpc-url",
Usage: "The Engine API endpoint of the existing chain",
Required: true,
},
&cli.StringFlag{
Name: "engine.jwt-secret",
Usage: "The JWT secret used to sign JWTs for the Engine API",
Required: true,
},
&cli.StringFlag{
Name: "config-path",
Usage: "The path to the configuration file",
Required: true,
},
&cli.StringFlag{
Name: "output-path",
Usage: "The file path for the output",
Required: true,
},
},
Action: func(context *cli.Context) error {
rpcURL := context.String("http.rpc-url")
engineURL := context.String("engine.rpc-url")
engineJWTSecret := context.String("engine.jwt-secret")
configPath := context.String("config-path")
outputPath := context.String("output-path")
benchConfig, err := core.LoadConfig(configPath)
if err != nil {
log.Crit("Failed to load configuration file", "err", err)
}
client, err := ethclient.Dial(rpcURL)
if err != nil {
log.Crit("Failed to connect to the Ethereum client", "url", rpcURL, "err", err)
}
var jwtSecret32 [32]byte
copy(jwtSecret32[:], hexutils.HexToBytes(engineJWTSecret))
c, err := rpc.DialOptions(context.Context, engineURL, rpc.WithHTTPAuth(node.NewJWTAuth(jwtSecret32)))
if err != nil {
log.Crit("Failed to create Engine client", "err", err)
}
engine := ethclient.NewClient(c)
switch benchConfig.TxType {
case "simpletransfer":
spec := simpletransfer.NewSpec(*benchConfig.SimpleTransfer)
blockNumber, err := client.BlockNumber(context.Context)
if err != nil {
log.Crit("Failed to get block number", "err", err)
}
txHashes, err := spec.Run(context.Context, client, engine)
if err != nil {
log.Crit("Failed to run simpletransfer", "err", err)
}
err = util.Export(context.Context, client, blockNumber, txHashes, outputPath)
if err != nil {
log.Crit("Failed to dump transactions", "err", err)
}
case "simplecall":
err = simplecall.NewSpec(*benchConfig.SimpleCall).Run(context.Context, client)
if err != nil {
log.Crit("Failed to run simplecall", "err", err)
}
default:
log.Crit("Unknown transaction type: %s", benchConfig.TxType)
}
return nil
},
},
{
Name: "import",
Usage: "import transactions to the chain",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "engine.rpc-url",
Usage: "The Engine API endpoint of the existing chain",
Required: true,
},
&cli.StringFlag{
Name: "engine.jwt-secret",
Usage: "The JWT secret used to sign JWTs for the Engine API",
Required: true,
},
&cli.StringFlag{
Name: "input-path",
Usage: "The file path for the input",
Required: true,
},
&cli.IntFlag{
Name: "txs-per-block",
Usage: "The number of transactions to include in each block",
Required: true,
},
},
Action: func(context *cli.Context) error {
engineURL := context.String("engine.rpc-url")
engineJWTSecret := context.String("engine.jwt-secret")
inputPath := context.String("input-path")
TxsPerBlock := context.Int("txs-per-block")
var jwtSecret32 [32]byte
copy(jwtSecret32[:], hexutils.HexToBytes(engineJWTSecret))
c, err := rpc.DialOptions(context.Context, engineURL, rpc.WithHTTPAuth(node.NewJWTAuth(jwtSecret32)))
if err != nil {
log.Crit("Failed to create Engine client", "err", err)
}
engine := ethclient.NewClient(c)
err = util.Import(context.Context, inputPath, engine, TxsPerBlock)
if err != nil {
log.Crit("Failed to import transactions", "err", err)
}
return nil
},
},
},
}
// This is the most root context, used to propagate
// cancellations to all spawned application-level goroutines
ctx, cancel := context.WithCancel(context.Background())
go func() {
opio.BlockOnInterrupts()
cancel()
}()
oplog.SetupDefaults()
if err := app.RunContext(ctx, os.Args); err != nil {
log.Error("application failed", "err", err)
os.Exit(1)
}
// TODO CTRL-C handling
//go func() {
// stop := make(chan os.Signal, 1)
// signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
// <-stop
// log.Info("Received SIGINT or SIGTERM. Shutting down gracefully...")
// os.Exit(0)
//}()
}