Skip to content

Commit

Permalink
Merge pull request ethereum#214 from OffchainLabs/client-support
Browse files Browse the repository at this point in the history
RPC-Client improvements
  • Loading branch information
PlasmaPower authored May 25, 2023
2 parents ffad045 + a23dc84 commit 87c313c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 56 deletions.
4 changes: 2 additions & 2 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (

// Client defines typed wrappers for the Ethereum RPC API.
type Client struct {
c *rpc.Client
c rpc.ClientInterface
}

// Dial connects a client to the given URL.
Expand All @@ -50,7 +50,7 @@ func DialContext(ctx context.Context, rawurl string) (*Client, error) {
}

// NewClient creates a client that uses the given RPC client.
func NewClient(c *rpc.Client) *Client {
func NewClient(c rpc.ClientInterface) *Client {
return &Client{c}
}

Expand Down
8 changes: 8 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,14 @@ func (n *Node) WSAuthEndpoint() string {
return "ws://" + n.wsAuth.listenAddr() + n.wsAuth.wsConfig.prefix
}

// JWTPath returns the path for JWT secret
func (n *Node) JWTPath() string {
if n.config.JWTSecret == "" {
return n.ResolvePath(datadirJWTKey)
}
return n.config.JWTSecret
}

// EventMux retrieves the event multiplexer used by all the network services in
// the current protocol stack.
func (n *Node) EventMux() *event.TypeMux {
Expand Down
28 changes: 8 additions & 20 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ const (
maxClientSubscriptionBuffer = 20000
)

type ClientInterface interface {
CallContext(ctx_in context.Context, result interface{}, method string, args ...interface{}) error
EthSubscribe(ctx context.Context, channel interface{}, args ...interface{}) (*ClientSubscription, error)
BatchCallContext(ctx context.Context, b []BatchElem) error
Close()
}

// BatchElem is an element in a batch request.
type BatchElem struct {
Method string
Expand All @@ -75,8 +82,6 @@ type BatchElem struct {

// Client represents a connection to an RPC server.
type Client struct {
requestHook RequestHook

idgen func() ID // for subscriptions
isHTTP bool // connection type: http, ws or ipc
services *serviceRegistry
Expand Down Expand Up @@ -329,34 +334,28 @@ func (c *Client) CallContext(ctx context.Context, result interface{}, method str
}
op := &requestOp{ids: []json.RawMessage{msg.ID}, resp: make(chan *jsonrpcMessage, 1)}

resultHook := c.onRequest(msg)
if c.isHTTP {
err = c.sendHTTP(ctx, op, msg)
} else {
err = c.send(ctx, op, msg)
}
if err != nil {
resultHook.OnResult(nil, err)
return err
}

// dispatch has accepted the request and will close the channel when it quits.
switch resp, err := op.wait(ctx, c); {
case err != nil:
resultHook.OnResult(resp, err)
return err
case resp.Error != nil:
resultHook.OnResult(resp, resp.Error)
return resp.Error
case len(resp.Result) == 0:
resultHook.OnResult(resp, ErrNoResult)
return ErrNoResult
default:
if result == nil {
return nil
}
err := json.Unmarshal(resp.Result, result)
resultHook.OnResult(resp, err)
return err
}
}
Expand Down Expand Up @@ -401,12 +400,6 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
byID[string(msg.ID)] = i
}

resultHooks := make([]ResultHook, len(msgs))
responsesForHooks := make([]interface{}, len(msgs))
for i, msg := range msgs {
resultHooks[i] = c.onRequest(msg)
}

var err error
if c.isHTTP {
err = c.sendBatchHTTP(ctx, op, msgs)
Expand All @@ -424,9 +417,7 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
// Find the element corresponding to this response.
// The element is guaranteed to be present because dispatch
// only sends valid IDs to our channel.
idx := byID[string(resp.ID)]
responsesForHooks[idx] = resp
elem := &b[idx]
elem := &b[byID[string(resp.ID)]]
if resp.Error != nil {
elem.Error = resp.Error
continue
Expand All @@ -437,9 +428,6 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
}
elem.Error = json.Unmarshal(resp.Result, elem.Result)
}
for i, hook := range resultHooks {
hook.OnResult(responsesForHooks[i], err)
}
return err
}

Expand Down
34 changes: 0 additions & 34 deletions rpc/client_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,3 @@ func DialTransport(ctx context.Context, rawUrl string, transport *http.Transport
}
return rpcClient, nil
}

func DialContextWithRequestHook(ctx context.Context, url string, hook RequestHook) (*Client, error) {
client, err := DialContext(ctx, url)
if err != nil {
return nil, err
}
client.requestHook = hook
return client, nil
}

type RequestHook interface {
OnRequest(request interface{}) ResultHook
}

type ResultHook interface {
OnResult(response interface{}, err error)
}

type noopResultHook struct{}

func (h noopResultHook) OnResult(interface{}, error) {
}

func (c *Client) onRequest(request interface{}) ResultHook {
hooks := c.requestHook
var respHooks ResultHook
if hooks != nil {
respHooks = hooks.OnRequest(request)
}
if respHooks == nil {
respHooks = noopResultHook{}
}
return respHooks
}

0 comments on commit 87c313c

Please sign in to comment.