From 271fc58ba059f9a744ab8d7543b45528c60636ee Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Wed, 12 Jun 2019 13:19:23 +0300 Subject: [PATCH 1/8] add rsk format of checksum --- apps/explorer/lib/explorer/chain/address.ex | 68 ++++++++++++++----- .../test/explorer/chain/address_test.exs | 16 +++++ 2 files changed, 67 insertions(+), 17 deletions(-) diff --git a/apps/explorer/lib/explorer/chain/address.ex b/apps/explorer/lib/explorer/chain/address.ex index a619d70546..d0711b4f8e 100644 --- a/apps/explorer/lib/explorer/chain/address.ex +++ b/apps/explorer/lib/explorer/chain/address.ex @@ -130,6 +130,20 @@ defmodule Explorer.Chain.Address do end def checksum(hash, iodata?) do + checksum_formatted = + case Application.get_env(:explorer, :checksum_function) || :eth do + :eth -> eth_checksum(hash) + :rsk -> rsk_checksum(hash) + end + + if iodata? do + ["0x" | checksum_formatted] + else + to_string(["0x" | checksum_formatted]) + end + end + + def eth_checksum(hash) do string_hash = hash |> to_string() @@ -137,26 +151,46 @@ defmodule Explorer.Chain.Address do match_byte_stream = stream_every_four_bytes_of_sha256(string_hash) - checksum_formatted = - string_hash - |> stream_binary() - |> Stream.zip(match_byte_stream) - |> Enum.map(fn - {digit, _} when digit in '0123456789' -> - digit + string_hash + |> stream_binary() + |> Stream.zip(match_byte_stream) + |> Enum.map(fn + {digit, _} when digit in '0123456789' -> + digit - {alpha, 1} -> - alpha - 32 + {alpha, 1} -> + alpha - 32 - {alpha, _} -> - alpha - end) + {alpha, _} -> + alpha + end) + end - if iodata? do - ["0x" | checksum_formatted] - else - to_string(["0x" | checksum_formatted]) - end + def rsk_checksum(hash) do + chain_id = Application.get_env(:explorer, :chain_id) + + string_hash = + hash + |> to_string() + |> String.trim_leading("0x") + + prefix = "#{chain_id}0x" + + match_byte_stream = stream_every_four_bytes_of_sha256("#{prefix}#{string_hash}") + + string_hash + |> stream_binary() + |> Stream.zip(match_byte_stream) + |> Enum.map(fn + {digit, _} when digit in '0123456789' -> + digit + + {alpha, 1} -> + alpha - 32 + + {alpha, _} -> + alpha + end) end defp stream_every_four_bytes_of_sha256(value) do diff --git a/apps/explorer/test/explorer/chain/address_test.exs b/apps/explorer/test/explorer/chain/address_test.exs index 22710e88bb..5f95d20471 100644 --- a/apps/explorer/test/explorer/chain/address_test.exs +++ b/apps/explorer/test/explorer/chain/address_test.exs @@ -28,6 +28,12 @@ defmodule Explorer.Chain.AddressTest do end describe "Phoenix.HTML.Safe.to_iodata/1" do + setup do + Application.put_env(:explorer, :checksum_function, :eth) + + :ok + end + defp str(value) do to_string(insert(:address, hash: value)) end @@ -39,5 +45,15 @@ defmodule Explorer.Chain.AddressTest do assert str("0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb") == "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB" assert str("0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb") == "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb" end + + test "returns the checksum rsk formatted address" do + Application.put_env(:explorer, :chain_id, 30) + Application.put_env(:explorer, :checksum_function, :rsk) + + assert str("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") == "0x5aaEB6053f3e94c9b9a09f33669435E7ef1bEAeD" + assert str("0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359") == "0xFb6916095cA1Df60bb79ce92cE3EA74c37c5d359" + assert str("0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb") == "0xDBF03B407c01E7CD3cBea99509D93F8Dddc8C6FB" + assert str("0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb") == "0xD1220A0Cf47c7B9BE7a2e6ba89F429762E7B9adB" + end end end From 3f3705c40e5a642af3d825de36337c1252f690c6 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Wed, 12 Jun 2019 13:55:41 +0300 Subject: [PATCH 2/8] add env vars for config --- apps/explorer/config/config.exs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/explorer/config/config.exs b/apps/explorer/config/config.exs index 94a1d79c6d..8a4f04dfca 100644 --- a/apps/explorer/config/config.exs +++ b/apps/explorer/config/config.exs @@ -80,7 +80,9 @@ if System.get_env("SOURCE_MODULE") == "TransactionAndLog" do end config :explorer, - solc_bin_api_url: "https://solc-bin.ethereum.org" + solc_bin_api_url: "https://solc-bin.ethereum.org", + chain_id: System.get_env("CHAIN_ID"), + checksum_function: System.get_env("CHECKSUM_FUNCTION") && String.to_atom(System.get_env("CHECKSUM_FUNCTION")) config :logger, :explorer, # keep synced with `config/config.exs` From 4010ff52e2837a640d10aa26fd3d17b7db14a391 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Wed, 12 Jun 2019 14:06:29 +0300 Subject: [PATCH 3/8] add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1c4814ac3..87bd35fdf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ - [#2123](https://github.com/poanetwork/blockscout/pull/2123) - fix coins percentage view - [#2119](https://github.com/poanetwork/blockscout/pull/2119) - fix map logging - [#2130](https://github.com/poanetwork/blockscout/pull/2130) - fix navigation +- [#2147](https://github.com/poanetwork/blockscout/pull/2147) - add rsk format of checksum ### Chore - [#2127](https://github.com/poanetwork/blockscout/pull/2127) - use previouse chromedriver version From 2207de931c852983b2100d50352a210dfea5e682 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 13 Jun 2019 15:50:51 +0300 Subject: [PATCH 4/8] add net_version function --- apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex | 13 +++++++++++++ .../ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex index 24fc593d85..71994da56c 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex @@ -249,6 +249,19 @@ defmodule EthereumJSONRPC do |> fetch_blocks_by_params(&Block.ByNephew.request/1, json_rpc_named_arguments) end + @spec fetch_net_version(json_rpc_named_arguments) :: {:ok, non_neg_integer()} | {:error, reason :: term} + def fetch_net_version(json_rpc_named_arguments) do + result = + %{id: 0, method: "net_version", params: []} + |> request() + |> json_rpc(json_rpc_named_arguments) + + case result do + {:ok, bin_number} -> {:ok, String.to_integer(bin_number)} + other -> other + end + end + @doc """ Fetches block number by `t:tag/0`. diff --git a/apps/ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs b/apps/ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs index 5f82b6a43f..32e1a6f324 100644 --- a/apps/ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs +++ b/apps/ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs @@ -886,6 +886,18 @@ defmodule EthereumJSONRPCTest do end end + describe "fetch_net_version/1" do + test "fetches net version", %{json_rpc_named_arguments: json_rpc_named_arguments} do + if json_rpc_named_arguments[:transport] == EthereumJSONRPC.Mox do + expect(EthereumJSONRPC.Mox, :json_rpc, fn _json, _options -> + {:ok, "1"} + end) + end + + assert {:ok, 1} = EthereumJSONRPC.fetch_net_version(json_rpc_named_arguments) + end + end + defp clear_mailbox do receive do _ -> clear_mailbox() From 43f88e364b3df1861602c7be6e6d0a309c69d909 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 13 Jun 2019 16:38:17 +0300 Subject: [PATCH 5/8] use fetched net version in rsk checksum --- apps/explorer/config/config.exs | 1 - apps/explorer/lib/explorer/application.ex | 18 +++++++- apps/explorer/lib/explorer/chain/address.ex | 3 +- .../lib/explorer/chain/net_version_cache.ex | 44 +++++++++++++++++++ .../test/explorer/chain/address_test.exs | 7 ++- 5 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 apps/explorer/lib/explorer/chain/net_version_cache.ex diff --git a/apps/explorer/config/config.exs b/apps/explorer/config/config.exs index 8a4f04dfca..9a7d9f4296 100644 --- a/apps/explorer/config/config.exs +++ b/apps/explorer/config/config.exs @@ -81,7 +81,6 @@ end config :explorer, solc_bin_api_url: "https://solc-bin.ethereum.org", - chain_id: System.get_env("CHAIN_ID"), checksum_function: System.get_env("CHECKSUM_FUNCTION") && String.to_atom(System.get_env("CHECKSUM_FUNCTION")) config :logger, :explorer, diff --git a/apps/explorer/lib/explorer/application.ex b/apps/explorer/lib/explorer/application.ex index 984695b3b4..98a71c7bac 100644 --- a/apps/explorer/lib/explorer/application.ex +++ b/apps/explorer/lib/explorer/application.ex @@ -6,7 +6,7 @@ defmodule Explorer.Application do use Application alias Explorer.Admin - alias Explorer.Chain.{BlockCountCache, BlockNumberCache, BlocksCache, TransactionCountCache} + alias Explorer.Chain.{BlockCountCache, BlockNumberCache, BlocksCache, NetVersionCache, TransactionCountCache} alias Explorer.Repo.PrometheusLogger @impl Application @@ -31,7 +31,8 @@ defmodule Explorer.Application do {Admin.Recovery, [[], [name: Admin.Recovery]]}, {TransactionCountCache, [[], []]}, {BlockCountCache, []}, - {ConCache, [name: BlocksCache.cache_name(), ttl_check_interval: false]} + con_cache_child_spec(BlocksCache.cache_name()), + con_cache_child_spec(NetVersionCache.cache_name()) ] children = base_children ++ configurable_children() @@ -79,4 +80,17 @@ defmodule Explorer.Application do http: HTTPoison ] end + + defp con_cache_child_spec(name) do + Supervisor.child_spec( + { + ConCache, + [ + name: name, + ttl_check_interval: false + ] + }, + id: {ConCache, name} + ) + end end diff --git a/apps/explorer/lib/explorer/chain/address.ex b/apps/explorer/lib/explorer/chain/address.ex index d0711b4f8e..147edf9dbe 100644 --- a/apps/explorer/lib/explorer/chain/address.ex +++ b/apps/explorer/lib/explorer/chain/address.ex @@ -15,6 +15,7 @@ defmodule Explorer.Chain.Address do Data, DecompiledSmartContract, Hash, + NetVersionCache, InternalTransaction, SmartContract, Token, @@ -167,7 +168,7 @@ defmodule Explorer.Chain.Address do end def rsk_checksum(hash) do - chain_id = Application.get_env(:explorer, :chain_id) + chain_id = NetVersionCache.version() string_hash = hash diff --git a/apps/explorer/lib/explorer/chain/net_version_cache.ex b/apps/explorer/lib/explorer/chain/net_version_cache.ex new file mode 100644 index 0000000000..c3df467e0c --- /dev/null +++ b/apps/explorer/lib/explorer/chain/net_version_cache.ex @@ -0,0 +1,44 @@ +defmodule Explorer.Chain.NetVersionCache do + @moduledoc """ + Caches chain version. + """ + + @cache_name :net_version + @key :version + + @spec version() :: non_neg_integer() | {:error, any()} + def version do + cached_value = fetch_from_cache() + + if is_nil(cached_value) do + fetch_from_node() + else + cached_value + end + end + + def cache_name do + @cache_name + end + + defp fetch_from_cache do + ConCache.get(@cache_name, @key) + end + + defp cache_value(value) do + ConCache.put(@cache_name, @key, value) + end + + defp fetch_from_node do + json_rpc_named_arguments = Application.get_env(:explorer, :json_rpc_named_arguments) + + case EthereumJSONRPC.fetch_net_version(json_rpc_named_arguments) do + {:ok, value} -> + cache_value(value) + value + + other -> + other + end + end +end diff --git a/apps/explorer/test/explorer/chain/address_test.exs b/apps/explorer/test/explorer/chain/address_test.exs index 5f95d20471..213913c766 100644 --- a/apps/explorer/test/explorer/chain/address_test.exs +++ b/apps/explorer/test/explorer/chain/address_test.exs @@ -1,6 +1,8 @@ defmodule Explorer.Chain.AddressTest do use Explorer.DataCase + import Mox + alias Explorer.Chain.Address alias Explorer.Repo @@ -47,7 +49,10 @@ defmodule Explorer.Chain.AddressTest do end test "returns the checksum rsk formatted address" do - Application.put_env(:explorer, :chain_id, 30) + expect(EthereumJSONRPC.Mox, :json_rpc, fn _json, _options -> + {:ok, "30"} + end) + Application.put_env(:explorer, :checksum_function, :rsk) assert str("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed") == "0x5aaEB6053f3e94c9b9a09f33669435E7ef1bEAeD" From 4608126cfa8db48bdc41044ca94d843648c67d9e Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 13 Jun 2019 16:43:53 +0300 Subject: [PATCH 6/8] fix credo --- apps/explorer/lib/explorer/chain/address.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/explorer/lib/explorer/chain/address.ex b/apps/explorer/lib/explorer/chain/address.ex index 147edf9dbe..d2d45ff494 100644 --- a/apps/explorer/lib/explorer/chain/address.ex +++ b/apps/explorer/lib/explorer/chain/address.ex @@ -15,8 +15,8 @@ defmodule Explorer.Chain.Address do Data, DecompiledSmartContract, Hash, - NetVersionCache, InternalTransaction, + NetVersionCache, SmartContract, Token, Transaction, From 658ff880997ab0ff6c71e2c784624530d4f9f54f Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 13 Jun 2019 16:53:11 +0300 Subject: [PATCH 7/8] fix child id --- .../block_scout_web/controllers/block_controller_test.exs | 4 ++-- .../block_scout_web/controllers/chain_controller_test.exs | 4 ++-- .../test/block_scout_web/features/viewing_chain_test.exs | 4 ++-- apps/explorer/test/explorer/chain/blocks_cache_test.exs | 4 ++-- apps/explorer/test/support/data_case.ex | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/apps/block_scout_web/test/block_scout_web/controllers/block_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/block_controller_test.exs index 9dea0841f9..e8c4af7de8 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/block_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/block_controller_test.exs @@ -3,8 +3,8 @@ defmodule BlockScoutWeb.BlockControllerTest do alias Explorer.Chain.Block setup do - Supervisor.terminate_child(Explorer.Supervisor, ConCache) - Supervisor.restart_child(Explorer.Supervisor, ConCache) + Supervisor.terminate_child(Explorer.Supervisor, {ConCache, :blocks}) + Supervisor.restart_child(Explorer.Supervisor, {ConCache, :blocks}) :ok end diff --git a/apps/block_scout_web/test/block_scout_web/controllers/chain_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/chain_controller_test.exs index 701a991c55..f902f15ad4 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/chain_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/chain_controller_test.exs @@ -9,8 +9,8 @@ defmodule BlockScoutWeb.ChainControllerTest do alias Explorer.Counters.AddressesWithBalanceCounter setup do - Supervisor.terminate_child(Explorer.Supervisor, ConCache) - Supervisor.restart_child(Explorer.Supervisor, ConCache) + Supervisor.terminate_child(Explorer.Supervisor, {ConCache, :blocks}) + Supervisor.restart_child(Explorer.Supervisor, {ConCache, :blocks}) start_supervised!(AddressesWithBalanceCounter) AddressesWithBalanceCounter.consolidate() diff --git a/apps/block_scout_web/test/block_scout_web/features/viewing_chain_test.exs b/apps/block_scout_web/test/block_scout_web/features/viewing_chain_test.exs index 1add7ed9d5..a32e4f5da3 100644 --- a/apps/block_scout_web/test/block_scout_web/features/viewing_chain_test.exs +++ b/apps/block_scout_web/test/block_scout_web/features/viewing_chain_test.exs @@ -10,8 +10,8 @@ defmodule BlockScoutWeb.ViewingChainTest do alias Explorer.Counters.AddressesWithBalanceCounter setup do - Supervisor.terminate_child(Explorer.Supervisor, ConCache) - Supervisor.restart_child(Explorer.Supervisor, ConCache) + Supervisor.terminate_child(Explorer.Supervisor, {ConCache, :blocks}) + Supervisor.restart_child(Explorer.Supervisor, {ConCache, :blocks}) Enum.map(401..404, &insert(:block, number: &1)) diff --git a/apps/explorer/test/explorer/chain/blocks_cache_test.exs b/apps/explorer/test/explorer/chain/blocks_cache_test.exs index f030502dc3..687d2ff30a 100644 --- a/apps/explorer/test/explorer/chain/blocks_cache_test.exs +++ b/apps/explorer/test/explorer/chain/blocks_cache_test.exs @@ -5,8 +5,8 @@ defmodule Explorer.Chain.BlocksCacheTest do alias Explorer.Repo setup do - Supervisor.terminate_child(Explorer.Supervisor, ConCache) - Supervisor.restart_child(Explorer.Supervisor, ConCache) + Supervisor.terminate_child(Explorer.Supervisor, {ConCache, :blocks}) + Supervisor.restart_child(Explorer.Supervisor, {ConCache, :blocks}) :ok end diff --git a/apps/explorer/test/support/data_case.ex b/apps/explorer/test/support/data_case.ex index ebaf91cf3d..1386a55d36 100644 --- a/apps/explorer/test/support/data_case.ex +++ b/apps/explorer/test/support/data_case.ex @@ -40,8 +40,8 @@ defmodule Explorer.DataCase do end Explorer.Chain.BlockNumberCache.setup() - Supervisor.terminate_child(Explorer.Supervisor, ConCache) - Supervisor.restart_child(Explorer.Supervisor, ConCache) + Supervisor.terminate_child(Explorer.Supervisor, {ConCache, :blocks}) + Supervisor.restart_child(Explorer.Supervisor, {ConCache, :blocks}) :ok end From 270b96fc1e4795200fe49f099953f7f687036b82 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 17 Jun 2019 11:45:05 +0300 Subject: [PATCH 8/8] add comment with rskip --- apps/explorer/lib/explorer/chain/address.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/explorer/lib/explorer/chain/address.ex b/apps/explorer/lib/explorer/chain/address.ex index d2d45ff494..28b67f1d66 100644 --- a/apps/explorer/lib/explorer/chain/address.ex +++ b/apps/explorer/lib/explorer/chain/address.ex @@ -144,6 +144,7 @@ defmodule Explorer.Chain.Address do end end + # https://github.com/rsksmart/RSKIPs/blob/master/IPs/RSKIP60.md def eth_checksum(hash) do string_hash = hash