Include and Log token_balances error attribute

pull/754/head
Igor Florian 6 years ago
parent 99012b2c4c
commit 52e861fa73
  1. 1
      apps/indexer/lib/indexer/block/realtime/fetcher.ex
  2. 6
      apps/indexer/lib/indexer/token_balance/fetcher.ex
  3. 30
      apps/indexer/lib/indexer/token_balances.ex
  4. 61
      apps/indexer/test/indexer/token_balances_test.exs

@ -146,6 +146,7 @@ defmodule Indexer.Block.Realtime.Fetcher do
|> put_in([Access.key(:internal_transactions, %{}), :params], internal_transactions_params)
|> put_in([Access.key(:token_balances), :params], token_balances),
{:ok, results} = ok <- Chain.import(chain_import_options) do
TokenBalances.log_fetching_errors(__MODULE__, token_balances)
async_import_remaining_block_data(results)
ok
end

@ -69,12 +69,14 @@ defmodule Indexer.TokenBalance.Fetcher do
end
def fetch_from_blockchain(token_balances) do
{:ok, token_balances_params} =
{:ok, token_balances} =
token_balances
|> Stream.map(&format_params/1)
|> TokenBalances.fetch_token_balances_from_blockchain()
token_balances_params
TokenBalances.log_fetching_errors(__MODULE__, token_balances)
token_balances
end
def import_token_balances(token_balances_params) do

@ -3,6 +3,8 @@ defmodule Indexer.TokenBalances do
Reads Token's balances using Smart Contract functions from the blockchain.
"""
require Logger
alias Explorer.Token.BalanceReader
@doc """
@ -42,11 +44,11 @@ defmodule Indexer.TokenBalances do
end
defp set_token_balance_value({:ok, balance}, token_balance) do
Map.merge(token_balance, %{value: balance, value_fetched_at: DateTime.utc_now()})
Map.merge(token_balance, %{value: balance, value_fetched_at: DateTime.utc_now(), error: nil})
end
defp set_token_balance_value({:error, _}, token_balance) do
Map.merge(token_balance, %{value: nil, value_fetched_at: nil})
defp set_token_balance_value({:error, error_message}, token_balance) do
Map.merge(token_balance, %{value: nil, value_fetched_at: nil, error: error_message})
end
def format_task_results({:exit, :timeout}), do: {:error, :timeout}
@ -54,4 +56,26 @@ defmodule Indexer.TokenBalances do
def ignore_request_with_timeouts({:error, :timeout}), do: false
def ignore_request_with_timeouts(_token_balance), do: true
def log_fetching_errors(from, token_balances_params) do
error_messages =
token_balances_params
|> Stream.filter(fn token_balance -> token_balance.error != nil end)
|> Enum.map(fn token_balance ->
"<address_hash: #{token_balance.token_contract_address_hash}, " <>
"contract_address_hash: #{token_balance.address_hash}, " <>
"block_number: #{token_balance.block_number}, " <> "error: #{token_balance.error}> \n"
end)
if Enum.any?(error_messages) do
Logger.debug(
[
"<#{from}> ",
"Errors while fetching TokenBalances through Contract interaction: \n",
error_messages
],
fetcher: :token_balances
)
end
end
end

@ -8,6 +8,7 @@ defmodule Indexer.TokenBalancesTest do
alias Explorer.Chain.Hash
import Mox
import ExUnit.CaptureLog
setup :verify_on_exit!
setup :set_mox_global
@ -88,6 +89,66 @@ defmodule Indexer.TokenBalancesTest do
end
end
describe "log_fetching_errors" do
test "logs the given from argument in final message" do
token_balance_params_with_error = Map.put(build(:token_balance), :error, "Error")
params = [token_balance_params_with_error]
from = "Tests"
log_message_response =
capture_log(fn ->
TokenBalances.log_fetching_errors(from, params)
end)
assert log_message_response =~ "<Tests"
end
test "log when there is a token_balance param with errors" do
from = "Tests"
token_balance_params_with_error = Map.put(build(:token_balance), :error, "Error")
params = [token_balance_params_with_error]
log_message_response =
capture_log(fn ->
TokenBalances.log_fetching_errors(from, params)
end)
assert log_message_response =~ "Error"
end
test "log multiple token balances params with errors" do
from = "Tests"
error_1 = "Error"
error_2 = "BadGateway"
params = [
Map.put(build(:token_balance), :error, error_1),
Map.put(build(:token_balance), :error, error_2)
]
log_message_response =
capture_log(fn ->
TokenBalances.log_fetching_errors(from, params)
end)
assert log_message_response =~ error_1
assert log_message_response =~ error_2
end
test "doesn't log when there aren't errors after fetching token balances" do
from = "Tests"
token_balance_params = Map.put(build(:token_balance), :error, nil)
params = [token_balance_params]
log_message_response =
capture_log(fn ->
TokenBalances.log_fetching_errors(from, params)
end)
assert log_message_response == ""
end
end
defp get_balance_from_blockchain() do
expect(
EthereumJSONRPC.Mox,

Loading…
Cancel
Save