From e001e0c06e54194258939c228f78cdfd95470cca Mon Sep 17 00:00:00 2001 From: aitbc1 Date: Sun, 29 Mar 2026 17:56:02 +0200 Subject: [PATCH] feat: add get_pending_transactions method to mempool implementations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔧 Mempool Enhancement: • Add get_pending_transactions() to InMemoryMempool class • Add get_pending_transactions() to DatabaseMempool class • Sort transactions by fee (highest first) and received time • Support optional chain_id parameter with settings default • Limit results with configurable limit parameter (default 100) • Return transaction content only for RPC endpoint consumption --- .../src/aitbc_chain/mempool.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/apps/blockchain-node/src/aitbc_chain/mempool.py b/apps/blockchain-node/src/aitbc_chain/mempool.py index 334a546b..28e238a0 100755 --- a/apps/blockchain-node/src/aitbc_chain/mempool.py +++ b/apps/blockchain-node/src/aitbc_chain/mempool.py @@ -115,6 +115,22 @@ class InMemoryMempool: with self._lock: return len(self._transactions) + def get_pending_transactions(self, chain_id: str = None, limit: int = 100) -> List[Dict[str, Any]]: + """Get pending transactions for RPC endpoint""" + from .config import settings + if chain_id is None: + chain_id = settings.chain_id + + with self._lock: + # Get transactions sorted by fee (highest first) and time + sorted_txs = sorted( + self._transactions.values(), + key=lambda t: (-t.fee, t.received_at) + ) + + # Return only the content, limited by the limit parameter + return [tx.content for tx in sorted_txs[:limit]] + def _evict_lowest_fee(self) -> None: """Evict the lowest-fee transaction to make room.""" if not self._transactions: @@ -259,6 +275,20 @@ class DatabaseMempool: with self._lock: return self._conn.execute("SELECT COUNT(*) FROM mempool WHERE chain_id = ?", (chain_id,)).fetchone()[0] + def get_pending_transactions(self, chain_id: str = None, limit: int = 100) -> List[Dict[str, Any]]: + """Get pending transactions for RPC endpoint""" + from .config import settings + if chain_id is None: + chain_id = settings.chain_id + + with self._lock: + rows = self._conn.execute( + "SELECT content FROM mempool WHERE chain_id = ? ORDER BY fee DESC, received_at ASC LIMIT ?", + (chain_id, limit) + ).fetchall() + + return [json.loads(row[0]) for row in rows] + def _update_gauge(self, chain_id: str = None) -> None: from .config import settings if chain_id is None: