From f7d79b666a79d6a8ee710704e9fbf0371c7433a9 Mon Sep 17 00:00:00 2001 From: Luke Imhoff Date: Tue, 19 Jun 2018 14:00:50 -0500 Subject: [PATCH] Handle error responses from trace_replayTransaction Conver error responses to error tuple with multiple reasons. --- .../lib/ethereum_jsonrpc/parity.ex | 96 +++++++++------- .../test/ethereum_jsonrpc/parity_test.exs | 108 ++++++++++++++++++ 2 files changed, 165 insertions(+), 39 deletions(-) diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/parity.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/parity.ex index ba607499a2..7cdc7297c6 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/parity.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/parity.ex @@ -10,31 +10,6 @@ defmodule EthereumJSONRPC.Parity do @doc """ Fetches the `t:Explorer.Chain.InternalTransaction.changeset/2` params from the Parity trace URL. - - iex> EthereumJSONRPC.Parity.fetch_internal_transactions([ - ...> %{ - ...> block_number: 1, - ...> hash_data: "0x0fa6f723216dba694337f9bb37d8870725655bdf2573526a39454685659e39b1" - ...> } - ...> ]) - {:ok, - [ - %{ - block_number: 1, - created_contract_address_hash: "0x1e0eaa06d02f965be2dfe0bc9ff52b2d82133461", - created_contract_code: "0x60606040526004361061008e576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063247b3210146100935780632ffdfc8a146100bc57806374294144146100f6578063ae4b1b5b14610125578063bf7370d11461017a578063d1104cb2146101a3578063eecd1079146101f8578063fcff021c14610221575b600080fd5b341561009e57600080fd5b6100a661024a565b6040518082815260200191505060405180910390f35b34156100c757600080fd5b6100e0600480803560ff16906020019091905050610253565b6040518082815260200191505060405180910390f35b341561010157600080fd5b610123600480803590602001909190803560ff16906020019091905050610276565b005b341561013057600080fd5b61013861037a565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561018557600080fd5b61018d61039f565b6040518082815260200191505060405180910390f35b34156101ae57600080fd5b6101b66104d9565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561020357600080fd5b61020b610588565b6040518082815260200191505060405180910390f35b341561022c57600080fd5b6102346105bd565b6040518082815260200191505060405180910390f35b600060c8905090565b6000600160008360ff1660ff168152602001908152602001600020549050919050565b61027e6104d9565b73ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156102b757600080fd5b60008160ff161115156102c957600080fd5b6002808111156102d557fe5b60ff168160ff16111515156102e957600080fd5b6000821180156103125750600160008260ff1660ff168152602001908152602001600020548214155b151561031d57600080fd5b81600160008360ff1660ff168152602001908152602001600020819055508060ff167fe868bbbdd6cd2efcd9ba6e0129d43c349b0645524aba13f8a43bfc7c5ffb0889836040518082815260200191505060405180910390a25050565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000806000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16638b8414c46000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b151561042f57600080fd5b6102c65a03f1151561044057600080fd5b5050506040518051905090508073ffffffffffffffffffffffffffffffffffffffff16630eaba26a6000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b15156104b857600080fd5b6102c65a03f115156104c957600080fd5b5050506040518051905091505090565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663a3b3fff16000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b151561056857600080fd5b6102c65a03f1151561057957600080fd5b50505060405180519050905090565b60006105b860016105aa600261059c61039f565b6105e590919063ffffffff16565b61060090919063ffffffff16565b905090565b60006105e06105ca61039f565b6105d261024a565b6105e590919063ffffffff16565b905090565b60008082848115156105f357fe5b0490508091505092915050565b600080828401905083811015151561061457fe5b80915050929150505600a165627a7a723058206b7eef2a57eb659d5e77e45ab5bc074e99c6a841921038cdb931e119c6aac46c0029", - from_address_hash: "0xe8ddc5c7a2d2f0d7a9798459c0104fdf5e987aca", - gas: 4533872, - gas_used: 382953, - index: 0, - init: "0x6060604052341561000f57600080fd5b60405160208061071a83398101604052808051906020019091905050806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506003600160006001600281111561007e57fe5b60ff1660ff168152602001908152602001600020819055506002600160006002808111156100a857fe5b60ff1660ff168152602001908152602001600020819055505061064a806100d06000396000f30060606040526004361061008e576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063247b3210146100935780632ffdfc8a146100bc57806374294144146100f6578063ae4b1b5b14610125578063bf7370d11461017a578063d1104cb2146101a3578063eecd1079146101f8578063fcff021c14610221575b600080fd5b341561009e57600080fd5b6100a661024a565b6040518082815260200191505060405180910390f35b34156100c757600080fd5b6100e0600480803560ff16906020019091905050610253565b6040518082815260200191505060405180910390f35b341561010157600080fd5b610123600480803590602001909190803560ff16906020019091905050610276565b005b341561013057600080fd5b61013861037a565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561018557600080fd5b61018d61039f565b6040518082815260200191505060405180910390f35b34156101ae57600080fd5b6101b66104d9565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561020357600080fd5b61020b610588565b6040518082815260200191505060405180910390f35b341561022c57600080fd5b6102346105bd565b6040518082815260200191505060405180910390f35b600060c8905090565b6000600160008360ff1660ff168152602001908152602001600020549050919050565b61027e6104d9565b73ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156102b757600080fd5b60008160ff161115156102c957600080fd5b6002808111156102d557fe5b60ff168160ff16111515156102e957600080fd5b6000821180156103125750600160008260ff1660ff168152602001908152602001600020548214155b151561031d57600080fd5b81600160008360ff1660ff168152602001908152602001600020819055508060ff167fe868bbbdd6cd2efcd9ba6e0129d43c349b0645524aba13f8a43bfc7c5ffb0889836040518082815260200191505060405180910390a25050565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000806000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16638b8414c46000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b151561042f57600080fd5b6102c65a03f1151561044057600080fd5b5050506040518051905090508073ffffffffffffffffffffffffffffffffffffffff16630eaba26a6000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b15156104b857600080fd5b6102c65a03f115156104c957600080fd5b5050506040518051905091505090565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663a3b3fff16000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b151561056857600080fd5b6102c65a03f1151561057957600080fd5b50505060405180519050905090565b60006105b860016105aa600261059c61039f565b6105e590919063ffffffff16565b61060090919063ffffffff16565b905090565b60006105e06105ca61039f565b6105d261024a565b6105e590919063ffffffff16565b905090565b60008082848115156105f357fe5b0490508091505092915050565b600080828401905083811015151561061457fe5b80915050929150505600a165627a7a723058206b7eef2a57eb659d5e77e45ab5bc074e99c6a841921038cdb931e119c6aac46c0029000000000000000000000000862d67cb0773ee3f8ce7ea89b328ffea861ab3ef", - trace_address: [], - transaction_hash: "0x0fa6f723216dba694337f9bb37d8870725655bdf2573526a39454685659e39b1", - type: "create", - value: 0 - } - ]} - """ def fetch_internal_transactions(transactions_params) when is_list(transactions_params) do id_to_params = id_to_params(transactions_params) @@ -43,10 +18,7 @@ defmodule EthereumJSONRPC.Parity do id_to_params |> trace_replay_transaction_requests() |> json_rpc(config(:trace_url)) do - internal_transactions_params = - trace_replay_transaction_responses_to_internal_transactions_params(responses, id_to_params) - - {:ok, internal_transactions_params} + trace_replay_transaction_responses_to_internal_transactions_params(responses, id_to_params) end end @@ -73,26 +45,72 @@ defmodule EthereumJSONRPC.Parity do defp trace_replay_transaction_responses_to_internal_transactions_params(responses, id_to_params) when is_list(responses) and is_map(id_to_params) do - responses - |> trace_replay_transaction_responses_to_traces(id_to_params) - |> Traces.to_elixir() - |> Traces.elixir_to_params() + with {:ok, traces} <- trace_replay_transaction_responses_to_traces(responses, id_to_params) do + params = + traces + |> Traces.to_elixir() + |> Traces.elixir_to_params() + + {:ok, params} + end end defp trace_replay_transaction_responses_to_traces(responses, id_to_params) when is_list(responses) and is_map(id_to_params) do - Enum.flat_map(responses, &trace_replay_transaction_response_to_traces(&1, id_to_params)) + responses + |> Enum.map(&trace_replay_transaction_response_to_traces(&1, id_to_params)) + |> Enum.reduce( + {:ok, []}, + fn + {:ok, traces}, {:ok, acc_traces_list} -> + {:ok, [traces | acc_traces_list]} + + {:ok, _}, {:error, _} = acc_error -> + acc_error + + {:error, reason}, {:ok, _} -> + {:error, [reason]} + + {:error, reason}, {:error, acc_reason} -> + {:error, [reason | acc_reason]} + end + ) + |> case do + {:ok, traces_list} -> + traces = + traces_list + |> Enum.reverse() + |> List.flatten() + + {:ok, traces} + + {:error, reverse_reasons} -> + reasons = Enum.reverse(reverse_reasons) + {:error, reasons} + end end defp trace_replay_transaction_response_to_traces(%{"id" => id, "result" => %{"trace" => traces}}, id_to_params) when is_list(traces) and is_map(id_to_params) do %{block_number: block_number, hash_data: transaction_hash} = Map.fetch!(id_to_params, id) - traces - |> Stream.with_index() - |> Enum.map(fn {trace, index} -> - Map.merge(trace, %{"blockNumber" => block_number, "index" => index, "transactionHash" => transaction_hash}) - end) + annotated_traces = + traces + |> Stream.with_index() + |> Enum.map(fn {trace, index} -> + Map.merge(trace, %{"blockNumber" => block_number, "index" => index, "transactionHash" => transaction_hash}) + end) + + {:ok, annotated_traces} + end + + defp trace_replay_transaction_response_to_traces(%{"id" => id, "error" => error}, id_to_params) + when is_map(id_to_params) do + %{block_number: block_number, hash_data: transaction_hash} = Map.fetch!(id_to_params, id) + + annotated_error = Map.merge(error, %{"blockNumber" => block_number, "transactionHash" => transaction_hash}) + + {:error, annotated_error} end defp trace_replay_transaction_requests(id_to_params) when is_map(id_to_params) do diff --git a/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/parity_test.exs b/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/parity_test.exs index e953ad0146..c67b8cf2f3 100644 --- a/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/parity_test.exs +++ b/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/parity_test.exs @@ -2,4 +2,112 @@ defmodule EthereumJSONRPC.ParityTest do use ExUnit.Case, async: true doctest EthereumJSONRPC.Parity + + describe "fetch_internal_transactions/1" do + test "with all valid transaction_params returns {:ok, transactions_params}" do + assert EthereumJSONRPC.Parity.fetch_internal_transactions([ + %{ + block_number: 1, + hash_data: "0x0fa6f723216dba694337f9bb37d8870725655bdf2573526a39454685659e39b1" + } + ]) == { + :ok, + [ + %{ + block_number: 1, + created_contract_address_hash: "0x1e0eaa06d02f965be2dfe0bc9ff52b2d82133461", + created_contract_code: + "0x60606040526004361061008e576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063247b3210146100935780632ffdfc8a146100bc57806374294144146100f6578063ae4b1b5b14610125578063bf7370d11461017a578063d1104cb2146101a3578063eecd1079146101f8578063fcff021c14610221575b600080fd5b341561009e57600080fd5b6100a661024a565b6040518082815260200191505060405180910390f35b34156100c757600080fd5b6100e0600480803560ff16906020019091905050610253565b6040518082815260200191505060405180910390f35b341561010157600080fd5b610123600480803590602001909190803560ff16906020019091905050610276565b005b341561013057600080fd5b61013861037a565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561018557600080fd5b61018d61039f565b6040518082815260200191505060405180910390f35b34156101ae57600080fd5b6101b66104d9565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561020357600080fd5b61020b610588565b6040518082815260200191505060405180910390f35b341561022c57600080fd5b6102346105bd565b6040518082815260200191505060405180910390f35b600060c8905090565b6000600160008360ff1660ff168152602001908152602001600020549050919050565b61027e6104d9565b73ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156102b757600080fd5b60008160ff161115156102c957600080fd5b6002808111156102d557fe5b60ff168160ff16111515156102e957600080fd5b6000821180156103125750600160008260ff1660ff168152602001908152602001600020548214155b151561031d57600080fd5b81600160008360ff1660ff168152602001908152602001600020819055508060ff167fe868bbbdd6cd2efcd9ba6e0129d43c349b0645524aba13f8a43bfc7c5ffb0889836040518082815260200191505060405180910390a25050565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000806000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16638b8414c46000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b151561042f57600080fd5b6102c65a03f1151561044057600080fd5b5050506040518051905090508073ffffffffffffffffffffffffffffffffffffffff16630eaba26a6000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b15156104b857600080fd5b6102c65a03f115156104c957600080fd5b5050506040518051905091505090565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663a3b3fff16000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b151561056857600080fd5b6102c65a03f1151561057957600080fd5b50505060405180519050905090565b60006105b860016105aa600261059c61039f565b6105e590919063ffffffff16565b61060090919063ffffffff16565b905090565b60006105e06105ca61039f565b6105d261024a565b6105e590919063ffffffff16565b905090565b60008082848115156105f357fe5b0490508091505092915050565b600080828401905083811015151561061457fe5b80915050929150505600a165627a7a723058206b7eef2a57eb659d5e77e45ab5bc074e99c6a841921038cdb931e119c6aac46c0029", + from_address_hash: "0xe8ddc5c7a2d2f0d7a9798459c0104fdf5e987aca", + gas: 4_533_872, + gas_used: 382_953, + index: 0, + init: + "0x6060604052341561000f57600080fd5b60405160208061071a83398101604052808051906020019091905050806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506003600160006001600281111561007e57fe5b60ff1660ff168152602001908152602001600020819055506002600160006002808111156100a857fe5b60ff1660ff168152602001908152602001600020819055505061064a806100d06000396000f30060606040526004361061008e576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063247b3210146100935780632ffdfc8a146100bc57806374294144146100f6578063ae4b1b5b14610125578063bf7370d11461017a578063d1104cb2146101a3578063eecd1079146101f8578063fcff021c14610221575b600080fd5b341561009e57600080fd5b6100a661024a565b6040518082815260200191505060405180910390f35b34156100c757600080fd5b6100e0600480803560ff16906020019091905050610253565b6040518082815260200191505060405180910390f35b341561010157600080fd5b610123600480803590602001909190803560ff16906020019091905050610276565b005b341561013057600080fd5b61013861037a565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561018557600080fd5b61018d61039f565b6040518082815260200191505060405180910390f35b34156101ae57600080fd5b6101b66104d9565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b341561020357600080fd5b61020b610588565b6040518082815260200191505060405180910390f35b341561022c57600080fd5b6102346105bd565b6040518082815260200191505060405180910390f35b600060c8905090565b6000600160008360ff1660ff168152602001908152602001600020549050919050565b61027e6104d9565b73ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156102b757600080fd5b60008160ff161115156102c957600080fd5b6002808111156102d557fe5b60ff168160ff16111515156102e957600080fd5b6000821180156103125750600160008260ff1660ff168152602001908152602001600020548214155b151561031d57600080fd5b81600160008360ff1660ff168152602001908152602001600020819055508060ff167fe868bbbdd6cd2efcd9ba6e0129d43c349b0645524aba13f8a43bfc7c5ffb0889836040518082815260200191505060405180910390a25050565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000806000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16638b8414c46000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b151561042f57600080fd5b6102c65a03f1151561044057600080fd5b5050506040518051905090508073ffffffffffffffffffffffffffffffffffffffff16630eaba26a6000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b15156104b857600080fd5b6102c65a03f115156104c957600080fd5b5050506040518051905091505090565b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663a3b3fff16000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b151561056857600080fd5b6102c65a03f1151561057957600080fd5b50505060405180519050905090565b60006105b860016105aa600261059c61039f565b6105e590919063ffffffff16565b61060090919063ffffffff16565b905090565b60006105e06105ca61039f565b6105d261024a565b6105e590919063ffffffff16565b905090565b60008082848115156105f357fe5b0490508091505092915050565b600080828401905083811015151561061457fe5b80915050929150505600a165627a7a723058206b7eef2a57eb659d5e77e45ab5bc074e99c6a841921038cdb931e119c6aac46c0029000000000000000000000000862d67cb0773ee3f8ce7ea89b328ffea861ab3ef", + trace_address: [], + transaction_hash: "0x0fa6f723216dba694337f9bb37d8870725655bdf2573526a39454685659e39b1", + type: "create", + value: 0 + } + ] + } + end + + test "with all invalid transaction_params returns {:error, reasons}" do + assert EthereumJSONRPC.Parity.fetch_internal_transactions([ + %{ + block_number: 1, + hash_data: "0x0000000000000000000000000000000000000000000000000000000000000001" + } + ]) == + {:error, + [ + %{ + "blockNumber" => 1, + "code" => -32603, + "data" => "TransactionNotFound", + "message" => + "Internal error occurred: {}, this should not be the case with eth_call, most likely a bug.", + "transactionHash" => "0x0000000000000000000000000000000000000000000000000000000000000001" + } + ]} + end + + test "with a mix of valid and invalid transaction_params returns {:error, reasons}" do + assert EthereumJSONRPC.Parity.fetch_internal_transactions([ + # start with :ok + %{ + block_number: 1, + hash_data: "0x0fa6f723216dba694337f9bb37d8870725655bdf2573526a39454685659e39b1" + }, + # :ok, :ok clause + %{ + block_number: 34, + hash_data: "0x3a3eb134e6792ce9403ea4188e5e79693de9e4c94e499db132be086400da79e6" + }, + # :ok, :error clause + %{ + block_number: 1, + hash_data: "0x0000000000000000000000000000000000000000000000000000000000000001" + }, + # :error, :ok clause + %{ + block_number: 35, + hash_data: "0x6b80a90c958fb5791a070929379ed6eb7a33ecdf9f9cafcada2f6803b3f25ec3" + }, + # :error, :error clause + %{ + block_number: 2, + hash_data: "0x0000000000000000000000000000000000000000000000000000000000000002" + } + ]) == + {:error, + [ + %{ + "blockNumber" => 1, + "code" => -32603, + "data" => "TransactionNotFound", + "message" => + "Internal error occurred: {}, this should not be the case with eth_call, most likely a bug.", + "transactionHash" => "0x0000000000000000000000000000000000000000000000000000000000000001" + }, + %{ + "blockNumber" => 35, + "code" => -32603, + "data" => "TransactionNotFound", + "message" => + "Internal error occurred: {}, this should not be the case with eth_call, most likely a bug.", + "transactionHash" => "0x6b80a90c958fb5791a070929379ed6eb7a33ecdf9f9cafcada2f6803b3f25ec3" + }, + %{ + "blockNumber" => 2, + "code" => -32603, + "data" => "TransactionNotFound", + "message" => + "Internal error occurred: {}, this should not be the case with eth_call, most likely a bug.", + "transactionHash" => "0x0000000000000000000000000000000000000000000000000000000000000002" + } + ]} + end + end end