Skip to content

Commit

Permalink
fix(test)_: fix nested sql requests deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
friofry committed Jan 31, 2025
1 parent d1de5e7 commit b66339f
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions rpc/network/db/network_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func (n *NetworksPersistence) GetRpcPersistence() RpcProvidersPersistenceInterfa
return n.rpcPersistence
}

// GetNetworks returns networks based on filters.
func (n *NetworksPersistence) GetNetworks(onlyEnabled bool, chainID *uint64) ([]*params.Network, error) {
// getNetworksWithoutProviders returns networks based on filters without populating providers.
func (n *NetworksPersistence) getNetworksWithoutProviders(onlyEnabled bool, chainID *uint64) ([]*params.Network, error) {
q := sq.Select(
"chain_id", "chain_name", "rpc_url", "fallback_url",
"block_explorer_url", "icon_url", "native_currency_name", "native_currency_symbol", "native_currency_decimals",
Expand All @@ -80,7 +80,6 @@ func (n *NetworksPersistence) GetNetworks(onlyEnabled bool, chainID *uint64) ([]
if err != nil {
return nil, err
}
defer rows.Close()

result := make([]*params.Network, 0, 10)
for rows.Next() {
Expand All @@ -94,25 +93,42 @@ func (n *NetworksPersistence) GetNetworks(onlyEnabled bool, chainID *uint64) ([]
if err != nil {
return nil, err
}
result = append(result, network)
}

if err = rows.Err(); err != nil {
return nil, err
}
rows.Close()

return result, nil
}

// Fetch RPC providers for the network
providers, err := n.rpcPersistence.GetRpcProviders(network.ChainID)
// populateNetworkProviders populates RPC providers for the given networks.
func (n *NetworksPersistence) populateNetworkProviders(networks []*params.Network) error {
for i := range networks {
providers, err := n.rpcPersistence.GetRpcProviders(networks[i].ChainID)
if err != nil {
return nil, fmt.Errorf("failed to fetch RPC providers for chain_id %d: %w", network.ChainID, err)
return fmt.Errorf("failed to fetch RPC providers for chain_id %d: %w", networks[i].ChainID, err)
}
network.RpcProviders = providers

// Fill deprecated URLs if necessary (assuming this is a function you have)
FillDeprecatedURLs(network, providers)
networks[i].RpcProviders = providers
FillDeprecatedURLs(networks[i], providers)
}
return nil
}

result = append(result, network)
// GetNetworks returns networks based on filters.
func (n *NetworksPersistence) GetNetworks(onlyEnabled bool, chainID *uint64) ([]*params.Network, error) {
networks, err := n.getNetworksWithoutProviders(onlyEnabled, chainID)
if err != nil {
return nil, err
}

if err = rows.Err(); err != nil {
if err := n.populateNetworkProviders(networks); err != nil {
return nil, err
}

return result, nil
return networks, nil
}

// GetAllNetworks returns all networks.
Expand Down

0 comments on commit b66339f

Please sign in to comment.