Commit a85aca84 authored by clabby's avatar clabby

Break out binary / indexing logic into separate packages

parent 05364a2a
package main
import (
"errors"
"os"
"github.com/mattn/go-isatty"
"github.com/urfave/cli/v2"
"github.com/ethereum-optimism/optimism/op-chain-ops/eof"
"github.com/ethereum/go-ethereum/log"
)
func main() {
log.Root().SetHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(isatty.IsTerminal(os.Stderr.Fd()))))
app := &cli.App{
Name: "eof-crawler",
Usage: "Scan a Geth database for EOF-prefixed contracts",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "db-path",
Usage: "Path to the geth LevelDB",
},
&cli.StringFlag{
Name: "out",
Value: "eof-contracts.json",
Usage: "Path to the output file",
},
},
Action: func(ctx *cli.Context) error {
dbPath := ctx.String("db-path")
if len(dbPath) == 0 {
return errors.New("Must specify a db-path")
}
out := ctx.String("out")
eof.IndexEOFContracts(dbPath, out)
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Crit("error indexing state", "err", err)
}
}
...@@ -6,6 +6,6 @@ Simple CLI tool to scan all accounts in a geth LevelDB for contracts that begin ...@@ -6,6 +6,6 @@ Simple CLI tool to scan all accounts in a geth LevelDB for contracts that begin
1. Pass the directory of the Geth DB into the tool 1. Pass the directory of the Geth DB into the tool
```sh ```sh
go run eof_crawler.go <db_path> go run ./cmd/eof-crawler/main.go --db-path <db_path> [--out <out_file>]
``` ```
2. Once the indexing has completed, an array of all EOF-prefixed contracts will be written to `eof_contracts.json`. 2. Once the indexing has completed, an array of all EOF-prefixed contracts will be written to `eof_contracts.json` or the designated output file.
package main package eof
import ( import (
"bytes" "bytes"
...@@ -31,9 +31,11 @@ type Account struct { ...@@ -31,9 +31,11 @@ type Account struct {
// emptyCodeHash is the known hash of an account with no code. // emptyCodeHash is the known hash of an account with no code.
var emptyCodeHash = crypto.Keccak256(nil) var emptyCodeHash = crypto.Keccak256(nil)
func main() { // IndexEOFContracts indexes all the EOF contracts in the state trie of the head block
// for the given db and writes them to a JSON file.
func IndexEOFContracts(dbPath string, out string) {
// Open an existing Ethereum database // Open an existing Ethereum database
db, err := rawdb.NewLevelDBDatabase(os.Args[1], 16, 16, "", true) db, err := rawdb.NewLevelDBDatabase(dbPath, 16, 16, "", true)
if err != nil { if err != nil {
log.Fatalf("Failed to open database: %v", err) log.Fatalf("Failed to open database: %v", err)
} }
...@@ -126,10 +128,10 @@ func main() { ...@@ -126,10 +128,10 @@ func main() {
if err != nil { if err != nil {
log.Fatalf("Cannot marshal EOF contracts: %v", err) log.Fatalf("Cannot marshal EOF contracts: %v", err)
} }
err = os.WriteFile("eof_contracts.json", file, 0644) err = os.WriteFile(out, file, 0644)
if err != nil { if err != nil {
log.Fatalf("Failed to write EOF contracts array to file: %v", err) log.Fatalf("Failed to write EOF contracts array to file: %v", err)
} }
log.Printf("Wrote list of EOF contracts to `eof_contracts.json`") log.Printf("Wrote list of EOF contracts to `%v`", out)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment