Skip to content

Commit

Permalink
simulators/ethereum/sync: send fcu to source node
Browse files Browse the repository at this point in the history
  • Loading branch information
fjl committed Oct 30, 2023
1 parent f51d2f4 commit 51e8728
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions simulators/ethereum/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func runSourceTest(t *hivesim.T, c *hivesim.Client, params hivesim.Params) {
t.Fatal(err)
}

// Send forkchoiceUpdated to ensure the client shares this block with others.
if err := source.sendForkchoiceUpdated(t); err != nil {
t.Fatal(err)
}

// Configure sink to connect to the source node.
enode, err := source.EnodeURL()
if err != nil {
Expand Down Expand Up @@ -141,38 +146,48 @@ type rpcRequest struct {

func (n *node) triggerSync(t *hivesim.T) error {
// Load the engine requests generated by hivechain.
var newpayload, fcu rpcRequest
var newpayload rpcRequest
if err := common.LoadJSON("chain/headnewpayload.json", &newpayload); err != nil {
return err
}
if err := common.LoadJSON("chain/headfcu.json", &fcu); err != nil {
c := n.engineClient()

// deliver newPayload
t.Logf("%s: %s", newpayload.Method, jsonString(newpayload.Params))
var resp engine.PayloadStatusV1
if err := c.Call(&resp, newpayload.Method, conv2any(newpayload.Params)...); err != nil {
return err
}
t.Logf("response: %+v", jsonString(resp))

// engine client setup
token := [32]byte{0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x73, 0x65}
engineURL := fmt.Sprintf("http://%v:8551/", n.IP)
ctx := context.Background()
c, _ := rpc.DialOptions(ctx, engineURL, rpc.WithHTTPAuth(gnode.NewJWTAuth(token)))
return n.sendForkchoiceUpdated(t)
}

// deliver newPayload
t.Logf("%s: %s", newpayload.Method, newpayload.Params)
var npresp engine.PayloadStatusV1
if err := c.Call(&npresp, newpayload.Method, conv2any(newpayload.Params)...); err != nil {
func (n *node) sendForkchoiceUpdated(t *hivesim.T) error {
var fcu rpcRequest
if err := common.LoadJSON("chain/headfcu.json", &fcu); err != nil {
return err
}
t.Logf("response: %+v", npresp)
c := n.engineClient()

// deliver forkchoiceUpdated
t.Logf("%s: %s", fcu.Method, fcu.Params)
t.Logf("%s: %s", fcu.Method, jsonString(fcu.Params))
var fcuresp engine.ForkChoiceResponse
if err := c.Call(&fcuresp, fcu.Method, conv2any(fcu.Params)...); err != nil {
return err
}
t.Logf("response: %+v", fcuresp)
t.Logf("response: %s", jsonString(&fcuresp))
return nil
}

func (n *node) engineClient() *rpc.Client {
// engine client setup
token := [32]byte{0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x73, 0x65}
engineURL := fmt.Sprintf("http://%v:8551/", n.IP)
ctx := context.Background()
c, _ := rpc.DialOptions(ctx, engineURL, rpc.WithHTTPAuth(gnode.NewJWTAuth(token)))
return c
}

// checkHead checks whether the remote chain head matches the given values.
func (n *node) checkHead() error {
var expected types.Header
Expand Down Expand Up @@ -204,3 +219,11 @@ func conv2any[T any](s []T) []any {
}
return cpy
}

func jsonString(v any) string {
enc, err := json.Marshal(v)
if err != nil {
return fmt.Sprint(v)
}
return string(enc)
}

0 comments on commit 51e8728

Please sign in to comment.