Skip to content

Commit

Permalink
Merge pull request #2777 from poanetwork/pp-remove-duplicate-blocks-b…
Browse files Browse the repository at this point in the history
…efore-insertion

Remove duplicate blocks from changes_list before import
  • Loading branch information
vbaranov committed Nov 25, 2019
2 parents 6caefe3 + 3fbe951 commit 824cc33
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
- [#2799](https://github.com/poanetwork/blockscout/pull/2799) - fix catchup fetcher for empty node and db
- [#2783](https://github.com/poanetwork/blockscout/pull/2783) - Fix stuck value and ticker on the token page
- [#2781](https://github.com/poanetwork/blockscout/pull/2781) - optimize txlist json rpc
- [#2777](https://github.com/poanetwork/blockscout/pull/2777) - Remove duplicate blocks from changes_list before import
- [#2770](https://github.com/poanetwork/blockscout/pull/2770) - do not re-fetch token instances without uris
- [#2769](https://github.com/poanetwork/blockscout/pull/2769) - optimize token token transfers query
- [#2761](https://github.com/poanetwork/blockscout/pull/2761) - add indexes for token instances fetching queries
Expand Down
5 changes: 4 additions & 1 deletion apps/explorer/lib/explorer/chain/import/runner/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ defmodule Explorer.Chain.Import.Runner.Blocks do
on_conflict = Map.get_lazy(options, :on_conflict, &default_on_conflict/0)

# Enforce Block ShareLocks order (see docs: sharelocks.md)
ordered_changes_list = Enum.sort_by(changes_list, & &1.hash)
ordered_changes_list =
changes_list
|> Enum.sort_by(& &1.hash)
|> Enum.dedup_by(& &1.hash)

Import.insert_changes_list(
repo,
Expand Down
14 changes: 14 additions & 0 deletions apps/explorer/test/explorer/chain/import/runner/blocks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,20 @@ defmodule Explorer.Chain.Import.Runner.BlocksTest do
insert_transaction(transaction2, options)
assert Chain.missing_block_number_ranges(range) == [(block_number + 1)..(block_number + 1)]
end

test "removes duplicate blocks (by hash) before inserting",
%{consensus_block: %{number: block_number, hash: block_hash, miner_hash: miner_hash}, options: options} do
new_block = params_for(:block, miner_hash: miner_hash, consensus: true)

%Ecto.Changeset{valid?: true, changes: block_changes} = Block.changeset(%Block{}, new_block)

result =
Multi.new()
|> Blocks.run([block_changes, block_changes], options)
|> Repo.transaction()

assert {:ok, %{blocks: [%{hash: block_hash, consensus: true}]}} = result
end
end

defp insert_block(block_params, options) do
Expand Down

0 comments on commit 824cc33

Please sign in to comment.