Skip to content

Commit 604bf2e

Browse files
committed
Merge bitcoin/bitcoin#28121: include verbose "reject-details" field in testmempoolaccept response
b6f0593 doc: add release note about testmempoolaccept debug-message (Matthew Zipkin) f9cac63 test: cover testmempoolaccept debug-message in RBF test (Matthew Zipkin) f9650e1 rbf: remove unecessary newline at end of error string (Matthew Zipkin) 221c789 rpc: include verbose reject-details field in testmempoolaccept response (Matthew Zipkin) Pull request description: Adds a new field `reject-details` in `testmempoolaccept` responses to include `m_debug_message` from `ValidationState`. This string is the complete error message thrown by the mempool in response to `sendrawtransaction`. The extra verbosity is helpful to consumers of `testmempoolaccept`, which is sort of a debug tool anyway. example: > > { > "txid": "07d7a59a7bdad4c3a5070659ea04147c9b755ad9e173c52b6a38e017abf0f5b8", > "wtxid": "5dc243b1b92ee2f5a43134eb3e23449be03d1abb3d7f3c03c836ed0f13c50185", > "allowed": false, > "reject-reason": "insufficient fee", > "reject-details": "insufficient fee, rejecting replacement 07d7a59a7bdad4c3a5070659ea04147c9b755ad9e173c52b6a38e017abf0f5b8; new feerate 0.00300000 BTC/kvB <= old feerate 0.00300000 BTC/kvB" > } ACKs for top commit: rkrux: re-ACK b6f0593 glozow: ACK b6f0593 Tree-SHA512: 340b8023d59cefa84598879c4efdb7c399a3f62da126e87c595523f302e53d33098fc69da9c5f8c92b7580dc75466c66cea372051f935b197265648fe15c43a3
2 parents 228aba2 + b6f0593 commit 604bf2e

File tree

10 files changed

+89
-28
lines changed

10 files changed

+89
-28
lines changed

doc/release-notes-28121.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The RPC `testmempoolaccept` response now includes a "reject-details" field in some cases,
2+
similar to the complete error messages returned by `sendrawtransaction` (#28121)

src/policy/rbf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ std::optional<std::string> GetEntriesForConflicts(const CTransaction& tx,
7171
// descendants (i.e. if multiple conflicts share a descendant, it will be counted multiple
7272
// times), but we just want to be conservative to avoid doing too much work.
7373
if (nConflictingCount > MAX_REPLACEMENT_CANDIDATES) {
74-
return strprintf("rejecting replacement %s; too many potential replacements (%d > %d)\n",
74+
return strprintf("rejecting replacement %s; too many potential replacements (%d > %d)",
7575
txid.ToString(),
7676
nConflictingCount,
7777
MAX_REPLACEMENT_CANDIDATES);

src/rpc/mempool.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ static RPCHelpMan testmempoolaccept()
146146
{RPCResult{RPCResult::Type::STR_HEX, "", "transaction wtxid in hex"},
147147
}},
148148
}},
149-
{RPCResult::Type::STR, "reject-reason", /*optional=*/true, "Rejection string (only present when 'allowed' is false)"},
149+
{RPCResult::Type::STR, "reject-reason", /*optional=*/true, "Rejection reason (only present when 'allowed' is false)"},
150+
{RPCResult::Type::STR, "reject-details", /*optional=*/true, "Rejection details (only present when 'allowed' is false and rejection details exist)"},
150151
}},
151152
}
152153
},
@@ -245,6 +246,7 @@ static RPCHelpMan testmempoolaccept()
245246
result_inner.pushKV("reject-reason", "missing-inputs");
246247
} else {
247248
result_inner.pushKV("reject-reason", state.GetRejectReason());
249+
result_inner.pushKV("reject-details", state.ToString());
248250
}
249251
}
250252
rpc_result.push_back(std::move(result_inner));

