parent
70bceea089
commit
c83c0d39c7
@ -0,0 +1,48 @@ |
|||||||
|
defmodule Explorer.Chain.Cache.Uncles do |
||||||
|
@moduledoc """ |
||||||
|
Caches the last known uncles |
||||||
|
""" |
||||||
|
|
||||||
|
alias Explorer.Chain.Block |
||||||
|
alias Explorer.Repo |
||||||
|
|
||||||
|
use Explorer.Chain.OrderedCache, |
||||||
|
name: :uncles, |
||||||
|
max_size: 60, |
||||||
|
ids_list_key: "uncle_numbers", |
||||||
|
preload: :transactions, |
||||||
|
preload: [miner: :names], |
||||||
|
preload: :rewards, |
||||||
|
preload: :nephews, |
||||||
|
ttl_check_interval: Application.get_env(:explorer, __MODULE__)[:ttl_check_interval], |
||||||
|
global_ttl: Application.get_env(:explorer, __MODULE__)[:global_ttl] |
||||||
|
|
||||||
|
import Ecto.Query |
||||||
|
|
||||||
|
@type element :: Block.t() |
||||||
|
|
||||||
|
@type id :: non_neg_integer() |
||||||
|
|
||||||
|
def element_to_id(%Block{number: number}), do: number |
||||||
|
|
||||||
|
def update_from_second_degree_relations(second_degree_relations) when is_nil(second_degree_relations), do: :ok |
||||||
|
|
||||||
|
def update_from_second_degree_relations(second_degree_relations) do |
||||||
|
uncle_hashes = |
||||||
|
second_degree_relations |
||||||
|
|> Enum.map(& &1.uncle_hash) |
||||||
|
|> Enum.uniq() |
||||||
|
|
||||||
|
query = |
||||||
|
from( |
||||||
|
block in Block, |
||||||
|
where: block.consensus == false and block.hash in ^uncle_hashes, |
||||||
|
inner_join: nephews in assoc(block, :nephews), |
||||||
|
preload: [nephews: block] |
||||||
|
) |
||||||
|
|
||||||
|
query |
||||||
|
|> Repo.all() |
||||||
|
|> update() |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,28 @@ |
|||||||
|
defmodule Explorer.Chain.Cache.UnclesTest do |
||||||
|
use Explorer.DataCase |
||||||
|
|
||||||
|
alias Explorer.Chain.Cache.Uncles |
||||||
|
alias Explorer.Repo |
||||||
|
|
||||||
|
setup do |
||||||
|
Supervisor.terminate_child(Explorer.Supervisor, Uncles.child_id()) |
||||||
|
Supervisor.restart_child(Explorer.Supervisor, Uncles.child_id()) |
||||||
|
|
||||||
|
:ok |
||||||
|
end |
||||||
|
|
||||||
|
describe "update_from_second_degree_relations/1" do |
||||||
|
test "fetches an uncle from a second_degree_relation and adds it to the cache" do |
||||||
|
block = insert(:block) |
||||||
|
uncle = insert(:block, consensus: false) |
||||||
|
|
||||||
|
uncle_hash = uncle.hash |
||||||
|
|
||||||
|
second_degree_relation = insert(:block_second_degree_relation, uncle_hash: uncle_hash, nephew: block) |
||||||
|
|
||||||
|
Uncles.update_from_second_degree_relations([second_degree_relation]) |
||||||
|
|
||||||
|
assert [%{hash: uncle_hash}] = Uncles.all() |
||||||
|
end |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue