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-chain-ops/ecotone-scalar: prefer .FillBytes() to .Bytes() and copy #13472

Merged
merged 6 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 22 additions & 8 deletions op-chain-ops/cmd/ecotone-scalar/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"math"
Expand All @@ -10,6 +11,14 @@ import (
"github.com/ethereum-optimism/optimism/op-service/eth"
)

// These names match those used in the SystemConfig contract
type outputTy struct {
BaseFee uint `json:"baseFeeScalar"`
BlobbaseFeeScalar uint `json:"blobbaseFeeScalar"`
ScalarHex string `json:"scalarHex"`
Scalar *big.Int `json:"scalar"` // post-ecotone
}

func main() {
var scalar, blobScalar uint
var decode string
Expand Down Expand Up @@ -43,13 +52,13 @@ func main() {
flag.Usage()
os.Exit(2)
}
encodedSlice := uint256.Bytes()
if len(encodedSlice) > 32 {
byteLen := (uint256.BitLen() + 7) / 8
if byteLen > 32 {
fmt.Fprintln(flag.CommandLine.Output(), "post-ecotone scalar out of uint256 range")
flag.Usage()
os.Exit(2)
}
copy(encoded[:], encodedSlice)
uint256.FillBytes(encoded[:])
decoded, err := eth.DecodeScalar(encoded)
if err != nil {
fmt.Fprintln(flag.CommandLine.Output(), "post-ecotone scalar could not be decoded:", err)
Expand All @@ -66,9 +75,14 @@ func main() {
}
i := new(big.Int).SetBytes(encoded[:])

fmt.Println("# base fee scalar :", scalar)
fmt.Println("# blob base fee scalar:", blobScalar)
fmt.Printf("# v1 hex encoding : 0x%x\n", encoded[:])
fmt.Println("# uint value for the 'scalar' parameter in SystemConfigProxy.setGasConfig():")
fmt.Println(i)
geoknee marked this conversation as resolved.
Show resolved Hide resolved
o, err := json.Marshal(outputTy{
BaseFee: scalar,
BlobbaseFeeScalar: blobScalar,
ScalarHex: fmt.Sprintf("0x%x", encoded[:]),
Scalar: i,
})
if err != nil {
panic(err)
}
fmt.Println(string(o))
}
40 changes: 40 additions & 0 deletions op-chain-ops/cmd/ecotone-scalar/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"bytes"
"encoding/json"
"os/exec"
"testing"
)

func runMainWithArgs(t *testing.T, args []string) (string, error) {
t.Helper()
cmd := exec.Command("go", "run", "main.go")
cmd.Args = append(cmd.Args, args...)

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

err := cmd.Run()
output := stdout.String() + stderr.String()
return output, err
}

func TestMain_PreEcotoneScalar(t *testing.T) {
output, err := runMainWithArgs(t, []string{"-decode=684000"})
geoknee marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Fatalf("unexpected error: %v, output: %s", err, output)
}

o := new(outputTy)

err = json.Unmarshal([]byte(output), o)
if err != nil {
t.Fatal(err)
}

if o.ScalarHex != "0x00000000000000000000000000000000000000000000000000000000000a6fe0" {
t.Errorf("did not find expected output: %s", output)
}
}