test/functional/feature_cltv.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ def run_test(self):
148148
# create and test one invalid tx per CLTV failure reason (5 in total)
149149
for i in range(5):
150150
spendtx = wallet.create_self_transfer()['tx']
151+
assert_equal(len(spendtx.vin), 1)
152+
coin = spendtx.vin[0]
153+
coin_txid = format(coin.prevout.hash, '064x')
154+
coin_vout = coin.prevout.n
151155
cltv_invalidate(spendtx, i)
152156

153157
expected_cltv_reject_reason = [
@@ -159,12 +163,15 @@ def run_test(self):
159163
][i]
160164
# First we show that this tx is valid except for CLTV by getting it
161165
# rejected from the mempool for exactly that reason.
166+
spendtx_txid = spendtx.hash
167+
spendtx_wtxid = spendtx.getwtxid()
162168
assert_equal(
163169
[{
164-
'txid': spendtx.hash,
165-
'wtxid': spendtx.getwtxid(),
170+
'txid': spendtx_txid,
171+
'wtxid': spendtx_wtxid,
166172
'allowed': False,
167173
'reject-reason': expected_cltv_reject_reason,
174+
'reject-details': expected_cltv_reject_reason + f", input 0 of {spendtx_txid} (wtxid {spendtx_wtxid}), spending {coin_txid}:{coin_vout}"
168175
}],
169176
self.nodes[0].testmempoolaccept(rawtxs=[spendtx.serialize().hex()], maxfeerate=0),
170177
)

test/functional/feature_dersig.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,23 @@ def run_test(self):
109109
self.log.info("Test that transactions with non-DER signatures cannot appear in a block")
110110
block.nVersion = 4
111111

112-
spendtx = self.create_tx(self.coinbase_txids[1])
112+
coin_txid = self.coinbase_txids[1]
113+
spendtx = self.create_tx(coin_txid)
113114
unDERify(spendtx)
114115
spendtx.rehash()
115116

116117
# First we show that this tx is valid except for DERSIG by getting it
117118
# rejected from the mempool for exactly that reason.
119+
spendtx_txid = spendtx.hash
120+
spendtx_wtxid = spendtx.getwtxid()
118121
assert_equal(
119122
[{
120-
'txid': spendtx.hash,
121-
'wtxid': spendtx.getwtxid(),
123+
'txid': spendtx_txid,
124+
'wtxid': spendtx_wtxid,
122125
'allowed': False,
123126
'reject-reason': 'mandatory-script-verify-flag-failed (Non-canonical DER signature)',
127+
'reject-details': 'mandatory-script-verify-flag-failed (Non-canonical DER signature), ' +
128+
f"input 0 of {spendtx_txid} (wtxid {spendtx_wtxid}), spending {coin_txid}:0"
124129
}],
125130
self.nodes[0].testmempoolaccept(rawtxs=[spendtx.serialize().hex()], maxfeerate=0),
126131
)

test/functional/feature_rbf.py

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,22 @@ def test_simple_doublespend(self):
103103
"""Simple doublespend"""
104104
# we use MiniWallet to create a transaction template with inputs correctly set,
105105
# and modify the output (amount, scriptPubKey) according to our needs
106-
tx = self.wallet.create_self_transfer()["tx"]
106+
tx = self.wallet.create_self_transfer(fee_rate=Decimal("0.003"))["tx"]
107107
tx1a_txid = self.nodes[0].sendrawtransaction(tx.serialize().hex())
108108

109109
# Should fail because we haven't changed the fee
110110
tx.vout[0].scriptPubKey[-1] ^= 1
111+
tx.rehash()
112+
tx_hex = tx.serialize().hex()
111113

112114
# This will raise an exception due to insufficient fee
113-
assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, tx.serialize().hex(), 0)
115+
reject_reason = "insufficient fee"
116+
reject_details = f"{reject_reason}, rejecting replacement {tx.hash}; new feerate 0.00300000 BTC/kvB <= old feerate 0.00300000 BTC/kvB"
117+
res = self.nodes[0].testmempoolaccept(rawtxs=[tx_hex])[0]
118+
assert_equal(res["reject-reason"], reject_reason)
119+
assert_equal(res["reject-details"], reject_details)
120+
assert_raises_rpc_error(-26, f"{reject_details}", self.nodes[0].sendrawtransaction, tx_hex, 0)
121+
114122

