From 89b96e8ac0c384e222de7517b819b5f022782e4d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 10 Apr 2023 09:46:56 +0930 Subject: [PATCH] pyln-testing: add support to tell bitcoind not to include txs if fee is too low. Signed-off-by: Rusty Russell --- contrib/pyln-testing/pyln/testing/utils.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/contrib/pyln-testing/pyln/testing/utils.py b/contrib/pyln-testing/pyln/testing/utils.py index 0c1d27a25..38c20605f 100644 --- a/contrib/pyln-testing/pyln/testing/utils.py +++ b/contrib/pyln-testing/pyln/testing/utils.py @@ -459,7 +459,7 @@ class BitcoinD(TailableProc): # int > 0 := wait for at least N transactions # 'tx_id' := wait for one transaction id given as a string # ['tx_id1', 'tx_id2'] := wait until all of the specified transaction IDs - def generate_block(self, numblocks=1, wait_for_mempool=0, to_addr=None): + def generate_block(self, numblocks=1, wait_for_mempool=0, to_addr=None, needfeerate=None): if wait_for_mempool: if isinstance(wait_for_mempool, str): wait_for_mempool = [wait_for_mempool] @@ -468,7 +468,7 @@ class BitcoinD(TailableProc): else: wait_for(lambda: len(self.rpc.getrawmempool()) >= wait_for_mempool) - mempool = self.rpc.getrawmempool() + mempool = self.rpc.getrawmempool(True) logging.debug("Generating {numblocks}, confirming {lenmempool} transactions: {mempool}".format( numblocks=numblocks, mempool=mempool, @@ -478,6 +478,21 @@ class BitcoinD(TailableProc): # As of 0.16, generate() is removed; use generatetoaddress. if to_addr is None: to_addr = self.rpc.getnewaddress() + + # We assume all-or-nothing. + if needfeerate is not None: + assert numblocks == 1 + # If any tx including ancestors is above the given feerate, mine all. + for txid, details in mempool.items(): + feerate = float(details['fees']['ancestor']) * 100_000_000 / (float(details['ancestorsize']) * 4 / 1000) + if feerate >= needfeerate: + return self.rpc.generatetoaddress(numblocks, to_addr) + else: + print(f"Feerate {feerate} for {txid} below {needfeerate}") + + # Otherwise, mine none. + return self.rpc.generateblock(to_addr, []) + return self.rpc.generatetoaddress(numblocks, to_addr) def simple_reorg(self, height, shift=0):