|
|
|
@ -4,7 +4,8 @@ defmodule Explorer.Counters.AddressTransactionsGasUsageCounter do |
|
|
|
|
""" |
|
|
|
|
use GenServer |
|
|
|
|
|
|
|
|
|
alias Explorer.Chain |
|
|
|
|
alias Ecto.Changeset |
|
|
|
|
alias Explorer.{Chain, Repo} |
|
|
|
|
|
|
|
|
|
@cache_name :address_transactions_gas_usage_counter |
|
|
|
|
@last_update_key "last_update" |
|
|
|
@ -16,7 +17,7 @@ defmodule Explorer.Counters.AddressTransactionsGasUsageCounter do |
|
|
|
|
read_concurrency: true |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
config = Application.get_env(:explorer, Explorer.Counters.AddressTransactionsGasUsageCounter) |
|
|
|
|
config = Application.get_env(:explorer, __MODULE__) |
|
|
|
|
@enable_consolidation Keyword.get(config, :enable_consolidation) |
|
|
|
|
|
|
|
|
|
@spec start_link(term()) :: GenServer.on_start() |
|
|
|
@ -51,7 +52,7 @@ defmodule Explorer.Counters.AddressTransactionsGasUsageCounter do |
|
|
|
|
update_cache(address) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
address_hash_string = get_address_hash_string(address) |
|
|
|
|
address_hash_string = to_string(address.hash) |
|
|
|
|
fetch_from_cache("hash_#{address_hash_string}") |
|
|
|
|
end |
|
|
|
|
|
|
|
|
@ -59,7 +60,7 @@ defmodule Explorer.Counters.AddressTransactionsGasUsageCounter do |
|
|
|
|
|
|
|
|
|
defp cache_expired?(address) do |
|
|
|
|
cache_period = address_transactions_gas_usage_counter_cache_period() |
|
|
|
|
address_hash_string = get_address_hash_string(address) |
|
|
|
|
address_hash_string = to_string(address.hash) |
|
|
|
|
updated_at = fetch_from_cache("hash_#{address_hash_string}_#{@last_update_key}") |
|
|
|
|
|
|
|
|
|
cond do |
|
|
|
@ -70,10 +71,11 @@ defmodule Explorer.Counters.AddressTransactionsGasUsageCounter do |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defp update_cache(address) do |
|
|
|
|
address_hash_string = get_address_hash_string(address) |
|
|
|
|
address_hash_string = to_string(address.hash) |
|
|
|
|
put_into_cache("hash_#{address_hash_string}_#{@last_update_key}", current_time()) |
|
|
|
|
new_data = Chain.address_to_gas_usage_count(address) |
|
|
|
|
put_into_cache("hash_#{address_hash_string}", new_data) |
|
|
|
|
put_into_db(address, new_data) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defp fetch_from_cache(key) do |
|
|
|
@ -90,8 +92,12 @@ defmodule Explorer.Counters.AddressTransactionsGasUsageCounter do |
|
|
|
|
:ets.insert(@cache_name, {key, value}) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defp get_address_hash_string(address) do |
|
|
|
|
Base.encode16(address.hash.bytes, case: :lower) |
|
|
|
|
defp put_into_db(_address, value) when is_nil(value), do: :ignore |
|
|
|
|
|
|
|
|
|
defp put_into_db(address, value) do |
|
|
|
|
address |
|
|
|
|
|> Changeset.change(%{gas_used: Decimal.to_integer(value)}) |
|
|
|
|
|> Repo.update() |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
defp current_time do |
|
|
|
|