115123
# Extra 0.1 BTC fee
116124
tx.vout[0].nValue -= int(0.1 * COIN)
@@ -154,7 +162,14 @@ def test_doublespend_chain(self):
154162
dbl_tx_hex = dbl_tx.serialize().hex()
155163

156164
# This will raise an exception due to insufficient fee
157-
assert_raises_rpc_error(-26, "insufficient fee", self.nodes[0].sendrawtransaction, dbl_tx_hex, 0)
165+
reject_reason = "insufficient fee"
166+
reject_details = f"{reject_reason}, rejecting replacement {dbl_tx.hash}, less fees than conflicting txs; 3.00 < 4.00"
167+
res = self.nodes[0].testmempoolaccept(rawtxs=[dbl_tx_hex])[0]
168+
assert_equal(res["reject-reason"], reject_reason)
169+
assert_equal(res["reject-details"], reject_details)
170+
assert_raises_rpc_error(-26, f"{reject_details}", self.nodes[0].sendrawtransaction, dbl_tx_hex, 0)
171+
172+
158173

159174
# Accepted with sufficient fee
160175
dbl_tx.vout[0].nValue = int(0.1 * COIN)
@@ -273,22 +288,30 @@ def test_spends_of_conflicting_outputs(self):
273288
utxo1 = self.make_utxo(self.nodes[0], int(1.2 * COIN))
274289
utxo2 = self.make_utxo(self.nodes[0], 3 * COIN)
275290

276-
tx1a_utxo = self.wallet.send_self_transfer(
291+
tx1a = self.wallet.send_self_transfer(
277292
from_node=self.nodes[0],
278293
utxo_to_spend=utxo1,
279294
sequence=0,
280295
fee=Decimal("0.1"),
281-
)["new_utxo"]
296+
)
297+
tx1a_utxo = tx1a["new_utxo"]
282298

283299
# Direct spend an output of the transaction we're replacing.
284-
tx2_hex = self.wallet.create_self_transfer_multi(
300+
tx2 = self.wallet.create_self_transfer_multi(
285301
utxos_to_spend=[utxo1, utxo2, tx1a_utxo],
286302
sequence=0,
287303
amount_per_output=int(COIN * tx1a_utxo["value"]),
288-
)["hex"]
304+
)["tx"]
305+
tx2_hex = tx2.serialize().hex()
289306

290307
# This will raise an exception
291-
assert_raises_rpc_error(-26, "bad-txns-spends-conflicting-tx", self.nodes[0].sendrawtransaction, tx2_hex, 0)
308+
reject_reason = "bad-txns-spends-conflicting-tx"
309+
reject_details = f"{reject_reason}, {tx2.hash} spends conflicting transaction {tx1a['tx'].hash}"
310+
res = self.nodes[0].testmempoolaccept(rawtxs=[tx2_hex])[0]
311+
assert_equal(res["reject-reason"], reject_reason)
312+
assert_equal(res["reject-details"], reject_details)
313+
assert_raises_rpc_error(-26, f"{reject_details}", self.nodes[0].sendrawtransaction, tx2_hex, 0)
314+
292315

293316
# Spend tx1a's output to test the indirect case.
294317
tx1b_utxo = self.wallet.send_self_transfer(
@@ -319,14 +342,21 @@ def test_new_unconfirmed_inputs(self):
319342
fee=Decimal("0.1"),
320343
)
321344

322-
tx2_hex = self.wallet.create_self_transfer_multi(
345+
tx2 = self.wallet.create_self_transfer_multi(
323346
utxos_to_spend=[confirmed_utxo, unconfirmed_utxo],
324347
sequence=0,
325348
amount_per_output=1 * COIN,
326-
)["hex"]
349+
)["tx"]
350+
tx2_hex = tx2.serialize().hex()
327351

