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
### 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
- [#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

@ -3,7 +3,7 @@ defmodule BlockScoutWeb.API.RPC.BlockController do
alias BlockScoutWeb.Chain, as: ChainWeb
alias Explorer.Chain
alias Explorer.Chain.BlockNumberCache
alias Explorer.Chain.Cache.BlockNumber
def getblockreward(conn, params) do
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
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)
end

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

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

@ -19,7 +19,7 @@ config :explorer,
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,
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.Chain.BlockNumberCache, enabled: false
config :explorer, Explorer.Chain.Cache.BlockNumber, enabled: false
config :explorer, Explorer.KnownTokens, enabled: false, store: :ets

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

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

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

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

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

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

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

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

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

@ -10,7 +10,8 @@ defmodule Explorer.Chain.Supply.RSK do
alias EthereumJSONRPC.FetchedBalances
alias Explorer.Chain.Address.CoinBalance
alias Explorer.Chain.{Block, BlockNumberCache, Wei}
alias Explorer.Chain.{Block, Wei}
alias Explorer.Chain.Cache.BlockNumber
alias Explorer.Repo
@cache_name :rsk_balance
@ -99,7 +100,7 @@ defmodule Explorer.Chain.Supply.RSK do
def cache_name, do: @cache_name
defp fetch_circulating_value do
max_number = BlockNumberCache.max_number()
max_number = BlockNumber.max_number()
params = [
%{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
alias Explorer.Chain.BlockCountCache
alias Explorer.Chain.Cache.BlockCount
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)
end
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: false)
_result = BlockCountCache.count(BlockTestCache)
_result = BlockCount.count(BlockTestCache)
Process.sleep(1000)
updated_value = BlockCountCache.count(BlockTestCache)
updated_value = BlockCount.count(BlockTestCache)
assert updated_value == 2
end
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: false)
_result = BlockCountCache.count(BlockTestCache)
_result = BlockCount.count(BlockTestCache)
Process.sleep(1000)
updated_value = BlockCountCache.count(BlockTestCache)
updated_value = BlockCount.count(BlockTestCache)
assert updated_value == 2
insert(:block, consensus: true)
insert(:block, consensus: true)
_updated_value = BlockCountCache.count(BlockTestCache)
_updated_value = BlockCount.count(BlockTestCache)
Process.sleep(1000)
updated_value = BlockCountCache.count(BlockTestCache)
updated_value = BlockCount.count(BlockTestCache)
assert updated_value == 2
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
alias Explorer.Chain.BlocksCache
alias Explorer.Chain.Cache.Blocks
alias Explorer.Repo
setup do
@ -14,9 +14,9 @@ defmodule Explorer.Chain.BlocksCacheTest do
test "adds a new value to cache" do
block = insert(:block) |> Repo.preload([:transactions, [miner: :names], :rewards])
BlocksCache.update(block)
Blocks.update(block)
assert BlocksCache.blocks() == [block]
assert Blocks.blocks() == [block]
end
test "adds a new elements removing the oldest one" do
@ -25,43 +25,43 @@ defmodule Explorer.Chain.BlocksCacheTest do
|> Enum.map(fn number ->
block = insert(:block, number: number)
BlocksCache.update(block)
Blocks.update(block)
block.number
end)
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()
assert Enum.map(BlocksCache.blocks(), & &1.number) == new_blocks
assert Enum.map(Blocks.blocks(), & &1.number) == new_blocks
end
test "does not add too old blocks" do
block = insert(:block, number: 100_000) |> Repo.preload([:transactions, [miner: :names], :rewards])
old_block = insert(:block, number: 1_000)
BlocksCache.update(block)
BlocksCache.update(old_block)
Blocks.update(block)
Blocks.update(old_block)
assert BlocksCache.blocks() == [block]
assert Blocks.blocks() == [block]
end
test "adds missing element" do
block1 = insert(:block, number: 10)
block2 = insert(:block, number: 4)
BlocksCache.update(block1)
BlocksCache.update(block2)
Blocks.update(block1)
Blocks.update(block2)
assert Enum.count(BlocksCache.blocks()) == 2
assert Enum.count(Blocks.blocks()) == 2
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
@ -69,16 +69,16 @@ defmodule Explorer.Chain.BlocksCacheTest do
test "updates cache" do
block = insert(:block)
BlocksCache.update(block)
Blocks.update(block)
block1 = insert(:block) |> Repo.preload([:transactions, [miner: :names], :rewards])
block2 = insert(:block) |> Repo.preload([:transactions, [miner: :names], :rewards])
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

@ -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
alias Explorer.Chain.TransactionsCache
alias Explorer.Chain.Cache.Transactions
alias Explorer.Repo
@size 51
@ -10,9 +10,9 @@ defmodule Explorer.Chain.TransactionsCacheTest do
test "adds a new value to a new cache with preloads" do
transaction = insert(:transaction) |> preload_all()
TransactionsCache.update(transaction)
Transactions.update(transaction)
assert TransactionsCache.take(1) == [transaction]
assert Transactions.take(1) == [transaction]
end
test "adds several elements, removing the oldest when necessary" do
@ -23,9 +23,9 @@ defmodule Explorer.Chain.TransactionsCacheTest do
insert(:transaction) |> with_block(block)
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 =
(@size + 1)..(@size + 10)
@ -34,14 +34,14 @@ defmodule Explorer.Chain.TransactionsCacheTest do
insert(:transaction) |> with_block(block)
end)
TransactionsCache.update(more_transactions)
Transactions.update(more_transactions)
kept_transactions =
Enum.reverse(transactions ++ more_transactions)
|> Enum.take(@size)
|> preload_all()
assert TransactionsCache.take(@size) == kept_transactions
assert Transactions.take(@size) == kept_transactions
end
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)
end)
TransactionsCache.update(transactions)
Transactions.update(transactions)
loaded_transactions = Enum.reverse(preload_all(transactions))
assert TransactionsCache.all() == loaded_transactions
assert Transactions.all() == loaded_transactions
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
test "adds intermediate transactions" do
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[10]) |> TransactionsCache.update()
insert(:transaction) |> with_block(blocks[1]) |> Transactions.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

@ -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()})
on_exit(fn ->
Supervisor.terminate_child(Explorer.Supervisor, {ConCache, Explorer.Chain.BlocksCache.cache_name()})
Supervisor.restart_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.Cache.Blocks.cache_name()})
end)
:ok

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

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

@ -11,7 +11,9 @@ defmodule Indexer.Block.Fetcher do
alias EthereumJSONRPC.{Blocks, FetchedBeneficiaries}
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.Fetcher.{
@ -185,13 +187,13 @@ defmodule Indexer.Block.Fetcher do
max_block = Enum.max_by(blocks, fn block -> block.number end)
min_block = Enum.min_by(blocks, fn block -> block.number end)
BlockNumberCache.update(max_block.number)
BlockNumberCache.update(min_block.number)
BlockNumber.update(max_block.number)
BlockNumber.update(min_block.number)
BlocksCache.update_blocks(blocks)
end
defp update_transactions_cache(transactions) do
TransactionsCache.update(transactions)
Transactions.update(transactions)
end
def import(

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

Loading…
Cancel
Save