diff --git a/params/networkhelper/provider_utils.go b/params/networkhelper/provider_utils.go index 566fef399..da6897868 100644 --- a/params/networkhelper/provider_utils.go +++ b/params/networkhelper/provider_utils.go @@ -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 { + 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] diff --git a/params/networkhelper/provider_utils_test.go b/params/networkhelper/provider_utils_test.go index 9b23b38b3..80f049405 100644 --- a/params/networkhelper/provider_utils_test.go +++ b/params/networkhelper/provider_utils_test.go @@ -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") +} diff --git a/rpc/network/db/utils.go b/rpc/network/db/utils.go index afbcfb9b6..6a45987c5 100644 --- a/rpc/network/db/utils.go +++ b/rpc/network/db/utils.go @@ -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 { + 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)) } } }()