328352
# This will raise an exception
329-
assert_raises_rpc_error(-26, "replacement-adds-unconfirmed", self.nodes[0].sendrawtransaction, tx2_hex, 0)
353+
reject_reason = "replacement-adds-unconfirmed"
354+
reject_details = f"{reject_reason}, replacement {tx2.hash} adds unconfirmed input, idx 1"
355+
res = self.nodes[0].testmempoolaccept(rawtxs=[tx2_hex])[0]
356+
assert_equal(res["reject-reason"], reject_reason)
357+
assert_equal(res["reject-details"], reject_details)
358+
assert_raises_rpc_error(-26, f"{reject_details}", self.nodes[0].sendrawtransaction, tx2_hex, 0)
359+
330360

331361
def test_too_many_replacements(self):
332362
"""Replacements that evict too many transactions are rejected"""
@@ -368,7 +398,13 @@ def test_too_many_replacements(self):
368398
double_tx_hex = double_tx.serialize().hex()
369399

370400
# This will raise an exception
371-
assert_raises_rpc_error(-26, "too many potential replacements", self.nodes[0].sendrawtransaction, double_tx_hex, 0)
401+
reject_reason = "too many potential replacements"
402+
reject_details = f"{reject_reason}, rejecting replacement {double_tx.hash}; too many potential replacements ({MAX_REPLACEMENT_LIMIT + 1} > {MAX_REPLACEMENT_LIMIT})"
403+
res = self.nodes[0].testmempoolaccept(rawtxs=[double_tx_hex])[0]
404+
assert_equal(res["reject-reason"], reject_reason)
405+
assert_equal(res["reject-details"], reject_details)
406+
assert_raises_rpc_error(-26, f"{reject_details}", self.nodes[0].sendrawtransaction, double_tx_hex, 0)
407+
372408

373409
# If we remove an input, it should pass
374410
double_tx.vin.pop()

test/functional/mempool_accept.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ def check_mempool_result(self, result_expected, *args, **kwargs):
6767
if "fees" in r:
6868
r["fees"].pop("effective-feerate")
6969
r["fees"].pop("effective-includes")
70+
if "reject-details" in r:
71+
r.pop("reject-details")
7072
assert_equal(result_expected, result_test)
7173
assert_equal(self.nodes[0].getmempoolinfo()['size'], self.mempool_size) # Must not change mempool state
7274

test/functional/mempool_accept_wtxid.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,15 @@ def run_test(self):
100100
"txid": child_one_txid,
101101
"wtxid": child_one_wtxid,
102102
"allowed": False,
103-
"reject-reason": "txn-already-in-mempool"
103+
"reject-reason": "txn-already-in-mempool",
104+
"reject-details": "txn-already-in-mempool"
104105
}])
105106
assert_equal(node.testmempoolaccept([child_two.serialize().hex()])[0], {
106107
"txid": child_two_txid,
107108
"wtxid": child_two_wtxid,
108109
"allowed": False,
109-
"reject-reason": "txn-same-nonwitness-data-in-mempool"
110+
"reject-reason": "txn-same-nonwitness-data-in-mempool",
111+
"reject-details": "txn-same-nonwitness-data-in-mempool"
110112
})
111113

112114
# sendrawtransaction will not throw but quits early when the exact same transaction is already in mempool

test/functional/mempool_package_rbf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def test_package_rbf_max_conflicts(self):
219219
package_child = self.wallet.create_self_transfer(fee_rate=child_feerate, utxo_to_spend=package_parent["new_utxos"][0])
220220

221221
pkg_results = node.submitpackage([package_parent["hex"], package_child["hex"]], maxfeerate=0)
222-
assert_equal(f"package RBF failed: too many potential replacements, rejecting replacement {package_child['tx'].rehash()}; too many potential replacements (102 > 100)\n", pkg_results["package_msg"])
222+
assert_equal(f"package RBF failed: too many potential replacements, rejecting replacement {package_child['tx'].rehash()}; too many potential replacements (102 > 100)", pkg_results["package_msg"])
223223
self.assert_mempool_contents(expected=expected_txns)
224224

