1
0
Fork 0

Add Flexpool tests for block notifications

Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
Julien Riou 2021-02-17 10:13:59 +01:00
parent 9d75604869
commit c20ed21c2d
No known key found for this signature in database
GPG key ID: FF42D23B580C89F7
2 changed files with 48 additions and 7 deletions

View file

@ -131,7 +131,9 @@ class FlexpoolHandler(Handler):
blocks = self.get_blocks(exchange_rate=self.exchange_rate, currency=self.currency)
if blocks:
# don't spam block notification at initialization
for block in blocks[MAX_NOTIFICATIONS_COUNT:]:
notification_slice = MAX_NOTIFICATIONS_COUNT if len(blocks) > MAX_NOTIFICATIONS_COUNT else 0
for block in blocks[notification_slice:]:
print(block)
if not last_block or last_block < block.number:
logger.info(f'new block {block.number}')
if self.notifier:
@ -145,7 +147,7 @@ class FlexpoolHandler(Handler):
except Exception as err:
logger.error('failed to send notification')
logger.exception(err)
last_remote_block = block
last_remote_block = block
if last_remote_block and last_remote_block.number:
return last_remote_block.number
@ -154,11 +156,12 @@ class FlexpoolHandler(Handler):
remote_blocks = flexpoolapi.pool.last_blocks(count=MAX_BLOCKS_COUNT)
# convert to blocks
blocks = []
for remote_block in remote_blocks:
block = Block(number=remote_block.number, hash=remote_block.hash, time=remote_block.time,
round_time=remote_block.round_time, reward=remote_block.total_rewards, luck=remote_block.luck,
exchange_rate=exchange_rate, currency=currency)
blocks.append(block)
if remote_blocks:
for remote_block in remote_blocks:
block = Block(number=remote_block.number, hash=remote_block.hash, time=remote_block.time,
round_time=remote_block.round_time, reward=remote_block.total_rewards,
luck=remote_block.luck, exchange_rate=exchange_rate, currency=currency)
blocks.append(block)
# sort by block number
return sorted(blocks)

View file

@ -2,6 +2,7 @@ from datetime import datetime, timedelta
import pytest
from companion.pools.flexpool import FlexpoolHandler, Transaction
from flexpoolapi.shared import Block as BlockApi
class TestFlexpoolHandler:
@ -64,3 +65,40 @@ class TestFlexpoolHandler:
notifier.notify_payment.assert_called_once()
else:
notifier.notify_payment.assert_not_called()
@staticmethod
def _create_blocks(numbers):
if numbers:
blocks = []
for number in numbers:
blocks.append(BlockApi(number=number, blockhash='h', block_type='bt', miner='m', difficulty=1,
timestamp=1, is_confirmed=True, round_time=1, luck=1.0, server_name='s',
block_reward=1, block_fees=1, uncle_inclusion_rewards=1, total_rewards=1))
return blocks
@pytest.mark.parametrize(
'last_block,remote_blocks,should_notify',
[
pytest.param(1, [1, 2], True, id='new_block_with_notification'),
pytest.param(None, [1], True, id='very_new_block_with_notification'),
pytest.param(1, [1], False, id='same_block_without_notification'),
pytest.param(9, range(1, 11), True, id='new_block_with_count_over_max_notification'),
pytest.param(10, range(1, 11), False, id='same_block_with_count_over_max_notification'),
pytest.param(None, None, False, id='zero_block_without_notification'),
]
)
def test_block(self, mocker, last_block, remote_blocks, should_notify):
notifier = mocker.Mock()
notifier.notify_block = mocker.Mock()
handler = FlexpoolHandler(notifier=notifier)
last_blocks = mocker.patch('flexpoolapi.pool.last_blocks')
last_blocks.return_value = self._create_blocks(remote_blocks)
block = handler.watch_blocks(last_block=last_block)
if remote_blocks:
assert block == remote_blocks[-1]
else:
assert block is None
if should_notify:
notifier.notify_block.assert_called_once()
else:
notifier.notify_block.assert_not_called()