From 8d4b745e9a1570c8c3dc9fcc8b0d053812cc6ab9 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sat, 21 Dec 2024 03:16:21 -0400 Subject: [PATCH] fix: use threading locks while loading stuff at startup --- eth_portfolio/_db/utils.py | 42 +++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/eth_portfolio/_db/utils.py b/eth_portfolio/_db/utils.py index c841b3c1..2e478457 100644 --- a/eth_portfolio/_db/utils.py +++ b/eth_portfolio/_db/utils.py @@ -1,3 +1,4 @@ +import threading from asyncio import create_task, gather, get_event_loop from contextlib import suppress from functools import lru_cache @@ -449,22 +450,24 @@ def get_token_transfer(transfer: evmspec.Log) -> Optional[TokenTransfer]: _TPK = Tuple[Tuple[int, ChecksumAddress], int] +_transactions_startup_lock = threading.Lock() @lru_cache(maxsize=None) def transactions_known_at_startup(chainid: int, from_address: ChecksumAddress) -> Dict[_TPK, bytes]: - transfers = {} - obj: Tuple[int, ChecksumAddress, int, bytes] - for nonce, raw in select( - (t.nonce, t.raw) - for t in entities.Transaction # type: ignore [attr-defined] - if t.from_address.chain.id == chainid and t.from_address.address == from_address - ): - transfers[nonce] = raw - return transfers + with _transactions_startup_lock: + transfers = {} + obj: Tuple[int, ChecksumAddress, int, bytes] + for nonce, raw in select( + (t.nonce, t.raw) + for t in entities.Transaction # type: ignore [attr-defined] + if t.from_address.chain.id == chainid and t.from_address.address == from_address + ): + transfers[nonce] = raw + return transfers _TokenTransferPK = Tuple[Tuple[int, int], int, int] - +_token_transfers_startup_lock = threading.Lock() @lru_cache(maxsize=None) def token_transfers_known_at_startup() -> Dict[_TokenTransferPK, bytes]: @@ -474,15 +477,16 @@ def token_transfers_known_at_startup() -> Dict[_TokenTransferPK, bytes]: log_index: int raw: bytes - transfers = {} - for chainid, block, tx_index, log_index, raw in select( - (t.block.chain.id, t.block.number, t.transaction_index, t.log_index, t.raw) - for t in entities.TokenTransfer # type: ignore [attr-defined] - if t.block.chain.id == chain.id - ): - pk = ((chainid, block), tx_index, log_index) - transfers[pk] = raw - return transfers + with _token_transfers_startup_lock: + transfers = {} + for chainid, block, tx_index, log_index, raw in select( + (t.block.chain.id, t.block.number, t.transaction_index, t.log_index, t.raw) + for t in entities.TokenTransfer # type: ignore [attr-defined] + if t.block.chain.id == chain.id + ): + pk = ((chainid, block), tx_index, log_index) + transfers[pk] = raw + return transfers @a_sync(default="async", executor=_token_transfer_write_executor)