Skip to content

Commit 105ad84

Browse files
committed
add blockchain.mempoolminfee, update relayfee comments
Adds the `blockchain.mempoolminfee` rpc to fetch the minimum feerate required to get into the daemons mempool. Updates the relayfee comments as the fee returned by `blockchain.relayfee` doesn't guarantee to get into the mempool.
1 parent 0b260d4 commit 105ad84

4 files changed

Lines changed: 39 additions & 6 deletions

File tree

docs/rpc-interface.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ A typical result is as follows (with annotated comments)::
9090
"blockchain.estimatefee": 12776,
9191
"blockchain.headers.subscribe": 2825,
9292
"blockchain.relayfee": 740,
93+
"blockchain.mempoolminfee": 121,
9394
"blockchain.scripthash.get_history": 196,
9495
"blockchain.scripthash.subscribe": 184626,
9596
"blockchain.transaction.broadcast": 19,

src/electrumx/server/daemon.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def __init__(
7979
self._networkinfo_cache = (None, 0)
8080
self._networkinfo_lock = asyncio.Lock()
8181

82+
self._mempoolinfo_cache = (None, 0)
83+
self._mempoolinfo_lock = asyncio.Lock()
84+
8285
async def __aenter__(self):
8386
self.session = aiohttp.ClientSession(connector=self.connector())
8487
return self
@@ -284,12 +287,27 @@ async def getnetworkinfo(self):
284287
self._networkinfo_cache = (val, time.time())
285288
return val
286289

290+
async def getmempoolinfo(self):
291+
"""Return the result of the 'getmempoolinfo' RPC call."""
292+
async with self._mempoolinfo_lock:
293+
cache_val, cache_time = self._mempoolinfo_cache
294+
if time.time() - cache_time < 60: # seconds
295+
return cache_val
296+
val = await self._send_single('getmempoolinfo')
297+
self._mempoolinfo_cache = (val, time.time())
298+
return val
299+
287300
async def relayfee(self):
288-
'''The minimum fee a low-priority tx must pay in order to be accepted
289-
to the daemon's memory pool.'''
301+
"""The minimum feerate required for a transaction to be relayed on by the backend to the
302+
bitcoin network. Doesn't guarantee mempool acceptance."""
290303
network_info = await self.getnetworkinfo()
291304
return network_info['relayfee']
292305

306+
async def mempoolminfee(self):
307+
"""The minimum feerate required for a transaction to get accepted into the backends mempool."""
308+
mempool_info = await self.getmempoolinfo()
309+
return mempool_info['mempoolminfee']
310+
293311
async def getrawtransaction(self, hex_hash, verbose=False):
294312
'''Return the serialized raw transaction with the given hash.'''
295313
# Cast to int because some coin daemons are old and require it
@@ -350,8 +368,8 @@ async def estimatefee(self, block_count):
350368
return self.coin.ESTIMATE_FEE
351369

352370
async def relayfee(self):
353-
'''The minimum fee a low-priority tx must pay in order to be accepted
354-
to the daemon's memory pool.'''
371+
"""The minimum fee required for a transaction to be relayed on by the server to the
372+
bitcoin network. Doesn't guarantee mempool acceptance."""
355373
return self.coin.RELAY_FEE
356374

357375

src/electrumx/server/session.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,11 +1387,16 @@ async def banner(self):
13871387
return banner
13881388

13891389
async def relayfee(self):
1390-
'''The minimum fee a low-priority tx must pay in order to be accepted
1391-
to the daemon's memory pool.'''
1390+
"""The minimum fee required for a transaction to be relayed on by the server to the
1391+
bitcoin network. Doesn't guarantee mempool acceptance."""
13921392
self.bump_cost(1.0)
13931393
return await self.daemon_request('relayfee')
13941394

1395+
async def mempoolminfee(self):
1396+
"""The minimum feerate required for a transaction to get accepted into the servers mempool."""
1397+
self.bump_cost(1.0)
1398+
return await self.daemon_request('mempoolminfee')
1399+
13951400
async def estimatefee(self, number, mode=None):
13961401
'''The estimated transaction fee per kilobyte to be paid for a
13971402
transaction to be included within a certain number of blocks.
@@ -1628,6 +1633,7 @@ def set_request_handlers(self, ptuple):
16281633
'blockchain.estimatefee': self.estimatefee,
16291634
'blockchain.headers.subscribe': self.headers_subscribe,
16301635
'blockchain.relayfee': self.relayfee,
1636+
'blockchain.mempoolminfee': self.mempoolminfee,
16311637
'blockchain.scripthash.get_balance': self.scripthash_get_balance,
16321638
'blockchain.scripthash.get_history': self.scripthash_get_history,
16331639
'blockchain.scripthash.get_mempool': self.scripthash_get_mempool,

tests/server/test_daemon.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ async def test_relayfee(daemon):
255255
assert await daemon.relayfee() == sats
256256

257257

258+
@pytest.mark.asyncio
259+
async def test_mempoolminfee(daemon):
260+
bitcoin_per_kvb = 0.00002123
261+
response = {"mempoolminfee": bitcoin_per_kvb, "other:": "cruft"}
262+
daemon.session = ClientSessionGood(('getmempoolinfo', [], response))
263+
assert await daemon.mempoolminfee() == bitcoin_per_kvb
264+
265+
258266
@pytest.mark.asyncio
259267
async def test_mempool_hashes(daemon):
260268
hashes = ['hex_hash1', 'hex_hash2']

0 commit comments

Comments
 (0)