Merge branch 'master' into ab-cache-for-chart-data

pull/2182/head
Ayrat Badykov 6 years ago committed by GitHub
commit 1815fe686a
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. 48
      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

@ -26,6 +26,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
- [#2149](https://github.com/poanetwork/blockscout/pull/2149) - remove pending transaction count
### Chore

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

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

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

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

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

@ -80,7 +80,8 @@ 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",
checksum_function: System.get_env("CHECKSUM_FUNCTION") && String.to_atom(System.get_env("CHECKSUM_FUNCTION"))
config :logger, :explorer,
# keep synced with `config/config.exs`

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

@ -16,6 +16,7 @@ defmodule Explorer.Chain.Address do
DecompiledSmartContract,
Hash,
InternalTransaction,
NetVersionCache,
SmartContract,
Token,
Transaction,
@ -130,6 +131,21 @@ 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
# https://github.com/rsksmart/RSKIPs/blob/master/IPs/RSKIP60.md
def eth_checksum(hash) do
string_hash =
hash
|> to_string()
@ -137,7 +153,6 @@ 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)
@ -151,12 +166,33 @@ defmodule Explorer.Chain.Address do
{alpha, _} ->
alpha
end)
if iodata? do
["0x" | checksum_formatted]
else
to_string(["0x" | checksum_formatted])
end
def rsk_checksum(hash) do
chain_id = NetVersionCache.version()
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

@ -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
use Explorer.DataCase
import Mox
alias Explorer.Chain.Address
alias Explorer.Repo
@ -28,6 +30,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 +47,18 @@ defmodule Explorer.Chain.AddressTest do
assert str("0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb") == "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB"
assert str("0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb") == "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"
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

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

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

Loading…
Cancel
Save