Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions minichain/mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ def __init__(self, max_size=1000, transactions_per_block=TRANSACTIONS_PER_BLOCK)
self.max_size = max_size
self.transactions_per_block = transactions_per_block

def _get_tx_id(self, tx):
return tx.tx_id

def add_transaction(self, tx):
"""
Adds a transaction to the pool if:
- Signature is valid
- Transaction is not a duplicate
- Mempool is not full
"""
tx_id = self._get_tx_id(tx)
tx_id = tx.tx_id

if not tx.verify():
logger.warning("Mempool: Invalid signature rejected")
Expand All @@ -47,7 +44,7 @@ def add_transaction(self, tx):

if replacement_index is not None:
old_tx = self._pending_txs[replacement_index]
self._seen_tx_ids.discard(self._get_tx_id(old_tx))
self._seen_tx_ids.discard(old_tx.tx_id)
self._pending_txs[replacement_index] = tx
else:
self._pending_txs.append(tx)
Expand All @@ -67,17 +64,17 @@ def get_transactions_for_block(self):

def remove_transactions(self, transactions):
with self._lock:
remove_ids = {self._get_tx_id(tx) for tx in transactions}
remove_ids = {tx.tx_id for tx in transactions}
remove_sender_nonces = {(tx.sender, tx.nonce) for tx in transactions}
if not remove_ids:
return
self._pending_txs = [
tx
for tx in self._pending_txs
if self._get_tx_id(tx) not in remove_ids
if tx.tx_id not in remove_ids
and (tx.sender, tx.nonce) not in remove_sender_nonces
]
self._seen_tx_ids = {self._get_tx_id(tx) for tx in self._pending_txs}
self._seen_tx_ids = {tx.tx_id for tx in self._pending_txs}

def __len__(self):
with self._lock:
Expand Down
Loading