Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-run the tzkt tests #501

Merged
merged 5 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions tests/integration/test_tzkt_reward_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
from NetworkConfiguration import default_network_config_map
from parameterized import parameterized

'''
These tests are cached. To re-run, delete contents of the tzkt_data folder.
'''


def load_reward_model(address, cycle, suffix) -> Optional[RewardProviderModel]:
path = join(dirname(__file__), f'tzkt_data/{address}_{cycle}_{suffix}.json')
Expand All @@ -32,13 +36,13 @@ def store_reward_model(address, cycle, suffix, model: RewardProviderModel):
delegate_staking_balance=model.delegate_staking_balance,
total_reward_amount=model.total_reward_amount,
delegator_balance_dict={
k: v
k: {'staking_balance': v['staking_balance']}
for k, v in model.delegator_balance_dict.items()
if v['staking_balance'] > 0
}
)
with open(path, 'w+') as f:
f.write(json.dumps(data))
f.write(json.dumps(data, indent=2))


dummy_addr_dict = dict(
Expand All @@ -61,15 +65,25 @@ def assertBalancesAlmostEqual(self, expected: dict, actual: dict, delta=1):
self.assertAlmostEqual(balances['staking_balance'], actual[address]['staking_balance'], delta=delta, msg=address)

@parameterized.expand([
('tz1ZRWFLgT9sz8iFi1VYWPfRYeUvUSFAaDao', 201),
('tz1Lhf4J9Qxoe3DZ2nfe8FGDnvVj7oKjnMY6', 185), # double baking (loss)
('tz1WnfXMPaNTBmH7DBPwqCWs9cPDJdkGBTZ8', 74), # double baking (gain)
('tz1PeZx7FXy7QRuMREGXGxeipb24RsMMzUNe', 135), # double endorsement (loss)
('tz1gk3TDbU7cJuiBRMhwQXVvgDnjsxuWhcEA', 135), # double endorsement (gain)
('tz1S1Aew75hMrPUymqenKfHo8FspppXKpW7h', 233), # revelation rewards
('tz1UUgPwikRHW1mEyVZfGYy6QaxrY6Y7WaG5', 207), # revelation miss
('tz1ZRWFLgT9sz8iFi1VYWPfRYeUvUSFAaDao', 201, 0),
('tz1Lhf4J9Qxoe3DZ2nfe8FGDnvVj7oKjnMY6', 185, 0), # double baking (loss)
('tz1WnfXMPaNTBmH7DBPwqCWs9cPDJdkGBTZ8', 74, 32000000), # double baking (gain)
('tz1PeZx7FXy7QRuMREGXGxeipb24RsMMzUNe', 135, 0), # double endorsement (loss)
('tz1gk3TDbU7cJuiBRMhwQXVvgDnjsxuWhcEA', 135, 19328043790), # double endorsement (gain)
('tz1S1Aew75hMrPUymqenKfHo8FspppXKpW7h', 233, 0), # revelation rewards
('tz1UUgPwikRHW1mEyVZfGYy6QaxrY6Y7WaG5', 207, 0), # revelation miss
])
def test_get_rewards_for_cycle_map(self, address, cycle):
def test_get_rewards_for_cycle_map(self, address, cycle, hardcoded_denunciation_reward):
'''
This test compares the total rewards and balance according to tzkt,
to the total rewards according to rpc.

It also compares the balances per delegator.

Note: double baking and double endorsement gain are taken into account
by the protocol but not by TRD, so there are discrepancies. To resolve,
we are hardcoding the expected gain in hardcoded_denunciation_reward.
'''
rpc_rewards = load_reward_model(address, cycle, 'actual')
if rpc_rewards is None:
rpc_impl = RpcRewardApiImpl(
Expand All @@ -84,8 +98,10 @@ def test_get_rewards_for_cycle_map(self, address, cycle):
baking_address=address)
tzkt_rewards = tzkt_impl.get_rewards_for_cycle_map(cycle, RewardsType.ACTUAL)

total_reward_amount = rpc_rewards.total_reward_amount - hardcoded_denunciation_reward

self.assertAlmostEqual(rpc_rewards.delegate_staking_balance, tzkt_rewards.delegate_staking_balance, delta=1)
self.assertAlmostEqual(rpc_rewards.total_reward_amount, tzkt_rewards.total_reward_amount, delta=1)
self.assertAlmostEqual(total_reward_amount, tzkt_rewards.total_reward_amount, delta=1)
self.assertBalancesAlmostEqual(rpc_rewards.delegator_balance_dict, tzkt_rewards.delegator_balance_dict, delta=1)

@parameterized.expand([
Expand Down Expand Up @@ -134,7 +150,7 @@ def test_staking_balance_issue(self):
baking_address=address)
tzkt_rewards = tzkt_impl.get_rewards_for_cycle_map(cycle, RewardsType.ACTUAL)

self.assertNotEqual(rpc_rewards.delegate_staking_balance, tzkt_rewards.delegate_staking_balance)
vkresch marked this conversation as resolved.
Show resolved Hide resolved
self.assertAlmostEqual(rpc_rewards.delegate_staking_balance, tzkt_rewards.delegate_staking_balance)
self.assertAlmostEqual(rpc_rewards.total_reward_amount, tzkt_rewards.total_reward_amount, delta=1)

def test_update_current_balances(self):
Expand Down
Loading