Skip to content

Commit

Permalink
fix_: address PR feedback part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
friofry committed Jan 6, 2025
1 parent d4927e9 commit afb9730
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 66 deletions.
2 changes: 1 addition & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion params/network_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 33 additions & 40 deletions params/networkhelper/provider_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 5 additions & 9 deletions params/networkhelper/provider_utils_test.go
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion rpc/network/db/network_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := &params.Network{}
err := rows.Scan(
Expand Down
2 changes: 1 addition & 1 deletion rpc/network/db/network_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion rpc/network/db/rpc_provider_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 2 additions & 12 deletions rpc/network/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}

0 comments on commit afb9730

Please sign in to comment.