From c5e4485d3d658b5fc53c2e254e93338ae4348c13 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 23 Aug 2019 12:54:27 +0300 Subject: [PATCH 1/4] deduplicate coin history records by delta On coin history page we show records that are found in the db. Since we may fetch a lot of duplicated balances for the same address if its balance is not changes for multiple blocks. This PR adds deduplication by delta value to remove duplication. --- apps/explorer/lib/explorer/chain.ex | 3 +++ apps/explorer/test/explorer/chain_test.exs | 26 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 58253c0f27..a6200a81f9 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -2967,6 +2967,9 @@ defmodule Explorer.Chain do |> CoinBalance.fetch_coin_balances(paging_options) |> page_coin_balances(paging_options) |> Repo.all() + |> Enum.dedup_by(fn record -> + record.delta == Decimal.new(0) + end) end def get_coin_balance(address_hash, block_number) do diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index f49be6da4e..10788b60ba 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -4159,4 +4159,30 @@ defmodule Explorer.ChainTest do assert Chain.staking_pools_count(:inactive) == 1 end end + + describe "address_to_coin_balances/2" do + test "deduplicates records by zero delta" do + address = insert(:address) + + 1..5 + |> Enum.each(fn block_number -> + insert(:block, number: block_number) + insert(:fetched_balance, value: 1, block_number: block_number, address_hash: address.hash) + end) + + insert(:block, number: 6) + insert(:fetched_balance, value: 2, block_number: 6, address_hash: address.hash) + + assert [first, second, third] = Chain.address_to_coin_balances(address.hash, []) + + assert first.block_number == 6 + assert first.delta == Decimal.new(1) + + assert second.block_number == 5 + assert second.delta == Decimal.new(0) + + assert third.block_number == 1 + assert third.delta == Decimal.new(1) + end + end end From e8ba3b22785931c890ebc856096b99482c99a399 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 23 Aug 2019 12:59:07 +0300 Subject: [PATCH 2/4] add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c26c016be..d6cfce7118 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - [#2497](https://github.com/poanetwork/blockscout/pull/2497) - Add generic Ordered Cache behaviour and implementation ### Fixes +- [#2616](https://github.com/poanetwork/blockscout/pull/2616) - deduplicate coin history records by delta - [#2592](https://github.com/poanetwork/blockscout/pull/2592) - process new metadata format for whisper - [#2572](https://github.com/poanetwork/blockscout/pull/2572) - Ease non-critical css - [#2570](https://github.com/poanetwork/blockscout/pull/2570) - Network icons preload From 045652a505a93120d28a1db92fc20241a6a8dd40 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 23 Aug 2019 14:00:16 +0300 Subject: [PATCH 3/4] fix tests --- apps/explorer/lib/explorer/chain.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index a6200a81f9..c781a3463e 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -2968,7 +2968,11 @@ defmodule Explorer.Chain do |> page_coin_balances(paging_options) |> Repo.all() |> Enum.dedup_by(fn record -> - record.delta == Decimal.new(0) + if record.delta == Decimal.new(0) do + :dup + else + System.unique_integer + end end) end From cd979c02c2f257fa06eb79d05684cd14333e6406 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 23 Aug 2019 14:02:46 +0300 Subject: [PATCH 4/4] mix format --- apps/explorer/lib/explorer/chain.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index c781a3463e..b2c18e8a50 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -2971,7 +2971,7 @@ defmodule Explorer.Chain do if record.delta == Decimal.new(0) do :dup else - System.unique_integer + System.unique_integer() end end) end