From 0ca82e17c55d0e37e17b28bc73dca13d1dc91f9d Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 21 Feb 2019 12:51:16 +0300 Subject: [PATCH 1/2] set logs block number form transaction block number if it's empty --- .../lib/indexer/block/fetcher/receipts.ex | 22 +++ .../indexer/block/fetcher/receipts_test.exs | 149 ++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 apps/indexer/test/indexer/block/fetcher/receipts_test.exs diff --git a/apps/indexer/lib/indexer/block/fetcher/receipts.ex b/apps/indexer/lib/indexer/block/fetcher/receipts.ex index 56ec86ecc63c..1257d8db2ac4 100644 --- a/apps/indexer/lib/indexer/block/fetcher/receipts.ex +++ b/apps/indexer/lib/indexer/block/fetcher/receipts.ex @@ -29,6 +29,10 @@ defmodule Indexer.Block.Fetcher.Receipts do {:error, reason}, {:ok, _acc} -> {:halt, {:error, reason}} end) + |> case do + {:ok, receipt_params} -> {:ok, set_block_number_to_logs(receipt_params, transaction_params)} + other -> other + end end def put(transactions_params, receipts_params) when is_list(transactions_params) and is_list(receipts_params) do @@ -41,4 +45,22 @@ defmodule Indexer.Block.Fetcher.Receipts do Map.merge(transaction_params, Map.fetch!(transaction_hash_to_receipt_params, transaction_hash)) end) end + + defp set_block_number_to_logs(%{logs: logs} = params, transaction_params) do + logs_with_block_numbers = + Enum.map(logs, fn %{transaction_hash: transaction_hash, block_number: block_number} = log_params -> + if is_nil(block_number) do + transaction = + Enum.find(transaction_params, fn transaction -> + transaction[:hash] == transaction_hash + end) + + %{log_params | block_number: transaction[:block_number]} + else + log_params + end + end) + + %{params | logs: logs_with_block_numbers} + end end diff --git a/apps/indexer/test/indexer/block/fetcher/receipts_test.exs b/apps/indexer/test/indexer/block/fetcher/receipts_test.exs new file mode 100644 index 000000000000..548f71bcf4cb --- /dev/null +++ b/apps/indexer/test/indexer/block/fetcher/receipts_test.exs @@ -0,0 +1,149 @@ +defmodule Indexer.Block.Fetcher.ReceiptsTest do + use EthereumJSONRPC.Case, async: false + + import Mox + + alias Indexer.Block.Fetcher + alias Indexer.Block.Fetcher.Receipts + + @moduletag capture_log: true + + setup :set_mox_global + + setup :verify_on_exit! + + describe "fetch/2" do + setup %{json_rpc_named_arguments: json_rpc_named_arguments} do + %{ + block_fetcher: %Fetcher{ + json_rpc_named_arguments: json_rpc_named_arguments, + receipts_concurrency: 10 + } + } + end + + test "fetches logs setting their blocks if they're null", %{ + block_fetcher: %Fetcher{json_rpc_named_arguments: json_rpc_named_arguments} = block_fetcher + } do + if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do + EthereumJSONRPC.Mox + |> expect(:json_rpc, fn _json, _options -> + {:ok, + [ + %{ + id: 0, + jsonrpc: "2.0", + result: %{ + "blockHash" => nil, + "blockNumber" => nil, + "contractAddress" => nil, + "cumulativeGasUsed" => "0x5208", + "gasUsed" => "0x5208", + "logs" => [ + %{ + "address" => "0x8bf38d4764929064f2d4d3a56520a76ab3df415b", + "blockHash" => nil, + "blockNumber" => nil, + "data" => "0x000000000000000000000000862d67cb0773ee3f8ce7ea89b328ffea861ab3ef", + "logIndex" => "0x0", + "topics" => ["0x600bcf04a13e752d1e3670a5a9f1c21177ca2a93c6f5391d4f1298d098097c22"], + "transactionHash" => "0x43bd884872de3e488692881baeec262e7b95234d3965248c39fe992fffd433e5", + "transactionIndex" => "0x0", + "transactionLogIndex" => "0x0", + "type" => "mined" + } + ], + "logsBloom" => + "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "root" => nil, + "status" => "0x1", + "transactionHash" => "0x43bd884872de3e488692881baeec262e7b95234d3965248c39fe992fffd433e5", + "transactionIndex" => "0x0" + } + }, + %{ + id: 1, + jsonrpc: "2.0", + result: %{ + "blockHash" => "0xf6b4b8c88df3ebd252ec476328334dc026cf66606a84fb769b3d3cbccc8471bd", + "blockNumber" => "0x25", + "contractAddress" => nil, + "cumulativeGasUsed" => "0xc512", + "gasUsed" => "0xc512", + "logs" => [ + %{ + "address" => "0x8bf38d4764929064f2d4d3a56520a76ab3df415b", + "blockHash" => "0xf6b4b8c88df3ebd252ec476328334dc026cf66606a84fb769b3d3cbccc8471bd", + "blockNumber" => "0x25", + "data" => "0x000000000000000000000000862d67cb0773ee3f8ce7ea89b328ffea861ab3ef", + "logIndex" => "0x0", + "topics" => ["0x600bcf04a13e752d1e3670a5a9f1c21177ca2a93c6f5391d4f1298d098097c22"], + "transactionHash" => "0x53bd884872de3e488692881baeec262e7b95234d3965248c39fe992fffd433e5", + "transactionIndex" => "0x0", + "transactionLogIndex" => "0x0", + "type" => "mined" + } + ], + "logsBloom" => + "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000200000000000000000000020000000000000000200000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "root" => nil, + "status" => "0x1", + "transactionHash" => "0x53bd884872de3e488692881baeec262e7b95234d3965248c39fe992fffd433e5", + "transactionIndex" => "0x0" + } + } + ]} + end) + end + + transaction_params = [ + %{ + block_hash: "0xf6b4b8c88df3ebd252ec476328334dc026cf66606a84fb769b3d3cbccc8471bd", + block_number: 46147, + from_address_hash: "0xa1e4380a3b1f749673e270229993ee55f35663b4", + gas: 21000, + gas_price: 50_000_000_000_000, + hash: "0x43bd884872de3e488692881baeec262e7b95234d3965248c39fe992fffd433e5", + index: 0, + input: "0x", + nonce: 0, + r: 61_965_845_294_689_009_770_156_372_156_374_760_022_787_886_965_323_743_865_986_648_153_755_601_564_112, + s: 31_606_574_786_494_953_692_291_101_914_709_926_755_545_765_281_581_808_821_704_454_381_804_773_090_106, + to_address_hash: "0x5df9b87991262f6ba471f09758cde1c0fc1de734", + v: 28, + value: 31337, + transaction_index: 0 + }, + %{ + block_hash: "0xf7b4b8c88df3ebd252ec476328334dc026cf66606a84fb769b3d3cbccc8471bd", + block_number: 46148, + from_address_hash: "0xa1e4380a3b1f749673e270229993ee55f35663b4", + gas: 21000, + gas_price: 50_000_000_000_000, + hash: "0x53bd884872de3e488692881baeec262e7b95234d3965248c39fe992fffd433e5", + index: 0, + input: "0x", + nonce: 0, + r: 61_965_845_294_689_009_770_156_372_156_374_760_022_787_886_965_323_743_865_986_648_153_755_601_564_112, + s: 31_606_574_786_494_953_692_291_101_914_709_926_755_545_765_281_581_808_821_704_454_381_804_773_090_106, + to_address_hash: "0x5df9b87991262f6ba471f09758cde1c0fc1de734", + v: 28, + value: 31337, + transaction_index: 0 + } + ] + + {:ok, %{logs: logs}} = Receipts.fetch(block_fetcher, transaction_params) + + assert Enum.find(logs, fn log -> + log[:transaction_hash] == "0x53bd884872de3e488692881baeec262e7b95234d3965248c39fe992fffd433e5" && + log[:block_number] == 37 + end) + + assert Enum.find(logs, fn log -> + log[:transaction_hash] == "0x43bd884872de3e488692881baeec262e7b95234d3965248c39fe992fffd433e5" && + log[:block_number] == 46147 + end) + end + end +end From a3ec5c50ae9f2dfafd56364e8568fb9b4eef99e8 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 21 Feb 2019 13:09:54 +0300 Subject: [PATCH 2/2] do not run test on parity and geth --- apps/indexer/test/indexer/block/fetcher/receipts_test.exs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/indexer/test/indexer/block/fetcher/receipts_test.exs b/apps/indexer/test/indexer/block/fetcher/receipts_test.exs index 548f71bcf4cb..95570db4a232 100644 --- a/apps/indexer/test/indexer/block/fetcher/receipts_test.exs +++ b/apps/indexer/test/indexer/block/fetcher/receipts_test.exs @@ -22,6 +22,8 @@ defmodule Indexer.Block.Fetcher.ReceiptsTest do } end + @tag :no_parity + @tag :no_geth test "fetches logs setting their blocks if they're null", %{ block_fetcher: %Fetcher{json_rpc_named_arguments: json_rpc_named_arguments} = block_fetcher } do