Skip to content

Commit

Permalink
Change verify_final_hash() -> verify_final_hash_against_difficulty()
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Nov 2, 2021
1 parent 3ee5e49 commit 834ffae
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
10 changes: 5 additions & 5 deletions include/ethash/ethash.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ ethash_errc ethash_verify_against_boundary(const struct ethash_epoch_context* co
* way the difficulty is stored in block headers.
*
* @return Error code: ::ETHASH_SUCCESS if valid, ::ETHASH_INVALID_FINAL_HASH if the final hash
* does not satisfies difficulty, ::ETHASH_INVALID_MIX_HASH if the provided mix hash
* does not satisfy difficulty, ::ETHASH_INVALID_MIX_HASH if the provided mix hash
* mismatches the computed one.
*/
ethash_errc ethash_verify_against_difficulty(const struct ethash_epoch_context* context,
Expand All @@ -184,12 +184,12 @@ static inline ethash_errc ethash_verify(const struct ethash_epoch_context* conte
/**
* Verify only the final hash. This can be performed quickly without accessing Ethash context.
*
* @return Error code: ::ETHASH_SUCCESS if valid, ::ETHASH_INVALID_FINAL_HASH if the final hash is
* not within provided boundary.
* @return Error code: ::ETHASH_SUCCESS if valid, ::ETHASH_INVALID_FINAL_HASH if the final hash
* does not satisfy difficulty.
*/
ethash_errc ethash_verify_final_hash(const union ethash_hash256* header_hash,
ethash_errc ethash_verify_final_hash_against_difficulty(const union ethash_hash256* header_hash,
const union ethash_hash256* mix_hash, uint64_t nonce,
const union ethash_hash256* boundary) noexcept;
const union ethash_hash256* difficulty) noexcept;

#ifdef __cplusplus
}
Expand Down
6 changes: 3 additions & 3 deletions include/ethash/ethash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ inline result hash(

result hash(const epoch_context_full& context, const hash256& header_hash, uint64_t nonce) noexcept;

inline std::error_code verify_final_hash(const hash256& header_hash, const hash256& mix_hash,
uint64_t nonce, const hash256& boundary) noexcept
inline std::error_code verify_final_hash_against_difficulty(const hash256& header_hash,
const hash256& mix_hash, uint64_t nonce, const hash256& difficulty) noexcept
{
return ethash_verify_final_hash(&header_hash, &mix_hash, nonce, &boundary);
return ethash_verify_final_hash_against_difficulty(&header_hash, &mix_hash, nonce, &difficulty);
}

inline std::error_code verify_against_difficulty(const epoch_context& context,
Expand Down
7 changes: 4 additions & 3 deletions lib/ethash/ethash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,11 @@ ethash_result ethash_hash(
}


ethash_errc ethash_verify_final_hash(const hash256* header_hash, const hash256* mix_hash,
uint64_t nonce, const hash256* boundary) noexcept
ethash_errc ethash_verify_final_hash_against_difficulty(const hash256* header_hash,
const hash256* mix_hash, uint64_t nonce, const hash256* difficulty) noexcept
{
return less_equal(hash_final(hash_seed(*header_hash, nonce), *mix_hash), *boundary) ?
return check_against_difficulty(
hash_final(hash_seed(*header_hash, nonce), *mix_hash), *difficulty) ?
ETHASH_SUCCESS :
ETHASH_INVALID_FINAL_HASH;
}
Expand Down
15 changes: 8 additions & 7 deletions test/unittests/test_ethash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,12 +579,12 @@ TEST(ethash, verify_hash_light)
EXPECT_EQ(to_hex(r.final_hash), t.final_hash_hex);
EXPECT_EQ(to_hex(r.mix_hash), t.mix_hash_hex);

auto ec = verify_final_hash(header_hash, mix_hash, nonce, boundary);
auto ec = verify_final_hash_against_difficulty(header_hash, mix_hash, nonce, difficulty);
EXPECT_EQ(ec, ETHASH_SUCCESS);
EXPECT_FALSE(ec);
EXPECT_EQ(ec.category(), ethash_category());

ec = verify_final_hash(header_hash, mix_hash, nonce, dec(boundary));
ec = verify_final_hash_against_difficulty(header_hash, mix_hash, nonce, inc(difficulty));
EXPECT_EQ(ec, ETHASH_INVALID_FINAL_HASH);

ec = verify_against_boundary(*context, header_hash, mix_hash, nonce, boundary);
Expand Down Expand Up @@ -614,7 +614,7 @@ TEST(ethash, verify_hash_light)
const bool within_significant_boundary = r.final_hash.bytes[0] == 0;
if (within_significant_boundary)
{
ec = verify_final_hash(header_hash, mix_hash, nonce + 1, boundary);
ec = verify_final_hash_against_difficulty(header_hash, mix_hash, nonce + 1, difficulty);
EXPECT_EQ(ec, ETHASH_INVALID_FINAL_HASH);

ec = verify_against_boundary(*context, header_hash, mix_hash, nonce + 1, boundary);
Expand Down Expand Up @@ -673,11 +673,12 @@ TEST(ethash, verify_final_hash_only)
const hash256 header_hash = {};
const hash256 mix_hash = {};
uint64_t nonce = 3221208;
const hash256 boundary =
to_hash256("000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
const hash256 difficulty =
to_hash256("00000000000000000000000000000000000000000000000000000000012853fe");

EXPECT_EQ(verify_final_hash(header_hash, mix_hash, nonce, boundary), ETHASH_SUCCESS);
EXPECT_EQ(verify_against_boundary(context, header_hash, mix_hash, nonce, boundary),
EXPECT_EQ(verify_final_hash_against_difficulty(header_hash, mix_hash, nonce, difficulty),
ETHASH_SUCCESS);
EXPECT_EQ(verify_against_difficulty(context, header_hash, mix_hash, nonce, difficulty),
ETHASH_INVALID_MIX_HASH);
}

Expand Down

0 comments on commit 834ffae

Please sign in to comment.