From 738d4151cbe35d087c73cc30f172136c25c6a391 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Mon, 13 May 2024 17:48:23 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Add=20`transaction=5Fglobal`=20f?= =?UTF-8?q?or=20the=20RPC=20session?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electrumx/server/http_session.py | 26 +----------------------- electrumx/server/session.py | 35 +++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/electrumx/server/http_session.py b/electrumx/server/http_session.py index 106abcb0..82a5f56f 100644 --- a/electrumx/server/http_session.py +++ b/electrumx/server/http_session.py @@ -1878,28 +1878,4 @@ async def transaction_global(self, request): offset = params.get(1, 0) op_type = params.get(2, None) reverse = params.get(3, True) - height = self.session_mgr.bp.height - - res = [] - count = 0 - history_list = [] - for current_height in range(height, self.coin.ATOMICALS_ACTIVATION_HEIGHT, -1): - txs = self.db.get_atomicals_block_txs(current_height) - for tx in txs: - tx_num, _ = self.db.get_tx_num_height_from_tx_hash(hex_str_to_hash(tx)) - history_list.append({ - "tx_num": tx_num, - "tx_hash": tx, - "height": current_height - }) - count += 1 - if count >= offset + limit: - break - history_list.sort(key=lambda x: x['tx_num'], reverse=reverse) - - for history in history_list: - data = await self.session_mgr.get_transaction_detail(history["tx_hash"], history["height"], history["tx_num"]) - if (op_type and op_type == data["op"]) or (not op_type and data["op"]): - res.append(data) - total = len(res) - return {"result": res[offset:offset+limit], "total": total, "limit": limit, "offset": offset} + return await self.session_mgr.transaction_global(limit, offset, op_type, reverse) diff --git a/electrumx/server/session.py b/electrumx/server/session.py index 6de797fe..47c4b05a 100644 --- a/electrumx/server/session.py +++ b/electrumx/server/session.py @@ -1033,7 +1033,7 @@ async def get_history_op(self, hashX, limit=10, offset=0, op=None, reverse=True) # Analysis the transaction detail by txid. # See BlockProcessor.op_list for the complete op list. - async def get_transaction_detail(self, txid, height=None, tx_num=-1): + async def get_transaction_detail(self, txid: str, height=None, tx_num=-1): tx_hash = hex_str_to_hash(txid) res = self._tx_detail_cache.get(tx_hash) if res: @@ -1229,6 +1229,38 @@ async def get_transaction_detail(self, txid, height=None, tx_num=-1): # Recursively encode the result. return auto_encode_bytes_elements(res) + async def transaction_global( + self, + limit: int = 10, + offset: int = 0, + op_type: Optional[str] = None, + reverse: bool = True + ): + height = self.bp.height + res = [] + count = 0 + history_list = [] + for current_height in range(height, self.env.coin.ATOMICALS_ACTIVATION_HEIGHT, -1): + txs = self.db.get_atomicals_block_txs(current_height) + for tx in txs: + tx_num, _ = self.db.get_tx_num_height_from_tx_hash(hex_str_to_hash(tx)) + history_list.append({ + "tx_num": tx_num, + "tx_hash": tx, + "height": current_height + }) + count += 1 + if count >= offset + limit: + break + history_list.sort(key=lambda x: x['tx_num'], reverse=reverse) + + for history in history_list: + data = await self.get_transaction_detail(history["tx_hash"], history["height"], history["tx_num"]) + if (op_type and op_type == data["op"]) or (not op_type and data["op"]): + res.append(data) + total = len(res) + return {"result": res[offset:offset+limit], "total": total, "limit": limit, "offset": offset} + async def _notify_sessions(self, height, touched): '''Notify sessions about height changes and touched addresses.''' height_changed = height != self.notified_height @@ -3110,6 +3142,7 @@ def set_request_handlers(self, ptuple): 'blockchain.atomicals.find_containers': self.atomicals_search_containers, 'blockchain.atomicals.get_holders': self.atomicals_get_holders, 'blockchain.atomicals.transaction': self.atomicals_transaction, + 'blockchain.atomicals.transaction_global': self.session_mgr.transaction_global, 'blockchain.atomicals.transaction_by_height': self.transaction_by_height, 'blockchain.atomicals.transaction_by_atomical_id': self.transaction_by_atomical_id, 'blockchain.atomicals.transaction_by_scripthash': self.transaction_by_scripthash,