From eefaabb62ca9eaf5abb69b6cc63b2801acdd31e8 Mon Sep 17 00:00:00 2001 From: Luke Imhoff Date: Tue, 31 Jul 2018 14:55:36 -0500 Subject: [PATCH] {:ok, nil} -> {:error, :not_found} in handle_get_block_by_tag Fixes #490 Compatibility with paritytech/parity-ethereum#8281, which fixed paritytech/parity-ethereum#8028 and made pending return be compliant with Ethereum docs. --- apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex | 6 +++- .../test/ethereum_jsonrpc/mox_test.exs | 33 +++++++++++++++++++ .../test/ethereum_jsonrpc_test.exs | 14 +++++--- 3 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 apps/ethereum_jsonrpc/test/ethereum_jsonrpc/mox_test.exs diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex index add9dd9c73db..063ada082110 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex @@ -194,7 +194,7 @@ defmodule EthereumJSONRPC do """ @spec fetch_block_number_by_tag(tag(), json_rpc_named_arguments) :: - {:ok, non_neg_integer()} | {:error, reason :: :invalid_tag | term()} + {:ok, non_neg_integer()} | {:error, reason :: :invalid_tag | :not_found | term()} def fetch_block_number_by_tag(tag, json_rpc_named_arguments) when tag in ~w(earliest latest pending) do tag |> get_block_by_tag_request() @@ -438,6 +438,10 @@ defmodule EthereumJSONRPC do {:ok, quantity_to_integer(quantity)} end + # https://github.com/paritytech/parity-ethereum/pull/8281 fixed + # https://github.com/paritytech/parity-ethereum/issues/8028 + defp handle_get_block_by_tag({:ok, nil}), do: {:error, :not_found} + defp handle_get_block_by_tag({:error, %{"code" => -32602}}), do: {:error, :invalid_tag} defp handle_get_block_by_tag({:error, _} = error), do: error end diff --git a/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/mox_test.exs b/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/mox_test.exs new file mode 100644 index 000000000000..5caf301c2b00 --- /dev/null +++ b/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/mox_test.exs @@ -0,0 +1,33 @@ +defmodule EthereumJSONRPC.MoxTest do + @moduledoc """ + Tests that only work with `EthereumJSONRPC.Mox` because they need precise data back from the network that can't be + gotten reliably. + """ + + use ExUnit.Case, async: true + + import EthereumJSONRPC.Case, only: [variant: 0] + import Mox + + setup do + %{ + json_rpc_named_arguments: [ + transport: EthereumJSONRPC.Mox, + transport_options: [], + variant: variant() + ] + } + end + + setup :verify_on_exit! + + describe "fetch_block_number_by_tag/2" do + test "with pending with null result", %{json_rpc_named_arguments: json_rpc_named_arguments} do + expect(EthereumJSONRPC.Mox, :json_rpc, fn _json, _options -> + {:ok, nil} + end) + + assert {:error, :not_found} = EthereumJSONRPC.fetch_block_number_by_tag("pending", json_rpc_named_arguments) + end + end +end diff --git a/apps/ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs b/apps/ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs index 2bc6fdf57815..4cfb734e043f 100644 --- a/apps/ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs +++ b/apps/ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs @@ -200,15 +200,21 @@ defmodule EthereumJSONRPCTest do test "with pending", %{json_rpc_named_arguments: json_rpc_named_arguments} do if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do expect(EthereumJSONRPC.Mox, :json_rpc, fn _json, _options -> - {:ok, %{"number" => "0x2"}} + {:ok, nil} end) end log_bad_gateway( fn -> EthereumJSONRPC.fetch_block_number_by_tag("pending", json_rpc_named_arguments) end, - fn result -> - assert {:ok, number} = result - assert number > 0 + fn + # Parity after https://github.com/paritytech/parity-ethereum/pull/8281 and anything spec-compliant + {:error, reason} -> + assert reason == :not_found + + # Parity before https://github.com/paritytech/parity-ethereum/pull/8281 + {:ok, number} -> + assert is_integer(number) + assert number > 0 end ) end