]> git.codecow.com Git - nano-pow.git/commitdiff
Use logger class in test webpage.
authorChris Duncan <chris@codecow.com>
Sun, 12 Jul 2026 07:51:33 +0000 (00:51 -0700)
committerChris Duncan <chris@codecow.com>
Sun, 12 Jul 2026 07:51:33 +0000 (00:51 -0700)
test/index.html

index d59485c3b9f2262a01da4b9b0514e127935e34d2..51d2ddc9a983b31e549727acb4cf45dcbac2ee69 100644 (file)
@@ -23,7 +23,9 @@ SPDX-License-Identifier: GPL-3.0-or-later
        </script>
        <script type="module">
                import { NanoPow } from 'nano-pow'
-               import { Cache, stats } from 'nano-pow/utils'
+               import { Cache, Logger, stats } from 'nano-pow/utils'
+
+               const logger = new Logger()
 
                try {
                        const glSize = (canvas => {
@@ -55,6 +57,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                        }
 
                        async function run(size, difficulty, effort, api, isOutputShown, isDebug, isSelfCheck) {
+                               logger.isEnabled = isDebug
                                // Generate once on load to compile shaders and initialize buffers
                                await NanoPow.work_generate(random(), { api, difficulty: '0', debug: isDebug })
                                const type = api
@@ -62,65 +65,65 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                if (isSelfCheck) {
                                        Cache.clear()
                                        document.getElementById('status').innerHTML = `RUNNING SELF-CHECK`
-                                       console.log(`%cNanoPow`, 'color:green', 'Checking validation against known values')
+                                       logger.log(`%cNanoPow`, 'color:green', 'Checking validation against known values')
 
                                        const expect = []
                                        let result
 
                                        // PASS
                                        result = await NanoPow.work_validate('47c83266398728cf', '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D', { api, debug: isDebug })
-                                       console.log(result)
+                                       logger.log(result)
                                        result = result.valid_all === '1'
-                                       console.log(`work_validate() output for good nonce 1 is ${result === true ? 'correct' : 'incorrect'}`)
+                                       logger.log(`work_validate() output for good nonce 1 is ${result === true ? 'correct' : 'incorrect'}`)
                                        expect.push(result)
 
                                        result = await NanoPow.work_validate('4a8fb104eebbd336', '8797585D56B8AEA3A62899C31FC088F9BE849BA8298A88E94F6E3112D4E55D01', { api, debug: isDebug })
-                                       console.log(result)
+                                       logger.log(result)
                                        result = result.valid_all === '1'
-                                       console.log(`work_validate() output for good nonce 2 is ${result === true ? 'correct' : 'incorrect'}`)
+                                       logger.log(`work_validate() output for good nonce 2 is ${result === true ? 'correct' : 'incorrect'}`)
                                        expect.push(result)
 
                                        result = await NanoPow.work_validate('c5d5d6f7c5d6ccd1', '281E89AC73B1082B464B9C3C1168384F846D39F6DF25105F8B4A22915E999117', { api, debug: isDebug })
-                                       console.log(result)
+                                       logger.log(result)
                                        result = result.valid_all === '1'
-                                       console.log(`work_validate() output for colliding nonce is ${result === true ? 'correct' : 'incorrect'}`)
+                                       logger.log(`work_validate() output for colliding nonce is ${result === true ? 'correct' : 'incorrect'}`)
                                        expect.push(result)
 
                                        result = await NanoPow.work_validate('6866c1ac3831a891', '7069D9CD1E85D6204301D254B0927F06ACC794C9EA5DF70EA5578458FB597090', { api, difficulty: 0xfffffe0000000000n, debug: isDebug })
-                                       console.log(result)
+                                       logger.log(result)
                                        result = result.valid === '1' && result.valid_all === '0' && result.valid_receive === '1'
-                                       console.log(`work_validate() output for good receive difficulty nonce is ${result === true ? 'correct' : 'incorrect'}`)
+                                       logger.log(`work_validate() output for good receive difficulty nonce is ${result === true ? 'correct' : 'incorrect'}`)
                                        expect.push(result)
 
                                        // XFAIL
                                        result = await NanoPow.work_validate('0000000000000000', '0000000000000000000000000000000000000000000000000000000000000000', { api, debug: isDebug })
-                                       console.log(result)
+                                       logger.log(result)
                                        result = result.valid_all === '0'
-                                       console.log(`work_validate() output for bad nonce 1 is ${result === true ? 'correct' : 'incorrect'}`)
+                                       logger.log(`work_validate() output for bad nonce 1 is ${result === true ? 'correct' : 'incorrect'}`)
                                        expect.push(result)
 
                                        result = await NanoPow.work_validate('c5d5d6f7c5d6ccd1', 'BA1E946BA3D778C2F30A83D44D2132CC6EEF010D8D06FF10A8ABD0100D8FB47E', { api, debug: isDebug })
-                                       console.log(result)
+                                       logger.log(result)
                                        result = result.valid_all === '0'
-                                       console.log(`work_validate() output for bad nonce 2 is ${result === true ? 'correct' : 'incorrect'}`)
+                                       logger.log(`work_validate() output for bad nonce 2 is ${result === true ? 'correct' : 'incorrect'}`)
                                        expect.push(result)
 
                                        result = await NanoPow.work_validate('ae238556213c3624', 'BF41D87DA3057FDC6050D2B00C06531F89F4AA6195D7C6C2EAAF15B6E703F8F6', { api, difficulty: 0xfffffff800000001n, debug: isDebug })
-                                       console.log(result)
+                                       logger.log(result)
                                        result = result.error === 'Invalid difficulty fffffff800000001'
-                                       console.log(`work_validate() output for bad difficulty beyond max is ${result === true ? 'correct' : 'incorrect'}`)
+                                       logger.log(`work_validate() output for bad difficulty beyond max is ${result === true ? 'correct' : 'incorrect'}`)
                                        expect.push(result)
 
                                        result = await NanoPow.work_validate('29a9ae0236990e2e', '32721F4BD2AFB6F6A08D41CD0DF3C0D9C0B5294F68D0D12422F52B28F0800B5F', { api, debug: isDebug })
-                                       console.log(result)
+                                       logger.log(result)
                                        result = result.valid_all === '0'
-                                       console.log(`work_validate() output for slightly wrong nonce is ${result === true ? 'correct' : 'incorrect'}`)
+                                       logger.log(`work_validate() output for slightly wrong nonce is ${result === true ? 'correct' : 'incorrect'}`)
                                        expect.push(result)
 
                                        result = await NanoPow.work_validate('7d903b18d03f9820', '39C57C28F904DFE4012288FFF64CE80C0F42601023A9C82108E8F7B2D186C150', { api, difficulty: 0xfffffe0000000000n, debug: isDebug })
-                                       console.log(result)
+                                       logger.log(result)
                                        result = result.valid === '0' && result.valid_all === '0' && result.valid_receive === '0'
-                                       console.log(`work_validate() output for bad receive difficulty nonce is ${result === true ? 'correct' : 'incorrect'}`)
+                                       logger.log(`work_validate() output for bad receive difficulty nonce is ${result === true ? 'correct' : 'incorrect'}`)
                                        expect.push(result)
 
                                        const prefixes = [
@@ -137,7 +140,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                                const start = performance.now()
                                                try {
                                                        result = await NanoPow.work_generate(hash, { api, difficulty, effort, debug: isDebug })
-                                                       console.log(result)
+                                                       logger.log(result)
                                                } catch (err) {
                                                        document.getElementById('output').innerHTML += `Error: ${err.message}<br/>${err.cause}<br/>${err.stack}`
                                                        console.error(err)
@@ -146,7 +149,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                                const end = performance.now()
                                                const check = await NanoPow.work_validate(result.work, result.hash, { difficulty, debug: isDebug })
                                                const isValid = (check.valid === '1' && BigInt(`0x${result.hash}`) === BigInt(`0x${hash.trim().replace('n', '').replace(/^0x/i, '')}`))
-                                               console.log(`work_generate() output for max value block hash is ${isValid === true ? 'correct' : 'incorrect'}`)
+                                               logger.log(`work_generate() output for max value block hash is ${isValid === true ? 'correct' : 'incorrect'}`)
                                                expect.push(isValid)
                                        }
 
@@ -160,7 +163,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                        }
                                }
 
-                               console.log(`%cNanoPow (${type})`, 'color:green', `Calculate proof-of-work for ${size} unique send block hashes`)
+                               logger.log(`%cNanoPow (${type})`, 'color:green', `Calculate proof-of-work for ${size} unique send block hashes`)
                                document.getElementById('status').innerHTML = `TESTING IN PROGRESS 0/${size}<br/>`
                                let times
                                try {
@@ -174,11 +177,11 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                document.getElementById('output').innerHTML += `<hr/>`
                                document.getElementById('summary').innerHTML += `${JSON.stringify(average(times, type, effort), null, '\t')}<br/>`
                                document.getElementById('status').innerHTML = `TESTING COMPLETE<br/>`
-                               console.log('%cTESTING COMPLETE', 'color:orange;font-weight:bold')
+                               logger.log('%cTESTING COMPLETE', 'color:orange;font-weight:bold')
                        }
 
                        async function score(runs, size, difficulty, effort, api) {
-                               console.log(`%cNanoPow ${api}`, 'color:green', `Calculate truncated harmonic mean of the truncated arithmetic rate across ${runs} runs of ${size} samples.`)
+                               logger.log(`%cNanoPow ${api}`, 'color:green', `Calculate truncated harmonic mean of the truncated arithmetic rate across ${runs} runs of ${size} samples.`)
                                const rates = []
                                for (let i = 0; i < runs; i++) {
                                        let times
@@ -196,13 +199,13 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                        document.getElementById('output').innerHTML += `Benchmark ${i + 1} score: ${truncatedRate} wps<br/>`
                                }
                                const results = Object.values(average(rates))[0]
-                               console.log(rates)
-                               console.log(results)
+                               logger.log(rates)
+                               logger.log(results)
                                const { truncatedHarmonic } = results
                                document.getElementById('output').innerHTML += `<hr/>`
                                document.getElementById('summary').innerHTML += `work-per-second: ${truncatedHarmonic}<br/>`
                                document.getElementById('status').innerHTML = `SCORING COMPLETE<br/>`
-                               console.log('%cSCORING COMPLETE', 'color:orange;font-weight:bold')
+                               logger.log('%cSCORING COMPLETE', 'color:orange;font-weight:bold')
                        }
 
                        async function execute(size, difficulty, effort, api, isOutputShown = false, debug = false) {
@@ -224,7 +227,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
                                        isValid = (result.hash === hash && check.hash === hash && check.valid === '1')
                                        if (isOutputShown) {
                                                output.textContent += `${isValid ? 'VALID' : 'INVALID'} (${end - start} ms)\n${JSON.stringify(result, ' ', 2)}\n`
-                                               if (!isValid) console.log(hash, result, check)
+                                               if (!isValid) logger.log(hash, result, check)
                                        }
                                }
                                return times