Skip to content

Commit 1303e45

Browse files
committed
session: add "bc.tx.testmempoolaccept" RPC
1 parent 5e66f5a commit 1303e45

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

src/electrumx/server/daemon.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,12 @@ async def broadcast_package(self, raw_txs: Sequence[str]):
361361
"""Broadcast a package of transactions to the network using 'submitpackage'."""
362362
return await self._send_single('submitpackage', (raw_txs, ))
363363

364+
async def testmempoolaccept(self, raw_txs: Sequence[str]):
365+
"""Query the daemon to test mempool acceptance of txs,
366+
without adding them to the mempool or broadcasting them.
367+
"""
368+
return await self._send_single('testmempoolaccept', (raw_txs, ))
369+
364370
async def height(self):
365371
'''Query the daemon for its current height.'''
366372
self._height = await self._send_single('getblockcount')

src/electrumx/server/session.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,37 @@ async def package_broadcast(self, tx_package: Sequence[str], verbose: bool = Fal
18881888
response['errors'] = errors
18891889
return response
18901890

1891+
async def transaction_testmempoolaccept(self, raw_txs: Sequence[str]) -> Sequence[dict]:
1892+
"""Returns result of mempool acceptance tests indicating if txs would be accepted by mempool.
1893+
1894+
raw_txs: a list of raw transactions as hexadecimal strings
1895+
"""
1896+
assert_list_or_tuple(raw_txs)
1897+
for raw_tx in raw_txs:
1898+
assert_hex_str(raw_tx)
1899+
self.bump_cost(0.25 + sum(len(tx) / 5000 for tx in raw_txs))
1900+
daemon_result = await self.daemon_request("testmempoolaccept", raw_txs)
1901+
1902+
response: list[dict] = []
1903+
for orig_item in daemon_result: # one item for each tx
1904+
new_item = {
1905+
"txid": orig_item["txid"],
1906+
"wtxid": orig_item["wtxid"],
1907+
}
1908+
# optional: "allowed" field
1909+
if orig_item.get("allowed") in (True, False):
1910+
new_item["allowed"] = orig_item["allowed"]
1911+
# optional: "reason" field
1912+
reason_str = (
1913+
orig_item.get("package-error")
1914+
or orig_item.get("reject-details")
1915+
or orig_item.get("reject-reason")
1916+
or None)
1917+
if reason_str is not None:
1918+
new_item["reason"] = reason_str
1919+
response.append(new_item)
1920+
return response
1921+
18911922
async def transaction_get(self, tx_hash: str, verbose=False):
18921923
'''Return the serialized raw transaction given its hash
18931924
@@ -2048,6 +2079,7 @@ def set_request_handlers(self, ptuple):
20482079

20492080
# experimental:
20502081
if ptuple >= (1, 7):
2082+
handlers['blockchain.transaction.testmempoolaccept'] = self.transaction_testmempoolaccept
20512083
handlers['blockchain.outpoint.subscribe'] = self.txoutpoint_subscribe
20522084
handlers['blockchain.outpoint.get_status'] = self.txoutpoint_get_status
20532085
handlers['blockchain.outpoint.unsubscribe'] = self.txoutpoint_unsubscribe

0 commit comments

Comments
 (0)