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

feat: proof of work audit part 2 #5495

Merged
merged 2 commits into from
Jun 26, 2023

Conversation

hansieodendaal
Copy link
Contributor

@hansieodendaal hansieodendaal commented Jun 23, 2023

Description

  • Implemented creation, underflow and overflow protection for Difficulty in difficulty.rs.
  • Removed all methods not adhering to these patterns/traits.
  • Fixed usage in the code to adhere to these.
  • Consolidated and refactored repetitive code in difficulty.rs.

Motivation and Context

Preparation for code audit

How Has This Been Tested?

Added unit tests
All unit tests pass

What process can a PR reviewer use to test or verify this change?

Code walkthrough
Review the new unit test and related code

Breaking Changes

  • None
  • Requires data directory on base node to be deleted
  • Requires hard fork
  • Other - Please specify

@github-actions
Copy link

github-actions bot commented Jun 23, 2023

Test Results (CI)

1 174 tests   1 174 ✔️  11m 31s ⏱️
     37 suites         0 💤
       1 files           0

Results for commit b95112d.

♻️ This comment has been updated with latest results.

@ghpbot-tari-project ghpbot-tari-project added P-acks_required Process - Requires more ACKs or utACKs P-reviews_required Process - Requires a review from a lead maintainer to be merged labels Jun 23, 2023
@github-actions
Copy link

github-actions bot commented Jun 23, 2023

Test Results (Integration tests)

  2 files  +  2  11 suites  +11   14m 7s ⏱️ + 14m 7s
26 tests +26  25 ✔️ +25  0 💤 ±0  1 +1 
27 runs  +27  26 ✔️ +26  0 💤 ±0  1 +1 

For more details on these failures, see this check.

Results for commit b95112d. ± Comparison against base commit 4a9f73c.

♻️ This comment has been updated with latest results.

@hansieodendaal hansieodendaal force-pushed the ho_pow_audit_02 branch 3 times, most recently from aeb68b4 to ac91604 Compare June 23, 2023 14:40
Copy link
Collaborator

@SWvheerden SWvheerden 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, I have one question on a test change

base_layer/core/src/proof_of_work/difficulty.rs Outdated Show resolved Hide resolved
base_layer/core/src/proof_of_work/difficulty.rs Outdated Show resolved Hide resolved
base_layer/core/src/proof_of_work/monero_rx/helpers.rs Outdated Show resolved Hide resolved

let achieved_target =
AchievedTargetDifficulty::try_construct(PowAlgorithm::Sha3, achieved - Difficulty::from(1), achieved)
AchievedTargetDifficulty::try_construct(PowAlgorithm::Sha3, achieved, achieved.checked_add(1).unwrap())
Copy link
Collaborator

Choose a reason for hiding this comment

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

These 2 lines are not equal:
We had (sha3, achieved -1, achieved)
Now we have: (sha3, achieved, achieved + 1)

Copy link
Contributor Author

@hansieodendaal hansieodendaal Jun 24, 2023

Choose a reason for hiding this comment

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

The original line in the test helper now panicked because it resulted in a difficulty of zero as these tests use localnet with min and max difficulties set to 1.

The single test that still failed due to this change, base_layer/core/src/chain_storage/blockchain_database.rs/async fn it_correctly_handles_duplicate_blocks(), because it bargained on a runtime difficulty of zero, was changed to honour minimum difficulty.

Implemented creation, underflow and overflow protection for Difficulty
Removed all methods not adhering to these patterns/traitys
Fixed usage in the code to adhere to these
@ghpbot-tari-project ghpbot-tari-project removed the P-reviews_required Process - Requires a review from a lead maintainer to be merged label Jun 26, 2023
@SWvheerden SWvheerden merged commit af32f96 into tari-project:development Jun 26, 2023
14 of 15 checks passed
@hansieodendaal hansieodendaal deleted the ho_pow_audit_02 branch June 26, 2023 06:42
SWvheerden pushed a commit that referenced this pull request Jun 26, 2023
Description
---
- Removed `max_block_time` for `struct LinearWeightedMovingAverage`:
- Removed manual assignment of `max_block_time` for `struct
LinearWeightedMovingAverage` as it is always a fixed multiple of
`target_time` according to the **LWMA-1** algorithm specification.
- Previously, `max_block_time` was asserted when compiling tests and
debug mode with function `fn assert_hybrid_pow_constants(`.
- This change resulted in the corresponding removal of `max_target_time`
from `struct PowAlgorithmConstants` (and consensus constants) as it is
now being calculated by `LinearWeightedMovingAverage`.
- Added documentation to public methods.

Dependent on #5495 

Motivation and Context
---
Preparation for code audit

How Has This Been Tested?
---
All unit tests pass

What process can a PR reviewer use to test or verify this change?
---
Code walkthrough

<!-- Checklist -->
<!-- 1. Is the title of your PR in the form that would make nice release
notes? The title, excluding the conventional commit
tag, will be included exactly as is in the CHANGELOG, so please think
about it carefully. -->


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->
Comment on lines +66 to 76
/// Helper function to provide the difficulty of the hash assuming the hash is big_endian
pub fn big_endian_difficulty(hash: &[u8]) -> Result<Difficulty, DifficultyError> {
let scalar = U256::from_big_endian(hash); // Big endian so the hash has leading zeroes
Difficulty::u256_scalar_to_difficulty(scalar)
}

/// Helper function to provide the difficulty of the hash assuming the hash is little_endian
pub fn little_endian_difficulty(hash: &[u8]) -> Result<Difficulty, DifficultyError> {
let scalar = U256::from_little_endian(hash); // Little endian so the hash has trailing zeroes
Difficulty::u256_scalar_to_difficulty(scalar)
}
Copy link
Collaborator

@AaronFeickert AaronFeickert Jun 27, 2023

Choose a reason for hiding this comment

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

These will panic if the provided slice length does not match the expected U256 size. This can't happen with the current callers, which are either test functions or use the output from 256-bit SHA3 hashing; however, it may be prudent to document this behavior, or to check the input slice and return an error if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I will add some checks.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Great! Since the functions already return a Result, it seems straightforward to add a check, and presumably this would have no performance hit in practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P-acks_required Process - Requires more ACKs or utACKs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants