2021-04-18 16:39:15 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-09-05 11:03:06 +00:00
|
|
|
"context"
|
2021-04-18 16:39:15 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-10-10 10:38:33 +00:00
|
|
|
"io"
|
2021-04-18 16:39:15 +00:00
|
|
|
"math/big"
|
2021-09-28 11:33:54 +00:00
|
|
|
"net"
|
2021-04-18 16:39:15 +00:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
2022-01-13 13:12:57 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2021-04-18 16:39:15 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2021-07-12 18:39:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-11-14 13:34:10 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2021-07-12 18:39:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth/filters"
|
2021-10-06 11:22:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2021-07-12 18:39:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2021-04-18 16:39:15 +00:00
|
|
|
rpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
|
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
|
2022-09-07 06:36:11 +00:00
|
|
|
"github.com/cerc-io/laconicd/rpc/ethereum/pubsub"
|
2022-10-10 10:38:33 +00:00
|
|
|
rpcfilters "github.com/cerc-io/laconicd/rpc/namespaces/ethereum/eth/filters"
|
|
|
|
"github.com/cerc-io/laconicd/rpc/types"
|
2022-09-07 06:36:11 +00:00
|
|
|
"github.com/cerc-io/laconicd/server/config"
|
|
|
|
evmtypes "github.com/cerc-io/laconicd/x/evm/types"
|
2021-04-18 16:39:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WebsocketsServer interface {
|
|
|
|
Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
type SubscriptionResponseJSON struct {
|
|
|
|
Jsonrpc string `json:"jsonrpc"`
|
|
|
|
Result interface{} `json:"result"`
|
|
|
|
ID float64 `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SubscriptionNotification struct {
|
|
|
|
Jsonrpc string `json:"jsonrpc"`
|
|
|
|
Method string `json:"method"`
|
|
|
|
Params *SubscriptionResult `json:"params"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SubscriptionResult struct {
|
|
|
|
Subscription rpc.ID `json:"subscription"`
|
|
|
|
Result interface{} `json:"result"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorResponseJSON struct {
|
|
|
|
Jsonrpc string `json:"jsonrpc"`
|
|
|
|
Error *ErrorMessageJSON `json:"error"`
|
|
|
|
ID *big.Int `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorMessageJSON struct {
|
|
|
|
Code *big.Int `json:"code"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type websocketsServer struct {
|
2021-09-28 11:33:54 +00:00
|
|
|
rpcAddr string // listen address of rest-server
|
|
|
|
wsAddr string // listen address of ws server
|
|
|
|
certFile string
|
|
|
|
keyFile string
|
|
|
|
api *pubSubAPI
|
|
|
|
logger log.Logger
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func NewWebsocketsServer(clientCtx client.Context, logger log.Logger, tmWSClient *rpcclient.WSClient, cfg *config.Config) WebsocketsServer {
|
2021-09-05 11:03:06 +00:00
|
|
|
logger = logger.With("api", "websocket-server")
|
2021-09-28 11:33:54 +00:00
|
|
|
_, port, _ := net.SplitHostPort(cfg.JSONRPC.Address)
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
return &websocketsServer{
|
2021-09-28 11:33:54 +00:00
|
|
|
rpcAddr: "localhost:" + port, // FIXME: this shouldn't be hardcoded to localhost
|
|
|
|
wsAddr: cfg.JSONRPC.WsAddress,
|
|
|
|
certFile: cfg.TLS.CertificatePath,
|
|
|
|
keyFile: cfg.TLS.KeyPath,
|
2022-01-13 13:12:57 +00:00
|
|
|
api: newPubSubAPI(clientCtx, logger, tmWSClient),
|
2021-09-28 11:33:54 +00:00
|
|
|
logger: logger,
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *websocketsServer) Start() {
|
|
|
|
ws := mux.NewRouter()
|
|
|
|
ws.Handle("/", s)
|
|
|
|
|
|
|
|
go func() {
|
2021-09-28 11:33:54 +00:00
|
|
|
var err error
|
|
|
|
if s.certFile == "" || s.keyFile == "" {
|
2024-04-03 12:14:49 +00:00
|
|
|
err = http.ListenAndServe(s.wsAddr, ws) /* #nosec G114 -- http functions have no support for timeouts */
|
2021-09-28 11:33:54 +00:00
|
|
|
} else {
|
2024-04-03 12:14:49 +00:00
|
|
|
err = http.ListenAndServeTLS(s.wsAddr, s.certFile, s.keyFile, ws) // #nosec G114
|
2021-09-28 11:33:54 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == http.ErrServerClosed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-12 18:39:35 +00:00
|
|
|
s.logger.Error("failed to start HTTP server for WS", "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *websocketsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2021-09-05 11:03:06 +00:00
|
|
|
upgrader := websocket.Upgrader{
|
2021-04-18 16:39:15 +00:00
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
s.logger.Debug("websocket upgrade failed", "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.readLoop(&wsConn{
|
|
|
|
mux: new(sync.Mutex),
|
|
|
|
conn: conn,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *websocketsServer) sendErrResponse(wsConn *wsConn, msg string) {
|
|
|
|
res := &ErrorResponseJSON{
|
|
|
|
Jsonrpc: "2.0",
|
|
|
|
Error: &ErrorMessageJSON{
|
|
|
|
Code: big.NewInt(-32600),
|
|
|
|
Message: msg,
|
|
|
|
},
|
|
|
|
ID: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = wsConn.WriteJSON(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
type wsConn struct {
|
|
|
|
conn *websocket.Conn
|
|
|
|
mux *sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wsConn) WriteJSON(v interface{}) error {
|
|
|
|
w.mux.Lock()
|
|
|
|
defer w.mux.Unlock()
|
|
|
|
|
|
|
|
return w.conn.WriteJSON(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wsConn) Close() error {
|
|
|
|
w.mux.Lock()
|
|
|
|
defer w.mux.Unlock()
|
|
|
|
|
|
|
|
return w.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wsConn) ReadMessage() (messageType int, p []byte, err error) {
|
|
|
|
// not protected by write mutex
|
|
|
|
|
|
|
|
return w.conn.ReadMessage()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *websocketsServer) readLoop(wsConn *wsConn) {
|
2022-02-25 19:08:30 +00:00
|
|
|
// subscriptions of current connection
|
|
|
|
subscriptions := make(map[rpc.ID]pubsub.UnsubscribeFunc)
|
|
|
|
defer func() {
|
|
|
|
// cancel all subscriptions when connection closed
|
2022-10-18 06:44:29 +00:00
|
|
|
// #nosec G705
|
2022-02-25 19:08:30 +00:00
|
|
|
for _, unsubFn := range subscriptions {
|
|
|
|
unsubFn()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
for {
|
|
|
|
_, mb, err := wsConn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
_ = wsConn.Close()
|
2022-10-10 10:38:33 +00:00
|
|
|
s.logger.Error("read message error, breaking read loop", "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
if isBatch(mb) {
|
|
|
|
if err := s.tcpGetAndSendResponse(wsConn, mb); err != nil {
|
|
|
|
s.sendErrResponse(wsConn, err.Error())
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
var msg map[string]interface{}
|
2022-10-10 10:38:33 +00:00
|
|
|
if err = json.Unmarshal(mb, &msg); err != nil {
|
|
|
|
s.sendErrResponse(wsConn, err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if method == eth_subscribe or eth_unsubscribe
|
|
|
|
method, ok := msg["method"].(string)
|
|
|
|
if !ok {
|
|
|
|
// otherwise, call the usual rpc server to respond
|
2022-10-10 10:38:33 +00:00
|
|
|
if err := s.tcpGetAndSendResponse(wsConn, mb); err != nil {
|
2021-04-18 16:39:15 +00:00
|
|
|
s.sendErrResponse(wsConn, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
connID, ok := msg["id"].(float64)
|
|
|
|
if !ok {
|
|
|
|
s.sendErrResponse(
|
|
|
|
wsConn,
|
|
|
|
fmt.Errorf("invalid type for connection ID: %T", msg["id"]).Error(),
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
switch method {
|
|
|
|
case "eth_subscribe":
|
2022-10-10 10:38:33 +00:00
|
|
|
params, ok := s.getParamsAndCheckValid(msg, wsConn)
|
|
|
|
if !ok {
|
2021-04-18 16:39:15 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
subID := rpc.NewID()
|
|
|
|
unsubFn, err := s.api.subscribe(wsConn, subID, params)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
s.sendErrResponse(wsConn, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2022-02-25 19:08:30 +00:00
|
|
|
subscriptions[subID] = unsubFn
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
res := &SubscriptionResponseJSON{
|
|
|
|
Jsonrpc: "2.0",
|
2021-06-08 07:07:11 +00:00
|
|
|
ID: connID,
|
2022-02-25 19:08:30 +00:00
|
|
|
Result: subID,
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
if err := wsConn.WriteJSON(res); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case "eth_unsubscribe":
|
2022-10-10 10:38:33 +00:00
|
|
|
params, ok := s.getParamsAndCheckValid(msg, wsConn)
|
2022-02-25 19:08:30 +00:00
|
|
|
if !ok {
|
2021-04-18 16:39:15 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-10 10:38:33 +00:00
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
id, ok := params[0].(string)
|
|
|
|
if !ok {
|
2021-04-18 16:39:15 +00:00
|
|
|
s.sendErrResponse(wsConn, "invalid parameters")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
subID := rpc.ID(id)
|
|
|
|
unsubFn, ok := subscriptions[subID]
|
|
|
|
if ok {
|
|
|
|
delete(subscriptions, subID)
|
|
|
|
unsubFn()
|
|
|
|
}
|
2022-10-10 10:38:33 +00:00
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
res := &SubscriptionResponseJSON{
|
|
|
|
Jsonrpc: "2.0",
|
2021-06-08 07:07:11 +00:00
|
|
|
ID: connID,
|
2021-04-18 16:39:15 +00:00
|
|
|
Result: ok,
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
if err := wsConn.WriteJSON(res); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// otherwise, call the usual rpc server to respond
|
2022-10-10 10:38:33 +00:00
|
|
|
if err := s.tcpGetAndSendResponse(wsConn, mb); err != nil {
|
2022-02-25 19:08:30 +00:00
|
|
|
s.sendErrResponse(wsConn, err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
// tcpGetAndSendResponse sends error response to client if params is invalid
|
|
|
|
func (s *websocketsServer) getParamsAndCheckValid(msg map[string]interface{}, wsConn *wsConn) ([]interface{}, bool) {
|
|
|
|
params, ok := msg["params"].([]interface{})
|
|
|
|
if !ok {
|
|
|
|
s.sendErrResponse(wsConn, "invalid parameters")
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(params) == 0 {
|
|
|
|
s.sendErrResponse(wsConn, "empty parameters")
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return params, true
|
|
|
|
}
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
// tcpGetAndSendResponse connects to the rest-server over tcp, posts a JSON-RPC request, and sends the response
|
|
|
|
// to the client over websockets
|
|
|
|
func (s *websocketsServer) tcpGetAndSendResponse(wsConn *wsConn, mb []byte) error {
|
2021-09-05 11:03:06 +00:00
|
|
|
req, err := http.NewRequestWithContext(context.Background(), "POST", "http://"+s.rpcAddr, bytes.NewBuffer(mb))
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Could not build request")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Could not perform request")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not read body from response")
|
|
|
|
}
|
|
|
|
|
|
|
|
var wsSend interface{}
|
|
|
|
err = json.Unmarshal(body, &wsSend)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to unmarshal rest-server response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return wsConn.WriteJSON(wsSend)
|
|
|
|
}
|
|
|
|
|
|
|
|
// pubSubAPI is the eth_ prefixed set of APIs in the Web3 JSON-RPC spec
|
|
|
|
type pubSubAPI struct {
|
2021-06-23 06:38:05 +00:00
|
|
|
events *rpcfilters.EventSystem
|
2021-04-18 16:39:15 +00:00
|
|
|
logger log.Logger
|
2022-01-13 13:12:57 +00:00
|
|
|
clientCtx client.Context
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newPubSubAPI creates an instance of the ethereum PubSub API.
|
2022-01-13 13:12:57 +00:00
|
|
|
func newPubSubAPI(clientCtx client.Context, logger log.Logger, tmWSClient *rpcclient.WSClient) *pubSubAPI {
|
2021-07-12 18:39:35 +00:00
|
|
|
logger = logger.With("module", "websocket-client")
|
2021-04-18 16:39:15 +00:00
|
|
|
return &pubSubAPI{
|
2021-07-12 18:39:35 +00:00
|
|
|
events: rpcfilters.NewEventSystem(logger, tmWSClient),
|
|
|
|
logger: logger,
|
2022-01-13 13:12:57 +00:00
|
|
|
clientCtx: clientCtx,
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
func (api *pubSubAPI) subscribe(wsConn *wsConn, subID rpc.ID, params []interface{}) (pubsub.UnsubscribeFunc, error) {
|
2021-04-18 16:39:15 +00:00
|
|
|
method, ok := params[0].(string)
|
|
|
|
if !ok {
|
2022-02-25 19:08:30 +00:00
|
|
|
return nil, errors.New("invalid parameters")
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch method {
|
|
|
|
case "newHeads":
|
|
|
|
// TODO: handle extra params
|
2022-02-25 19:08:30 +00:00
|
|
|
return api.subscribeNewHeads(wsConn, subID)
|
2021-04-18 16:39:15 +00:00
|
|
|
case "logs":
|
|
|
|
if len(params) > 1 {
|
2022-02-25 19:08:30 +00:00
|
|
|
return api.subscribeLogs(wsConn, subID, params[1])
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
2022-02-25 19:08:30 +00:00
|
|
|
return api.subscribeLogs(wsConn, subID, nil)
|
2021-04-18 16:39:15 +00:00
|
|
|
case "newPendingTransactions":
|
2022-02-25 19:08:30 +00:00
|
|
|
return api.subscribePendingTransactions(wsConn, subID)
|
2021-04-18 16:39:15 +00:00
|
|
|
case "syncing":
|
2022-02-25 19:08:30 +00:00
|
|
|
return api.subscribeSyncing(wsConn, subID)
|
2021-04-18 16:39:15 +00:00
|
|
|
default:
|
2022-02-25 19:08:30 +00:00
|
|
|
return nil, errors.Errorf("unsupported method %s", method)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
func (api *pubSubAPI) subscribeNewHeads(wsConn *wsConn, subID rpc.ID) (pubsub.UnsubscribeFunc, error) {
|
|
|
|
sub, unsubFn, err := api.events.SubscribeNewHeads()
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2022-02-25 19:08:30 +00:00
|
|
|
return nil, errors.Wrap(err, "error creating block filter")
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 11:22:32 +00:00
|
|
|
// TODO: use events
|
|
|
|
baseFee := big.NewInt(params.InitialBaseFee)
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
go func() {
|
|
|
|
headersCh := sub.Event()
|
|
|
|
errCh := sub.Err()
|
2021-04-18 16:39:15 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event, ok := <-headersCh:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, ok := event.Data.(tmtypes.EventDataNewBlockHeader)
|
|
|
|
if !ok {
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Debug("event data type mismatch", "type", fmt.Sprintf("%T", event.Data))
|
2021-04-18 16:39:15 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-14 13:34:10 +00:00
|
|
|
header := types.EthHeaderFromTendermint(data.Header, ethtypes.Bloom{}, baseFee)
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
// write to ws conn
|
|
|
|
res := &SubscriptionNotification{
|
|
|
|
Jsonrpc: "2.0",
|
|
|
|
Method: "eth_subscription",
|
|
|
|
Params: &SubscriptionResult{
|
|
|
|
Subscription: subID,
|
|
|
|
Result: header,
|
|
|
|
},
|
|
|
|
}
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
err = wsConn.WriteJSON(res)
|
|
|
|
if err != nil {
|
|
|
|
api.logger.Error("error writing header, will drop peer", "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
try(func() {
|
|
|
|
if err != websocket.ErrCloseSent {
|
|
|
|
_ = wsConn.Close()
|
|
|
|
}
|
|
|
|
}, api.logger, "closing websocket peer sub")
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
case err, ok := <-errCh:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Debug("dropping NewHeads WebSocket subscription", "subscription-id", subID, "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-25 19:08:30 +00:00
|
|
|
}()
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
return unsubFn, nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func try(fn func(), l log.Logger, desc string) {
|
|
|
|
defer func() {
|
|
|
|
if x := recover(); x != nil {
|
|
|
|
if err, ok := x.(error); ok {
|
|
|
|
// debug.PrintStack()
|
2021-07-12 18:39:35 +00:00
|
|
|
l.Debug("panic during "+desc, "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-12 18:39:35 +00:00
|
|
|
l.Debug(fmt.Sprintf("panic during %s: %+v", desc, x))
|
2021-04-18 16:39:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
fn()
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
func (api *pubSubAPI) subscribeLogs(wsConn *wsConn, subID rpc.ID, extra interface{}) (pubsub.UnsubscribeFunc, error) {
|
2021-04-18 16:39:15 +00:00
|
|
|
crit := filters.FilterCriteria{}
|
|
|
|
|
|
|
|
if extra != nil {
|
|
|
|
params, ok := extra.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
err := errors.New("invalid criteria")
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Debug("invalid criteria", "type", fmt.Sprintf("%T", extra))
|
2022-02-25 19:08:30 +00:00
|
|
|
return nil, err
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if params["address"] != nil {
|
2022-10-10 10:38:33 +00:00
|
|
|
address, isString := params["address"].(string)
|
|
|
|
addresses, isSlice := params["address"].([]interface{})
|
|
|
|
if !isString && !isSlice {
|
2021-07-12 18:39:35 +00:00
|
|
|
err := errors.New("invalid addresses; must be address or array of addresses")
|
|
|
|
api.logger.Debug("invalid addresses", "type", fmt.Sprintf("%T", params["address"]))
|
2022-02-25 19:08:30 +00:00
|
|
|
return nil, err
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
crit.Addresses = []common.Address{common.HexToAddress(address)}
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
if isSlice {
|
2021-04-18 16:39:15 +00:00
|
|
|
crit.Addresses = []common.Address{}
|
|
|
|
for _, addr := range addresses {
|
|
|
|
address, ok := addr.(string)
|
|
|
|
if !ok {
|
|
|
|
err := errors.New("invalid address")
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Debug("invalid address", "type", fmt.Sprintf("%T", addr))
|
2022-02-25 19:08:30 +00:00
|
|
|
return nil, err
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
crit.Addresses = append(crit.Addresses, common.HexToAddress(address))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if params["topics"] != nil {
|
|
|
|
topics, ok := params["topics"].([]interface{})
|
|
|
|
if !ok {
|
2021-07-12 18:39:35 +00:00
|
|
|
err := errors.Errorf("invalid topics: %s", topics)
|
|
|
|
api.logger.Error("invalid topics", "type", fmt.Sprintf("%T", topics))
|
2022-02-25 19:08:30 +00:00
|
|
|
return nil, err
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
crit.Topics = make([][]common.Hash, len(topics))
|
|
|
|
|
|
|
|
addCritTopic := func(topicIdx int, topic interface{}) error {
|
|
|
|
tstr, ok := topic.(string)
|
|
|
|
if !ok {
|
|
|
|
err := errors.Errorf("invalid topic: %s", topic)
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Error("invalid topic", "type", fmt.Sprintf("%T", topic))
|
2021-04-18 16:39:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
crit.Topics[topicIdx] = []common.Hash{common.HexToHash(tstr)}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for topicIdx, subtopics := range topics {
|
|
|
|
if subtopics == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// in case we don't have list, but a single topic value
|
|
|
|
if topic, ok := subtopics.(string); ok {
|
|
|
|
if err := addCritTopic(topicIdx, topic); err != nil {
|
2022-02-25 19:08:30 +00:00
|
|
|
return nil, err
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// in case we actually have a list of subtopics
|
|
|
|
subtopicsList, ok := subtopics.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
err := errors.New("invalid subtopics")
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Error("invalid subtopic", "type", fmt.Sprintf("%T", subtopics))
|
2022-02-25 19:08:30 +00:00
|
|
|
return nil, err
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
subtopicsCollect := make([]common.Hash, len(subtopicsList))
|
|
|
|
for idx, subtopic := range subtopicsList {
|
|
|
|
tstr, ok := subtopic.(string)
|
|
|
|
if !ok {
|
|
|
|
err := errors.Errorf("invalid subtopic: %s", subtopic)
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Error("invalid subtopic", "type", fmt.Sprintf("%T", subtopic))
|
2022-02-25 19:08:30 +00:00
|
|
|
return nil, err
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
subtopicsCollect[idx] = common.HexToHash(tstr)
|
|
|
|
}
|
|
|
|
|
|
|
|
crit.Topics[topicIdx] = subtopicsCollect
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
sub, unsubFn, err := api.events.SubscribeLogs(crit)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Error("failed to subscribe logs", "error", err.Error())
|
2022-02-25 19:08:30 +00:00
|
|
|
return nil, err
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
go func() {
|
|
|
|
ch := sub.Event()
|
|
|
|
errCh := sub.Err()
|
2021-04-18 16:39:15 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event, ok := <-ch:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dataTx, ok := event.Data.(tmtypes.EventDataTx)
|
|
|
|
if !ok {
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Debug("event data type mismatch", "type", fmt.Sprintf("%T", event.Data))
|
2021-04-18 16:39:15 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
txResponse, err := evmtypes.DecodeTxResponse(dataTx.TxResult.Result.Data)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Error("failed to decode tx response", "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-23 06:38:05 +00:00
|
|
|
logs := rpcfilters.FilterLogs(evmtypes.LogsToEthereum(txResponse.Logs), crit.FromBlock, crit.ToBlock, crit.Addresses, crit.Topics)
|
2021-04-18 16:39:15 +00:00
|
|
|
if len(logs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ethLog := range logs {
|
|
|
|
res := &SubscriptionNotification{
|
|
|
|
Jsonrpc: "2.0",
|
|
|
|
Method: "eth_subscription",
|
|
|
|
Params: &SubscriptionResult{
|
|
|
|
Subscription: subID,
|
|
|
|
Result: ethLog,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
err = wsConn.WriteJSON(res)
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
try(func() {
|
|
|
|
if err != websocket.ErrCloseSent {
|
2022-02-25 19:08:30 +00:00
|
|
|
_ = wsConn.Close()
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}, api.logger, "closing websocket peer sub")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case err, ok := <-errCh:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Debug("dropping Logs WebSocket subscription", "subscription-id", subID, "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-25 19:08:30 +00:00
|
|
|
}()
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
return unsubFn, nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
func (api *pubSubAPI) subscribePendingTransactions(wsConn *wsConn, subID rpc.ID) (pubsub.UnsubscribeFunc, error) {
|
|
|
|
sub, unsubFn, err := api.events.SubscribePendingTxs()
|
2021-04-18 16:39:15 +00:00
|
|
|
if err != nil {
|
2022-02-25 19:08:30 +00:00
|
|
|
return nil, errors.Wrap(err, "error creating block filter: %s")
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
go func() {
|
|
|
|
txsCh := sub.Event()
|
|
|
|
errCh := sub.Err()
|
2021-04-18 16:39:15 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev := <-txsCh:
|
2022-10-10 10:38:33 +00:00
|
|
|
data, ok := ev.Data.(tmtypes.EventDataTx)
|
|
|
|
if !ok {
|
|
|
|
api.logger.Debug("event data type mismatch", "type", fmt.Sprintf("%T", ev.Data))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
ethTxs, err := types.RawTxToEthTx(api.clientCtx, data.Tx)
|
2022-01-13 13:12:57 +00:00
|
|
|
if err != nil {
|
|
|
|
// not ethereum tx
|
2022-01-14 09:37:33 +00:00
|
|
|
continue
|
2022-01-13 13:12:57 +00:00
|
|
|
}
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
for _, ethTx := range ethTxs {
|
2022-02-25 19:08:30 +00:00
|
|
|
// write to ws conn
|
|
|
|
res := &SubscriptionNotification{
|
|
|
|
Jsonrpc: "2.0",
|
|
|
|
Method: "eth_subscription",
|
|
|
|
Params: &SubscriptionResult{
|
|
|
|
Subscription: subID,
|
|
|
|
Result: ethTx.Hash,
|
|
|
|
},
|
|
|
|
}
|
2022-01-14 09:37:33 +00:00
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
err = wsConn.WriteJSON(res)
|
|
|
|
if err != nil {
|
|
|
|
api.logger.Debug("error writing header, will drop peer", "error", err.Error())
|
|
|
|
|
|
|
|
try(func() {
|
|
|
|
if err != websocket.ErrCloseSent {
|
|
|
|
_ = wsConn.Close()
|
|
|
|
}
|
|
|
|
}, api.logger, "closing websocket peer sub")
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case err, ok := <-errCh:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-07-12 18:39:35 +00:00
|
|
|
api.logger.Debug("dropping PendingTransactions WebSocket subscription", subID, "error", err.Error())
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-25 19:08:30 +00:00
|
|
|
}()
|
2021-04-18 16:39:15 +00:00
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
return unsubFn, nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
func (api *pubSubAPI) subscribeSyncing(wsConn *wsConn, subID rpc.ID) (pubsub.UnsubscribeFunc, error) {
|
|
|
|
return nil, errors.New("syncing subscription is not implemented")
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
2022-10-10 10:38:33 +00:00
|
|
|
|
|
|
|
// copy from github.com/ethereum/go-ethereum/rpc/json.go
|
|
|
|
// isBatch returns true when the first non-whitespace characters is '['
|
|
|
|
func isBatch(raw []byte) bool {
|
|
|
|
for _, c := range raw {
|
|
|
|
// skip insignificant whitespace (http://www.ietf.org/rfc/rfc4627.txt)
|
|
|
|
if c == 0x20 || c == 0x09 || c == 0x0a || c == 0x0d {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return c == '['
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|