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 coinbase height deserialization #3129

Merged
merged 11 commits into from
Dec 5, 2021

Conversation

oxarbitrage
Copy link
Contributor

Motivation

In height deserialization we are allowing more than the spec. This will close #2226 if merged.

Specifications

A coinbase transaction for a block at block height greater than 0 MUST have a script that, as its first item, encodes the block height height as follows.

For height in the range {1 .. 16}, the encoding is a single byte of value 0x50 + height.

Otherwise, let heightBytes be the signed little-endian representation of height, using the minimum nonzero number of bytes such that the most significant byte is < 0x80. The length of heightBytes MUST be in the range {1 .. 5}. Then the encoding is the length of heightBytes encoded as one byte, followed by heightBytes itself.

This matches the encoding used by Bitcoin in the implementation of [BIP-34] (but the description here is to be considered normative).

Solution

Constrain the decoding by block height ranges. Added some tests.

Review

@teor2345 is probably the most familiar with this but anyone can take a look.

Reviewer Checklist

  • Code implements Specs and Designs
  • Tests for Expected Behaviour
  • Tests for Errors

Copy link
Collaborator

@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, but I think a few more tests would help.

zebra-chain/src/transparent/serialize.rs Outdated Show resolved Hide resolved
zebra-chain/src/transparent/tests/vectors.rs Show resolved Hide resolved
zebra-chain/src/transparent/tests/prop.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

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

Almost there, we just need to cover all the possible encoded byte formats in the tests.

zebra-chain/src/transparent/tests/vectors.rs Show resolved Hide resolved
zebra-chain/src/transparent/tests/vectors.rs Show resolved Hide resolved
zebra-chain/src/transparent/tests/prop.rs Outdated Show resolved Hide resolved
zebra-chain/src/transparent/tests/prop.rs Show resolved Hide resolved
@teor2345 teor2345 enabled auto-merge (squash) December 5, 2021 22:37
Copy link
Collaborator

@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, thanks for writing all those tests!

@teor2345 teor2345 merged commit 9416729 into ZcashFoundation:main Dec 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Require minimal height encodings during deserialization
2 participants