|
|
@ -32,9 +32,9 @@ import ( |
|
|
|
const ( |
|
|
|
const ( |
|
|
|
jsonrpcVersion = "2.0" |
|
|
|
jsonrpcVersion = "2.0" |
|
|
|
serviceMethodSeparator = "_" |
|
|
|
serviceMethodSeparator = "_" |
|
|
|
subscribeMethod = "eth_subscribe" |
|
|
|
subscribeMethodSuffix = "_subscribe" |
|
|
|
unsubscribeMethod = "eth_unsubscribe" |
|
|
|
unsubscribeMethodSuffix = "_unsubscribe" |
|
|
|
notificationMethod = "eth_subscription" |
|
|
|
notificationMethodSuffix = "_subscription" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type jsonRequest struct { |
|
|
|
type jsonRequest struct { |
|
|
@ -164,7 +164,7 @@ func parseRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// subscribe are special, they will always use `subscribeMethod` as first param in the payload
|
|
|
|
// subscribe are special, they will always use `subscribeMethod` as first param in the payload
|
|
|
|
if in.Method == subscribeMethod { |
|
|
|
if strings.HasSuffix(in.Method, subscribeMethodSuffix) { |
|
|
|
reqs := []rpcRequest{{id: &in.Id, isPubSub: true}} |
|
|
|
reqs := []rpcRequest{{id: &in.Id, isPubSub: true}} |
|
|
|
if len(in.Payload) > 0 { |
|
|
|
if len(in.Payload) > 0 { |
|
|
|
// first param must be subscription name
|
|
|
|
// first param must be subscription name
|
|
|
@ -174,17 +174,16 @@ func parseRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error) { |
|
|
|
return nil, false, &invalidRequestError{"Unable to parse subscription request"} |
|
|
|
return nil, false, &invalidRequestError{"Unable to parse subscription request"} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// all subscriptions are made on the eth service
|
|
|
|
reqs[0].service, reqs[0].method = strings.TrimSuffix(in.Method, subscribeMethodSuffix), subscribeMethod[0] |
|
|
|
reqs[0].service, reqs[0].method = "eth", subscribeMethod[0] |
|
|
|
|
|
|
|
reqs[0].params = in.Payload |
|
|
|
reqs[0].params = in.Payload |
|
|
|
return reqs, false, nil |
|
|
|
return reqs, false, nil |
|
|
|
} |
|
|
|
} |
|
|
|
return nil, false, &invalidRequestError{"Unable to parse subscription request"} |
|
|
|
return nil, false, &invalidRequestError{"Unable to parse subscription request"} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if in.Method == unsubscribeMethod { |
|
|
|
if strings.HasSuffix(in.Method, unsubscribeMethodSuffix) { |
|
|
|
return []rpcRequest{{id: &in.Id, isPubSub: true, |
|
|
|
return []rpcRequest{{id: &in.Id, isPubSub: true, |
|
|
|
method: unsubscribeMethod, params: in.Payload}}, false, nil |
|
|
|
method: in.Method, params: in.Payload}}, false, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
elems := strings.Split(in.Method, serviceMethodSeparator) |
|
|
|
elems := strings.Split(in.Method, serviceMethodSeparator) |
|
|
@ -216,8 +215,8 @@ func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error) |
|
|
|
|
|
|
|
|
|
|
|
id := &in[i].Id |
|
|
|
id := &in[i].Id |
|
|
|
|
|
|
|
|
|
|
|
// subscribe are special, they will always use `subscribeMethod` as first param in the payload
|
|
|
|
// subscribe are special, they will always use `subscriptionMethod` as first param in the payload
|
|
|
|
if r.Method == subscribeMethod { |
|
|
|
if strings.HasSuffix(r.Method, subscribeMethodSuffix) { |
|
|
|
requests[i] = rpcRequest{id: id, isPubSub: true} |
|
|
|
requests[i] = rpcRequest{id: id, isPubSub: true} |
|
|
|
if len(r.Payload) > 0 { |
|
|
|
if len(r.Payload) > 0 { |
|
|
|
// first param must be subscription name
|
|
|
|
// first param must be subscription name
|
|
|
@ -227,8 +226,7 @@ func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error) |
|
|
|
return nil, false, &invalidRequestError{"Unable to parse subscription request"} |
|
|
|
return nil, false, &invalidRequestError{"Unable to parse subscription request"} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// all subscriptions are made on the eth service
|
|
|
|
requests[i].service, requests[i].method = strings.TrimSuffix(r.Method, subscribeMethodSuffix), subscribeMethod[0] |
|
|
|
requests[i].service, requests[i].method = "eth", subscribeMethod[0] |
|
|
|
|
|
|
|
requests[i].params = r.Payload |
|
|
|
requests[i].params = r.Payload |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
@ -236,8 +234,8 @@ func parseBatchRequest(incomingMsg json.RawMessage) ([]rpcRequest, bool, Error) |
|
|
|
return nil, true, &invalidRequestError{"Unable to parse (un)subscribe request arguments"} |
|
|
|
return nil, true, &invalidRequestError{"Unable to parse (un)subscribe request arguments"} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if r.Method == unsubscribeMethod { |
|
|
|
if strings.HasSuffix(r.Method, unsubscribeMethodSuffix) { |
|
|
|
requests[i] = rpcRequest{id: id, isPubSub: true, method: unsubscribeMethod, params: r.Payload} |
|
|
|
requests[i] = rpcRequest{id: id, isPubSub: true, method: r.Method, params: r.Payload} |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -325,13 +323,13 @@ func (c *jsonCodec) CreateErrorResponseWithInfo(id interface{}, err Error, info |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// CreateNotification will create a JSON-RPC notification with the given subscription id and event as params.
|
|
|
|
// CreateNotification will create a JSON-RPC notification with the given subscription id and event as params.
|
|
|
|
func (c *jsonCodec) CreateNotification(subid string, event interface{}) interface{} { |
|
|
|
func (c *jsonCodec) CreateNotification(subid, namespace string, event interface{}) interface{} { |
|
|
|
if isHexNum(reflect.TypeOf(event)) { |
|
|
|
if isHexNum(reflect.TypeOf(event)) { |
|
|
|
return &jsonNotification{Version: jsonrpcVersion, Method: notificationMethod, |
|
|
|
return &jsonNotification{Version: jsonrpcVersion, Method: namespace + notificationMethodSuffix, |
|
|
|
Params: jsonSubscription{Subscription: subid, Result: fmt.Sprintf(`%#x`, event)}} |
|
|
|
Params: jsonSubscription{Subscription: subid, Result: fmt.Sprintf(`%#x`, event)}} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return &jsonNotification{Version: jsonrpcVersion, Method: notificationMethod, |
|
|
|
return &jsonNotification{Version: jsonrpcVersion, Method: namespace + notificationMethodSuffix, |
|
|
|
Params: jsonSubscription{Subscription: subid, Result: event}} |
|
|
|
Params: jsonSubscription{Subscription: subid, Result: event}} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|