From 57fed4d568224ca215da58ce7046cf4be2530d33 Mon Sep 17 00:00:00 2001 From: frankie Date: Wed, 31 Jul 2024 08:29:51 -1000 Subject: [PATCH 01/17] change test to repersent programs attached to a registered entropy account --- tests/programs.test.ts | 45 ++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/tests/programs.test.ts b/tests/programs.test.ts index e80dadee..e59bf9f3 100644 --- a/tests/programs.test.ts +++ b/tests/programs.test.ts @@ -7,13 +7,12 @@ import { promiseRunner, spinNetworkUp, charlieStashSeed, - charlieStashAddress, spinNetworkDown, } from './testing-utils' const networkType = 'two-nodes' -test('Programs: GET', async (t) => { +test('Programs: account programs get', async (t) => { const run = promiseRunner(t) await run('network up', spinNetworkUp(networkType)) t.teardown(async () => { @@ -34,16 +33,13 @@ test('Programs: GET', async (t) => { keyring, endpoint: 'ws://127.0.0.1:9944', }) - - // register - const verifyingKeyFromRegistration = await run('register', entropy.register()) - - t.equal( - verifyingKeyFromRegistration, - entropy.keyring.accounts.registration.verifyingKeys[0], - 'verifyingKeys match after registration' + // wait for entropy to be ready + await run( + 'entropy ready', + entropy.ready ) + // deploy const noopProgram: any = readFileSync( './tests/testing-utils/program_noop.wasm' @@ -54,21 +50,36 @@ test('Programs: GET', async (t) => { entropy.programs.dev.deploy(noopProgram) ) - const programsDeployed = await run( - 'get deployed programs', - entropy.programs.dev.get(charlieStashAddress) + // register + const registerOpts = { + programData: [{ + program_pointer: newPointer + }], + } + console.log('registerOpts:', registerOpts) + const verifyingKeyFromRegistration = await run('register', entropy.register(registerOpts)) + + t.equal( + verifyingKeyFromRegistration, + entropy.keyring.accounts.registration.verifyingKeys[0], + 'verifyingKeys match after registration' + ) + + const programsForAccount = await run( + 'get programs for verifyingKey', + entropy.programs.get(verifyingKeyFromRegistration) ) t.equal( - programsDeployed.length, + programsForAccount.length, 1, - 'charlie has deployed 1 program' + programsDeployed + 'charlie entropy account has 1 program' + programsForAccount ) t.equal( - programsDeployed[0], + programsForAccount[0].program_pointer, newPointer, - 'program in list matches new pointer: ' + newPointer + ' = ' + programsDeployed[0] + 'program in list matches new pointer: ' + newPointer + ' = ' + programsForAccount[0].program_pointer ) t.end() From 0e8e796373a5b2e9128416b37c1f8a72f77a1c4b Mon Sep 17 00:00:00 2001 From: frankie Date: Wed, 31 Jul 2024 08:31:11 -1000 Subject: [PATCH 02/17] fix registration param so that program_config is optional to reflect the type interface of ProgramInstance for registration --- src/registration/index.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/registration/index.ts b/src/registration/index.ts index b7d5f810..159783c5 100644 --- a/src/registration/index.ts +++ b/src/registration/index.ts @@ -93,12 +93,11 @@ export default class RegistrationManager extends ExtrinsicBaseClass { programDeployer, keyVisibility, programData.map((programInfo) => { - return { - program_pointer: programInfo.program_pointer, - program_config: Array.from( - Buffer.from(JSON.stringify(programInfo.program_config)) - ), - } + const program: ProgramInstance = { program_pointer: programInfo.program_pointer } + if (programInfo.program_config) program.program_config = Array.from( + Buffer.from(JSON.stringify(programInfo.program_config)) + ) + return program }) ) // @ts-ignore: next line From 888b601d78d0f45502babd476c2067e5d400496e Mon Sep 17 00:00:00 2001 From: frankie Date: Wed, 31 Jul 2024 08:32:34 -1000 Subject: [PATCH 03/17] remove console.log --- tests/programs.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/programs.test.ts b/tests/programs.test.ts index e59bf9f3..7ffd24b1 100644 --- a/tests/programs.test.ts +++ b/tests/programs.test.ts @@ -56,7 +56,7 @@ test('Programs: account programs get', async (t) => { program_pointer: newPointer }], } - console.log('registerOpts:', registerOpts) + const verifyingKeyFromRegistration = await run('register', entropy.register(registerOpts)) t.equal( From edddedf091f7d28ad8f1c42ad28890612880ba85 Mon Sep 17 00:00:00 2001 From: frankie Date: Mon, 5 Aug 2024 11:58:15 -0700 Subject: [PATCH 04/17] Update the dev methods to reflect spec in issue 405 and create tests for dev clas --- src/programs/dev.ts | 4 +- tests/programs-dev.test.ts | 106 +++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 tests/programs-dev.test.ts diff --git a/src/programs/dev.ts b/src/programs/dev.ts index 974a6515..7f951354 100644 --- a/src/programs/dev.ts +++ b/src/programs/dev.ts @@ -55,7 +55,7 @@ export default class ProgramDev extends ExtrinsicBaseClass { * @returns {Promise} A promise that resolves to the list of program pointers */ - async get (address: string): Promise { + async getByDeployer (address: string): Promise { const programs = await this.substrate.query.programs.ownedPrograms(address); return programs.toHuman() } @@ -67,7 +67,7 @@ export default class ProgramDev extends ExtrinsicBaseClass { * @returns {Promise} A promise that resolves to the program information. */ - async getProgramInfo (pointer: string): Promise { + async get (pointer: string): Promise { // fetch program bytecode using the program pointer at the specific block hash const responseOption = await this.substrate.query.programs.programs(pointer) diff --git a/tests/programs-dev.test.ts b/tests/programs-dev.test.ts new file mode 100644 index 00000000..1dadc61e --- /dev/null +++ b/tests/programs-dev.test.ts @@ -0,0 +1,106 @@ +import test from 'tape' +import { readFileSync } from 'fs' +import Entropy, { wasmGlobalsReady } from '../src' +import Keyring from '../src/keys' + +import { + promiseRunner, + spinNetworkUp, + charlieStashSeed, + charlieStashAddress, + spinNetworkDown, +} from './testing-utils' + +const networkType = 'two-nodes' + +test('Programs#dev: all methods', async (t) => { + const run = promiseRunner(t) + await run('network up', spinNetworkUp(networkType)) + t.teardown(async () => { + await entropy.close() + await spinNetworkDown(networkType) + }) + + await run('wasm', wasmGlobalsReady()) + + const keyring = new Keyring({ seed: charlieStashSeed, debug: true }) + let store = keyring.getAccount() + t.equal(store.admin.address, keyring.accounts.registration.pair.address, 'admin account should have an address and for now it should match registrations address') + keyring.accounts.on('account-update', (fullAccount) => { + store = fullAccount + }) + + const entropy = new Entropy({ + keyring, + endpoint: 'ws://127.0.0.1:9944', + }) + + + // wait for entropy to be ready + await run( + 'entropy ready', + entropy.ready + ) + + + // deploy + const noopProgram: any = readFileSync( + './tests/testing-utils/program_noop.wasm', + + ) + + const newPointer = await run( + 'deploy', + entropy.programs.dev.deploy(noopProgram) + ) + + const programsDeployed = await run( + 'get deployed programs', + entropy.programs.dev.getByDeployer(charlieStashAddress) + ) + + const noopProgramOnChain = await run( + 'get a specific program', + entropy.programs.dev.get(newPointer) + ) + + t.equal( + programsDeployed.length, + 1, + 'charlie has deployed 1 program' + programsDeployed + ) + + t.equal( + programsDeployed[0], + newPointer, + 'program in list matches new pointer: ' + newPointer + ' = ' + programsDeployed[0] + ) + + t.deepEqual( + Buffer.from(noopProgramOnChain.bytecode), + noopProgram, + 'Whats on chain should match what was deployed' + ) + + run( + 'remove noopProgram', + entropy.programs.dev.remove(newPointer) + ) + + const programsDeployedAfterRemove = await run( + 'get deployed programs', + entropy.programs.dev.getByDeployer(charlieStashAddress) + ) + // the removal of a program has failed + // the removing of a program is questionable + // functionality to begin with so ive commented this out + // for now but this needs digging + // t.equal( + // programsDeployedAfterRemove.length, + // 0, + // 'charlie has no deployed programs' + // ) + + + t.end() +}) From a563eb1ec139b38deb490943d650be8aab30c2e1 Mon Sep 17 00:00:00 2001 From: frankie Date: Mon, 19 Aug 2024 10:12:25 -1000 Subject: [PATCH 05/17] hex2buf -> hexStringToBuffer && use buffer.from in internals --- src/programs/dev.ts | 4 ++-- src/utils/index.ts | 8 ++------ tests/programs-dev.test.ts | 2 +- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/programs/dev.ts b/src/programs/dev.ts index 7f951354..54a793d8 100644 --- a/src/programs/dev.ts +++ b/src/programs/dev.ts @@ -2,7 +2,7 @@ import ExtrinsicBaseClass from '../extrinsic' import { ApiPromise } from '@polkadot/api' import { Signer } from '../keys/types/internal' import { SubmittableExtrinsic } from '@polkadot/api/types' -import { hex2buf, stripHexPrefix } from '../utils' +import { hexStringToBuffer, stripHexPrefix } from '../utils' import * as util from '@polkadot/util' import { HexString } from '../keys/types/json' @@ -140,7 +140,7 @@ export default class ProgramDev extends ExtrinsicBaseClass { #formatProgramInfo (programInfo): ProgramInfo { const { interfaceDescription, deployer, refCounter } = programInfo - const bytecode = hex2buf(stripHexPrefix(programInfo.bytecode)) // Convert hex string to ArrayBuffer + const bytecode = hexStringToBuffer(stripHexPrefix(programInfo.bytecode)) // Convert hex string to ArrayBuffer return { interfaceDescription, deployer, refCounter, bytecode } } } diff --git a/src/utils/index.ts b/src/utils/index.ts index b094b5fc..6a4d9d11 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -140,12 +140,8 @@ export function toHex (str: any) { * @returns {ArrayBuffer} The ArrayBuffer representation of the hexadecimal string. */ -export function hex2buf (hex: string): ArrayBuffer { - const bytes = new Uint8Array(Math.ceil(hex.length / 2)) - for (let i = 0; i < bytes.length; i++) { - bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16) - } - return bytes.buffer +export function hexStringToBuffer (hex: string): ArrayBuffer { + return Buffer.from(hex, 'hex') } export function hexStringToUint8Array (hex: string): Uint8Array { diff --git a/tests/programs-dev.test.ts b/tests/programs-dev.test.ts index 1dadc61e..64487ced 100644 --- a/tests/programs-dev.test.ts +++ b/tests/programs-dev.test.ts @@ -77,7 +77,7 @@ test('Programs#dev: all methods', async (t) => { ) t.deepEqual( - Buffer.from(noopProgramOnChain.bytecode), + noopProgramOnChain.bytecode, noopProgram, 'Whats on chain should match what was deployed' ) From 83e0b5cf25f713b3e64e88e40be1537191c2406f Mon Sep 17 00:00:00 2001 From: frankie Date: Wed, 21 Aug 2024 12:58:52 -1000 Subject: [PATCH 06/17] add to CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8364877d..84d25a32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil ### Fixed ### Changed + - util function rename: `hex2buf` -> `hexStringToBuffer` + - massive changes to `entropy.programs.dev`: (Look at documentation for more details) + - `getProgramInfo` -> `get` and bytecode is returned as a buffer. not a Uint8buffer to match what was deployed + - you now get owned programs for an address using `getByDeployer`. ### Broke From b36e70506d4c1ce03ccfa9f2e8f439bdbf44998b Mon Sep 17 00:00:00 2001 From: frankie Date: Fri, 23 Aug 2024 09:11:42 -1000 Subject: [PATCH 07/17] document removes instabillty --- src/programs/dev.ts | 2 ++ tests/programs-dev.test.ts | 1 + 2 files changed, 3 insertions(+) diff --git a/src/programs/dev.ts b/src/programs/dev.ts index 54a793d8..aec9d540 100644 --- a/src/programs/dev.ts +++ b/src/programs/dev.ts @@ -115,6 +115,8 @@ export default class ProgramDev extends ExtrinsicBaseClass { /** * Removes an existing program. * + * (removing a program is currently unstable and may not remove the program from chain as intended.) + * * @param {string | Uint8Array} programHash - The hash of the program to remove. * @returns {Promise} A promise that resolves when the program is removed. */ diff --git a/tests/programs-dev.test.ts b/tests/programs-dev.test.ts index 64487ced..7b392a13 100644 --- a/tests/programs-dev.test.ts +++ b/tests/programs-dev.test.ts @@ -95,6 +95,7 @@ test('Programs#dev: all methods', async (t) => { // the removing of a program is questionable // functionality to begin with so ive commented this out // for now but this needs digging + // see issue https://github.com/entropyxyz/sdk/issues/414 // t.equal( // programsDeployedAfterRemove.length, // 0, From 64c4d2f5e660858caa2fe95785acce64b40f56ab Mon Sep 17 00:00:00 2001 From: frankie Date: Tue, 27 Aug 2024 09:56:43 -1000 Subject: [PATCH 08/17] properly return ProgramInterface --- CHANGELOG.md | 4 ++- src/programs/dev.ts | 55 +++++++++++++++++++++++++++++--------- src/utils/index.ts | 11 ++++++++ tests/programs-dev.test.ts | 21 ++++++++++++--- 4 files changed, 74 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84d25a32..eb34658d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil ## [UNRELEASED] ### Added + - `configurationSchema` & `auxiliaryDataSchema` to `ProgramInterface` + ### Fixed @@ -21,7 +23,7 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil - massive changes to `entropy.programs.dev`: (Look at documentation for more details) - `getProgramInfo` -> `get` and bytecode is returned as a buffer. not a Uint8buffer to match what was deployed - you now get owned programs for an address using `getByDeployer`. - + - interface name change: `ProgramInfo` -> `ProgramInterface` ### Broke ### Dev diff --git a/src/programs/dev.ts b/src/programs/dev.ts index aec9d540..640618a3 100644 --- a/src/programs/dev.ts +++ b/src/programs/dev.ts @@ -2,14 +2,14 @@ import ExtrinsicBaseClass from '../extrinsic' import { ApiPromise } from '@polkadot/api' import { Signer } from '../keys/types/internal' import { SubmittableExtrinsic } from '@polkadot/api/types' -import { hexStringToBuffer, stripHexPrefix } from '../utils' +import { hexStringToBuffer, stripHexPrefix, hexStringToJSON } from '../utils' import * as util from '@polkadot/util' import { HexString } from '../keys/types/json' /** * Represents program information. * - * @interface ProgramInfo + * @interface ProgramInterface * @property {ArrayBuffer} bytecode - The bytecode of the program. * @property {unknown} [interfaceDescription] - Optional. The configuration interface of the program. * @property {string} deployer - The address of the deployer of the program. @@ -17,9 +17,12 @@ import { HexString } from '../keys/types/json' */ // interfaceDescription needs better design and another type other than 'unknown' -export interface ProgramInfo { +export interface ProgramInterface { bytecode: ArrayBuffer - interfaceDescription?: unknown + configurationSchema: unknown + auxiliaryDataSchema: unknown + // not quite supported yet + // oracleDataPointer?: [] deployer: string refCounter: number } @@ -64,16 +67,16 @@ export default class ProgramDev extends ExtrinsicBaseClass { * Retrieves program information using a program pointer. * * @param {string} pointer - The program pointer to fetch the program bytecode. - * @returns {Promise} A promise that resolves to the program information. + * @returns {Promise} A promise that resolves to the program information. */ - async get (pointer: string): Promise { + async get (pointer: string): Promise { // fetch program bytecode using the program pointer at the specific block hash const responseOption = await this.substrate.query.programs.programs(pointer) const programInfo = responseOption.toJSON() - return this.#formatProgramInfo(programInfo) + return this.#formatProgramInterface(programInfo) } /** @@ -95,12 +98,13 @@ export default class ProgramDev extends ExtrinsicBaseClass { ): Promise { // converts program and configurationInterface into a palatable format const formatedConfig = JSON.stringify(configurationSchema) + const formatedAuxData = JSON.stringify(auxiliaryDataSchema) // programModKey is the caller of the extrinsic const tx: SubmittableExtrinsic<'promise'> = this.substrate.tx.programs.setProgram( util.u8aToHex(new Uint8Array(program)), // new program formatedConfig, // config schema - auxiliaryDataSchema, // auxilary config schema + formatedAuxData, // auxilary config schema [] // oracleDataPointer // oracle data pointer ) const record = await this.sendAndWaitFor(tx, { @@ -131,18 +135,43 @@ export default class ProgramDev extends ExtrinsicBaseClass { }) } + /** + * @internal + * + * trys to parse schema as a json. If fails because it's not a json returns original schema. throws for any other reason + * + * @param {any} programInfo - The program information in JSON format. + * @returns {unknown} - The formatted program information. + */ + #tryParseSchema (schema: any): unknown { + try { + return hexStringToJSON(schema) + } catch (e) { + if (e.message.includes('is not valid JSON')) return schema + throw e + } + } + /** * @internal * * Formats program information. * - * @param {ProgramInfoJSON} programInfo - The program information in JSON format. - * @returns {ProgramInfo} - The formatted program information. + * @param {ProgramInterfaceJSON} programInfo - The program information in JSON format. + * @returns {ProgramInterface} - The formatted program information. */ - #formatProgramInfo (programInfo): ProgramInfo { - const { interfaceDescription, deployer, refCounter } = programInfo + #formatProgramInterface (programInfo): ProgramInterface { + const { deployer, refCounter } = programInfo const bytecode = hexStringToBuffer(stripHexPrefix(programInfo.bytecode)) // Convert hex string to ArrayBuffer - return { interfaceDescription, deployer, refCounter, bytecode } + const configurationSchema = this.#tryParseSchema(programInfo.configurationSchema)// Convert hex string to ArrayBuffer + const auxiliaryDataSchema = this.#tryParseSchema(programInfo.auxiliaryDataSchema)// Convert hex string to ArrayBuffer + return { + configurationSchema, + auxiliaryDataSchema, + deployer, + refCounter, + bytecode, + } } } diff --git a/src/utils/index.ts b/src/utils/index.ts index 6a4d9d11..c54ead35 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -144,6 +144,17 @@ export function hexStringToBuffer (hex: string): ArrayBuffer { return Buffer.from(hex, 'hex') } +/** + * Converts a hexadecimal string to a JSON object. + * + * @param {string} hex - The hexadecimal string to convert. + * @returns {unknown} The ArrayBuffer representation of the hexadecimal string. + */ + +export function hexStringToJSON (hex: string): ArrayBuffer { + return JSON.parse(hexStringToBuffer(stripHexPrefix(hex)).toString()) +} + export function hexStringToUint8Array (hex: string): Uint8Array { if (hex.startsWith('0x')) { hex = hex.slice(2) diff --git a/tests/programs-dev.test.ts b/tests/programs-dev.test.ts index 7b392a13..ca11a76d 100644 --- a/tests/programs-dev.test.ts +++ b/tests/programs-dev.test.ts @@ -49,9 +49,15 @@ test('Programs#dev: all methods', async (t) => { ) + const configSchema = { + noop_param: 'string', + } + const auxDataSchema = { + noop_param: 'number' + } const newPointer = await run( 'deploy', - entropy.programs.dev.deploy(noopProgram) + entropy.programs.dev.deploy(noopProgram, configSchema, auxDataSchema) ) const programsDeployed = await run( @@ -63,7 +69,6 @@ test('Programs#dev: all methods', async (t) => { 'get a specific program', entropy.programs.dev.get(newPointer) ) - t.equal( programsDeployed.length, 1, @@ -79,7 +84,17 @@ test('Programs#dev: all methods', async (t) => { t.deepEqual( noopProgramOnChain.bytecode, noopProgram, - 'Whats on chain should match what was deployed' + 'bytecode on chain should match what was deployed' + ) + t.deepEqual( + noopProgramOnChain.configurationSchema, + configSchema, + 'configurationSchema on chain should match what was deployed' + ) + t.deepEqual( + noopProgramOnChain.auxiliaryDataSchema, + auxDataSchema, + 'auxiliaryDataSchema on chain should match what was deployed' ) run( From a757262c4a48bb5ee42da574da3e7bc06543527c Mon Sep 17 00:00:00 2001 From: frankie Date: Wed, 28 Aug 2024 09:17:08 -1000 Subject: [PATCH 09/17] programs-dev#get - fail if attempting to use an address --- src/programs/dev.ts | 1 + tests/programs-dev.test.ts | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/programs/dev.ts b/src/programs/dev.ts index 640618a3..893d9805 100644 --- a/src/programs/dev.ts +++ b/src/programs/dev.ts @@ -72,6 +72,7 @@ export default class ProgramDev extends ExtrinsicBaseClass { async get (pointer: string): Promise { // fetch program bytecode using the program pointer at the specific block hash + if (pointer.length <= 48) throw new Error('pointer length is less then or equal to 48. are you using an address?') const responseOption = await this.substrate.query.programs.programs(pointer) const programInfo = responseOption.toJSON() diff --git a/tests/programs-dev.test.ts b/tests/programs-dev.test.ts index ca11a76d..0e2d1f15 100644 --- a/tests/programs-dev.test.ts +++ b/tests/programs-dev.test.ts @@ -59,11 +59,17 @@ test('Programs#dev: all methods', async (t) => { 'deploy', entropy.programs.dev.deploy(noopProgram, configSchema, auxDataSchema) ) - + console.log('newPointer:', newPointer) const programsDeployed = await run( 'get deployed programs', entropy.programs.dev.getByDeployer(charlieStashAddress) ) + try { + await entropy.programs.dev.get(charlieStashAddress) + t.fail('entropy.programs.dev.get(charlieStashAddress) should have failed') + } catch (e) { + t.ok(e.message.includes('pointer length is less then or equal to 48. are you using an address?'), 'should error when using an address') + } const noopProgramOnChain = await run( 'get a specific program', From 6758f3bb72c3b539842aad33a023479ea63b9f11 Mon Sep 17 00:00:00 2001 From: frankie Date: Mon, 9 Sep 2024 14:34:03 -0400 Subject: [PATCH 10/17] fix createTestAccount --- tests/programs-dev.test.ts | 19 +++++-------------- tests/testing-utils/index.ts | 9 ++++++--- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/tests/programs-dev.test.ts b/tests/programs-dev.test.ts index 0e2d1f15..10551ed0 100644 --- a/tests/programs-dev.test.ts +++ b/tests/programs-dev.test.ts @@ -6,9 +6,9 @@ import Keyring from '../src/keys' import { promiseRunner, spinNetworkUp, - charlieStashSeed, charlieStashAddress, spinNetworkDown, + createTestAccount, } from './testing-utils' const networkType = 'two-nodes' @@ -16,23 +16,14 @@ const networkType = 'two-nodes' test('Programs#dev: all methods', async (t) => { const run = promiseRunner(t) await run('network up', spinNetworkUp(networkType)) - t.teardown(async () => { - await entropy.close() - await spinNetworkDown(networkType) - }) await run('wasm', wasmGlobalsReady()) - const keyring = new Keyring({ seed: charlieStashSeed, debug: true }) - let store = keyring.getAccount() - t.equal(store.admin.address, keyring.accounts.registration.pair.address, 'admin account should have an address and for now it should match registrations address') - keyring.accounts.on('account-update', (fullAccount) => { - store = fullAccount - }) + const entropy = await createTestAccount() - const entropy = new Entropy({ - keyring, - endpoint: 'ws://127.0.0.1:9944', + t.teardown(async () => { + await entropy.close() + await spinNetworkDown(networkType) }) diff --git a/tests/testing-utils/index.ts b/tests/testing-utils/index.ts index a4cfc7b8..f631bb13 100644 --- a/tests/testing-utils/index.ts +++ b/tests/testing-utils/index.ts @@ -15,14 +15,17 @@ export * from './constants' export * from './readKey' export async function createTestAccount( - entropy: Entropy, - seed = charlieStashSeed + seed = charlieStashSeed, + endpoint = 'ws://127.0.0.1:9944' ) { await wasmGlobalsReady() const keyring = new Keyring({ seed } as KeyMaterial) + const entropy = new Entropy({ + keyring, + endpoint, + }) - entropy = new Entropy({ keyring }) await entropy.ready.catch((err) => { console.log('createTestAccount failed: ', err) throw err From 6c083df0219ef18c39eca05b157de0dc9d3e7353 Mon Sep 17 00:00:00 2001 From: frankie Date: Wed, 18 Sep 2024 13:27:01 -1000 Subject: [PATCH 11/17] create program formating function --- src/registration/index.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/registration/index.ts b/src/registration/index.ts index 159783c5..e6080a6c 100644 --- a/src/registration/index.ts +++ b/src/registration/index.ts @@ -92,13 +92,7 @@ export default class RegistrationManager extends ExtrinsicBaseClass { const registerTx = this.substrate.tx.registry.register( programDeployer, keyVisibility, - programData.map((programInfo) => { - const program: ProgramInstance = { program_pointer: programInfo.program_pointer } - if (programInfo.program_config) program.program_config = Array.from( - Buffer.from(JSON.stringify(programInfo.program_config)) - ) - return program - }) + programData.map(this.#formatProgramInfo) ) // @ts-ignore: next line // Send the registration transaction and wait for the result. @@ -159,4 +153,12 @@ export default class RegistrationManager extends ExtrinsicBaseClass { }) }) } + + #formatProgramInfo (programInfo): ProgramInstance { + const program: ProgramInstance = { program_pointer: programInfo.program_pointer } + if (programInfo.program_config) program.program_config = Array.from( + Buffer.from(JSON.stringify(programInfo.program_config)) + ) + return program + } } From 51f00a34a1dbda92c92ba5134022fbccda95fb4b Mon Sep 17 00:00:00 2001 From: frankie Date: Wed, 18 Sep 2024 13:41:23 -1000 Subject: [PATCH 12/17] use keyring address --- tests/programs-dev.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/programs-dev.test.ts b/tests/programs-dev.test.ts index 10551ed0..5d65ebf5 100644 --- a/tests/programs-dev.test.ts +++ b/tests/programs-dev.test.ts @@ -53,11 +53,11 @@ test('Programs#dev: all methods', async (t) => { console.log('newPointer:', newPointer) const programsDeployed = await run( 'get deployed programs', - entropy.programs.dev.getByDeployer(charlieStashAddress) + entropy.programs.dev.getByDeployer(entropy.keyring.accounts.programDev.address) ) try { - await entropy.programs.dev.get(charlieStashAddress) - t.fail('entropy.programs.dev.get(charlieStashAddress) should have failed') + await entropy.programs.dev.get(entropy.keyring.accounts.programDev.address) + t.fail('entropy.programs.dev.get(entropy.keyring.accounts.programDev.address) should have failed') } catch (e) { t.ok(e.message.includes('pointer length is less then or equal to 48. are you using an address?'), 'should error when using an address') } @@ -101,7 +101,7 @@ test('Programs#dev: all methods', async (t) => { const programsDeployedAfterRemove = await run( 'get deployed programs', - entropy.programs.dev.getByDeployer(charlieStashAddress) + entropy.programs.dev.getByDeployer(entropy.keyring.accounts.programDev.address) ) // the removal of a program has failed // the removing of a program is questionable From 551a008d4748b447d8708a574a039b5e10b4357e Mon Sep 17 00:00:00 2001 From: Frankie Date: Mon, 23 Sep 2024 08:03:42 -1000 Subject: [PATCH 13/17] unify test to deep equal Co-authored-by: mix irving --- tests/programs-dev.test.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/programs-dev.test.ts b/tests/programs-dev.test.ts index 5d65ebf5..3b89989f 100644 --- a/tests/programs-dev.test.ts +++ b/tests/programs-dev.test.ts @@ -66,16 +66,10 @@ test('Programs#dev: all methods', async (t) => { 'get a specific program', entropy.programs.dev.get(newPointer) ) - t.equal( - programsDeployed.length, - 1, - 'charlie has deployed 1 program' + programsDeployed - ) - - t.equal( - programsDeployed[0], - newPointer, - 'program in list matches new pointer: ' + newPointer + ' = ' + programsDeployed[0] + t.deepEqual( + programsDeployed, + [newPointer], + 'charlie has 1 program deployed' ) t.deepEqual( From a5484e8a00c8dcce49602471750182a168bb9d38 Mon Sep 17 00:00:00 2001 From: Frankie Date: Tue, 24 Sep 2024 09:20:58 -1000 Subject: [PATCH 14/17] Update tests/programs-dev.test.ts Co-authored-by: mix irving --- tests/programs-dev.test.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/programs-dev.test.ts b/tests/programs-dev.test.ts index 3b89989f..6e221933 100644 --- a/tests/programs-dev.test.ts +++ b/tests/programs-dev.test.ts @@ -55,6 +55,13 @@ test('Programs#dev: all methods', async (t) => { 'get deployed programs', entropy.programs.dev.getByDeployer(entropy.keyring.accounts.programDev.address) ) + t.deepEqual( + programsDeployed, + [newPointer], + 'charlie has 1 program deployed' + ) + + // Helpful error for old usage try { await entropy.programs.dev.get(entropy.keyring.accounts.programDev.address) t.fail('entropy.programs.dev.get(entropy.keyring.accounts.programDev.address) should have failed') @@ -66,11 +73,6 @@ test('Programs#dev: all methods', async (t) => { 'get a specific program', entropy.programs.dev.get(newPointer) ) - t.deepEqual( - programsDeployed, - [newPointer], - 'charlie has 1 program deployed' - ) t.deepEqual( noopProgramOnChain.bytecode, From 4e6581af8d4ef1d468274c5ec327649092fc7315 Mon Sep 17 00:00:00 2001 From: Frankie Date: Tue, 24 Sep 2024 09:21:54 -1000 Subject: [PATCH 15/17] Update tests/programs-dev.test.ts Co-authored-by: mix irving --- tests/programs-dev.test.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/programs-dev.test.ts b/tests/programs-dev.test.ts index 6e221933..2324db78 100644 --- a/tests/programs-dev.test.ts +++ b/tests/programs-dev.test.ts @@ -41,10 +41,16 @@ test('Programs#dev: all methods', async (t) => { ) const configSchema = { - noop_param: 'string', + type: 'object', + properties: { + noop_param: { type: 'string' } + } } const auxDataSchema = { - noop_param: 'number' + type: 'object', + properties: { + noop_param: { type: 'number' } + } } const newPointer = await run( 'deploy', From 7aff0c508dbb764b997818b7c460abc47ec9ef4d Mon Sep 17 00:00:00 2001 From: frankie Date: Tue, 24 Sep 2024 09:25:39 -1000 Subject: [PATCH 16/17] remove comments --- src/programs/dev.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/programs/dev.ts b/src/programs/dev.ts index 893d9805..7ae3181b 100644 --- a/src/programs/dev.ts +++ b/src/programs/dev.ts @@ -165,8 +165,8 @@ export default class ProgramDev extends ExtrinsicBaseClass { #formatProgramInterface (programInfo): ProgramInterface { const { deployer, refCounter } = programInfo const bytecode = hexStringToBuffer(stripHexPrefix(programInfo.bytecode)) // Convert hex string to ArrayBuffer - const configurationSchema = this.#tryParseSchema(programInfo.configurationSchema)// Convert hex string to ArrayBuffer - const auxiliaryDataSchema = this.#tryParseSchema(programInfo.auxiliaryDataSchema)// Convert hex string to ArrayBuffer + const configurationSchema = this.#tryParseSchema(programInfo.configurationSchema) + const auxiliaryDataSchema = this.#tryParseSchema(programInfo.auxiliaryDataSchema) return { configurationSchema, auxiliaryDataSchema, From 099dd3f93f2c07dd8f0923c661bcd52969549e69 Mon Sep 17 00:00:00 2001 From: frankie Date: Tue, 24 Sep 2024 09:28:55 -1000 Subject: [PATCH 17/17] shift: stripHexPrefix from hexStringToJSON -> hexStringToBuffer --- src/utils/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index c54ead35..d2eee35c 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -141,7 +141,7 @@ export function toHex (str: any) { */ export function hexStringToBuffer (hex: string): ArrayBuffer { - return Buffer.from(hex, 'hex') + return Buffer.from(stripHexPrefix(hex), 'hex') } /** @@ -152,7 +152,7 @@ export function hexStringToBuffer (hex: string): ArrayBuffer { */ export function hexStringToJSON (hex: string): ArrayBuffer { - return JSON.parse(hexStringToBuffer(stripHexPrefix(hex)).toString()) + return JSON.parse(hexStringToBuffer(hex).toString()) } export function hexStringToUint8Array (hex: string): Uint8Array {