]> zoso.dev Git - libnemo.git/commitdiff
Add more info to test logging. Comment out some portions of the test webpage to check...
authorChris Duncan <chris@zoso.dev>
Sat, 7 Dec 2024 00:39:58 +0000 (16:39 -0800)
committerChris Duncan <chris@zoso.dev>
Sat, 7 Dec 2024 00:39:58 +0000 (16:39 -0800)
GLOBALS.mjs
perf/account.perf.js
perf/wallet.perf.js
src/lib/pool.ts
src/lib/wallet.ts
test.html

index dea5a8c40cc3db72575810eab2152512dac7743f..e2657fb6e51a5b9268c25d280cd32cdde1fdfe94 100644 (file)
@@ -65,34 +65,35 @@ export function skip (name, ...args) {
 }
 
 export function test (name, fn) {
+       console.log(`=> Test: ${name}`)
        if (fn instanceof Promise) {
                try {
                        return fn
-                               .then(() => log(`PASS: ${name}`))
-                               .catch((err) => { error(`FAIL: ${name}: ${err}`) })
+                               .then(() => log(`  -> PASS: ${name}`))
+                               .catch((err) => { error(`  -> FAIL: ${name}: ${err}`) })
                } catch (err) {
-                       error(`FAIL: ${name}: ${err.message}`)
+                       error(`  -> FAIL: ${name}: ${err.message}`)
                        error(err)
                }
        } else if (fn?.constructor?.name === 'AsyncFunction') {
                try {
                        return fn()
-                               .then(() => log(`PASS: ${name}`))
-                               .catch((err) => error(`FAIL: ${name}: ${err.message}`))
+                               .then(() => log(`  -> PASS: ${name}`))
+                               .catch((err) => error(`  -> FAIL: ${name}: ${err.message}`))
                } catch (err) {
-                       error(`FAIL: ${name}: ${err.message}`)
+                       error(`  -> FAIL: ${name}: ${err.message}`)
                        error(err)
                }
        } else if (typeof fn === 'function') {
                try {
                        fn()
-                       log(`PASS: ${name}`)
+                       log(`  -> PASS: ${name}`)
                } catch (err) {
-                       error(`FAIL: ${name}: ${err.message}`)
+                       error(`  -> FAIL: ${name}: ${err.message}`)
                        error(err)
                }
        } else {
-               error(`FAIL: ${name}: test cannot execute on ${typeof fn} ${fn}`)
+               error(`  -> FAIL: ${name}: test cannot execute on ${typeof fn} ${fn}`)
        }
 }
 
index 4ce0a81877178bede84c3e02af8482217015d1d8..4f97b9c06f12624f82bd84c700f13c98764ed178 100644 (file)
@@ -10,19 +10,17 @@ import { Bip44Wallet, Blake2bWallet } from '#dist/main.js'
 await test('BIP-44 ckd performance test', async () => {
        const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD)
        await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)
-
+       let now = performance.now()
        const accounts = await wallet.accounts(0, 0x7fff)
-
-       console.log(`HERE`)
+       console.log(`${performance.now() - now} ms`)
        assert.equals(accounts.length, 0x8000)
 })
 
 await test('BLAKE2b ckd performance test', async () => {
        const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD)
        await wallet.unlock(NANO_TEST_VECTORS.PASSWORD)
-
+       let now = performance.now()
        const accounts = await wallet.accounts(0, 0x7fff)
-
-       console.log(`HERE 2`)
+       console.log(`${performance.now() - now} ms`)
        assert.equals(accounts.length, 0x8000)
 })
index 4b845a1eae74636bafab9e3295f881b0b3995c07..0e926184d419822143d5827e5cc6f175caedd02c 100644 (file)
@@ -8,17 +8,23 @@ import { NANO_TEST_VECTORS } from '#test/TEST_VECTORS.js'
 import { Bip44Wallet, Blake2bWallet } from '#dist/main.js'
 
 await test('creating BIP-44 wallets performance test', async () => {
-       const wallets = []
+       const seeds = []
+       let now = performance.now()
        for (let i = 0x80; i > 0; i--) {
-               wallets.push(await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD))
+               const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD)
+               seeds.push(wallet.seed)
        }
