Skip to content
Closed
Show file tree
Hide file tree
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
69 changes: 59 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"context"
"crypto/rand"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand All @@ -25,6 +26,12 @@ type Client interface {
// HttpAddr returns the address where the client is serving JSON-RPC.
HttpAddr() string

// AuthAddr returns the address where the client is serving authenticated JSON-RPC.
AuthAddr() string

// JWTSecret returns the JWT secret for authenticated endpoints.
JWTSecret() []byte

// Close closes the client.
Close() error
}
Expand Down Expand Up @@ -84,13 +91,7 @@ func newGethClient(ctx context.Context, geth string, chaindir string, verbose bo
return nil, err
}

jwt := make([]byte, 32)
rand.Read(jwt)
if err := os.WriteFile(fmt.Sprintf("%s/jwt.hex", tmp), []byte(hexutil.Encode(jwt)), 0600); err != nil {
return nil, err
}

g := &gethClient{path: geth, workdir: tmp, jwt: jwt, fcu: fcuRequest}
g := &gethClient{path: geth, workdir: tmp, jwt: nil, fcu: fcuRequest}
return g, nil
}

Expand All @@ -107,11 +108,12 @@ func (g *gethClient) Start(ctx context.Context, verbose bool) error {
"--gcmode=archive",
"--nodiscover",
"--http",
"--http.api=admin,eth,debug,net",
"--http.api=admin,eth,debug,net,testing",
fmt.Sprintf("--http.addr=%s", HOST),
fmt.Sprintf("--http.port=%s", PORT),
fmt.Sprintf("--authrpc.addr=%s", HOST),
fmt.Sprintf("--authrpc.port=%s", AUTHPORT),
fmt.Sprintf("--authrpc.jwtsecret=%s", fmt.Sprintf("%s/jwt.hex", g.workdir)),
// authrpc needed for engine API (forkchoiceUpdated)
}
)
g.cmd = exec.CommandContext(ctx, g.path, options...)
Expand All @@ -128,6 +130,29 @@ func (g *gethClient) Start(ctx context.Context, verbose bool) error {
// AfterStart is called after the client has been fully started.
// We send a forkchoiceUpdatedV2 request to the engine to trigger a post-merge forkchoice.
func (g *gethClient) AfterStart(ctx context.Context) error {
// Read JWT secret from the file that geth generated
// Wait for geth to create the file (it generates it on startup)
jwtPath := fmt.Sprintf("%s/geth/jwtsecret", g.workdir)
var jwtBytes []byte
var err error
for i := 0; i < 10; i++ {
jwtBytes, err = os.ReadFile(jwtPath)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
if err != nil {
return fmt.Errorf("failed to read JWT secret: %w", err)
}
// JWT secret file contains hex-encoded bytes, one per line
jwtHex := strings.TrimSpace(string(jwtBytes))
jwt, err := hexutil.Decode(jwtHex)
if err != nil {
return fmt.Errorf("failed to decode JWT secret: %w", err)
}
g.jwt = jwt

auth := node.NewJWTAuth(common.BytesToHash(g.jwt))
endpoint := fmt.Sprintf("http://%s:%s", HOST, AUTHPORT)
cl, err := rpc.DialOptions(ctx, endpoint, rpc.WithHTTPAuth(auth))
Expand All @@ -147,6 +172,30 @@ func (g *gethClient) HttpAddr() string {
return fmt.Sprintf("http://%s:%s", HOST, PORT)
}

// AuthAddr returns the address where the client is serving authenticated JSON-RPC.
func (g *gethClient) AuthAddr() string {
return fmt.Sprintf("http://%s:%s", HOST, AUTHPORT)
}

// JWTSecret returns the JWT secret for authenticated endpoints.
func (g *gethClient) JWTSecret() []byte {
// If JWT hasn't been loaded yet, read it from the file
if g.jwt == nil {
jwtPath := fmt.Sprintf("%s/geth/jwtsecret", g.workdir)
jwtBytes, err := os.ReadFile(jwtPath)
if err != nil {
return nil
}
jwtHex := strings.TrimSpace(string(jwtBytes))
jwt, err := hexutil.Decode(jwtHex)
if err != nil {
return nil
}
g.jwt = jwt
}
return g.jwt
}

// Close closes the client.
func (g *gethClient) Close() error {
g.cmd.Process.Kill()
Expand Down
20 changes: 20 additions & 0 deletions ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/rpc"
)

Expand All @@ -35,6 +37,24 @@ func newEthclientHandler(addr string) (*ethclientHandler, error) {
}, nil
}

func newAuthEthclientHandler(addr string, jwtSecret []byte) (*ethclientHandler, error) {
rt := &loggingRoundTrip{
inner: http.DefaultTransport,
}
httpClient := rpc.WithHTTPClient(&http.Client{Transport: rt})
auth := node.NewJWTAuth(common.BytesToHash(jwtSecret))
ctx := context.Background()
rpcClient, err := rpc.DialOptions(ctx, addr, httpClient, rpc.WithHTTPAuth(auth))
if err != nil {
return nil, err
}
return &ethclientHandler{
rpc: rpcClient,
logFile: nil,
transport: rt,
}, nil
}

func (l *ethclientHandler) RotateLog(filename string) error {
if l.logFile != nil {
if err := l.logFile.Close(); err != nil {
Expand Down
1 change: 1 addition & 0 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func runGenerator(ctx context.Context) error {
fmt.Println(" fail.")
fmt.Fprintf(os.Stderr, "failed to fill %s/%s: %s\n", methodTest.Name, test.Name, err)
fails++
handler.Close()
continue
}
fmt.Println(" done.")
Expand Down
19 changes: 11 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ require (
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3
)

replace github.com/ethereum/go-ethereum => github.com/MariusVanDerWijden/go-ethereum v1.8.22-0.20260122150205-ca4e4aee07f2

require (
github.com/DataDog/zstd v1.5.2 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
Expand All @@ -31,7 +33,6 @@ require (
github.com/consensys/gnark-crypto v0.18.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/crate-crypto/go-eth-kzg v1.4.0 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dchest/siphash v1.2.3 // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
Expand All @@ -43,22 +44,22 @@ require (
github.com/emicklei/dot v1.6.2 // indirect
github.com/ethereum/c-kzg-4844/v2 v2.1.5 // indirect
github.com/ethereum/go-bigmodexpfix v0.0.0-20250911101455-f9e208c548ab // indirect
github.com/ethereum/go-verkle v0.2.2 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/ferranbt/fastssz v0.1.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect
github.com/getsentry/sentry-go v0.27.0 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/gofrs/flock v0.12.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v1.0.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/graph-gophers/graphql-go v1.3.0 // indirect
github.com/hashicorp/go-bexpr v0.1.10 // indirect
Expand All @@ -69,7 +70,6 @@ require (
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c // indirect
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jedisct1/go-minisign v0.0.0-20230811132847-661be99b8267 // indirect
github.com/karalabe/hid v1.0.1-0.20240306101548-573246063e52 // indirect
github.com/kilic/bls12-381 v0.1.0 // indirect
github.com/klauspost/compress v1.16.0 // indirect
Expand All @@ -85,7 +85,6 @@ require (
github.com/mitchellh/pointerstructure v1.2.0 // indirect
github.com/naoina/go-stringutil v0.1.0 // indirect
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 // indirect
github.com/pion/dtls/v2 v2.2.7 // indirect
Expand All @@ -102,7 +101,7 @@ require (
github.com/protolambda/zrnt v0.34.1 // indirect
github.com/protolambda/ztyp v0.2.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/rs/cors v1.7.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
Expand All @@ -113,11 +112,15 @@ require (
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/urfave/cli/v2 v2.27.5 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/otel v1.39.0 // indirect
go.opentelemetry.io/otel/metric v1.39.0 // indirect
go.opentelemetry.io/otel/trace v1.39.0 // indirect
go.uber.org/automaxprocs v1.5.2 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/net v0.38.0 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.36.0 // indirect
golang.org/x/sys v0.39.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/time v0.9.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
Expand Down
Loading