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

Commit

Permalink
Fix relation between blocks transactions (#373)
Browse files Browse the repository at this point in the history
* rollback prod config for logs

* use custom query for assocs in blocks and transactions

---------

Co-authored-by: Santiago Pittella <[email protected]>
  • Loading branch information
SantiagoPittella and Santiago Pittella authored Nov 23, 2023
1 parent 23f1107 commit e10bd1f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
3 changes: 1 addition & 2 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ config :starknet_explorer, StarknetExplorerWeb.Endpoint,
config :swoosh, api_client: Swoosh.ApiClient.Finch, finch_name: StarknetExplorer.Finch

# Do not print debug messages in production
# config :logger, level: :info
config :logger, level: :debug
config :logger, level: :info

# Runtime production configuration, including reading
# of environment variables, is done on config/runtime.exs.
Expand Down
47 changes: 27 additions & 20 deletions lib/starknet_explorer/block/block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -431,15 +431,12 @@ defmodule StarknetExplorer.Block do
where: b.network == ^network,
limit: ^amount

query
|> Repo.all()
|> Repo.preload(:transactions)
Repo.all(query)
|> Enum.map(fn block ->
block
|> Map.put(
:transactions,
Enum.filter(block.transactions, fn tx -> tx.network == network end)
)
preload_query =
from tx in Transaction, where: tx.block_number == ^block.number and tx.network == ^network

Repo.preload(block, transactions: preload_query)
end)
end

Expand Down Expand Up @@ -479,13 +476,15 @@ defmodule StarknetExplorer.Block do
from b in Block,
where: b.hash == ^hash and b.network == ^network

case preload_transactions do
true ->
Repo.one(query)
|> Repo.preload(:transactions)
block = Repo.one(query)

if preload_transactions do
preload_query =
from tx in Transaction, where: tx.block_number == ^block.number and tx.network == ^network

false ->
Repo.one(query)
Repo.preload(block, transactions: preload_query)
else
block
end
end

Expand All @@ -504,22 +503,30 @@ defmodule StarknetExplorer.Block do
from b in Block,
where: b.number == ^num and b.network == ^network

if preload_transactions do
Repo.one(query)
|> Repo.preload(:transactions)
block = Repo.one(query)

if preload_transactions and not is_nil(block) do
preload_query =
from tx in Transaction, where: tx.block_number == ^block.number and tx.network == ^network

Repo.preload(block, transactions: preload_query)
else
Repo.one(query)
block
end
end

def get_by_number_with_receipts_preload(num, network) do
preload_query =
from tx in Transaction,
where: tx.block_number == ^num and tx.network == ^network,
preload: :receipt

query =
from b in Block,
where: b.number == ^num and b.network == ^network,
preload: [transactions: :receipt]
preload: [transactions: ^preload_query]

Repo.one(query)
|> Repo.preload(:transactions)
end

def get_by_partial_num(num, network) do
Expand Down
14 changes: 9 additions & 5 deletions lib/starknet_explorer/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,15 @@ defmodule StarknetExplorer.Transaction do
end

def paginate_transactions(params, network) do
Transaction
|> where([tx], tx.network == ^network)
|> preload(:block)
|> order_by(desc: :block_number)
|> Repo.paginate(params)
query =
from tx in Transaction,
join: b in Block,
as: :block,
on: tx.block_number == b.number and tx.network == ^network,
where: tx.network == ^network,
order_by: [desc: :block_number]

Repo.paginate(query, params)
end

def paginate_transactions_for_index(params, network, filter \\ "ALL")
Expand Down

0 comments on commit e10bd1f

Please sign in to comment.