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

add(mempool): Verify orphaned mempool transactions #8857

Open
wants to merge 60 commits into
base: main
Choose a base branch
from

Conversation

arya2
Copy link
Contributor

@arya2 arya2 commented Sep 9, 2024

Motivation

We want to verify transactions in the mempool that spend the transparent outputs of other transactions in the mempool.

Closes #8777.

Solution

  • Adds a handle to the mempool service in the transaction verifier
  • Adds AwaitOutput request in the mempool service
  • Updates the spent_utxos() method in the transaction verifier to try querying the mempool for spent outpoints before returning a TransparentInputNotFound error.
  • Polls the mempool service from the transaction verifier after verifying a mempool transaction that creates new UTXOs (in case they're spent by other mempool transactions that are still being verified)
  • Updates getblocktemplate method and ZIP-317 transaction selection to include transactions that spent the outputs of other transactions in the mempool when all of their dependencies have been selected

Tests

Added tests to check that:

  • Inserting a transaction also updates the transaction dependencies tracked in the verified set
  • The Storage.remove() method works as expected (the rest of evict_one() should be covered already)
  • The mempool responds to AwaitOutput requests when the output for the queried outpoint is:
    • Added to the mempool
    • Already available in the mempool
  • The transaction verifier:
    • Calls the mempool with AwaitOutput requests as expected and processes UnspentOutput responses correctly
    • Polls the mempool after verifying a mempool transaction that creates new transparent outputs
    • Includes all of the expected spent_mempool_outpoints in its response
  • Long poll ids remaining consistent as long the mempool contents are unchanged (and everything else is unchanged)
  • ZIP-317 mempool transaction selection excludes transactions that have dependencies unless their dependencies have already been selected

If needed, this PR could also add tests for:

  • ZIP-317 mempool transaction selection always returns the right order of transactions (this could be unnecessary since they’re sorted afterwards anyway but it could be a good sanity check)
  • The getblocktemplate method returns transactions with dependencies in the right order when the transaction hashes would have them sorted in the wrong order
  • Ideally at least a basic acceptance test that checks that it all works via RPCs

Follow Up Work

The mempool's Downloads sends a result to the response channel too early, it should wait until the transaction is being inserted into the mempool's verified set.

PR Author's Checklist

  • The PR name will make sense to users.
  • The PR provides a CHANGELOG summary.
  • The solution is tested.
  • The documentation is up to date.
  • The PR has a priority label.

PR Reviewer's Checklist

  • The PR Author's checklist is complete.
  • The PR resolves the issue.

… mempool setup argument, adds and uses an `init_test()` fn for passing a closed channel receiver in tests where no mempool service is needed in the transaction verifier.
…nd a `new_for_tests()` constructor for convenience)
…nsparent outputs has been verified.

adds a TODO for adding a `pending_outputs` field to the mempool Storage
…ng outputs requests when inserting new transactions into the mempool's verified set
@arya2 arya2 added A-mempool Area: Memory pool transactions P-Medium ⚡ labels Sep 9, 2024
@arya2 arya2 self-assigned this Sep 9, 2024
@github-actions github-actions bot added C-trivial Category: A trivial change that is not worth mentioning in the CHANGELOG labels Sep 9, 2024
@arya2 arya2 requested review from a team as code owners September 27, 2024 01:10
@arya2 arya2 requested review from oxarbitrage and removed request for a team September 27, 2024 01:10
@arya2 arya2 added A-consensus Area: Consensus rule updates A-concurrency Area: Async code, needs extra work to make it work properly. labels Sep 27, 2024
@arya2 arya2 requested a review from upbqdn September 30, 2024 22:04
@mpguerra mpguerra requested review from conradoplg and removed request for a team, oxarbitrage and upbqdn October 10, 2024 10:35
@oxarbitrage oxarbitrage added the do-not-merge Tells Mergify not to merge this PR label Oct 10, 2024
Copy link
Contributor

@conradoplg conradoplg left a comment

Choose a reason for hiding this comment

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

I'm still reviewing but I added a comment

Comment on lines 124 to 138
tip_rejected_exact: HashMap<UnminedTxId, ExactTipRejectionError>,
tip_rejected_exact: HashMap<transaction::Hash, ExactTipRejectionError>,
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the reason for this change? The semantics of tip_rejected_exact is for it to incude transactions that have failed to validate e.g. due to an invalid signature. If this is changed to a Hash, then a transaction with e.g. the same data but a valid signature would be rejected when it shouldn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is the reason for this change?

I think the broader change to mined ids was to avoid iterating through the verified set to find the auth digest when adding transaction dependencies (since OutPoint only includes the mined id).

The change to tip_rejected_exact was a mistake, it would work fine for the sendrawtransaction RPC, but I see it would incorrectly reject gossiped transaction ids when it should try to download and verify them.

Restored its type to the full UnminedTxId in ce6d169.

pub fn insert(
&mut self,
tx: VerifiedUnminedTx,
spent_mempool_outpoints: Vec<transparent::OutPoint>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Please document the new argument

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for catching that, documented in cc76b40.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-concurrency Area: Async code, needs extra work to make it work properly. A-consensus Area: Consensus rule updates A-mempool Area: Memory pool transactions C-trivial Category: A trivial change that is not worth mentioning in the CHANGELOG do-not-merge Tells Mergify not to merge this PR P-Medium ⚡
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature: support orphan transactions in the mempool
4 participants