Reverse Alphabet
Description
Reverse Alphabet is a simple yet powerful tool that takes any individual word as input and promptly returns its reverse.
Using Truebit to Execute Reverse Alphabet
We use Truebit to execute the Reserver alphabet task to get a verified result.
The code
You can find the Reverse Alphabet example within the folder truebit-nextgen-examples/function-tasks/<language>/reverse-alphabet
π src ββ π main.rs π Cargo.toml π Cargo.lock
Source
main.rs
fn run_task(input: String) -> String {
input.chars().rev().collect()
}
fn main() {
let input: String =
std::fs::read_to_string("input.txt").expect("Could not read from input.txt");
let output = run_task(input);
std::fs::write("output.txt", output).expect("Could not write to output.txt");
}
In/Out parameters
In order to send parameters to the Truebit task, you need to open the "input.txt" file and get the value from there. For now, only one input parameter is allowed.
let input: String = std::fs::read_to_string("input.txt").expect("Could not read from input.txt");
In order to retrieve the output value from the Truebit task, you need save it in the "output.txt" file.
std::fs::write("output.txt", output).expect("Could not write to output.txt");
Try out
We will use the Truebit CLI to compile and try our source code. Once the code is finished, we will deploy it to the coordination hub so that everyone who knows the namespace
and taskname
.
Step 1: Create the Reverse Alphabet source code
Within the src folder you will find a file called main in the case of Rust, C, C++, JS, and task.py in the case of Python. This is the Reverse Alphabet implementation that will be used for testing purpose.
Step 2: Build the source code
Execute the build command against Truebit node to get an instrumented Truebit task
truebit build truebit-nextgen-examples/function-tasks/js/reverse-alphabet/dist -l js
Output
Building the function task
Your function task is ready.
Use the following TaskId for execution or deployment:
js_d9254bbb26c626f7b1c6d20550aed4df34cbd6a9bf7558ae42396e6a4676d5a9
Step 3: Try the code
Execute the start command against the Truebit node to test our Algorithm. You will need to submit the instrumented task id + the input parameter.
truebit start js_d9254bbb26c626f7b1c6d20550aed4df34cbd6a9bf7558ae42396e6a4676d5a9 Hello_World
Output
Executing the function Task
Execution Id: 50e9408a-2a04-469d-b493-d9b27a12c771
Task executed with status: succeed
Task executed with result: dlroW_olleH
Task resource usage:
βββββββββββββββββ¬ββββββββββββββββ¬ββββββββββββββββ¬ββββββββ¬ββββββββββββββ¬βββββββββββ
β (index) β Unit β Limits β Peak β Last status β Capacity β
βββββββββββββββββΌββββββββββββββββΌββββββββββββββββΌββββββββΌββββββββββββββΌβββββββββββ€
β Gas β 'Steps' β 1099511627776 β 'N/A' β 1906503210 β 577 β
β Nested calls β 'Calls' β 65536 β 6372 β 6339 β 10 β
β Frame memory β 'Bytes' β 524288 β 20023 β 19034 β 26 β
β System memory β '64 Kb pages' β 2048 β 87 β 'π« 1' β 24 β
βββββββββββββββββ΄ββββββββββββββββ΄ββββββββββββββββ΄ββββββββ΄ββββββββββββββ΄βββββββββββ
Step 4: Deploy the task
Last, but not least, Execute the deploy command to deploy our task to the coordination hub, so that anyone with the namespace, taskname and the API key can execute it.
truebit deploy <namespace> <taskname> --taskId js_d9254bbb26c626f7b1c6d20550aed4df34cbd6a9bf7558ae42396e6a4676d5a9
Output
The function task has been deployed successfully.
The Task <taskName> version <version> was registered successfully in namespace <namespace>
Last updated
Was this helpful?