Skip to content

Commit e282764

Browse files
committed
Merge bitcoin/bitcoin#25228: test: add BIP-125 rule 5 testcase with default mempool
687adda test: add BIP-125 rule 5 testcase with default mempool (James O'Beirne) 6120e8e test: allow passing sequence through create_self_transfer_multi (James O'Beirne) Pull request description: Currently, we only test rule 5 of BIP-125 (replacement transactions cannot evict more than 100 transactions) by changing default mempool parameters to allow for more descendants. The current test works on a single transaction graph that has over 100 descendants. This patch adds a test to exercise rule 5 using the default mempool parameters. The case is a little more sophisticated: instead of working on a single transaction graph, it uses a replacement transaction to "unite" several UTXOs which join independent transaction graphs. The total number of transactions in these graphs sum to more than the max allowable replacement. I think the difference in transaction topology makes this a worthwhile testcase to have, setting aside the fact that this testcase works without having to use atypical mempool params. See also: [relevant discussion from IRC](https://www.erisian.com.au/bitcoin-core-dev/log-2022-05-27.html#l-126) ACKs for top commit: laanwj: Code review ACK 687adda LarryRuane: ACK 687adda Tree-SHA512: e589aeaf9d6f137d546b7809f8795d6f6043d87b15e97c2efe85b42ce8b49d977ee7d79440c542ca4b0b5ca2de527488029841a1ffc0d96c5771897df4b3f324
2 parents d8ae504 + 687adda commit e282764

File tree

2 files changed

+106
-4
lines changed

2 files changed

+106
-4
lines changed

test/functional/feature_rbf.py

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
MAX_REPLACEMENT_LIMIT = 100
3333
class ReplaceByFeeTest(BitcoinTestFramework):
3434
def set_test_params(self):
35-
self.num_nodes = 1
35+
self.num_nodes = 2
3636
self.extra_args = [
3737
[
3838
"-acceptnonstdtxn=1",
@@ -42,6 +42,9 @@ def set_test_params(self):
4242
"-limitdescendantcount=200",
4343
"-limitdescendantsize=101",
4444
],
45+
# second node has default mempool parameters
46+
[
47+
],
4548
]
4649
self.supports_cli = False
4750

@@ -73,6 +76,9 @@ def run_test(self):
7376
self.log.info("Running test too many replacements...")
7477
self.test_too_many_replacements()
7578

79+
self.log.info("Running test too many replacements using default mempool params...")
80+
self.test_too_many_replacements_with_default_mempool_params()
81+
7682
self.log.info("Running test opt-in...")
7783
self.test_opt_in()
7884

@@ -397,6 +403,94 @@ def test_too_many_replacements(self):
397403
double_tx_hex = double_tx.serialize().hex()
398404
self.nodes[0].sendrawtransaction(double_tx_hex, 0)
399405

406+
def test_too_many_replacements_with_default_mempool_params(self):
407+
"""
408+
Test rule 5 of BIP125 (do not allow replacements that cause more than 100
409+
evictions) without having to rely on non-default mempool parameters.
410+
411+
In order to do this, create a number of "root" UTXOs, and then hang
412+
enough transactions off of each root UTXO to exceed the MAX_REPLACEMENT_LIMIT.
413+
Then create a conflicting RBF replacement transaction.
414+
"""
415+
normal_node = self.nodes[1]
416+
wallet = MiniWallet(normal_node)
417+
wallet.rescan_utxos()
418+
# Clear mempools to avoid cross-node sync failure.
419+
for node in self.nodes:
420+
self.generate(node, 1)
421+
422+
# This has to be chosen so that the total number of transactions can exceed
423+
# MAX_REPLACEMENT_LIMIT without having any one tx graph run into the descendant
424+
# limit; 10 works.
425+
num_tx_graphs = 10
426+
427+
# (Number of transactions per graph, BIP125 rule 5 failure expected)
428+
cases = [
429+
# Test the base case of evicting fewer than MAX_REPLACEMENT_LIMIT
430+
# transactions.
431+
((MAX_REPLACEMENT_LIMIT // num_tx_graphs) - 1, False),
432+
433+
# Test hitting the rule 5 eviction limit.
434+
(MAX_REPLACEMENT_LIMIT // num_tx_graphs, True),
435+
]
436+
437+
for (txs_per_graph, failure_expected) in cases:
438+
self.log.debug(f"txs_per_graph: {txs_per_graph}, failure: {failure_expected}")
439+
# "Root" utxos of each txn graph that we will attempt to double-spend with
440+
# an RBF replacement.
441+
root_utxos = []
442+
443+
# For each root UTXO, create a package that contains the spend of that
444+
# UTXO and `txs_per_graph` children tx.
445+
for graph_num in range(num_tx_graphs):
446+
root_utxos.append(wallet.get_utxo())
447+
448+
optin_parent_tx = wallet.send_self_transfer_multi(
449+
from_node=normal_node,
450+
sequence=BIP125_SEQUENCE_NUMBER,
451+
utxos_to_spend=[root_utxos[graph_num]],
452+
num_outputs=txs_per_graph,
453+
)
454+
assert_equal(True, normal_node.getmempoolentry(optin_parent_tx['txid'])['bip125-replaceable'])
455+
new_utxos = optin_parent_tx['new_utxos']
456+
457+
for utxo in new_utxos:
458+
# Create spends for each output from the "root" of this graph.
459+
child_tx = wallet.send_self_transfer(
460+
from_node=normal_node,
461+
utxo_to_spend=utxo,
462+
)
463+
464+
assert normal_node.getmempoolentry(child_tx['txid'])
465+
466+
num_txs_invalidated = len(root_utxos) + (num_tx_graphs * txs_per_graph)
467+
468+
if failure_expected:
469+
assert num_txs_invalidated > MAX_REPLACEMENT_LIMIT
470+
else:
471+
assert num_txs_invalidated <= MAX_REPLACEMENT_LIMIT
472+
473+
# Now attempt to submit a tx that double-spends all the root tx inputs, which
474+
# would invalidate `num_txs_invalidated` transactions.
475+
double_tx = wallet.create_self_transfer_multi(
476+
from_node=normal_node,
477+
utxos_to_spend=root_utxos,
478+
fee_per_output=10_000_000, # absurdly high feerate
479+
)
480+
tx_hex = double_tx.serialize().hex()
481+
482+
if failure_expected:
483+
assert_raises_rpc_error(
484+
-26, "too many potential replacements", normal_node.sendrawtransaction, tx_hex, 0)
485+
else:
486+
txid = normal_node.sendrawtransaction(tx_hex, 0)
487+
assert normal_node.getmempoolentry(txid)
488+
489+
# Clear the mempool once finished, and rescan the other nodes' wallet
490+
# to account for the spends we've made on `normal_node`.
491+
self.generate(normal_node, 1)
492+
self.wallet.rescan_utxos()
493+
400494
def test_opt_in(self):
401495
"""Replacing should only work if orig tx opted in"""
402496
tx0_outpoint = self.make_utxo(self.nodes[0], int(1.1 * COIN))

test/functional/test_framework/wallet.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from random import choice
1111
from typing import (
1212
Any,
13+
List,
1314
Optional,
1415
)
1516
from test_framework.address import (
@@ -147,7 +148,7 @@ def get_descriptor(self):
147148
def get_address(self):
148149
return self._address
149150

150-
def get_utxo(self, *, txid: str = '', vout: Optional[int] = None, mark_as_spent=True):
151+
def get_utxo(self, *, txid: str = '', vout: Optional[int] = None, mark_as_spent=True) -> dict:
151152
"""
152153
Returns a utxo and marks it as spent (pops it from the internal list)
153154
@@ -215,14 +216,21 @@ def send_self_transfer_multi(self, **kwargs):
215216
return {'new_utxos': [self.get_utxo(txid=txid, vout=vout) for vout in range(len(tx.vout))],
216217
'txid': txid, 'hex': tx.serialize().hex(), 'tx': tx}
217218

218-
def create_self_transfer_multi(self, *, from_node, utxos_to_spend=None, num_outputs=1, fee_per_output=1000):
219+
def create_self_transfer_multi(
220+
self, *, from_node,
221+
utxos_to_spend: Optional[List[dict]] = None,
222+
num_outputs=1,
223+
sequence=0,
224+
fee_per_output=1000):
219225
"""
220226
Create and return a transaction that spends the given UTXOs and creates a
221227
certain number of outputs with equal amounts.
222228
"""
223229
utxos_to_spend = utxos_to_spend or [self.get_utxo()]
224230
# create simple tx template (1 input, 1 output)
225-
tx = self.create_self_transfer(fee_rate=0, from_node=from_node, utxo_to_spend=utxos_to_spend[0], mempool_valid=False)['tx']
231+
tx = self.create_self_transfer(
232+
fee_rate=0, from_node=from_node,
233+
utxo_to_spend=utxos_to_spend[0], sequence=sequence, mempool_valid=False)['tx']
226234

227235
# duplicate inputs, witnesses and outputs
228236
tx.vin = [deepcopy(tx.vin[0]) for _ in range(len(utxos_to_spend))]

0 commit comments

Comments
 (0)