From ff0db8c32b08e3b87dab3252f269781cbfd1834a Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 8 Feb 2019 14:58:42 +0300 Subject: [PATCH 1/5] add unique index on address_hash, block_hash and address_type for block_rewards --- .../20190208113202_add_unique_index_to_rewards.exs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 apps/explorer/priv/repo/migrations/20190208113202_add_unique_index_to_rewards.exs diff --git a/apps/explorer/priv/repo/migrations/20190208113202_add_unique_index_to_rewards.exs b/apps/explorer/priv/repo/migrations/20190208113202_add_unique_index_to_rewards.exs new file mode 100644 index 0000000000..cd306c7977 --- /dev/null +++ b/apps/explorer/priv/repo/migrations/20190208113202_add_unique_index_to_rewards.exs @@ -0,0 +1,12 @@ +defmodule Explorer.Repo.Migrations.AddUniqueIndexToRewards do + use Ecto.Migration + + def change do + create( + unique_index( + :block_rewards, + [:address_hash, :block_hash, :address_type] + ) + ) + end +end From 5cf4f860204b700ffc176ded668b33842885f561 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 8 Feb 2019 15:08:32 +0300 Subject: [PATCH 2/5] deduplicate input data --- .../lib/explorer/chain/import/runner/block/rewards.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex b/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex index 3ce55fbf07..2ada9eaa7d 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex +++ b/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex @@ -53,7 +53,11 @@ defmodule Explorer.Chain.Import.Runner.Block.Rewards do on_conflict = Map.get_lazy(options, :on_conflict, &default_on_conflict/0) # order so that row ShareLocks are grabbed in a consistent order - ordered_changes_list = Enum.sort_by(changes_list, &{&1.address_hash, &1.address_type, &1.block_hash}) + ordered_changes_list = + changes_list + |> Enum.uniq_by(&{&1.address_hash, &1.address_type, &1.block_hash}) + |> Enum.sort_by(&{&1.address_hash, &1.address_type, &1.block_hash}) + Import.insert_changes_list( repo, From b9bcd6ce98b1a9f2f0770ebe405d102e15803854 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 8 Feb 2019 15:10:50 +0300 Subject: [PATCH 3/5] mix format --- apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex b/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex index 2ada9eaa7d..c30a919b59 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex +++ b/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex @@ -58,7 +58,6 @@ defmodule Explorer.Chain.Import.Runner.Block.Rewards do |> Enum.uniq_by(&{&1.address_hash, &1.address_type, &1.block_hash}) |> Enum.sort_by(&{&1.address_hash, &1.address_type, &1.block_hash}) - Import.insert_changes_list( repo, ordered_changes_list, From 122d8baa84773158d1c588e779e3828021c5874d Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 11 Feb 2019 11:09:41 +0300 Subject: [PATCH 4/5] deduplicate input params in block reward fetcher --- .../lib/explorer/chain/import/runner/block/rewards.ex | 5 +---- apps/indexer/lib/indexer/block/reward/fetcher.ex | 4 +++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex b/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex index c30a919b59..3ce55fbf07 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex +++ b/apps/explorer/lib/explorer/chain/import/runner/block/rewards.ex @@ -53,10 +53,7 @@ defmodule Explorer.Chain.Import.Runner.Block.Rewards do on_conflict = Map.get_lazy(options, :on_conflict, &default_on_conflict/0) # order so that row ShareLocks are grabbed in a consistent order - ordered_changes_list = - changes_list - |> Enum.uniq_by(&{&1.address_hash, &1.address_type, &1.block_hash}) - |> Enum.sort_by(&{&1.address_hash, &1.address_type, &1.block_hash}) + ordered_changes_list = Enum.sort_by(changes_list, &{&1.address_hash, &1.address_type, &1.block_hash}) Import.insert_changes_list( repo, diff --git a/apps/indexer/lib/indexer/block/reward/fetcher.ex b/apps/indexer/lib/indexer/block/reward/fetcher.ex index 7e89166c74..fe1d5d2bf2 100644 --- a/apps/indexer/lib/indexer/block/reward/fetcher.ex +++ b/apps/indexer/lib/indexer/block/reward/fetcher.ex @@ -177,7 +177,8 @@ defmodule Indexer.Block.Reward.Fetcher do |> Enum.map(& &1.block_hash) |> Chain.gas_payment_by_block_hash() - Enum.map(beneficiaries_params, fn %{block_hash: block_hash} = beneficiary -> + beneficiaries_params + |> Enum.map(fn %{block_hash: block_hash} = beneficiary -> case gas_payment_by_block_hash do %{^block_hash => gas_payment} -> {:ok, minted} = Wei.cast(beneficiary.reward) @@ -187,6 +188,7 @@ defmodule Indexer.Block.Reward.Fetcher do beneficiary end end) + |> Enum.uniq() end defp import_block_reward_params(block_rewards_params) when is_list(block_rewards_params) do From 0be39ea2ec1abe0968d9f8dc035de4695c14ffb5 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Mon, 11 Feb 2019 17:18:36 +0300 Subject: [PATCH 5/5] fix cardinality error issue --- .../lib/indexer/block/reward/fetcher.ex | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/apps/indexer/lib/indexer/block/reward/fetcher.ex b/apps/indexer/lib/indexer/block/reward/fetcher.ex index fe1d5d2bf2..0c190f9719 100644 --- a/apps/indexer/lib/indexer/block/reward/fetcher.ex +++ b/apps/indexer/lib/indexer/block/reward/fetcher.ex @@ -177,18 +177,20 @@ defmodule Indexer.Block.Reward.Fetcher do |> Enum.map(& &1.block_hash) |> Chain.gas_payment_by_block_hash() - beneficiaries_params - |> Enum.map(fn %{block_hash: block_hash} = beneficiary -> - case gas_payment_by_block_hash do - %{^block_hash => gas_payment} -> - {:ok, minted} = Wei.cast(beneficiary.reward) - %{beneficiary | reward: Wei.sum(minted, gas_payment)} - - _ -> - beneficiary + Enum.map(beneficiaries_params, fn %{block_hash: block_hash, address_type: address_type} = beneficiary -> + if address_type == :validator do + case gas_payment_by_block_hash do + %{^block_hash => gas_payment} -> + {:ok, minted} = Wei.cast(beneficiary.reward) + %{beneficiary | reward: Wei.sum(minted, gas_payment)} + + _ -> + beneficiary + end + else + beneficiary end end) - |> Enum.uniq() end defp import_block_reward_params(block_rewards_params) when is_list(block_rewards_params) do