From 8f05ada7f4a6770699031442a270d12d5209c38f Mon Sep 17 00:00:00 2001 From: zachdaniel Date: Tue, 20 Nov 2018 11:50:10 -0500 Subject: [PATCH] chore: cleanup, error handling, tests --- .../transaction/_decoded_input.html.eex | 22 ++++++++++--- .../templates/transaction_log/index.html.eex | 13 +++++--- .../views/abi_encoded_value_view.ex | 4 +++ apps/block_scout_web/priv/gettext/default.pot | 19 +++++++---- .../priv/gettext/en/LC_MESSAGES/default.po | 19 +++++++---- .../views/abi_encoded_value_view_test.exs | 33 +++++++++++++++++-- 6 files changed, 84 insertions(+), 26 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/transaction/_decoded_input.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/transaction/_decoded_input.html.eex index 2391747dd4..91a5ce1a2b 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/transaction/_decoded_input.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/transaction/_decoded_input.html.eex @@ -35,15 +35,27 @@ <%= for {name, type, value} <- mapping do %> - <% copy_text = BlockScoutWeb.ABIEncodedValueView.copy_text(type, value) %> - + <%= case BlockScoutWeb.ABIEncodedValueView.copy_text(type, value) do %> + <% :error -> %> + <%= nil %> + <% copy_text -> %> + + <% end %> <%= name %> <%= type %> -
<%= BlockScoutWeb.ABIEncodedValueView.value_html(type, value) %>
+ <%= case BlockScoutWeb.ABIEncodedValueView.value_html(type, value) do %> + <% :error -> %> +
+ <%= gettext "Error rendering value" %> +
+ <% value -> %> +
<%= value %>
+ <% end %> + <% end %> diff --git a/apps/block_scout_web/lib/block_scout_web/templates/transaction_log/index.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/transaction_log/index.html.eex index a89eb44f0c..c4bb03a252 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/transaction_log/index.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/transaction_log/index.html.eex @@ -56,11 +56,14 @@ <%= for {name, type, indexed?, value} <- mapping do %> - <% copy_text = BlockScoutWeb.ABIEncodedValueView.copy_text(type, value) %> - - + <%= case BlockScoutWeb.ABIEncodedValueView.copy_text(type, value) do %> + <% :error -> %> + <%= nil %> + <% copy_text -> %> + + <% end %> <%= name %> <%= type %> diff --git a/apps/block_scout_web/lib/block_scout_web/views/abi_encoded_value_view.ex b/apps/block_scout_web/lib/block_scout_web/views/abi_encoded_value_view.ex index 28d649e1d0..a2ff2a4db2 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/abi_encoded_value_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/abi_encoded_value_view.ex @@ -21,6 +21,8 @@ defmodule BlockScoutWeb.ABIEncodedValueView do Logger.warn(fn -> ["Error determining value html for #{inspect(type)}: ", Exception.format(:error, exception)] end) + + :error end def copy_text(type, value) do @@ -32,6 +34,8 @@ defmodule BlockScoutWeb.ABIEncodedValueView do Logger.warn(fn -> ["Error determining copy text for #{inspect(type)}: ", Exception.format(:error, exception)] end) + + :error end def do_copy_text({:bytes, _type}, value) do diff --git a/apps/block_scout_web/priv/gettext/default.pot b/apps/block_scout_web/priv/gettext/default.pot index 14c452a124..4ba1db9e70 100644 --- a/apps/block_scout_web/priv/gettext/default.pot +++ b/apps/block_scout_web/priv/gettext/default.pot @@ -357,7 +357,7 @@ msgstr "" #, elixir-format #: lib/block_scout_web/templates/transaction/_decoded_input.html.eex:33 #: lib/block_scout_web/templates/transaction_log/index.html.eex:54 -#: lib/block_scout_web/templates/transaction_log/index.html.eex:115 +#: lib/block_scout_web/templates/transaction_log/index.html.eex:118 msgid "Data" msgstr "" @@ -601,7 +601,7 @@ msgstr "" #, elixir-format #: #: lib/block_scout_web/templates/transaction_internal_transaction/index.html.eex:23 -#: lib/block_scout_web/templates/transaction_log/index.html.eex:142 +#: lib/block_scout_web/templates/transaction_log/index.html.eex:145 msgid "Newer" msgstr "" @@ -823,7 +823,7 @@ msgid "There are no internal transactions for this transaction." msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction_log/index.html.eex:136 +#: lib/block_scout_web/templates/transaction_log/index.html.eex:139 msgid "There are no logs for this transaction." msgstr "" @@ -935,7 +935,7 @@ msgid "Top Accounts - %{subnetwork} Explorer" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction_log/index.html.eex:79 +#: lib/block_scout_web/templates/transaction_log/index.html.eex:82 msgid "Topics" msgstr "" @@ -1329,12 +1329,12 @@ msgid "Indexed?" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction_log/index.html.eex:120 +#: lib/block_scout_web/templates/transaction_log/index.html.eex:123 msgid "Show Raw Data" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction_log/index.html.eex:82 +#: lib/block_scout_web/templates/transaction_log/index.html.eex:85 msgid "Show Raw Topics" msgstr "" @@ -1375,6 +1375,11 @@ msgid "here" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction/_decoded_input.html.eex:53 +#: lib/block_scout_web/templates/transaction/_decoded_input.html.eex:65 msgid "Failed to decode input data." msgstr "" + +#, elixir-format +#: lib/block_scout_web/templates/transaction/_decoded_input.html.eex:53 +msgid "Error rendering value" +msgstr "" diff --git a/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po b/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po index 4889aea215..6510ad675e 100644 --- a/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po +++ b/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po @@ -357,7 +357,7 @@ msgstr "" #, elixir-format #: lib/block_scout_web/templates/transaction/_decoded_input.html.eex:33 #: lib/block_scout_web/templates/transaction_log/index.html.eex:54 -#: lib/block_scout_web/templates/transaction_log/index.html.eex:115 +#: lib/block_scout_web/templates/transaction_log/index.html.eex:118 msgid "Data" msgstr "" @@ -601,7 +601,7 @@ msgstr "" #, elixir-format #: #: lib/block_scout_web/templates/transaction_internal_transaction/index.html.eex:23 -#: lib/block_scout_web/templates/transaction_log/index.html.eex:142 +#: lib/block_scout_web/templates/transaction_log/index.html.eex:145 msgid "Newer" msgstr "" @@ -823,7 +823,7 @@ msgid "There are no internal transactions for this transaction." msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction_log/index.html.eex:136 +#: lib/block_scout_web/templates/transaction_log/index.html.eex:139 msgid "There are no logs for this transaction." msgstr "" @@ -935,7 +935,7 @@ msgid "Top Accounts - %{subnetwork} Explorer" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction_log/index.html.eex:79 +#: lib/block_scout_web/templates/transaction_log/index.html.eex:82 msgid "Topics" msgstr "" @@ -1329,12 +1329,12 @@ msgid "Indexed?" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction_log/index.html.eex:120 +#: lib/block_scout_web/templates/transaction_log/index.html.eex:123 msgid "Show Raw Data" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction_log/index.html.eex:82 +#: lib/block_scout_web/templates/transaction_log/index.html.eex:85 msgid "Show Raw Topics" msgstr "" @@ -1375,6 +1375,11 @@ msgid "here" msgstr "" #, elixir-format -#: lib/block_scout_web/templates/transaction/_decoded_input.html.eex:53 +#: lib/block_scout_web/templates/transaction/_decoded_input.html.eex:65 msgid "Failed to decode input data." msgstr "" + +#, elixir-format +#: lib/block_scout_web/templates/transaction/_decoded_input.html.eex:53 +msgid "Error rendering value" +msgstr "" diff --git a/apps/block_scout_web/test/block_scout_web/views/abi_encoded_value_view_test.exs b/apps/block_scout_web/test/block_scout_web/views/abi_encoded_value_view_test.exs index d6b5060347..c095bb4de4 100644 --- a/apps/block_scout_web/test/block_scout_web/views/abi_encoded_value_view_test.exs +++ b/apps/block_scout_web/test/block_scout_web/views/abi_encoded_value_view_test.exs @@ -6,6 +6,13 @@ defmodule BlockScoutWeb.ABIEncodedValueViewTest do defp value_html(type, value) do type |> ABIEncodedValueView.value_html(value) + |> case do + :error -> + raise "failed to generate html" + + other -> + other + end |> Phoenix.HTML.Safe.to_iodata() |> IO.iodata_to_binary() end @@ -13,6 +20,13 @@ defmodule BlockScoutWeb.ABIEncodedValueViewTest do defp copy_text(type, value) do type |> ABIEncodedValueView.copy_text(value) + |> case do + :error -> + raise "failed to generate copy text" + + other -> + other + end |> Phoenix.HTML.Safe.to_iodata() |> IO.iodata_to_binary() end @@ -75,7 +89,11 @@ defmodule BlockScoutWeb.ABIEncodedValueViewTest do address_bytes = "0x0000000000000000000000000000000000000000" |> String.trim_leading("0x") |> Base.decode16!() - assert value_html("address[]", [address_bytes, address_bytes, address_bytes, address_bytes]) == expected + assert value_html("address[4]", [address_bytes, address_bytes, address_bytes, address_bytes]) == expected + end + + test "it renders :dynamic values as bytes" do + assert value_html("uint", {:dynamic, <<1>>}) == "0x01" end end @@ -88,7 +106,18 @@ defmodule BlockScoutWeb.ABIEncodedValueViewTest do end test "it skips the formatting when copying lists" do - assert copy_text("uint[]", [1, 2, 3, 4]) == "[1, 2, 3, 4]" + assert copy_text("uint[4]", [1, 2, 3, 4]) == "[1, 2, 3, 4]" + end + + test "it copies bytes as their hex representation" do + hex = "0xffffff" + bytes = hex |> String.trim_leading("0x") |> Base.decode16!(case: :lower) + + assert copy_text("bytes", bytes) == hex + end + + test "it copies :dynamic values as bytes" do + assert copy_text("uint", {:dynamic, <<1>>}) == "0x01" end end end