Group Explorer caches

Problem: a bunch of different caches are under the Explorer.Chain namespaces, cluttering the codebase

Solution: create a subdirectory and collect them all under the Explorer.Chain.Cache namespace
pull/2323/head
pasqu4le 5 years ago
parent 8bcb31912b
commit da775b07b7
No known key found for this signature in database
GPG Key ID: 8F3EE01F1DC90687
  1. 1
      CHANGELOG.md
  2. 4
      apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/block_controller.ex
  3. 4
      apps/block_scout_web/test/support/conn_case.ex
  4. 4
      apps/block_scout_web/test/support/feature_case.ex
  5. 2
      apps/explorer/config/config.exs
  6. 2
      apps/explorer/config/test.exs
  7. 26
      apps/explorer/lib/explorer/application.ex
  8. 32
      apps/explorer/lib/explorer/chain.ex
  9. 5
      apps/explorer/lib/explorer/chain/address.ex
  10. 2
      apps/explorer/lib/explorer/chain/cache/block_count.ex
  11. 2
      apps/explorer/lib/explorer/chain/cache/block_number.ex
  12. 2
      apps/explorer/lib/explorer/chain/cache/blocks.ex
  13. 2
      apps/explorer/lib/explorer/chain/cache/net_version.ex
  14. 2
      apps/explorer/lib/explorer/chain/cache/transaction_count.ex
  15. 2
      apps/explorer/lib/explorer/chain/cache/transactions.ex
  16. 5
      apps/explorer/lib/explorer/chain/supply/rsk.ex
  17. 59
      apps/explorer/test/explorer/chain/block_number_cache_test.exs
  18. 24
      apps/explorer/test/explorer/chain/cache/block_count_test.exs
  19. 59
      apps/explorer/test/explorer/chain/cache/block_number_test.exs
  20. 36
      apps/explorer/test/explorer/chain/cache/blocks_test.exs
  21. 54
      apps/explorer/test/explorer/chain/cache/transaction_count_test.exs
  22. 34
      apps/explorer/test/explorer/chain/cache/transactions_test.exs
  23. 54
      apps/explorer/test/explorer/chain/transaction_count_cache_test.exs
  24. 4
      apps/explorer/test/explorer/market/market_history_cache_test.exs
  25. 8
      apps/explorer/test/explorer/market/market_test.exs
  26. 10
      apps/explorer/test/support/data_case.ex
  27. 10
      apps/indexer/lib/indexer/block/fetcher.ex
  28. 5
      apps/indexer/lib/indexer/fetcher/coin_balance_on_demand.ex

@ -11,6 +11,7 @@
- [#2291](https://github.com/poanetwork/blockscout/pull/2291) - dashboard fix for md resolution, transactions load fix, block info row fix, addresses page issue, check mark issue - [#2291](https://github.com/poanetwork/blockscout/pull/2291) - dashboard fix for md resolution, transactions load fix, block info row fix, addresses page issue, check mark issue
### Chore ### Chore
- [#2323](https://github.com/poanetwork/blockscout/pull/2323) - Group Explorer caches
- [#2302](https://github.com/poanetwork/blockscout/pull/2302) - fix names for xDai source - [#2302](https://github.com/poanetwork/blockscout/pull/2302) - fix names for xDai source
- [#2289](https://github.com/poanetwork/blockscout/pull/2289) - Optional websockets for dev environment - [#2289](https://github.com/poanetwork/blockscout/pull/2289) - Optional websockets for dev environment
- [#2307](https://github.com/poanetwork/blockscout/pull/2307) - add GoJoy to README - [#2307](https://github.com/poanetwork/blockscout/pull/2307) - add GoJoy to README

@ -3,7 +3,7 @@ defmodule BlockScoutWeb.API.RPC.BlockController do
alias BlockScoutWeb.Chain, as: ChainWeb alias BlockScoutWeb.Chain, as: ChainWeb
alias Explorer.Chain alias Explorer.Chain
alias Explorer.Chain.BlockNumberCache alias Explorer.Chain.Cache.BlockNumber
def getblockreward(conn, params) do def getblockreward(conn, params) do
with {:block_param, {:ok, unsafe_block_number}} <- {:block_param, Map.fetch(params, "blockno")}, with {:block_param, {:ok, unsafe_block_number}} <- {:block_param, Map.fetch(params, "blockno")},
@ -27,7 +27,7 @@ defmodule BlockScoutWeb.API.RPC.BlockController do
def eth_block_number(conn, params) do def eth_block_number(conn, params) do
id = Map.get(params, "id", 1) id = Map.get(params, "id", 1)
max_block_number = BlockNumberCache.max_number() max_block_number = BlockNumber.max_number()
render(conn, :eth_block_number, number: max_block_number, id: id) render(conn, :eth_block_number, number: max_block_number, id: id)
end end

@ -38,8 +38,8 @@ defmodule BlockScoutWeb.ConnCase do
Ecto.Adapters.SQL.Sandbox.mode(Explorer.Repo, {:shared, self()}) Ecto.Adapters.SQL.Sandbox.mode(Explorer.Repo, {:shared, self()})
end end
Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.TransactionsCache.cache_name()}) Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.Cache.Transactions.cache_name()})
Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.TransactionsCache.cache_name()}) Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.Cache.Transactions.cache_name()})
{:ok, conn: Phoenix.ConnTest.build_conn()} {:ok, conn: Phoenix.ConnTest.build_conn()}
end end

