Skip to content

Commit

Permalink
Add test coverage for extract_timestamps_json_rpc_lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Jul 18, 2023
1 parent 31610a5 commit d08ae5b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
7 changes: 6 additions & 1 deletion eth_defi/event_reader/lazy_timestamp_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def __init__(self, web3: Web3, start_block: int, end_block: int):
self.cache_by_block_hash = {}
self.cache_by_block_number = {}

#: How many API requets we have made
self.api_call_counter = 0

def update_block_hash(self, block_identifier: BlockIdentifier) -> int:
"""Internal function to get block timestamp from JSON-RPC and store it in the cache."""
# Skip web3.py stack of slow result formatters
Expand All @@ -56,7 +59,7 @@ def update_block_hash(self, block_identifier: BlockIdentifier) -> int:

if type(block_identifier) == int:
assert block_identifier > 0
result = self.web3.manager.request_blocking("eth_getBlockByNumber", (block_identifier, False))
result = self.web3.manager.request_blocking("eth_getBlockByNumber", (hex(block_identifier), False))
else:
if isinstance(block_identifier, HexBytes):
block_identifier = block_identifier.hex()
Expand All @@ -67,6 +70,8 @@ def update_block_hash(self, block_identifier: BlockIdentifier) -> int:

result = self.web3.manager.request_blocking("eth_getBlockByHash", (block_identifier, False))

self.api_call_counter += 1

# Note to self: block_number = 0 for the genesis block on Anvil
block_number = convert_jsonrpc_value_to_int(result["number"])
hash = result["hash"]
Expand Down
42 changes: 42 additions & 0 deletions tests/test_event_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from eth_defi.abi import get_contract
from eth_defi.chain import install_chain_middleware
from eth_defi.event_reader.lazy_timestamp_reader import extract_timestamps_json_rpc_lazy, LazyTimestampContainer
from eth_defi.event_reader.reader import read_events, BadTimestampValueReturned, TimestampNotFound, read_events_concurrent
from eth_defi.event_reader.web3factory import TunedWeb3Factory
from eth_defi.event_reader.web3worker import create_thread_pool_executor
Expand Down Expand Up @@ -154,3 +155,44 @@ def test_read_events_concurrent_two_blocks_concurrent(web3):
assert len(blocks) == 3
assert min(blocks) == 37898275
assert max(blocks) == 37898276



def test_read_events_lazy_timestamp(web3):
"""Read events but extract timestamps only for events, not whole block ranges.
"""

# Get contracts
Pair = get_contract(web3, "sushi/UniswapV2Pair.json")

events = [
Pair.events.Swap,
]

start_block = 37898275
end_block = start_block + 100
lazy_timestamp_container: LazyTimestampContainer = None

def wrapper(web3, start_block, end_block):
nonlocal lazy_timestamp_container
lazy_timestamp_container = extract_timestamps_json_rpc_lazy(web3, start_block, end_block)
return lazy_timestamp_container

swaps = list(
read_events(
web3,
start_block,
end_block,
events,
chunk_size=1000,
extract_timestamps=wrapper,
)
)

# API calls are less often than blocks we read
assert lazy_timestamp_container.api_call_counter == 80
assert len(swaps) == 206

for s in swaps:
assert s["timestamp"] > 0

0 comments on commit d08ae5b

Please sign in to comment.