Skip to content

Commit

Permalink
chore(wallet)_: remove unused upstream client (#5934)
Browse files Browse the repository at this point in the history
* chore(wallet)_: remove unused upstream client

fixes #5933
  • Loading branch information
friofry authored Oct 11, 2024
1 parent 2c5737b commit 86cd41d
Show file tree
Hide file tree
Showing 23 changed files with 35 additions and 258 deletions.
10 changes: 0 additions & 10 deletions api/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,6 @@ func DefaultNodeConfig(installationID string, request *requests.CreateAccount, o
nodeConfig.NetworkID = nodeConfig.Networks[0].ChainID
}

if request.UpstreamConfig != "" {
nodeConfig.UpstreamConfig = params.UpstreamRPCConfig{
Enabled: true,
URL: request.UpstreamConfig,
}
} else {
nodeConfig.UpstreamConfig.URL = mainnet(request.WalletSecretsConfig.StatusProxyStageName).RPCURL
nodeConfig.UpstreamConfig.Enabled = true
}

nodeConfig.Name = DefaultNodeName
nodeConfig.NoDiscovery = true
nodeConfig.MaxPeers = DefaultMaxPeers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS upstream_config;
1 change: 0 additions & 1 deletion appdatabase/node_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ func randomNodeConfig() *params.NodeConfig {
LogMaxSize: randomInt(math.MaxInt64),
LogCompressRotated: randomBool(),
LogToStderr: randomBool(),
UpstreamConfig: params.UpstreamRPCConfig{Enabled: randomBool(), URL: randomString()},
ClusterConfig: params.ClusterConfig{
Enabled: randomBool(),
Fleet: randomString(),
Expand Down
4 changes: 0 additions & 4 deletions cmd/ping-community/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,6 @@ func defaultNodeConfig(installationID string) (*params.NodeConfig, error) {
nodeConfig.NetworkID = 1
nodeConfig.LogLevel = "ERROR"
nodeConfig.DataDir = api.DefaultDataDir
nodeConfig.UpstreamConfig = params.UpstreamRPCConfig{
Enabled: true,
URL: "https://mainnet.infura.io/v3/800c641949d64d768a5070a1b0511938",
}

nodeConfig.Name = "StatusIM"
clusterConfig, err := params.LoadClusterConfigFromFleet("eth.prod")
Expand Down
4 changes: 0 additions & 4 deletions cmd/populate-db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,6 @@ func defaultNodeConfig(installationID string) (*params.NodeConfig, error) {
nodeConfig.NetworkID = 1
nodeConfig.LogLevel = "ERROR"
nodeConfig.DataDir = api.DefaultDataDir
nodeConfig.UpstreamConfig = params.UpstreamRPCConfig{
Enabled: true,
URL: "https://mainnet.infura.io/v3/800c641949d64d768a5070a1b0511938",
}

nodeConfig.Name = "StatusIM"
clusterConfig, err := params.LoadClusterConfigFromFleet("eth.prod")
Expand Down
5 changes: 0 additions & 5 deletions cmd/spiff-workflow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,6 @@ func defaultNodeConfig(installationID string) (*params.NodeConfig, error) {
// Disable to avoid errors about empty ClusterConfig.BootNodes.
nodeConfig.NoDiscovery = true

nodeConfig.UpstreamConfig = params.UpstreamRPCConfig{
Enabled: true,
URL: "https://mainnet.infura.io/v3/800c641949d64d768a5070a1b0511938",
}

nodeConfig.Name = "StatusIM"
clusterConfig, err := params.LoadClusterConfigFromFleet("status.prod")
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion node/get_status_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func (n *StatusNode) setupRPCClient() (err error) {
},
}

n.rpcClient, err = rpc.NewClient(gethNodeClient, n.config.NetworkID, n.config.UpstreamConfig, n.config.Networks, n.appDB, providerConfigs)
n.rpcClient, err = rpc.NewClient(gethNodeClient, n.config.NetworkID, n.config.Networks, n.appDB, providerConfigs)
if err != nil {
return
}
Expand Down
3 changes: 0 additions & 3 deletions node/status_node_rpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ func TestNodeRPCClientCallOnlyPublicAPIs(t *testing.T) {

statusNode, err := createAndStartStatusNode(&params.NodeConfig{
APIModules: "", // no whitelisted API modules; use only public APIs
UpstreamConfig: params.UpstreamRPCConfig{
URL: "https://infura.io",
Enabled: true},
WakuConfig: params.WakuConfig{
Enabled: true,
},
Expand Down
2 changes: 1 addition & 1 deletion node/status_node_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (b *StatusNode) initServices(config *params.NodeConfig, mediaServer *server
setSettingsNotifier(accDB, settingsFeed)

services := []common.StatusService{}
services = appendIf(config.UpstreamConfig.Enabled, services, b.rpcFiltersService())
services = append(services, b.rpcFiltersService())
services = append(services, b.subscriptionService())
services = append(services, b.rpcStatsService())
services = append(services, b.appmetricsService())
Expand Down
12 changes: 0 additions & 12 deletions nodecfg/node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,6 @@ func insertClusterConfig(tx *sql.Tx, c *params.NodeConfig) error {
return err
}

func insertUpstreamConfig(tx *sql.Tx, c *params.NodeConfig) error {
_, err := tx.Exec(`INSERT OR REPLACE INTO upstream_config (enabled, url, synthetic_id) VALUES (?, ?, 'id')`, c.UpstreamConfig.Enabled, c.UpstreamConfig.URL)
return err
}

func insertLightETHConfig(tx *sql.Tx, c *params.NodeConfig) error {
_, err := tx.Exec(`INSERT OR REPLACE INTO light_eth_config (enabled, database_cache, min_trusted_fraction, synthetic_id) VALUES (?, ?, ?, 'id')`, c.LightEthConfig.Enabled, c.LightEthConfig.DatabaseCache, c.LightEthConfig.MinTrustedFraction)
return err
Expand Down Expand Up @@ -336,7 +331,6 @@ func nodeConfigUpgradeInserts() []insertFn {
insertHTTPConfig,
insertIPCConfig,
insertLogConfig,
insertUpstreamConfig,
insertClusterConfig,
insertClusterConfigNodes,
insertLightETHConfig,
Expand All @@ -360,7 +354,6 @@ func nodeConfigNormalInserts() []insertFn {
insertHTTPConfig,
insertIPCConfig,
insertLogConfig,
insertUpstreamConfig,
insertClusterConfig,
insertClusterConfigNodes,
insertLightETHConfig,
Expand Down Expand Up @@ -501,11 +494,6 @@ func loadNodeConfig(tx *sql.Tx) (*params.NodeConfig, error) {
return nil, err
}

err = tx.QueryRow("SELECT enabled, url FROM upstream_config WHERE synthetic_id = 'id'").Scan(&nodecfg.UpstreamConfig.Enabled, &nodecfg.UpstreamConfig.URL)
if err != nil && err != sql.ErrNoRows {
return nil, err
}

rows, err = tx.Query(`SELECT
chain_id, chain_name, rpc_url, block_explorer_url, icon_url, native_currency_name,
native_currency_symbol, native_currency_decimals, is_test, layer, enabled, chain_color, short_name
Expand Down
56 changes: 0 additions & 56 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -293,20 +292,6 @@ func NewLimits(min, max int) Limits {
}
}

// ----------
// UpstreamRPCConfig
// ----------

// UpstreamRPCConfig stores configuration for upstream rpc connection.
type UpstreamRPCConfig struct {
// Enabled flag specifies whether feature is enabled
Enabled bool

// URL sets the rpc upstream host address for communication with
// a non-local infura endpoint.
URL string
}

type ProviderConfig struct {
// Enabled flag specifies whether feature is enabled
Enabled bool `validate:"required"`
Expand Down Expand Up @@ -459,9 +444,6 @@ type NodeConfig struct {
// EnableStatusService should be true to enable methods under status namespace.
EnableStatusService bool

// UpstreamConfig extra config for providing upstream infura server.
UpstreamConfig UpstreamRPCConfig `json:"UpstreamConfig"`

// Initial networks to load
Networks []Network

Expand Down Expand Up @@ -951,9 +933,6 @@ func NewNodeConfig(dataDir string, networkID uint64) (*NodeConfig, error) {
LogFile: "",
LogLevel: "ERROR",
NoDiscovery: true,
UpstreamConfig: UpstreamRPCConfig{
URL: getUpstreamURL(networkID),
},
LightEthConfig: LightEthConfig{
DatabaseCache: 16,
},
Expand Down Expand Up @@ -1061,10 +1040,6 @@ func (c *NodeConfig) Validate() error {
}
}

if c.UpstreamConfig.Enabled && c.LightEthConfig.Enabled {
return fmt.Errorf("both UpstreamConfig and LightEthConfig are enabled, but they are mutually exclusive")
}

if err := c.validateChildStructs(validate); err != nil {
return err
}
Expand Down Expand Up @@ -1096,9 +1071,6 @@ func (c *NodeConfig) Validate() error {

func (c *NodeConfig) validateChildStructs(validate *validator.Validate) error {
// Validate child structs
if err := c.UpstreamConfig.Validate(validate); err != nil {
return err
}
if err := c.ClusterConfig.Validate(validate); err != nil {
return err
}
Expand All @@ -1117,23 +1089,6 @@ func (c *NodeConfig) validateChildStructs(validate *validator.Validate) error {
return nil
}

// Validate validates the UpstreamRPCConfig struct and returns an error if inconsistent values are found
func (c *UpstreamRPCConfig) Validate(validate *validator.Validate) error {
if !c.Enabled {
return nil
}

if err := validate.Struct(c); err != nil {
return err
}

if _, err := url.ParseRequestURI(c.URL); err != nil {
return fmt.Errorf("UpstreamRPCConfig.URL '%s' is invalid: %v", c.URL, err.Error())
}

return nil
}

// Validate validates the ClusterConfig struct and returns an error if inconsistent values are found
func (c *ClusterConfig) Validate(validate *validator.Validate) error {
if !c.Enabled {
Expand Down Expand Up @@ -1188,17 +1143,6 @@ func (c *TorrentConfig) Validate(validate *validator.Validate) error {
return nil
}

func getUpstreamURL(networkID uint64) string {
switch networkID {
case MainNetworkID:
return MainnetEthereumNetworkURL
case GoerliNetworkID:
return GoerliEthereumNetworkURL
}

return ""
}

// Save dumps configuration to the disk
func (c *NodeConfig) Save() error {
data, err := json.MarshalIndent(c, "", " ")
Expand Down
15 changes: 0 additions & 15 deletions params/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,6 @@ func TestNodeConfigValidate(t *testing.T) {
}`,
Error: "NodeKey is invalid",
},
{
Name: "Validate that UpstreamConfig.URL is validated if UpstreamConfig is enabled",
Config: `{
"NetworkId": 1,
"DataDir": "/some/dir",
"KeyStoreDir": "/some/dir",
"KeycardPairingDataFile": "/some/dir/keycard/pairings.json",
"NoDiscovery": true,
"UpstreamConfig": {
"Enabled": true,
"URL": "[bad.url]"
}
}`,
Error: "'[bad.url]' is invalid",
},
{
Name: "Validate that UpstreamConfig.URL is not validated if UpstreamConfig is disabled",
Config: `{
Expand Down
77 changes: 4 additions & 73 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,9 @@ type ClientInterface interface {
type Client struct {
sync.RWMutex

upstreamEnabled bool
upstreamURL string
UpstreamChainID uint64

local *gethrpc.Client
upstream chain.ClientInterface
rpcClientsMutex sync.RWMutex
rpcClients map[uint64]chain.ClientInterface
rpsLimiterMutex sync.RWMutex
Expand All @@ -115,12 +112,11 @@ type Client struct {
// Is initialized in a build-tag-dependent module
var verifProxyInitFn func(c *Client)

// NewClient initializes Client and tries to connect to both,
// upstream and local node.
// NewClient initializes Client
//
// Client is safe for concurrent use and will automatically
// reconnect to the server if connection is lost.
func NewClient(client *gethrpc.Client, upstreamChainID uint64, upstream params.UpstreamRPCConfig, networks []params.Network, db *sql.DB, providerConfigs []params.ProviderConfig) (*Client, error) {
func NewClient(client *gethrpc.Client, upstreamChainID uint64, networks []params.Network, db *sql.DB, providerConfigs []params.ProviderConfig) (*Client, error) {
var err error

log := log.New("package", "status-go/rpc.Client")
Expand All @@ -144,40 +140,8 @@ func NewClient(client *gethrpc.Client, upstreamChainID uint64, upstream params.U
providerConfigs: providerConfigs,
}

var opts []gethrpc.ClientOption
opts = append(opts,
gethrpc.WithHeaders(http.Header{
"User-Agent": {rpcUserAgentUpstreamName},
}),
)

if upstream.Enabled {
c.UpstreamChainID = upstreamChainID
c.upstreamEnabled = upstream.Enabled
c.upstreamURL = upstream.URL
upstreamClient, err := gethrpc.DialOptions(context.Background(), c.upstreamURL, opts...)
if err != nil {
return nil, fmt.Errorf("dial upstream server: %s", err)
}
limiter, err := c.getRPCRpsLimiter(c.upstreamURL)
if err != nil {
return nil, fmt.Errorf("get RPC limiter: %s", err)
}
hostPortUpstream, err := extractHostFromURL(c.upstreamURL)
if err != nil {
hostPortUpstream = "upstream"
}

// Include the chain-id in the rpc client
rpcName := fmt.Sprintf("%s-chain-id-%d", hostPortUpstream, upstreamChainID)

ethClients := []ethclient.RPSLimitedEthClientInterface{
ethclient.NewRPSLimitedEthClient(upstreamClient, limiter, rpcName),
}
c.upstream = chain.NewClient(ethClients, upstreamChainID)
}

c.router = newRouter(c.upstreamEnabled)
c.UpstreamChainID = upstreamChainID
c.router = newRouter(true)

if verifProxyInitFn != nil {
verifProxyInitFn(&c)
Expand Down Expand Up @@ -235,9 +199,6 @@ func (c *Client) getClientUsingCache(chainID uint64) (chain.ClientInterface, err

network := c.NetworkManager.Find(chainID)
if network == nil {
if c.UpstreamChainID == chainID {
return c.upstream, nil
}
return nil, fmt.Errorf("could not find network: %d", chainID)
}

Expand Down Expand Up @@ -368,36 +329,6 @@ func (c *Client) SetClient(chainID uint64, client chain.ClientInterface) {
c.rpcClients[chainID] = client
}

// 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
}
rpsLimiter, err := c.getRPCRpsLimiter(url)
if err != nil {
return err
}
c.Lock()
hostPortUpstream, err := extractHostFromURL(url)
if err != nil {
hostPortUpstream = "upstream"
}

ethClients := []ethclient.RPSLimitedEthClientInterface{
ethclient.NewRPSLimitedEthClient(rpcClient, rpsLimiter, hostPortUpstream),
}
c.upstream = chain.NewClient(ethClients, c.UpstreamChainID)
c.upstreamURL = url
c.Unlock()

return nil
}

// Call performs a JSON-RPC call with the given arguments and unmarshals into
// result if no error occurred.
//
Expand Down
Loading

0 comments on commit 86cd41d

Please sign in to comment.