diff --git a/src/mining.nim b/src/mining.nim index e07c3f2..76e4110 100644 --- a/src/mining.nim +++ b/src/mining.nim @@ -13,6 +13,9 @@ proc readUint256BE*(ba: ByteArrayBE[32]): UInt256 {.noSideEffect.}= result = result shl 8 or ba[i].u256 proc willMulOverflow(a, b: uint64): bool {.noSideEffect.}= + # Returns true if a * b overflows + # false otherwise + # We assume a <= b if a > b: return willMulOverflow(b, a) @@ -93,7 +96,7 @@ proc isValid(nonce: uint64, else: let hi_hash = candidate.table[0] - result = willMulOverflow(hi_hash, difficulty) + result = not willMulOverflow(hi_hash, difficulty) proc mine*(full_size: Natural, dataset: seq[Hash[512]], header: Hash[256], difficulty: uint64): uint64 = diff --git a/tests/test_mining.nim b/tests/test_mining.nim index ec2e04b..cb20396 100644 --- a/tests/test_mining.nim +++ b/tests/test_mining.nim @@ -1,7 +1,7 @@ # Copyright (c) 2018 Status Research & Development GmbH # Distributed under the Apache v2 License (license terms are at http://www.apache.org/licenses/LICENSE-2.0). -import ../src/ethash, unittest, keccak_tiny, times +import ../src/ethash, unittest, keccak_tiny, times, strutils suite "Test mining": @@ -29,3 +29,12 @@ suite "Test mining": echo " Done, time taken: ", $(cpuTime() - start), " seconds" check: mined_nonce == 0x495732e0ed7a801c'u64 + + # test "1st test from official ethereum/tests repo": + # # https://github.com/ethereum/tests/blob/b6aa0947a8e20f4140dd2647882791be6ceb2ac5/PoWTests/ethash_tests.json + + # let + # seed = Hash[256]() + # cache = mkcache(16776896, seed) + + # check: $keccak_256(cache) == toUpperAscii "35ded12eecf2ce2e8da2e15c06d463aae9b84cb2530a00b932e4bbc484cde353" \ No newline at end of file