Only lose consensus when a block will immediately claim consensus

Fixes
https://github.com/poanetwork/blockscout/pull/833#issuecomment-426102868.
pull/833/head
Luke Imhoff 6 years ago
parent dcf5e3fe14
commit 5ab216f0e0
  1. 14
      apps/explorer/lib/explorer/chain/import.ex

@ -1068,15 +1068,21 @@ defmodule Explorer.Chain.Import do
defp lose_consensus(blocks_changes, %{timeout: timeout, timestamps: %{updated_at: updated_at}}) defp lose_consensus(blocks_changes, %{timeout: timeout, timestamps: %{updated_at: updated_at}})
when is_list(blocks_changes) do when is_list(blocks_changes) do
ordered_block_number = ordered_consensus_block_number =
blocks_changes blocks_changes
|> MapSet.new(& &1.number) |> Enum.reduce(MapSet.new(), fn
%{consensus: true, number: number}, acc ->
MapSet.put(acc, number)
%{consensus: false}, acc ->
acc
end)
|> Enum.sort() |> Enum.sort()
query = query =
from( from(
block in Block, block in Block,
where: block.number in ^ordered_block_number, where: block.number in ^ordered_consensus_block_number,
update: [ update: [
set: [ set: [
consensus: false, consensus: false,
@ -1091,7 +1097,7 @@ defmodule Explorer.Chain.Import do
{:ok, result} {:ok, result}
rescue rescue
postgrex_error in Postgrex.Error -> postgrex_error in Postgrex.Error ->
{:error, %{exception: postgrex_error, block_numbers: ordered_block_number}} {:error, %{exception: postgrex_error, consensus_block_numbers: ordered_consensus_block_number}}
end end
end end

Loading…
Cancel
Save