Skip to content

Commit

Permalink
chore(as-sdk-tests): add tests for the tally convenience methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomasvdam committed Jun 18, 2024
1 parent 54a89d2 commit 3e0c023
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 14 deletions.
5 changes: 5 additions & 0 deletions libs/as-sdk-integration-tests/assembly/index.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions libs/as-sdk-integration-tests/assembly/tally.ts
Original file line number Diff line number Diff line change
@@ -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));
}
21 changes: 9 additions & 12 deletions libs/as-sdk-integration-tests/assembly/vm-tests.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}


145 changes: 143 additions & 2 deletions libs/as-sdk-integration-tests/src/tallyvm.test.ts
Original file line number Diff line number Diff line change
@@ -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'
);
Expand Down Expand Up @@ -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}]'
);
});
});

0 comments on commit 3e0c023

Please sign in to comment.