From 07bd999e35b7eb065153a623917c58c057a98278 Mon Sep 17 00:00:00 2001 From: Luke Imhoff Date: Tue, 25 Sep 2018 10:22:28 -0500 Subject: [PATCH] update_block_second_degree_relations After blocks are inserted, mark any rows in block_second_degree_relations as uncle_fetched_at where the uncle_hash matches the blocks hashes, so that stream_unfetched_uncle_hashes will no longer return those blocks. --- apps/explorer/lib/explorer/chain/import.ex | 41 +++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/apps/explorer/lib/explorer/chain/import.ex b/apps/explorer/lib/explorer/chain/import.ex index 22987fb5c1..cc1e99e16b 100644 --- a/apps/explorer/lib/explorer/chain/import.ex +++ b/apps/explorer/lib/explorer/chain/import.ex @@ -378,7 +378,8 @@ defmodule Explorer.Chain.Import do %{Block => blocks_changes} -> timestamps = Map.fetch!(options, :timestamps) - Multi.run(multi, :blocks, fn _ -> + multi + |> Multi.run(:blocks, fn _ -> insert_blocks( blocks_changes, %{ @@ -387,6 +388,16 @@ defmodule Explorer.Chain.Import do } ) end) + |> Multi.run(:uncle_fetched_block_second_degree_relations, fn %{blocks: blocks} when is_list(blocks) -> + update_block_second_degree_relations( + blocks, + %{ + timeout: + options[:block_second_degree_relations][:timeout] || @insert_block_second_degree_relations_timeout, + timestamps: timestamps + } + ) + end) _ -> multi @@ -965,6 +976,34 @@ defmodule Explorer.Chain.Import do {:ok, inserted} end + defp update_block_second_degree_relations(blocks, %{timeout: timeout, timestamps: %{updated_at: updated_at}}) + when is_list(blocks) do + ordered_uncle_hashes = + blocks + |> MapSet.new(& &1.hash) + |> Enum.sort() + + query = + from( + bsdr in Block.SecondDegreeRelation, + where: bsdr.uncle_hash in ^ordered_uncle_hashes, + update: [ + set: [ + uncle_fetched_at: ^updated_at + ] + ] + ) + + try do + {_, result} = Repo.update_all(query, [], timeout: timeout) + + {:ok, result} + rescue + postgrex_error in Postgrex.Error -> + {:error, %{exception: postgrex_error, uncle_hashes: ordered_uncle_hashes}} + end + end + defp update_transactions(internal_transactions, %{ timeout: timeout, timestamps: timestamps