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

Commit

Permalink
Fix bug where we cached blocks from fetcher (#279)
Browse files Browse the repository at this point in the history
      * Fix bug where we cached blocks from fetcher

* Fix compiler warnings
  • Loading branch information
jrchatruc authored Sep 27, 2023
1 parent b42e337 commit 2edb497
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
16 changes: 15 additions & 1 deletion lib/starknet_explorer/block/block_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule StarknetExplorer.BlockUtils do
alias StarknetExplorer.{Rpc, Block}
alias StarknetExplorer.IndexCache

def fetch_and_store(block_height, network) do
def fetch_store_and_cache(block_height, network) do
with false <- already_stored?(block_height, network),
{:ok, block = %{"block_number" => block_number}} <- fetch_block(block_height, network),
:ok <- store_block(block, network),
Expand All @@ -17,6 +17,20 @@ defmodule StarknetExplorer.BlockUtils do
end
end

def fetch_and_store(block_height, network) do
with false <- already_stored?(block_height, network),
{:ok, block = %{"block_number" => block_number}} <- fetch_block(block_height, network),
:ok <- store_block(block, network) do
{:ok, block_number}
else
true ->
{:ok, block_height}

error ->
{:error, error}
end
end

def fetch_and_update(block_height, network) do
with block_from_sql <- Block.get_by_number_with_receipts_preload(block_height, network),
{:ok, block_from_rpc = %{"block_number" => block_number}} <-
Expand Down
2 changes: 1 addition & 1 deletion lib/starknet_explorer/blockchain/state_sync_system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ defmodule StarknetExplorer.Blockchain.StateSyncSystem do
defp try_fetch(true, state = %StateSyncSystem{network: network}) do
next_to_fetch = state.current_block_number + 1

{:ok, _} = BlockUtils.fetch_and_store(next_to_fetch, network)
{:ok, _} = BlockUtils.fetch_store_and_cache(next_to_fetch, network)

Counts.insert_or_update(network)

Expand Down
6 changes: 3 additions & 3 deletions lib/starknet_explorer/counts.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule StarknetExplorer.Counts do
use Ecto.Schema
import Ecto.Query
alias StarknetExplorer.Events
alias StarknetExplorer.Message
alias StarknetExplorer.Transaction
# alias StarknetExplorer.Events
# alias StarknetExplorer.Message
# alias StarknetExplorer.Transaction
alias StarknetExplorer.{Counts, BlockUtils}
alias StarknetExplorer.Repo

Expand Down
2 changes: 2 additions & 0 deletions lib/starknet_explorer/data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,13 @@ defmodule StarknetExplorer.Data do
|> Map.put(:message_count, counts.messages)
|> Map.put(:events_count, counts.events)
|> Map.put(:transaction_count, counts.transactions)
|> Map.put(:block_count, counts.blocks)
else
Map.new()
|> Map.put(:message_count, 0)
|> Map.put(:events_count, 0)
|> Map.put(:transaction_count, 0)
|> Map.put(:block_count, 0)
end
end
end
4 changes: 1 addition & 3 deletions lib/starknet_explorer_web/live/pages/home/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,12 @@ defmodule StarknetExplorerWeb.HomeLive.Index do
end)
|> Map.new()

{:ok, max_block_height} = StarknetExplorer.BlockUtils.block_height(socket.assigns.network)

assign(socket,
blocks: blocks,
transactions: transactions,
entities_count: entities_count,
latest_block: latest_block,
block_height: StarknetExplorer.Utils.format_number_for_display(max_block_height)
block_height: entities_count.block_count
)
end
end
Expand Down

0 comments on commit 2edb497

Please sign in to comment.