From 9ea23446471fc3b2d5c5296b44c800ce9e7bed10 Mon Sep 17 00:00:00 2001 From: Anthony Laibe <491074+alaibe@users.noreply.github.com> Date: Tue, 13 Sep 2022 10:40:29 +0200 Subject: [PATCH] fix: allow to update network in app (#2852) Prevent to override change done via the app --- nodecfg/node_config.go | 53 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/nodecfg/node_config.go b/nodecfg/node_config.go index 37a0eb1fc..b0f4ea09c 100644 --- a/nodecfg/node_config.go +++ b/nodecfg/node_config.go @@ -124,20 +124,53 @@ func insertNetworkConfig(tx *sql.Tx, c *params.NodeConfig) error { } func insertNetworkConfigWithChainColorShortName(tx *sql.Tx, c *params.NodeConfig) error { - for _, network := range c.Networks { - _, 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, + 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 }