Skip to content

Commit

Permalink
dont fetch current block
Browse files Browse the repository at this point in the history
  • Loading branch information
ape364 committed Jun 8, 2024
1 parent b5e8732 commit 3d28ba5
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 127 deletions.
4 changes: 3 additions & 1 deletion aioetherscan/modules/extra/generators/blocks_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def restore(self) -> None:


class BlocksRange:
def __init__(self, start_block: int, end_block: int, blocks_limit: int, blocks_limit_divider: int) -> None:
def __init__(
self, start_block: int, end_block: int, blocks_limit: int, blocks_limit_divider: int
) -> None:
self.start_block = start_block
self.end_block = end_block

Expand Down
38 changes: 17 additions & 21 deletions aioetherscan/modules/extra/generators/generator_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import sys
from itertools import count
from typing import Callable, Any, Optional, TYPE_CHECKING, AsyncIterator

Expand All @@ -10,9 +11,10 @@


class GeneratorUtils:
DEFAULT_START_BLOCK: int = 0
DEFAULT_BLOCKS_LIMIT: int = 2048
DEFAULT_BLOCKS_LIMIT_DIVIDER: int = 2
_DEFAULT_START_BLOCK: int = 0
_DEFAULT_END_BLOCK: int = sys.maxsize
_DEFAULT_BLOCKS_LIMIT: int = 2048
_DEFAULT_BLOCKS_LIMIT_DIVIDER: int = 2

