From: Chris Duncan Date: Thu, 7 Aug 2025 19:43:32 +0000 (-0700) Subject: Start fixing block tests. X-Git-Tag: v0.10.5~43^2~52 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=a9c90b67d895a06f40bc63ca474bc7a6b9746010;p=libnemo.git Start fixing block tests. --- diff --git a/test/test.blocks.mjs b/test/test.blocks.mjs index 962ef55..bd6c233 100644 --- a/test/test.blocks.mjs +++ b/test/test.blocks.mjs @@ -27,16 +27,14 @@ await Promise.all([ await test('throw on negative balances', async () => { assert.throws(() => { new Block(ADDRESS_0, '7000000000000000000000000000000', '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D', ADDRESS_2) - .send('12000000000000000000000000000000') - .to(ADDRESS_1) + .send(ADDRESS_1, '12000000000000000000000000000000') }, { message: 'Negative balance' }) }) await test('allow zero balances', async () => { const block = new Block( ADDRESS_0, '9007199254740991', '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D', ADDRESS_2) - .send('9007199254740991') - .to(ADDRESS_1) + .send(ADDRESS_1, '9007199254740991') assert.notEqual(block.balance, 0) assert.equal(block.balance, BigInt(0)) }) @@ -44,22 +42,19 @@ await Promise.all([ await test('subtract balance from SendBlock correctly', async () => { const block = new Block( ADDRESS_0, '3000000000000000000000000000000', '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D', ADDRESS_2) - .send('2000000000000000000000000000000') - .to(ADDRESS_1) + .send(ADDRESS_1, '2000000000000000000000000000000') assert.equal(block.balance, 1000000000000000000000000000000n) }) await test('add balance from ReceiveBlock correctly', async () => { const block = new Block(ADDRESS_0, '2000000000000000000000000000000', '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D', ADDRESS_2) - .receive('1000000000000000000000000000000') - .from(RECEIVE_BLOCK.link) + .receive(RECEIVE_BLOCK.link, '1000000000000000000000000000000') assert.equal(block.balance, 3000000000000000000000000000000n) }) await test('fail to receive from address, link must be block hash', async () => { assert.throws(new Block(ADDRESS_0, '2000000000000000000000000000000', '92BA74A7D6DC7557F3EDA95ADC6341D51AC777A0A6FF0688A5C492AB2B2CB40D', ADDRESS_2) - .receive('1000000000000000000000000000000') - .from(ADDRESS_1)) + .receive(ADDRESS_1, '1000000000000000000000000000000')) }) }), @@ -71,8 +66,7 @@ await Promise.all([ await assert.resolves(wallet.unlock(PASSWORD)) const block = new Block(ADDRESS_0, '0', OPEN_BLOCK.previous, OPEN_BLOCK.representative) - .receive(OPEN_BLOCK.balance) - .from(OPEN_BLOCK.link) + .receive(OPEN_BLOCK.link, OPEN_BLOCK.balance) const signature = await wallet.sign(0, block, 'hex') assert.equal(block.signature, signature) @@ -82,8 +76,7 @@ await Promise.all([ await test('fail to sign open block with wallet when locked', async () => { const wallet = await Wallet.import('BIP-44', PASSWORD, BIP39_SEED) const block = new Block(OPEN_BLOCK.account, '0', OPEN_BLOCK.previous, OPEN_BLOCK.representative) - .receive(OPEN_BLOCK.balance) - .from(OPEN_BLOCK.link) + .receive(OPEN_BLOCK.link, OPEN_BLOCK.balance) await assert.rejects(wallet.sign(0, block)) assert.equal(block.hash, OPEN_BLOCK.hash) @@ -93,9 +86,8 @@ await Promise.all([ }) await test('sign open block with private key', async () => { - const block = await (new Block(OPEN_BLOCK.account, '0', OPEN_BLOCK.previous, OPEN_BLOCK.representative) - .receive(OPEN_BLOCK.balance) - .from(OPEN_BLOCK.link)) + const block = await new Block(OPEN_BLOCK.account, '0', OPEN_BLOCK.previous, OPEN_BLOCK.representative) + .receive(OPEN_BLOCK.link, OPEN_BLOCK.balance) .pow(OPEN_BLOCK.work) await block.sign(OPEN_BLOCK.key) assert.equal(block.hash, OPEN_BLOCK.hash) @@ -103,9 +95,8 @@ await Promise.all([ }) await test('sign receive block', async () => { - const block = await (new Block(RECEIVE_BLOCK.account, RECEIVE_BLOCK.balance, RECEIVE_BLOCK.previous, RECEIVE_BLOCK.representative) - .receive(0) - .from(RECEIVE_BLOCK.link)) + const block = await new Block(RECEIVE_BLOCK.account, RECEIVE_BLOCK.balance, RECEIVE_BLOCK.previous, RECEIVE_BLOCK.representative) + .receive(RECEIVE_BLOCK.link, 0) .pow(RECEIVE_BLOCK.work) await block.sign(RECEIVE_BLOCK.key) assert.equal(block.hash, RECEIVE_BLOCK.hash) @@ -114,8 +105,7 @@ await Promise.all([ await test('sign receive block without work', async () => { const block = new Block(RECEIVE_BLOCK.account, RECEIVE_BLOCK.balance, RECEIVE_BLOCK.previous, RECEIVE_BLOCK.representative) - .receive('0') - .from(RECEIVE_BLOCK.link) + .receive(RECEIVE_BLOCK.link, '0') await block.sign(RECEIVE_BLOCK.key) assert.equal(block.hash, RECEIVE_BLOCK.hash) assert.equal(block.signature, RECEIVE_BLOCK.signature) @@ -123,9 +113,8 @@ await Promise.all([ }) await test('sign send block', async () => { - const block = await (new Block(SEND_BLOCK.account, SEND_BLOCK.balance, SEND_BLOCK.previous, SEND_BLOCK.representative) - .send('0') - .to(SEND_BLOCK.link)) + const block = await new Block(SEND_BLOCK.account, SEND_BLOCK.balance, SEND_BLOCK.previous, SEND_BLOCK.representative) + .send(SEND_BLOCK.link, '0') .pow(SEND_BLOCK.work) await block.sign(SEND_BLOCK.key) assert.equal(block.hash, SEND_BLOCK.hash) @@ -133,21 +122,19 @@ await Promise.all([ }) await test('sign send block without work', async () => { - const block = new Block(SEND_BLOCK.account, SEND_BLOCK.balance, SEND_BLOCK.previous, SEND_BLOCK.representative) - .send(0n) - .to(SEND_BLOCK.link) - await block.sign(SEND_BLOCK.key) - //@ts-expect-error - assert.equal(block.hash.map(b => b.toString(16).padStart(2, '0')).join(''), SEND_BLOCK.hash) + const block = await new Block(SEND_BLOCK.account, SEND_BLOCK.balance, SEND_BLOCK.previous, SEND_BLOCK.representative) + .send(SEND_BLOCK.link, 0n) + .sign(SEND_BLOCK.key) + + assert.equal(block.hash, SEND_BLOCK.hash) assert.equal(block.signature, SEND_BLOCK.signature) assert.equal(block.work, '') }) await test('sign change rep block', async () => { const work = '0000000000000000' - const block = await (new Block('nano_3igf8hd4sjshoibbbkeitmgkp1o6ug4xads43j6e4gqkj5xk5o83j8ja9php', '3000000000000000000000000000000', '128106287002E595F479ACD615C818117FCB3860EC112670557A2467386249D4') - .change() - .to('nano_1anrzcuwe64rwxzcco8dkhpyxpi8kd7zsjc1oeimpc3ppca4mrjtwnqposrs')) + const block = await new Block('nano_3igf8hd4sjshoibbbkeitmgkp1o6ug4xads43j6e4gqkj5xk5o83j8ja9php', '3000000000000000000000000000000', '128106287002E595F479ACD615C818117FCB3860EC112670557A2467386249D4') + .change('nano_1anrzcuwe64rwxzcco8dkhpyxpi8kd7zsjc1oeimpc3ppca4mrjtwnqposrs') .pow(work) await block.sign('781186FB9EF17DB6E3D1056550D9FAE5D5BBADA6A6BC370E4CBB938B1DC71DA3') // Did not find a private key at nano docs for this address assert.equal(block.signature?.toUpperCase(), 'A3C3C66D6519CBC0A198E56855942DEACC6EF741021A1B11279269ADC587DE1DA53CD478B8A47553231104CF24D742E1BB852B0546B87038C19BAE20F9082B0D') @@ -155,18 +142,16 @@ await Promise.all([ }) await test('sign change rep block without work', async () => { - const block = new Block(ADDRESS_0, '0', 'F3C1D7B6EE97DA09D4C00538CEA93CBA5F74D78FD3FBE71347D2DFE7E53DF327') - .change() - .to('nano_34amtofxstsfyqcgphp8piij9u33widykq9wbz6ysjpxhbgmqu8btu1eexer') - await block.sign(PRIVATE_0) + const block = await new Block(ADDRESS_0, '0', 'F3C1D7B6EE97DA09D4C00538CEA93CBA5F74D78FD3FBE71347D2DFE7E53DF327') + .change('nano_34amtofxstsfyqcgphp8piij9u33widykq9wbz6ysjpxhbgmqu8btu1eexer') + .sign(PRIVATE_0) assert.equal(block.signature?.toUpperCase(), '2BD2F905E74B5BEE3E2277CED1D1E3F7535E5286B6E22F7B08A814AA9E5C4E1FEA69B61D60B435ADC2CE756E6EE5F5BE7EC691FE87E024A0B22A3D980CA5B305') assert.equal(block.work, '') }) await test('fail to sign open block without key', async () => { - const block = await (new Block(OPEN_BLOCK.account, '0', OPEN_BLOCK.previous, OPEN_BLOCK.representative) - .receive(OPEN_BLOCK.balance) - .from(OPEN_BLOCK.link)) + const block = await new Block(OPEN_BLOCK.account, '0', OPEN_BLOCK.previous, OPEN_BLOCK.representative) + .receive(OPEN_BLOCK.link, OPEN_BLOCK.balance) .pow(OPEN_BLOCK.work) //@ts-expect-error diff --git a/test/test.ledger.mjs b/test/test.ledger.mjs index f405f64..f506608 100644 --- a/test/test.ledger.mjs +++ b/test/test.ledger.mjs @@ -11,26 +11,22 @@ import { NANO_TEST_VECTORS } from './VECTORS.mjs' */ let Account /** -* @type {typeof import('../dist/types.d.ts').Ledger} +* @type {typeof import('../dist/types.d.ts').Block} */ -let Ledger +let Block /** -* @type {typeof import('../dist/types.d.ts').ReceiveBlock} +* @type {typeof import('../dist/types.d.ts').Ledger} */ -let ReceiveBlock +let Ledger /** * @type {typeof import('../dist/types.d.ts').Rpc} */ let Rpc -/** -* @type {typeof import('../dist/types.d.ts').SendBlock} -*/ -let SendBlock if (isNode) { - ({ Account, Ledger, ReceiveBlock, Rpc, SendBlock } = await import('../dist/nodejs.min.js')) + ({ Account, Block, Ledger, Rpc } = await import('../dist/nodejs.min.js')) } else { - ({ Account, Ledger, ReceiveBlock, Rpc, SendBlock } = await import('../dist/browser.min.js')) + ({ Account, Block, Ledger, Rpc } = await import('../dist/browser.min.js')) } const rpc = new Rpc(env.NODE_URL ?? '', env.API_KEY_NAME) @@ -43,6 +39,8 @@ await Promise.all([ /* node:coverage disable */ suite('Ledger hardware wallet', { skip: false || isNode || Ledger.isUnsupported }, async () => { + const { RECEIVE_BLOCK, SEND_BLOCK } = NANO_TEST_VECTORS + let wallet, account, openBlock, sendBlock, receiveBlock await test('request permissions', async () => { @@ -116,14 +114,8 @@ await Promise.all([ }) await test('sign open block from block', async () => { - openBlock = new ReceiveBlock( - account, - '0', - NANO_TEST_VECTORS.RECEIVE_BLOCK.link, - NANO_TEST_VECTORS.RECEIVE_BLOCK.balance, - NANO_TEST_VECTORS.RECEIVE_BLOCK.representative, - '0' - ) + openBlock = new Block(account, '0', RECEIVE_BLOCK.representative, account.publicKey) + .receive(RECEIVE_BLOCK.link, RECEIVE_BLOCK.balance) assert.ok(/^[A-Fa-f0-9]{64}$/.test(openBlock.hash)) assert.nullish(openBlock.signature) @@ -147,14 +139,8 @@ await Promise.all([ }) await test('sign send block from wallet which requires cache to be up-to-date', async () => { - sendBlock = new SendBlock( - account, - openBlock.balance, - account.address, - '0', - NANO_TEST_VECTORS.SEND_BLOCK.representative, - openBlock.hash - ) + sendBlock = new Block(account, openBlock.balance, SEND_BLOCK.representative, openBlock.hash) + .send(account.address, '0') assert.ok(/^[A-Fa-f0-9]{64}$/.test(sendBlock.hash)) assert.nullish(sendBlock.signature) @@ -167,14 +153,8 @@ await Promise.all([ }) await test('sign a receive block from block object which can accept previous block for cache', async () => { - receiveBlock = new ReceiveBlock( - account, - sendBlock.balance, - sendBlock.hash, - '0', - NANO_TEST_VECTORS.RECEIVE_BLOCK.representative, - sendBlock.hash - ) + receiveBlock = new Block(account, sendBlock.balance, RECEIVE_BLOCK.representative, sendBlock.hash) + .receive(sendBlock.hash, '0') assert.ok(/^[A-Fa-f0-9]{64}$/.test(sendBlock.hash)) assert.nullish(receiveBlock.signature) @@ -186,15 +166,24 @@ await Promise.all([ assert.ok(/^[A-Fa-f0-9]{128}$/.test(receiveBlock.signature ?? '')) }) + await test('sign a send block from wallet without frontier block inheriting cache contents', async () => { + sendBlock = new Block(account, receiveBlock.balance, SEND_BLOCK.representative, receiveBlock.hash) + .send(account.address, '0') + + assert.ok(/^[A-Fa-f0-9]{64}$/.test(sendBlock.hash)) + assert.nullish(sendBlock.signature) + assert.equal(sendBlock.account.publicKey, account.publicKey) + + const signature = await wallet.sign(0, sendBlock) + + assert.exists(sendBlock.signature) + assert.ok(/^[A-Fa-f0-9]{128}$/.test(sendBlock.signature ?? '')) + assert.equal(signature, sendBlock.signature) + }) + await test('sign a receive block from wallet including frontier block for cache', async () => { - sendBlock = new ReceiveBlock( - account, - receiveBlock.balance, - receiveBlock.hash, - '0', - NANO_TEST_VECTORS.RECEIVE_BLOCK.representative, - receiveBlock.hash - ) + sendBlock = new Block(account, receiveBlock.balance, RECEIVE_BLOCK.representative, receiveBlock.hash) + .receive(receiveBlock.hash, '0') assert.ok(/^[A-Fa-f0-9]{64}$/.test(receiveBlock.hash)) assert.nullish(sendBlock.signature) @@ -218,14 +207,8 @@ await Promise.all([ }) await test('fail to sign a block without caching frontier', async () => { - sendBlock = new SendBlock( - account, - receiveBlock.balance, - account.address, - '0', - NANO_TEST_VECTORS.SEND_BLOCK.representative, - receiveBlock.previous - ) + sendBlock = new Block(account, receiveBlock.balance, SEND_BLOCK.representative, receiveBlock.previous) + .send(account.address, '0') await assert.rejects(sendBlock.sign(0)) }) })