Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Add block fetcher tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fkrause98 committed Jul 6, 2023
1 parent 87eb1d2 commit f05f03f
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 12 deletions.
7 changes: 6 additions & 1 deletion lib/starknet_explorer/block_fetcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule StarknetExplorer.BlockFetcher do
require Logger
alias StarknetExplorer.{Rpc, BlockFetcher, Block}
defstruct [:block_height, :latest_block_fetched]
# 1 second
@fetch_interval 300
def start_link(args) do
GenServer.start_link(__MODULE__, args)
Expand Down Expand Up @@ -47,6 +46,12 @@ defmodule StarknetExplorer.BlockFetcher do
end
end

@impl true
def handle_info(:stop, _) do
Logger.info("Stopping BlockFetcher")
{:stop, :normal, :ok}
end

defp fetch_block_height() do
case Rpc.get_block_height() do
{:ok, height} ->
Expand Down
10 changes: 6 additions & 4 deletions lib/starknet_explorer/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ defmodule StarknetExplorer.Transaction do
:version
]

@fields @l1_handler_tx_fields ++
@invoke_tx_fields ++
@declare_tx_fields ++ @deploy_contract_tx_fields ++ @deploy_account_tx_fields

@primary_key {:hash, :string, []}
schema "transactions" do
field :constructor_calldata, :string
field :constructor_calldata, {:array, :string}
field :class_hash, :string
field :type, :string
field :max_fee, :string
Expand All @@ -96,9 +100,7 @@ defmodule StarknetExplorer.Transaction do
tx
|> cast(
attrs,
@invoke_tx_fields ++
@deploy_tx_fields ++
@declare_tx_fields
@fields
)
|> validate_according_to_tx_type(attrs)
|> unique_constraint(:hash)
Expand Down
2 changes: 1 addition & 1 deletion lib/starknet_explorer_web/live/pages/block_detail.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule StarknetExplorerWeb.BlockDetailLive do
<li>Sequencer Address <%= @block["sequencer_address"] %></li>
<li>Status <%= @block["status"] %></li>
<li>
Timestamp <%= @block["timestamp"]
<%= @block["timestamp"]
|> DateTime.from_unix()
|> then(fn {:ok, time} -> time end) %> UTC
</li>
Expand Down
3 changes: 1 addition & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ defmodule StarknetExplorer.MixProject do
setup: ["deps.get", "ecto.setup", "assets.setup", "assets.build"],
"ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
"ecto.reset": ["ecto.drop", "ecto.setup"],
# test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"],
test: ["test"],
test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"],
"assets.setup": ["tailwind.install --if-missing", "esbuild.install --if-missing"],
"assets.build": ["tailwind default", "esbuild default"],
"assets.deploy": ["tailwind default --minify", "esbuild default --minify", "phx.digest"]
Expand Down
2 changes: 1 addition & 1 deletion priv/repo/migrations/20230704215332_transactions.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule StarknetExplorer.Repo.Migrations.Transactions do
add :calldata, {:array, :string}
add :chain_id, {:array, :string}
add :class_hash, :string
add :constructor_calldata, :string
add :constructor_calldata, {:array, :string}
add :contract_address, :string
add :contract_address_salt, :string
add :contract_class, :string
Expand Down
18 changes: 18 additions & 0 deletions test/block_fetcher_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule StarknetExplorer.BlockFetcher.Test do
alias StarknetExplorer.{BlockFetcher, Block, Rpc, Repo}
use StarknetExplorer.DataCase

test "Fetch some blocks during 10 seconds, check db is ok" do
{:ok, pid} = BlockFetcher.start_link([])
Process.send_after(pid, :stop, 10_000)
Process.sleep(10_000)
block_height = Block.highest_fetched_block_number()

for i <- 1..block_height do
{:ok, %{"block_hash" => hash, "block_number" => number}} = Rpc.get_block_by_number(i)
db_block = Repo.one!(from b in Block, where: b.number == ^i)
assert hash == db_block.hash
assert number == db_block.number
end
end
end
4 changes: 2 additions & 2 deletions test/support/data_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ defmodule StarknetExplorer.DataCase do
Sets up the sandbox based on the test tags.
"""
def setup_sandbox(tags) do
# pid = Ecto.Adapters.SQL.Sandbox.start_owner!(StarknetExplorer.Repo, shared: not tags[:async])
# on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(StarknetExplorer.Repo, shared: not tags[:async])
on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ExUnit.start()
# Ecto.Adapters.SQL.Sandbox.mode(StarknetExplorer.Repo, :manual)
Ecto.Adapters.SQL.Sandbox.mode(StarknetExplorer.Repo, :manual)

0 comments on commit f05f03f

Please sign in to comment.