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. 23
      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 - [#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 - [#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 - [#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 ### Chore
- [#2127](https://github.com/poanetwork/blockscout/pull/2127) - use previouse chromedriver version - [#2127](https://github.com/poanetwork/blockscout/pull/2127) - use previouse chromedriver version

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

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

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

@ -19,15 +19,7 @@ defmodule BlockScoutWeb.TransactionRawTraceController do
:token_transfers => :optional :token_transfers => :optional
} }
) do ) do
options = [ internal_transactions = Chain.transaction_to_internal_transactions(transaction)
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)
render( render(
conn, conn,

@ -1052,7 +1052,7 @@ defmodule Explorer.Chain do
when is_list(options) do when is_list(options) do
necessity_by_association = Keyword.get(options, :necessity_by_association, %{}) necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
fetch_transactions() Transaction
|> where(hash: ^hash) |> where(hash: ^hash)
|> join_associations(necessity_by_association) |> join_associations(necessity_by_association)
|> Repo.one() |> Repo.one()
@ -1953,7 +1953,6 @@ defmodule Explorer.Chain do
|> Keyword.get(:paging_options, @default_paging_options) |> Keyword.get(:paging_options, @default_paging_options)
|> fetch_transactions() |> fetch_transactions()
|> where([transaction], not is_nil(transaction.block_number) and not is_nil(transaction.index)) |> 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) |> join_associations(necessity_by_association)
|> preload([{:token_transfers, [:token, :from_address, :to_address]}]) |> preload([{:token_transfers, [:token, :from_address, :to_address]}])
|> Repo.all() |> Repo.all()
@ -2146,7 +2145,7 @@ defmodule Explorer.Chain do
|> page_internal_transaction(paging_options) |> page_internal_transaction(paging_options)
|> limit(^paging_options.page_size) |> limit(^paging_options.page_size)
|> order_by([internal_transaction], asc: internal_transaction.index) |> order_by([internal_transaction], asc: internal_transaction.index)
|> preload(transaction: :block) |> preload(:transaction)
|> Repo.all() |> Repo.all()
end end
@ -2702,9 +2701,9 @@ defmodule Explorer.Chain do
@spec transaction_has_token_transfers?(Hash.t()) :: boolean() @spec transaction_has_token_transfers?(Hash.t()) :: boolean()
def transaction_has_token_transfers?(transaction_hash) do 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 end
@spec address_tokens_with_balance(Hash.Address.t(), [any()]) :: [] @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 end
test "with transaction with internal transactions loads associations with in necessity_by_association" do 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, insert(:internal_transaction_create,
transaction: transaction, transaction: transaction,
@ -2127,7 +2130,7 @@ defmodule Explorer.ChainTest do
%InternalTransaction{ %InternalTransaction{
from_address: %Ecto.Association.NotLoaded{}, from_address: %Ecto.Association.NotLoaded{},
to_address: %Ecto.Association.NotLoaded{}, to_address: %Ecto.Association.NotLoaded{},
transaction: %Transaction{} transaction: %Transaction{block: %Ecto.Association.NotLoaded{}}
} }
] = Chain.transaction_to_internal_transactions(transaction) ] = Chain.transaction_to_internal_transactions(transaction)
@ -2135,15 +2138,15 @@ defmodule Explorer.ChainTest do
%InternalTransaction{ %InternalTransaction{
from_address: %Address{}, from_address: %Address{},
to_address: nil, to_address: nil,
transaction: %Transaction{} transaction: %Transaction{block: %Block{}}
} }
] = ] =
Chain.transaction_to_internal_transactions( Chain.transaction_to_internal_transactions(
transaction, transaction,
necessity_by_association: %{ necessity_by_association: %{
from_address: :optional, :from_address => :optional,
to_address: :optional, :to_address => :optional,
transaction: :optional [transaction: :block] => :optional
} }
) )
end end

Loading…
Cancel
Save