More transaction controllers improvements

pull/2249/head
pasqu4le 6 years ago
parent 3c6abf4d23
commit cccbf129cc
No known key found for this signature in database
GPG Key ID: 8F3EE01F1DC90687
  1. 1
      CHANGELOG.md
  2. 2
      apps/block_scout_web/lib/block_scout_web/controllers/block_transaction_controller.ex
  3. 17
      apps/block_scout_web/lib/block_scout_web/controllers/transaction_controller.ex
  4. 3
      apps/block_scout_web/lib/block_scout_web/controllers/transaction_internal_transaction_controller.ex
  5. 10
      apps/block_scout_web/lib/block_scout_web/controllers/transaction_raw_trace_controller.ex
  6. 9
      apps/explorer/lib/explorer/chain.ex
  7. 17
      apps/explorer/test/explorer/chain_test.exs

@ -57,6 +57,7 @@
- [#2204](https://github.com/poanetwork/blockscout/pull/2204) - fix large contract verification
- [#2247](https://github.com/poanetwork/blockscout/pull/2247) - hide logs search if there are no logs
- [#2248](https://github.com/poanetwork/blockscout/pull/2248) - sort block after query execution for average block time
- [#2249](https://github.com/poanetwork/blockscout/pull/2249) - More transaction controllers improvements
### Chore
- [#2127](https://github.com/poanetwork/blockscout/pull/2127) - use previouse chromedriver version

@ -17,7 +17,7 @@ defmodule BlockScoutWeb.BlockTransactionController do
Keyword.merge(
[
necessity_by_association: %{
:block => :required,
:block => :optional,
[created_contract_address: :names] => :optional,
[from_address: :names] => :required,
[to_address: :names] => :optional

@ -61,21 +61,16 @@ defmodule BlockScoutWeb.TransactionController do
end
def show(conn, %{"id" => id}) do
case Chain.string_to_transaction_hash(id) do
{:ok, transaction_hash} -> show_transaction(conn, id, Chain.hash_to_transaction(transaction_hash))
:error -> conn |> put_status(422) |> render("invalid.html", transaction_hash: id)
end
end
defp show_transaction(conn, id, {:error, :not_found}) do
conn |> put_status(404) |> render("not_found.html", transaction_hash: id)
end
defp show_transaction(conn, id, {:ok, %Chain.Transaction{} = transaction}) do
with {:ok, transaction_hash} <- Chain.string_to_transaction_hash(id),
{:ok, %Chain.Transaction{} = transaction} <- Chain.hash_to_transaction(transaction_hash) do
if Chain.transaction_has_token_transfers?(transaction.hash) do
redirect(conn, to: transaction_token_transfer_path(conn, :index, id))
else
redirect(conn, to: transaction_internal_transaction_path(conn, :index, id))
end
else
:error -> conn |> put_status(422) |> render("invalid.html", transaction_hash: id)
{:error, :not_found} -> conn |> put_status(404) |> render("not_found.html", transaction_hash: id)
end
end
end

@ -17,7 +17,8 @@ defmodule BlockScoutWeb.TransactionInternalTransactionController do
necessity_by_association: %{
[created_contract_address: :names] => :optional,
[from_address: :names] => :optional,
[to_address: :names] => :optional
[to_address: :names] => :optional,
[transaction: :block] => :optional
}
],
paging_options(params)

@ -19,15 +19,7 @@ defmodule BlockScoutWeb.TransactionRawTraceController do
:token_transfers => :optional
}
) do
options = [
necessity_by_association: %{
[created_contract_address: :names] => :optional,
[from_address: :names] => :optional,
[to_address: :names] => :optional
}
]
internal_transactions = Chain.transaction_to_internal_transactions(transaction, options)
internal_transactions = Chain.transaction_to_internal_transactions(transaction)
render(
conn,

@ -1052,7 +1052,7 @@ defmodule Explorer.Chain do
when is_list(options) do
necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
fetch_transactions()
Transaction
|> where(hash: ^hash)
|> join_associations(necessity_by_association)
|> Repo.one()
@ -1953,7 +1953,6 @@ defmodule Explorer.Chain do
|> Keyword.get(:paging_options, @default_paging_options)
|> fetch_transactions()
|> where([transaction], not is_nil(transaction.block_number) and not is_nil(transaction.index))
|> order_by([transaction], desc: transaction.block_number, desc: transaction.index)
|> join_associations(necessity_by_association)
|> preload([{:token_transfers, [:token, :from_address, :to_address]}])
|> Repo.all()
@ -2146,7 +2145,7 @@ defmodule Explorer.Chain do
|> page_internal_transaction(paging_options)
|> limit(^paging_options.page_size)
|> order_by([internal_transaction], asc: internal_transaction.index)
|> preload(transaction: :block)
|> preload(:transaction)
|> Repo.all()
end
@ -2702,9 +2701,9 @@ defmodule Explorer.Chain do
@spec transaction_has_token_transfers?(Hash.t()) :: boolean()
def transaction_has_token_transfers?(transaction_hash) do
query = from(tt in TokenTransfer, where: tt.transaction_hash == ^transaction_hash, limit: 1, select: 1)
query = from(tt in TokenTransfer, where: tt.transaction_hash == ^transaction_hash)
Repo.one(query) != nil
Repo.exists?(query)
end
@spec address_tokens_with_balance(Hash.Address.t(), [any()]) :: []

@ -2110,11 +2110,14 @@ defmodule Explorer.ChainTest do
])
)
assert internal_transaction.transaction.block.number == block.number
assert internal_transaction.transaction.block_number == block.number
end
test "with transaction with internal transactions loads associations with in necessity_by_association" do
transaction = insert(:transaction)
transaction =
:transaction
|> insert()
|> with_block()
insert(:internal_transaction_create,
transaction: transaction,
@ -2127,7 +2130,7 @@ defmodule Explorer.ChainTest do
%InternalTransaction{
from_address: %Ecto.Association.NotLoaded{},
to_address: %Ecto.Association.NotLoaded{},
transaction: %Transaction{}
transaction: %Transaction{block: %Ecto.Association.NotLoaded{}}
}
] = Chain.transaction_to_internal_transactions(transaction)
@ -2135,15 +2138,15 @@ defmodule Explorer.ChainTest do
%InternalTransaction{
from_address: %Address{},
to_address: nil,
transaction: %Transaction{}
transaction: %Transaction{block: %Block{}}
}
] =
Chain.transaction_to_internal_transactions(
transaction,
necessity_by_association: %{
from_address: :optional,
to_address: :optional,
transaction: :optional
:from_address => :optional,
:to_address => :optional,
[transaction: :block] => :optional
}
)
end

Loading…
Cancel
Save