Fix timeout duration parsing

pull/7089/head
Qwerty5Uiop 2 years ago
parent 622c2abffe
commit d4aa6deea3
  1. 25
      apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/geth.ex
  2. 18
      apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/utility/common_helper.ex

@ -27,15 +27,7 @@ defmodule EthereumJSONRPC.Geth do
def fetch_internal_transactions(transactions_params, json_rpc_named_arguments) when is_list(transactions_params) do def fetch_internal_transactions(transactions_params, json_rpc_named_arguments) when is_list(transactions_params) do
id_to_params = id_to_params(transactions_params) id_to_params = id_to_params(transactions_params)
debug_trace_transaction_timeout = json_rpc_named_arguments_corrected_timeout = correct_timeouts(json_rpc_named_arguments)
Application.get_env(:ethereum_jsonrpc, __MODULE__)[:debug_trace_transaction_timeout]
parsed_timeout = CommonHelper.parse_duration(debug_trace_transaction_timeout)
json_rpc_named_arguments_corrected_timeout =
json_rpc_named_arguments
|> put_in([:transport_options, :http_options, :timeout], parsed_timeout)
|> put_in([:transport_options, :http_options, :recv_timeout], parsed_timeout)
with {:ok, responses} <- with {:ok, responses} <-
id_to_params id_to_params
@ -49,6 +41,21 @@ defmodule EthereumJSONRPC.Geth do
end end
end end
defp correct_timeouts(json_rpc_named_arguments) do
debug_trace_transaction_timeout =
Application.get_env(:ethereum_jsonrpc, __MODULE__)[:debug_trace_transaction_timeout]
case CommonHelper.parse_duration(debug_trace_transaction_timeout) do
{:error, :invalid_format} ->
json_rpc_named_arguments
parsed_timeout ->
json_rpc_named_arguments
|> put_in([:transport_options, :http_options, :timeout], parsed_timeout)
|> put_in([:transport_options, :http_options, :recv_timeout], parsed_timeout)
end
end
@doc """ @doc """
Fetches the first trace from the trace URL. Fetches the first trace from the trace URL.
""" """

@ -3,17 +3,17 @@ defmodule EthereumJSONRPC.Utility.CommonHelper do
Common helper functions Common helper functions
""" """
# converts duration like "5s", "2m" to milliseconds # converts duration like "5s", "2m", "1h5m" to milliseconds
@duration_regex ~r/^(\d+)([smh]{1})$/ @duration_regex ~r/(\d+)([smhSMH]?)/
def parse_duration(duration) do def parse_duration(duration) do
case Regex.run(@duration_regex, duration) do case Regex.scan(@duration_regex, duration) do
[_, number, granularity] -> [] ->
number
|> String.to_integer()
|> convert_to_ms(granularity)
_ ->
{:error, :invalid_format} {:error, :invalid_format}
parts ->
Enum.reduce(parts, 0, fn [_, number, granularity], acc ->
acc + convert_to_ms(String.to_integer(number), String.downcase(granularity))
end)
end end
end end

Loading…
Cancel
Save