@ -27,8 +27,8 @@ defmodule BlockScoutWeb.FeatureCase do
Ecto.Adapters.SQL.Sandbox.mode(Explorer.Repo, {:shared, self()}) Ecto.Adapters.SQL.Sandbox.mode(Explorer.Repo, {:shared, self()})
end end
Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.TransactionsCache.cache_name()}) Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.Cache.Transactions.cache_name()})
Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.TransactionsCache.cache_name()}) Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.Cache.Transactions.cache_name()})
metadata = Phoenix.Ecto.SQL.Sandbox.metadata_for(Explorer.Repo, self()) metadata = Phoenix.Ecto.SQL.Sandbox.metadata_for(Explorer.Repo, self())
{:ok, session} = Wallaby.start_session(metadata: metadata) {:ok, session} = Wallaby.start_session(metadata: metadata)

@ -19,7 +19,7 @@ config :explorer,
config :explorer, Explorer.Counters.AverageBlockTime, enabled: true config :explorer, Explorer.Counters.AverageBlockTime, enabled: true
config :explorer, Explorer.Chain.BlockNumberCache, enabled: true config :explorer, Explorer.Chain.Cache.BlockNumber, enabled: true
config :explorer, Explorer.ExchangeRates.Source.CoinMarketCap, config :explorer, Explorer.ExchangeRates.Source.CoinMarketCap,
pages: String.to_integer(System.get_env("COINMARKETCAP_PAGES") || "10") pages: String.to_integer(System.get_env("COINMARKETCAP_PAGES") || "10")

@ -13,7 +13,7 @@ config :explorer, Explorer.Repo,
config :explorer, Explorer.ExchangeRates, enabled: false, store: :ets config :explorer, Explorer.ExchangeRates, enabled: false, store: :ets
config :explorer, Explorer.Chain.BlockNumberCache, enabled: false config :explorer, Explorer.Chain.Cache.BlockNumber, enabled: false
config :explorer, Explorer.KnownTokens, enabled: false, store: :ets config :explorer, Explorer.KnownTokens, enabled: false, store: :ets

@ -7,13 +7,13 @@ defmodule Explorer.Application do
alias Explorer.Admin alias Explorer.Admin
alias Explorer.Chain.{ alias Explorer.Chain.Cache.{
BlockCountCache, BlockCount,
BlockNumberCache, BlockNumber,
BlocksCache, Blocks,
NetVersionCache, NetVersion,
TransactionCountCache, TransactionCount,
TransactionsCache Transactions
} }
alias Explorer.Chain.Supply.RSK alias Explorer.Chain.Supply.RSK
@ -41,13 +41,13 @@ defmodule Explorer.Application do
Explorer.SmartContract.SolcDownloader, Explorer.SmartContract.SolcDownloader,
{Registry, keys: :duplicate, name: Registry.ChainEvents, id: Registry.ChainEvents}, {Registry, keys: :duplicate, name: Registry.ChainEvents, id: Registry.ChainEvents},
{Admin.Recovery, [[], [name: Admin.Recovery]]}, {Admin.Recovery, [[], [name: Admin.Recovery]]},
{TransactionCountCache, [[], []]}, {TransactionCount, [[], []]},
{BlockCountCache, []}, {BlockCount, []},
con_cache_child_spec(BlocksCache.cache_name()), con_cache_child_spec(Blocks.cache_name()),
con_cache_child_spec(NetVersionCache.cache_name()), con_cache_child_spec(NetVersion.cache_name()),
con_cache_child_spec(MarketHistoryCache.cache_name()), con_cache_child_spec(MarketHistoryCache.cache_name()),
con_cache_child_spec(RSK.cache_name(), ttl_check_interval: :timer.minutes(1), global_ttl: :timer.minutes(30)), con_cache_child_spec(RSK.cache_name(), ttl_check_interval: :timer.minutes(1), global_ttl: :timer.minutes(30)),
con_cache_child_spec(TransactionsCache.cache_name()) con_cache_child_spec(Transactions.cache_name())
] ]
children = base_children ++ configurable_children() children = base_children ++ configurable_children()
@ -56,7 +56,7 @@ defmodule Explorer.Application do
res = Supervisor.start_link(children, opts) res = Supervisor.start_link(children, opts)
BlockNumberCache.setup() BlockNumber.setup()
res res
end end

