-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
180 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Entropy from "@entropyxyz/sdk"; | ||
import fs from "node:fs/promises" | ||
import { isAbsolute, join } from "node:path" | ||
import { u8aToHex } from "@polkadot/util" | ||
|
||
import { DeployProgramParams } from "./types" | ||
|
||
export async function deployProgram (entropy: Entropy, params: DeployProgramParams) { | ||
const bytecode = await loadFile(params.bytecodePath) | ||
const configurationSchema = await loadFile(params.configurationSchemaPath, 'json') | ||
const auxillaryDataSchema = await loadFile(params.auxillaryDataSchemaPath, 'json') | ||
// QUESTION: where / how are schema validated? | ||
|
||
return entropy.programs.dev.deploy( | ||
bytecode, | ||
jsonToHex(configurationSchema), | ||
jsonToHex(auxillaryDataSchema) | ||
) | ||
} | ||
|
||
function loadFile (path?: string, encoding?: string) { | ||
if (path === undefined) return | ||
|
||
const absolutePath = isAbsolute(path) | ||
? path | ||
: join(process.cwd(), path) | ||
|
||
switch (encoding) { | ||
case undefined: | ||
return fs.readFile(absolutePath) | ||
|
||
case 'json': | ||
return fs.readFile(absolutePath, 'utf-8') | ||
.then(string => JSON.parse(string)) | ||
|
||
default: | ||
throw Error('unknown encoding: ' + encoding) | ||
// return fs.readFile(absolutePath, encoding) | ||
} | ||
} | ||
|
||
function jsonToHex (obj?: object) { | ||
if (obj === undefined) return | ||
|
||
const encoder = new TextEncoder() | ||
const byteArray = encoder.encode(JSON.stringify(obj)) | ||
|
||
return u8aToHex(new Uint8Array(byteArray)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,77 @@ | ||
import test from 'tape' | ||
import { readFileSync } from 'node:fs' | ||
|
||
import { promiseRunner, charlieStashSeed, setupTest } from './testing-utils' | ||
import { addProgram } from '../src/flows/programs/add' | ||
import { viewPrograms } from '../src/flows/programs/view' | ||
import { removeProgram } from '../src/flows/programs/remove' | ||
import { AddProgramParams } from '../src/flows/programs/types' | ||
import { deployProgram } from '../src/flows/programs/deploy' | ||
|
||
const networkType = 'two-nodes' | ||
|
||
test('programs', async t => { | ||
const { run, entropy } = await setupTest(t, { seed: charlieStashSeed, networkType }) | ||
await run('charlie stash register', entropy.register()) | ||
const noopProgram: any = readFileSync( | ||
new URL('./programs/program_noop.wasm', import.meta.url) | ||
) | ||
const newPointer = await run( | ||
'deploy', | ||
entropy.programs.dev.deploy(noopProgram) | ||
) | ||
|
||
const noopProgramInstance: AddProgramParams = { | ||
programPointer: newPointer, | ||
programConfig: '', | ||
} | ||
|
||
t.test('Add Program', async ap => { | ||
const runAp = promiseRunner(ap) | ||
|
||
const programsBeforeAdd = await runAp('get programs initial', entropy.programs.get(entropy.programs.verifyingKey)) | ||
ap.equal(programsBeforeAdd.length, 1, 'charlie has 1 program') | ||
await runAp('adding program', addProgram(entropy, noopProgramInstance)) | ||
const programsAfterAdd = await runAp('get programs after add', entropy.programs.get(entropy.programs.verifyingKey)) | ||
ap.equal(programsAfterAdd.length, 2, 'charlie has 2 programs') | ||
ap.end() | ||
await run('register', entropy.register()) // TODO: consider removing this in favour of just testing add | ||
|
||
let programPointer1 | ||
|
||
t.test('programs - deploy', async t => { | ||
const run = promiseRunner(t) | ||
|
||
programPointer1 = await run ( | ||
'deploy!', | ||
deployProgram(entropy, { | ||
bytecodePath: './tests/programs/program_noop.wasm' | ||
}) | ||
) | ||
|
||
t.end() | ||
}) | ||
|
||
t.test('Remove Program', async rp => { | ||
const runRp = promiseRunner(rp) | ||
const programsBeforeRemove = await runRp('get programs initial', entropy.programs.get(entropy.programs.verifyingKey)) | ||
|
||
rp.equal(programsBeforeRemove.length, 2, 'charlie has 2 programs') | ||
await runRp('removing noop program', removeProgram(entropy, { programPointer: newPointer, verifyingKey: entropy.programs.verifyingKey })) | ||
const programsAfterRemove = await runRp('get programs initial', entropy.programs.get(entropy.programs.verifyingKey)) | ||
rp.equal(programsAfterRemove.length, 1, 'charlie has 1 less program') | ||
rp.end() | ||
const getPrograms = () => viewPrograms(entropy, { verifyingKey: entropy.programs.verifyingKey }) | ||
const verifyingKey = entropy.programs.verifyingKey | ||
|
||
t.test('programs - add', async t => { | ||
const run = promiseRunner(t) | ||
|
||
const programsBeforeAdd = await run('get programs initial', getPrograms()) | ||
t.equal(programsBeforeAdd.length, 1, 'charlie has 1 program') | ||
|
||
await run( | ||
'adding program', | ||
addProgram(entropy, { programPointer: programPointer1, programConfig: '' }) | ||
) | ||
const programsAfterAdd = await run('get programs after add', getPrograms()) | ||
t.equal(programsAfterAdd.length, 2, 'charlie has 2 programs') | ||
|
||
t.end() | ||
}) | ||
|
||
t.test('View Program', async vp => { | ||
const runVp = promiseRunner(vp) | ||
const programs = await runVp('get charlie programs', viewPrograms(entropy, { verifyingKey: entropy.programs.verifyingKey })) | ||
t.test('programs - remove', async t => { | ||
const run = promiseRunner(t) | ||
|
||
const programsBeforeRemove = await run('get programs initial', getPrograms()) | ||
t.equal(programsBeforeRemove.length, 2, 'charlie has 2 programs') | ||
|
||
await run( | ||
'removing noop program', | ||
removeProgram(entropy, { programPointer: programPointer1, verifyingKey }) | ||
) | ||
const programsAfterRemove = await run('get programs initial', getPrograms()) | ||
t.equal(programsAfterRemove.length, 1, 'charlie has 1 less program') | ||
|
||
t.end() | ||
}) | ||
|
||
t.test('programs - view', async t => { | ||
const run = promiseRunner(t) | ||
|
||
const programs = await run( | ||
'get charlie programs', | ||
viewPrograms(entropy, { verifyingKey }) | ||
) | ||
|
||
t.equal(programs.length, 1, 'charlie has 1 program') | ||
|
||
vp.equal(programs.length, 1, 'charlie has 1 program') | ||
vp.end() | ||
t.end() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters