Skip to content

Commit 544cbf7

Browse files
committed
tests: Use batched RPC in feature_fee_estimation
feature_fee_estimation has a lot of loops that hit the RPC many times in succession in order to setup scenarios. Using batched requests for these can reduce the test's runtime without effecting the test's behavior.
1 parent 4ad7272 commit 544cbf7

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

test/functional/feature_fee_estimation.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
def small_txpuzzle_randfee(
26-
wallet, from_node, conflist, unconflist, amount, min_fee, fee_increment
26+
wallet, from_node, conflist, unconflist, amount, min_fee, fee_increment, batch_reqs
2727
):
2828
"""Create and send a transaction with a random fee using MiniWallet.
2929
@@ -57,8 +57,11 @@ def small_txpuzzle_randfee(
5757
tx.vout[0].nValue = int((total_in - amount - fee) * COIN)
5858
tx.vout.append(deepcopy(tx.vout[0]))
5959
tx.vout[1].nValue = int(amount * COIN)
60+
tx.rehash()
61+
txid = tx.hash
62+
tx_hex = tx.serialize().hex()
6063

61-
txid = from_node.sendrawtransaction(hexstring=tx.serialize().hex(), maxfeerate=0)
64+
batch_reqs.append(from_node.sendrawtransaction.get_request(hexstring=tx_hex, maxfeerate=0))
6265
unconflist.append({"txid": txid, "vout": 0, "value": total_in - amount - fee})
6366
unconflist.append({"txid": txid, "vout": 1, "value": amount})
6467

@@ -115,13 +118,12 @@ def check_estimates(node, fees_seen):
115118
check_smart_estimates(node, fees_seen)
116119

117120

118-
def send_tx(wallet, node, utxo, feerate):
119-
"""Broadcast a 1in-1out transaction with a specific input and feerate (sat/vb)."""
120-
return wallet.send_self_transfer(
121-
from_node=node,
121+
def make_tx(wallet, utxo, feerate):
122+
"""Create a 1in-1out transaction with a specific input and feerate (sat/vb)."""
123+
return wallet.create_self_transfer(
122124
utxo_to_spend=utxo,
123125
fee_rate=Decimal(feerate * 1000) / COIN,
124-
)['txid']
126+
)
125127

126128

127129
class EstimateFeeTest(BitcoinTestFramework):
@@ -156,6 +158,7 @@ def transact_and_mine(self, numblocks, mining_node):
156158
# resorting to tx's that depend on the mempool when those run out
157159
for _ in range(numblocks):
158160
random.shuffle(self.confutxo)
161+
batch_sendtx_reqs = []
159162
for _ in range(random.randrange(100 - 50, 100 + 50)):
160163
from_index = random.randint(1, 2)
161164
(tx_bytes, fee) = small_txpuzzle_randfee(
@@ -166,9 +169,12 @@ def transact_and_mine(self, numblocks, mining_node):
166169
Decimal("0.005"),
167170
min_fee,
168171
min_fee,
172+
batch_sendtx_reqs,
169173
)
170174
tx_kbytes = tx_bytes / 1000.0
171175
self.fees_per_kb.append(float(fee) / tx_kbytes)
176+
for node in self.nodes:
177+
node.batch(batch_sendtx_reqs)
172178
self.sync_mempools(wait=0.1)
173179
mined = mining_node.getblock(self.generate(mining_node, 1)[0], True)["tx"]
174180
# update which txouts are confirmed
@@ -245,14 +251,20 @@ def sanity_check_rbf_estimates(self, utxos):
245251
assert_greater_than_or_equal(len(utxos), 250)
246252
for _ in range(5):
247253
# Broadcast 45 low fee transactions that will need to be RBF'd
254+
txs = []
248255
for _ in range(45):
249256
u = utxos.pop(0)
250-
txid = send_tx(self.wallet, node, u, low_feerate)
257+
tx = make_tx(self.wallet, u, low_feerate)
251258
utxos_to_respend.append(u)
252-
txids_to_replace.append(txid)
259+
txids_to_replace.append(tx["txid"])
260+
txs.append(tx)
253261
# Broadcast 5 low fee transaction which don't need to
254262
for _ in range(5):
255-
send_tx(self.wallet, node, utxos.pop(0), low_feerate)
263+
tx = make_tx(self.wallet, utxos.pop(0), low_feerate)
264+
txs.append(tx)
265+
batch_send_tx = [node.sendrawtransaction.get_request(tx["hex"]) for tx in txs]
266+
for n in self.nodes:
267+
n.batch(batch_send_tx)
256268
# Mine the transactions on another node
257269
self.sync_mempools(wait=0.1, nodes=[node, miner])
258270
for txid in txids_to_replace:
@@ -261,7 +273,12 @@ def sanity_check_rbf_estimates(self, utxos):
261273
# RBF the low-fee transactions
262274
while len(utxos_to_respend) > 0:
263275
u = utxos_to_respend.pop(0)
264-
send_tx(self.wallet, node, u, high_feerate)
276+
tx = make_tx(self.wallet, u, high_feerate)
277+
node.sendrawtransaction(tx["hex"])
278+
txs.append(tx)
279+
dec_txs = [res["result"] for res in node.batch([node.decoderawtransaction.get_request(tx["hex"]) for tx in txs])]
280+
self.wallet.scan_txs(dec_txs)
281+
265282

266283
# Mine the last replacement txs
267284
self.sync_mempools(wait=0.1, nodes=[node, miner])

test/functional/test_framework/wallet.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ def scan_tx(self, tx):
141141
if out['scriptPubKey']['hex'] == self._scriptPubKey.hex():
142142
self._utxos.append(self._create_utxo(txid=tx["txid"], vout=out["n"], value=out["value"], height=0))
143143

144+
def scan_txs(self, txs):
145+
for tx in txs:
146+
self.scan_tx(tx)
147+
144148
def sign_tx(self, tx, fixed_length=True):
145149
"""Sign tx that has been created by MiniWallet in P2PK mode"""
146150
assert_equal(self._mode, MiniWalletMode.RAW_P2PK)

0 commit comments

Comments
 (0)