Merge branch 'master' into use-internal-transaction-init-for-contract-code

pull/1954/head
Zach Daniel 6 years ago committed by GitHub
commit 75918c07b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      CHANGELOG.md
  2. 53
      apps/block_scout_web/assets/css/theme/_rinkeby_variables.scss
  3. 37
      apps/block_scout_web/assets/static/images/rinkeby_logo.svg
  4. 8
      apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/block_controller.ex
  5. 52
      apps/block_scout_web/lib/block_scout_web/etherscan.ex
  6. 28
      apps/block_scout_web/lib/block_scout_web/views/api/rpc/block_view.ex
  7. 33
      apps/block_scout_web/lib/block_scout_web/views/api/rpc/eth_rpc_view.ex
  8. 1
      apps/block_scout_web/test/block_scout_web/controllers/api/rpc/address_controller_test.exs
  9. 2
      apps/explorer/config/config.exs
  10. 2
      apps/explorer/config/test.exs
  11. 57
      apps/explorer/lib/explorer/chain/block_number_cache.ex
  12. 64
      apps/explorer/test/explorer/chain/block_number_cache_test.exs
  13. 2
      apps/explorer/test/support/data_case.ex
  14. 14
      apps/indexer/lib/indexer/block/fetcher.ex

@ -1,6 +1,7 @@
## Current
### Features
- [#1963](https://github.com/poanetwork/blockscout/pull/1963) - added rinkeby theme and rinkeby logo
- [#1959](https://github.com/poanetwork/blockscout/pull/1959) - added goerli theme and goerli logo
- [#1928](https://github.com/poanetwork/blockscout/pull/1928) - pagination styles were updated
- [#1948](https://github.com/poanetwork/blockscout/pull/1948) - added ropsten theme and ropsten logo
@ -21,6 +22,7 @@
- [#1920](https://github.com/poanetwork/blockscout/pull/1920) - fix: remove source code fields from list endpoint
- [#1876](https://github.com/poanetwork/blockscout/pull/1876) - async calculate a count of blocks
- [#1941](https://github.com/poanetwork/blockscout/pull/1941) - feat: add on demand fetching and stale attr to rpc
- [#1933](https://github.com/poanetwork/blockscout/pull/1933) - add eth_BlockNumber json rpc method
- [#1952](https://github.com/poanetwork/blockscout/pull/1952) - feat: exclude empty contracts by default
- [#1954](https://github.com/poanetwork/blockscout/pull/1954) - feat: use creation init on self destruct

@ -1,3 +1,50 @@
$primary: #2a3f54;
$secondary: #39c4a9;
$tertiary: #39c4a9;
// general
$primary: #193653;
$secondary: #49a2ee;
$tertiary: #41c3a9;
$additional-font: #a3ceff;
// footer
$footer-background-color: $primary;
$footer-title-color: #fff;
$footer-text-color: $additional-font;
$footer-item-disc-color: $additional-font;
.footer-logo { filter: brightness(0) invert(1); }
// dashboard
$dashboard-line-color-price: $tertiary; // price left border
$dashboard-banner-chart-legend-value-color: $additional-font; // chart labels
$dashboard-stats-item-value-color: $additional-font; // stat values
$dashboard-stats-item-border-color: $tertiary; // stat border
$dashboard-banner-gradient-start: $primary; // gradient begin
$dashboard-banner-gradient-end: lighten($primary, 5); // gradient end
$dashboard-banner-network-plain-container-background-color: #244468; // stats bg
// navigation
.navbar { box-shadow: 0px 0px 30px 0px rgba(21, 53, 80, 0.12); } // header shadow
$header-icon-border-color-hover: $tertiary; // top border on hover
$header-icon-color-hover: $tertiary; // nav icon on hover
.dropdown-item:hover, .dropdown-item:focus { background-color: $tertiary !important; } // dropdown item on hover
// buttons
$btn-line-bg: #fff; // button bg
$btn-line-color: $tertiary; // button border and font color && hover bg color
$btn-copy-color: $tertiary; // btn copy
$btn-qr-color: $tertiary; // btn qr-code
//links & tile
.tile a { color: $tertiary !important; } // links color for badges
.tile-type-block {
border-left: 4px solid $tertiary;
} // tab active bg
// card
$card-background-1: $secondary;
$card-tab-active: $secondary;

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 8.5 KiB

After

Width:  |  Height:  |  Size: 12 KiB

@ -3,6 +3,7 @@ defmodule BlockScoutWeb.API.RPC.BlockController do
alias BlockScoutWeb.Chain, as: ChainWeb
alias Explorer.Chain
alias Explorer.Chain.BlockNumberCache
def getblockreward(conn, params) do
with {:block_param, {:ok, unsafe_block_number}} <- {:block_param, Map.fetch(params, "blockno")},
@ -23,4 +24,11 @@ defmodule BlockScoutWeb.API.RPC.BlockController do
render(conn, :error, error: "Block does not exist")
end
end
def eth_block_number(conn, params) do
id = Map.get(params, "id", 1)
max_block_number = BlockNumberCache.max_number()
render(conn, :eth_block_number, number: max_block_number, id: id)
end
end

@ -279,6 +279,12 @@ defmodule BlockScoutWeb.Etherscan do
"result" => nil
}
@block_eth_block_number_example_value %{
"jsonrpc" => "2.0",
"result" => "767969",
"id" => 1
}
@contract_listcontracts_example_value %{
"status" => "1",
"message" => "OK",
@ -476,11 +482,26 @@ defmodule BlockScoutWeb.Etherscan do
enum_interpretation: %{"0" => "error", "1" => "ok"}
}
@jsonrpc_version_type %{
type: "string",
example: ~s("2.0")
}
@message_type %{
type: "string",
example: ~s("OK")
}
@hex_number_type %{
type: "string",
example: ~s("767969")
}
@id_type %{
type: "string",
example: ~s("1")
}
@wei_type %{
type: "wei",
definition: &__MODULE__.wei_type_definition/1,
@ -1737,6 +1758,35 @@ defmodule BlockScoutWeb.Etherscan do
]
}
@block_eth_block_number_action %{
name: "eth_block_number",
description: "Mimics Ethereum JSON RPC's eth_blockNumber. Returns the lastest block number",
required_params: [],
optional_params: [
%{
key: "id",
placeholder: "request id",
type: "integer",
description: "A nonnegative integer that represents the json rpc request id."
}
],
responses: [
%{
code: "200",
description: "successful request",
example_value: Jason.encode!(@block_eth_block_number_example_value),
model: %{
name: "Result",
fields: %{
jsonrpc: @jsonrpc_version_type,
id: @id_type,
result: @hex_number_type
}
}
}
]
}
@block_getblockreward_action %{
name: "getblockreward",
description: "Get block reward by block number.",
@ -2171,7 +2221,7 @@ defmodule BlockScoutWeb.Etherscan do
@block_module %{
name: "block",
actions: [@block_getblockreward_action]
actions: [@block_getblockreward_action, @block_eth_block_number_action]
}
@contract_module %{

@ -1,7 +1,7 @@
defmodule BlockScoutWeb.API.RPC.BlockView do
use BlockScoutWeb, :view
alias BlockScoutWeb.API.RPC.RPCView
alias BlockScoutWeb.API.RPC.{EthRPCView, RPCView}
alias Explorer.Chain.{Hash, Wei}
def render("block_reward.json", %{block: block, reward: reward}) do
@ -22,7 +22,33 @@ defmodule BlockScoutWeb.API.RPC.BlockView do
RPCView.render("show.json", data: data)
end
def render("eth_block_number.json", %{number: number, id: id}) do
result = encode_quantity(number)
EthRPCView.render("show.json", %{result: result, id: id})
end
def render("error.json", %{error: error}) do
RPCView.render("error.json", error: error)
end
defp encode_quantity(binary) when is_binary(binary) do
hex_binary = Base.encode16(binary, case: :lower)
result = String.replace_leading(hex_binary, "0", "")
final_result = if result == "", do: "0", else: result
"0x#{final_result}"
end
defp encode_quantity(value) when is_integer(value) do
value
|> :binary.encode_unsigned()
|> encode_quantity()
end
defp encode_quantity(value) when is_nil(value) do
nil
end
end

@ -0,0 +1,33 @@
defmodule BlockScoutWeb.API.RPC.EthRPCView do
use BlockScoutWeb, :view
defstruct [:result, :id, :error]
def render("show.json", %{result: result, id: id}) do
%__MODULE__{
result: result,
id: id
}
end
def render("error.json", %{error: message, id: id}) do
%__MODULE__{
error: message,
id: id
}
end
defimpl Poison.Encoder, for: BlockScoutWeb.API.RPC.EthRPCView do
def encode(%BlockScoutWeb.API.RPC.EthRPCView{result: result, id: id, error: error}, _options) when is_nil(error) do
"""
{"jsonrpc":"2.0","result":"#{result}","id":#{id}}
"""
end
def encode(%BlockScoutWeb.API.RPC.EthRPCView{id: id, error: error}, _options) do
"""
{"jsonrpc":"2.0","error": #{error},"id": #{id}}
"""
end
end
end

@ -25,7 +25,6 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do
start_supervised!(AddressesWithBalanceCounter)
Application.put_env(:explorer, AverageBlockTime, enabled: true)
BlockNumberCache.setup(cache_period: 0)
on_exit(fn ->
Application.put_env(:explorer, AverageBlockTime, enabled: false)

@ -13,6 +13,8 @@ config :explorer,
config :explorer, Explorer.Counters.AverageBlockTime, enabled: true
config :explorer, Explorer.Chain.BlockNumberCache, enabled: true
config :explorer, Explorer.ExchangeRates.Source.CoinMarketCap,
pages: String.to_integer(System.get_env("COINMARKETCAP_PAGES") || "10")

@ -13,6 +13,8 @@ config :explorer, Explorer.Repo,
config :explorer, Explorer.ExchangeRates, enabled: false, store: :ets
config :explorer, Explorer.Chain.BlockNumberCache, enabled: false
config :explorer, Explorer.KnownTokens, enabled: false, store: :ets
config :explorer, Explorer.Counters.AverageBlockTime, enabled: false

@ -6,13 +6,10 @@ defmodule Explorer.Chain.BlockNumberCache do
alias Explorer.Chain
@tab :block_number_cache
# 30 minutes
@cache_period 1_000 * 60 * 30
@key "min_max"
@opts_key "opts"
@spec setup(Keyword.t()) :: :ok
def setup(opts \\ []) do
@spec setup() :: :ok
def setup do
if :ets.whereis(@tab) == :undefined do
:ets.new(@tab, [
:set,
@ -22,7 +19,6 @@ defmodule Explorer.Chain.BlockNumberCache do
])
end
setup_opts(opts)
update_cache()
:ok
@ -41,15 +37,11 @@ defmodule Explorer.Chain.BlockNumberCache do
end
defp value(type) do
initial_cache = {_min, _max, old_current_time} = cached_values()
{min, max, _current_time} =
if current_time() - old_current_time > cache_period() do
update_cache()
{min, max} =
if Application.get_env(:explorer, __MODULE__)[:enabled] do
cached_values()
else
initial_cache
min_and_max_from_db()
end
case type do
@ -59,18 +51,29 @@ defmodule Explorer.Chain.BlockNumberCache do
end
end
defp update_cache do
current_time = current_time()
{min, max} = min_and_max_from_db()
tuple = {min, max, current_time}
@spec update(non_neg_integer()) :: boolean()
def update(number) do
{old_min, old_max} = cached_values()
:ets.insert(@tab, {@key, tuple})
cond do
number > old_max ->
tuple = {old_min, number}
:ets.insert(@tab, {@key, tuple})
number < old_min ->
tuple = {number, old_max}
:ets.insert(@tab, {@key, tuple})
true ->
false
end
end
defp setup_opts(opts) do
cache_period = opts[:cache_period] || @cache_period
defp update_cache do
{min, max} = min_and_max_from_db()
tuple = {min, max}
:ets.insert(@tab, {@opts_key, cache_period})
:ets.insert(@tab, {@key, tuple})
end
defp cached_values do
@ -79,22 +82,10 @@ defmodule Explorer.Chain.BlockNumberCache do
cached_values
end
defp cache_period do
[{_, cache_period}] = :ets.lookup(@tab, @opts_key)
cache_period
end
defp min_and_max_from_db do
Chain.fetch_min_and_max_block_numbers()
rescue
_e ->
{0, 0}
end
defp current_time do
utc_now = DateTime.utc_now()
DateTime.to_unix(utc_now, :millisecond)
end
end

@ -3,6 +3,14 @@ defmodule Explorer.Chain.BlockNumberCacheTest do
alias Explorer.Chain.BlockNumberCache
setup do
Application.put_env(:explorer, Explorer.Chain.BlockNumberCache, enabled: true)
on_exit(fn ->
Application.put_env(:explorer, Explorer.Chain.BlockNumberCache, enabled: false)
end)
end
describe "max_number/1" do
test "returns max number" do
insert(:block, number: 5)
@ -11,33 +19,6 @@ defmodule Explorer.Chain.BlockNumberCacheTest do
assert BlockNumberCache.max_number() == 5
end
test "invalidates cache if period did pass" do
insert(:block, number: 5)
BlockNumberCache.setup(cache_period: 2_000)
assert BlockNumberCache.max_number() == 5
insert(:block, number: 10)
Process.sleep(2_000)
assert BlockNumberCache.max_number() == 10
assert BlockNumberCache.min_number() == 5
end
test "does not invalidate cache if period time did not pass" do
insert(:block, number: 5)
BlockNumberCache.setup(cache_period: 10_000)
assert BlockNumberCache.max_number() == 5
insert(:block, number: 10)
assert BlockNumberCache.max_number() == 5
end
end
describe "min_number/1" do
@ -48,32 +29,31 @@ defmodule Explorer.Chain.BlockNumberCacheTest do
assert BlockNumberCache.max_number() == 2
end
end
test "invalidates cache" do
insert(:block, number: 5)
BlockNumberCache.setup(cache_period: 2_000)
describe "update/1" do
test "updates max number" do
insert(:block, number: 2)
assert BlockNumberCache.min_number() == 5
BlockNumberCache.setup()
insert(:block, number: 2)
assert BlockNumberCache.max_number() == 2
Process.sleep(2_000)
assert BlockNumberCache.update(3)
assert BlockNumberCache.min_number() == 2
assert BlockNumberCache.max_number() == 5
assert BlockNumberCache.max_number() == 3
end
test "does not invalidate cache if period time did not pass" do
insert(:block, number: 5)
test "updates min number" do
insert(:block, number: 2)
BlockNumberCache.setup(cache_period: 10_000)
BlockNumberCache.setup()
assert BlockNumberCache.max_number() == 5
assert BlockNumberCache.min_number() == 2
insert(:block, number: 2)
assert BlockNumberCache.update(1)
assert BlockNumberCache.max_number() == 5
assert BlockNumberCache.min_number() == 1
end
end
end

@ -39,7 +39,7 @@ defmodule Explorer.DataCase do
Ecto.Adapters.SQL.Sandbox.mode(Explorer.Repo, {:shared, self()})
end
Explorer.Chain.BlockNumberCache.setup(cache_period: 0)
Explorer.Chain.BlockNumberCache.setup()
:ok
end

@ -11,7 +11,7 @@ defmodule Indexer.Block.Fetcher do
alias EthereumJSONRPC.{Blocks, FetchedBeneficiaries}
alias Explorer.Chain
alias Explorer.Chain.{Address, Block, Hash, Import, Transaction}
alias Explorer.Chain.{Address, Block, BlockNumberCache, Hash, Import, Transaction}
alias Indexer.Block.Fetcher.Receipts
alias Indexer.Fetcher.{
@ -171,13 +171,23 @@ defmodule Indexer.Block.Fetcher do
transactions: %{params: transactions_with_receipts}
}
) do
{:ok, %{inserted: inserted, errors: blocks_errors}}
result = {:ok, %{inserted: inserted, errors: blocks_errors}}
update_block_cache(inserted[:blocks])
result
else
{step, {:error, reason}} -> {:error, {step, reason}}
{:import, {:error, step, failed_value, changes_so_far}} -> {:error, {step, failed_value, changes_so_far}}
end
end
defp update_block_cache(blocks) do
max_block = Enum.max_by(blocks, fn block -> block.number end)
min_block = Enum.min_by(blocks, fn block -> block.number end)
BlockNumberCache.update(max_block.number)
BlockNumberCache.update(min_block.number)
end
def import(
%__MODULE__{broadcast: broadcast, callback_module: callback_module} = state,
options

Loading…
Cancel
Save