Skip to content

Commit

Permalink
{:ok, nil} -> {:error, :not_found} in handle_get_block_by_tag
Browse files Browse the repository at this point in the history
Fixes #490

Compatibility with openethereum/parity-ethereum#8281, which fixed
openethereum/parity-ethereum#8028 and made pending return be compliant
with Ethereum docs.
  • Loading branch information
KronicDeth committed Aug 1, 2018
1 parent cf3114c commit eefaabb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
6 changes: 5 additions & 1 deletion apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex
Expand Up @@ -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()
Expand Down Expand Up @@ -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
33 changes: 33 additions & 0 deletions 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
14 changes: 10 additions & 4 deletions apps/ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs
Expand Up @@ -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
Expand Down

0 comments on commit eefaabb

Please sign in to comment.