From f1db75ca46d46d2701daca8be5deeb3c69e34e9e Mon Sep 17 00:00:00 2001 From: Den Smalonski Date: Wed, 11 Sep 2024 14:07:39 +0200 Subject: [PATCH 1/2] fix: smart contract csv --- .../api/v2/smart_contract_controller.ex | 33 ++----------------- .../lib/explorer/chain/smart_contract.ex | 6 ++-- 2 files changed, 7 insertions(+), 32 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/v2/smart_contract_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/v2/smart_contract_controller.ex index 3326eb5e36fb..00ca8ad5d4b9 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/v2/smart_contract_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/v2/smart_contract_controller.ex @@ -304,36 +304,8 @@ defmodule BlockScoutWeb.API.V2.SmartContractController do defp items_csv( conn, %{ - "from_period" => from_period, - "to_period" => to_period, - }, - csv_export_module - ) do - with true <- Application.get_env(:block_scout_web, :recaptcha)[:is_disabled] do - csv_export_module.export(from_period, to_period) - |> Enum.reduce_while(put_resp_params(conn), fn chunk, conn -> - case Conn.chunk(conn, chunk) do - {:ok, conn} -> - {:cont, conn} - - {:error, :closed} -> - {:halt, conn} - end - end) - else - :error -> - unprocessable_entity(conn) - - false -> - not_found(conn) - end - end - - defp items_csv( - conn, - %{ - "from_period" => from_period, - "to_period" => to_period, + "from_period" => from_period, + "to_period" => to_period, "recaptcha_response" => recaptcha_response }, csv_export_module @@ -354,6 +326,7 @@ defmodule BlockScoutWeb.API.V2.SmartContractController do unprocessable_entity(conn) {:recaptcha, false} -> + IO.puts("Failed to export CSV due to invalid recaptcha response.") not_found(conn) end end diff --git a/apps/explorer/lib/explorer/chain/smart_contract.ex b/apps/explorer/lib/explorer/chain/smart_contract.ex index a99853b425e1..9782b6f99dfe 100644 --- a/apps/explorer/lib/explorer/chain/smart_contract.ex +++ b/apps/explorer/lib/explorer/chain/smart_contract.ex @@ -1297,12 +1297,14 @@ defmodule Explorer.Chain.SmartContract do def verified_contracts_by_date_range(from_period, to_period) do query = from(contract in __MODULE__) date_format = "%Y-%m-%d" + from_date = Timex.parse!(from_period, date_format, :strftime) |> Timex.beginning_of_day() + to_date = Timex.parse!(to_period, date_format, :strftime) |> Timex.end_of_day() query |> where( [contract], - contract.inserted_at >= ^Timex.parse!(from_period, date_format, :strftime) and - contract.inserted_at <= ^Timex.parse!(to_period, date_format, :strftime) + contract.inserted_at >= ^from_date and + contract.inserted_at <= ^to_date ) |> order_by(desc: :id) |> Chain.select_repo([]).all() From 0df605e8e3dd5400867f7b321c311831982e193e Mon Sep 17 00:00:00 2001 From: Den Smalonski Date: Thu, 12 Sep 2024 12:03:02 +0200 Subject: [PATCH 2/2] feat: add epoch --- .../lib/block_scout_web/views/api/v2/block_view.ex | 1 + .../ethereum_jsonrpc/lib/ethereum_jsonrpc/block.ex | 14 +++++++++++++- .../lib/ethereum_jsonrpc/block/by_tag.ex | 2 +- .../lib/ethereum_jsonrpc/blocks.ex | 1 - apps/explorer/lib/explorer/chain/block.ex | 4 +++- .../20241011220133_add_epoch_to_block.exs | 9 +++++++++ 6 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 apps/explorer/priv/repo/migrations/20241011220133_add_epoch_to_block.exs diff --git a/apps/block_scout_web/lib/block_scout_web/views/api/v2/block_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api/v2/block_view.ex index 9c4b40a3516f..cf83a8eba318 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api/v2/block_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api/v2/block_view.ex @@ -40,6 +40,7 @@ defmodule BlockScoutWeb.API.V2.BlockView do "miner" => Helper.address_with_info(nil, block.miner, block.miner_hash, false), "size" => block.size, "hash" => block.hash, + "epoch" => block.epoch, "parent_hash" => block.parent_hash, "difficulty" => block.difficulty, "total_difficulty" => block.total_difficulty, diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/block.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/block.ex index 1e0e717ab595..85b7d36a517c 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/block.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/block.ex @@ -29,6 +29,7 @@ defmodule EthereumJSONRPC.Block do extra_data: EthereumJSONRPC.hash(), gas_limit: non_neg_integer(), gas_used: non_neg_integer(), + epoch: non_neg_integer(), hash: EthereumJSONRPC.hash(), logs_bloom: EthereumJSONRPC.hash(), miner_hash: EthereumJSONRPC.hash(), @@ -53,6 +54,7 @@ defmodule EthereumJSONRPC.Block do * `"difficulty"` - `t:EthereumJSONRPC.quantity/0` of the difficulty for this block. * `"extraData"` - the extra `t:EthereumJSONRPC.data/0` field of this block. * `"gasLimit" - maximum gas `t:EthereumJSONRPC.quantity/0` in this block. + * `"epoch"` - `t:EthereumJSONRPC.quantity/0` of the epoch for this block. * `"gasUsed" - the total `t:EthereumJSONRPC.quantity/0` of gas used by all transactions in this block. * `"hash"` - the `t:EthereumJSONRPC.hash/0` of the block. * `"logsBloom"` - `t:EthereumJSONRPC.data/0` for the [Bloom filter](https://en.wikipedia.org/wiki/Bloom_filter) @@ -262,6 +264,7 @@ defmodule EthereumJSONRPC.Block do "extraData" => extra_data, "gasLimit" => gas_limit, "gasUsed" => gas_used, + "epoch" => epoch, "hash" => hash, "logsBloom" => logs_bloom, "miner" => miner_hash, @@ -283,6 +286,7 @@ defmodule EthereumJSONRPC.Block do extra_data: extra_data, gas_limit: gas_limit, gas_used: gas_used, + epoch: epoch, hash: hash, logs_bloom: logs_bloom, miner_hash: miner_hash, @@ -311,6 +315,7 @@ defmodule EthereumJSONRPC.Block do "gasLimit" => gas_limit, "gasUsed" => gas_used, "hash" => hash, + "epoch" => epoch, "logsBloom" => logs_bloom, "miner" => miner_hash, "number" => number, @@ -332,6 +337,7 @@ defmodule EthereumJSONRPC.Block do gas_used: gas_used, hash: hash, logs_bloom: logs_bloom, + epoch: epoch, miner_hash: miner_hash, mix_hash: Map.get(elixir, "mixHash", "0x0"), nonce: Map.get(elixir, "nonce", 0), @@ -358,6 +364,7 @@ defmodule EthereumJSONRPC.Block do "gasUsed" => gas_used, "hash" => hash, "logsBloom" => logs_bloom, + "epoch" => epoch, "miner" => miner_hash, "number" => number, "parentHash" => parent_hash, @@ -379,6 +386,7 @@ defmodule EthereumJSONRPC.Block do hash: hash, logs_bloom: logs_bloom, miner_hash: miner_hash, + epoch: epoch, mix_hash: Map.get(elixir, "mixHash", "0x0"), nonce: Map.get(elixir, "nonce", 0), number: number, @@ -406,6 +414,7 @@ defmodule EthereumJSONRPC.Block do "hash" => hash, "logsBloom" => logs_bloom, "miner" => miner_hash, + "epoch" => epoch, "number" => number, "parentHash" => parent_hash, "receiptsRoot" => receipts_root, @@ -425,6 +434,7 @@ defmodule EthereumJSONRPC.Block do hash: hash, logs_bloom: logs_bloom, miner_hash: miner_hash, + epoch: epoch, mix_hash: Map.get(elixir, "mixHash", "0x0"), nonce: Map.get(elixir, "nonce", 0), number: number, @@ -448,6 +458,7 @@ defmodule EthereumJSONRPC.Block do "gasLimit" => gas_limit, "gasUsed" => gas_used, "hash" => hash, + "epoch" => epoch, "logsBloom" => logs_bloom, "miner" => miner_hash, "number" => number, @@ -471,6 +482,7 @@ defmodule EthereumJSONRPC.Block do mix_hash: Map.get(elixir, "mixHash", "0x0"), nonce: Map.get(elixir, "nonce", 0), number: number, + epoch: epoch, parent_hash: parent_hash, receipts_root: receipts_root, size: size, @@ -806,7 +818,7 @@ defmodule EthereumJSONRPC.Block do end defp entry_to_elixir({key, quantity}, _block) - when key in ~w(difficulty gasLimit gasUsed minimumGasPrice baseFeePerGas number size cumulativeDifficulty totalDifficulty paidFees minimumGasPrice) and + when key in ~w(difficulty gasLimit gasUsed minimumGasPrice baseFeePerGas number size cumulativeDifficulty totalDifficulty paidFees minimumGasPrice epoch) and not is_nil(quantity) do {key, quantity_to_integer(quantity)} end diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/block/by_tag.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/block/by_tag.ex index d257f4e506c5..575ab54d28f5 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/block/by_tag.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/block/by_tag.ex @@ -8,7 +8,7 @@ defmodule EthereumJSONRPC.Block.ByTag do alias EthereumJSONRPC.Blocks def request(%{id: id, tag: tag}) when is_binary(tag) do - EthereumJSONRPC.request(%{id: id, method: "eth_getBlockByNumber", params: [tag, false]}) + EthereumJSONRPC.request(%{id: id, method: "hmy_getBlockByNumber", params: [tag, false]}) end def number_from_result({:ok, %{"number" => nil}}), do: {:error, :not_found} diff --git a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/blocks.ex b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/blocks.ex index a109a618c593..c98cc27c81f2 100644 --- a/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/blocks.ex +++ b/apps/ethereum_jsonrpc/lib/ethereum_jsonrpc/blocks.ex @@ -45,7 +45,6 @@ defmodule EthereumJSONRPC.Blocks do {:error, error}, %{errors: errors} = acc -> %{acc | errors: [error | errors]} end) - elixir_blocks = to_elixir(blocks) elixir_uncles = elixir_to_uncles(elixir_blocks) diff --git a/apps/explorer/lib/explorer/chain/block.ex b/apps/explorer/lib/explorer/chain/block.ex index a1558e29db10..c4f3cf75f04d 100644 --- a/apps/explorer/lib/explorer/chain/block.ex +++ b/apps/explorer/lib/explorer/chain/block.ex @@ -22,7 +22,7 @@ defmodule Explorer.Chain.Block do alias Explorer.Chain.Block.{EmissionReward, Reward, SecondDegreeRelation} alias Explorer.Repo - @optional_attrs ~w(size refetch_needed total_difficulty difficulty base_fee_per_gas)a + @optional_attrs ~w(size refetch_needed total_difficulty difficulty base_fee_per_gas epoch)a |> (&(case Application.compile_env(:explorer, :chain_type) do "rsk" -> &1 ++ @@ -104,6 +104,7 @@ defmodule Explorer.Chain.Block do number: block_number(), parent_hash: Hash.t(), size: non_neg_integer(), + epoch: non_neg_integer(), timestamp: DateTime.t(), total_difficulty: difficulty(), transactions: %Ecto.Association.NotLoaded{} | [Transaction.t()], @@ -120,6 +121,7 @@ defmodule Explorer.Chain.Block do field(:gas_used, :decimal) field(:nonce, Hash.Nonce) field(:number, :integer) + field(:epoch, :integer) field(:size, :integer) field(:timestamp, :utc_datetime_usec) field(:total_difficulty, :decimal) diff --git a/apps/explorer/priv/repo/migrations/20241011220133_add_epoch_to_block.exs b/apps/explorer/priv/repo/migrations/20241011220133_add_epoch_to_block.exs new file mode 100644 index 000000000000..3c704a47f1dc --- /dev/null +++ b/apps/explorer/priv/repo/migrations/20241011220133_add_epoch_to_block.exs @@ -0,0 +1,9 @@ +defmodule Explorer.Repo.Migrations.AddEpochToBlock do + use Ecto.Migration + + def change do + alter table(:blocks) do + add(:epoch, :bigint, null: true) + end + end +end