From: Chris Duncan Date: Sun, 6 Jul 2025 08:03:33 +0000 (-0700) Subject: Validate PoW before committing to block. X-Git-Tag: v0.10.5~103 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=0c7547f311806601e4572030bec4ecce59a682eb;p=libnemo.git Validate PoW before committing to block. --- diff --git a/src/lib/block.ts b/src/lib/block.ts index d430bed..3686e3c 100644 --- a/src/lib/block.ts +++ b/src/lib/block.ts @@ -91,6 +91,13 @@ abstract class Block { if ('error' in result) { throw new Error('Failed to generate work', { cause: result.error }) } + const check = await NanoPow.work_validate(result.work, this.previous, { difficulty }) + if ('error' in check) { + throw new Error('Failed to validate work generated', { cause: check.error }) + } + if (check.valid === '0') { + throw new Error('Wwork generated is invalid', { cause: `${check.difficulty} < ${difficulty}` }) + } this.work = result.work } diff --git a/test/test.calculate-pow.mjs b/test/test.calculate-pow.mjs index da742b8..a2d90ed 100644 --- a/test/test.calculate-pow.mjs +++ b/test/test.calculate-pow.mjs @@ -19,6 +19,7 @@ await suite('Calculate proof-of-work', async () => { NANO_TEST_VECTORS.SEND_BLOCK.previous ) await block.pow() + assert.equals(block.previous.length, 64) assert.equals(block.work?.length, 16) @@ -39,6 +40,7 @@ await suite('Calculate proof-of-work', async () => { .update(bytes) .digest('hex') .slice(8, 16) + assert.ok(parseInt(hash.slice(0, 2), 16) > 0xf0) assert.equals(parseInt(hash.slice(2, 8), 16), 0xffffff) })