From 55854d9b3bf32a13978766b0f724fa18951744ec Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Tue, 11 Jun 2019 10:55:08 +0300 Subject: [PATCH] add test --- .../lib/indexer/block/realtime/fetcher.ex | 6 +++ .../lib/indexer/fetcher/uncle_block.ex | 26 ++++++------- .../test/indexer/fetcher/uncle_block_test.exs | 38 +++++++++++++++++++ 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/apps/indexer/lib/indexer/block/realtime/fetcher.ex b/apps/indexer/lib/indexer/block/realtime/fetcher.ex index 8778677f33..db48fb6f1e 100644 --- a/apps/indexer/lib/indexer/block/realtime/fetcher.ex +++ b/apps/indexer/lib/indexer/block/realtime/fetcher.ex @@ -201,6 +201,12 @@ defmodule Indexer.Block.Realtime.Fetcher do end end + def import(_, _) do + Logger.warn("Empty parameters were provided for realtime fetcher") + + {:ok, []} + end + defp start_fetch_and_import(number, block_fetcher, previous_number, max_number_seen) do start_at = determine_start_at(number, previous_number, max_number_seen) diff --git a/apps/indexer/lib/indexer/fetcher/uncle_block.ex b/apps/indexer/lib/indexer/fetcher/uncle_block.ex index 00aeaa62e0..213604a73e 100644 --- a/apps/indexer/lib/indexer/fetcher/uncle_block.ex +++ b/apps/indexer/lib/indexer/fetcher/uncle_block.ex @@ -104,18 +104,18 @@ defmodule Indexer.Fetcher.UncleBlock do {nephew_hash_bytes, index} end - defp run_blocks(%Blocks{blocks_params: []}, _, original_entries), do: {:retry, original_entries} - - defp run_blocks( - %Blocks{ - blocks_params: blocks_params, - transactions_params: transactions_params, - block_second_degree_relations_params: block_second_degree_relations_params, - errors: errors - }, - block_fetcher, - original_entries - ) do + def run_blocks(%Blocks{blocks_params: []}, _, original_entries), do: {:retry, original_entries} + + def run_blocks( + %Blocks{ + blocks_params: blocks_params, + transactions_params: transactions_params, + block_second_degree_relations_params: block_second_degree_relations_params, + errors: errors + }, + block_fetcher, + original_entries + ) do addresses_params = Addresses.extract_addresses(%{blocks: blocks_params, transactions: transactions_params}) case Block.Fetcher.import(block_fetcher, %{ @@ -241,7 +241,7 @@ defmodule Indexer.Fetcher.UncleBlock do {nephew_hash_bytes, index} end - defp error_to_entry(%{data: %{nephew_hash: hash}, index: index}) when is_binary(hash) do + defp error_to_entry(%{data: %{nephew_hash: hash, index: index}}) when is_binary(hash) do {:ok, %Hash{bytes: nephew_hash_bytes}} = Hash.Full.cast(hash) {nephew_hash_bytes, index} diff --git a/apps/indexer/test/indexer/fetcher/uncle_block_test.exs b/apps/indexer/test/indexer/fetcher/uncle_block_test.exs index b4f93ef038..f3350b8de7 100644 --- a/apps/indexer/test/indexer/fetcher/uncle_block_test.exs +++ b/apps/indexer/test/indexer/fetcher/uncle_block_test.exs @@ -6,6 +6,7 @@ defmodule Indexer.Fetcher.UncleBlockTest do import EthereumJSONRPC, only: [integer_to_quantity: 1] + alias EthereumJSONRPC.Blocks alias Explorer.Chain alias Explorer.Chain.Hash alias Indexer.Block @@ -170,6 +171,43 @@ defmodule Indexer.Fetcher.UncleBlockTest do end end + describe "run_blocks/2" do + test "converts errors to entries for retry", %{json_rpc_named_arguments: json_rpc_named_arguments} do + miner_hash = + address_hash() + |> to_string() + + block_number = 1 + + index = 0 + + hash = "0xa0814f0478fe90c82852f812fd74c96df148654c326d2600d836e6908ebb62b4" + + params = %Blocks{ + errors: [ + %{ + code: 404, + data: %{index: index, nephew_hash: hash}, + message: "Not Found" + } + ], + blocks_params: [%{miner_hash: miner_hash, number: block_number}] + } + + assert {:retry, [{bin_hash, ^index}]} = + UncleBlock.run_blocks( + params, + %Block.Fetcher{ + json_rpc_named_arguments: json_rpc_named_arguments, + callback_module: Indexer.Block.Realtime.Fetcher + }, + [] + ) + + assert Hash.Full.cast(bin_hash) == Hash.Full.cast(hash) + end + end + defp wait(producer) do producer.() rescue