from decimal import Decimal

import pytest

from common import AVERAGE_BLOCKS_COUNT_PER_DAY, share_to_hashrate, difficulty_to_hashrate
from reward import calculate_fpps

daily_stats = [
    (20823531150111.52, AVERAGE_BLOCKS_COUNT_PER_DAY, 153, 6.25, 146.92, 54262120448, 0.01878856),
    (20823531150111.52, AVERAGE_BLOCKS_COUNT_PER_DAY, 150, 6.25, 134.241, 131915972608, 0.04526282),
    (20823531150111.52, AVERAGE_BLOCKS_COUNT_PER_DAY, 159, 6.25, 166.88621843, 131827384320, 0.04621152),
    (20823531150111.52, AVERAGE_BLOCKS_COUNT_PER_DAY, 169, 6.25, 143.84104968, 146015387648 + 33910784, 0.04980496),
    (21349873786209.2, AVERAGE_BLOCKS_COUNT_PER_DAY, 159, 6.25, 118.30299223, 157934997504, 0.05173821),
    (21434395961348.92, AVERAGE_BLOCKS_COUNT_PER_DAY, 156, 6.25, 84.69085027, 173906698240, 0.05511371),
    (21434395961348.92, AVERAGE_BLOCKS_COUNT_PER_DAY, 162, 6.25, 134.17229408, 170159624192, 0.05619136),
    (21434395961348.92, AVERAGE_BLOCKS_COUNT_PER_DAY, 143, 6.25, 196.39438398, 170671919104, 0.06070141),
    (21434395961348.92, AVERAGE_BLOCKS_COUNT_PER_DAY, 143, 6.25, 168.16327939, 168169627648 + 2544932061184, 0.93995668),
    (21434395961348.92, AVERAGE_BLOCKS_COUNT_PER_DAY, 129, 6.25, 185.34915975, 166377984000 + 6673153871872, 2.45279678),
    (21434395961348.92, AVERAGE_BLOCKS_COUNT_PER_DAY, 142, 6.25, 202.39953166, 164935897088 + 6684052074496, 2.45252406),
    (21434395961348.92, AVERAGE_BLOCKS_COUNT_PER_DAY, 157, 6.25, 144.37461607, 160511279104 + 6673859739648, 2.28602635),
    (21434395961348.92, AVERAGE_BLOCKS_COUNT_PER_DAY, 148, 6.25, 104.20186702, 128912842752 + 12720912191488, 4.16893203),
    (21434395961348.92, AVERAGE_BLOCKS_COUNT_PER_DAY, 121, 6.25, 120.02466217, 154536284160 + 19335180537856, 6.58489969),
    (21434395961348.92, AVERAGE_BLOCKS_COUNT_PER_DAY, 121, 6.25, 120.02466217, 154536284160 + 19335180537856, 6.58489969),
    (21434395961348.92, AVERAGE_BLOCKS_COUNT_PER_DAY, 138, 6.25, 141.62327207, 172597325824 + 19545865674752, 6.69375414),
    (21434395961348.92, AVERAGE_BLOCKS_COUNT_PER_DAY, 137, 6.25, 147.75229392, 160268738560 + 20616356478976, 7.10359012),
    (21455226865438.402, AVERAGE_BLOCKS_COUNT_PER_DAY, 153, 6.25, 160.80575280, 155456135168 + 19682727641088, 6.75075298),
    (21724134900047.27, AVERAGE_BLOCKS_COUNT_PER_DAY, 146, 6.25, 147.82061349, 132320653312 + 24581014765568, 8.26177230),
    (21724134900047.27, AVERAGE_BLOCKS_COUNT_PER_DAY, 141, 6.25, 115.10198626, 142570143744 + 24444839677952, 7.99767897),
    (21724134900047.27, AVERAGE_BLOCKS_COUNT_PER_DAY, 133, 6.25, 131.78857835, 144470233088 + 24289250353152, 8.14402609),
    (21724134900047.27, AVERAGE_BLOCKS_COUNT_PER_DAY, 159, 6.25, 189.53161494, 98967334912 + 21950965149696, 7.55363100)
]


@pytest.mark.parametrize("difficulty,avg_blocks,total_blocks,reward_per_block,total_fees,shares,reward", daily_stats)
def test_calculate_fpps_daily(difficulty, avg_blocks, total_blocks, reward_per_block, total_fees, shares, reward):
    day_in_seconds = 24 * 60 * 60
    hashrate = share_to_hashrate(shares, day_in_seconds)
    network_hashrate = difficulty_to_hashrate(Decimal(difficulty))
    block_rewards = AVERAGE_BLOCKS_COUNT_PER_DAY * reward_per_block

    fpps_reward = calculate_fpps(Decimal(hashrate), network_hashrate, block_rewards, Decimal(total_fees), total_blocks,
                                 avg_blocks)
    assert round(fpps_reward, 8) == round(Decimal(reward), 8)
