Add Your Own Builtins
Some service functionalities have ubiquitous demand making them suitable candidates to be directly deployed to a peer node. The Aqua distributed hash table (DHT) is an example of builtin service. The remainder of this tutorial guides you through the steps necessary to create and deploy a Builtin service.
In order to have a service available out-of-the-box with the necessary startup and scheduling scripts, we can take advantage of the Fluence deployer feature for Node native services. This feature handles the complete deployment process including
    module uploads,
    service deployment and
    script initialization and scheduling
Note that the deployment process is a fully automated workflow requiring you to merely submit your service assets, i.e., Wasm modules and configuration scripts, in the appropriate format as a PR to the Fluence repository.
At this point you should have a solid grasp of creating service modules and their associated configuration files.
Our first step is fork the Fluence repo by clicking on the Fork button, upper right of the repo webpage, and follow the instructions to create a local copy. In your local repo copy, checkout a new branch with a new, unique branch name:
1
cd fluence
2
git checkout -b MyBranchName
Copied!
In our new branch, we create a directory with the service name in the deploy/builtin directory:
1
cd deploy/builtins
2
mkdir my-new-super-service
3
cd my-new-super-service
Copied!
Replace my-new-super-service with your service name.
Now we can build and populate the required directory structure with your service assets. You should put your service files in the corresponding my-new-super-service directory.

Requirements

In order to deploy a builtin service, we need
    the Wasm file for each module required for the service
    the blueprint file for the service
    the optional start and scheduling scripts

Blueprint

Blueprints capture the service name and dependencies:
1
// example_blueprint.json
2
{
3
"name": "my-new-super-service",
4
"dependencies": [
5
"name:my_module_1",
6
"name:my_module_2",
7
"hash:Hash(my_module_3.wasm)"
8
]
9
}
Copied!
Where
    name specifies the service's name and
    dependencies list the names of the Wasm modules or the Blake3 hash of the Wasm module
In the above example, my_module_i refers to ith module created when you compiled your service code
The easiest way to get the Blake3 hash of our Wasm modules is to install the b3sum utility:
1
cargo install b3sum
2
b3sum my_module_3.wasm
Copied!
If you decide to use the hash approach, please use the hash for the config files names as well (see below).

Start Script

Start scripts, which are optional, execute once after service deployment or node restarts and are submitted as AIR files and may be accompanied by a json file containing the necessary parameters.
1
;; on_start.air
2
(seq
3
(call relay ("some_service_alias" "some_func1") [variable1] result)
4
(call relay ("some_service_alias" "some_func2") [variable2 result])
5
)
Copied!
and the associated data file:
1
// on_start.json data for on_start.air
2
{
3
"variable1" : "some_string",
4
"variable2" : 5,
5
}
Copied!

Scheduling Script

Scheduling scripts allow us to decouple service execution from the client and instead can rely on a cron-like scheduler running on a node to trigger our service(s).

Directory Structure

Now that we got our requirements covered, we can populate the directory structure we started to lay out at the beginning of this section. As mentioned above, service deployment as a builtin is an automated workflow one our PR is accepted. Hence, it is imperative to adhere to the directory structure below:
1
-- builtins
2
-- {service_alias}
3
-- scheduled
4
-- {script_name}_{interval_in_seconds}.air [optional]
5
-- blueprint.json
6
-- on_start.air [optional]
7
-- on_start.json [optional]
8
-- {module1_name}.wasm
9
-- {module1_name}_config.json
10
-- Hash(module2_name.wasm).wasm
11
-- Hash(module2_name.wasm)_config.json
12
...
Copied!
For a complete example, please see the aqua-dht builtin:
1
fluence
2
--deploy
3
--builtins
4
--aqua-dht
5
-aqua-dht.wasm
6
-aqua-dht_config.json
7
-blueprint.json
8
-scheduled
9
-sqlite3.wasm # or 558a483b1c141b66765947cf6a674abe5af2bb5b86244dfca41e5f5eb2a86e9e.wasm
10
-sqlite3_config.json # or 558a483b1c141b66765947cf6a674abe5af2bb5b86244dfca41e5f5eb2a86e9e_config.json
Copied!
which is based on the eponymous service project.
Last modified 1mo ago