Skip to content

Commit 50cede3

Browse files
committed
[mempool] Allow one extra single-ancestor transaction per package
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
1 parent 1212808 commit 50cede3

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

616630
// 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
@@ -63,6 +63,12 @@ static const unsigned int DEFAULT_ANCESTOR_SIZE_LIMIT = 101;
6363
static const unsigned int DEFAULT_DESCENDANT_LIMIT = 25;
6464
/** Default for -limitdescendantsize, maximum kilobytes of in-mempool descendants */
6565
static const unsigned int DEFAULT_DESCENDANT_SIZE_LIMIT = 101;
66+
/**
67+
* An extra transaction can be added to a package, as long as it only has one
68+
* ancestor and is no larger than this. Not really any reason to make this
69+
* configurable as it doesn't materially change DoS parameters.
70+
*/
71+
static const unsigned int EXTRA_DESCENDANT_TX_SIZE_LIMIT = 10000;
6672
/** Default for -mempoolexpiry, expiration time for mempool transactions in hours */
6773
static const unsigned int DEFAULT_MEMPOOL_EXPIRY = 336;
6874
/** 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)