225225
# Make singleton tx to conflict with in next batch
@@ -234,7 +234,7 @@ def test_package_rbf_max_conflicts(self):
234234
package_parent = self.wallet.create_self_transfer_multi(utxos_to_spend=double_spending_coins, fee_per_output=parent_fee_per_conflict)
235235
package_child = self.wallet.create_self_transfer(fee_rate=child_feerate, utxo_to_spend=package_parent["new_utxos"][0])
236236
pkg_results = node.submitpackage([package_parent["hex"], package_child["hex"]], maxfeerate=0)
237-
assert_equal(f"package RBF failed: too many potential replacements, rejecting replacement {package_child['tx'].rehash()}; too many potential replacements (101 > 100)\n", pkg_results["package_msg"])
237+
assert_equal(f"package RBF failed: too many potential replacements, rejecting replacement {package_child['tx'].rehash()}; too many potential replacements (101 > 100)", pkg_results["package_msg"])
238238
self.assert_mempool_contents(expected=expected_txns)
239239

240240
# Finally, evict MAX_REPLACEMENT_CANDIDATES

test/functional/rpc_packages.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,21 @@ def test_independent(self, coin):
110110
self.assert_testres_equal(package_bad, testres_bad)
111111

112112
self.log.info("Check testmempoolaccept tells us when some transactions completed validation successfully")
113-
tx_bad_sig_hex = node.createrawtransaction([{"txid": coin["txid"], "vout": 0}],
113+
tx_bad_sig_hex = node.createrawtransaction([{"txid": coin["txid"], "vout": coin["vout"]}],
114114
{address : coin["amount"] - Decimal("0.0001")})
115115
tx_bad_sig = tx_from_hex(tx_bad_sig_hex)
116116
testres_bad_sig = node.testmempoolaccept(self.independent_txns_hex + [tx_bad_sig_hex])
117117
# By the time the signature for the last transaction is checked, all the other transactions
118118
# have been fully validated, which is why the node returns full validation results for all
119119
# transactions here but empty results in other cases.
120+
tx_bad_sig_txid = tx_bad_sig.rehash()
121+
tx_bad_sig_wtxid = tx_bad_sig.getwtxid()
120122
assert_equal(testres_bad_sig, self.independent_txns_testres + [{
121-
"txid": tx_bad_sig.rehash(),
122-
"wtxid": tx_bad_sig.getwtxid(), "allowed": False,
123-
"reject-reason": "mandatory-script-verify-flag-failed (Operation not valid with the current stack size)"
123+
"txid": tx_bad_sig_txid,
124+
"wtxid": tx_bad_sig_wtxid, "allowed": False,
125+
"reject-reason": "mandatory-script-verify-flag-failed (Operation not valid with the current stack size)",
126+
"reject-details": "mandatory-script-verify-flag-failed (Operation not valid with the current stack size), " +
127+
f"input 0 of {tx_bad_sig_txid} (wtxid {tx_bad_sig_wtxid}), spending {coin['txid']}:{coin['vout']}"
124128
}])
125129

126130
self.log.info("Check testmempoolaccept reports txns in packages that exceed max feerate")
@@ -304,7 +308,8 @@ def test_rbf(self):
304308
assert testres_rbf_single[0]["allowed"]
305309
testres_rbf_package = self.independent_txns_testres_blank + [{
306310
"txid": replacement_tx["txid"], "wtxid": replacement_tx["wtxid"], "allowed": False,
307-
"reject-reason": "bip125-replacement-disallowed"
311+
"reject-reason": "bip125-replacement-disallowed",
312+
"reject-details": "bip125-replacement-disallowed"
308313
}]
309314
self.assert_testres_equal(self.independent_txns_hex + [replacement_tx["hex"]], testres_rbf_package)
310315

0 commit comments

Comments
 (0)