Skip to content

Commit

Permalink
Merge pull request #30 from fjl/hivechain-forkenv
Browse files Browse the repository at this point in the history
add forkchoiceUpdated and hive forkenv to test output
  • Loading branch information
lightclient authored Jan 30, 2024
2 parents 8309117 + 2bb7d53 commit 2693616
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 37 deletions.
13 changes: 13 additions & 0 deletions chain/headfcu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"jsonrpc": "2.0",
"id": "fcu20",
"method": "engine_forkchoiceUpdatedV3",
"params": [
{
"headBlockHash": "0x8f64c4436f7213cfdf02cfb9f45d012f1774dfb329b8803de5e7479b11586902",
"safeBlockHash": "0x8f64c4436f7213cfdf02cfb9f45d012f1774dfb329b8803de5e7479b11586902",
"finalizedBlockHash": "0x8f64c4436f7213cfdf02cfb9f45d012f1774dfb329b8803de5e7479b11586902"
},
null
]
}
49 changes: 22 additions & 27 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -37,13 +35,27 @@ type gethClient struct {
path string
workdir string
jwt []byte
fcu rpcRequest
}

type rpcRequest struct {
Method string
Params []any
}

// newGethClient instantiates a new GethClient.
//
// 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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand Down
23 changes: 14 additions & 9 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +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
files := []string{
"genesis.json",
"chain.rlp",
"forkenv.json",
"headfcu.json",
}
fmt.Println("copying genesis.json")
err = cp.CopyFileOverwrite(filepath.Join(outDir, "genesis.json"), filepath.Join(chainDir, "genesis.json"))
err := os.MkdirAll(outDir, 0755)
if err != nil {
return err
}
fmt.Println("copying chain.rlp")
err = cp.CopyFileOverwrite(filepath.Join(outDir, "chain.rlp"), filepath.Join(chainDir, "chain.rlp"))
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
}
Expand Down
2 changes: 1 addition & 1 deletion mkchain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2693616

Please sign in to comment.