chore: improve test coverage

pull/1119/head
zachdaniel 6 years ago
parent 8dc75f29db
commit 63f7ca7f1d
  1. 14
      apps/explorer/lib/explorer/chain/log.ex
  2. 64
      apps/explorer/test/explorer/chain/log_test.exs

@ -107,8 +107,8 @@ defmodule Explorer.Chain.Log do
"""
def decode(_log, %Transaction{to_address: nil}), do: {:error, :no_to_address}
def decode(log, %Transaction{to_address: %{smart_contract: %{abi: abi}}}) when not is_nil(abi) do
with {:ok, selector, mapping} <- find_and_decode(abi, log),
def decode(log, transaction = %Transaction{to_address: %{smart_contract: %{abi: abi}}}) when not is_nil(abi) do
with {:ok, selector, mapping} <- find_and_decode(abi, log, transaction),
identifier <- Base.encode16(selector.method_id, case: :lower),
text <- function_call(selector.function, mapping),
do: {:ok, identifier, text, mapping}
@ -116,8 +116,8 @@ defmodule Explorer.Chain.Log do
def decode(_log, _transaction), do: {:error, :contract_not_verified}
defp find_and_decode(abi, log) do
with {selector, mapping} <-
defp find_and_decode(abi, log, transaction) do
with {selector, mapping} when selector != :error <-
abi
|> ABI.parse_specification(include_events?: true)
|> Event.find_and_decode(
@ -128,10 +128,14 @@ defmodule Explorer.Chain.Log do
log.data.bytes
) do
{:ok, selector, mapping}
else
:error ->
{:error, :could_not_decode}
end
rescue
_ ->
Logger.warn(fn -> ["Could not decode input data for log: ", Hash.to_iodata(log.hash)] end)
Logger.warn(fn -> ["Could not decode input data for log from transaction: ", Hash.to_iodata(transaction.hash)] end)
{:error, :could_not_decode}
end

@ -37,4 +37,68 @@ defmodule Explorer.Chain.LogTest do
assert changeset.changes.first_topic === "ham"
end
end
describe "decode/2" do
test "that a contract call transaction that has no verified contract returns a commensurate error" do
transaction =
:transaction
|> insert(to_address: insert(:contract_address))
|> Repo.preload(to_address: :smart_contract)
log = insert(:log, transaction: transaction)
assert Log.decode(log, transaction) == {:error, :contract_not_verified}
end
test "that a contract call transaction that has a verified contract returns the decoded input data" do
smart_contract =
insert(:smart_contract,
abi: [
%{
"anonymous" => false,
"inputs" => [
%{"indexed" => true, "name" => "_from_human", "type" => "string"},
%{"indexed" => false, "name" => "_number", "type" => "uint256"},
%{"indexed" => true, "name" => "_belly", "type" => "bool"}
],
"name" => "WantsPets",
"type" => "event"
}
]
)
topic1 = "0x" <> Base.encode16(:keccakf1600.hash(:sha3_256, "WantsPets(string,uint256,bool)"), case: :lower)
topic2 = "0x" <> Base.encode16(:keccakf1600.hash(:sha3_256, "bob"), case: :lower)
topic3 = "0x0000000000000000000000000000000000000000000000000000000000000001"
data = "0x0000000000000000000000000000000000000000000000000000000000000000"
to_address = insert(:address, smart_contract: smart_contract)
transaction =
:transaction_to_verified_contract
|> insert(to_address: to_address)
|> Repo.preload(to_address: :smart_contract)
log =
insert(:log,
transaction: transaction,
first_topic: topic1,
second_topic: topic2,
third_topic: topic3,
fourth_topic: nil,
data: data
)
assert Log.decode(log, transaction) ==
{:ok, "eb9b3c4c", "WantsPets(string indexed _from_human, uint256 _number, bool indexed _belly)",
[
{"_from_human", "string", true,
{:dynamic,
<<56, 228, 122, 123, 113, 157, 206, 99, 102, 42, 234, 244, 52, 64, 50, 111, 85, 27, 138, 126, 225,
152, 206, 227, 92, 181, 213, 23, 242, 210, 150, 162>>}},
{"_number", "uint256", false, 0},
{"_belly", "bool", true, true}
]}
end
end
end

Loading…
Cancel
Save