-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmempool.py
More file actions
63 lines (51 loc) · 1.55 KB
/
mempool.py
File metadata and controls
63 lines (51 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Module for managing the transaction pool (mempool) with thread-safe operations.
"""
import threading
import logging
# Get logger
logger = logging.getLogger(__name__)
# Internal pool mapping (sender, nonce) -> transaction dict
t_pool: dict[tuple[str, int], dict] = {}
# Lock for synchronizing access to the pool
_pool_lock = threading.Lock()
def add_transaction(tx: dict) -> None:
"""
Add a validated transaction to the mempool.
Keyed by (sender, nonce).
"""
key = (tx['sender'], tx['nonce'])
with _pool_lock:
t_pool[key] = tx
logger.debug(f"Mempool: {tx} is added")
def remove_transactions(txs: list[dict]) -> None:
"""
Remove a list of transactions from the mempool.
"""
with _pool_lock:
for tx in txs:
key = (tx['sender'], tx['nonce'])
removed_tx = t_pool.pop(key, None)
if removed_tx:
logger.debug(f"Mempool: {removed_tx} was removed")
pass
def get_all_transactions() -> list[dict]:
"""
Retrieve all pending transactions from the mempool.
"""
with _pool_lock:
# Return a copy to prevent external mutation
return list(t_pool.values())
def clear_mempool() -> None:
"""
Remove all transactions from the mempool.
"""
with _pool_lock:
t_pool.clear()
def pool_snapshot() -> dict[tuple[str, int], dict]:
"""
Return a shallow copy of the internal pool mapping.
Useful for inspection or passing to validation functions.
"""
with _pool_lock:
return dict(t_pool)