From 229df065b4c3aa827f4ecfc9b03c7578cf9ce524 Mon Sep 17 00:00:00 2001 From: Luke Imhoff Date: Wed, 23 Jan 2019 14:22:37 -0600 Subject: [PATCH] Failing regression tests for #1337 --- .../parity/fetched_beneficiaries.ex | 1 - .../parity/fetched_beneficiaries_test.exs | 2 +- .../indexer/block/catchup/fetcher_test.exs | 102 ++++++++++++++++++ .../uncataloged_rewards/importer_test.exs | 44 ++++++++ 4 files changed, 147 insertions(+), 2 deletions(-) diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/parity/fetched_beneficiaries.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/parity/fetched_beneficiaries.ex index 6ee59474c8..a47c7d6377 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/parity/fetched_beneficiaries.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/parity/fetched_beneficiaries.ex @@ -30,7 +30,6 @@ defmodule EthereumJSONRPC.Parity.FetchedBeneficiaries do ...> }, ...> %{ ...> "action" => %{"author" => "0x2", "rewardType" => "external", "value" => "0x0"}, - ...> "blockHash" => "0x52a8d2185282506ce681364d2aa0c085ba45fdeb5d6c0ddec1131617a71ee2ca", ...> "blockHash" => "0xFFF", ...> "blockNumber" => 12, ...> "result" => nil, diff --git a/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/parity/fetched_beneficiaries_test.exs b/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/parity/fetched_beneficiaries_test.exs index 769432b7f4..5c5cee4871 100644 --- a/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/parity/fetched_beneficiaries_test.exs +++ b/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/parity/fetched_beneficiaries_test.exs @@ -41,7 +41,7 @@ defmodule EthereumJSONRPC.Parity.FetchedBeneficiariesTest do block_number = 1_000 block_quantity = EthereumJSONRPC.integer_to_quantity(block_number) hash1 = "0xef481b4e2c3ed62265617f2e9dfcdf3cf3efc11a" - hash2 = "0x523b6539ff08d72a6c8bb598af95bf50c1ea839c" + hash2 = "0xef481b4e2c3ed62265617f2e9dfcdf3cf3efc11a" reward = "0xde0b6b3a7640000" responses = [ diff --git a/apps/indexer/test/indexer/block/catchup/fetcher_test.exs b/apps/indexer/test/indexer/block/catchup/fetcher_test.exs index 2832fc6dda..4b66cae068 100644 --- a/apps/indexer/test/indexer/block/catchup/fetcher_test.exs +++ b/apps/indexer/test/indexer/block/catchup/fetcher_test.exs @@ -2,6 +2,7 @@ defmodule Indexer.Block.Catchup.FetcherTest do use EthereumJSONRPC.Case, async: false use Explorer.DataCase + import EthereumJSONRPC, only: [integer_to_quantity: 1] import Mox alias Indexer.{Block, CoinBalance, InternalTransaction, Token, TokenBalance} @@ -111,4 +112,105 @@ defmodule Indexer.Block.Catchup.FetcherTest do assert_receive {:uncles, [^uncle_hash]} end end + + describe "task/1" do + test "ignores fetched beneficiaries with different hash for same number", %{ + json_rpc_named_arguments: json_rpc_named_arguments + } do + latest_block_number = 1 + latest_block_quantity = integer_to_quantity(latest_block_number) + + block_number = latest_block_number - 1 + block_hash = block_hash() + block_quantity = integer_to_quantity(block_number) + + miner_hash = address_hash() + miner_hash_data = to_string(miner_hash) + + new_block_hash = block_hash() + + refute block_hash == new_block_hash + + EthereumJSONRPC.Mox + |> expect(:json_rpc, fn %{method: "eth_getBlockByNumber", params: ["latest", false]}, _options -> + {:ok, %{"number" => latest_block_quantity}} + end) + |> expect(:json_rpc, fn [ + %{ + id: id, + jsonrpc: "2.0", + method: "eth_getBlockByNumber", + params: [^block_quantity, true] + } + ], + _options -> + {:ok, + [ + %{ + id: id, + jsonrpc: "2.0", + result: %{ + "hash" => to_string(block_hash), + "number" => block_quantity, + "difficulty" => "0x0", + "gasLimit" => "0x0", + "gasUsed" => "0x0", + "extraData" => "0x0", + "logsBloom" => "0x0", + "miner" => miner_hash_data, + "parentHash" => + block_hash() + |> to_string(), + "receiptsRoot" => "0x0", + "size" => "0x0", + "sha3Uncles" => "0x0", + "stateRoot" => "0x0", + "timestamp" => "0x0", + "totalDifficulty" => "0x0", + "transactions" => [], + "transactionsRoot" => "0x0", + "uncles" => [] + } + } + ]} + end) + |> expect(:json_rpc, fn [%{id: id, jsonrpc: "2.0", method: "trace_block", params: [^block_quantity]}], _options -> + { + :ok, + [ + %{ + id: id, + jsonrpc: "2.0", + result: [ + %{ + "action" => %{ + "author" => miner_hash_data, + "rewardType" => "external", + "value" => "0x0" + }, + "blockHash" => to_string(new_block_hash), + "blockNumber" => block_number, + "result" => nil, + "subtraces" => 0, + "traceAddress" => [], + "transactionHash" => nil, + "transactionPosition" => nil, + "type" => "reward" + } + ] + } + ] + } + end) + + assert {:ok, _} = + Fetcher.task(%Fetcher{ + blocks_batch_size: 1, + block_fetcher: %Block.Fetcher{ + callback_module: Fetcher, + json_rpc_named_arguments: json_rpc_named_arguments + } + }) + end + end end diff --git a/apps/indexer/test/indexer/block/uncataloged_rewards/importer_test.exs b/apps/indexer/test/indexer/block/uncataloged_rewards/importer_test.exs index 68987d284b..282865c5dd 100644 --- a/apps/indexer/test/indexer/block/uncataloged_rewards/importer_test.exs +++ b/apps/indexer/test/indexer/block/uncataloged_rewards/importer_test.exs @@ -97,5 +97,49 @@ defmodule Indexer.Block.UncatalogedRewards.ImporterTest do assert {:ok, [%Reward{block_hash: ^block_hash, address_type: ^address_type, reward: ^reward}]} = Importer.fetch_and_import_rewards([block]) end + + # regression test for https://github.com/poanetwork/blockscout/issues/1337 + test "with different block hash due to consensus switch between database query and trace_block" do + miner = insert(:address) + block = insert(:block, miner: miner) + block_hash = block.hash + address_type = :validator + insert(:reward, block_hash: block_hash, address_hash: miner.hash, address_type: address_type, reward: 1) + value = "0x2" + + expect(EthereumJSONRPC.Mox, :json_rpc, fn [%{id: id, method: "trace_block"}], _options -> + trace_block_block_hash = block_hash() + + refute trace_block_block_hash == block_hash + + {:ok, + [ + %{ + id: id, + result: [ + %{ + "action" => %{ + "author" => to_string(miner), + "rewardType" => "external", + "value" => value + }, + "blockHash" => to_string(trace_block_block_hash), + "blockNumber" => block.number, + "result" => nil, + "subtraces" => 0, + "traceAddress" => [], + "transactionHash" => nil, + "transactionPosition" => nil, + "type" => "reward" + } + ] + } + ]} + end) + + {:ok, reward} = Wei.cast(value) + + assert {:ok, []} = Importer.fetch_and_import_rewards([block]) + end end end