From f0f3babd38732c2084fd1659d5f355bb1e9f408a Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Tue, 22 Oct 2019 16:02:39 +0300 Subject: [PATCH] format result from ipc --- .../lib/ethereum_jsonrpc/http.ex | 6 ++-- .../lib/ethereum_jsonrpc/ipc.ex | 29 ++++++++++--------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/http.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/http.ex index 6d9f946554..8e2c47dd0a 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/http.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/http.ex @@ -132,7 +132,7 @@ defmodule EthereumJSONRPC.HTTP do # restrict response to only those fields supported by the JSON-RPC 2.0 standard, which means that level of keys is # validated, so we can indicate that with switch to atom keys. - defp standardize_response(%{"jsonrpc" => "2.0" = jsonrpc, "id" => id} = unstandardized) do + def standardize_response(%{"jsonrpc" => "2.0" = jsonrpc, "id" => id} = unstandardized) do # Nethermind return string ids id = quantity_to_integer(id) @@ -155,8 +155,8 @@ defmodule EthereumJSONRPC.HTTP do # restrict error to only those fields supported by the JSON-RPC 2.0 standard, which means that level of keys is # validated, so we can indicate that with switch to atom keys. - defp standardize_error(%{"code" => code, "message" => message} = unstandardized) - when is_integer(code) and is_binary(message) do + def standardize_error(%{"code" => code, "message" => message} = unstandardized) + when is_integer(code) and is_binary(message) do standardized = %{code: code, message: message} case Map.fetch(unstandardized, "data") do diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/ipc.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/ipc.ex index 992b99226b..a387f9547f 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/ipc.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/ipc.ex @@ -2,6 +2,8 @@ defmodule EthereumJSONRPC.IPC do use GenServer @moduledoc false + import EthereumJSONRPC.HTTP, only: [standardize_response: 1] + # Server def child_spec(opts) do @@ -74,9 +76,20 @@ defmodule EthereumJSONRPC.IPC do with {:ok, response} <- post(__MODULE__, Jason.encode!(payload)), {:ok, decoded_body} <- Jason.decode(response) do case decoded_body do - %{"error" => error} -> {:error, error} - result = [%{} | _] -> {:ok, format_batch(result)} - result -> {:ok, Map.get(result, "result")} + %{"error" => error} -> + {:error, error} + + result = [%{} | _] -> + list = + result + |> Enum.reverse() + |> List.flatten() + |> Enum.map(&standardize_response/1) + + {:ok, list} + + result -> + {:ok, Map.get(result, "result")} end else {:error, %Jason.DecodeError{data: ""}} -> {:error, :empty_response} @@ -84,14 +97,4 @@ defmodule EthereumJSONRPC.IPC do {:error, error} -> {:error, error} end end - - defp format_batch(list) do - list - |> Enum.sort(fn %{"id" => id1}, %{"id" => id2} -> - id1 <= id2 - end) - |> Enum.map(fn %{"result" => result} -> - result - end) - end end