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.
pull/802/head
Luke Imhoff 6 years ago
parent 483ea2339f
commit 07bd999e35
  1. 41
      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

Loading…
Cancel
Save