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

add forkchoiceUpdated and hive forkenv to test output #30

Merged
merged 3 commits into from
Jan 30, 2024
Merged
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
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
Loading