Merge pull request #3044 from poanetwork/vb-prevent-devizion-by-zero

Prevent division by zero on /accounts page
pull/3046/head
Victor Baranov 5 years ago committed by GitHub
commit 97dd01ec1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 5
      apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex
  3. 4
      apps/block_scout_web/lib/block_scout_web/templates/address/_tile.html.eex
  4. 4
      apps/block_scout_web/lib/block_scout_web/templates/address/index.html.eex
  5. 10
      apps/block_scout_web/lib/block_scout_web/templates/common_components/_table-loader.html.eex
  6. 10
      apps/block_scout_web/lib/block_scout_web/views/address_view.ex
  7. 4
      apps/block_scout_web/lib/block_scout_web/views/common_components_view.ex
  8. 26
      apps/block_scout_web/priv/gettext/default.pot
  9. 26
      apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po
  10. 20
      apps/block_scout_web/test/block_scout_web/views/address_view_test.exs

@ -3,6 +3,7 @@
### Features ### Features
### Fixes ### Fixes
- [#3044](https://github.com/poanetwork/blockscout/pull/3044) - Prevent division by zero on /accounts page
- [#3043](https://github.com/poanetwork/blockscout/pull/3043) - Extract host name for split couple of indexer and web app - [#3043](https://github.com/poanetwork/blockscout/pull/3043) - Extract host name for split couple of indexer and web app
- [#3042](https://github.com/poanetwork/blockscout/pull/3042) - Speedup pending txs list query - [#3042](https://github.com/poanetwork/blockscout/pull/3042) - Speedup pending txs list query
- [#2944](https://github.com/poanetwork/blockscout/pull/2944) - Split js logic into multiple files - [#2944](https://github.com/poanetwork/blockscout/pull/2944) - Split js logic into multiple files

@ -57,9 +57,12 @@ defmodule BlockScoutWeb.AddressController do
end end
def index(conn, _params) do def index(conn, _params) do
total_supply = Chain.total_supply()
render(conn, "index.html", render(conn, "index.html",
current_path: current_path(conn), current_path: current_path(conn),
address_count: Chain.address_estimated_count() address_count: Chain.address_estimated_count(),
total_supply: total_supply
) )
end end

@ -18,12 +18,10 @@
<% end %> <% end %>
</span> </span>
</td> </td>
<%= if balance_percentage_enabled?() do %> <%= if balance_percentage_enabled?(@total_supply) do %>
<td class="stakes-td color-lighten"> <td class="stakes-td color-lighten">
<!-- percentage of coins from total supply --> <!-- percentage of coins from total supply -->
<%= if @total_supply do %>
<%= balance_percentage(@address, @total_supply) %> <%= balance_percentage(@address, @total_supply) %>
<% end %>
</td> </td>
<% end %> <% end %>
<td class="stakes-td"> <td class="stakes-td">

@ -25,7 +25,7 @@
Balance Balance
</div> </div>
</th> </th>
<%= if balance_percentage_enabled?() do %> <%= if balance_percentage_enabled?(@total_supply) do %>
<th class="stakes-table-th"> <th class="stakes-table-th">
<div class="stakes-table-th-content"> <div class="stakes-table-th-content">
Percentage Percentage
@ -40,7 +40,7 @@
</tr> </tr>
</thead> </thead>
<tbody data-items data-selector="top-addresses-list"> <tbody data-items data-selector="top-addresses-list">
<%= render BlockScoutWeb.CommonComponentsView, "_table-loader.html" %> <%= render BlockScoutWeb.CommonComponentsView, "_table-loader.html", total_supply: @total_supply %>
</tbody> </tbody>
</table> </table>
</div> </div>

@ -8,7 +8,7 @@
<td class="stakes-td"> <td class="stakes-td">
<span class="table-content-loader"></span> <span class="table-content-loader"></span>
</td> </td>
<%= if balance_percentage_enabled?() do %> <%= if balance_percentage_enabled?(@total_supply) do %>
<td class="stakes-td"> <td class="stakes-td">
<span class="table-content-loader"></span> <span class="table-content-loader"></span>
</td> </td>
@ -27,7 +27,7 @@
<td class="stakes-td"> <td class="stakes-td">
<span class="table-content-loader"></span> <span class="table-content-loader"></span>
</td> </td>
<%= if balance_percentage_enabled?() do %> <%= if balance_percentage_enabled?(@total_supply) do %>
<td class="stakes-td"> <td class="stakes-td">
<span class="table-content-loader"></span> <span class="table-content-loader"></span>
</td> </td>
@ -46,7 +46,7 @@
<td class="stakes-td"> <td class="stakes-td">
<span class="table-content-loader"></span> <span class="table-content-loader"></span>
</td> </td>
<%= if balance_percentage_enabled?() do %> <%= if balance_percentage_enabled?(@total_supply) do %>
<td class="stakes-td"> <td class="stakes-td">
<span class="table-content-loader"></span> <span class="table-content-loader"></span>
</td> </td>
@ -65,7 +65,7 @@
<td class="stakes-td"> <td class="stakes-td">
<span class="table-content-loader"></span> <span class="table-content-loader"></span>
</td> </td>
<%= if balance_percentage_enabled?() do %> <%= if balance_percentage_enabled?(@total_supply) do %>
<td class="stakes-td"> <td class="stakes-td">
<span class="table-content-loader"></span> <span class="table-content-loader"></span>
</td> </td>
@ -84,7 +84,7 @@
<td class="stakes-td"> <td class="stakes-td">
<span class="table-content-loader"></span> <span class="table-content-loader"></span>
</td> </td>
<%= if balance_percentage_enabled?() do %> <%= if balance_percentage_enabled?(@total_supply) do %>
<td class="stakes-td"> <td class="stakes-td">
<span class="table-content-loader"></span> <span class="table-content-loader"></span>
</td> </td>

@ -110,13 +110,14 @@ defmodule BlockScoutWeb.AddressView do
format_wei_value(balance, :ether) format_wei_value(balance, :ether)
end end
def balance_percentage_enabled? do def balance_percentage_enabled?(total_supply) do
Application.get_env(:block_scout_web, :show_percentage) Application.get_env(:block_scout_web, :show_percentage) && total_supply > 0
end end
def balance_percentage(_, nil), do: "" def balance_percentage(_, nil), do: ""
def balance_percentage(%Address{fetched_coin_balance: balance}, total_supply) do def balance_percentage(%Address{fetched_coin_balance: balance}, total_supply) do
if total_supply > 0 do
balance balance
|> Wei.to(:ether) |> Wei.to(:ether)
|> Decimal.div(Decimal.new(total_supply)) |> Decimal.div(Decimal.new(total_supply))
@ -124,6 +125,11 @@ defmodule BlockScoutWeb.AddressView do
|> Decimal.round(4) |> Decimal.round(4)
|> Decimal.to_string(:normal) |> Decimal.to_string(:normal)
|> Kernel.<>("% #{gettext("Market Cap")}") |> Kernel.<>("% #{gettext("Market Cap")}")
else
balance
|> Wei.to(:ether)
|> Decimal.to_string(:normal)
end
end end
def empty_exchange_rate?(exchange_rate) do def empty_exchange_rate?(exchange_rate) do

@ -1,7 +1,7 @@
defmodule BlockScoutWeb.CommonComponentsView do defmodule BlockScoutWeb.CommonComponentsView do
use BlockScoutWeb, :view use BlockScoutWeb, :view
def balance_percentage_enabled? do def balance_percentage_enabled?(total_supply) do
Application.get_env(:block_scout_web, :show_percentage) Application.get_env(:block_scout_web, :show_percentage) && total_supply > 0
end end
end end

@ -897,7 +897,7 @@ msgstr ""
#: lib/block_scout_web/templates/tokens/transfer/index.html.eex:14 #: lib/block_scout_web/templates/tokens/transfer/index.html.eex:14
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:4 #: lib/block_scout_web/templates/transaction/_tabs.html.eex:4
#: lib/block_scout_web/templates/transaction_token_transfer/index.html.eex:7 #: lib/block_scout_web/templates/transaction_token_transfer/index.html.eex:7
#: lib/block_scout_web/views/address_view.ex:308 #: lib/block_scout_web/views/address_view.ex:314
#: lib/block_scout_web/views/tokens/instance/overview_view.ex:90 #: lib/block_scout_web/views/tokens/instance/overview_view.ex:90
#: lib/block_scout_web/views/tokens/overview_view.ex:35 #: lib/block_scout_web/views/tokens/overview_view.ex:35
#: lib/block_scout_web/views/transaction_view.ex:314 #: lib/block_scout_web/views/transaction_view.ex:314
@ -1035,8 +1035,8 @@ msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:24 #: lib/block_scout_web/templates/chain/show.html.eex:24
#: lib/block_scout_web/templates/layout/app.html.eex:30 #: lib/block_scout_web/templates/layout/app.html.eex:30
#: lib/block_scout_web/views/address_view.ex:126 #: lib/block_scout_web/views/address_view.ex:127
#: lib/block_scout_web/views/address_view.ex:126 #: lib/block_scout_web/views/address_view.ex:127
msgid "Market Cap" msgid "Market Cap"
msgstr "" msgstr ""
@ -1488,7 +1488,7 @@ msgid "Transaction Speed"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/address/_tile.html.eex:33 #: lib/block_scout_web/templates/address/_tile.html.eex:31
msgid "Transactions sent" msgid "Transactions sent"
msgstr "" msgstr ""
@ -1802,7 +1802,7 @@ msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/address/_tabs.html.eex:37 #: lib/block_scout_web/templates/address/_tabs.html.eex:37
#: lib/block_scout_web/templates/address_validation/index.html.eex:13 #: lib/block_scout_web/templates/address_validation/index.html.eex:13
#: lib/block_scout_web/views/address_view.ex:313 #: lib/block_scout_web/views/address_view.ex:319
msgid "Blocks Validated" msgid "Blocks Validated"
msgstr "" msgstr ""
@ -1812,18 +1812,18 @@ msgstr ""
#: lib/block_scout_web/templates/api_docs/_action_tile.html.eex:187 #: lib/block_scout_web/templates/api_docs/_action_tile.html.eex:187
#: lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex:126 #: lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex:126
#: lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex:149 #: lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex:149
#: lib/block_scout_web/views/address_view.ex:309 #: lib/block_scout_web/views/address_view.ex:315
msgid "Code" msgid "Code"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/address/_tabs.html.eex:26 #: lib/block_scout_web/templates/address/_tabs.html.eex:26
#: lib/block_scout_web/views/address_view.ex:312 #: lib/block_scout_web/views/address_view.ex:318
msgid "Coin Balance History" msgid "Coin Balance History"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/views/address_view.ex:310 #: lib/block_scout_web/views/address_view.ex:316
msgid "Decompiled Code" msgid "Decompiled Code"
msgstr "" msgstr ""
@ -1832,7 +1832,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_internal_transaction/index.html.eex:19 #: lib/block_scout_web/templates/address_internal_transaction/index.html.eex:19
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:11 #: lib/block_scout_web/templates/transaction/_tabs.html.eex:11
#: lib/block_scout_web/templates/transaction_internal_transaction/index.html.eex:6 #: lib/block_scout_web/templates/transaction_internal_transaction/index.html.eex:6
#: lib/block_scout_web/views/address_view.ex:307 #: lib/block_scout_web/views/address_view.ex:313
#: lib/block_scout_web/views/transaction_view.ex:315 #: lib/block_scout_web/views/transaction_view.ex:315
msgid "Internal Transactions" msgid "Internal Transactions"
msgstr "" msgstr ""
@ -1842,7 +1842,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_logs/index.html.eex:8 #: lib/block_scout_web/templates/address_logs/index.html.eex:8
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:17 #: lib/block_scout_web/templates/transaction/_tabs.html.eex:17
#: lib/block_scout_web/templates/transaction_log/index.html.eex:8 #: lib/block_scout_web/templates/transaction_log/index.html.eex:8
#: lib/block_scout_web/views/address_view.ex:314 #: lib/block_scout_web/views/address_view.ex:320
#: lib/block_scout_web/views/transaction_view.ex:316 #: lib/block_scout_web/views/transaction_view.ex:316
msgid "Logs" msgid "Logs"
msgstr "" msgstr ""
@ -1850,7 +1850,7 @@ msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/address/_tabs.html.eex:62 #: lib/block_scout_web/templates/address/_tabs.html.eex:62
#: lib/block_scout_web/templates/tokens/overview/_tabs.html.eex:25 #: lib/block_scout_web/templates/tokens/overview/_tabs.html.eex:25
#: lib/block_scout_web/views/address_view.ex:311 #: lib/block_scout_web/views/address_view.ex:317
#: lib/block_scout_web/views/tokens/overview_view.ex:37 #: lib/block_scout_web/views/tokens/overview_view.ex:37
msgid "Read Contract" msgid "Read Contract"
msgstr "" msgstr ""
@ -1859,7 +1859,7 @@ msgstr ""
#: lib/block_scout_web/templates/address/_tabs.html.eex:14 #: lib/block_scout_web/templates/address/_tabs.html.eex:14
#: lib/block_scout_web/templates/address_token/index.html.eex:8 #: lib/block_scout_web/templates/address_token/index.html.eex:8
#: lib/block_scout_web/templates/address_token_transfer/index.html.eex:11 #: lib/block_scout_web/templates/address_token_transfer/index.html.eex:11
#: lib/block_scout_web/views/address_view.ex:305 #: lib/block_scout_web/views/address_view.ex:311
msgid "Tokens" msgid "Tokens"
msgstr "" msgstr ""
@ -1871,6 +1871,6 @@ msgstr ""
#: lib/block_scout_web/templates/block_transaction/index.html.eex:18 #: lib/block_scout_web/templates/block_transaction/index.html.eex:18
#: lib/block_scout_web/templates/chain/show.html.eex:140 #: lib/block_scout_web/templates/chain/show.html.eex:140
#: lib/block_scout_web/templates/layout/_topnav.html.eex:50 #: lib/block_scout_web/templates/layout/_topnav.html.eex:50
#: lib/block_scout_web/views/address_view.ex:306 #: lib/block_scout_web/views/address_view.ex:312
msgid "Transactions" msgid "Transactions"
msgstr "" msgstr ""

@ -897,7 +897,7 @@ msgstr ""
#: lib/block_scout_web/templates/tokens/transfer/index.html.eex:14 #: lib/block_scout_web/templates/tokens/transfer/index.html.eex:14
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:4 #: lib/block_scout_web/templates/transaction/_tabs.html.eex:4
#: lib/block_scout_web/templates/transaction_token_transfer/index.html.eex:7 #: lib/block_scout_web/templates/transaction_token_transfer/index.html.eex:7
#: lib/block_scout_web/views/address_view.ex:308 #: lib/block_scout_web/views/address_view.ex:314
#: lib/block_scout_web/views/tokens/instance/overview_view.ex:90 #: lib/block_scout_web/views/tokens/instance/overview_view.ex:90
#: lib/block_scout_web/views/tokens/overview_view.ex:35 #: lib/block_scout_web/views/tokens/overview_view.ex:35
#: lib/block_scout_web/views/transaction_view.ex:314 #: lib/block_scout_web/views/transaction_view.ex:314
@ -1035,8 +1035,8 @@ msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/chain/show.html.eex:24 #: lib/block_scout_web/templates/chain/show.html.eex:24
#: lib/block_scout_web/templates/layout/app.html.eex:30 #: lib/block_scout_web/templates/layout/app.html.eex:30
#: lib/block_scout_web/views/address_view.ex:126 #: lib/block_scout_web/views/address_view.ex:127
#: lib/block_scout_web/views/address_view.ex:126 #: lib/block_scout_web/views/address_view.ex:127
msgid "Market Cap" msgid "Market Cap"
msgstr "" msgstr ""
@ -1488,7 +1488,7 @@ msgid "Transaction Speed"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/address/_tile.html.eex:33 #: lib/block_scout_web/templates/address/_tile.html.eex:31
msgid "Transactions sent" msgid "Transactions sent"
msgstr "" msgstr ""
@ -1802,7 +1802,7 @@ msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/address/_tabs.html.eex:37 #: lib/block_scout_web/templates/address/_tabs.html.eex:37
#: lib/block_scout_web/templates/address_validation/index.html.eex:13 #: lib/block_scout_web/templates/address_validation/index.html.eex:13
#: lib/block_scout_web/views/address_view.ex:313 #: lib/block_scout_web/views/address_view.ex:319
msgid "Blocks Validated" msgid "Blocks Validated"
msgstr "" msgstr ""
@ -1812,18 +1812,18 @@ msgstr ""
#: lib/block_scout_web/templates/api_docs/_action_tile.html.eex:187 #: lib/block_scout_web/templates/api_docs/_action_tile.html.eex:187
#: lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex:126 #: lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex:126
#: lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex:149 #: lib/block_scout_web/templates/api_docs/_eth_rpc_item.html.eex:149
#: lib/block_scout_web/views/address_view.ex:309 #: lib/block_scout_web/views/address_view.ex:315
msgid "Code" msgid "Code"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/address/_tabs.html.eex:26 #: lib/block_scout_web/templates/address/_tabs.html.eex:26
#: lib/block_scout_web/views/address_view.ex:312 #: lib/block_scout_web/views/address_view.ex:318
msgid "Coin Balance History" msgid "Coin Balance History"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/views/address_view.ex:310 #: lib/block_scout_web/views/address_view.ex:316
msgid "Decompiled Code" msgid "Decompiled Code"
msgstr "" msgstr ""
@ -1832,7 +1832,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_internal_transaction/index.html.eex:19 #: lib/block_scout_web/templates/address_internal_transaction/index.html.eex:19
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:11 #: lib/block_scout_web/templates/transaction/_tabs.html.eex:11
#: lib/block_scout_web/templates/transaction_internal_transaction/index.html.eex:6 #: lib/block_scout_web/templates/transaction_internal_transaction/index.html.eex:6
#: lib/block_scout_web/views/address_view.ex:307 #: lib/block_scout_web/views/address_view.ex:313
#: lib/block_scout_web/views/transaction_view.ex:315 #: lib/block_scout_web/views/transaction_view.ex:315
msgid "Internal Transactions" msgid "Internal Transactions"
msgstr "" msgstr ""
@ -1842,7 +1842,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_logs/index.html.eex:8 #: lib/block_scout_web/templates/address_logs/index.html.eex:8
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:17 #: lib/block_scout_web/templates/transaction/_tabs.html.eex:17
#: lib/block_scout_web/templates/transaction_log/index.html.eex:8 #: lib/block_scout_web/templates/transaction_log/index.html.eex:8
#: lib/block_scout_web/views/address_view.ex:314 #: lib/block_scout_web/views/address_view.ex:320
#: lib/block_scout_web/views/transaction_view.ex:316 #: lib/block_scout_web/views/transaction_view.ex:316
msgid "Logs" msgid "Logs"
msgstr "" msgstr ""
@ -1850,7 +1850,7 @@ msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/address/_tabs.html.eex:62 #: lib/block_scout_web/templates/address/_tabs.html.eex:62
#: lib/block_scout_web/templates/tokens/overview/_tabs.html.eex:25 #: lib/block_scout_web/templates/tokens/overview/_tabs.html.eex:25
#: lib/block_scout_web/views/address_view.ex:311 #: lib/block_scout_web/views/address_view.ex:317
#: lib/block_scout_web/views/tokens/overview_view.ex:37 #: lib/block_scout_web/views/tokens/overview_view.ex:37
msgid "Read Contract" msgid "Read Contract"
msgstr "" msgstr ""
@ -1859,7 +1859,7 @@ msgstr ""
#: lib/block_scout_web/templates/address/_tabs.html.eex:14 #: lib/block_scout_web/templates/address/_tabs.html.eex:14
#: lib/block_scout_web/templates/address_token/index.html.eex:8 #: lib/block_scout_web/templates/address_token/index.html.eex:8
#: lib/block_scout_web/templates/address_token_transfer/index.html.eex:11 #: lib/block_scout_web/templates/address_token_transfer/index.html.eex:11
#: lib/block_scout_web/views/address_view.ex:305 #: lib/block_scout_web/views/address_view.ex:311
msgid "Tokens" msgid "Tokens"
msgstr "" msgstr ""
@ -1871,6 +1871,6 @@ msgstr ""
#: lib/block_scout_web/templates/block_transaction/index.html.eex:18 #: lib/block_scout_web/templates/block_transaction/index.html.eex:18
#: lib/block_scout_web/templates/chain/show.html.eex:140 #: lib/block_scout_web/templates/chain/show.html.eex:140
#: lib/block_scout_web/templates/layout/_topnav.html.eex:50 #: lib/block_scout_web/templates/layout/_topnav.html.eex:50
#: lib/block_scout_web/views/address_view.ex:306 #: lib/block_scout_web/views/address_view.ex:312
msgid "Transactions" msgid "Transactions"
msgstr "" msgstr ""

@ -143,6 +143,26 @@ defmodule BlockScoutWeb.AddressViewTest do
end end
end end
describe "balance_percentage_enabled/1" do
test "with non_zero market cap" do
Application.put_env(:block_scout_web, :show_percentage, true)
assert AddressView.balance_percentage_enabled?(100_500) == true
end
test "with zero market cap" do
Application.put_env(:block_scout_web, :show_percentage, true)
assert AddressView.balance_percentage_enabled?(0) == false
end
test "with switched off show_percentage" do
Application.put_env(:block_scout_web, :show_percentage, false)
assert AddressView.balance_percentage_enabled?(100_501) == false
end
end
test "balance_percentage/1" do test "balance_percentage/1" do
Application.put_env(:explorer, :supply, Explorer.Chain.Supply.ProofOfAuthority) Application.put_env(:explorer, :supply, Explorer.Chain.Supply.ProofOfAuthority)
address = insert(:address, fetched_coin_balance: 2_524_608_000_000_000_000_000_000) address = insert(:address, fetched_coin_balance: 2_524_608_000_000_000_000_000_000)

Loading…
Cancel
Save