Skip to content

Commit

Permalink
fix: use threading locks while loading stuff at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler authored Dec 21, 2024
1 parent 1af19d4 commit 8d4b745
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions eth_portfolio/_db/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import threading
from asyncio import create_task, gather, get_event_loop
from contextlib import suppress
from functools import lru_cache
Expand Down Expand Up @@ -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]:
Expand All @@ -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)
Expand Down

0 comments on commit 8d4b745

Please sign in to comment.