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

Implement Expanded to Compact Difficulty Conversion #1196

Merged
merged 15 commits into from Oct 30, 2020

Conversation

teor2345
Copy link
Contributor

@teor2345 teor2345 commented Oct 22, 2020

Motivation

We need to check if the compact difficulty in the block header satisfies the difficulty adjustment contextual validation rules.

Those rules require us to perform calculations on U256 expanded difficulty values, and convert the results to CompactDifficulty.

This PR covers the first stage of these checks: converting expanded to compact difficulty.
(And adds a bunch of tests and cleanups.)

The next step is to design contextual validation for difficulty, see ticket #1036.

Solution

  • convert expanded to compact difficulty according to the Zcash specification
  • add specific unit tests for difficulty edge cases
  • use the block test vectors to in the difficulty unit tests
  • add proptests for expanded to compact conversions and round-trips
  • add missing proptests for the other difficulty types
  • do other minor cleanups

Related Issues

Difficulty Tracking #802
Difficulty Contextual Validation Design #1036

Other Resources

zcashd implementation:

@teor2345 teor2345 added A-consensus Area: Consensus rule updates A-rust Area: Updates to Rust code C-enhancement Category: This is an improvement C-cleanup Category: This is a cleanup labels Oct 22, 2020
@teor2345 teor2345 added this to the First Alpha Release milestone Oct 22, 2020
@teor2345 teor2345 added this to Review in progress in 🦓 via automation Oct 22, 2020
@teor2345 teor2345 self-assigned this Oct 22, 2020
@teor2345 teor2345 requested a review from a team October 22, 2020 09:16
zebra-chain/src/work/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/work/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/work/difficulty.rs Outdated Show resolved Hide resolved
zebra-chain/src/work/tests/prop.rs Outdated Show resolved Hide resolved
zebra-chain/src/work/tests/prop.rs Outdated Show resolved Hide resolved
Copy link
Contributor Author

@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'll make these changes over the next day or so.

zebra-chain/src/work/arbitrary.rs Outdated Show resolved Hide resolved
Comment on lines 37 to 39
ExpandedDifficulty::from_hash(&block::Hash(bytes))
.to_compact()
.into()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
ExpandedDifficulty::from_hash(&block::Hash(bytes))
.to_compact()
.into()
Some(ExpandedDifficulty::from_hash(&block::Hash(bytes))
.to_compact())

zebra-chain/src/work/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/work/difficulty.rs Outdated Show resolved Hide resolved
zebra-chain/src/work/tests/prop.rs Outdated Show resolved Hide resolved
zebra-chain/src/work/arbitrary.rs Outdated Show resolved Hide resolved
zebra-chain/src/work/tests/prop.rs Outdated Show resolved Hide resolved
@teor2345 teor2345 requested a review from a team October 26, 2020 00:12
@teor2345
Copy link
Contributor Author

I opened #1231 to follow up the Windows failure at:
https://github.com/ZcashFoundation/zebra/pull/1196/checks?check_run_id=1320727402#step:7:1397

I'll close and re-open the PR to re-run the checks.

@teor2345 teor2345 closed this Oct 29, 2020
🦓 automation moved this from Review in progress to Done Oct 29, 2020
@teor2345 teor2345 reopened this Oct 29, 2020
🦓 automation moved this from Done to Review in progress Oct 29, 2020
@teor2345 teor2345 merged commit 1c31225 into ZcashFoundation:main Oct 30, 2020
🦓 automation moved this from Review in progress to Done Oct 30, 2020
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 C-cleanup Category: This is a cleanup C-enhancement Category: This is an improvement
Projects
No open projects
🦓
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants