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

change(mempool): Contextually validates mempool transactions in best chain #5716

Merged
merged 28 commits into from
Nov 30, 2022

Conversation

arya2
Copy link
Contributor

@arya2 arya2 commented Nov 24, 2022

Motivation

Mempool transactions collected into a block template need to be contextually valid in the best chain to produce a block template that's contextually valid on the best chain.

Closes #5376

Solution

  • Add check::nullifiers::no_duplicates_in_chain that searches the non-finalized state as well as the finalized state for duplicate nullifiers
  • Update check::anchors fns and errors accommodate transactions without a PreparedBlock
  • Add new state request to check that the anchors and nullifiers of a transaction are valid on the best chain
  • Use the new state request in the transactions' verifier for mempool transactions

Review

This PR is part of regular scheduled work.

Reviewer Checklist

  • Will the PR name make sense to users?
    • Does it need extra CHANGELOG info? (new features, breaking changes, large changes)
  • Are the PR labels correct?
  • Does the code do what the ticket and PR says?
  • How do you know it works? Does it have tests?

- check::anchors fn for tx iter
- TODO comments for unifying nullifiers and anchors checks
- new state request

Updates unknown anchor errors to accomodate tx-only check

Calls new state fn from transaction verifier
updates TransactionContextualValidity request to check sprout anchors

adds comment mentioning TransactionContextualValidity ignores UTXOs
@arya2 arya2 added C-bug Category: This is a bug A-consensus Area: Consensus rule updates A-rust Area: Updates to Rust code P-Medium ⚡ A-state Area: State / database changes labels Nov 24, 2022
@arya2 arya2 requested review from a team as code owners November 24, 2022 19:53
@arya2 arya2 requested review from teor2345 and removed request for a team November 24, 2022 19:53
@github-actions github-actions bot added the C-enhancement Category: This is an improvement label Nov 24, 2022
@arya2 arya2 self-assigned this Nov 24, 2022
@teor2345 teor2345 removed the request for review from a team November 24, 2022 20:03
Copy link
Contributor

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

Looks great, seems like it should work!

Since the mempool currently has a lot of very large transactions, we need to be careful of performance. I've also made some suggestions for improving various APIs so they are easier to call correctly.

I'd like to see some unit tests for the new and modified consensus-critical checks.

@codecov
Copy link

codecov bot commented Nov 25, 2022

Codecov Report

Merging #5716 (a01ab07) into main (63124ba) will increase coverage by 0.12%.
The diff coverage is 53.12%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5716      +/-   ##
==========================================
+ Coverage   78.67%   78.79%   +0.12%     
==========================================
  Files         306      306              
  Lines       38552    38676     +124     
==========================================
+ Hits        30331    30475     +144     
+ Misses       8221     8201      -20     

- comments / docs
- a TransactionError variant for ValidateContextError
Copy link
Contributor

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

Looks good, I just have some questions and ideas around error handling.

Good catch with the expensive Sprout checks!

@arya2 arya2 requested a review from a team as a code owner November 29, 2022 00:45
@arya2 arya2 force-pushed the mempool-context-valid branch from 388a3f4 to 513b811 Compare November 29, 2022 01:00
Copy link
Contributor

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

Looking good, I don't mind if we do the joinsplit parallelisation in a different PR.

(But it's important to do soon, because it is a denial of service risk.)

Co-authored-by: teor <teor@riseup.net>
@arya2
Copy link
Contributor Author

arya2 commented Nov 29, 2022

Any idea what "the trait bound zcash_history::Error: Clone is not satisfied" was about?

Worked fine locally. 😢

@teor2345
Copy link
Contributor

Any idea what "the trait bound zcash_history::Error: Clone is not satisfied" was about?

Worked fine locally. 😢

As far as I can see, zcash_history::Error does not impl Clone:
https://docs.rs/zcash_history/latest/zcash_history/enum.Error.html
https://github.com/zcash/librustzcash/blob/580f87dc3ec5d12a449b437a0358228b0fe936f3/zcash_history/src/lib.rs#L20-L26

@teor2345
Copy link
Contributor

Feel free to wrap it in an Arc.

Copy link
Contributor

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

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

Thanks, looks good!

@arya2
Copy link
Contributor Author

arya2 commented Nov 30, 2022

As far as I can see, zcash_history::Error does not impl Clone

Well, that would do it. I wonder how this happened:
image

Oh well.

mergify bot added a commit that referenced this pull request Nov 30, 2022
@mergify mergify bot merged commit eb0a2ef into main Nov 30, 2022
@mergify mergify bot deleted the mempool-context-valid branch November 30, 2022 04:40
teor2345 added a commit that referenced this pull request Feb 6, 2023
…chain (#5716) - manual conflicts with getblocktemplate-rpcs

* updates comments

* adds check nullifier no dup fns for transactions

* Adds:
- check::anchors fn for tx iter
- TODO comments for unifying nullifiers and anchors checks
- new state request

Updates unknown anchor errors to accomodate tx-only check

Calls new state fn from transaction verifier

* updates check::anchors fns to use transactions

updates TransactionContextualValidity request to check sprout anchors

adds comment mentioning TransactionContextualValidity ignores UTXOs

* conditions new state req call on is_mempool

updates tests

* fix doc link / lint error

* checks for duplicate nullifiers with closures

* Update zebra-state/src/service/check/nullifier.rs

Co-authored-by: teor <teor@riseup.net>

* documents find_duplicate_nullifier params

moves if let statement into for loop

* renames new state req/res

* asserts correct response variant in tx verifier

* adds CheckBestChainTipShieldedSpends call in tx verifier to async checks

* re-adds tracing instrumentation to check::anchors fn

renames transaction_in_state to transaction_in_chain

* adds block/tx wrapper fns for anchors checks

* uses UnminedTx instead of transaction.hash()

deletes broken test

* updates new state req/res name

* updates tests and uses par_iter for anchors checks

* Updates check::anchors pub fn docs.

* Adds:
- comments / docs
- a TransactionError variant for ValidateContextError

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

* moves downcast to From impl

rustfmt

* moves the ValidateContextError into an Arc

updates comments and naming

* leaves par_iter for another PR

* puts io::Error in an Arc

* updates anchors tests to call tx_anchors check

* updates tests to call tx_no_duplicates_in_chain

slightly improves formatting

* Update zebra-consensus/src/error.rs

Co-authored-by: teor <teor@riseup.net>

* moves Arc from HistoryError to ValidateContextError

Co-authored-by: teor <teor@riseup.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-consensus Area: Consensus rule updates A-rust Area: Updates to Rust code A-state Area: State / database changes C-bug Category: This is a bug C-enhancement Category: This is an improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do contextual validation on mempool transactions using the state best tip
2 participants