Skip to content

Commit

Permalink
WPI, client transition is working with array parameteres.
Browse files Browse the repository at this point in the history
  • Loading branch information
ranlavanet committed Aug 18, 2022
1 parent c8bb938 commit c1917ea
Show file tree
Hide file tree
Showing 26 changed files with 54 additions and 2,156 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ require (
github.com/cosmos/ledger-cosmos-go v0.11.1 // indirect
github.com/cosmos/ledger-go v0.9.2 // indirect
github.com/danieljoos/wincred v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.8.0
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dgraph-io/badger/v2 v2.2007.2 // indirect
Expand Down
2 changes: 1 addition & 1 deletion relayer/chainproxy/jsonRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (nm *JrpcMessage) Send(ctx context.Context) (*pairingtypes.RelayReply, erro
var result json.RawMessage
connectCtx, cancel := context.WithTimeout(ctx, DefaultTimeout)
defer cancel()
err = rpc.CallContext(connectCtx, &result, nm.msg.Method, nm.msg.Params...)
err = rpc.CallContext(connectCtx, &result, nm.msg.Method, nm.msg.Params)
//
// Wrap result back to json
replyMsg := JsonrpcMessage{
Expand Down
62 changes: 51 additions & 11 deletions relayer/chainproxy/rpcclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (c *Client) SupportedModules() (map[string]string, error) {
var result map[string]string
ctx, cancel := context.WithTimeout(context.Background(), subscribeTimeout)
defer cancel()
err := c.CallContext(ctx, &result, "rpc_modules")
err := c.CallContext(ctx, &result, "rpc_modules", make([]interface{}, 0))
return result, err
}

Expand Down Expand Up @@ -279,24 +279,38 @@ func (c *Client) SetHeader(key, value string) {
//
// The result must be a pointer so that package json can unmarshal into it. You
// can also pass nil, in which case the result is ignored.
func (c *Client) Call(result interface{}, method string, args ...interface{}) error {
func (c *Client) Call(result interface{}, method string, args interface{}) error {
ctx := context.Background()
return c.CallContext(ctx, result, method, args...)
return c.CallContext(ctx, result, method, args)
}

// CallContext performs a JSON-RPC call with the given arguments. If the context is
// canceled before the call has successfully returned, CallContext returns immediately.
//
// The result must be a pointer so that package json can unmarshal into it. You
// can also pass nil, in which case the result is ignored.
func (c *Client) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
func (c *Client) CallContext(ctx context.Context, result interface{}, method string, params interface{}) error {
if result != nil && reflect.TypeOf(result).Kind() != reflect.Ptr {
return fmt.Errorf("call result parameter must be pointer or nil interface: %v", result)
}
msg, err := c.newMessage(method, args...)
if err != nil {
return err

var msg *jsonrpcMessage
var err error
switch p := params.(type) {
case []interface{}:
msg, err = c.newMessageArray(method, p...)
if err != nil {
return err
}
case map[string]interface{}:
msg, err = c.newMessageMap(method, p)
if err != nil {
return err
}
default:
return fmt.Errorf("unknown parameters type %v", p)
}

op := &requestOp{ids: []json.RawMessage{msg.ID}, resp: make(chan *jsonrpcMessage, 1)}

if c.isHTTP {
Expand Down Expand Up @@ -352,7 +366,7 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
resp: make(chan *jsonrpcMessage, len(b)),
}
for i, elem := range b {
msg, err := c.newMessage(elem.Method, elem.Args...)
msg, err := c.newMessageArray(elem.Method, elem.Args...)
if err != nil {
return err
}
Expand Down Expand Up @@ -395,7 +409,7 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
// Notify sends a notification, i.e. a method call that doesn't expect a response.
func (c *Client) Notify(ctx context.Context, method string, args ...interface{}) error {
op := new(requestOp)
msg, err := c.newMessage(method, args...)
msg, err := c.newMessageArray(method, args...)
if err != nil {
return err
}
Expand Down Expand Up @@ -443,7 +457,7 @@ func (c *Client) Subscribe(ctx context.Context, namespace string, channel interf
return nil, ErrNotificationsUnsupported
}

msg, err := c.newMessage(namespace+subscribeMethodSuffix, args...)
msg, err := c.newMessageArray(namespace+subscribeMethodSuffix, args...)
if err != nil {
return nil, err
}
Expand All @@ -464,7 +478,7 @@ func (c *Client) Subscribe(ctx context.Context, namespace string, channel interf
return op.sub, nil
}

func (c *Client) newMessage(method string, paramsIn ...interface{}) (*jsonrpcMessage, error) {
func (c *Client) newMessageArray(method string, paramsIn ...interface{}) (*jsonrpcMessage, error) {
msg := &jsonrpcMessage{Version: vsn, ID: c.nextID(), Method: method}
if paramsIn != nil { // prevent sending "params":null
var err error
Expand All @@ -475,6 +489,32 @@ func (c *Client) newMessage(method string, paramsIn ...interface{}) (*jsonrpcMes
return msg, nil
}

func (c *Client) newMessageMap(method string, paramsIn map[string]interface{}) (*jsonrpcMessage, error) {
msg := &jsonrpcMessage{Version: vsn, ID: c.nextID(), Method: method}
if paramsIn != nil { // prevent sending "params":null
var err error
if msg.Params, err = json.Marshal(paramsIn); err != nil {
return nil, err
}

// test this too:
//
// var paramsMap = make(map[string]json.RawMessage, len(paramsIn))
// for name, value := range paramsIn {
// valueJSON, err := json.Marshal(value)
// if err != nil {
// return nil, err
// }
// paramsMap[name] = valueJSON
// }
// if msg.Params, err = json.Marshal(paramsMap); err != nil {
// return nil, err
// }

}
return msg, nil
}

// send registers op with the dispatch loop, then sends msg on the connection.
// if sending fails, op is deregistered.
func (c *Client) send(ctx context.Context, op *requestOp, msg interface{}) error {
Expand Down
89 changes: 0 additions & 89 deletions relayer/chainproxy/rpcclient/client_example_test.go

This file was deleted.

Loading

0 comments on commit c1917ea

Please sign in to comment.