Skip to content

Commit

Permalink
handle errors in batch requests (#98)
Browse files Browse the repository at this point in the history
* handle errors in batch requests

* fix tests
  • Loading branch information
ayrat555 authored Dec 25, 2021
1 parent e6c5dda commit 2d06687
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.12.3-otp-24
erlang 24.0.6
elixir 1.13.1-otp-24
erlang 24.1.4
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Mix.Config
import Config

config :ethereumex,
http_options: [pool_timeout: 5000, receive_timeout: 15_000],
Expand Down
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
use Mix.Config
import Config

config :ethereumex, url: System.get_env("ETHEREUM_URL")
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Mix.Config
import Config

config :ethereumex, url: "http://localhost:8545"

Expand Down
13 changes: 10 additions & 3 deletions lib/ethereumex/client/base_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -463,14 +463,21 @@ defmodule Ethereumex.Client.BaseClient do
payload |> Jason.encode!()
end

@spec format_batch([map()]) :: [map() | nil | binary()]
@spec format_batch([map()]) :: [{:ok, map() | nil | binary()} | {:error, any}]
def format_batch(list) do
list
|> Enum.sort(fn %{"id" => id1}, %{"id" => id2} ->
id1 <= id2
end)
|> Enum.map(fn %{"result" => result} ->
result
|> Enum.map(fn
%{"result" => result} ->
{:ok, result}

%{"error" => error} ->
{:error, error}

other ->
{:error, other}
end)
end

Expand Down
3 changes: 1 addition & 2 deletions lib/ethereumex/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ defmodule Ethereumex.HttpClient do
request = Finch.build(:post, url, headers, payload)

case Finch.request(request, EthereumexFinch, Config.http_options()) do
{:ok, response} ->
%Finch.Response{body: body, status: code} = response
{:ok, %Finch.Response{body: body, status: code}} ->
decode_body(body, code)

{:error, error} ->
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Ethereumex.Mixfile do
[
app: :ethereumex,
version: @version,
elixir: "~> 1.7",
elixir: "~> 1.13",
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
package: package(),
Expand Down Expand Up @@ -44,7 +44,7 @@ defmodule Ethereumex.Mixfile do

defp deps do
[
{:finch, "~> 0.9"},
{:finch, "~> 0.10"},
{:jason, "~> 1.2"},
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"castore": {:hex, :castore, "0.1.13", "ccf3ab251ffaebc4319f41d788ce59a6ab3f42b6c27e598ad838ffecee0b04f9", [:mix], [], "hexpm", "a14a7eecfec7e20385493dbb92b0d12c5d77ecfd6307de10102d58c94e8c49c0"},
"castore": {:hex, :castore, "0.1.14", "3f6d7c7c1574c402fef29559d3f1a7389ba3524bc6a090a5e9e6abc3af65dcca", [:mix], [], "hexpm", "b34af542eadb727e6c8b37fdf73e18b2e02eb483a4ea0b52fd500bc23f052b7b"},
"credo": {:hex, :credo, "1.6.1", "7dc76dcdb764a4316c1596804c48eada9fff44bd4b733a91ccbf0c0f368be61e", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "698607fb5993720c7e93d2d8e76f2175bba024de964e160e2f7151ef3ab82ac5"},
"dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"},
"earmark_parser": {:hex, :earmark_parser, "1.4.17", "6f3c7e94170377ba45241d394389e800fb15adc5de51d0a3cd52ae766aafd63f", [:mix], [], "hexpm", "f93ac89c9feca61c165b264b5837bf82344d13bebc634cd575cb711e2e342023"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.25.5", "ac3c5425a80b4b7c4dfecdf51fa9c23a44877124dd8ca34ee45ff608b1c6deb9", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "688cfa538cdc146bc4291607764a7f1fcfa4cce8009ecd62de03b27197528350"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"finch": {:hex, :finch, "0.9.1", "ab2b0151ba88543e221cb50bf0734860db55e8748816ee16e4997fe205f7b315", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6d6b898a59d19f84958eaffec40580f5a9ff88a31e93156707fa8b1d552aa425"},
"finch": {:hex, :finch, "0.10.0", "8e5e6101ae98e7f1ef830594f774411a2f9cbce4f92d8179502da69fbbff52bc", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "80324ba22edbdebca6fac05c8517e7457b79dfe101e3bf6b2f7c5c65c93a9077"},
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
"makeup_elixir": {:hex, :makeup_elixir, "0.15.2", "dc72dfe17eb240552857465cc00cce390960d9a0c055c4ccd38b70629227e97c", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "fd23ae48d09b32eff49d4ced2b43c9f086d402ee4fd4fcb2d7fad97fa8823e75"},
Expand Down
35 changes: 30 additions & 5 deletions test/ethereumex/client/base_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ defmodule Ethereumex.Client.BaseClientTest do
def uppercase([first, second]), do: Enum.join([first, second], "_")
# web3_client_version -> web3_clientVersion and keep this logic
def uppercase([first, second | tail]) do
uppered =
tail
|> Enum.map(&String.capitalize/1)
|> Enum.join()
uppered = Enum.map_join(tail, &String.capitalize/1)

Enum.join([first, second], "_") <> uppered
end
Expand Down Expand Up @@ -328,9 +325,37 @@ defmodule Ethereumex.Client.BaseClientTest do
{:web3_sha3, ["0x68656c6c6f20776f726c64"]}
]

_ = ClientMock.batch_request(requests)
assert _ = ClientMock.batch_request(requests)

assert Ethereumex.Counter.get(:rpc_counter) == initial_count + length(requests)
end
end

describe ".format_batch/1" do
test "formats batch response" do
batch = [
%{
"error" => %{"code" => -32_000, "message" => "execution reverted"},
"id" => 86,
"jsonrpc" => "2.0"
},
%{
"result" => 42,
"id" => 87,
"jsonrpc" => "2.0"
},
%{
"result" => 50,
"id" => 85,
"jsonrpc" => "2.0"
}
]

assert [
{:ok, 50},
{:error, %{"code" => -32_000, "message" => "execution reverted"}},
{:ok, 42}
] = ClientMock.format_batch(batch)
end
end
end
6 changes: 3 additions & 3 deletions test/ethereumex/http_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,9 @@ defmodule Ethereumex.HttpClientTest do
{
:ok,
[
<<_::binary>>,
<<_::binary>>,
"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"
{:ok, <<_::binary>>},
{:ok, <<_::binary>>},
{:ok, "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"}
]
} = result
end
Expand Down
6 changes: 3 additions & 3 deletions test/ethereumex/ipc_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,9 @@ defmodule Ethereumex.IpcClientTest do
{
:ok,
[
<<_::binary>>,
<<_::binary>>,
"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"
{:ok, <<_::binary>>},
{:ok, <<_::binary>>},
{:ok, "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"}
]
} = result
end
Expand Down

0 comments on commit 2d06687

Please sign in to comment.