2017-09-14 20:14:31 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-09-22 17:49:20 +00:00
|
|
|
"database/sql"
|
2024-07-31 06:21:11 +00:00
|
|
|
"encoding/base64"
|
2017-09-25 16:04:40 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2017-09-14 20:14:31 +00:00
|
|
|
"fmt"
|
2024-08-13 09:22:05 +00:00
|
|
|
"net/http"
|
2024-02-23 14:27:08 +00:00
|
|
|
"net/url"
|
2017-09-25 16:04:40 +00:00
|
|
|
"reflect"
|
2024-08-14 13:15:14 +00:00
|
|
|
"strings"
|
2017-09-25 16:04:40 +00:00
|
|
|
"sync"
|
2018-04-10 10:02:54 +00:00
|
|
|
"time"
|
2017-09-14 20:14:31 +00:00
|
|
|
|
2018-03-20 18:35:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2017-09-14 20:14:31 +00:00
|
|
|
gethrpc "github.com/ethereum/go-ethereum/rpc"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2024-08-15 09:31:47 +00:00
|
|
|
appCommon "github.com/status-im/status-go/common"
|
2019-01-18 09:01:14 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2023-02-20 09:32:45 +00:00
|
|
|
"github.com/status-im/status-go/rpc/chain"
|
2024-09-24 13:07:26 +00:00
|
|
|
"github.com/status-im/status-go/rpc/chain/ethclient"
|
|
|
|
"github.com/status-im/status-go/rpc/chain/rpclimiter"
|
2021-09-22 17:49:20 +00:00
|
|
|
"github.com/status-im/status-go/rpc/network"
|
2021-03-08 11:18:43 +00:00
|
|
|
"github.com/status-im/status-go/services/rpcstats"
|
2023-08-01 18:50:30 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/common"
|
2017-09-14 20:14:31 +00:00
|
|
|
)
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
const (
|
|
|
|
// DefaultCallTimeout is a default timeout for an RPC call
|
|
|
|
DefaultCallTimeout = time.Minute
|
2024-07-31 06:21:11 +00:00
|
|
|
|
|
|
|
// Names of providers
|
|
|
|
providerGrove = "grove"
|
|
|
|
providerInfura = "infura"
|
|
|
|
ProviderStatusProxy = "status-proxy"
|
2024-08-13 10:07:20 +00:00
|
|
|
|
2024-08-15 09:31:47 +00:00
|
|
|
mobile = "mobile"
|
|
|
|
desktop = "desktop"
|
|
|
|
|
2024-08-13 10:07:20 +00:00
|
|
|
// rpcUserAgentFormat 'procurator': *an agent representing others*, aka a "proxy"
|
|
|
|
// allows for the rpc client to have a dedicated user agent, which is useful for the proxy server logs.
|
2024-08-15 09:31:47 +00:00
|
|
|
rpcUserAgentFormat = "procuratee-%s/%s"
|
2024-08-13 10:25:19 +00:00
|
|
|
|
|
|
|
// rpcUserAgentUpstreamFormat a separate user agent format for upstream, because we should not be using upstream
|
|
|
|
// if we see this user agent in the logs that means parts of the application are using a malconfigured http client
|
2024-08-15 09:31:47 +00:00
|
|
|
rpcUserAgentUpstreamFormat = "procuratee-%s-upstream/%s"
|
2018-04-10 10:02:54 +00:00
|
|
|
)
|
|
|
|
|
2018-08-02 07:07:55 +00:00
|
|
|
// List of RPC client errors.
|
|
|
|
var (
|
2024-08-13 10:07:20 +00:00
|
|
|
ErrMethodNotFound = fmt.Errorf("the method does not exist/is not available")
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// rpcUserAgentName the user agent
|
2024-08-15 09:31:47 +00:00
|
|
|
rpcUserAgentName = fmt.Sprintf(rpcUserAgentFormat, "no-GOOS", params.Version)
|
|
|
|
rpcUserAgentUpstreamName = fmt.Sprintf(rpcUserAgentUpstreamFormat, "no-GOOS", params.Version)
|
2018-08-02 07:07:55 +00:00
|
|
|
)
|
|
|
|
|
2024-08-13 10:07:20 +00:00
|
|
|
func init() {
|
2024-08-15 09:31:47 +00:00
|
|
|
if appCommon.IsMobilePlatform() {
|
|
|
|
rpcUserAgentName = fmt.Sprintf(rpcUserAgentFormat, mobile, params.Version)
|
|
|
|
rpcUserAgentUpstreamName = fmt.Sprintf(rpcUserAgentUpstreamFormat, mobile, params.Version)
|
|
|
|
} else {
|
|
|
|
rpcUserAgentName = fmt.Sprintf(rpcUserAgentFormat, desktop, params.Version)
|
|
|
|
rpcUserAgentUpstreamName = fmt.Sprintf(rpcUserAgentUpstreamFormat, desktop, params.Version)
|
2024-08-13 10:07:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 16:04:40 +00:00
|
|
|
// Handler defines handler for RPC methods.
|
2021-09-22 17:49:20 +00:00
|
|
|
type Handler func(context.Context, uint64, ...interface{}) (interface{}, error)
|
2017-09-25 16:04:40 +00:00
|
|
|
|
2023-08-01 18:50:30 +00:00
|
|
|
type ClientInterface interface {
|
2024-09-24 13:07:26 +00:00
|
|
|
AbstractEthClient(chainID common.ChainID) (ethclient.BatchCallClient, error)
|
2024-06-27 21:27:09 +00:00
|
|
|
EthClient(chainID uint64) (chain.ClientInterface, error)
|
|
|
|
EthClients(chainIDs []uint64) (map[uint64]chain.ClientInterface, error)
|
|
|
|
CallContext(context context.Context, result interface{}, chainID uint64, method string, args ...interface{}) error
|
2024-09-19 18:24:06 +00:00
|
|
|
Call(result interface{}, chainID uint64, method string, args ...interface{}) error
|
|
|
|
CallRaw(body string) string
|
|
|
|
GetNetworkManager() *network.Manager
|
2023-08-01 18:50:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:14:31 +00:00
|
|
|
// Client represents RPC client with custom routing
|
|
|
|
// scheme. It automatically decides where RPC call
|
|
|
|
// goes - Upstream or Local node.
|
|
|
|
type Client struct {
|
2019-03-21 12:02:16 +00:00
|
|
|
sync.RWMutex
|
|
|
|
|
2017-09-14 20:14:31 +00:00
|
|
|
upstreamEnabled bool
|
|
|
|
upstreamURL string
|
2021-09-22 17:49:20 +00:00
|
|
|
UpstreamChainID uint64
|
2017-09-14 20:14:31 +00:00
|
|
|
|
2024-02-23 14:27:08 +00:00
|
|
|
local *gethrpc.Client
|
|
|
|
upstream chain.ClientInterface
|
|
|
|
rpcClientsMutex sync.RWMutex
|
|
|
|
rpcClients map[uint64]chain.ClientInterface
|
2024-05-20 11:21:21 +00:00
|
|
|
rpsLimiterMutex sync.RWMutex
|
2024-09-24 13:07:26 +00:00
|
|
|
limiterPerProvider map[string]*rpclimiter.RPCRpsLimiter
|
2017-09-14 20:14:31 +00:00
|
|
|
|
2021-09-22 17:49:20 +00:00
|
|
|
router *router
|
|
|
|
NetworkManager *network.Manager
|
2017-09-25 16:04:40 +00:00
|
|
|
|
|
|
|
handlersMx sync.RWMutex // mx guards handlers
|
|
|
|
handlers map[string]Handler // locally registered handlers
|
2018-03-20 18:35:28 +00:00
|
|
|
log log.Logger
|
2023-03-20 13:02:09 +00:00
|
|
|
|
2024-07-31 06:21:11 +00:00
|
|
|
walletNotifier func(chainID uint64, message string)
|
|
|
|
providerConfigs []params.ProviderConfig
|
2017-09-14 20:14:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-31 15:52:21 +00:00
|
|
|
// Is initialized in a build-tag-dependent module
|
|
|
|
var verifProxyInitFn func(c *Client)
|
|
|
|
|
2017-09-14 20:14:31 +00:00
|
|
|
// NewClient initializes Client and tries to connect to both,
|
|
|
|
// upstream and local node.
|
|
|
|
//
|
|
|
|
// Client is safe for concurrent use and will automatically
|
|
|
|
// reconnect to the server if connection is lost.
|
2024-07-31 06:21:11 +00:00
|
|
|
func NewClient(client *gethrpc.Client, upstreamChainID uint64, upstream params.UpstreamRPCConfig, networks []params.Network, db *sql.DB, providerConfigs []params.ProviderConfig) (*Client, error) {
|
2021-09-22 17:49:20 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
log := log.New("package", "status-go/rpc.Client")
|
|
|
|
networkManager := network.NewManager(db)
|
2024-05-13 13:02:22 +00:00
|
|
|
if networkManager == nil {
|
|
|
|
return nil, errors.New("failed to create network manager")
|
|
|
|
}
|
|
|
|
|
2021-09-22 17:49:20 +00:00
|
|
|
err = networkManager.Init(networks)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Network manager failed to initialize", "error", err)
|
2017-09-25 16:04:40 +00:00
|
|
|
}
|
2017-09-14 20:14:31 +00:00
|
|
|
|
2021-09-22 17:49:20 +00:00
|
|
|
c := Client{
|
2024-02-23 14:27:08 +00:00
|
|
|
local: client,
|
|
|
|
NetworkManager: networkManager,
|
|
|
|
handlers: make(map[string]Handler),
|
|
|
|
rpcClients: make(map[uint64]chain.ClientInterface),
|
2024-09-24 13:07:26 +00:00
|
|
|
limiterPerProvider: make(map[string]*rpclimiter.RPCRpsLimiter),
|
2024-02-23 14:27:08 +00:00
|
|
|
log: log,
|
2024-07-31 06:21:11 +00:00
|
|
|
providerConfigs: providerConfigs,
|
2021-09-22 17:49:20 +00:00
|
|
|
}
|
2017-09-14 20:14:31 +00:00
|
|
|
|
2024-08-13 10:25:19 +00:00
|
|
|
var opts []gethrpc.ClientOption
|
|
|
|
opts = append(opts,
|
|
|
|
gethrpc.WithHeaders(http.Header{
|
|
|
|
"User-Agent": {rpcUserAgentUpstreamName},
|
|
|
|
}),
|
|
|
|
)
|
2024-08-13 09:22:05 +00:00
|
|
|
|
2017-09-14 20:14:31 +00:00
|
|
|
if upstream.Enabled {
|
2021-09-22 17:49:20 +00:00
|
|
|
c.UpstreamChainID = upstreamChainID
|
2017-09-14 20:14:31 +00:00
|
|
|
c.upstreamEnabled = upstream.Enabled
|
|
|
|
c.upstreamURL = upstream.URL
|
2024-08-13 10:25:19 +00:00
|
|
|
upstreamClient, err := gethrpc.DialOptions(context.Background(), c.upstreamURL, opts...)
|
2017-09-14 20:14:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("dial upstream server: %s", err)
|
|
|
|
}
|
2024-05-20 11:21:21 +00:00
|
|
|
limiter, err := c.getRPCRpsLimiter(c.upstreamURL)
|
2024-02-23 14:27:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("get RPC limiter: %s", err)
|
|
|
|
}
|
2024-08-14 13:15:14 +00:00
|
|
|
hostPortUpstream, err := extractHostFromURL(c.upstreamURL)
|
2024-07-02 17:58:55 +00:00
|
|
|
if err != nil {
|
|
|
|
hostPortUpstream = "upstream"
|
|
|
|
}
|
2024-08-14 13:15:14 +00:00
|
|
|
|
|
|
|
// Include the chain-id in the rpc client
|
|
|
|
rpcName := fmt.Sprintf("%s-chain-id-%d", hostPortUpstream, upstreamChainID)
|
|
|
|
|
2024-09-24 13:07:26 +00:00
|
|
|
ethClients := []ethclient.RPSLimitedEthClientInterface{
|
|
|
|
ethclient.NewRPSLimitedEthClient(upstreamClient, limiter, rpcName),
|
|
|
|
}
|
|
|
|
c.upstream = chain.NewClient(ethClients, upstreamChainID)
|
2017-09-14 20:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.router = newRouter(c.upstreamEnabled)
|
|
|
|
|
2022-10-31 15:52:21 +00:00
|
|
|
if verifProxyInitFn != nil {
|
|
|
|
verifProxyInitFn(&c)
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
return &c, nil
|
2017-09-14 20:14:31 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 18:24:06 +00:00
|
|
|
func (c *Client) GetNetworkManager() *network.Manager {
|
|
|
|
return c.NetworkManager
|
|
|
|
}
|
|
|
|
|
2023-03-20 13:02:09 +00:00
|
|
|
func (c *Client) SetWalletNotifier(notifier func(chainID uint64, message string)) {
|
|
|
|
c.walletNotifier = notifier
|
|
|
|
}
|
|
|
|
|
2024-08-14 13:15:14 +00:00
|
|
|
func extractHostFromURL(inputURL string) (string, error) {
|
2024-07-02 17:58:55 +00:00
|
|
|
parsedURL, err := url.Parse(inputURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedURL.Host, nil
|
|
|
|
}
|
|
|
|
|
2024-09-24 13:07:26 +00:00
|
|
|
func (c *Client) getRPCRpsLimiter(key string) (*rpclimiter.RPCRpsLimiter, error) {
|
2024-05-20 11:21:21 +00:00
|
|
|
c.rpsLimiterMutex.Lock()
|
|
|
|
defer c.rpsLimiterMutex.Unlock()
|
2024-07-31 06:21:11 +00:00
|
|
|
if limiter, ok := c.limiterPerProvider[key]; ok {
|
2024-02-23 14:27:08 +00:00
|
|
|
return limiter, nil
|
|
|
|
}
|
2024-09-24 13:07:26 +00:00
|
|
|
limiter := rpclimiter.NewRPCRpsLimiter()
|
2024-07-31 06:21:11 +00:00
|
|
|
c.limiterPerProvider[key] = limiter
|
2024-02-23 14:27:08 +00:00
|
|
|
return limiter, nil
|
|
|
|
}
|
|
|
|
|
2024-07-31 06:21:11 +00:00
|
|
|
func getProviderConfig(providerConfigs []params.ProviderConfig, providerName string) (params.ProviderConfig, error) {
|
|
|
|
for _, providerConfig := range providerConfigs {
|
|
|
|
if providerConfig.Name == providerName {
|
|
|
|
return providerConfig, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return params.ProviderConfig{}, fmt.Errorf("provider config not found for provider: %s", providerName)
|
|
|
|
}
|
|
|
|
|
2023-09-20 08:41:23 +00:00
|
|
|
func (c *Client) getClientUsingCache(chainID uint64) (chain.ClientInterface, error) {
|
2023-08-09 07:20:58 +00:00
|
|
|
c.rpcClientsMutex.Lock()
|
|
|
|
defer c.rpcClientsMutex.Unlock()
|
2021-09-22 17:49:20 +00:00
|
|
|
if rpcClient, ok := c.rpcClients[chainID]; ok {
|
2023-09-20 08:41:23 +00:00
|
|
|
if rpcClient.GetWalletNotifier() == nil {
|
|
|
|
rpcClient.SetWalletNotifier(c.walletNotifier)
|
2023-03-20 13:02:09 +00:00
|
|
|
}
|
2021-09-22 17:49:20 +00:00
|
|
|
return rpcClient, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
network := c.NetworkManager.Find(chainID)
|
|
|
|
if network == nil {
|
2023-02-20 09:32:45 +00:00
|
|
|
if c.UpstreamChainID == chainID {
|
|
|
|
return c.upstream, nil
|
|
|
|
}
|
2021-09-22 17:49:20 +00:00
|
|
|
return nil, fmt.Errorf("could not find network: %d", chainID)
|
|
|
|
}
|
|
|
|
|
2024-08-14 13:15:14 +00:00
|
|
|
ethClients := c.getEthClients(network)
|
2024-07-31 06:21:11 +00:00
|
|
|
if len(ethClients) == 0 {
|
|
|
|
return nil, fmt.Errorf("could not find any RPC URL for chain: %d", chainID)
|
2021-09-22 17:49:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 06:21:11 +00:00
|
|
|
client := chain.NewClient(ethClients, chainID)
|
2024-09-24 13:07:26 +00:00
|
|
|
client.SetWalletNotifier(c.walletNotifier)
|
2024-07-31 06:21:11 +00:00
|
|
|
c.rpcClients[chainID] = client
|
|
|
|
return client, nil
|
|
|
|
}
|
2024-02-23 14:27:08 +00:00
|
|
|
|
2024-09-24 13:07:26 +00:00
|
|
|
func (c *Client) getEthClients(network *params.Network) []ethclient.RPSLimitedEthClientInterface {
|
2024-07-31 06:21:11 +00:00
|
|
|
urls := make(map[string]string)
|
|
|
|
keys := make([]string, 0)
|
|
|
|
authMap := make(map[string]string)
|
|
|
|
|
|
|
|
// find proxy provider
|
|
|
|
proxyProvider, err := getProviderConfig(c.providerConfigs, ProviderStatusProxy)
|
2024-07-02 17:58:55 +00:00
|
|
|
if err != nil {
|
2024-07-31 06:21:11 +00:00
|
|
|
c.log.Warn("could not find provider config for status-proxy", "error", err)
|
2024-07-02 17:58:55 +00:00
|
|
|
}
|
|
|
|
|
2024-07-31 06:21:11 +00:00
|
|
|
if proxyProvider.Enabled {
|
|
|
|
key := ProviderStatusProxy
|
|
|
|
keyFallback := ProviderStatusProxy + "-fallback"
|
|
|
|
urls[key] = network.DefaultRPCURL
|
|
|
|
urls[keyFallback] = network.DefaultFallbackURL
|
|
|
|
keys = []string{key, keyFallback}
|
|
|
|
authMap[key] = proxyProvider.User + ":" + proxyProvider.Password
|
|
|
|
authMap[keyFallback] = authMap[key]
|
2024-07-02 17:58:55 +00:00
|
|
|
}
|
2024-07-31 06:21:11 +00:00
|
|
|
keys = append(keys, []string{"main", "fallback"}...)
|
|
|
|
urls["main"] = network.RPCURL
|
|
|
|
urls["fallback"] = network.FallbackURL
|
|
|
|
|
2024-09-24 13:07:26 +00:00
|
|
|
ethClients := make([]ethclient.RPSLimitedEthClientInterface, 0)
|
2024-08-14 13:15:14 +00:00
|
|
|
for index, key := range keys {
|
2024-07-31 06:21:11 +00:00
|
|
|
var rpcClient *gethrpc.Client
|
2024-09-24 13:07:26 +00:00
|
|
|
var rpcLimiter *rpclimiter.RPCRpsLimiter
|
2024-07-31 06:21:11 +00:00
|
|
|
var err error
|
|
|
|
var hostPort string
|
|
|
|
url := urls[key]
|
|
|
|
|
|
|
|
if len(url) > 0 {
|
2024-08-13 09:22:05 +00:00
|
|
|
// For now, we only support auth for status-proxy.
|
2024-07-31 06:21:11 +00:00
|
|
|
authStr, ok := authMap[key]
|
|
|
|
var opts []gethrpc.ClientOption
|
|
|
|
if ok {
|
|
|
|
authEncoded := base64.StdEncoding.EncodeToString([]byte(authStr))
|
2024-08-13 09:22:05 +00:00
|
|
|
opts = append(opts,
|
|
|
|
gethrpc.WithHeaders(http.Header{
|
|
|
|
"Authorization": {"Basic " + authEncoded},
|
2024-08-13 10:07:20 +00:00
|
|
|
"User-Agent": {rpcUserAgentName},
|
2024-08-13 09:22:05 +00:00
|
|
|
}),
|
|
|
|
)
|
2024-07-31 06:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpcClient, err = gethrpc.DialOptions(context.Background(), url, opts...)
|
|
|
|
if err != nil {
|
|
|
|
c.log.Error("dial server "+key, "error", err)
|
|
|
|
}
|
|
|
|
|
2024-08-14 13:15:14 +00:00
|
|
|
// If using the status-proxy, consider each endpoint as a separate provider
|
|
|
|
circuitKey := fmt.Sprintf("%s-%d", key, index)
|
|
|
|
// Otherwise host is good enough
|
|
|
|
if !strings.Contains(url, "status.im") {
|
|
|
|
hostPort, err = extractHostFromURL(url)
|
|
|
|
if err == nil {
|
|
|
|
circuitKey = hostPort
|
|
|
|
}
|
2024-07-31 06:21:11 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 13:15:14 +00:00
|
|
|
rpcLimiter, err = c.getRPCRpsLimiter(circuitKey)
|
2024-07-31 06:21:11 +00:00
|
|
|
if err != nil {
|
|
|
|
c.log.Error("get RPC limiter "+key, "error", err)
|
|
|
|
}
|
|
|
|
|
2024-09-24 13:07:26 +00:00
|
|
|
ethClients = append(ethClients, ethclient.NewRPSLimitedEthClient(rpcClient, rpcLimiter, circuitKey))
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 06:21:11 +00:00
|
|
|
return ethClients
|
2021-09-22 17:49:20 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 09:22:05 +00:00
|
|
|
// EthClient returns ethclient.Client per chain
|
2023-09-20 08:41:23 +00:00
|
|
|
func (c *Client) EthClient(chainID uint64) (chain.ClientInterface, error) {
|
2023-02-20 09:32:45 +00:00
|
|
|
client, err := c.getClientUsingCache(chainID)
|
2021-09-22 17:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
2021-09-22 17:49:20 +00:00
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2023-08-01 18:50:30 +00:00
|
|
|
// AbstractEthClient returns a partial abstraction used by new components for testing purposes
|
2024-09-24 13:07:26 +00:00
|
|
|
func (c *Client) AbstractEthClient(chainID common.ChainID) (ethclient.BatchCallClient, error) {
|
2023-08-01 18:50:30 +00:00
|
|
|
client, err := c.getClientUsingCache(uint64(chainID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2023-09-20 08:41:23 +00:00
|
|
|
func (c *Client) EthClients(chainIDs []uint64) (map[uint64]chain.ClientInterface, error) {
|
|
|
|
clients := make(map[uint64]chain.ClientInterface, 0)
|
2023-02-20 09:32:45 +00:00
|
|
|
for _, chainID := range chainIDs {
|
|
|
|
client, err := c.getClientUsingCache(chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-24 08:38:27 +00:00
|
|
|
clients[chainID] = client
|
2023-02-20 09:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return clients, nil
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 08:41:23 +00:00
|
|
|
// SetClient strictly for testing purposes
|
|
|
|
func (c *Client) SetClient(chainID uint64, client chain.ClientInterface) {
|
|
|
|
c.rpcClientsMutex.Lock()
|
|
|
|
defer c.rpcClientsMutex.Unlock()
|
|
|
|
c.rpcClients[chainID] = client
|
|
|
|
}
|
|
|
|
|
2019-03-21 12:02:16 +00:00
|
|
|
// UpdateUpstreamURL changes the upstream RPC client URL, if the upstream is enabled.
|
|
|
|
func (c *Client) UpdateUpstreamURL(url string) error {
|
|
|
|
if c.upstream == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcClient, err := gethrpc.Dial(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-20 11:21:21 +00:00
|
|
|
rpsLimiter, err := c.getRPCRpsLimiter(url)
|
2024-02-23 14:27:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-21 12:02:16 +00:00
|
|
|
c.Lock()
|
2024-08-14 13:15:14 +00:00
|
|
|
hostPortUpstream, err := extractHostFromURL(url)
|
2024-07-02 17:58:55 +00:00
|
|
|
if err != nil {
|
|
|
|
hostPortUpstream = "upstream"
|
|
|
|
}
|
2024-09-24 13:07:26 +00:00
|
|
|
|
|
|
|
ethClients := []ethclient.RPSLimitedEthClientInterface{
|
|
|
|
ethclient.NewRPSLimitedEthClient(rpcClient, rpsLimiter, hostPortUpstream),
|
|
|
|
}
|
|
|
|
c.upstream = chain.NewClient(ethClients, c.UpstreamChainID)
|
2019-03-21 12:02:16 +00:00
|
|
|
c.upstreamURL = url
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:14:31 +00:00
|
|
|
// Call performs a JSON-RPC call with the given arguments and unmarshals into
|
|
|
|
// result if no error occurred.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// It uses custom routing scheme for calls.
|
2021-09-22 17:49:20 +00:00
|
|
|
func (c *Client) Call(result interface{}, chainID uint64, method string, args ...interface{}) error {
|
2017-09-14 20:14:31 +00:00
|
|
|
ctx := context.Background()
|
2021-09-22 17:49:20 +00:00
|
|
|
return c.CallContext(ctx, result, chainID, method, args...)
|
2017-09-14 20:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// It uses custom routing scheme for calls.
|
2018-04-10 10:02:54 +00:00
|
|
|
// If there are any local handlers registered for this call, they will handle it.
|
2021-09-22 17:49:20 +00:00
|
|
|
func (c *Client) CallContext(ctx context.Context, result interface{}, chainID uint64, method string, args ...interface{}) error {
|
2021-03-08 11:18:43 +00:00
|
|
|
rpcstats.CountCall(method)
|
2018-08-02 07:07:55 +00:00
|
|
|
if c.router.routeBlocked(method) {
|
|
|
|
return ErrMethodNotFound
|
|
|
|
}
|
|
|
|
|
2017-09-25 16:04:40 +00:00
|
|
|
// check locally registered handlers first
|
|
|
|
if handler, ok := c.handler(method); ok {
|
2021-09-22 17:49:20 +00:00
|
|
|
return c.callMethod(ctx, result, chainID, handler, args...)
|
2017-09-25 16:04:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 17:49:20 +00:00
|
|
|
return c.CallContextIgnoringLocalHandlers(ctx, result, chainID, method, args...)
|
2018-04-10 10:02:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CallContextIgnoringLocalHandlers performs a JSON-RPC call with the given
|
|
|
|
// arguments.
|
|
|
|
//
|
|
|
|
// If there are local handlers registered for this call, they would
|
|
|
|
// be ignored. It is useful if the call is happening from within a local
|
|
|
|
// handler itself.
|
|
|
|
// Upstream calls routing will be used anyway.
|
2021-09-22 17:49:20 +00:00
|
|
|
func (c *Client) CallContextIgnoringLocalHandlers(ctx context.Context, result interface{}, chainID uint64, method string, args ...interface{}) error {
|
2018-08-02 07:07:55 +00:00
|
|
|
if c.router.routeBlocked(method) {
|
|
|
|
return ErrMethodNotFound
|
|
|
|
}
|
|
|
|
|
2017-09-19 09:52:38 +00:00
|
|
|
if c.router.routeRemote(method) {
|
2023-02-20 09:32:45 +00:00
|
|
|
client, err := c.getClientUsingCache(chainID)
|
2021-09-22 17:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-20 09:32:45 +00:00
|
|
|
return client.CallContext(ctx, result, method, args...)
|
2017-09-14 20:14:31 +00:00
|
|
|
}
|
2018-04-10 10:02:54 +00:00
|
|
|
|
2019-11-27 12:22:23 +00:00
|
|
|
if c.local == nil {
|
|
|
|
c.log.Warn("Local JSON-RPC endpoint missing", "method", method)
|
|
|
|
return errors.New("missing local JSON-RPC endpoint")
|
|
|
|
}
|
2017-09-19 09:52:38 +00:00
|
|
|
return c.local.CallContext(ctx, result, method, args...)
|
2017-09-14 20:14:31 +00:00
|
|
|
}
|
2017-09-25 16:04:40 +00:00
|
|
|
|
|
|
|
// RegisterHandler registers local handler for specific RPC method.
|
|
|
|
//
|
|
|
|
// If method is registered, it will be executed with given handler and
|
|
|
|
// never routed to the upstream or local servers.
|
|
|
|
func (c *Client) RegisterHandler(method string, handler Handler) {
|
|
|
|
c.handlersMx.Lock()
|
|
|
|
defer c.handlersMx.Unlock()
|
|
|
|
|
|
|
|
c.handlers[method] = handler
|
|
|
|
}
|
|
|
|
|
2022-10-31 15:52:21 +00:00
|
|
|
// UnregisterHandler removes a previously registered handler.
|
|
|
|
func (c *Client) UnregisterHandler(method string) {
|
|
|
|
c.handlersMx.Lock()
|
|
|
|
defer c.handlersMx.Unlock()
|
|
|
|
|
|
|
|
delete(c.handlers, method)
|
|
|
|
}
|
|
|
|
|
2017-09-25 16:04:40 +00:00
|
|
|
// callMethod calls registered RPC handler with given args and pointer to result.
|
|
|
|
// It handles proper params and result converting
|
|
|
|
//
|
|
|
|
// TODO(divan): use cancellation via context here?
|
2021-09-22 17:49:20 +00:00
|
|
|
func (c *Client) callMethod(ctx context.Context, result interface{}, chainID uint64, handler Handler, args ...interface{}) error {
|
|
|
|
response, err := handler(ctx, chainID, args...)
|
2017-09-25 16:04:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if result is nil, just ignore result -
|
|
|
|
// the same way as gethrpc.CallContext() caller would expect
|
|
|
|
if result == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-12 10:26:25 +00:00
|
|
|
return setResultFromRPCResponse(result, response)
|
2017-09-25 16:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handler is a concurrently safe method to get registered handler by name.
|
|
|
|
func (c *Client) handler(method string) (Handler, bool) {
|
|
|
|
c.handlersMx.RLock()
|
|
|
|
defer c.handlersMx.RUnlock()
|
|
|
|
handler, ok := c.handlers[method]
|
|
|
|
return handler, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// setResultFromRPCResponse tries to set result value from response using reflection
|
|
|
|
// as concrete types are unknown.
|
|
|
|
func setResultFromRPCResponse(result, response interface{}) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("invalid result type: %s", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
responseValue := reflect.ValueOf(response)
|
|
|
|
|
|
|
|
// If it is called via CallRaw, result has type json.RawMessage and
|
|
|
|
// we should marshal the response before setting it.
|
|
|
|
// Otherwise, it is called with CallContext and result is of concrete type,
|
|
|
|
// thus we should try to set it as it is.
|
|
|
|
// If response type and result type are incorrect, an error should be returned.
|
|
|
|
// TODO(divan): add additional checks for result underlying value, if needed:
|
|
|
|
// some example: https://golang.org/src/encoding/json/decode.go#L596
|
|
|
|
switch reflect.ValueOf(result).Elem().Type() {
|
|
|
|
case reflect.TypeOf(json.RawMessage{}), reflect.TypeOf([]byte{}):
|
|
|
|
data, err := json.Marshal(response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
responseValue = reflect.ValueOf(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
value := reflect.ValueOf(result).Elem()
|
|
|
|
if !value.CanSet() {
|
|
|
|
return errors.New("can't assign value to result")
|
|
|
|
}
|
|
|
|
value.Set(responseValue)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|