From afb9730236f300b778e0ee6a5494d1c2ff72ef18 Mon Sep 17 00:00:00 2001 From: Andrey Bocharnikov Date: Mon, 6 Jan 2025 22:10:17 +0400 Subject: [PATCH] fix_: address PR feedback part 1 --- params/config.go | 2 +- params/network_config.go | 2 +- params/networkhelper/provider_utils.go | 73 ++++++++++----------- params/networkhelper/provider_utils_test.go | 14 ++-- rpc/network/db/network_db.go | 2 +- rpc/network/db/network_db_test.go | 2 +- rpc/network/db/rpc_provider_db.go | 2 +- rpc/network/testutil/testutil.go | 14 +--- 8 files changed, 45 insertions(+), 66 deletions(-) diff --git a/params/config.go b/params/config.go index 821c7c547f..0439883452 100644 --- a/params/config.go +++ b/params/config.go @@ -481,7 +481,7 @@ type NodeConfig struct { // (persistent storage of user's mailserver records). MailserversConfig MailserversConfig - // Web3ProviderConfig extra configuration for provider.Service + // Web3ProviderConfig extra configuration for provider.Service. // (desktop provider API) Web3ProviderConfig Web3ProviderConfig diff --git a/params/network_config.go b/params/network_config.go index 31f2b8e7d4..d9c9ad6009 100644 --- a/params/network_config.go +++ b/params/network_config.go @@ -47,7 +47,7 @@ type Network struct { RpcProviders []RpcProvider `json:"rpcProviders" validate:"dive,required"` // List of RPC providers, in the order in which they are accessed // Deprecated fields (kept for backward compatibility) - // FIXME: remove deprecated fields (keeping until client integrate this). TODO: add ticket URL + // FIXME: Removal of deprecated fields in integration PR https://github.com/status-im/status-go/pull/6178 DefaultRPCURL string `json:"defaultRpcUrl" validate:"omitempty,url"` // Deprecated: proxy rpc url DefaultFallbackURL string `json:"defaultFallbackURL" validate:"omitempty,url"` // Deprecated: proxy fallback url DefaultFallbackURL2 string `json:"defaultFallbackURL2" validate:"omitempty,url"` // Deprecated: second proxy fallback url diff --git a/params/networkhelper/provider_utils.go b/params/networkhelper/provider_utils.go index 97f3d0b6ad..3ee52dd711 100644 --- a/params/networkhelper/provider_utils.go +++ b/params/networkhelper/provider_utils.go @@ -11,7 +11,7 @@ import ( // preserving user-defined providers and maintaining the Enabled state. func MergeProvidersPreservingUsersAndEnabledState(currentProviders, newProviders []params.RpcProvider) []params.RpcProvider { // Create a map for quick lookup of the Enabled state by Name - enabledState := make(map[string]bool) + enabledState := make(map[string]bool, len(currentProviders)) for _, provider := range currentProviders { enabledState[provider.Name] = provider.Enabled } @@ -24,7 +24,7 @@ func MergeProvidersPreservingUsersAndEnabledState(currentProviders, newProviders } // Retain current providers of type UserProviderType and add them to the beginning of the list - var mergedProviders []params.RpcProvider + mergedProviders := make([]params.RpcProvider, 0, len(currentProviders)+len(newProviders)) for _, provider := range currentProviders { if provider.Type == params.UserProviderType { mergedProviders = append(mergedProviders, provider) @@ -119,58 +119,51 @@ func OverrideEmbeddedProxyProviders(networks []params.Network, enabled bool, use func OverrideDirectProvidersAuth(networks []params.Network, authTokens map[string]string) []params.Network { updatedNetworks := make([]params.Network, len(networks)) - for i, network := range networks { - updatedNetwork := network - updatedProviders := make([]params.RpcProvider, len(network.RpcProviders)) + copy(updatedNetworks, networks) - for j, provider := range network.RpcProviders { - updatedProvider := provider - - if provider.Type == params.EmbeddedDirectProviderType { - host, err := extractHost(provider.URL) - if err == nil { - for suffix, token := range authTokens { - if strings.HasSuffix(host, suffix) && token != "" { - updatedProvider.AuthType = params.TokenAuth - updatedProvider.AuthToken = token - break - } - } - } + for i := range updatedNetworks { + network := &updatedNetworks[i] + + for j := range network.RpcProviders { + provider := &network.RpcProviders[j] + + if provider.Type != params.EmbeddedDirectProviderType { + continue } - updatedProviders[j] = updatedProvider - } + host, err := extractHost(provider.URL) + if err != nil { + continue + } - updatedNetwork.RpcProviders = updatedProviders - updatedNetworks[i] = updatedNetwork + for suffix, token := range authTokens { + if strings.HasSuffix(host, suffix) && token != "" { + provider.AuthType = params.TokenAuth + provider.AuthToken = token + break + } + } + } } return updatedNetworks } func OverrideGanacheToken(networks []params.Network, ganacheURL string, chainID uint64, tokenOverride params.TokenOverride) []params.Network { updatedNetworks := make([]params.Network, len(networks)) - for i, network := range networks { - updatedNetwork := network + copy(updatedNetworks, networks) - if network.ChainID == chainID { - updatedProviders := make([]params.RpcProvider, len(network.RpcProviders)) + for i := range updatedNetworks { + network := &updatedNetworks[i] - for j, provider := range network.RpcProviders { - updatedProvider := provider - if ganacheURL != "" { - updatedProvider.URL = ganacheURL - } - updatedProviders[j] = updatedProvider - } - - updatedNetwork.RpcProviders = updatedProviders - updatedNetwork.TokenOverrides = []params.TokenOverride{ - tokenOverride, + if network.ChainID != chainID { + continue + } + for j := range network.RpcProviders { + if ganacheURL != "" { + network.RpcProviders[j].URL = ganacheURL } } - - updatedNetworks[i] = updatedNetwork + network.TokenOverrides = []params.TokenOverride{tokenOverride} } return updatedNetworks } diff --git a/params/networkhelper/provider_utils_test.go b/params/networkhelper/provider_utils_test.go index f911c41c77..d03eaa1627 100644 --- a/params/networkhelper/provider_utils_test.go +++ b/params/networkhelper/provider_utils_test.go @@ -1,9 +1,11 @@ package networkhelper_test import ( + "reflect" "strings" "testing" + "github.com/brianvoe/gofakeit/v6" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -48,13 +50,7 @@ func TestMergeProvidersPreserveEnabledAndOrder(t *testing.T) { } // Assertions - require.Len(t, mergedProviders, len(expectedProviders), "Expected %d providers after merging", len(expectedProviders)) - - // Compare providers - for i, expectedProvider := range expectedProviders { - actualProvider := mergedProviders[i] - testutil.CompareProviders(t, expectedProvider, actualProvider) - } + require.True(t, reflect.DeepEqual(mergedProviders, expectedProviders), "Merged providers should match the expected providers") } func TestUpdateEmbeddedProxyProviders(t *testing.T) { // Arrange: Create a sample list of networks with various provider types @@ -71,8 +67,8 @@ func TestUpdateEmbeddedProxyProviders(t *testing.T) { networks[0].RpcProviders[1].Enabled = false networks[1].RpcProviders[1].Enabled = false - user := "test_user" - password := "test_password" + user := gofakeit.Username() + password := gofakeit.LetterN(5) // Call the function to update embedded-proxy providers updatedNetworks := networkhelper.OverrideEmbeddedProxyProviders(networks, true, user, password) diff --git a/rpc/network/db/network_db.go b/rpc/network/db/network_db.go index e71c149644..2005fe649e 100644 --- a/rpc/network/db/network_db.go +++ b/rpc/network/db/network_db.go @@ -81,7 +81,7 @@ func (n *NetworksPersistence) GetNetworks(onlyEnabled bool, chainID *uint64) ([] } defer rows.Close() - var result []*params.Network + result := make([]*params.Network, 0, 10) for rows.Next() { network := ¶ms.Network{} err := rows.Scan( diff --git a/rpc/network/db/network_db_test.go b/rpc/network/db/network_db_test.go index 633407e161..d03ecccc00 100644 --- a/rpc/network/db/network_db_test.go +++ b/rpc/network/db/network_db_test.go @@ -66,7 +66,7 @@ func DefaultProviders(chainID uint64) []params.RpcProvider { // Helper function to add and verify networks func (s *NetworksPersistenceTestSuite) addAndVerifyNetworks(networks []*params.Network) { - var networkValues []params.Network + networkValues := make([]params.Network, 0, len(networks)) for _, network := range networks { networkValues = append(networkValues, *network) } diff --git a/rpc/network/db/rpc_provider_db.go b/rpc/network/db/rpc_provider_db.go index 30f6afa5af..b9eb775f3e 100644 --- a/rpc/network/db/rpc_provider_db.go +++ b/rpc/network/db/rpc_provider_db.go @@ -101,7 +101,7 @@ func (p *RpcProvidersPersistence) GetRpcProvidersByType(chainID uint64, provider return nil, err } - var result []params.RpcProvider + result := make([]params.RpcProvider, 0, len(allProviders)) for _, provider := range allProviders { if provider.Type == providerType { result = append(result, provider) diff --git a/rpc/network/testutil/testutil.go b/rpc/network/testutil/testutil.go index 5706917fd4..509bcdd00f 100644 --- a/rpc/network/testutil/testutil.go +++ b/rpc/network/testutil/testutil.go @@ -83,7 +83,7 @@ func CompareNetworks(t require.TestingT, expected, actual *params.Network) { // Helper function to compare lists of providers func CompareProvidersList(t require.TestingT, expectedProviders, actualProviders []params.RpcProvider) { require.Len(t, actualProviders, len(expectedProviders)) - expectedMap := make(map[string]params.RpcProvider) + expectedMap := make(map[string]params.RpcProvider, len(expectedProviders)) for _, provider := range expectedProviders { expectedMap[provider.Name] = provider } @@ -98,7 +98,7 @@ func CompareProvidersList(t require.TestingT, expectedProviders, actualProviders // Helper function to compare lists of networks func CompareNetworksList(t require.TestingT, expectedNetworks, actualNetworks []*params.Network) { require.Len(t, actualNetworks, len(expectedNetworks)) - expectedMap := make(map[uint64]*params.Network) + expectedMap := make(map[uint64]*params.Network, len(expectedNetworks)) for _, network := range expectedNetworks { expectedMap[network.ChainID] = network } @@ -109,13 +109,3 @@ func CompareNetworksList(t require.TestingT, expectedNetworks, actualNetworks [] CompareNetworks(t, expectedNetwork, network) } } -func ConvertNetworksToPointers(networks []params.Network) []*params.Network { - result := make([]*params.Network, len(networks)) - for i := range networks { - // Create a copy of the current network - network := networks[i] - // Store a pointer to the copy - result[i] = &network - } - return result -}