-       assert.equals(wallets.length, 0x80)
+       console.log(`${performance.now() - now} ms`)
+       assert.equals(seeds.length, 0x80)
 })
 
 await test('creating BLAKE2b wallets performance test', async () => {
-       const wallets = []
+       const seeds = []
+       let now = performance.now()
        for (let i = 0x80; i > 0; i--) {
-               wallets.push(await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD))
+               const wallet = await Blake2bWallet.create(NANO_TEST_VECTORS.PASSWORD)
+               seeds.push(wallet.seed)
        }
-       assert.equals(wallets.length, 0x80)
+       console.log(`${performance.now() - now} ms`)
+       assert.equals(seeds.length, 0x80)
 })
index b44b2ed37aa29f415e0ae2b4c3ae51ea8be64d63..7c249b9e8de2e6f20f11c0ce82af62ad3769f402 100644 (file)
@@ -12,7 +12,6 @@ type Thread = {
 export class Pool {
        #approach: 'converge' | 'divide' = 'divide'
        #cores: number = Math.max(1, navigator.hardwareConcurrency - 1)
-       // #cores: number = 1
        #queue: object[] = []
        #resolve: Function = (value: unknown): void => { }
        #results: object[] = []
@@ -46,7 +45,6 @@ export class Pool {
        }
 
        #assign (thread: Thread, next: any[]): void {
-               console.dir(thread.worker)
                if (next?.length > 0) {
                        thread.isBusy = true
                        const buf = new TextEncoder().encode(JSON.stringify(next)).buffer
index 2c3ae3bfcc5666135a50309e26eff75d801d512d..38a93d047ba2b186844480dbe2658550712dc459 100644 (file)
@@ -29,7 +29,6 @@ abstract class Wallet {
        #id: Entropy\r
        #locked: boolean = true\r
        #mnemonic: Bip39Mnemonic | null\r
-       #pool: Pool\r
        #safe: Safe\r
        #seed: string | null\r
        get id () { return this.#id.hex }\r
@@ -59,7 +58,6 @@ abstract class Wallet {
                        ? new Entropy(id)\r
                        : new Entropy(16)\r
                this.#mnemonic = mnemonic ?? null\r
-               this.#pool = new Pool(`const Blake2b = ${Blake2b}\n${NanoNaCl}`)\r
                this.#safe = new Safe()\r
                this.#seed = seed ?? null\r
        }\r
@@ -97,7 +95,9 @@ abstract class Wallet {
                        let results = await this.ckd(indexes)\r
                        const data: any = []\r
                        results.forEach(r => data.push({ privateKey: r.privateKey, index: r.index }))\r
-                       const keypairs: KeyPair[] = await this.#pool.work('divide', data)\r
+                       const pool = new Pool(`const Blake2b = ${Blake2b}\n${NanoNaCl}`)\r
+                       const keypairs: KeyPair[] = await pool.work('divide', data)\r
+                       pool.dismiss()\r
                        for (const keypair of keypairs) {\r
                                if (keypair.privateKey == null) throw new RangeError('Account private key missing')\r
                                if (keypair.publicKey == null) throw new RangeError('Account public key missing')\r
@@ -266,7 +266,6 @@ abstract class Wallet {
 */\r
 export class Bip44Wallet extends Wallet {\r
        static #isInternal: boolean = false\r
-       #pool: Pool\r
 \r
        constructor (seed: string, mnemonic?: Bip39Mnemonic, id?: string) {\r
                if (!Bip44Wallet.#isInternal) {\r
@@ -274,7 +273,6 @@ export class Bip44Wallet extends Wallet {
                }\r
                Bip44Wallet.#isInternal = false\r
                super(seed, mnemonic, id)\r
-               this.#pool = new Pool(Bip44Ckd)\r
        }\r
 \r
        /**\r
@@ -423,9 +421,11 @@ export class Bip44Wallet extends Wallet {
        * @returns {Promise<Account>}\r
        */\r
        async ckd (indexes: number[]): Promise<KeyPair[]> {\r
+               const pool = new Pool(Bip44Ckd)\r
                const data: any = []\r
                indexes.forEach(i => data.push({ seed: this.seed, index: i }))\r
-               const privateKeys: KeyPair[] = await this.#pool.work('divide', data)\r
+               const privateKeys: KeyPair[] = await pool.work('divide', data)\r
+               pool.dismiss()\r
                return privateKeys\r
        }\r
 }\r
index 7347f65c1e7241eb31f33ae20557e01db2915df7..010cfaaa7eec66e1729c029545d4b6deb433e803 100644 (file)
--- a/test.html
+++ b/test.html
@@ -2,75 +2,75 @@
 
 <head>
        <link rel="icon" href="./favicon.ico">
-       <script type="module" src="./dist/global.min.js"></script>
-       <script type="module" src="./dist/test.min.js"></script>
+       <!-- <script type="module" src="./dist/global.min.js"></script> -->
+       <!-- <script type="module" src="./dist/test.min.js"></script> -->
        <script type="module" src="./dist/perf.min.js"></script>
-       <!-- <script type="module">
-               console.log(`bip44`)
-               const bipWallet = await libnemo.Bip44Wallet.create('test')
-               await bipWallet.unlock('test')
-               console.log(bipWallet.mnemonic)
-               let now = performance.now()
-               const bipAccounts = await bipWallet.accounts(0, 0x2000)
-               console.log(`${bipAccounts.length} bip44 accounts done: ${performance.now() - now} ms`)
-               const bipAccount = bipAccounts[0]
-               console.log(bipAccount.privateKey)
-               console.log(bipAccount.publicKey)
-               console.log(bipAccount.address)
+       <script type="module">
+               // console.log(`bip44`)
+               // const bipWallet = await libnemo.Bip44Wallet.create('test')
+               // await bipWallet.unlock('test')
+               // console.log(bipWallet.mnemonic)
+               // let now = performance.now()
+               // const bipAccounts = await bipWallet.accounts(0, 0x2000)
+               // console.log(`${bipAccounts.length} bip44 accounts done: ${performance.now() - now} ms`)
+               // const bipAccount = bipAccounts[0]
+               // console.log(bipAccount.privateKey)
+               // console.log(bipAccount.publicKey)
+               // console.log(bipAccount.address)
 
-               console.log(`blake2b`)
-               const blakeWallet = await libnemo.Blake2bWallet.create('test')
-               await blakeWallet.unlock('test')
-               console.log(blakeWallet.mnemonic)
-               now = performance.now()
-               const blakeAccounts = await blakeWallet.accounts(0, 0x2000)
-               console.log(`${blakeAccounts.length} blake2b accounts done: ${performance.now() - now} ms`)
-               const blakeAccount = blakeAccounts[0]
-               console.log(blakeAccount.privateKey)
-               console.log(blakeAccount.publicKey)
-               console.log(blakeAccount.address)
+               // console.log(`blake2b`)
+               // const blakeWallet = await libnemo.Blake2bWallet.create('test')
+               // await blakeWallet.unlock('test')
+               // console.log(blakeWallet.mnemonic)
+               // now = performance.now()
+               // const blakeAccounts = await blakeWallet.accounts(0, 0x2000)
+               // console.log(`${blakeAccounts.length} blake2b accounts done: ${performance.now() - now} ms`)
+               // const blakeAccount = blakeAccounts[0]
+               // console.log(blakeAccount.privateKey)
+               // console.log(blakeAccount.publicKey)
+               // console.log(blakeAccount.address)
 
-               console.log('PoW')
-               let blocks = []
-               console.log(`send blocks`)
-               now = performance.now()
-               for (let i = 0; i < 10; i++) {
-                       const block = new libnemo.SendBlock(
-                               'nano_1xmastiputrdrhnf35jdh4yj1q339tyuk86w3k6oy5knede8zfowpa1rgjpn',
-                               '100000000000000000000000000',
-                               'nano_1xmastreedxwfhpktqxppwgwwhdx1p6hiskpw7jt8g5y19khyy38axg4tohm',
-                               '1',
-                               'nano_3rw4un6ys57hrb39sy1qx8qy5wukst1iiponztrz9qiz6qqa55kxzx4491or',
-                               '4E5004CA14899B8F9AABA7A76D010F73E6BAE54948912588B8C4FE0A3B558CA5')
-                       try {
-                               await block.pow()
-                               console.log(`block ${i + 1} work: ${block.work}`)
-                       } catch (err) {
-                               console.error(err)
-                       }
-                       blocks.push(block)
-               }
-               console.log(`send pow done (${performance.now() - now} ms)`)
-               now = performance.now()
-               for (let i = 0; i < 10; i++) {
-                       const block = new libnemo.ReceiveBlock(
-                               'nano_1xmastiputrdrhnf35jdh4yj1q339tyuk86w3k6oy5knede8zfowpa1rgjpn',
-                               '0',
-                               'FB0D886948EF4BBC410C0C64C16291E31AB360BEB21B338988A6C37D89C24362',
-                               '1',
-                               'nano_3rw4un6ys57hrb39sy1qx8qy5wukst1iiponztrz9qiz6qqa55kxzx4491or',
-                               'B320F9AD2C3341E0FA6EFAF093C2D618036D10DB800F14830A928A44B8DD265C')
-                       try {
-                               await block.pow()
-                               console.log(`block ${i + 1} work: ${block.work}`)
-                       } catch (err) {
-                               console.error(err)
-                       }
-                       blocks.push(block)
-               }
-               console.log(`receive done (${performance.now() - now} ms)`)
-               console.log(`pow done (${performance.now() - now} ms)`)
-       </script> -->
+               // console.log('PoW')
+               // let blocks = []
+               // console.log(`send blocks`)
+               // now = performance.now()
+               // for (let i = 0; i < 10; i++) {
+               //      const block = new libnemo.SendBlock(
+               //              'nano_1xmastiputrdrhnf35jdh4yj1q339tyuk86w3k6oy5knede8zfowpa1rgjpn',
+               //              '100000000000000000000000000',
+               //              'nano_1xmastreedxwfhpktqxppwgwwhdx1p6hiskpw7jt8g5y19khyy38axg4tohm',
+               //              '1',
+               //              'nano_3rw4un6ys57hrb39sy1qx8qy5wukst1iiponztrz9qiz6qqa55kxzx4491or',
+               //              '4E5004CA14899B8F9AABA7A76D010F73E6BAE54948912588B8C4FE0A3B558CA5')
+               //      try {
+               //              await block.pow()
+               //              console.log(`block ${i + 1} work: ${block.work}`)
+               //      } catch (err) {
+               //              console.error(err)
+               //      }
+               //      blocks.push(block)
+               // }
+               // console.log(`send pow done (${performance.now() - now} ms)`)
+               // now = performance.now()
+               // for (let i = 0; i < 10; i++) {
+               //      const block = new libnemo.ReceiveBlock(
+               //              'nano_1xmastiputrdrhnf35jdh4yj1q339tyuk86w3k6oy5knede8zfowpa1rgjpn',
+               //              '0',
+               //              'FB0D886948EF4BBC410C0C64C16291E31AB360BEB21B338988A6C37D89C24362',
+               //              '1',
+               //              'nano_3rw4un6ys57hrb39sy1qx8qy5wukst1iiponztrz9qiz6qqa55kxzx4491or',
+               //              'B320F9AD2C3341E0FA6EFAF093C2D618036D10DB800F14830A928A44B8DD265C')
+               //      try {
+               //              await block.pow()
+               //              console.log(`block ${i + 1} work: ${block.work}`)
+               //      } catch (err) {
+               //              console.error(err)
+               //      }
+               //      blocks.push(block)
+               // }
+               // console.log(`receive done (${performance.now() - now} ms)`)
+               // console.log(`pow done (${performance.now() - now} ms)`)
+       </script>
        <style>body{background:black;}</style>
 </head>