From 345bec4a7a450afbf3abb80c3e671eb1b6f12328 Mon Sep 17 00:00:00 2001 From: Sebastian Abondano Date: Thu, 15 Nov 2018 14:50:42 -0500 Subject: [PATCH] fetch_beneficiaries/2 works with all reward types Why: * For the indexer to update block reward contract beneficiaries for all types (e.g. block, external, uncle, etc). For details on Parity's reward types please see: https://github.com/paritytech/parity-ethereum/blob/0f90696528e11e3e697a808e2d7983b9733d02bc/rpc/src/v1/types/trace.rs#L299-L308 * Issue link: https://github.com/poanetwork/blockscout/issues/767 --- .../lib/ethereum_jsonrpc/parity.ex | 2 +- .../test/ethereum_jsonrpc/parity_test.exs | 60 ++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/parity.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/parity.ex index 043d8191ac..8771cd3b50 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/parity.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/parity.ex @@ -74,7 +74,7 @@ defmodule EthereumJSONRPC.Parity do defp extract_beneficiaries(traces) when is_list(traces) do Enum.reduce(traces, MapSet.new(), fn - %{"action" => %{"rewardType" => "block", "author" => author}, "blockNumber" => block_number}, beneficiaries -> + %{"type" => "reward", "blockNumber" => block_number, "action" => %{"author" => author}}, beneficiaries -> beneficiary = %{ block_number: block_number, address_hash: author diff --git a/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/parity_test.exs b/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/parity_test.exs index c3e84db531..689369a63f 100644 --- a/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/parity_test.exs +++ b/apps/ethereum_jsonrpc/test/ethereum_jsonrpc/parity_test.exs @@ -294,6 +294,64 @@ defmodule EthereumJSONRPC.ParityTest do assert fetched_beneficiaries == expected_beneficiaries end + test "with 'external' 'rewardType'", %{ + json_rpc_named_arguments: json_rpc_named_arguments + } do + block_number = 5_609_295 + block_quantity = EthereumJSONRPC.integer_to_quantity(block_number) + hash1 = "0xe8ddc5c7a2d2f0d7a9798459c0104fdf5e987aca" + hash2 = "0x523b6539ff08d72a6c8bb598af95bf50c1ea839c" + + if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do + expect(EthereumJSONRPC.Mox, :json_rpc, fn %{params: [^block_quantity]}, _options -> + {:ok, + [ + %{ + "action" => %{ + "author" => hash1, + "rewardType" => "external", + "value" => "0xde0b6b3a7640000" + }, + "blockHash" => "0xf19a4ea2bb4f2d8839f4c3ec11e0e86c29d57799d7073713958fe1990e197cf5", + "blockNumber" => 5_609_295, + "result" => nil, + "subtraces" => 0, + "traceAddress" => [], + "transactionHash" => nil, + "transactionPosition" => nil, + "type" => "reward" + }, + %{ + "action" => %{ + "author" => hash2, + "rewardType" => "external", + "value" => "0xde0b6b3a7640000" + }, + "blockHash" => "0xf19a4ea2bb4f2d8839f4c3ec11e0e86c29d57799d7073713958fe1990e197cf5", + "blockNumber" => 5_609_295, + "result" => nil, + "subtraces" => 0, + "traceAddress" => [], + "transactionHash" => nil, + "transactionPosition" => nil, + "type" => "reward" + } + ]} + end) + end + + expected_beneficiaries = + MapSet.new([ + %{block_number: block_number, address_hash: hash2}, + %{block_number: block_number, address_hash: hash1} + ]) + + {:ok, fetched_beneficiaries} = + EthereumJSONRPC.Parity.fetch_beneficiaries(5_609_295..5_609_295, json_rpc_named_arguments) + + assert fetched_beneficiaries == expected_beneficiaries + end + test "with no rewards, returns {:ok, []}", %{ json_rpc_named_arguments: json_rpc_named_arguments } do @@ -309,7 +367,7 @@ defmodule EthereumJSONRPC.ParityTest do end end - test "with nil rewards, returns {:error, }", %{ + test "with nil rewards, returns {:error, reason}", %{ json_rpc_named_arguments: json_rpc_named_arguments } do if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do