@ -31,9 +31,6 @@ defmodule Explorer.Chain do
Address.CurrentTokenBalance, Address.CurrentTokenBalance,
Address.TokenBalance, Address.TokenBalance,
Block, Block,
BlockCountCache,
BlockNumberCache,
BlocksCache,
Data, Data,
DecompiledSmartContract, DecompiledSmartContract,
Hash, Hash,
@ -45,12 +42,19 @@ defmodule Explorer.Chain do
Token, Token,
TokenTransfer, TokenTransfer,
Transaction, Transaction,
TransactionCountCache,
TransactionsCache,
Wei Wei
} }
alias Explorer.Chain.Block.{EmissionReward, Reward} alias Explorer.Chain.Block.{EmissionReward, Reward}
alias Explorer.Chain.Cache.{
BlockCount,
BlockNumber,
Blocks,
TransactionCount,
Transactions
}
alias Explorer.Chain.Import.Runner alias Explorer.Chain.Import.Runner
alias Explorer.Counters.AddressesWithBalanceCounter alias Explorer.Counters.AddressesWithBalanceCounter
alias Explorer.Market.MarketHistoryCache alias Explorer.Market.MarketHistoryCache
@ -266,7 +270,7 @@ defmodule Explorer.Chain do
when is_list(options) do when is_list(options) do
paging_options = Keyword.get(options, :paging_options) || %PagingOptions{page_size: 50} paging_options = Keyword.get(options, :paging_options) || %PagingOptions{page_size: 50}
{block_number, transaction_index, log_index} = paging_options.key || {BlockNumberCache.max_number(), 0, 0} {block_number, transaction_index, log_index} = paging_options.key || {BlockNumber.max_number(), 0, 0}
base_query = base_query =
from(log in Log, from(log in Log,
@ -1132,7 +1136,7 @@ defmodule Explorer.Chain do
""" """
@spec indexed_ratio() :: Decimal.t() @spec indexed_ratio() :: Decimal.t()
def indexed_ratio do def indexed_ratio do
{min, max} = BlockNumberCache.min_and_max_numbers() {min, max} = BlockNumber.min_and_max_numbers()
case {min, max} do case {min, max} do
{0, 0} -> {0, 0} ->
@ -1214,12 +1218,12 @@ defmodule Explorer.Chain do
block_type = Keyword.get(options, :block_type, "Block") block_type = Keyword.get(options, :block_type, "Block")
if block_type == "Block" && !paging_options.key do if block_type == "Block" && !paging_options.key do
if BlocksCache.enough_elements?(paging_options.page_size) do if Blocks.enough_elements?(paging_options.page_size) do
BlocksCache.blocks(paging_options.page_size) Blocks.blocks(paging_options.page_size)
else else
elements = fetch_blocks(block_type, paging_options, necessity_by_association) elements = fetch_blocks(block_type, paging_options, necessity_by_association)
BlocksCache.rewrite_cache(elements) Blocks.rewrite_cache(elements)
elements elements
end end
@ -1978,11 +1982,11 @@ defmodule Explorer.Chain do
if is_nil(paging_options.key) do if is_nil(paging_options.key) do
paging_options.page_size paging_options.page_size
|> TransactionsCache.take_enough() |> Transactions.take_enough()
|> case do |> case do
nil -> nil ->
transactions = fetch_recent_collated_transactions(paging_options, necessity_by_association) transactions = fetch_recent_collated_transactions(paging_options, necessity_by_association)
TransactionsCache.update(transactions) Transactions.update(transactions)
transactions transactions
transactions -> transactions ->
@ -2127,7 +2131,7 @@ defmodule Explorer.Chain do
""" """
@spec transaction_estimated_count() :: non_neg_integer() @spec transaction_estimated_count() :: non_neg_integer()
def transaction_estimated_count do def transaction_estimated_count do
cached_value = TransactionCountCache.value() cached_value = TransactionCount.value()
if is_nil(cached_value) do if is_nil(cached_value) do
%Postgrex.Result{rows: [[rows]]} = %Postgrex.Result{rows: [[rows]]} =
@ -2146,7 +2150,7 @@ defmodule Explorer.Chain do
""" """
@spec block_estimated_count() :: non_neg_integer() @spec block_estimated_count() :: non_neg_integer()
def block_estimated_count do def block_estimated_count do
cached_value = BlockCountCache.count() cached_value = BlockCount.count()
if is_nil(cached_value) do if is_nil(cached_value) do
%Postgrex.Result{rows: [[count]]} = Repo.query!("SELECT reltuples FROM pg_class WHERE relname = 'blocks';") %Postgrex.Result{rows: [[count]]} = Repo.query!("SELECT reltuples FROM pg_class WHERE relname = 'blocks';")

@ -16,13 +16,14 @@ defmodule Explorer.Chain.Address do
DecompiledSmartContract, DecompiledSmartContract,
Hash, Hash,
InternalTransaction, InternalTransaction,
NetVersionCache,
SmartContract, SmartContract,
Token, Token,
Transaction, Transaction,
Wei Wei
} }
alias Explorer.Chain.Cache.NetVersion
@optional_attrs ~w(contract_code fetched_coin_balance fetched_coin_balance_block_number nonce decompiled verified)a @optional_attrs ~w(contract_code fetched_coin_balance fetched_coin_balance_block_number nonce decompiled verified)a
@required_attrs ~w(hash)a @required_attrs ~w(hash)a
@allowed_attrs @optional_attrs ++ @required_attrs @allowed_attrs @optional_attrs ++ @required_attrs
@ -169,7 +170,7 @@ defmodule Explorer.Chain.Address do
end end
def rsk_checksum(hash) do def rsk_checksum(hash) do
chain_id = NetVersionCache.version() chain_id = NetVersion.version()
string_hash = string_hash =
hash hash

