From 5ab216f0e057f00e89d6577ee7b7be0147a1b47d Mon Sep 17 00:00:00 2001 From: Luke Imhoff Date: Tue, 2 Oct 2018 07:48:43 -0500 Subject: [PATCH] Only lose consensus when a block will immediately claim consensus Fixes https://github.com/poanetwork/blockscout/pull/833#issuecomment-426102868. --- apps/explorer/lib/explorer/chain/import.ex | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/apps/explorer/lib/explorer/chain/import.ex b/apps/explorer/lib/explorer/chain/import.ex index 831e01b74f..470a93851a 100644 --- a/apps/explorer/lib/explorer/chain/import.ex +++ b/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}}) when is_list(blocks_changes) do - ordered_block_number = + ordered_consensus_block_number = 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() query = from( block in Block, - where: block.number in ^ordered_block_number, + where: block.number in ^ordered_consensus_block_number, update: [ set: [ consensus: false, @@ -1091,7 +1097,7 @@ defmodule Explorer.Chain.Import do {:ok, result} rescue 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