Skip to content

rpc: Properly throw on internal I/O errors in GetTransaction - #35728

Merged
ryanofsky merged 6 commits into
bitcoin:masterfrom
maflcko:2607-io-error-tx
Nov 3, 2026
Merged

ryanofsky merged 6 commits into
bitcoin:masterfrom
maflcko:2607-io-error-tx

Conversation

@maflcko

@maflcko maflcko commented Jul 15, 2026

Copy link
Copy Markdown
Member

Currently the GetTransaction return value is a nullable transaction
pointer, where nullptr represents two different outcomes: I/O error and
missing tx.

Reporting a confirmed transaction as missing is wrong, so properly
return an error instead.

@DrahtBot DrahtBot changed the title rpc: Properly throw on internal I/O errors in GetTransaction rpc: Properly throw on internal I/O errors in GetTransaction Jul 15, 2026
@DrahtBot

DrahtBot commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK furszy, l0rinc, musaHaruna, ryanofsky
Stale ACK enirox001, yuvicc, sedited

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36597 (wallet: return broadcast errors from CommitTransaction by pseudoramdom)
  • #36566 (private broadcast: wait synchronously for mempool acceptance by andrewtoth)
  • #36002 (txindex: allow running in pruned mode by andrewtoth)
  • #31252 (rpc: print P2WSH and P2SH redem Script in getrawtransaction and getblock by polespinasa)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/actions/runs/29414186507/job/87348122506
LLM reason (✨ experimental): CI failed due to a Python lint error from ruff (F401 unused import in test/functional/feature_io_errors.py).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@yuvicc

yuvicc commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Concept ACK
Distinguishing “transaction not found” from internal read failures in TxIndex::FindTx/GetTransaction makes sense.

Comment thread src/rpc/rawtransaction.cpp Resolved

@musaHaruna musaHaruna left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK on separating expected outcomes ("transaction not found") from unexpected failures (I/O errors).

Comment thread test/functional/feature_io_errors.py Resolved

@l0rinc l0rinc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve just started reviewing this and wanted to clarify the overlap with #34132 before continuing.

The error handling here complements it, but there’s overlap in the index database reads: this PR throws on undecodable entries, while #34132 makes those failures fatal in CDBWrapper. It also removes TryRead(), which the legacy lookup here uses.

I don’t mind rebasing again if this lands first, but I’d like to clarify how we want these database-read failures handled so the two changes fit together.

Comment thread src/index/txindex.cpp Resolved
Comment thread doc/release-notes-35728.md Resolved
MarcoFalke added 3 commits November 2, 2026 07:51
…etTransaction

Currently, the FindTx return value is an optional, where nullopt
represents two different outcomes: I/O error and missing tx.

One may argue that this is fine in ProcessPSBT, because a -txindex
lookup error is rare, and an error where the fallbacks to lookup the tx
are also failing is even rarer. However, a -txindex corruption may also
point to a larger system corruption and it seems better to notify the
user properly by throwing an exception.

If I/O errors should be ignored in the future, it would be trivial to
catch them here. However, that decision should be up to the
caller, and not enforced by the low-level function.

Do the same for GetTransaction:
Currently the GetTransaction return value is a nullable transaction
pointer, where nullptr represents two different outcomes: I/O error and
missing tx.

Reporting a confirmed transaction as missing is wrong, so properly
return an error instead.
ProcessPSBT repeats the txindex and mempool lookups in GetTransaction.
Call GetTransaction instead, so the lookup and its error handling are
in one place.

GetTransaction checks the mempool before the txindex, so a prevout in
the mempool is now found without reading the index.

@furszy furszy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK fad49fd

