From 0184e9726ad40966437b6d5180ecc10855024eb6 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 12 Apr 2019 11:15:58 +0300 Subject: [PATCH] sort decompiled smart contract in lexicographical order --- .../address_decompiled_contract/index.html.eex | 2 +- .../views/address_decompiled_contract_view.ex | 8 +++++++- ...s => address_decompiled_contract_view_test.exs} | 14 +++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) rename apps/block_scout_web/test/block_scout_web/views/{address_decompiled_code_test.exs => address_decompiled_contract_view_test.exs} (92%) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/address_decompiled_contract/index.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/address_decompiled_contract/index.html.eex index d2412d62c1..64f364d9b2 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/address_decompiled_contract/index.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/address_decompiled_contract/index.html.eex @@ -6,7 +6,7 @@ <%= render BlockScoutWeb.AddressView, "_tabs.html", assigns %> - <%= for {contract, _i} <- Enum.with_index(@address.decompiled_smart_contracts) do %> + <%= for contract <- sort_contracts_by_version(@address.decompiled_smart_contracts) do %>

<%= gettext "Decompiler version" %>

diff --git a/apps/block_scout_web/lib/block_scout_web/views/address_decompiled_contract_view.ex b/apps/block_scout_web/lib/block_scout_web/views/address_decompiled_contract_view.ex index 348df51152..148d82b818 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/address_decompiled_contract_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/address_decompiled_contract_view.ex @@ -13,7 +13,7 @@ defmodule BlockScoutWeb.AddressDecompiledContractView do "\e[93m" => "57, 115, 0", # yellow "\e[92m" => "119, 232, 81", - # purple + # red "\e[94m" => "136, 0, 0" } @@ -28,6 +28,12 @@ defmodule BlockScoutWeb.AddressDecompiledContractView do |> add_line_numbers() end + def sort_contracts_by_version(decompiled_contracts) do + decompiled_contracts + |> Enum.sort_by(& &1.decompiler_version) + |> Enum.reverse() + end + defp add_line_numbers(code) do code |> String.split("\n") diff --git a/apps/block_scout_web/test/block_scout_web/views/address_decompiled_code_test.exs b/apps/block_scout_web/test/block_scout_web/views/address_decompiled_contract_view_test.exs similarity index 92% rename from apps/block_scout_web/test/block_scout_web/views/address_decompiled_code_test.exs rename to apps/block_scout_web/test/block_scout_web/views/address_decompiled_contract_view_test.exs index 8d94d9f724..9e12503c3a 100644 --- a/apps/block_scout_web/test/block_scout_web/views/address_decompiled_code_test.exs +++ b/apps/block_scout_web/test/block_scout_web/views/address_decompiled_contract_view_test.exs @@ -1,5 +1,5 @@ defmodule BlockScoutWeb.AddressDecompiledContractViewTest do - use BlockScoutWeb.ConnCase, async: true + use Explorer.DataCase alias BlockScoutWeb.AddressDecompiledContractView @@ -59,4 +59,16 @@ defmodule BlockScoutWeb.AddressDecompiledContractViewTest do " #\n # eveem.org 6 Feb 2019\n # Decompiled source of 0x00Bd9e214FAb74d6fC21bf1aF34261765f57e875\n #\n # Let's make the world open source\n # \n #\n # I failed with these:\n # - unknowne77c646d(?)\n # - transferFromWithData(address _from, address _to, uint256 _value, bytes _data)\n # All the rest is below.\n #\n\n\n # Storage definitions and getters\n\n def storage:\n allowance is uint256 => uint256 # mask(256, 0) at storage #2\n stor4 is uint256 => uint8 # mask(8, 0) at storage #4\n\n def allowance(address _owner, address _spender) payable: \n require (calldata.size - 4) >= 64\n return allowance[sha3(((320 - 1) and (320 - 1) and _owner), 1), ((320 - 1) and _spender and (320 - 1))]\n\n\n #\n # Regular functions - see Tutorial for understanding quirks of the code\n #\n\n\n # folder failed in this function - may be terribly long, sorry\n def unknownc47d033b(?) payable: \n if (calldata.size - 4) < 32:\n revert\n else:\n if not (320 - 1) or not cd[4]:\n revert\n else:\n mem[0] = (320 - 1) and (320 - 1) and cd[4]\n mem[32] = 4\n mem[96] = bool(stor4[((320 - 1) and (320 - 1) and cd[4])])\n return bool(stor4[((320 - 1) and (320 - 1) and cd[4])])\n\n def _fallback() payable: # default function\n revert\n\n" end end + + describe "sort_contracts_by_version/1" do + test "sorts contracts in lexicographical order" do + contract2 = insert(:decompiled_smart_contract, decompiler_version: "v2") + contract1 = insert(:decompiled_smart_contract, decompiler_version: "v1") + contract3 = insert(:decompiled_smart_contract, decompiler_version: "v3") + + result = AddressDecompiledContractView.sort_contracts_by_version([contract2, contract1, contract3]) + + assert result == [contract3, contract2, contract1] + end + end end