How to Create Function Tasks

Function Tasks verify and provide a transcript for the execution of serverless function code.

circle-info

Check the Supported Languages section to see the specific task preparation steps required for your development language.

Prerequisites

There are several important considerations when writing Function Task code:

  • Execution environment: The task must run the same on any computer architecture and operating system. Therefore, Function Tasks execute in an isolated WebAssembly sandbox using our deterministic WASI implementation. Please, check the WebAssembly section for more information.

  • No network calls: To provide maximum security, the execution environment does not provide network access. If you need to access data from an external source, consider using an API Task.

  • Determinism: The execution environment guarantees determinism - All Function Tasks will produce the same outputs for given inputs when executed by any Truebit Verify node.

Building A Function Task

1. Write Your Function Task Code

The task developer will create the code, following the best practices described below:

Replace network operation with filesystem reads and writes

If your program would normally read and write to network endpoints, you'll need to replace this with filesystem operations.

For example, instead of making an HTTP request to example.com, read from the input.txt file. Click here to see an example.

You can write results to the filesystem into an output file. Click here to see an example.

Random Numbers

If your program normally uses random numbers, in Truebit you will always get the same sequence. This is done to ensure a deterministic execution.

Dates and Times

If your program would normally get date-time values, in Truebit you will always get the same value, which is "1970.1.1 0:1:40". This is done to ensure a deterministic execution.

Output value

The task must return a value. If the result is empty, the task will be marked as failed.

2. Test Your Function Task

Once your code is ready for testing, the development workflow involves two primary steps using the Truebit CLI:

  • Build and Prepare: Execute the build command to generate a WebAssembly (WASM) file. Truebit utilizes WebAssembly to ensure cross-platform consistency across any hardware or operating system. During this stage, the file is automatically instrumented to guarantee deterministic execution.

  • Execute and Test: Run the start command to deploy and verify your compiled code within the Truebit environment.

circle-check

3. Deploy The Function Task

Once the task is ready for being used by others, the Task Developer will deploy it to make it available for being called by the Task Requester.

Authorize The Task execution

The Task Authorization process is an optional step. This step allows the Task Developer to define who has permission to execute the deployed task.

circle-info

Click Here to see more information about the Task Authorization process.

4. Execute The Function Task

The Task Requester executes the task using the Task Execution Endpoint from the Truebit API.

Last updated

Was this helpful?