Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(config)_: integrate new rpc providers configs #6178

Merged
merged 20 commits into from
Jan 31, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(config)_: Do not store embedded RPC provider credentials in the DB
* add method to make a deepcopy of a network + tests
* improved logging
* improved memory allocation
friofry committed Jan 31, 2025
commit 31a9488c181594540c81352344eca7f44e6a5684
22 changes: 14 additions & 8 deletions params/networkhelper/provider_utils.go
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ func ToggleUserProviders(providers []params.RpcProvider, enabled bool) []params.

// GetEmbeddedProviders returns the embedded providers from the list.
func GetEmbeddedProviders(providers []params.RpcProvider) []params.RpcProvider {
var embeddedProviders []params.RpcProvider
embeddedProviders := make([]params.RpcProvider, 0, len(providers))
for _, provider := range providers {
if provider.Type != params.UserProviderType {
embeddedProviders = append(embeddedProviders, provider)
@@ -62,7 +62,7 @@ func GetEmbeddedProviders(providers []params.RpcProvider) []params.RpcProvider {

// GetUserProviders returns the user-defined providers from the list.
func GetUserProviders(providers []params.RpcProvider) []params.RpcProvider {
var userProviders []params.RpcProvider
userProviders := make([]params.RpcProvider, 0, len(providers))
for _, provider := range providers {
if provider.Type == params.UserProviderType {
userProviders = append(userProviders, provider)
@@ -118,19 +118,25 @@ func OverrideEmbeddedProxyProviders(networks []params.Network, enabled bool, use
return updatedNetworks
}

func deepCopyNetworks(networks []params.Network) []params.Network {
func DeepCopyNetwork(network params.Network) params.Network {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this could be a method of params.network?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Igor's suggestion for this and the function below.

updatedNetwork := network
updatedNetwork.RpcProviders = make([]params.RpcProvider, len(network.RpcProviders))
copy(updatedNetwork.RpcProviders, network.RpcProviders)
updatedNetwork.TokenOverrides = make([]params.TokenOverride, len(network.TokenOverrides))
copy(updatedNetwork.TokenOverrides, network.TokenOverrides)
return updatedNetwork
}

func DeepCopyNetworks(networks []params.Network) []params.Network {
updatedNetworks := make([]params.Network, len(networks))
for i, network := range networks {
updatedNetwork := network
updatedNetwork.RpcProviders = make([]params.RpcProvider, len(network.RpcProviders))
copy(updatedNetwork.RpcProviders, network.RpcProviders)
updatedNetworks[i] = updatedNetwork
updatedNetworks[i] = DeepCopyNetwork(network)
}
return updatedNetworks
}

func OverrideDirectProvidersAuth(networks []params.Network, authTokens map[string]string) []params.Network {
updatedNetworks := deepCopyNetworks(networks)
updatedNetworks := DeepCopyNetworks(networks)

for i := range updatedNetworks {
network := &updatedNetworks[i]
21 changes: 21 additions & 0 deletions params/networkhelper/provider_utils_test.go
Original file line number Diff line number Diff line change
@@ -143,3 +143,24 @@ func TestOverrideDirectProvidersAuth(t *testing.T) {
}
}
}

func TestDeepCopyNetwork(t *testing.T) {
originalNetwork := testutil.CreateNetwork(api.MainnetChainID, "Ethereum Mainnet", []params.RpcProvider{
*params.NewUserProvider(api.MainnetChainID, "Provider1", "https://userprovider.example.com", true),
*params.NewDirectProvider(api.MainnetChainID, "Provider2", "https://mainnet.infura.io/v3/", true),
})

originalNetwork.TokenOverrides = []params.TokenOverride{
{Symbol: "token1", Address: common.HexToAddress("0x123")},
}

copiedNetwork := networkhelper.DeepCopyNetwork(*originalNetwork)

assert.True(t, reflect.DeepEqual(originalNetwork, &copiedNetwork), "Copied network should be deeply equal to the original")

// Modify the copied network and verify that the original network remains unchanged
copiedNetwork.RpcProviders[0].Enabled = false
copiedNetwork.TokenOverrides[0].Symbol = "modifiedSymbol"
assert.NotEqual(t, originalNetwork.RpcProviders[0].Enabled, copiedNetwork.RpcProviders[0].Enabled, "Original network should remain unchanged")
assert.NotEqual(t, originalNetwork.TokenOverrides[0].Symbol, copiedNetwork.TokenOverrides[0].Symbol, "Original network should remain unchanged")
}
10 changes: 8 additions & 2 deletions rpc/network/db/utils.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,9 @@ package db

import (
"database/sql"
"errors"
"fmt"

"github.com/status-im/status-go/params"
)

@@ -65,13 +67,17 @@ func ExecuteWithinTransaction(db *sql.DB, fn func(tx *sql.Tx) error) (err error)
}
defer func() {
if p := recover(); p != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is an smart thing to rollback in case of a panic 👍
Perhaps we should do this for all db transactions? Or at least write it down as a guideline?

err = fmt.Errorf("panic: %v", p)
_ = tx.Rollback()
panic(p)
} else if err != nil {
_ = tx.Rollback()
rollbackErr := tx.Rollback()
if rollbackErr != nil {
err = errors.Join(err, fmt.Errorf("transaction rollback failed: %w", rollbackErr))
}
} else {
if commitErr := tx.Commit(); commitErr != nil {
err = fmt.Errorf("transaction commit failed: %w", commitErr)
err = errors.Join(err, fmt.Errorf("transaction commit failed: %w", commitErr))
}
}
}()