Log token balances that raised errors

pull/1098/head
Felipe Renan 6 years ago
parent a79fa5b3e4
commit e2827ba907
  1. 1
      apps/indexer/lib/indexer/block/realtime/fetcher.ex
  2. 4
      apps/indexer/lib/indexer/token_balance/fetcher.ex
  3. 12
      apps/indexer/lib/indexer/token_balances.ex
  4. 28
      apps/indexer/test/indexer/token_balances_test.exs

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

@ -58,8 +58,6 @@ defmodule Indexer.TokenBalance.Fetcher do
@impl BufferedTask
def run(entries, _json_rpc_named_arguments) do
Logger.debug(fn -> "fetching #{length(entries)} token balances" end)
result =
entries
|> Enum.map(&format_params/1)
@ -80,8 +78,6 @@ defmodule Indexer.TokenBalance.Fetcher do
|> Enum.filter(&(&1.retries_count <= @max_retries))
|> TokenBalances.fetch_token_balances_from_blockchain()
TokenBalances.log_fetching_errors(__MODULE__, token_balances)
token_balances
end

@ -25,6 +25,8 @@ defmodule Indexer.TokenBalances do
* `block_number` - The block number that the address_hash has the balance.
"""
def fetch_token_balances_from_blockchain(token_balances) do
Logger.debug(fn -> "fetching #{Enum.count(token_balances)} token balances" end)
requested_token_balances =
token_balances
|> Task.async_stream(&fetch_token_balance/1, on_timeout: :kill_task)
@ -64,6 +66,10 @@ defmodule Indexer.TokenBalances do
defp schedule_token_balances([]), do: nil
defp schedule_token_balances(unfetched_token_balances) do
Logger.debug(fn -> "#{Enum.count(unfetched_token_balances)} token balances will be retried" end)
log_fetching_errors(unfetched_token_balances)
unfetched_token_balances
|> Enum.map(fn token_balance ->
{:ok, address_hash} = Chain.string_to_address_hash(token_balance.address_hash)
@ -93,20 +99,20 @@ defmodule Indexer.TokenBalances do
|> Enum.map(&Map.merge(&1, %{value: nil, value_fetched_at: nil, error: :timeout}))
end
def log_fetching_errors(from, token_balances_params) do
def log_fetching_errors(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.address_hash}, " <>
"contract_address_hash: #{token_balance.token_contract_address_hash}, " <>
"block_number: #{token_balance.block_number}, " <> "error: #{token_balance.error}> \n"
"block_number: #{token_balance.block_number}, " <>
"error: #{token_balance.error}>, " <> "retried: #{Map.get(token_balance, :retries_count, 1)} times\n"
end)
if Enum.any?(error_messages) do
Logger.debug(
[
"<#{from}> ",
"Errors while fetching TokenBalances through Contract interaction: \n",
error_messages
],

@ -53,7 +53,8 @@ defmodule Indexer.TokenBalancesTest do
%{
address_hash: to_string(address.hash),
block_number: 1_000,
token_contract_address_hash: to_string(token.contract_address_hash)
token_contract_address_hash: to_string(token.contract_address_hash),
retries_count: 1
}
]
@ -71,12 +72,14 @@ defmodule Indexer.TokenBalancesTest do
%{
token_contract_address_hash: Hash.to_string(token.contract_address_hash),
address_hash: address_hash_string,
block_number: 1_000
block_number: 1_000,
retries_count: 1
},
%{
token_contract_address_hash: Hash.to_string(token.contract_address_hash),
address_hash: address_hash_string,
block_number: 1_001
block_number: 1_001,
retries_count: 1
}
]
@ -93,31 +96,28 @@ defmodule Indexer.TokenBalancesTest 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)
TokenBalances.log_fetching_errors(params)
end)
assert log_message_response =~ "<Tests"
assert log_message_response =~ "Error"
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")
token_balance_params_with_error = Map.merge(build(:token_balance), %{error: "Error", retries_count: 1})
params = [token_balance_params_with_error]
log_message_response =
capture_log(fn ->
TokenBalances.log_fetching_errors(from, params)
TokenBalances.log_fetching_errors(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"
@ -128,7 +128,7 @@ defmodule Indexer.TokenBalancesTest do
log_message_response =
capture_log(fn ->
TokenBalances.log_fetching_errors(from, params)
TokenBalances.log_fetching_errors(params)
end)
assert log_message_response =~ error_1
@ -136,13 +136,12 @@ defmodule Indexer.TokenBalancesTest do
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)
TokenBalances.log_fetching_errors(params)
end)
assert log_message_response == ""
@ -169,9 +168,8 @@ defmodule Indexer.TokenBalancesTest do
token_balances = MapSet.new([token_balance_a, token_balance_b])
fetched_token_balances = MapSet.new([token_balance_a])
unfetched_token_balances = MapSet.new([token_balance_b])
assert TokenBalances.unfetched_token_balances(token_balances, fetched_token_balances) == unfetched_token_balances
assert TokenBalances.unfetched_token_balances(token_balances, fetched_token_balances) == [token_balance_b]
end
end

Loading…
Cancel
Save