From da775b07b7bacc8e96a5af8a0ad19d226b8f6f24 Mon Sep 17 00:00:00 2001 From: pasqu4le Date: Tue, 9 Jul 2019 12:21:18 +0200 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + .../controllers/api/rpc/block_controller.ex | 4 +- .../block_scout_web/test/support/conn_case.ex | 4 +- .../test/support/feature_case.ex | 4 +- apps/explorer/config/config.exs | 2 +- apps/explorer/config/test.exs | 2 +- apps/explorer/lib/explorer/application.ex | 26 ++++---- apps/explorer/lib/explorer/chain.ex | 32 +++++----- apps/explorer/lib/explorer/chain/address.ex | 5 +- .../block_count.ex} | 2 +- .../block_number.ex} | 2 +- .../{blocks_cache.ex => cache/blocks.ex} | 2 +- .../net_version.ex} | 2 +- .../transaction_count.ex} | 2 +- .../transactions.ex} | 2 +- .../explorer/lib/explorer/chain/supply/rsk.ex | 5 +- .../chain/block_number_cache_test.exs | 59 ------------------- .../block_count_test.exs} | 24 ++++---- .../chain/cache/block_number_test.exs | 59 +++++++++++++++++++ .../blocks_test.exs} | 36 +++++------ .../chain/cache/transaction_count_test.exs | 54 +++++++++++++++++ .../transactions_test.exs} | 34 +++++------ .../chain/transaction_count_cache_test.exs | 54 ----------------- .../market/market_history_cache_test.exs | 4 +- .../test/explorer/market/market_test.exs | 8 +-- apps/explorer/test/support/data_case.ex | 10 ++-- apps/indexer/lib/indexer/block/fetcher.ex | 10 ++-- .../indexer/fetcher/coin_balance_on_demand.ex | 5 +- 28 files changed, 232 insertions(+), 222 deletions(-) rename apps/explorer/lib/explorer/chain/{block_count_cache.ex => cache/block_count.ex} (98%) rename apps/explorer/lib/explorer/chain/{block_number_cache.ex => cache/block_number.ex} (97%) rename apps/explorer/lib/explorer/chain/{blocks_cache.ex => cache/blocks.ex} (98%) rename apps/explorer/lib/explorer/chain/{net_version_cache.ex => cache/net_version.ex} (94%) rename apps/explorer/lib/explorer/chain/{transaction_count_cache.ex => cache/transaction_count.ex} (98%) rename apps/explorer/lib/explorer/chain/{transactions_cache.ex => cache/transactions.ex} (98%) delete mode 100644 apps/explorer/test/explorer/chain/block_number_cache_test.exs rename apps/explorer/test/explorer/chain/{block_count_cache_test.exs => cache/block_count_test.exs} (52%) create mode 100644 apps/explorer/test/explorer/chain/cache/block_number_test.exs rename apps/explorer/test/explorer/chain/{blocks_cache_test.exs => cache/blocks_test.exs} (65%) create mode 100644 apps/explorer/test/explorer/chain/cache/transaction_count_test.exs rename apps/explorer/test/explorer/chain/{transactions_cache_test.exs => cache/transactions_test.exs} (70%) delete mode 100644 apps/explorer/test/explorer/chain/transaction_count_cache_test.exs diff --git a/CHANGELOG.md b/CHANGELOG.md index 48f8b3045f..cf4eb708d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/block_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/block_controller.ex index 8beb8ea8ea..91569a96da 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/block_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/block_controller.ex @@ -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 diff --git a/apps/block_scout_web/test/support/conn_case.ex b/apps/block_scout_web/test/support/conn_case.ex index d0b9b066a1..ca486b33c2 100644 --- a/apps/block_scout_web/test/support/conn_case.ex +++ b/apps/block_scout_web/test/support/conn_case.ex @@ -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 diff --git a/apps/block_scout_web/test/support/feature_case.ex b/apps/block_scout_web/test/support/feature_case.ex index b9e6f000d7..a9476a7b85 100644 --- a/apps/block_scout_web/test/support/feature_case.ex +++ b/apps/block_scout_web/test/support/feature_case.ex @@ -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) diff --git a/apps/explorer/config/config.exs b/apps/explorer/config/config.exs index 10012bbc67..65528753eb 100644 --- a/apps/explorer/config/config.exs +++ b/apps/explorer/config/config.exs @@ -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") diff --git a/apps/explorer/config/test.exs b/apps/explorer/config/test.exs index f27fc3b350..d91f4dbdfc 100644 --- a/apps/explorer/config/test.exs +++ b/apps/explorer/config/test.exs @@ -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 diff --git a/apps/explorer/lib/explorer/application.ex b/apps/explorer/lib/explorer/application.ex index 5a6aa1795b..c1fb19f600 100644 --- a/apps/explorer/lib/explorer/application.ex +++ b/apps/explorer/lib/explorer/application.ex @@ -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 diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index fcad694a58..222b0bd5da 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -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';") diff --git a/apps/explorer/lib/explorer/chain/address.ex b/apps/explorer/lib/explorer/chain/address.ex index 28b67f1d66..478c35deb1 100644 --- a/apps/explorer/lib/explorer/chain/address.ex +++ b/apps/explorer/lib/explorer/chain/address.ex @@ -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 diff --git a/apps/explorer/lib/explorer/chain/block_count_cache.ex b/apps/explorer/lib/explorer/chain/cache/block_count.ex similarity index 98% rename from apps/explorer/lib/explorer/chain/block_count_cache.ex rename to apps/explorer/lib/explorer/chain/cache/block_count.ex index 8eaa1ccf58..8ba09cc86f 100644 --- a/apps/explorer/lib/explorer/chain/block_count_cache.ex +++ b/apps/explorer/lib/explorer/chain/cache/block_count.ex @@ -1,4 +1,4 @@ -defmodule Explorer.Chain.BlockCountCache do +defmodule Explorer.Chain.Cache.BlockCount do @moduledoc """ Cache for block count. """ diff --git a/apps/explorer/lib/explorer/chain/block_number_cache.ex b/apps/explorer/lib/explorer/chain/cache/block_number.ex similarity index 97% rename from apps/explorer/lib/explorer/chain/block_number_cache.ex rename to apps/explorer/lib/explorer/chain/cache/block_number.ex index 2f335c7a2c..5212117793 100644 --- a/apps/explorer/lib/explorer/chain/block_number_cache.ex +++ b/apps/explorer/lib/explorer/chain/cache/block_number.ex @@ -1,4 +1,4 @@ -defmodule Explorer.Chain.BlockNumberCache do +defmodule Explorer.Chain.Cache.BlockNumber do @moduledoc """ Cache for max and min block numbers. """ diff --git a/apps/explorer/lib/explorer/chain/blocks_cache.ex b/apps/explorer/lib/explorer/chain/cache/blocks.ex similarity index 98% rename from apps/explorer/lib/explorer/chain/blocks_cache.ex rename to apps/explorer/lib/explorer/chain/cache/blocks.ex index 778d2bc5d6..76fc2473a6 100644 --- a/apps/explorer/lib/explorer/chain/blocks_cache.ex +++ b/apps/explorer/lib/explorer/chain/cache/blocks.ex @@ -1,4 +1,4 @@ -defmodule Explorer.Chain.BlocksCache do +defmodule Explorer.Chain.Cache.Blocks do @moduledoc """ Caches the last imported blocks """ diff --git a/apps/explorer/lib/explorer/chain/net_version_cache.ex b/apps/explorer/lib/explorer/chain/cache/net_version.ex similarity index 94% rename from apps/explorer/lib/explorer/chain/net_version_cache.ex rename to apps/explorer/lib/explorer/chain/cache/net_version.ex index c3df467e0c..ac33ca8f45 100644 --- a/apps/explorer/lib/explorer/chain/net_version_cache.ex +++ b/apps/explorer/lib/explorer/chain/cache/net_version.ex @@ -1,4 +1,4 @@ -defmodule Explorer.Chain.NetVersionCache do +defmodule Explorer.Chain.Cache.NetVersion do @moduledoc """ Caches chain version. """ diff --git a/apps/explorer/lib/explorer/chain/transaction_count_cache.ex b/apps/explorer/lib/explorer/chain/cache/transaction_count.ex similarity index 98% rename from apps/explorer/lib/explorer/chain/transaction_count_cache.ex rename to apps/explorer/lib/explorer/chain/cache/transaction_count.ex index 95685c0fd4..e1fe2dad82 100644 --- a/apps/explorer/lib/explorer/chain/transaction_count_cache.ex +++ b/apps/explorer/lib/explorer/chain/cache/transaction_count.ex @@ -1,4 +1,4 @@ -defmodule Explorer.Chain.TransactionCountCache do +defmodule Explorer.Chain.Cache.TransactionCount do @moduledoc """ Cache for estimated transaction count. """ diff --git a/apps/explorer/lib/explorer/chain/transactions_cache.ex b/apps/explorer/lib/explorer/chain/cache/transactions.ex similarity index 98% rename from apps/explorer/lib/explorer/chain/transactions_cache.ex rename to apps/explorer/lib/explorer/chain/cache/transactions.ex index 3859561295..54748258cf 100644 --- a/apps/explorer/lib/explorer/chain/transactions_cache.ex +++ b/apps/explorer/lib/explorer/chain/cache/transactions.ex @@ -1,4 +1,4 @@ -defmodule Explorer.Chain.TransactionsCache do +defmodule Explorer.Chain.Cache.Transactions do @moduledoc """ Caches the latest imported transactions """ diff --git a/apps/explorer/lib/explorer/chain/supply/rsk.ex b/apps/explorer/lib/explorer/chain/supply/rsk.ex index bc4171e45f..cdb043418b 100644 --- a/apps/explorer/lib/explorer/chain/supply/rsk.ex +++ b/apps/explorer/lib/explorer/chain/supply/rsk.ex @@ -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"} diff --git a/apps/explorer/test/explorer/chain/block_number_cache_test.exs b/apps/explorer/test/explorer/chain/block_number_cache_test.exs deleted file mode 100644 index 7b501a718b..0000000000 --- a/apps/explorer/test/explorer/chain/block_number_cache_test.exs +++ /dev/null @@ -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 diff --git a/apps/explorer/test/explorer/chain/block_count_cache_test.exs b/apps/explorer/test/explorer/chain/cache/block_count_test.exs similarity index 52% rename from apps/explorer/test/explorer/chain/block_count_cache_test.exs rename to apps/explorer/test/explorer/chain/cache/block_count_test.exs index d1bbd5eb53..646af8de43 100644 --- a/apps/explorer/test/explorer/chain/block_count_cache_test.exs +++ b/apps/explorer/test/explorer/chain/cache/block_count_test.exs @@ -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 diff --git a/apps/explorer/test/explorer/chain/cache/block_number_test.exs b/apps/explorer/test/explorer/chain/cache/block_number_test.exs new file mode 100644 index 0000000000..381ded440b --- /dev/null +++ b/apps/explorer/test/explorer/chain/cache/block_number_test.exs @@ -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 diff --git a/apps/explorer/test/explorer/chain/blocks_cache_test.exs b/apps/explorer/test/explorer/chain/cache/blocks_test.exs similarity index 65% rename from apps/explorer/test/explorer/chain/blocks_cache_test.exs rename to apps/explorer/test/explorer/chain/cache/blocks_test.exs index 687d2ff30a..52ac4eabb4 100644 --- a/apps/explorer/test/explorer/chain/blocks_cache_test.exs +++ b/apps/explorer/test/explorer/chain/cache/blocks_test.exs @@ -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 diff --git a/apps/explorer/test/explorer/chain/cache/transaction_count_test.exs b/apps/explorer/test/explorer/chain/cache/transaction_count_test.exs new file mode 100644 index 0000000000..5020486cb8 --- /dev/null +++ b/apps/explorer/test/explorer/chain/cache/transaction_count_test.exs @@ -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 diff --git a/apps/explorer/test/explorer/chain/transactions_cache_test.exs b/apps/explorer/test/explorer/chain/cache/transactions_test.exs similarity index 70% rename from apps/explorer/test/explorer/chain/transactions_cache_test.exs rename to apps/explorer/test/explorer/chain/cache/transactions_test.exs index a72055c276..8a768b7dae 100644 --- a/apps/explorer/test/explorer/chain/transactions_cache_test.exs +++ b/apps/explorer/test/explorer/chain/cache/transactions_test.exs @@ -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 diff --git a/apps/explorer/test/explorer/chain/transaction_count_cache_test.exs b/apps/explorer/test/explorer/chain/transaction_count_cache_test.exs deleted file mode 100644 index a1b0d72485..0000000000 --- a/apps/explorer/test/explorer/chain/transaction_count_cache_test.exs +++ /dev/null @@ -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 diff --git a/apps/explorer/test/explorer/market/market_history_cache_test.exs b/apps/explorer/test/explorer/market/market_history_cache_test.exs index 1421d75214..6dbd15fe7f 100644 --- a/apps/explorer/test/explorer/market/market_history_cache_test.exs +++ b/apps/explorer/test/explorer/market/market_history_cache_test.exs @@ -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 diff --git a/apps/explorer/test/explorer/market/market_test.exs b/apps/explorer/test/explorer/market/market_test.exs index 7e399ea4cd..3fd111c0cd 100644 --- a/apps/explorer/test/explorer/market/market_test.exs +++ b/apps/explorer/test/explorer/market/market_test.exs @@ -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 diff --git a/apps/explorer/test/support/data_case.ex b/apps/explorer/test/support/data_case.ex index 2ec7cde365..e159891987 100644 --- a/apps/explorer/test/support/data_case.ex +++ b/apps/explorer/test/support/data_case.ex @@ -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 diff --git a/apps/indexer/lib/indexer/block/fetcher.ex b/apps/indexer/lib/indexer/block/fetcher.ex index b436d61e03..4155538e76 100644 --- a/apps/indexer/lib/indexer/block/fetcher.ex +++ b/apps/indexer/lib/indexer/block/fetcher.ex @@ -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( diff --git a/apps/indexer/lib/indexer/fetcher/coin_balance_on_demand.ex b/apps/indexer/lib/indexer/fetcher/coin_balance_on_demand.ex index 75af9c9bfc..4dfed6e73a 100644 --- a/apps/indexer/lib/indexer/fetcher/coin_balance_on_demand.ex +++ b/apps/indexer/lib/indexer/fetcher/coin_balance_on_demand.ex @@ -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