fix: networks get updated from a single place now, network.Manager

Runtime networks related env vars are being in use now, updating necessary rpc urls.
This commit is contained in:
Sale Djenic 2023-11-07 12:14:31 +01:00 committed by saledjenic
parent 53c847fda5
commit 752d1a47f6
3 changed files with 22 additions and 96 deletions

View File

@ -120,30 +120,6 @@ func randomCustomNodes() map[string]string {
return result return result
} }
func randomNetworkSlice() []params.Network {
m := randomInt(7) + 1
var result []params.Network
for i := 0; i < m; i++ {
n := params.Network{
ChainID: uint64(i),
ChainName: randomString(),
RPCURL: randomString(),
BlockExplorerURL: randomString(),
IconURL: randomString(),
NativeCurrencyName: randomString(),
NativeCurrencySymbol: randomString(),
NativeCurrencyDecimals: uint64(int64(randomInt(math.MaxInt64))),
IsTest: randomBool(),
Layer: uint64(int64(randomInt(math.MaxInt64))),
Enabled: randomBool(),
ChainColor: randomString(),
ShortName: randomString(),
}
result = append(result, n)
}
return result
}
func randomNodeConfig() *params.NodeConfig { func randomNodeConfig() *params.NodeConfig {
privK, _ := crypto.GenerateKey() privK, _ := crypto.GenerateKey()
@ -189,7 +165,6 @@ func randomNodeConfig() *params.NodeConfig {
LogCompressRotated: randomBool(), LogCompressRotated: randomBool(),
LogToStderr: randomBool(), LogToStderr: randomBool(),
UpstreamConfig: params.UpstreamRPCConfig{Enabled: randomBool(), URL: randomString()}, UpstreamConfig: params.UpstreamRPCConfig{Enabled: randomBool(), URL: randomString()},
Networks: randomNetworkSlice(),
ClusterConfig: params.ClusterConfig{ ClusterConfig: params.ClusterConfig{
Enabled: randomBool(), Enabled: randomBool(),
Fleet: randomString(), Fleet: randomString(),

View File

@ -97,58 +97,6 @@ func insertLogConfig(tx *sql.Tx, c *params.NodeConfig) error {
return err return err
} }
func insertNetworkConfigWithChainColorShortName(tx *sql.Tx, c *params.NodeConfig) error {
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 FROM networks")
if err != nil {
return err
}
var currentNetworks []*params.Network
defer rows.Close()
for rows.Next() {
network := params.Network{}
err := rows.Scan(
&network.ChainID, &network.ChainName, &network.RPCURL, &network.BlockExplorerURL, &network.IconURL,
&network.NativeCurrencyName, &network.NativeCurrencySymbol,
&network.NativeCurrencyDecimals, &network.IsTest, &network.Layer, &network.Enabled, &network.ChainColor, &network.ShortName,
)
if err != nil {
return err
}
currentNetworks = append(currentNetworks, &network)
}
for _, network := range c.Networks {
found := false
for _, currentNetwork := range currentNetworks {
if currentNetwork.ChainID == network.ChainID {
found = true
_, err := tx.Exec(`UPDATE networks SET chain_color = ?, short_name = ? WHERE chain_id = ?`, network.ChainColor, network.ShortName, network.ChainID)
if err != nil {
return err
}
break
}
}
if !found {
_, err := tx.Exec(`
INSERT OR REPLACE INTO networks (
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
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
network.ChainID, network.ChainName, network.RPCURL, network.BlockExplorerURL, network.IconURL,
network.NativeCurrencyName, network.NativeCurrencySymbol, network.NativeCurrencyDecimals,
network.IsTest, network.Layer, network.Enabled, network.ChainColor, network.ShortName,
)
if err != nil {
return err
}
}
}
return nil
}
func insertLightETHConfigTrustedNodes(tx *sql.Tx, c *params.NodeConfig) error { func insertLightETHConfigTrustedNodes(tx *sql.Tx, c *params.NodeConfig) error {
if _, err := tx.Exec(`DELETE FROM light_eth_trusted_nodes WHERE synthetic_id = 'id'`); err != nil { if _, err := tx.Exec(`DELETE FROM light_eth_trusted_nodes WHERE synthetic_id = 'id'`); err != nil {
return err return err
@ -391,7 +339,6 @@ func nodeConfigNormalInserts() []insertFn {
insertIPCConfig, insertIPCConfig,
insertLogConfig, insertLogConfig,
insertUpstreamConfig, insertUpstreamConfig,
insertNetworkConfigWithChainColorShortName,
insertClusterConfig, insertClusterConfig,
insertClusterConfigNodes, insertClusterConfigNodes,
insertLightETHConfig, insertLightETHConfig,

View File

@ -130,6 +130,14 @@ func (nm *Manager) Init(networks []params.Network) error {
// Add new networks and update related chain id for the old ones // Add new networks and update related chain id for the old ones
for i := range networks { for i := range networks {
found := false found := false
// Update original urls if they are empty
if networks[i].OriginalRPCURL == "" {
networks[i].OriginalRPCURL = networks[i].RPCURL
}
if networks[i].OriginalFallbackURL == "" {
networks[i].OriginalFallbackURL = networks[i].FallbackURL
}
for j := range currentNetworks { for j := range currentNetworks {
if currentNetworks[j].ChainID == networks[i].ChainID { if currentNetworks[j].ChainID == networks[i].ChainID {
found = true found = true
@ -140,7 +148,7 @@ func (nm *Manager) Init(networks []params.Network) error {
errors += fmt.Sprintf("error updating network fallback_url for ChainID: %d, %s", currentNetworks[j].ChainID, err.Error()) errors += fmt.Sprintf("error updating network fallback_url for ChainID: %d, %s", currentNetworks[j].ChainID, err.Error())
} }
} }
err := nm.UpdateOriginalURL(currentNetworks[j].ChainID, networks[i].RPCURL, networks[i].FallbackURL) err := nm.UpdateRPCURLs(currentNetworks[j].ChainID, networks[i].RPCURL, networks[i].OriginalRPCURL, networks[i].FallbackURL, networks[i].OriginalFallbackURL)
if err != nil { if err != nil {
errors += fmt.Sprintf("error updating network original url for ChainID: %d, %s", currentNetworks[j].ChainID, err.Error()) errors += fmt.Sprintf("error updating network original url for ChainID: %d, %s", currentNetworks[j].ChainID, err.Error())
} }
@ -149,16 +157,11 @@ func (nm *Manager) Init(networks []params.Network) error {
} }
if !found { if !found {
// Add network if doesn't exist // Insert new network
err := nm.Upsert(&networks[i]) err := nm.Upsert(&networks[i])
if err != nil { if err != nil {
errors += fmt.Sprintf("error inserting network with ChainID: %d, %s", networks[i].ChainID, err.Error()) errors += fmt.Sprintf("error inserting network with ChainID: %d, %s", networks[i].ChainID, err.Error())
} }
err = nm.UpdateOriginalURL(networks[i].ChainID, networks[i].RPCURL, networks[i].FallbackURL)
if err != nil {
errors += fmt.Sprintf("error updating network original url for ChainID: %d, %s", networks[i].ChainID, err.Error())
}
} }
} }
@ -190,8 +193,9 @@ func (nm *Manager) UpdateRelatedChainID(chainID uint64, relatedChainID uint64) e
return err return err
} }
func (nm *Manager) UpdateOriginalURL(chainID uint64, originalRPCURL, OriginalFallbackURL string) error { func (nm *Manager) UpdateRPCURLs(chainID uint64, rpcURL, originalRPCURL, fallbackURL, originalFallbackURL string) error {
_, err := nm.db.Exec(`UPDATE networks SET original_rpc_url = ?, original_fallback_url = ? WHERE chain_id = ?`, originalRPCURL, OriginalFallbackURL, chainID) _, err := nm.db.Exec(`UPDATE networks SET rpc_url = ?, original_rpc_url = ?, fallback_url = ?, original_fallback_url = ? WHERE chain_id = ?`,
rpcURL, originalRPCURL, fallbackURL, originalFallbackURL, chainID)
return err return err
} }