Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bdk_electrum: Handle negative heights properly #1837

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

evanlinjin
Copy link
Member

@evanlinjin evanlinjin commented Feb 16, 2025

Description

This fixes a problem with bdk_electrum making bogus transaction_get_merkle calls to the Electrum server.

In electrum, heights returned alongside txs may be 0 or -1. 0 means the tx is unconfirmed. -1 means the tx is unconfirmed and spends an unconfirmed tx.

Previously, the codebase assumed that heights cannot be negative and used a i32 as usize cast (which would lead to the casted height being 18446744073709551615). Then the codebase will try to call transaction_get_merkle with the overflowed height.

Notes to the reviewers

The test introduced in this PR does not fail with the old codebase. What ends up happening is that transaction_get_merke will be called with the overflow height and the Electrum server will return with "merkle not found".

To see the failure, you can change the height as usize casts to use .try_into().expect() then you will see a panic.

These changes should be applied as 1.0.1 and 1.1.1 fixes.

Changelog notice

  • Fix bdk_electrum handling of negative spk-history height.

Checklists

All Submissions:

  • I've signed all my commits
  • I followed the contribution guidelines
  • I ran cargo fmt and cargo clippy before committing

Bugfixes:

  • I've added tests to reproduce the issue which are now passing

* [ ] This pull request breaks the existing API
* [ ] I'm linking the issue being fixed by this PR

In electrum, heights returned alongside txs may be 0 or -1. 0 means the
tx is unconfirmed. -1 means the tx is unconfirmed and spends an
unconfirmed tx.

Previously, the codebase assumed that heights cannot be negative and
used a `i32 as usize` cast (which would lead to panic if the i32 is
negative).
@evanlinjin evanlinjin force-pushed the fix/electrum_negative_height branch from d300ba4 to ada073b Compare February 16, 2025 05:49
Is an spk history contains a tx that spends another unconfirmed tx, the
Electrum API will return the tx with a negative height. This should
succeed and not panic.
@evanlinjin evanlinjin marked this pull request as ready for review February 16, 2025 06:01
@evanlinjin evanlinjin self-assigned this Feb 16, 2025
@evanlinjin evanlinjin added the bug Something isn't working label Feb 16, 2025
@evanlinjin evanlinjin added this to the 2.0.0 milestone Feb 16, 2025
Copy link
Contributor

@LLFourn LLFourn left a comment

Choose a reason for hiding this comment

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

Nice work. Especially with the test.

tx_update,
tx_res.tx_hash,
tx_res.height.try_into().expect("must not overflow"),
)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

⛏️ We can just match on the try into result and branch based on that to avoid the unwrap/expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Needs Review
Development

Successfully merging this pull request may close these issues.

2 participants