Skip to content

Commit 51a6e2c

Browse files
committed
Merge #15681: [mempool] Allow one extra single-ancestor transaction per package
50cede3 [mempool] Allow one extra single-ancestor transaction per package (Matt Corallo) Pull request description: This implements the proposed policy change from [1], which allows certain classes of contract protocols involving revocation punishments to use CPFP. Note that some such use-cases may still want some form of one-deep package relay, though even this alone may greatly simplify some lightning fee negotiation. [1] https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2018-November/016518.html ACKs for top commit: ajtowns: ACK 50cede3 -- looked over code again, compared with previous commit, compiles, etc. sdaftuar: ACK 50cede3 ryanofsky: utACK 50cede3. Changes since last review: adding EXTRA_DESCENDANT_TX_SIZE_LIMIT constant, changing max ancestor size from 1,000,000 to nLimitAncestorSize constant (101,000), fixing test comment and getting rid of unused test node. Tree-SHA512: b052c2a0f384855572b4579310131897b612201214b5abbb225167224e4f550049e300b471dbf320928652571e92ca2d650050b7cf39ac92b3bc1d2bcd386c1c
2 parents f4b1fe7 + 50cede3 commit 51a6e2c

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

src/validation.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,21 @@ static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool
616616
size_t nLimitDescendantSize = gArgs.GetArg("-limitdescendantsize", DEFAULT_DESCENDANT_SIZE_LIMIT)*1000;
617617
std::string errString;
618618
if (!pool.CalculateMemPoolAncestors(entry, setAncestors, nLimitAncestors, nLimitAncestorSize, nLimitDescendants, nLimitDescendantSize, errString)) {
619-
return state.Invalid(ValidationInvalidReason::TX_MEMPOOL_POLICY, false, REJECT_NONSTANDARD, "too-long-mempool-chain", errString);
619+
setAncestors.clear();
620+
// If the new transaction is relatively small (up to 40k weight)
621+
// and has at most one ancestor (ie ancestor limit of 2, including
622+
// the new transaction), allow it if its parent has exactly the
623+
// descendant limit descendants.
624+
//
625+
// This allows protocols which rely on distrusting counterparties
626+
// being able to broadcast descendants of an unconfirmed transaction
627+
// to be secure by simply only having two immediately-spendable
628+
// outputs - one for each counterparty. For more info on the uses for
629+
// this, see https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2018-November/016518.html
630+
if (nSize > EXTRA_DESCENDANT_TX_SIZE_LIMIT ||
631+
!pool.CalculateMemPoolAncestors(entry, setAncestors, 2, nLimitAncestorSize, nLimitDescendants + 1, nLimitDescendantSize + EXTRA_DESCENDANT_TX_SIZE_LIMIT, errString)) {
632+
return state.Invalid(ValidationInvalidReason::TX_MEMPOOL_POLICY, false, REJECT_NONSTANDARD, "too-long-mempool-chain", errString);
633+
}
620634
}
621635

622636
// A transaction that spends outputs that would be replaced by it is invalid. Now

src/validation.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ static const unsigned int DEFAULT_ANCESTOR_SIZE_LIMIT = 101;
6464
static const unsigned int DEFAULT_DESCENDANT_LIMIT = 25;
6565
/** Default for -limitdescendantsize, maximum kilobytes of in-mempool descendants */
6666
static const unsigned int DEFAULT_DESCENDANT_SIZE_LIMIT = 101;
67+
/**
68+
* An extra transaction can be added to a package, as long as it only has one
69+
* ancestor and is no larger than this. Not really any reason to make this
70+
* configurable as it doesn't materially change DoS parameters.
71+
*/
72+
static const unsigned int EXTRA_DESCENDANT_TX_SIZE_LIMIT = 10000;
6773
/** Default for -mempoolexpiry, expiration time for mempool transactions in hours */
6874
static const unsigned int DEFAULT_MEMPOOL_EXPIRY = 336;
6975
/** Maximum kilobytes for transactions to store for processing during reorg */
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2014-2019 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test descendant package tracking carve-out allowing one final transaction in
6+
an otherwise-full package as long as it has only one parent and is <= 10k in
7+
size.
8+
"""
9+
10+
from decimal import Decimal
11+
12+
from test_framework.test_framework import BitcoinTestFramework
13+
from test_framework.util import assert_equal, assert_raises_rpc_error, satoshi_round
14+
15+
MAX_ANCESTORS = 25
16+
MAX_DESCENDANTS = 25
17+
18+
class MempoolPackagesTest(BitcoinTestFramework):
19+
def set_test_params(self):
20+
self.num_nodes = 1
21+
self.extra_args = [["-maxorphantx=1000"]]
22+
23+
def skip_test_if_missing_module(self):
24+
self.skip_if_no_wallet()
25+
26+
# Build a transaction that spends parent_txid:vout
27+
# Return amount sent
28+
def chain_transaction(self, node, parent_txids, vouts, value, fee, num_outputs):
29+
send_value = satoshi_round((value - fee)/num_outputs)
30+
inputs = []
31+
for (txid, vout) in zip(parent_txids, vouts):
32+
inputs.append({'txid' : txid, 'vout' : vout})
33+
outputs = {}
34+
for i in range(num_outputs):
35+
outputs[node.getnewaddress()] = send_value
36+
rawtx = node.createrawtransaction(inputs, outputs)
37+
signedtx = node.signrawtransactionwithwallet(rawtx)
38+
txid = node.sendrawtransaction(signedtx['hex'])
39+
fulltx = node.getrawtransaction(txid, 1)
40+
assert len(fulltx['vout']) == num_outputs # make sure we didn't generate a change output
41+
return (txid, send_value)
42+
43+
def run_test(self):
44+
# Mine some blocks and have them mature.
45+
self.nodes[0].generate(101)
46+
utxo = self.nodes[0].listunspent(10)
47+
txid = utxo[0]['txid']
48+
vout = utxo[0]['vout']
49+
value = utxo[0]['amount']
50+
51+
fee = Decimal("0.0002")
52+
# MAX_ANCESTORS transactions off a confirmed tx should be fine
53+
chain = []
54+
for _ in range(4):
55+
(txid, sent_value) = self.chain_transaction(self.nodes[0], [txid], [vout], value, fee, 2)
56+
vout = 0
57+
value = sent_value
58+
chain.append([txid, value])
59+
for _ in range(MAX_ANCESTORS - 4):
60+
(txid, sent_value) = self.chain_transaction(self.nodes[0], [txid], [0], value, fee, 1)
61+
value = sent_value
62+
chain.append([txid, value])
63+
(second_chain, second_chain_value) = self.chain_transaction(self.nodes[0], [utxo[1]['txid']], [utxo[1]['vout']], utxo[1]['amount'], fee, 1)
64+
65+
# Check mempool has MAX_ANCESTORS + 1 transactions in it
66+
assert_equal(len(self.nodes[0].getrawmempool(True)), MAX_ANCESTORS + 1)
67+
68+
# Adding one more transaction on to the chain should fail.
69+
assert_raises_rpc_error(-26, "too-long-mempool-chain", self.chain_transaction, self.nodes[0], [txid], [0], value, fee, 1)
70+
# ...even if it chains on from some point in the middle of the chain.
71+
assert_raises_rpc_error(-26, "too-long-mempool-chain", self.chain_transaction, self.nodes[0], [chain[2][0]], [1], chain[2][1], fee, 1)
72+
assert_raises_rpc_error(-26, "too-long-mempool-chain", self.chain_transaction, self.nodes[0], [chain[1][0]], [1], chain[1][1], fee, 1)
73+
# ...even if it chains on to two parent transactions with one in the chain.
74+
assert_raises_rpc_error(-26, "too-long-mempool-chain", self.chain_transaction, self.nodes[0], [chain[0][0], second_chain], [1, 0], chain[0][1] + second_chain_value, fee, 1)
75+
# ...especially if its > 40k weight
76+
assert_raises_rpc_error(-26, "too-long-mempool-chain", self.chain_transaction, self.nodes[0], [chain[0][0]], [1], chain[0][1], fee, 350)
77+
# But not if it chains directly off the first transaction
78+
self.chain_transaction(self.nodes[0], [chain[0][0]], [1], chain[0][1], fee, 1)
79+
# and the second chain should work just fine
80+
self.chain_transaction(self.nodes[0], [second_chain], [0], second_chain_value, fee, 1)
81+
82+
# Finally, check that we added two transactions
83+
assert_equal(len(self.nodes[0].getrawmempool(True)), MAX_ANCESTORS + 3)
84+
85+
if __name__ == '__main__':
86+
MempoolPackagesTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
'rpc_invalidateblock.py',
158158
'feature_rbf.py',
159159
'mempool_packages.py',
160+
'mempool_package_onemore.py',
160161
'rpc_createmultisig.py',
161162
'feature_versionbits_warning.py',
162163
'rpc_preciousblock.py',

0 commit comments

Comments
 (0)