From e8c205ae27a67d5481eb16c154f345d477aa1430 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 4 Mar 2019 10:14:11 +0300 Subject: [PATCH 1/3] fix coin balance params reducer for pening transaction --- .../lib/indexer/address/coin_balances.ex | 10 +++++-- .../indexer/address/coin_balances_test.exs | 30 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/apps/indexer/lib/indexer/address/coin_balances.ex b/apps/indexer/lib/indexer/address/coin_balances.ex index 03f0c4f847..6b50f0ecb0 100644 --- a/apps/indexer/lib/indexer/address/coin_balances.ex +++ b/apps/indexer/lib/indexer/address/coin_balances.ex @@ -29,10 +29,16 @@ defmodule Indexer.Address.CoinBalances do defp reducer({:logs_params, logs_params}, acc) when is_list(logs_params) do # a log MUST have and address_hash - Enum.into(logs_params, acc, fn %{address_hash: address_hash, block_number: block_number} - when is_binary(address_hash) and is_integer(block_number) -> + logs_params + |> Enum.into(acc, fn %{address_hash: address_hash, block_number: block_number} + when is_binary(address_hash) and is_integer(block_number) -> + %{address_hash: address_hash, block_number: block_number} + + %{type: "pending"} -> + nil end) + |> Enum.reject(fn val -> is_nil(val) end) end defp reducer({:transactions_params, transactions_params}, initial) when is_list(transactions_params) do diff --git a/apps/indexer/test/indexer/address/coin_balances_test.exs b/apps/indexer/test/indexer/address/coin_balances_test.exs index a0161ce013..adff9a66f4 100644 --- a/apps/indexer/test/indexer/address/coin_balances_test.exs +++ b/apps/indexer/test/indexer/address/coin_balances_test.exs @@ -113,8 +113,34 @@ defmodule Indexer.Address.CoinBalancesTest do params_set = CoinBalances.params_set(%{logs_params: [log_params]}) - assert MapSet.size(params_set) == 1 - assert %{address_hash: address_hash, block_number: block_number} + assert Enum.count(params_set) == 1 + assert %{address_hash: address_hash, block_number: block_number} == List.first(params_set) + end + + test "with log skips pending transactions" do + block_number = 1 + + address_hash = + Factory.address_hash() + |> to_string() + + log_params1 = + :log + |> Factory.params_for() + |> Map.put(:block_number, nil) + |> Map.put(:address_hash, address_hash) + |> Map.put(:type, "pending") + + log_params2 = + :log + |> Factory.params_for() + |> Map.put(:block_number, block_number) + |> Map.put(:address_hash, address_hash) + + params_set = CoinBalances.params_set(%{logs_params: [log_params1, log_params2]}) + + assert Enum.count(params_set) == 1 + assert %{address_hash: address_hash, block_number: block_number} == List.first(params_set) end test "with transaction without to_address_hash extracts from_address_hash" do From 9d21d4687f92f6f57784f2b6a5a4485a51f8bee0 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 4 Mar 2019 13:25:52 +0300 Subject: [PATCH 2/3] use MapSet --- apps/indexer/lib/indexer/address/coin_balances.ex | 1 + apps/indexer/test/indexer/address/coin_balances_test.exs | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/apps/indexer/lib/indexer/address/coin_balances.ex b/apps/indexer/lib/indexer/address/coin_balances.ex index 6b50f0ecb0..b313d056c0 100644 --- a/apps/indexer/lib/indexer/address/coin_balances.ex +++ b/apps/indexer/lib/indexer/address/coin_balances.ex @@ -39,6 +39,7 @@ defmodule Indexer.Address.CoinBalances do nil end) |> Enum.reject(fn val -> is_nil(val) end) + |> MapSet.new() end defp reducer({:transactions_params, transactions_params}, initial) when is_list(transactions_params) do diff --git a/apps/indexer/test/indexer/address/coin_balances_test.exs b/apps/indexer/test/indexer/address/coin_balances_test.exs index adff9a66f4..5eae99b22c 100644 --- a/apps/indexer/test/indexer/address/coin_balances_test.exs +++ b/apps/indexer/test/indexer/address/coin_balances_test.exs @@ -113,8 +113,8 @@ defmodule Indexer.Address.CoinBalancesTest do params_set = CoinBalances.params_set(%{logs_params: [log_params]}) - assert Enum.count(params_set) == 1 - assert %{address_hash: address_hash, block_number: block_number} == List.first(params_set) + assert MapSet.size(params_set) == 1 + assert MapSet.new([%{address_hash: address_hash, block_number: block_number}]) == params_set end test "with log skips pending transactions" do @@ -139,8 +139,8 @@ defmodule Indexer.Address.CoinBalancesTest do params_set = CoinBalances.params_set(%{logs_params: [log_params1, log_params2]}) - assert Enum.count(params_set) == 1 - assert %{address_hash: address_hash, block_number: block_number} == List.first(params_set) + assert MapSet.size(params_set) == 1 + assert MapSet.new([%{address_hash: address_hash, block_number: block_number}]) == params_set end test "with transaction without to_address_hash extracts from_address_hash" do From 228baff5d69cc9800179e2aac0bf025c4a1cb599 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 4 Mar 2019 16:48:23 +0300 Subject: [PATCH 3/3] fix comment --- apps/indexer/lib/indexer/address/coin_balances.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/indexer/lib/indexer/address/coin_balances.ex b/apps/indexer/lib/indexer/address/coin_balances.ex index b313d056c0..83441927c1 100644 --- a/apps/indexer/lib/indexer/address/coin_balances.ex +++ b/apps/indexer/lib/indexer/address/coin_balances.ex @@ -28,7 +28,7 @@ defmodule Indexer.Address.CoinBalances do end defp reducer({:logs_params, logs_params}, acc) when is_list(logs_params) do - # a log MUST have and address_hash + # a log MUST have address_hash and block_number logs_params |> Enum.into(acc, fn %{address_hash: address_hash, block_number: block_number}