Fibonacci

Description

The Fibonacci sequence is a set of integers (the Fibonacci numbers) that starts with a zero, followed by a one, then by another one, and then by a series of steadily increasing numbers. The sequence follows the rule that each number is equal to the sum of the preceding two numbers.

The Fibonacci sequence begins with the following 14 integers:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 ...

Each number, starting with the third, adheres to the prescribed formula. For example, the seventh number, 8, is preceded by 3 and 5, which add up to 8.

The sequence can theoretically continue to infinity, using the same formula for each new number. Some resources show the Fibonacci sequence starting with a one instead of a zero, but this is fairly uncommon.

Using Truebit to Execute Fibonacci

We use Truebit to execute the Fibonacci task to get a verified result.

The code

You can find the Fibonacci example within the folder truebit-nextgen-examples/function-tasks/<language>/fibonacci.

πŸ“ src └─ πŸ“„ main.rs πŸ“„ Cargo.toml πŸ“„ Cargo.lock

The target folder and the Cargo.lock file are going to be generated after running the build command within the truebit-cli

Source

main.rs

fn fibonacci(n: u32) -> u64 {
    if n == 0 {
        return 0;
    } else if n == 1 {
        return 1;
    }

    let mut a = 0;
    let mut b = 1;
    let mut c = 0;

    for _i in 2..=n {
        c = a + b;
        a = b;
        b = c;
    }

    return c;
}

fn run_task(input: String) -> String {
    fibonacci(input.parse().unwrap()).to_string()
}

fn main() {
    let input: String =
        std::fs::read_to_string("input.txt").expect("Could not read from input.txt");
    let output: String = 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 fibonacci 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 fibonacci 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

This in an example of how to build a JS task.

truebit build truebit-nextgen-examples/function-tasks/js/fibonacci/dist -l js

Output

Building the function task
Your function task is ready.
Use the following TaskId for execution or deployment:
js_391426a3fca881a6b3a05c49e33113e0d1829c3cca46f64b51f2d336c71cd184

The taskId will always starts with the language prefix + "_" + cID

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_391426a3fca881a6b3a05c49e33113e0d1829c3cca46f64b51f2d336c71cd184 10

Output

Executing the function Task
Execution Id: e406e754-b90b-4c62-9394-2cf0bb2c5647
Task executed with status: succeed
Task executed with result: 55
Task resource usage:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index)       β”‚ Unit          β”‚ Limits        β”‚ Peak  β”‚ Last status β”‚ Capacity β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Gas           β”‚ 'Steps'       β”‚ 1099511627776 β”‚ 'N/A' β”‚ 1907033199  β”‚ 577      β”‚
β”‚ Nested calls  β”‚ 'Calls'       β”‚ 65536         β”‚ 6387  β”‚ 6352        β”‚ 10       β”‚
β”‚ Frame memory  β”‚ 'Bytes'       β”‚ 524288        β”‚ 20115 β”‚ 19073       β”‚ 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_391426a3fca881a6b3a05c49e33113e0d1829c3cca46f64b51f2d336c71cd184

Output

The function task has been deployed successfully.
The Task <taskName> version <version> was registered successfully in namespace <namespace>

Last updated

Was this helpful?