Skip to content

Commit

Permalink
Add websocket v2 tests; Fix websocket v1 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitosing authored and vbaranov committed May 16, 2023
1 parent a13450b commit 468e092
Show file tree
Hide file tree
Showing 7 changed files with 441 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,7 @@
### Fixes

- [#7490](https://github.com/blockscout/blockscout/pull/7490) - Fix pending txs is not a map
- [#7474](https://github.com/blockscout/blockscout/pull/7474) - Websocket v2 improvements
- [#7472](https://github.com/blockscout/blockscout/pull/7472) - Fix RE_CAPTCHA_DISABLED variable parsing
- [#7391](https://github.com/blockscout/blockscout/pull/7391) - Fix: cannot read properties of null (reading 'value')
- [#7377](https://github.com/blockscout/blockscout/pull/7377), [#7454](https://github.com/blockscout/blockscout/pull/7454) - API v2 improvements
Expand Down
Expand Up @@ -76,7 +76,7 @@ defmodule BlockScoutWeb.TransactionChannel do
%Phoenix.Socket{handler: BlockScoutWeb.UserSocketV2} = socket
)
when is_list(transactions) do
push(socket, "transaction", %{transactions: Enum.count(transactions)})
push(socket, "transaction", %{transaction: Enum.count(transactions)})

{:noreply, socket}
end
Expand Down
2 changes: 1 addition & 1 deletion apps/block_scout_web/lib/block_scout_web/endpoint.ex
Expand Up @@ -7,7 +7,7 @@ defmodule BlockScoutWeb.Endpoint do
end

socket("/socket", BlockScoutWeb.UserSocket, websocket: [timeout: 45_000])
socket("/socket/v2", BlockScoutWeb.UserSocketV2, websocket: [timeout: :infinity])
socket("/socket/v2", BlockScoutWeb.UserSocketV2, websocket: [timeout: 45_000])

# Serve at "/" the static files from "priv/static" directory.
#
Expand Down
31 changes: 9 additions & 22 deletions apps/block_scout_web/lib/block_scout_web/notifier.ex
Expand Up @@ -5,6 +5,8 @@ defmodule BlockScoutWeb.Notifier do

alias Absinthe.Subscription

alias BlockScoutWeb.API.V2, as: API_V2

alias BlockScoutWeb.{
AddressContractVerificationViaFlattenedCodeView,
AddressContractVerificationViaJsonView,
Expand Down Expand Up @@ -143,6 +145,7 @@ defmodule BlockScoutWeb.Notifier do
Endpoint.broadcast("transactions:#{transaction_hash}", "raw_trace", %{raw_trace_origin: transaction_hash})
end

# internal txs broadcast disabled on the indexer level, therefore it out of scope of the refactoring within https://github.com/blockscout/blockscout/pull/7474
def handle_event({:chain_event, :internal_transactions, :realtime, internal_transactions}) do
internal_transactions
|> Stream.map(
Expand All @@ -156,7 +159,7 @@ defmodule BlockScoutWeb.Notifier do
def handle_event({:chain_event, :token_transfers, :realtime, all_token_transfers}) do
all_token_transfers_full =
all_token_transfers
|> Stream.map(
|> Enum.map(
&(&1
|> Repo.preload([:from_address, :to_address, :token, transaction: :block]))
)
Expand All @@ -178,15 +181,12 @@ defmodule BlockScoutWeb.Notifier do
end

def handle_event({:chain_event, :transactions, :realtime, transactions}) do
preloads = [:block, created_contract_address: :names, from_address: :names, to_address: :names]

transactions
|> Enum.map(& &1.hash)
|> Chain.hashes_to_transactions(
necessity_by_association: %{
:block => :optional,
[created_contract_address: :names] => :optional,
[from_address: :names] => :optional,
[to_address: :names] => :optional
}
|> Enum.map(
&(&1
|> Repo.preload(if API_V2.enabled?(), do: [:token_transfers | preloads], else: preloads))
)
|> broadcast_transactions_websocket_v2()
|> Enum.map(fn tx ->
Expand Down Expand Up @@ -477,20 +477,7 @@ defmodule BlockScoutWeb.Notifier do
grouped = Map.merge(grouped_by_to, grouped_by_from, fn _k, v1, v2 -> Enum.uniq(v1 ++ v2) end)

for {address_hash, elements} <- grouped do
if "0xbb36c792b9b45aaf8b848a1392b0d6559202729e" == "#{address_hash}" do
debug("addresses:#{address_hash}", event)
debug(%{map_key => elements}, "12312312313")
end

Endpoint.broadcast("addresses:#{address_hash}", event, %{map_key => elements})
end
end

defp debug(value, key) do
require Logger
Logger.configure(truncate: :infinity)
Logger.info(key)
Logger.info(Kernel.inspect(value, limit: :infinity, printable_limit: :infinity))
value
end
end
Expand Up @@ -80,7 +80,11 @@ defmodule BlockScoutWeb.AddressChannelTest do

Notifier.handle_event({:chain_event, :transactions, :realtime, [pending]})

assert_receive %Phoenix.Socket.Broadcast{topic: ^topic, event: "pending_transaction", payload: payload},
assert_receive %Phoenix.Socket.Broadcast{
topic: ^topic,
event: "pending_transaction",
payload: %{transaction: _} = payload
},
:timer.seconds(5)

assert payload.address.hash == address.hash
Expand All @@ -95,7 +99,13 @@ defmodule BlockScoutWeb.AddressChannelTest do

Notifier.handle_event({:chain_event, :transactions, :realtime, [transaction]})

assert_receive %Phoenix.Socket.Broadcast{topic: ^topic, event: "transaction", payload: payload}, :timer.seconds(5)
assert_receive %Phoenix.Socket.Broadcast{
topic: ^topic,
event: "transaction",
payload: %{transaction: _} = payload
},
:timer.seconds(5)

assert payload.address.hash == address.hash
assert payload.transaction.hash == transaction.hash
end
Expand All @@ -108,7 +118,13 @@ defmodule BlockScoutWeb.AddressChannelTest do

Notifier.handle_event({:chain_event, :transactions, :realtime, [transaction]})

assert_receive %Phoenix.Socket.Broadcast{topic: ^topic, event: "transaction", payload: payload}, :timer.seconds(5)
assert_receive %Phoenix.Socket.Broadcast{
topic: ^topic,
event: "transaction",
payload: %{transaction: _} = payload
},
:timer.seconds(5)

assert payload.address.hash == address.hash
assert payload.transaction.hash == transaction.hash
end
Expand All @@ -121,11 +137,19 @@ defmodule BlockScoutWeb.AddressChannelTest do

Notifier.handle_event({:chain_event, :transactions, :realtime, [transaction]})

assert_receive %Phoenix.Socket.Broadcast{topic: ^topic, event: "transaction", payload: payload}, :timer.seconds(5)
assert_receive %Phoenix.Socket.Broadcast{
topic: ^topic,
event: "transaction",
payload: %{transaction: _} = payload
},
:timer.seconds(5)

assert payload.address.hash == address.hash
assert payload.transaction.hash == transaction.hash

refute_receive _, 100, "Received duplicate broadcast."
refute_receive %Phoenix.Socket.Broadcast{topic: ^topic, event: "transaction", payload: %{transaction: _}},
100,
"Received duplicate broadcast."
end

test "notified of new_internal_transaction for matching from_address", %{address: address, topic: topic} do
Expand Down
@@ -1,7 +1,7 @@
defmodule BlockScoutWeb.TransactionChannelTest do
use BlockScoutWeb.ChannelCase

alias Explorer.Chain.Hash
alias Explorer.Chain.{Hash, Import, Transaction}
alias BlockScoutWeb.Notifier

test "subscribed user is notified of new_transaction topic" do
Expand All @@ -16,7 +16,7 @@ defmodule BlockScoutWeb.TransactionChannelTest do
Notifier.handle_event({:chain_event, :transactions, :realtime, [transaction]})

receive do
%Phoenix.Socket.Broadcast{topic: ^topic, event: "transaction", payload: payload} ->
%Phoenix.Socket.Broadcast{topic: ^topic, event: "transaction", payload: %{transaction: _transaction} = payload} ->
assert payload.transaction.hash == transaction.hash
after
:timer.seconds(5) ->
Expand All @@ -33,7 +33,11 @@ defmodule BlockScoutWeb.TransactionChannelTest do
Notifier.handle_event({:chain_event, :transactions, :realtime, [pending]})

receive do
%Phoenix.Socket.Broadcast{topic: ^topic, event: "pending_transaction", payload: payload} ->
%Phoenix.Socket.Broadcast{
topic: ^topic,
event: "pending_transaction",
payload: %{transaction: _transaction} = payload
} ->
assert payload.transaction.hash == pending.hash
after
:timer.seconds(5) ->
Expand Down

0 comments on commit 468e092

Please sign in to comment.