From: Chris Duncan Date: Wed, 12 Mar 2025 21:44:54 +0000 (-0700) Subject: Add CLI support for validate calls. Add some debug logging. Remove display of block... X-Git-Tag: v3.1.0~9^2~5 X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=3691dd08f5171d081f7d00477c7e5cdd043e3bca;p=nano-pow.git Add CLI support for validate calls. Add some debug logging. Remove display of block hash from console output. --- diff --git a/cli.js b/cli.js index 5656b87..d969774 100755 --- a/cli.js +++ b/cli.js @@ -15,20 +15,22 @@ if (hashes.length === 0) { process.exit(1) } +let fn = 'search' +let work = '' const options = {} for (let i = 0; i < args.length; i++) { switch (args[i]) { case ('--threshold'): case ('-t'): { if (args[i + 1] == null) throw new Error('Missing argument for threshold') - if (/^[0-9A-Fa-f]{,8}$/.test(args[i + 1])) throw new Error('Invalid threshold') + if (!/^[0-9A-Fa-f]{0,8}$/.test(args[i + 1])) throw new Error('Invalid threshold') options['threshold'] = parseInt(args[i + 1], 16) break } case ('--effort'): case ('-e'): { if (args[i + 1] == null) throw new Error('Missing argument for effort') - if (/^[0-9]{,2}$/.test(args[i + 1])) throw new Error('Invalid effort') + if (!/^[0-9]{0,2}$/.test(args[i + 1])) throw new Error('Invalid effort') options['effort'] = parseInt(args[i + 1], 10) break } @@ -37,8 +39,18 @@ for (let i = 0; i < args.length; i++) { options['debug'] = true break } + case ('--validate'): + case ('-v'): { + if (args[i + 1] == null) throw new Error('Missing argument for work validation') + if (!/^[0-9A-Fa-f]{16}$/.test(args[i + 1])) throw new Error('Invalid work to validate') + fn = 'validate' + work = `'${args[i + 1]}', ` + break + } } } +if (options['debug']) console.log(`NanoPowCli.${fn}()`) +if (options['debug']) console.log(`${fn} options`, JSON.stringify(options)); (async () => { const NanoPow = await fs.readFile(`${import.meta.dirname}/main.min.js`, 'utf-8') @@ -84,8 +96,7 @@ for (let i = 0; i < args.length; i++) { const hashes = ["${hashes.join('","')}"] for (const hash of hashes) { try { - console.log(\`cli \${hash}\`) - const work = await NanoPow.search(hash) + const work = await NanoPow.${fn}(${work}hash, ${JSON.stringify(options)}) console.log(\`cli \${work}\`) } catch (err) { console.error(\`cli \${err}\`)