Skip to content

Commit

Permalink
Force validation in block downloader (#3413)
Browse files Browse the repository at this point in the history
* forceValidation

* Fix Ethash edge case (<= rather than <), In spec its >

* randomNumberValidation

* Max 0

* Rename target

* Added test

* cosmetic in the test

Co-authored-by: lukasz.rozmej <lukasz.rozmej@gmail.com>
  • Loading branch information
MarekM25 and LukaszRozmej authored Sep 14, 2021
1 parent 13b80d8 commit 2ee8d40
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 115 deletions.
10 changes: 5 additions & 5 deletions src/Nethermind/Nethermind.Consensus.Ethash/Ethash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ private static ulong GetRandomNonce()
return BitConverter.ToUInt64(buffer, 0);
}

private bool IsLessThanTarget(byte[] result, UInt256 difficulty)
private bool IsLessOrEqualThanTarget(byte[] result, UInt256 difficulty)
{
UInt256 resultAsInteger = new(result, true);
BigInteger threshold = BigInteger.Divide(_2To256, (BigInteger)difficulty);
return (BigInteger)resultAsInteger < threshold;
BigInteger target = BigInteger.Divide(_2To256, (BigInteger)difficulty);
return (BigInteger)resultAsInteger <= target;
}

public (Keccak MixHash, ulong Nonce) Mine(BlockHeader header, ulong? startNonce = null)
Expand All @@ -178,7 +178,7 @@ private bool IsLessThanTarget(byte[] result, UInt256 difficulty)
{
byte[] result;
(mixHash, result, _) = Hashimoto(fullSize, dataSet, headerHashed, null, nonce);
if (IsLessThanTarget(result, header.Difficulty))
if (IsLessOrEqualThanTarget(result, header.Difficulty))
{
break;
}
Expand Down Expand Up @@ -243,7 +243,7 @@ public bool Validate(BlockHeader header)
return false;
}

return IsLessThanTarget(result, header.Difficulty);
return IsLessOrEqualThanTarget(result, header.Difficulty);
}

private readonly Stopwatch _cacheStopwatch = new();
Expand Down
Loading

0 comments on commit 2ee8d40

Please sign in to comment.