Skip to content

Commit

Permalink
fix: uniq by hash, instead of transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed May 29, 2019
1 parent 32472a0 commit 5edf4b4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- [#2008](https://github.com/poanetwork/blockscout/pull/2008) - add new function clause for xDai network beneficiaries
- [#2009](https://github.com/poanetwork/blockscout/pull/2009) - addresses page improvements
- [#2027](https://github.com/poanetwork/blockscout/pull/2027) - fix: `BlocksTransactionsMismatch` ignoring blocks without transactions
- [#2062](https://github.com/poanetwork/blockscout/pull/2062) - fix: uniq by hash, instead of transaction

### Chore

Expand Down
2 changes: 1 addition & 1 deletion apps/explorer/lib/explorer/chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ defmodule Explorer.Chain do

queries
|> Stream.flat_map(&Repo.all/1)
|> Stream.uniq()
|> Stream.uniq_by(& &1.hash)
|> Stream.concat(rewards_list)
|> Enum.sort_by(fn item ->
case item do
Expand Down
63 changes: 15 additions & 48 deletions apps/explorer/lib/explorer/chain/token_transfer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -204,59 +204,26 @@ defmodule Explorer.Chain.TokenTransfer do
transaction_hashes_from_token_transfers_sql(address_bytes, paging_options)
end

defp transaction_hashes_from_token_transfers_sql(address_bytes, %PagingOptions{key: nil, page_size: page_size}) do
{:ok, %Postgrex.Result{rows: transaction_hashes_from_token_transfers}} =
Repo.query(
"""
SELECT transaction_hash
FROM
(
SELECT transaction_hash
FROM token_transfers
WHERE from_address_hash = $1
UNION
SELECT transaction_hash
FROM token_transfers
WHERE to_address_hash = $1
) as token_transfers_transaction_hashes
LIMIT $2
""",
[address_bytes, page_size]
defp transaction_hashes_from_token_transfers_sql(address_bytes, %PagingOptions{page_size: page_size} = paging_options) do
to_query =
from(token_transfer in TokenTransfer,
where: token_transfer.to_address_hash == ^address_bytes,
select: type(token_transfer.to_address_hash, :binary)
)

List.flatten(transaction_hashes_from_token_transfers)
end
from_query =
from(token_transfer in TokenTransfer,
where: token_transfer.from_address_hash == ^address_bytes,
select: type(token_transfer.from_address_hash, :binary)
)

defp transaction_hashes_from_token_transfers_sql(address_bytes, %PagingOptions{
key: {block_number, _index},
page_size: page_size
}) do
{:ok, %Postgrex.Result{rows: transaction_hashes_from_token_transfers}} =
Repo.query(
"""
SELECT transaction_hash
FROM
(
SELECT transaction_hash
FROM token_transfers
WHERE from_address_hash = $1
AND block_number < $2
UNION
SELECT transaction_hash
FROM token_transfers
WHERE to_address_hash = $1
AND block_number < $2
) as token_transfers_transaction_hashes
LIMIT $3
""",
[address_bytes, block_number, page_size]
query =
from(token_transfer in page_transaction_hashes_from_token_transfers(from_query, paging_options),
union: ^page_transaction_hashes_from_token_transfers(to_query, paging_options),
limit: ^page_size
)

List.flatten(transaction_hashes_from_token_transfers)
Repo.all(query)
end

defp page_transaction_hashes_from_token_transfers(query, %PagingOptions{key: nil}), do: query
Expand Down

0 comments on commit 5edf4b4

Please sign in to comment.