Skip to content

Commit

Permalink
Move json methods to op-service
Browse files Browse the repository at this point in the history
  • Loading branch information
ImTei committed Feb 7, 2024
1 parent 1d45b49 commit 961dbee
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 37 deletions.
5 changes: 3 additions & 2 deletions cannon/cmd/load_elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/urfave/cli/v2"

"github.com/ethereum-optimism/optimism/cannon/mipsevm"
"github.com/ethereum-optimism/optimism/op-service/jsonutil"
)

var (
Expand Down Expand Up @@ -66,10 +67,10 @@ func LoadELF(ctx *cli.Context) error {
if err != nil {
return fmt.Errorf("failed to compute program metadata: %w", err)
}
if err := WriteJSON[*mipsevm.Metadata](ctx.Path(LoadELFMetaFlag.Name), meta); err != nil {
if err := jsonutil.WriteJSON[*mipsevm.Metadata](ctx.Path(LoadELFMetaFlag.Name), meta, OutFilePerm); err != nil {
return fmt.Errorf("failed to output metadata: %w", err)
}
return WriteJSON[*mipsevm.State](ctx.Path(LoadELFOutFlag.Name), state)
return jsonutil.WriteJSON[*mipsevm.State](ctx.Path(LoadELFOutFlag.Name), state, OutFilePerm)
}

var LoadELFCommand = &cli.Command{
Expand Down
13 changes: 8 additions & 5 deletions cannon/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/ethereum-optimism/optimism/cannon/mipsevm"
preimage "github.com/ethereum-optimism/optimism/op-preimage"
"github.com/ethereum-optimism/optimism/op-service/jsonutil"
)

var (
Expand Down Expand Up @@ -90,6 +91,8 @@ var (
Name: "pprof.cpu",
Usage: "enable pprof cpu profiling",
}

OutFilePerm = os.FileMode(0o755)
)

type Proof struct {
Expand Down Expand Up @@ -234,7 +237,7 @@ func Run(ctx *cli.Context) error {
defer profile.Start(profile.NoShutdownHook, profile.ProfilePath("."), profile.CPUProfile).Stop()
}

state, err := LoadJSON[mipsevm.State](ctx.Path(RunInputFlag.Name))
state, err := jsonutil.LoadJSON[mipsevm.State](ctx.Path(RunInputFlag.Name))
if err != nil {
return err
}
Expand Down Expand Up @@ -284,7 +287,7 @@ func Run(ctx *cli.Context) error {
l.Info("no metadata file specified, defaulting to empty metadata")
meta = &mipsevm.Metadata{Symbols: nil} // provide empty metadata by default
} else {
if m, err := LoadJSON[mipsevm.Metadata](metaPath); err != nil {
if m, err := jsonutil.LoadJSON[mipsevm.Metadata](metaPath); err != nil {
return fmt.Errorf("failed to load metadata: %w", err)
} else {
meta = m
Expand Down Expand Up @@ -337,7 +340,7 @@ func Run(ctx *cli.Context) error {
}

if snapshotAt(state) {
if err := WriteJSON(fmt.Sprintf(snapshotFmt, step), state); err != nil {
if err := jsonutil.WriteJSON(fmt.Sprintf(snapshotFmt, step), state, OutFilePerm); err != nil {
return fmt.Errorf("failed to write state snapshot: %w", err)
}
}
Expand Down Expand Up @@ -369,7 +372,7 @@ func Run(ctx *cli.Context) error {
proof.OracleValue = witness.PreimageValue
proof.OracleOffset = witness.PreimageOffset
}
if err := WriteJSON(fmt.Sprintf(proofFmt, step), proof); err != nil {
if err := jsonutil.WriteJSON(fmt.Sprintf(proofFmt, step), proof, OutFilePerm); err != nil {
return fmt.Errorf("failed to write proof data: %w", err)
}
} else {
Expand Down Expand Up @@ -398,7 +401,7 @@ func Run(ctx *cli.Context) error {
}
}

if err := WriteJSON(ctx.Path(RunOutputFlag.Name), state); err != nil {
if err := jsonutil.WriteJSON(ctx.Path(RunOutputFlag.Name), state, OutFilePerm); err != nil {
return fmt.Errorf("failed to write state output: %w", err)
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion cannon/cmd/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/ethereum-optimism/optimism/cannon/mipsevm"
"github.com/ethereum-optimism/optimism/op-service/jsonutil"
"github.com/urfave/cli/v2"
)

Expand All @@ -25,7 +26,7 @@ var (
func Witness(ctx *cli.Context) error {
input := ctx.Path(WitnessInputFlag.Name)
output := ctx.Path(WitnessOutputFlag.Name)
state, err := LoadJSON[mipsevm.State](input)
state, err := jsonutil.LoadJSON[mipsevm.State](input)
if err != nil {
return fmt.Errorf("invalid input state (%v): %w", input, err)
}
Expand Down
2 changes: 1 addition & 1 deletion op-chain-ops/cmd/op-upgrade/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func entrypoint(ctx *cli.Context) error {

// Write the batch to disk or stdout
if outfile := ctx.Path("outfile"); outfile != "" {
if err := jsonutil.WriteJSON(outfile, batch); err != nil {
if err := jsonutil.WriteJSON(outfile, batch, 0o666); err != nil {
return err
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion op-chain-ops/cmd/op-version-check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func entrypoint(ctx *cli.Context) error {
}
// Write contract versions to disk or stdout
if outfile := ctx.Path("outfile"); outfile != "" {
if err := jsonutil.WriteJSON(outfile, output); err != nil {
if err := jsonutil.WriteJSON(outfile, output, 0o666); err != nil {
return err
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions op-node/cmd/genesis/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ var Subcommands = cli.Commands{
return err
}

return jsonutil.WriteJSON(ctx.String("outfile.l1"), l1Genesis)
return jsonutil.WriteJSON(ctx.String("outfile.l1"), l1Genesis, 0o666)
},
},
{
Expand Down Expand Up @@ -250,10 +250,10 @@ var Subcommands = cli.Commands{
return fmt.Errorf("generated rollup config does not pass validation: %w", err)
}

if err := jsonutil.WriteJSON(ctx.String("outfile.l2"), l2Genesis); err != nil {
if err := jsonutil.WriteJSON(ctx.String("outfile.l2"), l2Genesis, 0o666); err != nil {
return err
}
return jsonutil.WriteJSON(ctx.String("outfile.rollup"), rollupConfig)
return jsonutil.WriteJSON(ctx.String("outfile.rollup"), rollupConfig, 0o666)
},
},
}
Expand Down
7 changes: 4 additions & 3 deletions cannon/cmd/json.go → op-service/jsonutil/json.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package jsonutil

import (
"encoding/json"
Expand Down Expand Up @@ -27,14 +27,14 @@ func LoadJSON[X any](inputPath string) (*X, error) {
return &state, nil
}

func WriteJSON[X any](outputPath string, value X) error {
func WriteJSON[X any](outputPath string, value X, perm os.FileMode) error {
if outputPath == "" {
return nil
}
var out io.Writer
finish := func() error { return nil }
if outputPath != "-" {
f, err := ioutil.NewAtomicWriterCompressed(outputPath, 0755)
f, err := ioutil.NewAtomicWriterCompressed(outputPath, perm)
if err != nil {
return fmt.Errorf("failed to open output file: %w", err)
}
Expand All @@ -48,6 +48,7 @@ func WriteJSON[X any](outputPath string, value X) error {
out = os.Stdout
}
enc := json.NewEncoder(out)
enc.SetIndent("", " ")
if err := enc.Encode(value); err != nil {
return fmt.Errorf("failed to encode to JSON: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions cannon/cmd/json_test.go → op-service/jsonutil/json_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package jsonutil

import (
"encoding/json"
Expand All @@ -13,7 +13,7 @@ func TestRoundTripJSON(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, "test.json")
data := &jsonTestData{A: "yay", B: 3}
err := WriteJSON(file, data)
err := WriteJSON(file, data, 0o755)
require.NoError(t, err)

// Confirm the file is uncompressed
Expand All @@ -32,7 +32,7 @@ func TestRoundTripJSONWithGzip(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, "test.json.gz")
data := &jsonTestData{A: "yay", B: 3}
err := WriteJSON(file, data)
err := WriteJSON(file, data, 0o755)
require.NoError(t, err)

// Confirm the file isn't raw JSON
Expand Down
18 changes: 0 additions & 18 deletions op-service/jsonutil/write.go

This file was deleted.

0 comments on commit 961dbee

Please sign in to comment.