Skip to content

Commit b272ecf

Browse files
committed
Reject replacements that add new unconfirmed inputs
1 parent fc8c19a commit b272ecf

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

qa/replace-by-fee/rbf-tests.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,30 @@ def setUpClass(cls):
3535
cls.proxy = bitcoin.rpc.Proxy()
3636

3737
@classmethod
38-
def tearDownClass(cls):
39-
# Make sure mining works
38+
def mine_mempool(cls):
39+
"""Mine until mempool is empty"""
4040
mempool_size = 1
4141
while mempool_size:
42-
cls.proxy.call('generate',1)
42+
cls.proxy.call('generate', 1)
4343
new_mempool_size = len(cls.proxy.getrawmempool())
4444

4545
# It's possible to get stuck in a loop here if the mempool has
4646
# transactions that can't be mined.
4747
assert(new_mempool_size != mempool_size)
4848
mempool_size = new_mempool_size
4949

50-
def make_txout(self, amount, scriptPubKey=CScript([1])):
50+
@classmethod
51+
def tearDownClass(cls):
52+
# Make sure mining works
53+
cls.mine_mempool()
54+
55+
def make_txout(self, amount, confirmed=True, scriptPubKey=CScript([1])):
5156
"""Create a txout with a given amount and scriptPubKey
5257
5358
Mines coins as needed.
59+
60+
confirmed - txouts created will be confirmed in the blockchain;
61+
unconfirmed otherwise.
5462
"""
5563
fee = 1*COIN
5664
while self.proxy.getbalance() < amount + fee:
@@ -72,6 +80,10 @@ def make_txout(self, amount, scriptPubKey=CScript([1])):
7280

7381
tx2_txid = self.proxy.sendrawtransaction(tx2, True)
7482

83+
# If requested, ensure txouts are confirmed.
84+
if confirmed:
85+
self.mine_mempool()
86+
7587
return COutPoint(tx2_txid, 0)
7688

7789
def test_simple_doublespend(self):
@@ -276,5 +288,24 @@ def test_spends_of_conflicting_outputs(self):
276288
else:
277289
self.fail()
278290

291+
def test_new_unconfirmed_inputs(self):
292+
"""Replacements that add new unconfirmed inputs are rejected"""
293+
confirmed_utxo = self.make_txout(1.1*COIN)
294+
unconfirmed_utxo = self.make_txout(0.1*COIN, False)
295+
296+
tx1 = CTransaction([CTxIn(confirmed_utxo)],
297+
[CTxOut(1.0*COIN, CScript([b'a']))])
298+
tx1_txid = self.proxy.sendrawtransaction(tx1, True)
299+
300+
tx2 = CTransaction([CTxIn(confirmed_utxo), CTxIn(unconfirmed_utxo)],
301+
tx1.vout)
302+
303+
try:
304+
tx2_txid = self.proxy.sendrawtransaction(tx2, True)
305+
except bitcoin.rpc.JSONRPCException as exp:
306+
self.assertEqual(exp.error['code'], -26)
307+
else:
308+
self.fail()
309+
279310
if __name__ == '__main__':
280311
unittest.main()

src/main.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
10091009
LOCK(pool.cs);
10101010

10111011
CFeeRate newFeeRate(nFees, nSize);
1012+
set<uint256> setConflictsParents;
10121013
BOOST_FOREACH(const uint256 hashConflicting, setConflicts)
10131014
{
10141015
CTxMemPool::txiter mi = pool.mapTx.find(hashConflicting);
@@ -1042,6 +1043,11 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
10421043
REJECT_INSUFFICIENTFEE, "insufficient fee");
10431044
}
10441045

1046+
BOOST_FOREACH(const CTxIn &txin, mi->GetTx().vin)
1047+
{
1048+
setConflictsParents.insert(txin.prevout.hash);
1049+
}
1050+
10451051
// For efficiency we simply sum up the pre-calculated
10461052
// fees/size-with-descendants values from the mempool package
10471053
// tracking; this does mean the pathological case of diamond tx
@@ -1050,6 +1056,24 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
10501056
nConflictingSize += mi->GetSizeWithDescendants();
10511057
}
10521058

1059+
for (unsigned int j = 0; j < tx.vin.size(); j++)
1060+
{
1061+
// We don't want to accept replacements that require low
1062+
// feerate junk to be mined first. Ideally we'd keep track of
1063+
// the ancestor feerates and make the decision based on that,
1064+
// but for now requiring all new inputs to be confirmed works.
1065+
if (!setConflictsParents.count(tx.vin[j].prevout.hash))
1066+
{
1067+
// Rather than check the UTXO set - potentially expensive -
1068+
// it's cheaper to just check if the new input refers to a
1069+
// tx that's in the mempool.
1070+
if (pool.mapTx.find(tx.vin[j].prevout.hash) != pool.mapTx.end())
1071+
return state.DoS(0, error("AcceptToMemoryPool: replacement %s adds unconfirmed input, idx %d",
1072+
hash.ToString(), j),
1073+
REJECT_NONSTANDARD, "replacement-adds-unconfirmed");
1074+
}
1075+
}
1076+
10531077
// The replacement must pay greater fees than the transactions it
10541078
// replaces - if we did the bandwidth used by those conflicting
10551079
// transactions would not be paid for.

0 commit comments

Comments
 (0)