wsa-implement-emission-reward-tile
William Sanches 6 years ago
parent 57fab18a39
commit dfc600b1c9
No known key found for this signature in database
GPG Key ID: 27250E49FB133014
  1. 57
      apps/block_scout_web/test/block_scout_web/channels/reward_channel_test.exs
  2. 32
      apps/block_scout_web/test/block_scout_web/features/viewing_addresses_test.exs
  3. 75
      apps/explorer/test/explorer/chain_test.exs

@ -0,0 +1,57 @@
defmodule BlockScoutWeb.RewardChannelTest do
use BlockScoutWeb.ChannelCase, async: false
alias BlockScoutWeb.Notifier
describe "user subscribed to rewards" do
test "does nothing if the configuration is turned off" do
Application.put_env(:block_scout_web, BlockScoutWeb.Chain, has_emission_funds: false)
address = insert(:address)
block = insert(:block)
reward = insert(:reward, address_hash: address.hash, block_hash: block.hash)
topic = "rewards:#{address.hash}"
@endpoint.subscribe(topic)
Notifier.handle_event({:chain_event, :block_rewards, :realtime, [reward]})
refute_receive _, :timer.seconds(2)
Application.put_env(:block_scout_web, BlockScoutWeb.Chain, has_emission_funds: false)
end
test "notified of new reward for matching address" do
Application.put_env(:block_scout_web, BlockScoutWeb.Chain, has_emission_funds: true)
address = insert(:address)
block = insert(:block)
reward = insert(:reward, address_hash: address.hash, block_hash: block.hash)
topic = "rewards:#{address.hash}"
@endpoint.subscribe(topic)
Notifier.handle_event({:chain_event, :block_rewards, :realtime, [reward]})
assert_receive %Phoenix.Socket.Broadcast{topic: ^topic, event: "new_reward", payload: _}, :timer.seconds(5)
Application.put_env(:block_scout_web, BlockScoutWeb.Chain, has_emission_funds: false)
end
test "not notified of new reward for other address" do
Application.put_env(:block_scout_web, BlockScoutWeb.Chain, has_emission_funds: true)
address = insert(:address)
block = insert(:block)
reward = insert(:reward, address_hash: address.hash, block_hash: block.hash)
topic = "rewards:0x0"
@endpoint.subscribe(topic)
Notifier.handle_event({:chain_event, :block_rewards, :realtime, [reward]})
refute_receive _, :timer.seconds(2)
Application.put_env(:block_scout_web, BlockScoutWeb.Chain, has_emission_funds: false)
end
end
end

@ -22,10 +22,27 @@ defmodule BlockScoutWeb.ViewingAddressesTest do
|> insert(from_address: lincoln, to_address: taft)
|> with_block(block)
lincoln_reward =
:reward
|> insert(
address_hash: lincoln.hash,
block_hash: block.hash,
address_type: :emission_funds
)
taft_reward =
:reward
|> insert(
address_hash: taft.hash,
block_hash: block.hash,
address_type: :validator
)
{:ok,
%{
addresses: %{lincoln: lincoln, taft: taft},
block: block,
rewards: {lincoln_reward, taft_reward},
transactions: %{from_lincoln: from_lincoln, from_taft: from_taft}
}}
end
@ -163,6 +180,21 @@ defmodule BlockScoutWeb.ViewingAddressesTest do
|> assert_has(AddressPage.transaction_address_link(transactions.from_lincoln, :to))
|> refute_has(AddressPage.transaction_address_link(transactions.from_lincoln, :from))
end
test "sees rewards to and from an address alongside transactions", %{
addresses: addresses,
session: session,
transactions: transactions
} do
Application.put_env(:block_scout_web, BlockScoutWeb.Chain, has_emission_funds: true)
session
|> AddressPage.visit_page(addresses.lincoln)
|> assert_has(AddressPage.transaction(transactions.from_taft))
|> assert_has(AddressPage.transaction(transactions.from_lincoln))
Application.put_env(:block_scout_web, BlockScoutWeb.Chain, has_emission_funds: false)
end
end
describe "viewing internal transactions" do

@ -392,6 +392,81 @@ defmodule Explorer.ChainTest do
assert [fourth, third, second, first, sixth, fifth] == result
end
test "with emission rewards" do
Application.put_env(:block_scout_web, BlockScoutWeb.Chain, has_emission_funds: true)
block = insert(:block)
insert(
:reward,
address_hash: block.miner_hash,
block_hash: block.hash,
address_type: :validator
)
insert(
:reward,
address_hash: block.miner_hash,
block_hash: block.hash,
address_type: :emission_funds
)
assert [{_, _}] = Chain.address_to_transactions_with_rewards(block.miner)
Application.put_env(:block_scout_web, BlockScoutWeb.Chain, has_emission_funds: false)
end
test "with emission rewards and transactions" do
Application.put_env(:block_scout_web, BlockScoutWeb.Chain, has_emission_funds: true)
block = insert(:block)
insert(
:reward,
address_hash: block.miner_hash,
block_hash: block.hash,
address_type: :validator
)
insert(
:reward,
address_hash: block.miner_hash,
block_hash: block.hash,
address_type: :emission_funds
)
:transaction
|> insert(from_address: block.miner)
|> with_block()
|> Repo.preload(:token_transfers)
assert [_, {_, _}] = Chain.address_to_transactions_with_rewards(block.miner, direction: :from)
Application.put_env(:block_scout_web, BlockScoutWeb.Chain, has_emission_funds: false)
end
test "with emissions rewards, but feature disabled" do
Application.put_env(:block_scout_web, BlockScoutWeb.Chain, has_emission_funds: false)
block = insert(:block)
insert(
:reward,
address_hash: block.miner_hash,
block_hash: block.hash,
address_type: :validator
)
insert(
:reward,
address_hash: block.miner_hash,
block_hash: block.hash,
address_type: :emission_funds
)
assert [] == Chain.address_to_transactions_with_rewards(block.miner)
end
end
describe "total_transactions_sent_by_address/1" do

Loading…
Cancel
Save