@ -1,4 +1,4 @@
defmodule Explorer.Chain.BlockCountCache do defmodule Explorer.Chain.Cache.BlockCount do
@moduledoc """ @moduledoc """
Cache for block count. Cache for block count.
""" """

@ -1,4 +1,4 @@
defmodule Explorer.Chain.BlockNumberCache do defmodule Explorer.Chain.Cache.BlockNumber do
@moduledoc """ @moduledoc """
Cache for max and min block numbers. Cache for max and min block numbers.
""" """

@ -1,4 +1,4 @@
defmodule Explorer.Chain.BlocksCache do defmodule Explorer.Chain.Cache.Blocks do
@moduledoc """ @moduledoc """
Caches the last imported blocks Caches the last imported blocks
""" """

@ -1,4 +1,4 @@
defmodule Explorer.Chain.NetVersionCache do defmodule Explorer.Chain.Cache.NetVersion do
@moduledoc """ @moduledoc """
Caches chain version. Caches chain version.
""" """

@ -1,4 +1,4 @@
defmodule Explorer.Chain.TransactionCountCache do defmodule Explorer.Chain.Cache.TransactionCount do
@moduledoc """ @moduledoc """
Cache for estimated transaction count. Cache for estimated transaction count.
""" """

@ -1,4 +1,4 @@
defmodule Explorer.Chain.TransactionsCache do defmodule Explorer.Chain.Cache.Transactions do
@moduledoc """ @moduledoc """
Caches the latest imported transactions Caches the latest imported transactions
""" """

