Skip to content

Commit

Permalink
🚀 Add transaction_global for the RPC session
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed May 13, 2024
1 parent 542f0d6 commit 738d415
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
26 changes: 1 addition & 25 deletions electrumx/server/http_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
35 changes: 34 additions & 1 deletion electrumx/server/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 738d415

Please sign in to comment.