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

fix(electrum): Fix fetch_prev_txout #1443

Merged
merged 1 commit into from
May 15, 2024

Conversation

ValuedMammal
Copy link
Contributor

@ValuedMammal ValuedMammal commented May 14, 2024

Previously we inserted every TxOut of a previous tx at the same outpoint, which is incorrect because an outpoint only points to a single TxOut. Now just get the TxOut corresponding to the txin prevout and insert it with its outpoint.

Notes to the reviewers

The bug in question was demonstrated in a discord comment https://discord.com/channels/753336465005608961/1239693193159639073/1239704153400414298 but I don't think we've opened an issue yet. Essentially, because of a mismatch between the outpoint and txout stored in TxGraph, we weren't summing the inputs correctly which caused calculate_fee to fail with NegativeFee error.

fixes #1419

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

Previously we inserted every TxOut of a previous tx
at the same outpoint, which is incorrect because an outpoint
only points to a single TxOut. Now just get the TxOut
corresponding to the txin prevout and insert it with
its outpoint.
@notmandatory notmandatory added the bug Something isn't working label May 14, 2024
@notmandatory notmandatory added this to the 1.0.0-alpha milestone May 14, 2024
@notmandatory
Copy link
Member

notmandatory commented May 14, 2024

This looks like the right solution but Is it possible to create a test that fails before the change and passes after? I'd like to make sure an issue like this doesn't sneak in again.

@notmandatory
Copy link
Member

notmandatory commented May 14, 2024

Would it also make sense to fix TxGraph::insert_txout() to not insert duplicates? Since it's a public function I'm worried end users could make the same mistake and insert duplicate txouts and get the same miscalculation. The real problem may be deeper down in the TxGraph::apply_update() call which I believe should prevent creating duplicates in the TxGraph.txs map.

@ValuedMammal
Copy link
Contributor Author

As I understand the behavior of TxGraph, only the first txout inserted will be kept and attempting to insert another one at the same outpoint just returns an empty changeset. In the integration test for electrum the outpoint we're looking for will always have a vout of 0 (they are coinbase txs), so calculating the fee of the received tx happens to work by sheer luck. A more robust test would involve controlling the utxo spent by Core and in which order it appears in the tx which is posing more of a challenge.

@evanlinjin
Copy link
Member

evanlinjin commented May 15, 2024

Damn this is a huge oversight by @LagginTimes and myself. Thanks for taking this on.

@LagginTimes
Copy link
Contributor

LagginTimes commented May 15, 2024

ACK af15ebb

@evanlinjin
Copy link
Member

We definitely need more tests for this. We can add these tests in subsequent PRs.

I propose a separate test specific to testing fee calculation that takes into account what @ValuedMammal mentioned in #1443 (comment):

In the integration test for electrum the outpoint we're looking for will always have a vout of 0 (they are coinbase txs), so calculating the fee of the received tx happens to work by sheer luck. A more robust test would involve controlling the utxo spent by Core and in which order it appears in the tx which is posing more of a challenge.

Copy link
Member

@evanlinjin evanlinjin left a comment

Choose a reason for hiding this comment

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

ACK af15ebb

@evanlinjin evanlinjin merged commit 2f059a1 into bitcoindevkit:master May 15, 2024
12 checks passed
@ValuedMammal ValuedMammal deleted the fix/electrum-prevout branch May 15, 2024 13:57
@notmandatory notmandatory mentioned this pull request May 23, 2024
33 tasks
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
Archived in project
Development

Successfully merging this pull request may close these issues.

Unable to calculate_fee when syncing with electrum
4 participants