@ -10,7 +10,8 @@ defmodule Explorer.Chain.Supply.RSK do
alias EthereumJSONRPC.FetchedBalances alias EthereumJSONRPC.FetchedBalances
alias Explorer.Chain.Address.CoinBalance alias Explorer.Chain.Address.CoinBalance
alias Explorer.Chain.{Block, BlockNumberCache, Wei} alias Explorer.Chain.{Block, Wei}
alias Explorer.Chain.Cache.BlockNumber
alias Explorer.Repo alias Explorer.Repo
@cache_name :rsk_balance @cache_name :rsk_balance
@ -99,7 +100,7 @@ defmodule Explorer.Chain.Supply.RSK do
def cache_name, do: @cache_name def cache_name, do: @cache_name
defp fetch_circulating_value do defp fetch_circulating_value do
max_number = BlockNumberCache.max_number() max_number = BlockNumber.max_number()
params = [ params = [
%{block_quantity: integer_to_quantity(max_number), hash_data: "0x0000000000000000000000000000000001000006"} %{block_quantity: integer_to_quantity(max_number), hash_data: "0x0000000000000000000000000000000001000006"}

@ -1,59 +0,0 @@
defmodule Explorer.Chain.BlockNumberCacheTest do
use Explorer.DataCase
alias Explorer.Chain.BlockNumberCache
setup do
Application.put_env(:explorer, Explorer.Chain.BlockNumberCache, enabled: true)
on_exit(fn ->
Application.put_env(:explorer, Explorer.Chain.BlockNumberCache, enabled: false)
end)
end
describe "max_number/1" do
test "returns max number" do
insert(:block, number: 5)
BlockNumberCache.setup()
assert BlockNumberCache.max_number() == 5
end
end
describe "min_number/1" do
test "returns max number" do
insert(:block, number: 2)
BlockNumberCache.setup()
assert BlockNumberCache.max_number() == 2
end
end
describe "update/1" do
test "updates max number" do
insert(:block, number: 2)
BlockNumberCache.setup()
assert BlockNumberCache.max_number() == 2
assert BlockNumberCache.update(3)
assert BlockNumberCache.max_number() == 3
end
test "updates min number" do
insert(:block, number: 2)
BlockNumberCache.setup()
assert BlockNumberCache.min_number() == 2
assert BlockNumberCache.update(1)
assert BlockNumberCache.min_number() == 1
end
end
end

@ -1,55 +1,55 @@
defmodule Explorer.Chain.BlockCountCacheTest do defmodule Explorer.Chain.Cache.BlockCountTest do
use Explorer.DataCase use Explorer.DataCase
alias Explorer.Chain.BlockCountCache alias Explorer.Chain.Cache.BlockCount
test "returns default transaction count" do test "returns default transaction count" do
BlockCountCache.start_link(name: BlockTestCache) BlockCount.start_link(name: BlockTestCache)
result = BlockCountCache.count(BlockTestCache) result = BlockCount.count(BlockTestCache)
assert is_nil(result) assert is_nil(result)
end end
test "updates cache if initial value is zero" do test "updates cache if initial value is zero" do
BlockCountCache.start_link(name: BlockTestCache) BlockCount.start_link(name: BlockTestCache)
insert(:block, consensus: true) insert(:block, consensus: true)
insert(:block, consensus: true) insert(:block, consensus: true)
insert(:block, consensus: false) insert(:block, consensus: false)
_result = BlockCountCache.count(BlockTestCache) _result = BlockCount.count(BlockTestCache)
Process.sleep(1000) Process.sleep(1000)
updated_value = BlockCountCache.count(BlockTestCache) updated_value = BlockCount.count(BlockTestCache)
assert updated_value == 2 assert updated_value == 2
end end
test "does not update cache if cache period did not pass" do test "does not update cache if cache period did not pass" do
BlockCountCache.start_link(name: BlockTestCache) BlockCount.start_link(name: BlockTestCache)
insert(:block, consensus: true) insert(:block, consensus: true)
insert(:block, consensus: true) insert(:block, consensus: true)
insert(:block, consensus: false) insert(:block, consensus: false)
_result = BlockCountCache.count(BlockTestCache) _result = BlockCount.count(BlockTestCache)
Process.sleep(1000) Process.sleep(1000)
updated_value = BlockCountCache.count(BlockTestCache) updated_value = BlockCount.count(BlockTestCache)
assert updated_value == 2 assert updated_value == 2
insert(:block, consensus: true) insert(:block, consensus: true)
insert(:block, consensus: true) insert(:block, consensus: true)
_updated_value = BlockCountCache.count(BlockTestCache) _updated_value = BlockCount.count(BlockTestCache)
Process.sleep(1000) Process.sleep(1000)
updated_value = BlockCountCache.count(BlockTestCache) updated_value = BlockCount.count(BlockTestCache)
assert updated_value == 2 assert updated_value == 2
end end

@ -0,0 +1,59 @@
defmodule Explorer.Chain.Cache.BlockNumberTest do
use Explorer.DataCase
alias Explorer.Chain.Cache.BlockNumber
setup do
Application.put_env(:explorer, Explorer.Chain.Cache.BlockNumber, enabled: true)
on_exit(fn ->
Application.put_env(:explorer, Explorer.Chain.Cache.BlockNumber, enabled: false)
end)
end
describe "max_number/1" do
test "returns max number" do
insert(:block, number: 5)
BlockNumber.setup()
assert BlockNumber.max_number() == 5
end
end
describe "min_number/1" do
test "returns max number" do
insert(:block, number: 2)
BlockNumber.setup()
assert BlockNumber.max_number() == 2
end
end
describe "update/1" do
test "updates max number" do
insert(:block, number: 2)
BlockNumber.setup()
assert BlockNumber.max_number() == 2
assert BlockNumber.update(3)
assert BlockNumber.max_number() == 3
end
test "updates min number" do
insert(:block, number: 2)
BlockNumber.setup()
assert BlockNumber.min_number() == 2
assert BlockNumber.update(1)
assert BlockNumber.min_number() == 1
end
end
end

@ -1,7 +1,7 @@
defmodule Explorer.Chain.BlocksCacheTest do defmodule Explorer.Chain.Cache.BlocksTest do
use Explorer.DataCase use Explorer.DataCase
alias Explorer.Chain.BlocksCache alias Explorer.Chain.Cache.Blocks
alias Explorer.Repo alias Explorer.Repo
setup do setup do
@ -14,9 +14,9 @@ defmodule Explorer.Chain.BlocksCacheTest do
test "adds a new value to cache" do test "adds a new value to cache" do
block = insert(:block) |> Repo.preload([:transactions, [miner: :names], :rewards]) block = insert(:block) |> Repo.preload([:transactions, [miner: :names], :rewards])
BlocksCache.update(block) Blocks.update(block)
assert BlocksCache.blocks() == [block] assert Blocks.blocks() == [block]
end end
test "adds a new elements removing the oldest one" do test "adds a new elements removing the oldest one" do
@ -25,43 +25,43 @@ defmodule Explorer.Chain.BlocksCacheTest do
|> Enum.map(fn number -> |> Enum.map(fn number ->
block = insert(:block, number: number) block = insert(:block, number: number)
BlocksCache.update(block) Blocks.update(block)
block.number block.number
end) end)
new_block = insert(:block, number: 70) new_block = insert(:block, number: 70)
BlocksCache.update(new_block) Blocks.update(new_block)
new_blocks = blocks |> List.replace_at(0, new_block.number) |> Enum.sort() |> Enum.reverse() new_blocks = blocks |> List.replace_at(0, new_block.number) |> Enum.sort() |> Enum.reverse()
assert Enum.map(BlocksCache.blocks(), & &1.number) == new_blocks assert Enum.map(Blocks.blocks(), & &1.number) == new_blocks
end end
test "does not add too old blocks" do test "does not add too old blocks" do
block = insert(:block, number: 100_000) |> Repo.preload([:transactions, [miner: :names], :rewards]) block = insert(:block, number: 100_000) |> Repo.preload([:transactions, [miner: :names], :rewards])
old_block = insert(:block, number: 1_000) old_block = insert(:block, number: 1_000)
BlocksCache.update(block) Blocks.update(block)
BlocksCache.update(old_block) Blocks.update(old_block)
assert BlocksCache.blocks() == [block] assert Blocks.blocks() == [block]
end end
test "adds missing element" do test "adds missing element" do
block1 = insert(:block, number: 10) block1 = insert(:block, number: 10)
block2 = insert(:block, number: 4) block2 = insert(:block, number: 4)
BlocksCache.update(block1) Blocks.update(block1)
BlocksCache.update(block2) Blocks.update(block2)
assert Enum.count(BlocksCache.blocks()) == 2 assert Enum.count(Blocks.blocks()) == 2
block3 = insert(:block, number: 6) block3 = insert(:block, number: 6)
BlocksCache.update(block3) Blocks.update(block3)
assert Enum.map(BlocksCache.blocks(), & &1.number) == [10, 6, 4] assert Enum.map(Blocks.blocks(), & &1.number) == [10, 6, 4]
end end
end end
@ -69,16 +69,16 @@ defmodule Explorer.Chain.BlocksCacheTest do
test "updates cache" do test "updates cache" do
block = insert(:block) block = insert(:block)
BlocksCache.update(block) Blocks.update(block)
block1 = insert(:block) |> Repo.preload([:transactions, [miner: :names], :rewards]) block1 = insert(:block) |> Repo.preload([:transactions, [miner: :names], :rewards])
block2 = insert(:block) |> Repo.preload([:transactions, [miner: :names], :rewards]) block2 = insert(:block) |> Repo.preload([:transactions, [miner: :names], :rewards])
new_blocks = [block1, block2] new_blocks = [block1, block2]
BlocksCache.rewrite_cache(new_blocks) Blocks.rewrite_cache(new_blocks)
assert BlocksCache.blocks() == [block2, block1] assert Blocks.blocks() == [block2, block1]
end end
end end
end end

@ -0,0 +1,54 @@
defmodule Explorer.Chain.Cache.TransactionCountTest do
use Explorer.DataCase
alias Explorer.Chain.Cache.TransactionCount
test "returns default transaction count" do
TransactionCount.start_link([[], [name: TestCache]])
result = TransactionCount.value(TestCache)
assert is_nil(result)
end
test "updates cache if initial value is zero" do
TransactionCount.start_link([[], [name: TestCache]])
insert(:transaction)
insert(:transaction)
_result = TransactionCount.value(TestCache)
Process.sleep(1000)
updated_value = TransactionCount.value(TestCache)
assert updated_value == 2
end
test "does not update cache if cache period did not pass" do
TransactionCount.start_link([[], [name: TestCache]])
insert(:transaction)
insert(:transaction)
_result = TransactionCount.value(TestCache)
Process.sleep(1000)
updated_value = TransactionCount.value(TestCache)
assert updated_value == 2
insert(:transaction)
insert(:transaction)
_updated_value = TransactionCount.value(TestCache)
Process.sleep(1000)
updated_value = TransactionCount.value(TestCache)
assert updated_value == 2
end
end

@ -1,7 +1,7 @@
defmodule Explorer.Chain.TransactionsCacheTest do defmodule Explorer.Chain.Cache.TransactionsTest do
use Explorer.DataCase use Explorer.DataCase
alias Explorer.Chain.TransactionsCache alias Explorer.Chain.Cache.Transactions
alias Explorer.Repo alias Explorer.Repo
@size 51 @size 51
@ -10,9 +10,9 @@ defmodule Explorer.Chain.TransactionsCacheTest do
test "adds a new value to a new cache with preloads" do test "adds a new value to a new cache with preloads" do
transaction = insert(:transaction) |> preload_all() transaction = insert(:transaction) |> preload_all()
TransactionsCache.update(transaction) Transactions.update(transaction)
assert TransactionsCache.take(1) == [transaction] assert Transactions.take(1) == [transaction]
end end
test "adds several elements, removing the oldest when necessary" do test "adds several elements, removing the oldest when necessary" do
@ -23,9 +23,9 @@ defmodule Explorer.Chain.TransactionsCacheTest do
insert(:transaction) |> with_block(block) insert(:transaction) |> with_block(block)
end) end)
TransactionsCache.update(transactions) Transactions.update(transactions)
assert TransactionsCache.all() == Enum.reverse(preload_all(transactions)) assert Transactions.all() == Enum.reverse(preload_all(transactions))
more_transactions = more_transactions =
(@size + 1)..(@size + 10) (@size + 1)..(@size + 10)
@ -34,14 +34,14 @@ defmodule Explorer.Chain.TransactionsCacheTest do
insert(:transaction) |> with_block(block) insert(:transaction) |> with_block(block)
end) end)
TransactionsCache.update(more_transactions) Transactions.update(more_transactions)
kept_transactions = kept_transactions =
Enum.reverse(transactions ++ more_transactions) Enum.reverse(transactions ++ more_transactions)
|> Enum.take(@size) |> Enum.take(@size)
|> preload_all() |> preload_all()
assert TransactionsCache.take(@size) == kept_transactions assert Transactions.take(@size) == kept_transactions
end end
test "does not add a transaction too old when full" do test "does not add a transaction too old when full" do
@ -52,28 +52,28 @@ defmodule Explorer.Chain.TransactionsCacheTest do
insert(:transaction) |> with_block(block) insert(:transaction) |> with_block(block)
end) end)
TransactionsCache.update(transactions) Transactions.update(transactions)
loaded_transactions = Enum.reverse(preload_all(transactions)) loaded_transactions = Enum.reverse(preload_all(transactions))
assert TransactionsCache.all() == loaded_transactions assert Transactions.all() == loaded_transactions
block = insert(:block, number: 1) block = insert(:block, number: 1)
insert(:transaction) |> with_block(block) |> TransactionsCache.update() insert(:transaction) |> with_block(block) |> Transactions.update()
assert TransactionsCache.all() == loaded_transactions assert Transactions.all() == loaded_transactions
end end
test "adds intermediate transactions" do test "adds intermediate transactions" do
blocks = 1..10 |> Map.new(fn n -> {n, insert(:block, number: n)} end) blocks = 1..10 |> Map.new(fn n -> {n, insert(:block, number: n)} end)
insert(:transaction) |> with_block(blocks[1]) |> TransactionsCache.update() insert(:transaction) |> with_block(blocks[1]) |> Transactions.update()
insert(:transaction) |> with_block(blocks[10]) |> TransactionsCache.update() insert(:transaction) |> with_block(blocks[10]) |> Transactions.update()
assert TransactionsCache.size() == 2 assert Transactions.size() == 2
insert(:transaction) |> with_block(blocks[5]) |> TransactionsCache.update() insert(:transaction) |> with_block(blocks[5]) |> Transactions.update()
assert TransactionsCache.size() == 3 assert Transactions.size() == 3
end end
end end