def __init__(self, client: 'Client') -> None:
self._client = client
Expand All @@ -21,10 +23,10 @@ async def token_transfers(
self,
contract_address: str = None,
address: str = None,
start_block: int = DEFAULT_START_BLOCK,
end_block: Optional[int] = None,
blocks_limit: int = DEFAULT_BLOCKS_LIMIT,
blocks_limit_divider: int = DEFAULT_BLOCKS_LIMIT_DIVIDER,
start_block: int = _DEFAULT_START_BLOCK,
end_block: int = _DEFAULT_END_BLOCK,
blocks_limit: int = _DEFAULT_BLOCKS_LIMIT,
blocks_limit_divider: int = _DEFAULT_BLOCKS_LIMIT_DIVIDER,
) -> AsyncIterator[Transfer]:
parser_params = self._get_parser_params(self._client.account.token_transfers, locals())
async for transfer in self._parse_by_blocks(**parser_params):
Expand All @@ -33,10 +35,10 @@ async def token_transfers(
async def normal_txs(
self,
address: str,
start_block: int = DEFAULT_START_BLOCK,
end_block: Optional[int] = None,
blocks_limit: int = DEFAULT_BLOCKS_LIMIT,
blocks_limit_divider: int = DEFAULT_BLOCKS_LIMIT_DIVIDER,
start_block: int = _DEFAULT_START_BLOCK,
end_block: int = _DEFAULT_END_BLOCK,
blocks_limit: int = _DEFAULT_BLOCKS_LIMIT,
blocks_limit_divider: int = _DEFAULT_BLOCKS_LIMIT_DIVIDER,
) -> AsyncIterator[Transfer]:
parser_params = self._get_parser_params(self._client.account.normal_txs, locals())
async for transfer in self._parse_by_blocks(**parser_params):
Expand All @@ -45,10 +47,10 @@ async def normal_txs(
async def internal_txs(
self,
address: str,
start_block: int = DEFAULT_START_BLOCK,
end_block: Optional[int] = None,
blocks_limit: int = DEFAULT_BLOCKS_LIMIT,
blocks_limit_divider: int = DEFAULT_BLOCKS_LIMIT_DIVIDER,
start_block: int = _DEFAULT_START_BLOCK,
end_block: int = _DEFAULT_END_BLOCK,
blocks_limit: int = _DEFAULT_BLOCKS_LIMIT,
blocks_limit_divider: int = _DEFAULT_BLOCKS_LIMIT_DIVIDER,
txhash: Optional[str] = None,
) -> AsyncIterator[Transfer]:
parser_params = self._get_parser_params(self._client.account.internal_txs, locals())
Expand All @@ -71,9 +73,6 @@ async def _parse_by_blocks(
blocks_limit: int,
blocks_limit_divider: int,
) -> AsyncIterator[Transfer]:
if end_block is None:
end_block = await self._get_current_block()

blocks_parser = self._get_blocks_parser(
api_method, request_params, start_block, end_block, blocks_limit, blocks_limit_divider
)
Expand All @@ -97,9 +96,6 @@ async def _parse_by_pages(
for row in result:
yield row

async def _get_current_block(self) -> int:
return int(await self._client.proxy.block_number(), 16)

@staticmethod
def _without_keys(params: dict, excluded_keys: tuple[str, ...] = ('self',)) -> dict:
return {k: v for k, v in params.items() if k not in excluded_keys}
Expand Down
38 changes: 10 additions & 28 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ aiosignal==1.3.1 ; python_version >= "3.9" and python_version < "4.0"
async-timeout==4.0.3 ; python_version >= "3.9" and python_version < "3.11"
asyncio-throttle==1.0.2 ; python_version >= "3.9" and python_version < "4.0"
attrs==23.2.0 ; python_version >= "3.9" and python_version < "4.0"
certifi==2024.2.2 ; python_version >= "3.9" and python_version < "4.0"
certifi==2024.6.2 ; python_version >= "3.9" and python_version < "4.0"
cfgv==3.4.0 ; python_version >= "3.9" and python_version < "4.0"
charset-normalizer==3.3.2 ; python_version >= "3.9" and python_version < "4.0"
colorama==0.4.6 ; python_version >= "3.9" and python_version < "4.0" and sys_platform == "win32"
Expand All @@ -19,16 +19,15 @@ identify==2.5.36 ; python_version >= "3.9" and python_version < "4.0"
idna==3.7 ; python_version >= "3.9" and python_version < "4.0"
iniconfig==2.0.0 ; python_version >= "3.9" and python_version < "4.0"
multidict==6.0.5 ; python_version >= "3.9" and python_version < "4.0"
nodeenv==1.8.0 ; python_version >= "3.9" and python_version < "4.0"
nodeenv==1.9.1 ; python_version >= "3.9" and python_version < "4.0"
packaging==24.0 ; python_version >= "3.9" and python_version < "4.0"
platformdirs==4.2.2 ; python_version >= "3.9" and python_version < "4.0"
pluggy==1.5.0 ; python_version >= "3.9" and python_version < "4.0"
pre-commit==3.7.1 ; python_version >= "3.9" and python_version < "4.0"
pytest-asyncio==0.21.2 ; python_version >= "3.9" and python_version < "4.0"
pytest==7.4.4 ; python_version >= "3.9" and python_version < "4.0"
pyyaml==6.0.1 ; python_version >= "3.9" and python_version < "4.0"
requests==2.32.2 ; python_version >= "3.9" and python_version < "4.0"
setuptools==70.0.0 ; python_version >= "3.9" and python_version < "4.0"
requests==2.32.3 ; python_version >= "3.9" and python_version < "4.0"
tomli==2.0.1 ; python_version >= "3.9" and python_version < "3.11"
urllib3==2.2.1 ; python_version >= "3.9" and python_version < "4.0"
virtualenv==20.26.2 ; python_version >= "3.9" and python_version < "4.0"
Expand Down
3 changes: 2 additions & 1 deletion tests/extra/generators/test_blocks_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from aioetherscan.modules.extra.generators.blocks_range import Limit, BlocksRange

INITIAL_LIMIT = 2 ** 4
INITIAL_LIMIT = 2**4
BLOCKS_RANGE_DIVIDER = 2

START_BLOCK = 1000
Expand Down Expand Up @@ -65,6 +65,7 @@ def test_limit_get(limit: Limit):

# ############################### blocks range ################################


def test_br_init(br: BlocksRange):
assert br.start_block == START_BLOCK
assert br.end_block == END_BLOCK
Expand Down
37 changes: 2 additions & 35 deletions tests/extra/generators/test_generator_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,26 +177,19 @@ async def test_parse_by_blocks_end_block_is_none(generator_utils):
blocks_parser_mock.return_value.txs_generator = MagicMock(side_effect=transfers_mock)
generator_utils._get_blocks_parser = blocks_parser_mock

current_block = 200
get_current_block_mock = AsyncMock(return_value=current_block)
generator_utils._get_current_block = get_current_block_mock

transfers = []
async for transfer in generator_utils._parse_by_blocks(
api_method=None,
request_params={'param': 'value'},
start_block=100,
end_block=None,
end_block=200,
blocks_limit=1000,
blocks_limit_divider=2,
):
transfers.append(transfer)
assert transfers == transfers_for_test()

get_current_block_mock.assert_awaited_once()
blocks_parser_mock.assert_called_once_with(
None, {'param': 'value'}, 100, current_block, 1000, 2
)
blocks_parser_mock.assert_called_once_with(None, {'param': 'value'}, 100, 200, 1000, 2)


async def test_parse_by_pages_ok(generator_utils):
Expand Down Expand Up @@ -224,32 +217,6 @@ async def test_parse_by_pages_error(generator_utils):
assert e.value.args[0] == 'test error'


async def test_get_current_block(generator_utils):
generator_utils._client = Mock()
generator_utils._client.proxy.block_number = AsyncMock(return_value='0x2d0')

result = await generator_utils._get_current_block()

generator_utils._client.proxy.block_number.assert_awaited_once()
assert isinstance(result, int)
assert result == int('0x2d0', 16)


async def test_get_current_block_error(generator_utils):
generator_utils._client = Mock()
generator_utils._client.proxy.block_number = AsyncMock(
side_effect=EtherscanClientApiError('message', 'code')
)

with pytest.raises(EtherscanClientApiError) as e:
await generator_utils._get_current_block()

generator_utils._client.proxy.block_number.assert_awaited_once()

assert e.value.message == 'message'
assert e.value.result == 'code'


@pytest.mark.parametrize(
'params, excluded, expected',
[
Expand Down
8 changes: 6 additions & 2 deletions tests/extra/generators/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from aioetherscan.modules.extra.generators.helpers import tx_block_number, drop_block, get_max_block_number
from aioetherscan.modules.extra.generators.helpers import (
tx_block_number,
drop_block,
get_max_block_number,
)


def test_tx_block_number():
Expand Down Expand Up @@ -28,7 +32,7 @@ def test_drop_block():


def test_get_max_block_number():
max_block_number = 2 ** 16
max_block_number = 2**16
transfers = [
dict(blockNumber=123),
dict(blockNumber=max_block_number),
Expand Down
Loading

0 comments on commit 3d28ba5

Please sign in to comment.