diff --git a/libs/as-sdk-integration-tests/assembly/index.ts b/libs/as-sdk-integration-tests/assembly/index.ts index 333d7de..843c34f 100644 --- a/libs/as-sdk-integration-tests/assembly/index.ts +++ b/libs/as-sdk-integration-tests/assembly/index.ts @@ -1,4 +1,5 @@ import { httpFetch, Process } from '../../as-sdk/assembly/index'; +import { testTallyVmReveals, testTallyVmRevealsFiltered } from './tally'; import { testTallyVmHttp, testTallyVmMode } from './vm-tests'; const args = Process.args().at(1); @@ -11,6 +12,10 @@ if (args === 'testHttpRejection') { testTallyVmMode(); } else if (args === 'testTallyVmHttp') { testTallyVmHttp(); +} else if (args === 'testTallyVmReveals') { + testTallyVmReveals(); +} else if (args === 'testTallyVmRevealsFiltered') { + testTallyVmRevealsFiltered(); } export function testHttpRejection(): void { diff --git a/libs/as-sdk-integration-tests/assembly/tally.ts b/libs/as-sdk-integration-tests/assembly/tally.ts new file mode 100644 index 0000000..f7c7806 --- /dev/null +++ b/libs/as-sdk-integration-tests/assembly/tally.ts @@ -0,0 +1,14 @@ +import { JSON } from "json-as"; +import { Tally, Process } from "../../as-sdk/assembly"; + +export function testTallyVmReveals(): void { + const reveals = Tally.getReveals(); + + Process.exit_with_message(0, JSON.stringify(reveals)); +} + +export function testTallyVmRevealsFiltered(): void { + const reveals = Tally.getConsensusReveals(); + + Process.exit_with_message(0, JSON.stringify(reveals)); +} diff --git a/libs/as-sdk-integration-tests/assembly/vm-tests.ts b/libs/as-sdk-integration-tests/assembly/vm-tests.ts index 80fdd56..c275f15 100644 --- a/libs/as-sdk-integration-tests/assembly/vm-tests.ts +++ b/libs/as-sdk-integration-tests/assembly/vm-tests.ts @@ -1,28 +1,25 @@ -import { Process, httpFetch } from '../../as-sdk/assembly/index'; +import { Process, httpFetch } from "../../as-sdk/assembly/index"; export function testTallyVmMode(): void { - const envs = Process.envs(); - const vmMode = envs.get('VM_MODE'); - - if (vmMode === 'tally') { - Process.exit_with_message(0, 'tally'); - } else { - Process.exit_with_message(1, 'dr'); + if (Process.isTallyVmMode()) { + Process.exit_with_message(0, "tally"); + } else if (Process.isDrVmMode()) { + Process.exit_with_message(1, "dr"); } + + throw new Error(`Unknown VM mode: ${Process.getVmMode()}`); } export function testTallyVmHttp(): void { - const response = httpFetch('https://swapi.dev/api/planets/1/'); + const response = httpFetch("https://swapi.dev/api/planets/1/"); const fulfilled = response.fulfilled; const rejected = response.rejected; if (fulfilled !== null) { - Process.exit_with_message(1, 'this should not be allowed in tally mode'); + Process.exit_with_message(1, "this should not be allowed in tally mode"); } if (rejected !== null) { Process.exit_with_result(0, rejected.bytes); } } - - diff --git a/libs/as-sdk-integration-tests/src/tallyvm.test.ts b/libs/as-sdk-integration-tests/src/tallyvm.test.ts index 019d4a6..6561372 100644 --- a/libs/as-sdk-integration-tests/src/tallyvm.test.ts +++ b/libs/as-sdk-integration-tests/src/tallyvm.test.ts @@ -1,8 +1,22 @@ -import { TallyVmAdapter, callVm } from '../../../dist/libs/vm'; +import { TallyVmAdapter, callVm } from '../../../dist/libs/vm/src/index'; import { readFile } from 'node:fs/promises'; describe('TallyVm', () => { - it('should run in tally vm mode', async () => { + it('should run in dr vm mode by default', async () => { + const wasmBinary = await readFile( + 'dist/libs/as-sdk-integration-tests/debug.wasm' + ); + const result = await callVm({ + args: ['testTallyVmMode'], + envs: {}, + binary: new Uint8Array(wasmBinary), + }); + + expect(result.resultAsString).toEqual('dr'); + expect(result.exitCode).toBe(1); + }); + + it('should run in tally vm mode with the adapter', async () => { const wasmBinary = await readFile( 'dist/libs/as-sdk-integration-tests/debug.wasm' ); @@ -37,4 +51,131 @@ describe('TallyVm', () => { expect(result.resultAsString).toEqual('http_fetch is not allowed in tally'); expect(result.exitCode).toBe(0); }); + + it('should fail when the reveals and consensus arrays are not the same length', async () => { + const wasmBinary = await readFile( + 'dist/libs/as-sdk-integration-tests/debug.wasm' + ); + + const reveals = [ + { + salt: [5, 0, 1, 2, 3], + exit_code: 1, + gas_used: '200000', + reveal: [1, 2, 3, 3, 3, 3, 3], + }, + { + salt: [5, 0, 1, 2, 3], + exit_code: 1, + gas_used: '1336', + reveal: [1, 2, 3, 3, 3, 3, 3], + }, + ]; + const consensus = [0]; + + const result = await callVm( + { + args: [ + 'testTallyVmReveals', + JSON.stringify(reveals), + JSON.stringify(consensus), + ], + envs: {}, + binary: new Uint8Array(wasmBinary), + }, + undefined, + new TallyVmAdapter() + ); + + expect(result.exitCode).toBe(255); + expect(result.stderr.length).toBeGreaterThan(1); + }); + + it('should be able to parse the reveals', async () => { + const wasmBinary = await readFile( + 'dist/libs/as-sdk-integration-tests/debug.wasm' + ); + + const reveals = [ + { + salt: [0, 1, 2, 3], + exit_code: 0, + gas_used: '200000', + reveal: [0, 2, 3, 3, 3, 3, 3], + }, + { + salt: [4, 1, 2, 3], + exit_code: 1, + gas_used: '1336', + reveal: [9, 2, 3, 3, 3, 3, 3], + }, + ]; + const consensus = [0, 0]; + + const result = await callVm( + { + args: [ + 'testTallyVmReveals', + JSON.stringify(reveals), + JSON.stringify(consensus), + ], + envs: {}, + binary: new Uint8Array(wasmBinary), + }, + undefined, + new TallyVmAdapter() + ); + + expect(result.exitCode).toBe(0); + expect(result.resultAsString).toBe( + '[{"salt":[0,1,2,3],"exit_code":0,"gas_used":"200000","reveal":[0,2,3,3,3,3,3],"inConsensus":0},{"salt":[4,1,2,3],"exit_code":1,"gas_used":"1336","reveal":[9,2,3,3,3,3,3],"inConsensus":0}]' + ); + }); + + it('should provide a convenience method to filter out outliers.', async () => { + const wasmBinary = await readFile( + 'dist/libs/as-sdk-integration-tests/debug.wasm' + ); + + const reveals = [ + { + salt: [3], + exit_code: 1, + gas_used: '200000', + reveal: [1], + }, + { + salt: [2], + exit_code: 0, + gas_used: '1336', + reveal: [2], + }, + { + salt: [1], + exit_code: 0, + gas_used: '1346', + reveal: [3], + }, + ]; + const consensus = [1, 0, 0]; + + const result = await callVm( + { + args: [ + 'testTallyVmRevealsFiltered', + JSON.stringify(reveals), + JSON.stringify(consensus), + ], + envs: {}, + binary: new Uint8Array(wasmBinary), + }, + undefined, + new TallyVmAdapter() + ); + + expect(result.exitCode).toBe(0); + expect(result.resultAsString).toBe( + '[{"salt":[2],"exit_code":0,"gas_used":"1336","reveal":[2],"inConsensus":0},{"salt":[1],"exit_code":0,"gas_used":"1346","reveal":[3],"inConsensus":0}]' + ); + }); });