From f5268fc4504e084b1f8110de37cc93b8fc2fae1d Mon Sep 17 00:00:00 2001 From: goodsoft Date: Wed, 27 Mar 2019 15:33:01 +0200 Subject: [PATCH] Address review comments --- .../explorer/chain/import/runner/blocks.ex | 20 ++++++++----------- ...2921_lose_consensus_for_invalid_blocks.sql | 4 ++-- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/apps/explorer/lib/explorer/chain/import/runner/blocks.ex b/apps/explorer/lib/explorer/chain/import/runner/blocks.ex index c351bfd218..100f120290 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/blocks.ex +++ b/apps/explorer/lib/explorer/chain/import/runner/blocks.ex @@ -573,12 +573,10 @@ defmodule Explorer.Chain.Import.Runner.Blocks do initial = from(t in Transaction, where: false) Enum.reduce(blocks_changes, initial, fn %{consensus: consensus, hash: hash, number: number}, acc -> - case consensus do - false -> - from(transaction in acc, or_where: transaction.block_hash == ^hash and transaction.block_number == ^number) - - true -> - from(transaction in acc, or_where: transaction.block_hash != ^hash and transaction.block_number == ^number) + if consensus do + from(transaction in acc, or_where: transaction.block_hash != ^hash and transaction.block_number == ^number) + else + from(transaction in acc, or_where: transaction.block_hash == ^hash and transaction.block_number == ^number) end end) end @@ -587,12 +585,10 @@ defmodule Explorer.Chain.Import.Runner.Blocks do initial = from(b in Block, where: false) Enum.reduce(blocks_changes, initial, fn %{consensus: consensus, parent_hash: parent_hash, number: number}, acc -> - case consensus do - false -> - acc - - true -> - from(block in acc, or_where: block.number == ^(number - 1) and block.hash != ^parent_hash) + if consensus do + from(block in acc, or_where: block.number == ^(number - 1) and block.hash != ^parent_hash) + else + acc end end) end diff --git a/apps/explorer/priv/repo/migrations/scripts/20190326202921_lose_consensus_for_invalid_blocks.sql b/apps/explorer/priv/repo/migrations/scripts/20190326202921_lose_consensus_for_invalid_blocks.sql index 38e8678484..f201ccbece 100644 --- a/apps/explorer/priv/repo/migrations/scripts/20190326202921_lose_consensus_for_invalid_blocks.sql +++ b/apps/explorer/priv/repo/migrations/scripts/20190326202921_lose_consensus_for_invalid_blocks.sql @@ -1,5 +1,5 @@ -UPDATE blocks SET consensus = FALSE -WHERE number IN ( +UPDATE blocks SET consensus = FALSE, updated_at = NOW() +WHERE consensus AND number IN ( SELECT b0.number - 1 FROM "blocks" AS b0 LEFT JOIN "blocks" AS b1 ON (b0."parent_hash" = b1."hash") AND b1."consensus" WHERE b0."number" > 0 AND b0."consensus" AND b1."hash" IS NULL