Added (failing) light Proof of Work test

This commit is contained in:
mratsim 2018-02-21 12:34:33 +01:00
parent 237b01f62e
commit 971871dbe7
3 changed files with 23 additions and 11 deletions

View File

@ -64,11 +64,11 @@ proc get_cachesize_lut*(block_number: Natural): uint64 {.noSideEffect, inline.}
# ###############################################################################
# Cache generation
proc mkcache*(cache_size: int, seed: Hash[256]): seq[Hash[512]] {.noSideEffect.}=
proc mkcache*(cache_size: uint64, seed: Hash[256]): seq[Hash[512]] {.noSideEffect.}=
# The starting cache size is a set of 524288 64-byte values
let n = cache_size div HASH_BYTES
let n = int(cache_size div HASH_BYTES)
# Sequentially produce the initial dataset
result = newSeq[Hash[512]](n)
@ -164,7 +164,7 @@ proc initMix(s: U512): array[MIX_BYTES div HASH_BYTES * 512 div 32, uint32] {.no
proc hashimoto(header: Hash[256],
nonce: uint64,
fullsize: Natural,
full_size: Natural,
dataset_lookup: DatasetLookup
): HashimotoHash {.noInit, noSideEffect.}=
let
@ -195,8 +195,8 @@ proc hashimoto(header: Hash[256],
result.value = keccak256 concat_hash(s, result.mix_digest)
proc hashimoto_light(full_size:Natural, cache: seq[Hash[512]],
header: Hash[256], nonce: uint64): HashimotoHash {.noSideEffect, inline.} =
proc hashimoto_light*(full_size:Natural, cache: seq[Hash[512]],
header: Hash[256], nonce: uint64): HashimotoHash {.noSideEffect, inline.} =
let light: DatasetLookup = proc(x: Natural): Hash[512] = calc_data_set_item(cache, x)
hashimoto(header,
@ -204,7 +204,7 @@ proc hashimoto_light(full_size:Natural, cache: seq[Hash[512]],
full_size,
light)
proc hashimoto_full(full_size:Natural, dataset: seq[Hash[512]],
proc hashimoto_full*(full_size:Natural, dataset: seq[Hash[512]],
header: Hash[256], nonce: uint64): HashimotoHash {.noSideEffect, inline.} =
let full: DatasetLookup = proc(x: Natural): Hash[512] = dataset[x]
@ -216,6 +216,7 @@ proc hashimoto_full(full_size:Natural, dataset: seq[Hash[512]],
# ###############################################################################
# Defining the seed hash
proc get_seedhash*(block_number: uint32): Hash[256] {.noSideEffect.} =
for i in 0'u32 ..< block_number div EPOCH_LENGTH:
proc get_seedhash*(block_number: uint64): Hash[256] {.noSideEffect.} =
# uint64 are not Ordinal :/
for i in 0 ..< int(block_number div EPOCH_LENGTH):
result = keccak256 result.toByteArrayBE

View File

@ -31,4 +31,4 @@ proc concat_hash*(s: U512, cmix: array[8, uint32]): array[(512 + 8 * 32) div 8,
for i, b in cmix:
let offset = s.sizeof + i
result[offset ..< offset + 4] = cast[array[8, byte]](b)
result[offset ..< offset + 8] = cast[array[8, byte]](b)

View File

@ -109,7 +109,7 @@ suite "Seed hash":
check: get_seedhash(i) == expected
expected = keccak_256(expected.toByteArrayBE)
suite "[Not Implemented] Dagger hashimoto computation":
suite "Dagger hashimoto computation":
test "Light compute":
# Taken from https://github.com/paritytech/parity/blob/05f47b635951f942b493747ca3bc71de90a95d5d/ethash/src/compute.rs#L372-L394
@ -132,4 +132,15 @@ suite "[Not Implemented] Dagger hashimoto computation":
])
let nonce = 0xd7b3ac70a301a249'u64
## difficulty = 0x085657254bd9u64
## difficulty = 0x085657254bd9u64
let blk = 486382'u # block number
let light_cache = mkcache(blk.get_cache_size, blk.get_seedhash)
let r = hashimoto_light(blk.get_data_size,
light_cache,
blk.get_seedhash,
nonce
)
check: r.mix_digest == expected_mix_hash
check: r.value == expected_boundary