-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
357 lines (307 loc) · 12.2 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"time"
"github.com/ethereum/go-ethereum/rpc"
"github.com/joho/godotenv"
)
func printWithTimestamp(message string) {
// Get the current time
currentTime := time.Now()
// Format the time as needed. Example format: "2006-01-02 15:04:05"
timestamp := currentTime.Format("2006-01-02 15:04:05")
// Print the message with the timestamp
fmt.Printf("[%s] %s\n", timestamp, message)
}
func main() {
var startBlockNum int
var endBlockNum int
var chain string
var checkpoint int
flag.IntVar(&startBlockNum, "start-block-num", 11443817, "the block to start from")
flag.IntVar(&endBlockNum, "end-block-num", 11443817, "the block to end at")
flag.IntVar(&checkpoint, "checkpoint", -1, "checkpoint to load")
flag.StringVar(&chain, "chain", "base", "chain to use")
flag.Parse()
fmt.Println("Starting block: ", startBlockNum)
fmt.Println("End block: ", endBlockNum)
fmt.Println("Chain: ", chain)
fmt.Println("Checkpoint: ", checkpoint)
// Load the .env file
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}
var clientLocation string
if chain == "base" {
clientLocation = os.Getenv("BASE_RPC_URL")
} else {
clientLocation = os.Getenv("OPTIMISM_RPC_URL")
}
client, err := rpc.Dial(clientLocation)
if err != nil {
// Cannot connect to local node for some reason
log.Fatal(err)
}
var blockNum int
opcodes := make(map[string]int)
opcodesGasCost := make(map[string]float64)
averageOpcodesGasCost := make(map[string]float64)
maxOpcodesGasCost := make(map[string]float64)
minOpcodesGasCost := make(map[string]float64)
individualOpcodesGasCost := make(map[string]map[string]float64)
if checkpoint > 0 {
var err error
dirName := fmt.Sprintf("./results/%s/%s_%s/%s_%s", chain, strconv.Itoa(startBlockNum), strconv.Itoa(endBlockNum), strconv.Itoa(startBlockNum), strconv.Itoa(checkpoint))
opcodes, err = LoadJSONIntoIntMap(filepath.Join(dirName, "opcodesDistribution.json"))
if err != nil {
log.Fatalf("Error loading JSON file: %v", err)
}
averageOpcodesGasCost, err = LoadJSONIntoFloat64Map(filepath.Join(dirName, "averageOpcodesGasCost.json"))
if err != nil {
log.Fatalf("Error loading JSON file: %v", err)
}
maxOpcodesGasCost, err = LoadJSONIntoFloat64Map(filepath.Join(dirName, "maxOpcodesGasCost.json"))
if err != nil {
log.Fatalf("Error loading JSON file: %v", err)
}
minOpcodesGasCost, err = LoadJSONIntoFloat64Map(filepath.Join(dirName, "minOpcodesGasCost.json"))
if err != nil {
log.Fatalf("Error loading JSON file: %v", err)
}
opcodesGasCost, err = LoadJSONIntoFloat64Map(filepath.Join(dirName, "totalOpcodesGasCost.json"))
if err != nil {
log.Fatalf("Error loading JSON file: %v", err)
}
individualOpcodesGasCost, err = LoadJSONIntoFloat64NestedMap(filepath.Join(dirName, "individualOpcodesGasCost.json"))
if err != nil {
log.Fatalf("Error loading JSON file: %v", err)
}
// Output the loaded data for verification
fmt.Printf("opcodes: %v\n", opcodes)
fmt.Printf("averageOpcodesGasCost: %v\n", averageOpcodesGasCost)
fmt.Printf("maxOpcodesGasCost: %v\n", maxOpcodesGasCost)
fmt.Printf("minOpcodesGasCost: %v\n", minOpcodesGasCost)
fmt.Printf("opcodesGasCost: %v\n", opcodesGasCost)
fmt.Printf("individualOpcodesGasCost: %v\n", individualOpcodesGasCost)
blockNum = checkpoint
} else {
blockNum = startBlockNum
}
for ; blockNum <= endBlockNum; blockNum++ {
printWithTimestamp(strconv.Itoa(blockNum))
// checkpoint every 100 blocks
if (blockNum-startBlockNum) != 0 && (blockNum-startBlockNum)%100 == 0 {
// Calculate the current average gas cost for each opcode
for opcode, gas := range opcodesGasCost {
averageOpcodesGasCost[opcode] = gas / float64(opcodes[opcode])
}
dirName := fmt.Sprintf("./results/%s/%s_%s/%s_%s", chain, strconv.Itoa(startBlockNum), strconv.Itoa(endBlockNum), strconv.Itoa(startBlockNum), strconv.Itoa(blockNum))
saveResults(dirName, opcodes, averageOpcodesGasCost, maxOpcodesGasCost, minOpcodesGasCost, opcodesGasCost, individualOpcodesGasCost)
}
var result []map[string]interface{}
numTries := 2
blockNumHex := fmt.Sprintf("0x%x", blockNum)
for {
if numTries == 0 {
panic("Failed to trace block after 2 tries")
}
err = client.CallContext(context.Background(), &result, "debug_traceBlockByNumber", blockNumHex)
if err != nil {
log.Println("Failed to trace block: %v", err)
time.Sleep(1 * time.Second)
numTries -= 1
continue
}
break
}
if numTries == 0 {
log.Println("Failed to trace block %d after 2 tries, using tx trace instead", blockNum)
var result json.RawMessage
err := client.CallContext(context.Background(), &result, "eth_getBlockByNumber", blockNumHex, false)
if err != nil {
log.Fatalf("Failed to get block: %v", err)
}
var block map[string]interface{}
err = json.Unmarshal(result, &block)
if err != nil {
log.Fatalf("Failed to unmarshal block: %v", err)
}
txs := block["transactions"]
for _, txHash := range txs.([]interface{}) {
var txResult map[string]interface{}
err := client.CallContext(context.Background(), &txResult, "debug_traceTransaction", txHash)
if err != nil {
log.Fatalf("Failed to trace transaction for hash %s: %v", txHash, err)
}
for _, logEntry := range txResult["structLogs"].([]interface{}) {
log := logEntry.(map[string]interface{})
opcodes[log["op"].(string)]++
gasCost := float64(log["gasCost"].(float64))
// Add the gas cost to individualOpcodesGasCost, where
// {"<OPCODE>": {"<GAS_COST>": <COUNT>}}
if individualOpcodesGasCost[log["op"].(string)] == nil {
individualOpcodesGasCost[log["op"].(string)] = make(map[string]float64)
}
individualOpcodesGasCost[log["op"].(string)][strconv.Itoa(int(gasCost))] += 1
opcodesGasCost[log["op"].(string)] += gasCost
if maxOpcodesGasCost[log["op"].(string)] < gasCost {
maxOpcodesGasCost[log["op"].(string)] = gasCost
}
if minOpcodesGasCost[log["op"].(string)] == 0 || minOpcodesGasCost[log["op"].(string)] > gasCost {
minOpcodesGasCost[log["op"].(string)] = gasCost
}
}
}
} else {
callAllocatedGas := 0
getCallUsedGas := false
callOperation := ""
prevLog := map[string]interface{}{}
for _, txTrace := range result[:30] {
res := txTrace["result"].(map[string]interface{})
structLogs := res["structLogs"].([]interface{})
for _, logEntry := range structLogs {
log := logEntry.(map[string]interface{})
opcodes[log["op"].(string)]++
gasCost := float64(log["gasCost"].(float64))
// Within a CALL, DELEGATECALL, STATICCALL
// - The gascost of the CALL = the amount allocated - the amount remaining before the first operation in the call
// - The total cost of the CALL = the amount of gas left after it returns - the amount of available gas before the call
// - The total cost of the CALL execution = amount of gas left after it returns - the gas cost of the CALL - amount of gas left before the call
// - use the gas diff iff the next depth is somehow the same
if getCallUsedGas {
callGasCost := callAllocatedGas - int(log["gas"].(float64))
if log["depth"].(float64) == prevLog["depth"].(float64) {
callGasCost = int(prevLog["gas"].(float64)) - int(log["gas"].(float64))
}
if individualOpcodesGasCost[callOperation] == nil {
individualOpcodesGasCost[callOperation] = make(map[string]float64)
}
individualOpcodesGasCost[callOperation][strconv.Itoa(int(callGasCost))] += 1
opcodesGasCost[callOperation] += float64(callGasCost)
if maxOpcodesGasCost[callOperation] < float64(callGasCost) {
maxOpcodesGasCost[callOperation] = float64(callGasCost)
}
if minOpcodesGasCost[callOperation] == 0 || minOpcodesGasCost[callOperation] > float64(callGasCost) {
minOpcodesGasCost[callOperation] = float64(callGasCost)
}
getCallUsedGas = false
if callGasCost < 0 {
fmt.Println(prevLog)
fmt.Println(log)
fmt.Println(structLogs)
panic("Negative gas cost")
}
}
// If there's a new call, track the total gas for the call and update it in the next log
if (log["op"].(string) == "CALL") || (log["op"].(string) == "DELEGATECALL") || (log["op"].(string) == "STATICCALL") {
getCallUsedGas = true
callAllocatedGas = int(log["gasCost"].(float64))
callOperation = log["op"].(string)
prevLog = log
continue
}
// Add the gas cost to individualOpcodesGasCost, where
// {"<OPCODE>": {"<GAS_COST>": <COUNT>}}
if individualOpcodesGasCost[log["op"].(string)] == nil {
individualOpcodesGasCost[log["op"].(string)] = make(map[string]float64)
}
individualOpcodesGasCost[log["op"].(string)][strconv.Itoa(int(gasCost))] += 1
opcodesGasCost[log["op"].(string)] += gasCost
if maxOpcodesGasCost[log["op"].(string)] < gasCost {
maxOpcodesGasCost[log["op"].(string)] = gasCost
}
if minOpcodesGasCost[log["op"].(string)] == 0 || minOpcodesGasCost[log["op"].(string)] > gasCost {
minOpcodesGasCost[log["op"].(string)] = gasCost
}
prevLog = log
}
}
}
}
// Calculate average gas cost for each opcode
for opcode, gas := range opcodesGasCost {
averageOpcodesGasCost[opcode] = gas / float64(opcodes[opcode])
}
dirName := fmt.Sprintf("./results/%s/%s_%s", chain, strconv.Itoa(startBlockNum), strconv.Itoa(endBlockNum))
saveResults(dirName, opcodes, averageOpcodesGasCost, maxOpcodesGasCost, minOpcodesGasCost, opcodesGasCost, individualOpcodesGasCost)
defer client.Close()
}
func saveResults(dirName string, opcodes map[string]int, averageOpcodesGasCost map[string]float64, maxOpcodesGasCost map[string]float64, minOpcodesGasCost map[string]float64, opcodesGasCost map[string]float64, individualOpcodesGasCost map[string]map[string]float64) {
err := os.MkdirAll(dirName, os.ModePerm)
if err != nil {
log.Fatalf("Error creating directory: %v", err)
}
writeJSON(individualOpcodesGasCost, filepath.Join(dirName, "individualOpcodesGasCost.json"))
writeJSON(opcodes, filepath.Join(dirName, "opcodesDistribution.json"))
writeJSON(averageOpcodesGasCost, filepath.Join(dirName, "averageOpcodesGasCost.json"))
writeJSON(maxOpcodesGasCost, filepath.Join(dirName, "maxOpcodesGasCost.json"))
writeJSON(minOpcodesGasCost, filepath.Join(dirName, "minOpcodesGasCost.json"))
writeJSON(opcodesGasCost, filepath.Join(dirName, "totalOpcodesGasCost.json"))
}
func writeJSON(data interface{}, fileName string) {
// Convert map to JSON
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
log.Fatalf("Error marshalling to JSON: %v", err)
}
// Write JSON to a file
file, err := os.Create(fileName)
if err != nil {
log.Fatalf("Error creating file: %v", err)
}
defer file.Close()
_, err = file.Write(jsonData)
if err != nil {
log.Fatalf("Error writing to file: %v", err)
}
}
// LoadJSONIntoIntMap loads a JSON file and decodes it into a map[string]int
func LoadJSONIntoIntMap(filename string) (map[string]int, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("failed to open JSON file: %w", err)
}
defer file.Close()
var data map[string]int
decoder := json.NewDecoder(file)
if err := decoder.Decode(&data); err != nil {
return nil, fmt.Errorf("failed to decode JSON data: %w", err)
}
return data, nil
}
// LoadJSONIntoFloat64Map loads a JSON file and decodes it into a map[string]float64
func LoadJSONIntoFloat64Map(filename string) (map[string]float64, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("failed to open JSON file: %w", err)
}
defer file.Close()
var data map[string]float64
decoder := json.NewDecoder(file)
if err := decoder.Decode(&data); err != nil {
return nil, fmt.Errorf("failed to decode JSON data: %w", err)
}
return data, nil
}
func LoadJSONIntoFloat64NestedMap(filename string) (map[string]map[string]float64, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("failed to open JSON file: %w", err)
}
defer file.Close()
var data map[string]map[string]float64
decoder := json.NewDecoder(file)
if err := decoder.Decode(&data); err != nil {
return nil, fmt.Errorf("failed to decode JSON data: %w", err)
}
return data, nil
}