@ -1,54 +0,0 @@
defmodule Explorer.Chain.TransactionCountCacheTest do
use Explorer.DataCase
alias Explorer.Chain.TransactionCountCache
test "returns default transaction count" do
TransactionCountCache.start_link([[], [name: TestCache]])
result = TransactionCountCache.value(TestCache)
assert is_nil(result)
end
test "updates cache if initial value is zero" do
TransactionCountCache.start_link([[], [name: TestCache]])
insert(:transaction)
insert(:transaction)
_result = TransactionCountCache.value(TestCache)
Process.sleep(1000)
updated_value = TransactionCountCache.value(TestCache)
assert updated_value == 2
end
test "does not update cache if cache period did not pass" do
TransactionCountCache.start_link([[], [name: TestCache]])
insert(:transaction)
insert(:transaction)
_result = TransactionCountCache.value(TestCache)
Process.sleep(1000)
updated_value = TransactionCountCache.value(TestCache)
assert updated_value == 2
insert(:transaction)
insert(:transaction)
_updated_value = TransactionCountCache.value(TestCache)
Process.sleep(1000)
updated_value = TransactionCountCache.value(TestCache)
assert updated_value == 2
end
end

@ -9,8 +9,8 @@ defmodule Explorer.Market.MarketHistoryCacheTest do
Supervisor.restart_child(Explorer.Supervisor, {ConCache, MarketHistoryCache.cache_name()}) Supervisor.restart_child(Explorer.Supervisor, {ConCache, MarketHistoryCache.cache_name()})
on_exit(fn -> on_exit(fn ->
Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.BlocksCache.cache_name()}) Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.Cache.Blocks.cache_name()})
Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.BlocksCache.cache_name()}) Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.Cache.Blocks.cache_name()})
end) end)
:ok :ok

