Merge pull request #6511 from blockscout/np-fix-api-2-txs-1

Split ordering cases for txs
pull/6512/head
Victor Baranov 2 years ago committed by GitHub
commit e3b3468f2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      CHANGELOG.md
  2. 24
      apps/explorer/lib/explorer/chain.ex
  3. 73
      apps/explorer/priv/repo/migrations/20221126103223_add_txs_indexes.exs

@ -32,7 +32,7 @@
- [#6473](https://github.com/blockscout/blockscout/pull/6473) - Fix state changes for contract creation transactions - [#6473](https://github.com/blockscout/blockscout/pull/6473) - Fix state changes for contract creation transactions
- [#6475](https://github.com/blockscout/blockscout/pull/6475) - Fix token name with unicode graphemes shortening - [#6475](https://github.com/blockscout/blockscout/pull/6475) - Fix token name with unicode graphemes shortening
- [#6420](https://github.com/blockscout/blockscout/pull/6420) - Fix address logs search - [#6420](https://github.com/blockscout/blockscout/pull/6420) - Fix address logs search
- [#6390](https://github.com/blockscout/blockscout/pull/6390), [#6502](https://github.com/blockscout/blockscout/pull/6502) - Fix transactions responses in API v2 - [#6390](https://github.com/blockscout/blockscout/pull/6390), [#6502](https://github.com/blockscout/blockscout/pull/6502), [#6511](https://github.com/blockscout/blockscout/pull/6511) - Fix transactions responses in API v2
- [#6357](https://github.com/blockscout/blockscout/pull/6357), [#6409](https://github.com/blockscout/blockscout/pull/6409), [#6428](https://github.com/blockscout/blockscout/pull/6428) - Fix definitions of NETWORK_PATH, API_PATH, SOCKET_ROOT: process trailing slash - [#6357](https://github.com/blockscout/blockscout/pull/6357), [#6409](https://github.com/blockscout/blockscout/pull/6409), [#6428](https://github.com/blockscout/blockscout/pull/6428) - Fix definitions of NETWORK_PATH, API_PATH, SOCKET_ROOT: process trailing slash
- [#6338](https://github.com/blockscout/blockscout/pull/6338) - Fix token search with space - [#6338](https://github.com/blockscout/blockscout/pull/6338) - Fix token search with space
- [#6329](https://github.com/blockscout/blockscout/pull/6329) - Prevent logger from truncating response from rust verifier service in case of an error - [#6329](https://github.com/blockscout/blockscout/pull/6329) - Prevent logger from truncating response from rust verifier service in case of an error

@ -437,7 +437,7 @@ defmodule Explorer.Chain do
options options
|> Keyword.get(:paging_options, @default_paging_options) |> Keyword.get(:paging_options, @default_paging_options)
|> fetch_transactions(from_block, to_block) |> fetch_transactions(from_block, to_block, true)
end end
defp transactions_block_numbers_at_address(address_hash, options) do defp transactions_block_numbers_at_address(address_hash, options) do
@ -3448,7 +3448,7 @@ defmodule Explorer.Chain do
|> pending_transactions_query() |> pending_transactions_query()
|> apply_filter_by_method_id_to_transactions(method_id_filter) |> apply_filter_by_method_id_to_transactions(method_id_filter)
|> apply_filter_by_tx_type_to_transactions(type_filter) |> apply_filter_by_tx_type_to_transactions(type_filter)
|> order_by([transaction], desc: transaction.inserted_at, desc: transaction.hash) |> order_by([transaction], desc: transaction.inserted_at, asc: transaction.hash)
|> join_associations(necessity_by_association) |> join_associations(necessity_by_association)
|> (&if(old_ui?, do: preload(&1, [{:token_transfers, [:token, :from_address, :to_address]}]), else: &1)).() |> (&if(old_ui?, do: preload(&1, [{:token_transfers, [:token, :from_address, :to_address]}]), else: &1)).()
|> Repo.all() |> Repo.all()
@ -4404,16 +4404,26 @@ defmodule Explorer.Chain do
if Repo.one(query), do: true, else: false if Repo.one(query), do: true, else: false
end end
defp fetch_transactions(paging_options \\ nil, from_block \\ nil, to_block \\ nil) do defp fetch_transactions(paging_options \\ nil, from_block \\ nil, to_block \\ nil, is_address? \\ false) do
Transaction Transaction
|> order_for_transactions(is_address?)
|> where_block_number_in_period(from_block, to_block)
|> handle_paging_options(paging_options)
end
defp order_for_transactions(query, true) do
query
|> order_by([transaction], |> order_by([transaction],
desc: transaction.block_number, desc: transaction.block_number,
desc: transaction.index, desc: transaction.index,
desc: transaction.inserted_at, desc: transaction.inserted_at,
desc: transaction.hash asc: transaction.hash
) )
|> where_block_number_in_period(from_block, to_block) end
|> handle_paging_options(paging_options)
defp order_for_transactions(query, _) do
query
|> order_by([transaction], desc: transaction.block_number, desc: transaction.index)
end end
defp fetch_transactions_in_ascending_order_by_index(paging_options) do defp fetch_transactions_in_ascending_order_by_index(paging_options) do
@ -4626,7 +4636,7 @@ defmodule Explorer.Chain do
[transaction], [transaction],
(is_nil(transaction.block_number) and (is_nil(transaction.block_number) and
(transaction.inserted_at < ^inserted_at or (transaction.inserted_at < ^inserted_at or
(transaction.inserted_at == ^inserted_at and transaction.hash < ^hash))) or (transaction.inserted_at == ^inserted_at and transaction.hash > ^hash))) or
not is_nil(transaction.block_number) not is_nil(transaction.block_number)
) )
end end

@ -0,0 +1,73 @@
defmodule Explorer.Repo.Migrations.AddTxsIndexes do
use Ecto.Migration
@disable_ddl_transaction true
@disable_migration_lock true
def change do
drop_if_exists(
index(
:transactions,
[:from_address_hash, "block_number DESC NULLS FIRST", "index DESC NULLS FIRST", :hash],
name: "transactions_from_address_hash_recent_collated_index",
concurrently: true
)
)
drop_if_exists(
index(
:transactions,
[:to_address_hash, "block_number DESC NULLS FIRST", "index DESC NULLS FIRST", :hash],
name: "transactions_to_address_hash_recent_collated_index",
concurrently: true
)
)
drop_if_exists(
index(
:transactions,
[:created_contract_address_hash, "block_number DESC NULLS FIRST", "index DESC NULLS FIRST", :hash],
name: "transactions_created_contract_address_hash_recent_collated_index",
concurrently: true
)
)
create_if_not_exists(
index(
:transactions,
[
:from_address_hash,
"block_number DESC NULLS FIRST",
"index DESC NULLS FIRST",
"inserted_at DESC",
"hash ASC"
],
name: "transactions_from_address_hash_with_pending_index",
concurrently: true
)
)
create_if_not_exists(
index(
:transactions,
[:to_address_hash, "block_number DESC NULLS FIRST", "index DESC NULLS FIRST", "inserted_at DESC", "hash ASC"],
name: "transactions_to_address_hash_with_pending_index",
concurrently: true
)
)
create_if_not_exists(
index(
:transactions,
[
:created_contract_address_hash,
"block_number DESC NULLS FIRST",
"index DESC NULLS FIRST",
"inserted_at DESC",
"hash ASC"
],
name: "transactions_created_contract_address_hash_with_pending_index",
concurrently: true
)
)
end
end
Loading…
Cancel
Save