Skip to content

fix(rpc): verify fetched mempool txids before caching and document trust assumption - #2316

Merged
evanlinjin merged 1 commit into
bitcoindevkit:masterfrom
tvpeter:fix/rpc-verify-mempool-txid
Oct 15, 2026
Merged

evanlinjin merged 1 commit into
bitcoindevkit:masterfrom
tvpeter:fix/rpc-verify-mempool-txid

Conversation

@tvpeter

@tvpeter tvpeter commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR adds a check in Emitter::mempool_at: when a mempool transaction is fetched with get_raw_transaction, its computed txid must match the txid that was requested before it goes into mempool_snapshot. This stops an RPC response with the wrong transaction body from being emitted or cached under the requested txid. It also adds module-level documentation stating that bdk_bitcoind_rpc assumes a connection to a trusted bitcoind node.

This addresses only the missing txid-validation part of #2282, following discussions on #2283.

Notes to the reviewers

  • On a mismatch the check returns the existing Error::UnexpectedStructure, so it is non-breaking. A single mismatch fails the whole poll; callers retry on the next call.
  • Only freshly fetched transactions are checked. Entries already in mempool_snapshot were checked when they were inserted.
  • A txid does not commit to witness data, so a transaction served with altered witnesses is not detected. This is documented on mempool_at.
  • Out of scope / possible follow-up: next_block does not verify that the returned block matches the requested hash, merkle root, or witness commitment.
  • The test uses a mock RpcApi that implements only call. serde/serde_json come from the bitcoincore_rpc::jsonrpc re-exports, so no new dependencies are added.

Changelog notice

  • Emitter::mempool/mempool_at: when a mempool transaction is fetched from the node, check that its computed txid matches the requested txid. A mismatch is rejected with Error::UnexpectedStructure.
  • Document that bdk_bitcoind_rpc assumes a trusted bitcoind RPC connection.

Checklists

All Submissions:

New Features:

  • I've added tests for the new feature
  • I've added docs for the new feature

Bugfixes:

  • This pull request breaks the existing API
  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

@tvpeter
tvpeter requested a review from evanlinjin as a code owner September 16, 2026 07:45
@tvpeter tvpeter changed the title fix(rpc): verify fetched mempool tx txids and document trust assumption fix(rpc): verify fetched mempool txids before caching and document trust assumption Sep 16, 2026
@codecov

codecov Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 78.88%. Comparing base (e417c43) to head (0d6fb41).
⚠️ Report is 15 commits behind head on master.

Files with missing lines Patch % Lines
crates/bitcoind_rpc/src/lib.rs 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2316      +/-   ##
==========================================
+ Coverage   78.84%   78.88%   +0.03%     
==========================================
  Files          31       31              
  Lines        6060     6061       +1     
  Branches      288      289       +1     
==========================================
+ Hits         4778     4781       +3     
+ Misses       1203     1201       -2     
  Partials       79       79              
Flag Coverage Δ
rust 78.88% <66.66%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tvpeter

tvpeter commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

The MSRV job is failing because of a transitive dependency yoke-derive (via esplora-client -> reqwest -> url -> idna-> icu -> yoke -> yoke-derive) that was updated yesterday (2026-09-15) to 0.8.3 and requires Rustc 1.87 to build. So we can either pin it to 0.8.2 in pin-msrv or bump the MSRV to 1.87.

Happy to open a tiny PR for the pin if that's preferred.

@evanlinjin

Copy link
Copy Markdown
Contributor

@tvpeter I also experienced the CI failure and opened this PR: #2317

@tvpeter
tvpeter force-pushed the fix/rpc-verify-mempool-txid branch from 2141610 to 3fba250 Compare September 18, 2026 12:10

@Dmenec Dmenec 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.

Approach ACK

I was thinking if the validation should retry (maybe 3 times at most) when failing so you don't throw the whole poll, but I think in this specific scenario it would be more annoying than useful.

Not sure if compute_txid cover the witness, so maybe a node could still serve a different witness for the same txid? If not, maybe worth a word in the doc so it doesn't read as the whole tx being verified.

Also, checking the code I noticed that next_block fetches blocks by hash but never checks the returned block actually hashes to what was requested:

pub fn next_block(&mut self) -> Result<Option<BlockEvent<Block>>, bitcoincore_rpc::Error> {
if let Some((checkpoint, block)) = poll(self, move |hash, client| client.get_block(hash))? {
// Stop tracking unconfirmed transactions that have been confirmed in this block.
for tx in &block.txdata {
self.mempool_snapshot.remove(&tx.compute_txid());
}
return Ok(Some(BlockEvent { block, checkpoint }));
}
Ok(None)
}

I think in the same way we should check the block hash, merkle root and witness commitment. Checking only the hash isn't enough, since it only covers the header, so a node could serve the genuine header with forged txs. I saw your benchmarks in #2283 so, as much, it should be a few ms per block. Given the scope was narrowed there, maybe better as a follow-up?

Also, just a few nits I've seen through

Comment thread crates/bitcoind_rpc/src/lib.rs Outdated
Ok(())
}

//A fetched mempool tx whose body does not match the requested txid is rejected.

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.

Suggested change
//A fetched mempool tx whose body does not match the requested txid is rejected.
// A fetched mempool tx whose body does not match the requested txid is rejected.

Comment thread crates/bitcoind_rpc/src/lib.rs Resolved
Comment thread crates/bitcoind_rpc/src/lib.rs Resolved
Comment thread crates/bitcoind_rpc/Cargo.toml Outdated
Comment on lines +27 to +28
serde = "1"
serde_json = "1"

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.

nit: these deps aren't needed, bitcoincore_rpc already re-exports both. In the test module this should be enough

use bitcoincore_rpc::jsonrpc::{serde, serde_json};

@notmandatory
notmandatory removed their request for review September 23, 2026 17:00
- Add `tx.compute_txid() == txid` check to validate that fetched raw
mempool transaction hashes against their requested txid before caching
in Emitter::mempool_at. Returns `Error::UnexpectedStructure` for a
mismatch.

- Add a regression test for the above check.

- Document that the crate assumes a trusted `bitcoind` connection, and
that the txid check does not cover witness data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@evanlinjin
evanlinjin force-pushed the fix/rpc-verify-mempool-txid branch from 3fba250 to 0d6fb41 Compare October 15, 2026 03:30
@evanlinjin
evanlinjin requested a review from a team as a code owner October 15, 2026 03:30
@evanlinjin evanlinjin moved this to Needs Review in BDK Chain Oct 15, 2026
@evanlinjin evanlinjin added this to the Chain 0.24.0 milestone Oct 15, 2026

@evanlinjin evanlinjin 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 0d6fb41

@evanlinjin
evanlinjin merged commit bc9e873 into bitcoindevkit:master Oct 15, 2026
18 of 19 checks passed
@github-project-automation github-project-automation Bot moved this from Needs Review to Done in BDK Chain Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants