-
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
1 parent
4e8b4f0
commit 3b40b20
Showing
10 changed files
with
184 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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'); | ||
} | ||
} | ||
|
||
export function testTallyVmHttp(): void { | ||
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'); | ||
} | ||
|
||
if (rejected !== null) { | ||
Process.exit_with_result(0, rejected.bytes); | ||
} | ||
} | ||
|
||
|
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,40 @@ | ||
import { TallyVmAdapter, callVm } from '../../../dist/libs/vm'; | ||
import { readFile } from 'node:fs/promises'; | ||
|
||
describe('TallyVm', () => { | ||
it('should run in tally vm mode', async () => { | ||
const wasmBinary = await readFile( | ||
'dist/libs/as-sdk-integration-tests/debug.wasm' | ||
); | ||
const result = await callVm( | ||
{ | ||
args: ['testTallyVmMode'], | ||
envs: {}, | ||
binary: new Uint8Array(wasmBinary), | ||
}, | ||
undefined, | ||
new TallyVmAdapter() | ||
); | ||
|
||
expect(result.resultAsString).toEqual('tally'); | ||
expect(result.exitCode).toBe(0); | ||
}); | ||
|
||
it('should fail to make an http call', async () => { | ||
const wasmBinary = await readFile( | ||
'dist/libs/as-sdk-integration-tests/debug.wasm' | ||
); | ||
const result = await callVm( | ||
{ | ||
args: ['testTallyVmHttp'], | ||
envs: {}, | ||
binary: new Uint8Array(wasmBinary), | ||
}, | ||
undefined, | ||
new TallyVmAdapter() | ||
); | ||
|
||
expect(result.resultAsString).toEqual('http_fetch is not allowed in tally'); | ||
expect(result.exitCode).toBe(0); | ||
}); | ||
}); |
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
17 changes: 14 additions & 3 deletions
17
libs/vm/src/default-vm-adapter.ts → libs/vm/src/data-request-vm-adapter.ts
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,40 @@ | ||
import type { HttpFetchAction } from './types/vm-actions'; | ||
import { HttpFetchResponse } from './types/vm-actions.js'; | ||
import type { VmAdapter } from './types/vm-adapter'; | ||
import { PromiseStatus } from './types/vm-promise.js'; | ||
import type { VmCallData } from './vm'; | ||
|
||
export default class TallyVmAdapter implements VmAdapter { | ||
private processId?: string; | ||
|
||
modifyVmCallData(input: VmCallData): VmCallData { | ||
return { | ||
...input, | ||
envs: { | ||
...input.envs, | ||
VM_MODE: 'tally' | ||
} | ||
}; | ||
} | ||
|
||
setProcessId(processId: string) { | ||
this.processId = processId; | ||
} | ||
|
||
async httpFetch( | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
_action: HttpFetchAction | ||
): Promise<PromiseStatus<HttpFetchResponse>> { | ||
const error = new TextEncoder().encode('http_fetch is not allowed in tally'); | ||
|
||
return PromiseStatus.rejected( | ||
new HttpFetchResponse({ | ||
bytes: Array.from(error), | ||
content_length: error.length, | ||
headers: {}, | ||
status: 0, | ||
url: '', | ||
}) | ||
); | ||
} | ||
} |
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,8 +1,28 @@ | ||
import type { VmCallData } from "../vm"; | ||
import type { HttpFetchAction, HttpFetchResponse } from "./vm-actions"; | ||
import { PromiseStatus } from "./vm-promise.js"; | ||
|
||
|
||
export interface VmAdapter { | ||
/** | ||
* Allows the adapter to modify the call data before executing | ||
* this can be used to inject arguments, environment variables, etc. | ||
* | ||
* @param input | ||
*/ | ||
modifyVmCallData(input: VmCallData): VmCallData; | ||
|
||
/** | ||
* Sets the process id in order to identify a vm call in the logs | ||
* | ||
* @param processId | ||
*/ | ||
setProcessId(processId: string): void, | ||
|
||
/** | ||
* Method to do a remote http fetch call | ||
* | ||
* @param action | ||
*/ | ||
httpFetch(action: HttpFetchAction): Promise<PromiseStatus<HttpFetchResponse>>; | ||
} |
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