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 ZIP-0244 TxId Digest support #2129

Merged
merged 9 commits into from
Jul 6, 2021
Merged

Add ZIP-0244 TxId Digest support #2129

merged 9 commits into from
Jul 6, 2021

Conversation

conradoplg
Copy link
Collaborator

@conradoplg conradoplg commented May 10, 2021

Motivation

ZIP-0244 specifies a new Transaction ID Digest which the hash of a "tree" of hashes from specific fields of the transaction, rather than the hash of the entire transaction itself.

Solution

This uses librustzcash to compute the digest. In order to call it, the transaction is re-serialized and deserialized with librustzcash.

While the implementation is done, it still has this pending issues:

  • The test still fails because librustzcash support for ZIP-0244 is not finished yet. Should be finished soon. (The tests of this PR won't pass until that)
  • Only a single test is done, which does not use Orchard data. This is because zebra does not parse Orchard data yet. However, a list of test vectors is already included. (The tests of this PR will pass even if that's not ready, but they'll need a simple change after Orchard support is added, in order to also test with Orchard test vectors)

The code in this pull request has:

  • Documentation Comments
  • Unit Tests and Property Tests

Review

@teor2345 already reviewed the draft so I'm assuming they'll do the final review

Related Issues

Closes #2050

Follow Up Work

ZIP-0244 also specifies a new sighash and a new authorizing data digest, which need to be implemented too.

@teor2345 teor2345 self-requested a review May 10, 2021 21:48
zebra-chain/Cargo.toml Outdated Show resolved Hide resolved
zebra-chain/src/transaction/txidhash.rs Outdated Show resolved Hide resolved
zebra-chain/src/transaction/txidhash.rs Outdated Show resolved Hide resolved
zebra-test/src/zip0244.rs Outdated Show resolved Hide resolved
zebra-chain/src/transaction/txidhash.rs Outdated Show resolved Hide resolved
zebra-chain/src/transaction/hash.rs Outdated Show resolved Hide resolved
zebra-chain/Cargo.toml Outdated Show resolved Hide resolved
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.

This looks really good!

I made a few suggestions, but overall the structure looks like what we'd want here. (And I'm glad it works.)

Let's also add some documentation comments:

  • Add a short summary to the top of each module
  • Explain the purpose of every new object
  • Explain what each method does, and each method argument

Doc comments don't need to be long, 1-2 sentences is enough. Check out some of the other modules for examples.

And let's add some more tests:

  • deserialization and serialization round-trip for zcash_primitives::transaction::Transaction::read using fake v5 transactions
    • this test helps us make sure our serialization works the same as librustzcash
    • for an example, see
      /// Do a round-trip test on fake v5 transactions created from v4 transactions
      /// in the block test vectors.
      ///
      /// Covers Sapling only, Transparent only, and Sapling/Transparent v5
      /// transactions.
      #[test]
      fn fake_v5_round_trip() {
  • deserialization and serialization round-trip for zcash_primitives::transaction::Transaction::read using an empty v5 transaction
    • we might want to turn the empty transaction struct into a lazy_static! constant, then use it in both tests
    • for an example, see
      /// An empty transaction v5, with no Orchard, Sapling, or Transparent data
      ///
      /// empty transaction are invalid, but Zebra only checks this rule in
      /// zebra_consensus::transaction::Verifier
      #[test]
      fn empty_v5_round_trip() {

zcash_primitives might reject empty transactions - if that's the case, let's skip that test.

@conradoplg
Copy link
Collaborator Author

@teor2345 I believe I've made all the requested changes. The new tests share a lot of code with the older ones, though. Do you think it warrants some refactoring?

I'm also considering moving the reserialization code (get branch ID from tx, convert to librustzcash enum, call librustzcash read) to a separate function, since it's repeated a lot in the tests now.

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.

This looks good so far, let's take it out of draft once you've done the refactoring.

@conradoplg conradoplg marked this pull request as ready for review May 12, 2021 17:28
@conradoplg
Copy link
Collaborator Author

Finished refactoring and took it out of draft.

@conradoplg conradoplg requested a review from teor2345 May 12, 2021 17:32
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 had one suggestion to future-proof the code.

Looking forward to the librustzcash update for transaction IDs, so we can test this code properly.

@teor2345 teor2345 mentioned this pull request May 19, 2021
2 tasks
@dconnolly dconnolly added the A-rust Area: Updates to Rust code label May 20, 2021
@conradoplg
Copy link
Collaborator Author

Solved conflicts and updated test to not skip Orchard transactions now that we parse them.

The only thing left is the test to pass. It's possibly a librustzcash issue which we decided to not investigate yet since their implementation is still under review.

@conradoplg
Copy link
Collaborator Author

This is finally ready 🎉

I had to implement Arbitrary in some types to make the tests pass since librustzcash validates the fields when computing the txid and we can no longer generate invalid public keys, etc.

However I'm not sure if my approach is correct, since proptest advises against using prop_filter but I'm not sure how bad it is in this particular case. Feedback is welcome.

These changes are in a seprate commit (b7bda55) to make reviewing them easier

@teor2345 teor2345 dismissed their stale review June 30, 2021 23:37

The requested changes have been made

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.

I made some specific suggestions about proptests and test vectors. But they are all optional.

I'd like @dconnolly or @jvff to do the final review here. Whoever is available after this PR is revised.

zebra-test/src/zip0244.rs Show resolved Hide resolved
zebra-chain/src/orchard/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/sapling/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/sapling/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/sapling/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/transaction/tests/vectors.rs Show resolved Hide resolved
Copy link
Contributor

@jvff jvff 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 did add some suggestions and nit-picks 😅

zebra-chain/src/orchard/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/sapling/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/transaction/txidhash.rs Outdated Show resolved Hide resolved
zebra-chain/src/transaction/hash.rs Outdated Show resolved Hide resolved
zebra-chain/src/transaction/tests/vectors.rs Outdated Show resolved Hide resolved
zebra-chain/src/transaction/tests/vectors.rs Outdated Show resolved Hide resolved
conradoplg and others added 4 commits July 2, 2021 11:10
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
Copy link
Collaborator Author

@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.

Another round of fixes, thanks for the great suggestions!

zebra-chain/src/transaction/hash.rs Outdated Show resolved Hide resolved
zebra-chain/src/transaction/hash.rs Outdated Show resolved Hide resolved
zebra-chain/src/transaction/tests/vectors.rs Outdated Show resolved Hide resolved
zebra-chain/src/orchard/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/sapling/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/sapling/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/sapling/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/sapling/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/transaction/tests/vectors.rs Outdated Show resolved Hide resolved
zebra-chain/src/transaction/txidhash.rs Outdated Show resolved Hide resolved
jvff
jvff previously approved these changes Jul 2, 2021
Copy link
Contributor

@jvff jvff 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! Just one optional suggestion.

zebra-chain/src/transaction/tests/vectors.rs Outdated Show resolved Hide resolved
zebra-chain/src/sapling/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/sapling/arbitrary.rs Outdated Show resolved Hide resolved
Co-authored-by: Janito Vaqueiro Ferreira Filho <janito.vff@gmail.com>
Copy link
Contributor

@dconnolly dconnolly left a comment

Choose a reason for hiding this comment

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

Love it! I resolved a lot of conversations too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rust Area: Updates to Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ZIP-244: Implement Non-Malleable transaction ids
4 participants