Remove excess delegatorShare calls

staking
Vadim 5 years ago committed by Victor Baranov
parent 93816551c7
commit e5466cd569
  1. 34
      apps/explorer/lib/explorer/staking/contract_state.ex
  2. 27
      apps/explorer/lib/explorer/staking/stake_snapshotting.ex
  3. 36
      apps/explorer/test/explorer/staking/contract_state_test.exs

@ -177,17 +177,22 @@ defmodule Explorer.Staking.ContractState do
candidate_reward_responses =
get_candidate_reward_responses(pool_staking_responses, global_responses, pool_staking_keys, contracts, abi)
# to keep sort order when using `perform_grouped_requests` (see below)
delegator_keys = Enum.map(staker_responses, fn {key, _} -> key end)
# call `BlockReward.delegatorShare` function for each delegator
# to get their reward share of the pool (needed for the `Delegators` list in UI)
delegator_responses =
Enum.reduce(staker_responses, %{}, fn {{pool_staking_address, staker_address, _is_active} = key, value}, acc ->
if pool_staking_address != staker_address do
Map.put(acc, key, value)
else
acc
end
end)
delegator_reward_responses =
get_delegator_reward_responses(
staker_responses,
delegator_responses,
pool_staking_responses,
global_responses,
delegator_keys,
contracts,
abi
)
@ -325,14 +330,16 @@ defmodule Explorer.Staking.ContractState do
end
defp get_delegator_reward_responses(
staker_responses,
delegator_responses,
pool_staking_responses,
global_responses,
delegator_keys,
contracts,
abi
) do
staker_responses
# to keep sort order when using `perform_grouped_requests` (see below)
delegator_keys = Enum.map(delegator_responses, fn {key, _} -> key end)
delegator_responses
|> Enum.map(fn {{pool_staking_address, _staker_address, _is_active}, resp} ->
staking_resp = pool_staking_responses[pool_staking_address]
@ -348,14 +355,19 @@ defmodule Explorer.Staking.ContractState do
end
defp get_delegator_entries(staker_responses, delegator_reward_responses) do
Enum.map(staker_responses, fn {{pool_address, delegator_address, is_active}, response} ->
delegator_reward_response = delegator_reward_responses[{pool_address, delegator_address, is_active}]
Enum.map(staker_responses, fn {{pool_address, delegator_address, is_active} = key, response} ->
delegator_share =
if Map.has_key?(delegator_reward_responses, key) do
delegator_reward_responses[key].delegator_share
else
0
end
Map.merge(response, %{
address_hash: delegator_address,
staking_address_hash: pool_address,
is_active: is_active,
reward_ratio: Float.floor(delegator_reward_response.delegator_share / 10_000, 2)
reward_ratio: Float.floor(delegator_share / 10_000, 2)
})
end)
end

@ -98,13 +98,21 @@ defmodule Explorer.Staking.StakeSnapshotting do
end)
|> ContractReader.perform_grouped_requests(pool_staking_keys, contracts, abi)
# to keep sort order when using `perform_grouped_requests` (see below)
delegator_keys = Enum.map(staker_responses, fn {key, _} -> key end)
# call `BlockReward.delegatorShare` function for each delegator
# to get their reward share of the pool (needed for the `Delegators` list in UI)
delegator_responses =
Enum.reduce(staker_responses, %{}, fn {{pool_staking_address, staker_address} = key, value}, acc ->
if pool_staking_address != staker_address do
Map.put(acc, key, value)
else
acc
end
end)
delegator_keys = Enum.map(delegator_responses, fn {key, _} -> key end)
delegator_reward_responses =
staker_responses
delegator_responses
|> Enum.map(fn {{pool_staking_address, _staker_address}, resp} ->
staking_resp = pool_staking_responses[pool_staking_address]
@ -145,8 +153,13 @@ defmodule Explorer.Staking.StakeSnapshotting do
# form entries for updating the `staking_pools_delegators` table in DB
delegator_entries =
Enum.map(staker_responses, fn {{pool_staking_address, staker_address}, resp} ->
delegator_reward_resp = delegator_reward_responses[{pool_staking_address, staker_address}]
Enum.map(staker_responses, fn {{pool_staking_address, staker_address} = key, resp} ->
delegator_share =
if Map.has_key?(delegator_reward_responses, key) do
delegator_reward_responses[key].delegator_share
else
0
end
%{
address_hash: staker_address,
@ -155,7 +168,7 @@ defmodule Explorer.Staking.StakeSnapshotting do
max_withdraw_allowed: 0,
ordered_withdraw: 0,
ordered_withdraw_epoch: 0,
snapshotted_reward_ratio: Float.floor(delegator_reward_resp.delegator_share / 10_000, 2),
snapshotted_reward_ratio: Float.floor(delegator_share / 10_000, 2),
snapshotted_stake_amount: resp.snapshotted_stake_amount,
stake_amount: 0,
staking_address_hash: pool_staking_address

@ -614,22 +614,22 @@ defmodule Explorer.Staking.ContractStateTest do
EthereumJSONRPC.Mox,
:json_rpc,
fn requests, _opts ->
assert length(requests) == 16
assert length(requests) == 10
{:ok,
format_responses([
# 1 BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000051615",
# 2 BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# 3 BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# 4 BlockRewardAuRa.delegatorShare
"0x00000000000000000000000000000000000000000000000000000000000a2c2a",
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# 5 BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000051615",
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# 6 BlockRewardAuRa.delegatorShare
"0x00000000000000000000000000000000000000000000000000000000000f4240",
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# 7 BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# 8 BlockRewardAuRa.delegatorShare
@ -637,18 +637,6 @@ defmodule Explorer.Staking.ContractStateTest do
# 9 BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# 10 BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# 11 BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# 12 BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# 13 BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# 14 BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000025fa3",
# 15 BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# 16 BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000012fd1"
])}
end
@ -998,19 +986,13 @@ defmodule Explorer.Staking.ContractStateTest do
EthereumJSONRPC.Mox,
:json_rpc,
fn requests, _opts ->
assert length(requests) == 14
assert length(requests) == 10
{:ok,
format_responses([
# BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000000000",
# BlockRewardAuRa.delegatorShare
"0x00000000000000000000000000000000000000000000000000000000000a2c2a",
# BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000051615",
# BlockRewardAuRa.delegatorShare
"0x00000000000000000000000000000000000000000000000000000000000f4240",
# BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
@ -1025,8 +1007,6 @@ defmodule Explorer.Staking.ContractStateTest do
# BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000025fa3",
# BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000012fd1",
# BlockRewardAuRa.delegatorShare
"0x0000000000000000000000000000000000000000000000000000000000012fd1"

Loading…
Cancel
Save