@ -6,12 +6,12 @@ defmodule Explorer.MarketTest do
alias Explorer.Repo alias Explorer.Repo
setup do setup do
Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.BlocksCache.cache_name()}) Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.Cache.Blocks.cache_name()})
Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.BlocksCache.cache_name()}) Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.Cache.Blocks.cache_name()})
on_exit(fn -> on_exit(fn ->
Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.BlocksCache.cache_name()}) Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.Cache.Blocks.cache_name()})
Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.BlocksCache.cache_name()}) Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.Cache.Blocks.cache_name()})
end) end)
:ok :ok

@ -39,11 +39,11 @@ defmodule Explorer.DataCase do
Ecto.Adapters.SQL.Sandbox.mode(Explorer.Repo, {:shared, self()}) Ecto.Adapters.SQL.Sandbox.mode(Explorer.Repo, {:shared, self()})
end end
Explorer.Chain.BlockNumberCache.setup() Explorer.Chain.Cache.BlockNumber.setup()
Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.BlocksCache.cache_name()}) Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.Cache.Blocks.cache_name()})
Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.BlocksCache.cache_name()}) Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.Cache.Blocks.cache_name()})
Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.TransactionsCache.cache_name()}) Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.Cache.Transactions.cache_name()})
Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.TransactionsCache.cache_name()}) Supervisor.restart_child(Explorer.Supervisor, {ConCache, Explorer.Chain.Cache.Transactions.cache_name()})
:ok :ok
end end

