Skip to content

Non-Wallet Tx verification doesn't work for RPC backend #498

Closed

Description

@rajarshimaitra

Describe the bug
Recently @LLFourn described a possible problem with updated sync logic in verify_tx logic in dev discord https://discord.com/channels/753336465005608961/753367451319926827/917624709426855946. I verified the issue exists currently even without the updated sync logic in rpc blockchain too.

verify_tx doesn't work for non wallet transactions with a rpc blockhain without txindex.

This happens because get_raw_transaction here

bdk/src/blockchain/rpc.rs

Lines 327 to 329 in 64e88f0

fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
Ok(Some(self.client.get_raw_transaction(txid, None)?))
}
will return error if the transaction is not wallet related.

To Reproduce
This can be reproduced with the test code added here
https://github.com/rajarshimaitra/bdk-example/blob/13097bab3f7742e8cc46c9b4b78be0fd7d44a315/src/main.rs#L276-L335

with the following error message

running 1 test
thread 'test::test_transaction_validation' panicked at 'called `Result::unwrap()` on an `Err` value: Global(Rpc(JsonRpc(Rpc(RpcError { code: -5, message: "No such mempool transaction. Use -txindex or provide a block hash to enable blockchain transaction queries. Use gettransaction for wallet transactions.", data: None }))))', src/main.rs:335:63
stack backtrace:

Expected behavior
verify_tx should verify any transaction, not only just wallet txs.

Possible recourse
We should enforce txindex to be used in rpc backend when using verify feature.

Activity

  1. added
    bug
    on Dec 12, 2021
  2. LLFourn commented on Dec 14, 2021

    @LLFourn
    Collaborator

    So on the call I think we agreed the solution here is not to verify the transactions on the rpc backend.

    This bit of code has to be removed

    bdk/src/wallet/mod.rs

    Lines 1540 to 1555 in 64e88f0

    #[cfg(feature = "verify")]
    {
    debug!("Verifying transactions...");
    for mut tx in self.database.borrow().iter_txs(true)? {
    if !tx.verified {
    verify::verify_tx(
    tx.transaction.as_ref().ok_or(Error::TransactionNotFound)?,
    self.database.borrow().deref(),
    &self.client,
    )?;
    tx.verified = true;
    self.database.borrow_mut().set_tx(&tx)?;
    }
    }
    }

    and instead each backend that benefits from verification (esplora, electrum) should verify the transactions as they come in during sync. Furthermore the verified field on TransactionDetails can be removed (this will be a non-breaking DB change hopefully).

  3. rajarshimaitra commented on Dec 15, 2021

    @rajarshimaitra
    ContributorAuthor

    and instead each backend that benefits from verification (esplora, electrum) should verify the transactions as they come in during sync.

    Maybe I am missing something and forgot to ask this in the meeting, can you elaborate on whats the difference between electrum and esplora from rpc, in context of verification? They also have a bitcoin core backend running right? So transactions will be consensus verified in them too automatically?

    Also in that case of refactoring, should verify_tx still be an public open function? or it will be something only internal to the syncs?

    Another related thought, I feel like there's value in exposing the bitcoinconsensus through our API. There might be downstream usecases when dev needs to check for verification of custom transactions. There is no other exposed API wrapper around bitcoinconsensus as far as I know in the eco. I so feel like its important to keep the API live. Maybe make it an wallet API?

  4. added
    discussion
    and removed
    bug
    on Dec 15, 2021
  5. notmandatory commented on Dec 15, 2021

    @notmandatory
    Contributor

    Maybe I am missing something and forgot to ask this in the meeting, can you elaborate on whats the difference between electrum and esplora from rpc, in context of verification? They also have a bitcoin core backend running right? So transactions will be consensus verified in them too automatically?

    I think what @LLFourn meant is that if you're using rpc then it must be on your localhost and therefore you implicitly trust what ever transaction data you're getting. But for electrum or esplora you may be connecting to someone else's hosted service (ie. blockstream) and in that case it's a good idea to verify the transaction data.

  6. rajarshimaitra commented on Dec 16, 2021

    @rajarshimaitra
    ContributorAuthor

    Ah ok, yes that makes sense. And I agree, verification is a costly operation and we should avoid that whenever possible.

  7. rajarshimaitra commented on Dec 16, 2021

    @rajarshimaitra
    ContributorAuthor

    I am working on a PR for this.

  8. rajarshimaitra commented on Jan 9, 2022

    @rajarshimaitra
    ContributorAuthor

    Closing after #502

  9. added 2 commits that reference this issue on Feb 22, 2022
  10. added a commit that references this issue on Aug 2, 2022
  11. added a commit that references this issue on Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions