Skip to content

Commit

Permalink
Merge pull request lightclient#23 from jsvisa/post-merge-fork-chain
Browse files Browse the repository at this point in the history
client: run geth in post-merge phase
  • Loading branch information
lightclient committed Oct 2, 2023
2 parents 00582f0 + 0d81139 commit 8417a60
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 65 deletions.
3 changes: 2 additions & 1 deletion blockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/trie"
)

// genSimpleChain generates a short chain with a few basic transactions.
Expand Down Expand Up @@ -71,7 +72,7 @@ func genSimpleChain(engine consensus.Engine) (*core.Genesis, []*types.Block, *ty
Code: common.FromHex("0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063a9059cbb14610030575b600080fd5b61004a6004803603810190610045919061016a565b610060565b60405161005791906101c5565b60405180910390f35b60008273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040516100bf91906101ef565b60405180910390a36001905092915050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000610101826100d6565b9050919050565b610111816100f6565b811461011c57600080fd5b50565b60008135905061012e81610108565b92915050565b6000819050919050565b61014781610134565b811461015257600080fd5b50565b6000813590506101648161013e565b92915050565b60008060408385031215610181576101806100d1565b5b600061018f8582860161011f565b92505060206101a085828601610155565b9150509250929050565b60008115159050919050565b6101bf816101aa565b82525050565b60006020820190506101da60008301846101b6565b92915050565b6101e981610134565b82525050565b600060208201905061020460008301846101e0565b9291505056fea2646970667358221220b469033f4b77b9565ee84e0a2f04d496b18160d26034d54f9487e57788fd36d564736f6c63430008120033"),
}

genesis := gspec.MustCommit(gendb)
genesis := gspec.MustCommit(gendb, trie.NewDatabase(gendb, trie.HashDefaults))

chain, _ := core.GenerateChain(gspec.Config, genesis, engine, gendb, 10, func(i int, gen *core.BlockGen) {
var (
Expand Down
53 changes: 52 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@ package main

import (
"context"
"crypto/rand"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"

"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
)

// Client is an interface for generically interacting with Ethereum clients.
type Client interface {
// Start starts client, but does not wait for the command to exit.
Start(ctx context.Context, verbose bool) error

// AfterStart is called after the client has been fully started.
AfterStart(ctx context.Context) error

// HttpAddr returns the address where the client is servering its
// JSON-RPC.
HttpAddr() string
Expand All @@ -33,6 +43,7 @@ type gethClient struct {
workdir string
blocks []*types.Block
genesis *core.Genesis
jwt []byte
}

// newGethClient instantiates a new GethClient.
Expand Down Expand Up @@ -72,12 +83,19 @@ func newGethClient(ctx context.Context, path string, genesis *core.Genesis, bloc
return nil, err
}

return &gethClient{path: path, genesis: genesis, blocks: blocks, workdir: tmp}, nil
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
}

return &gethClient{path: path, genesis: genesis, blocks: blocks, workdir: tmp, jwt: jwt}, nil
}

// Start starts geth, but does not wait for the command to exit.
func (g *gethClient) Start(ctx context.Context, verbose bool) error {
fmt.Println("starting client")

var (
args = ctx.Value(ARGS).(*Args)
options = []string{
Expand All @@ -90,6 +108,8 @@ func (g *gethClient) Start(ctx context.Context, verbose bool) error {
"--http.api=admin,eth,debug",
fmt.Sprintf("--http.addr=%s", HOST),
fmt.Sprintf("--http.port=%s", PORT),
fmt.Sprintf("--authrpc.port=%s", AUTHPORT),
fmt.Sprintf("--authrpc.jwtsecret=%s", fmt.Sprintf("%s/jwt.hex", g.workdir)),
}
)
g.cmd = exec.CommandContext(
Expand All @@ -107,6 +127,37 @@ func (g *gethClient) Start(ctx context.Context, verbose bool) error {
return nil
}

// 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 {
var (
auth = node.NewJWTAuth(common.BytesToHash(g.jwt))
endpoint = fmt.Sprintf("http://%s:%s", HOST, AUTHPORT)
)
cl, err := rpc.DialOptions(ctx, endpoint, rpc.WithHTTPAuth(auth))
if err != nil {
return err
}
defer cl.Close()

geth := ethclient.NewClient(cl)
block, err := geth.BlockByNumber(ctx, nil)
if err != nil {
return err
}

var resp engine.ForkChoiceResponse
err = cl.CallContext(ctx, &resp, "engine_forkchoiceUpdatedV2", &engine.ForkchoiceStateV1{
HeadBlockHash: block.Hash(),
SafeBlockHash: block.Hash(),
FinalizedBlockHash: block.Hash(),
}, nil)
if status := resp.PayloadStatus.Status; status != engine.VALID {
fmt.Printf("initializing forkchoice updated failed: status %s, err %v\n", status, err)
}
return err
}

// HttpAddr returns the address where the client is servering its JSON-RPC.
func (g *gethClient) HttpAddr() string {
return fmt.Sprintf("http://%s:%s", HOST, PORT)
Expand Down
8 changes: 7 additions & 1 deletion generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/trie"
"github.com/lightclient/rpctestgen/testgen"
)

Expand All @@ -37,6 +38,11 @@ func runGenerator(ctx context.Context) error {
}
defer client.Close()

err = client.AfterStart(ctx)
if err != nil {
return err
}

// Generate test fixtures for all methods. Store them in the format:
// outputDir/methodName/testName.io
fmt.Println("filling tests...")
Expand Down Expand Up @@ -128,7 +134,7 @@ func initChain(ctx context.Context, args *Args) (*chainData, error) {

// Create BlockChain to verify client responses against.
db := rawdb.NewMemoryDatabase()
chain.gspec.MustCommit(db)
chain.gspec.MustCommit(db, trie.NewDatabase(db, trie.HashDefaults))

var err error
chain.bc, err = core.NewBlockChain(db, nil, chain.gspec, nil, beacon.New(ethash.NewFaker()), vm.Config{}, nil, nil)
Expand Down
54 changes: 38 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,81 @@ go 1.18

require (
github.com/alexflint/go-arg v1.4.3
github.com/ethereum/go-ethereum v1.11.4
github.com/ethereum/go-ethereum v1.13.1
github.com/open-rpc/meta-schema v0.0.0-20210416041958-626a15d0a618
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0
)

require (
github.com/DataDog/zstd v1.5.2 // indirect
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/VictoriaMetrics/fastcache v1.6.0 // indirect
github.com/alexflint/go-scalar v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.5.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/errors v1.9.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811 // indirect
github.com/cockroachdb/pebble v0.0.0-20230906160148-46873a6a7a06 // indirect
github.com/cockroachdb/redact v1.1.3 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.10.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/crate-crypto/go-kzg-4844 v0.3.0 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/edsrzf/mmap-go v1.0.0 // indirect
github.com/ethereum/c-kzg-4844 v0.3.1 // indirect
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 // indirect
github.com/getsentry/sentry-go v0.18.0 // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.3.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/go-bexpr v0.1.10 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.0 // indirect
github.com/huin/goupnp v1.0.3 // indirect
github.com/holiman/uint256 v1.2.3 // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/klauspost/compress v1.15.15 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/mitchellh/pointerstructure v1.2.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.39.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // 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
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/exp v0.0.0-20230206171751-46f607a40771 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/urfave/cli/v2 v2.25.7 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230810033253-352e893a4cad // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/tools v0.9.1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
Loading

0 comments on commit 8417a60

Please sign in to comment.