@ -11,7 +11,9 @@ defmodule Indexer.Block.Fetcher do
alias EthereumJSONRPC.{Blocks, FetchedBeneficiaries} alias EthereumJSONRPC.{Blocks, FetchedBeneficiaries}
alias Explorer.Chain alias Explorer.Chain
alias Explorer.Chain.{Address, Block, BlockNumberCache, BlocksCache, Hash, Import, Transaction, TransactionsCache} alias Explorer.Chain.{Address, Block, Hash, Import, Transaction}
alias Explorer.Chain.Cache.Blocks, as: BlocksCache
alias Explorer.Chain.Cache.{BlockNumber, Transactions}
alias Indexer.Block.Fetcher.Receipts alias Indexer.Block.Fetcher.Receipts
alias Indexer.Fetcher.{ alias Indexer.Fetcher.{
@ -185,13 +187,13 @@ defmodule Indexer.Block.Fetcher do
max_block = Enum.max_by(blocks, fn block -> block.number end) max_block = Enum.max_by(blocks, fn block -> block.number end)
min_block = Enum.min_by(blocks, fn block -> block.number end) min_block = Enum.min_by(blocks, fn block -> block.number end)
BlockNumberCache.update(max_block.number) BlockNumber.update(max_block.number)
BlockNumberCache.update(min_block.number) BlockNumber.update(min_block.number)
BlocksCache.update_blocks(blocks) BlocksCache.update_blocks(blocks)
end end
defp update_transactions_cache(transactions) do defp update_transactions_cache(transactions) do
TransactionsCache.update(transactions) Transactions.update(transactions)
end end
def import( def import(

@ -17,8 +17,9 @@ defmodule Indexer.Fetcher.CoinBalanceOnDemand do
alias EthereumJSONRPC.FetchedBalances alias EthereumJSONRPC.FetchedBalances
alias Explorer.{Chain, Repo} alias Explorer.{Chain, Repo}
alias Explorer.Chain.{Address, BlockNumberCache} alias Explorer.Chain.Address
alias Explorer.Chain.Address.CoinBalance alias Explorer.Chain.Address.CoinBalance
alias Explorer.Chain.Cache.BlockNumber
alias Explorer.Counters.AverageBlockTime alias Explorer.Counters.AverageBlockTime
alias Indexer.Fetcher.CoinBalance, as: CoinBalanceFetcher alias Indexer.Fetcher.CoinBalance, as: CoinBalanceFetcher
alias Timex.Duration alias Timex.Duration
@ -161,7 +162,7 @@ defmodule Indexer.Fetcher.CoinBalanceOnDemand do
end end
defp latest_block_number do defp latest_block_number do
BlockNumberCache.max_number() BlockNumber.max_number()
end end
defp stale_balance_window(block_number) do defp stale_balance_window(block_number) do

Loading…
Cancel
Save