From b49d655fb91b070051457b9eb984f05194076ee5 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Mon, 29 Jan 2024 19:22:13 +0100 Subject: [PATCH 1/3] copy forkenv.json to output directory --- generate.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/generate.go b/generate.go index 14fdd70..50eae19 100644 --- a/generate.go +++ b/generate.go @@ -150,6 +150,11 @@ func copyChainFiles(chainDir, outDir string) error { if err != nil { return err } + fmt.Println("copying forkenv.json") + err = cp.CopyFileOverwrite(filepath.Join(outDir, "forkenv.json"), filepath.Join(chainDir, "forkenv.json")) + if err != nil { + return err + } return nil } From 2c309dab4dbbf4d26af39a242e2cb1fd6e270456 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 30 Jan 2024 12:44:56 +0100 Subject: [PATCH 2/3] use hivechain's forkchoiceUpdated --- chain/headfcu.json | 13 ++++++++++++ client.go | 49 +++++++++++++++++++++------------------------- mkchain.sh | 2 +- 3 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 chain/headfcu.json diff --git a/chain/headfcu.json b/chain/headfcu.json new file mode 100644 index 0000000..00ac59e --- /dev/null +++ b/chain/headfcu.json @@ -0,0 +1,13 @@ +{ + "jsonrpc": "2.0", + "id": "fcu20", + "method": "engine_forkchoiceUpdatedV3", + "params": [ + { + "headBlockHash": "0x8f64c4436f7213cfdf02cfb9f45d012f1774dfb329b8803de5e7479b11586902", + "safeBlockHash": "0x8f64c4436f7213cfdf02cfb9f45d012f1774dfb329b8803de5e7479b11586902", + "finalizedBlockHash": "0x8f64c4436f7213cfdf02cfb9f45d012f1774dfb329b8803de5e7479b11586902" + }, + null + ] +} \ No newline at end of file diff --git a/client.go b/client.go index e32e18b..4dc81a2 100644 --- a/client.go +++ b/client.go @@ -8,10 +8,8 @@ import ( "os/exec" "path/filepath" - "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/ethclient" "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/rpc" ) @@ -37,6 +35,12 @@ type gethClient struct { path string workdir string jwt []byte + fcu rpcRequest +} + +type rpcRequest struct { + Method string + Params []any } // newGethClient instantiates a new GethClient. @@ -44,6 +48,14 @@ type gethClient struct { // The client's data directory is set to a temporary location and it // initializes with the genesis and the provided blocks. func newGethClient(ctx context.Context, geth string, chaindir string, verbose bool) (*gethClient, error) { + // Load ForkchoiceUpdated from test chain. + var fcuRequest rpcRequest + fcuFile := filepath.Join(chaindir, "headfcu.json") + if err := common.LoadJSON(fcuFile, &fcuRequest); err != nil { + return nil, err + } + + // Initialize the data directory. tmp, err := os.MkdirTemp("", "rpctestgen-*") if err != nil { return nil, err @@ -76,7 +88,8 @@ func newGethClient(ctx context.Context, geth string, chaindir string, verbose bo return nil, err } - return &gethClient{path: geth, workdir: tmp, jwt: jwt}, nil + g := &gethClient{path: geth, workdir: tmp, jwt: jwt, fcu: fcuRequest} + return g, nil } // Start starts geth, but does not wait for the command to exit. @@ -99,11 +112,7 @@ func (g *gethClient) Start(ctx context.Context, verbose bool) error { fmt.Sprintf("--authrpc.jwtsecret=%s", fmt.Sprintf("%s/jwt.hex", g.workdir)), } ) - g.cmd = exec.CommandContext( - ctx, - g.path, - options..., - ) + g.cmd = exec.CommandContext(ctx, g.path, options...) if verbose { g.cmd.Stdout = os.Stdout g.cmd.Stderr = os.Stderr @@ -117,32 +126,18 @@ 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 { - var ( - auth = node.NewJWTAuth(common.BytesToHash(g.jwt)) - endpoint = fmt.Sprintf("http://%s:%s", HOST, AUTHPORT) - ) + 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) + err = cl.CallContext(ctx, nil, g.fcu.Method, g.fcu.Params...) if err != nil { - return err + return fmt.Errorf("geth rejected forkchoiceUpdated: %v", 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 + return nil } // HttpAddr returns the address where the client is servering its JSON-RPC. diff --git a/mkchain.sh b/mkchain.sh index c8038e4..f05fc2b 100644 --- a/mkchain.sh +++ b/mkchain.sh @@ -6,4 +6,4 @@ mkdir -p chain/ -tx-interval 1 \ -fork-interval 0 \ -lastfork cancun \ - -outputs genesis,chain,forkenv,headstate,txinfo,accounts + -outputs genesis,chain,forkenv,headstate,txinfo,accounts,headfcu From 2bb7d5353a33ff8d784c92216b9fd2394c9395d6 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 30 Jan 2024 12:48:53 +0100 Subject: [PATCH 3/3] copy headfcu.json to output directory --- generate.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/generate.go b/generate.go index 50eae19..9808c61 100644 --- a/generate.go +++ b/generate.go @@ -135,25 +135,25 @@ func mkdir(path string) error { return nil } +// copyChainFiles copies the chain into the tests output directory. +// Here we copy all files required to run the tests. func copyChainFiles(chainDir, outDir string) error { - err := os.MkdirAll(outDir, 0755) - if err != nil { - return err - } - fmt.Println("copying genesis.json") - err = cp.CopyFileOverwrite(filepath.Join(outDir, "genesis.json"), filepath.Join(chainDir, "genesis.json")) - if err != nil { - return err + files := []string{ + "genesis.json", + "chain.rlp", + "forkenv.json", + "headfcu.json", } - fmt.Println("copying chain.rlp") - err = cp.CopyFileOverwrite(filepath.Join(outDir, "chain.rlp"), filepath.Join(chainDir, "chain.rlp")) + err := os.MkdirAll(outDir, 0755) if err != nil { return err } - fmt.Println("copying forkenv.json") - err = cp.CopyFileOverwrite(filepath.Join(outDir, "forkenv.json"), filepath.Join(chainDir, "forkenv.json")) - if err != nil { - return err + for _, f := range files { + fmt.Println("copying", f) + err = cp.CopyFileOverwrite(filepath.Join(outDir, f), filepath.Join(chainDir, f)) + if err != nil { + return err + } } return nil }