try {
tx = GetTransaction(blockindex, node.mempool.get(), txid, chainman.m_blockman, hash_block);
} catch (const std::runtime_error&) {
if (blockindex) WITH_LOCK(::cs_main, CheckBlockDataAvailability(chainman.m_blockman, *blockindex, /*check_for_undo=*/false));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
if (blockindex) WITH_LOCK(::cs_main, CheckBlockDataAvailability(chainman.m_blockman, *blockindex, /*check_for_undo=*/false));
if (blockindex) WITH_LOCK(chainman.GetMutext(), CheckBlockDataAvailability(chainman.m_blockman, *blockindex, /*check_for_undo=*/false));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx, but I'll leave further nits for now, to limit the review churn :)

@DrahtBot
DrahtBot requested a review from ryanofsky November 2, 2026 15:59

@l0rinc l0rinc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK fad49fd

Only left some simplification nits, happy to re-ACK if you pick any of them up, but I’m also fine with merging as-is.

def test_blk_dat_io_error(self):
node = self.nodes[0]
self.restart_node(0, ["-txindex", "-rest"])
self.wait_until(lambda: node.getindexinfo()["txindex"]["synced"])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

faaf4a7 test: Add test to check RPCs throw on IO errors:

nit:

Suggested change
self.wait_until(lambda: node.getindexinfo()["txindex"]["synced"])
sync_txindex(self, node)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure sync_txindex has been removed by now. The line can just be removed

@ryanofsky ryanofsky Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re: #35728 (comment)

Pretty sure sync_txindex has been removed by now. The line can just be removed

Note: this is referring to #36618, which removed sync_txindex and made every node start and restart wait for index sync. After a rebase, the restart_node call above already waits, so this wait_until line is redundant but harmless. (This PR would need to be rebased to remove here.)

Comment thread test/functional/feature_io_errors.py Resolved
def test_getrawtransaction_block_data_errors(self):
node = self.nodes[0]
self.log.info("Test getrawtransaction with a header-only block")
block = create_block(tmpl=node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fa5ad60 test: Add test for getrawtransaction block read errors:

nit: generateblock(..., submit=False) could be used here:

Reuse the block-generation helper
diff --git a/test/functional/rpc_rawtransaction.py b/test/functional/rpc_rawtransaction.py
index 56492c660b..998114642a 100755
--- a/test/functional/rpc_rawtransaction.py
+++ b/test/functional/rpc_rawtransaction.py
@@ -18,6 +18,2 @@ from itertools import product
 
-from test_framework.blocktools import (
-    NORMAL_GBT_REQUEST_PARAMS,
-    create_block,
-)
 from test_framework.messages import (
@@ -101,7 +97,6 @@ class RawTransactionsTest(BitcoinTestFramework):
         self.log.info("Test getrawtransaction with a header-only block")
-        block = create_block(tmpl=node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS))
-        block.solve()
-        node.submitheader(block.serialize()[:80].hex())
+        block = self.generateblock(node, output="raw(55)", transactions=[], submit=False)
+        node.submitheader(block["hex"])
 
-        assert_raises_rpc_error(-1, "Block not available (not fully downloaded)", node.getrawtransaction, txid="a" * 64, blockhash=block.hash_hex)
+        assert_raises_rpc_error(-1, "Block not available (not fully downloaded)", node.getrawtransaction, txid="a" * 64, blockhash=block["hash"])
 

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx, will do if I have to re-touch

self.wait_until(lambda: node.getindexinfo()["txindex"]["synced"])
wallet = MiniWallet(node)

tx = wallet.send_self_transfer(from_node=node)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

faaf4a7 test: Add test to check RPCs throw on IO errors:

nit: similarly, send_self_transfer_chain(..., chain_length=2) seems simpler here

Reuse the existing transaction-chain helper
diff --git a/test/functional/feature_io_errors.py b/test/functional/feature_io_errors.py
index 3fff0ad44b..fff7345729 100755
--- a/test/functional/feature_io_errors.py
+++ b/test/functional/feature_io_errors.py
@@ -27,9 +27,8 @@ class IOErrorTest(BitcoinTestFramework):
         self.wait_until(lambda: node.getindexinfo()["txindex"]["synced"])
         wallet = MiniWallet(node)
 
-        tx = wallet.send_self_transfer(from_node=node)
+        tx, tx2 = wallet.send_self_transfer_chain(from_node=node, chain_length=2)
         txid = tx["txid"]
-        tx2 = wallet.send_self_transfer(from_node=node, utxo_to_spend=tx["new_utxo"])
         self.generate(node, 1)
         psbt = node.createpsbt(inputs=[{"txid": txid, "vout": 0}], outputs=[])
 

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx, will do if I have to re-touch

Comment thread src/index/txindex.cpp Resolved
if (!tx) {
tx = node.mempool->get(psbt_input.prev_txid);
}
uint256 block_hash;

ghost Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fa7d278 refactor: Use GetTransaction in ProcessPSBT:

Nit: was searching for where this is used - it's not, we could name it dummy_hash to make that clear.

ghost Nov 3, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx, could rename to dummy_block_hash if I have to re-touch, but I think it is fine, the scope ends in 4 lines anyway.

Comment thread test/functional/rpc_rawtransaction.py Resolved
Comment thread src/rpc/rawtransaction.cpp Resolved
Comment thread src/rpc/rawtransaction.cpp Resolved
Comment thread test/functional/feature_io_errors.py Resolved
@maflcko

ghost commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

(I'll leave the test nits for the future)

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK fad49fd

Reviewed the range-diff since my previous ACK at fa87067. The updated lookup skips leftover txindex entries referring to unknown blocks or non-active blocks without data. Missing active-chain data and failures to open or read a file still produce errors.

Checking block availability after GetTransaction fails also handles the pruning race: if the block was pruned before it could be read, the RPC reports that it was pruned instead of returning a generic I/O error.

The added REST error coverage, clearer diagnostics, formatting-helper cleanup, and release-note changes look good.

Happy to re-ACK if this is retouched, or to open a follow-up PR right after merge to address some of the remaining review comments.

@ryanofsky ryanofsky self-assigned this Nov 3, 2026

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK fad49fd. Since last review, WarnAndThrowErr takes the format string and arguments directly, and the release note wording is updated. Thanks!

Comment thread src/index/txindex.cpp Resolved
def test_blk_dat_io_error(self):
node = self.nodes[0]
self.restart_node(0, ["-txindex", "-rest"])
self.wait_until(lambda: node.getindexinfo()["txindex"]["synced"])

ghost Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re: #35728 (comment)

Pretty sure sync_txindex has been removed by now. The line can just be removed

Note: this is referring to #36618, which removed sync_txindex and made every node start and restart wait for index sync. After a rebase, the restart_node call above already waits, so this wait_until line is redundant but harmless. (This PR would need to be rebased to remove here.)

@maflcko

ghost commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

I rolled the test fixups into #36748 (along with other sync fixups I carried around). The other two remaining follow-ups would be:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants