Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

op-node: genesis deduplicate json write #9323

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions op-node/cmd/genesis/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/ethereum-optimism/optimism/op-bindings/hardhat"
"github.com/ethereum-optimism/optimism/op-chain-ops/genesis"
"github.com/ethereum-optimism/optimism/op-service/jsonutil"
)

var (
Expand Down Expand Up @@ -126,7 +127,7 @@ var Subcommands = cli.Commands{
return err
}

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

if err := writeJSONFile(ctx.String("outfile.l2"), l2Genesis); err != nil {
if err := jsonutil.WriteJSON(ctx.String("outfile.l2"), l2Genesis); err != nil {
return err
}
return writeJSONFile(ctx.String("outfile.rollup"), rollupConfig)
return jsonutil.WriteJSON(ctx.String("outfile.rollup"), rollupConfig)
},
},
}

// writeJSONFile will write a JSON file to disk at the given path
// containing the JSON serialized input value.
func writeJSONFile(outfile string, input any) error {
f, err := os.OpenFile(outfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return err
}
defer f.Close()

enc := json.NewEncoder(f)
enc.SetIndent("", " ")
return enc.Encode(input)
}

// rpcBlock represents the JSON serialization of a block from an Ethereum RPC.
type rpcBlock struct {
Hash common.Hash `json:"hash"`
Expand Down
Loading