Merge branch 'master' into master

pull/2183/head
maxgrapps 6 years ago committed by GitHub
commit 1b75f209c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 4
      apps/block_scout_web/test/block_scout_web/controllers/block_controller_test.exs
  3. 4
      apps/block_scout_web/test/block_scout_web/controllers/chain_controller_test.exs
  4. 4
      apps/block_scout_web/test/block_scout_web/features/viewing_chain_test.exs
  5. 13
      apps/ethereum_jsonrpc/lib/ethereum_jsonrpc.ex
  6. 12
      apps/ethereum_jsonrpc/test/ethereum_jsonrpc_test.exs
  7. 3
      apps/explorer/config/config.exs
  8. 18
      apps/explorer/lib/explorer/application.ex
  9. 70
      apps/explorer/lib/explorer/chain/address.ex
  10. 44
      apps/explorer/lib/explorer/chain/net_version_cache.ex
  11. 21
      apps/explorer/test/explorer/chain/address_test.exs
  12. 4
      apps/explorer/test/explorer/chain/blocks_cache_test.exs
  13. 4
      apps/explorer/test/support/data_case.ex

@ -27,6 +27,7 @@
- [#2123](https://github.com/poanetwork/blockscout/pull/2123) - fix coins percentage view - [#2123](https://github.com/poanetwork/blockscout/pull/2123) - fix coins percentage view
- [#2119](https://github.com/poanetwork/blockscout/pull/2119) - fix map logging - [#2119](https://github.com/poanetwork/blockscout/pull/2119) - fix map logging
- [#2130](https://github.com/poanetwork/blockscout/pull/2130) - fix navigation - [#2130](https://github.com/poanetwork/blockscout/pull/2130) - fix navigation
- [#2147](https://github.com/poanetwork/blockscout/pull/2147) - add rsk format of checksum
- [#2149](https://github.com/poanetwork/blockscout/pull/2149) - remove pending transaction count - [#2149](https://github.com/poanetwork/blockscout/pull/2149) - remove pending transaction count
### Chore ### Chore

@ -3,8 +3,8 @@ defmodule BlockScoutWeb.BlockControllerTest do
alias Explorer.Chain.Block alias Explorer.Chain.Block
setup do setup do
Supervisor.terminate_child(Explorer.Supervisor, ConCache) Supervisor.terminate_child(Explorer.Supervisor, {ConCache, :blocks})
Supervisor.restart_child(Explorer.Supervisor, ConCache) Supervisor.restart_child(Explorer.Supervisor, {ConCache, :blocks})
:ok :ok
end end

@ -9,8 +9,8 @@ defmodule BlockScoutWeb.ChainControllerTest do
alias Explorer.Counters.AddressesWithBalanceCounter alias Explorer.Counters.AddressesWithBalanceCounter
setup do setup do
Supervisor.terminate_child(Explorer.Supervisor, ConCache) Supervisor.terminate_child(Explorer.Supervisor, {ConCache, :blocks})
Supervisor.restart_child(Explorer.Supervisor, ConCache) Supervisor.restart_child(Explorer.Supervisor, {ConCache, :blocks})
start_supervised!(AddressesWithBalanceCounter) start_supervised!(AddressesWithBalanceCounter)
AddressesWithBalanceCounter.consolidate() AddressesWithBalanceCounter.consolidate()

@ -10,8 +10,8 @@ defmodule BlockScoutWeb.ViewingChainTest do
alias Explorer.Counters.AddressesWithBalanceCounter alias Explorer.Counters.AddressesWithBalanceCounter
setup do setup do
Supervisor.terminate_child(Explorer.Supervisor, ConCache) Supervisor.terminate_child(Explorer.Supervisor, {ConCache, :blocks})
Supervisor.restart_child(Explorer.Supervisor, ConCache) Supervisor.restart_child(Explorer.Supervisor, {ConCache, :blocks})
Enum.map(401..404, &insert(:block, number: &1)) Enum.map(401..404, &insert(:block, number: &1))

@ -249,6 +249,19 @@ defmodule EthereumJSONRPC do
|> fetch_blocks_by_params(&Block.ByNephew.request/1, json_rpc_named_arguments) |> fetch_blocks_by_params(&Block.ByNephew.request/1, json_rpc_named_arguments)
end 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 """ @doc """
Fetches block number by `t:tag/0`. Fetches block number by `t:tag/0`.

@ -886,6 +886,18 @@ defmodule EthereumJSONRPCTest do
end end
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 defp clear_mailbox do
receive do receive do
_ -> clear_mailbox() _ -> clear_mailbox()

@ -80,7 +80,8 @@ if System.get_env("SOURCE_MODULE") == "TransactionAndLog" do
end end
config :explorer, config :explorer,
solc_bin_api_url: "https://solc-bin.ethereum.org" solc_bin_api_url: "https://solc-bin.ethereum.org",
checksum_function: System.get_env("CHECKSUM_FUNCTION") && String.to_atom(System.get_env("CHECKSUM_FUNCTION"))
config :logger, :explorer, config :logger, :explorer,
# keep synced with `config/config.exs` # keep synced with `config/config.exs`

@ -6,7 +6,7 @@ defmodule Explorer.Application do
use Application use Application
alias Explorer.Admin alias Explorer.Admin
alias Explorer.Chain.{BlockCountCache, BlockNumberCache, BlocksCache, TransactionCountCache} alias Explorer.Chain.{BlockCountCache, BlockNumberCache, BlocksCache, NetVersionCache, TransactionCountCache}
alias Explorer.Repo.PrometheusLogger alias Explorer.Repo.PrometheusLogger
@impl Application @impl Application
@ -31,7 +31,8 @@ defmodule Explorer.Application do
{Admin.Recovery, [[], [name: Admin.Recovery]]}, {Admin.Recovery, [[], [name: Admin.Recovery]]},
{TransactionCountCache, [[], []]}, {TransactionCountCache, [[], []]},
{BlockCountCache, []}, {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() children = base_children ++ configurable_children()
@ -79,4 +80,17 @@ defmodule Explorer.Application do
http: HTTPoison http: HTTPoison
] ]
end end
defp con_cache_child_spec(name) do
Supervisor.child_spec(
{
ConCache,
[
name: name,
ttl_check_interval: false
]
},
id: {ConCache, name}
)
end
end end

@ -16,6 +16,7 @@ defmodule Explorer.Chain.Address do
DecompiledSmartContract, DecompiledSmartContract,
Hash, Hash,
InternalTransaction, InternalTransaction,
NetVersionCache,
SmartContract, SmartContract,
Token, Token,
Transaction, Transaction,
@ -130,6 +131,21 @@ defmodule Explorer.Chain.Address do
end end
def checksum(hash, iodata?) do 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
# https://github.com/rsksmart/RSKIPs/blob/master/IPs/RSKIP60.md
def eth_checksum(hash) do
string_hash = string_hash =
hash hash
|> to_string() |> to_string()
@ -137,26 +153,46 @@ defmodule Explorer.Chain.Address do
match_byte_stream = stream_every_four_bytes_of_sha256(string_hash) match_byte_stream = stream_every_four_bytes_of_sha256(string_hash)
checksum_formatted = string_hash
string_hash |> stream_binary()
|> stream_binary() |> Stream.zip(match_byte_stream)
|> Stream.zip(match_byte_stream) |> Enum.map(fn
|> Enum.map(fn {digit, _} when digit in '0123456789' ->
{digit, _} when digit in '0123456789' -> digit
digit
{alpha, 1} -> {alpha, 1} ->
alpha - 32 alpha - 32
{alpha, _} -> {alpha, _} ->
alpha alpha
end) end)
end
if iodata? do def rsk_checksum(hash) do
["0x" | checksum_formatted] chain_id = NetVersionCache.version()
else
to_string(["0x" | checksum_formatted]) string_hash =
end 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 end
defp stream_every_four_bytes_of_sha256(value) do defp stream_every_four_bytes_of_sha256(value) do

@ -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

@ -1,6 +1,8 @@
defmodule Explorer.Chain.AddressTest do defmodule Explorer.Chain.AddressTest do
use Explorer.DataCase use Explorer.DataCase
import Mox
alias Explorer.Chain.Address alias Explorer.Chain.Address
alias Explorer.Repo alias Explorer.Repo
@ -28,6 +30,12 @@ defmodule Explorer.Chain.AddressTest do
end end
describe "Phoenix.HTML.Safe.to_iodata/1" do describe "Phoenix.HTML.Safe.to_iodata/1" do
setup do
Application.put_env(:explorer, :checksum_function, :eth)
:ok
end
defp str(value) do defp str(value) do
to_string(insert(:address, hash: value)) to_string(insert(:address, hash: value))
end end
@ -39,5 +47,18 @@ defmodule Explorer.Chain.AddressTest do
assert str("0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb") == "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB" assert str("0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb") == "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"
assert str("0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb") == "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb" assert str("0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb") == "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"
end end
test "returns the checksum rsk formatted address" do
expect(EthereumJSONRPC.Mox, :json_rpc, fn _json, _options ->
{:ok, "30"}
end)
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
end end

@ -5,8 +5,8 @@ defmodule Explorer.Chain.BlocksCacheTest do
alias Explorer.Repo alias Explorer.Repo
setup do setup do
Supervisor.terminate_child(Explorer.Supervisor, ConCache) Supervisor.terminate_child(Explorer.Supervisor, {ConCache, :blocks})
Supervisor.restart_child(Explorer.Supervisor, ConCache) Supervisor.restart_child(Explorer.Supervisor, {ConCache, :blocks})
:ok :ok
end end

@ -40,8 +40,8 @@ defmodule Explorer.DataCase do
end end
Explorer.Chain.BlockNumberCache.setup() Explorer.Chain.BlockNumberCache.setup()
Supervisor.terminate_child(Explorer.Supervisor, ConCache) Supervisor.terminate_child(Explorer.Supervisor, {ConCache, :blocks})
Supervisor.restart_child(Explorer.Supervisor, ConCache) Supervisor.restart_child(Explorer.Supervisor, {ConCache, :blocks})
:ok :ok
end end

Loading…
Cancel
Save