Order internal transactions by newest first

Uses `block`.`number` desc (NULLs first), then 
`transaction`.`transaction_index` desc, then 
`internal_transaction`.`index` desc
pull/156/head
Tim Mecklem 7 years ago
parent b05fd93da6
commit 5f7f9c48d0
  1. 4
      apps/explorer/lib/explorer/chain.ex
  2. 37
      apps/explorer/test/explorer/chain_test.exs

@ -442,7 +442,9 @@ defmodule Explorer.Chain do
InternalTransaction InternalTransaction
|> where_address_fields_match(address_fields, id) |> where_address_fields_match(address_fields, id)
|> join(:inner, [inner_transaction], transaction in assoc(inner_transaction, :transaction)) |> join(:inner, [internal_transaction], transaction in assoc(internal_transaction, :transaction))
|> join(:left, [internal_transaction, transaction], block in assoc(transaction, :block))
|> order_by([it, transaction, block], desc: block.number, desc: transaction.transaction_index, desc: it.index)
|> preload(transaction: :block) |> preload(transaction: :block)
|> join_associations(necessity_by_association) |> join_associations(necessity_by_association)
|> Repo.all() |> Repo.all()

@ -591,7 +591,7 @@ defmodule Explorer.ChainTest do
describe "address_to_internal_transactions/1" do describe "address_to_internal_transactions/1" do
test "with single transaction containing an internal transaction" do test "with single transaction containing an internal transaction" do
address = insert(:address) address = insert(:address)
transaction = insert(:transaction, to_address_id: address.id) transaction = insert(:transaction)
%InternalTransaction{id: expected_id} = %InternalTransaction{id: expected_id} =
insert(:internal_transaction, transaction_id: transaction.id, to_address_id: address.id) insert(:internal_transaction, transaction_id: transaction.id, to_address_id: address.id)
@ -629,6 +629,41 @@ defmodule Explorer.ChainTest do
} }
) )
end end
test "Returns results in reverse chronological order by block number, transaction index, internal transaction index" do
address = insert(:address)
pending_transaction = :transaction |> insert(transaction_index: "3")
first_block = insert(:block, number: 2000)
first_transaction = :transaction |> insert(transaction_index: "10") |> with_block(first_block)
second_transaction = :transaction |> insert(transaction_index: "20") |> with_block(first_block)
second_block = insert(:block, number: 4000)
third_transaction = :transaction |> insert(transaction_index: "5") |> with_block(second_block)
%InternalTransaction{id: pending_id} =
insert(:internal_transaction, transaction: pending_transaction, to_address_id: address.id, index: 0)
%InternalTransaction{id: first_id} =
insert(:internal_transaction, transaction: first_transaction, to_address_id: address.id, index: 0)
%InternalTransaction{id: second_id} =
insert(:internal_transaction, transaction: second_transaction, to_address_id: address.id, index: 0)
%InternalTransaction{id: third_id} =
insert(:internal_transaction, transaction: third_transaction, to_address_id: address.id, index: 0)
%InternalTransaction{id: fourth_id} =
insert(:internal_transaction, transaction: third_transaction, to_address_id: address.id, index: 1)
result =
address
|> Chain.address_to_internal_transactions()
|> Enum.map(fn internal_transaction -> internal_transaction.id end)
assert [pending_id, fourth_id, third_id, second_id, first_id] == result
end
end end
describe "transaction_hash_to_internal_transactions/1" do describe "transaction_hash_to_internal_transactions/1" do

Loading…
Cancel
Save