-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
137 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters