First steps writing Holochain hApps with Rust
Now updated for Holochain v0.0.2!
If you have been following the development of Holochain you may be aware that a developer preview of the next iteration has been released.
Writing WebAssembly that complies with the Holochain runtime can be tricky. To make development as streamlined as possible the core team has been developing a Holochain-dev-kit (HDK) for the first supported language, Rust! In the near future there are also plans to release an HDK for AssemblyScript, a strictly-typed subset of TypeScript, and the community is encouraged to develop an HDK for their language of choice.
In this article we will walk through the steps of creating a simple hApp using Rust and highlight some of the differences from the previous version.
First step is to download the appropriate dev preview release for your OS. If you decide to build the latest version from source, be warned that the API is undergoing rapid change, so some of the steps in this article may not work. The release contains the binary for the holochain developer command line tool, hc, which is used to generate a skeleton app, run tests and build the app package.
Ensure that hc is available on your path. If you instead decide to build from source cargo will ensure the binaries are on your path automatically.
If you want to jump ahead to see what the completed project will look like, the full source code is available on GitHub.
We will be making a classic to-do list hApp. A user can create new lists and add items to a list. They should also be able to retrieve a list by its address and all of the items on each list.
Let’s begin by generating an empty hApp skeleton by running:
hc init todo-list
This will generate the following directory structure:
│ └── …
We will create a single zome called `lists` that uses a Rust build system:
hc generate zomes/lists rust
The project structure should now be as follows:
│ └── …
│ ├── .build
│ ├── Cargo.toml
│ └── src
│ └── lib.rs
Writing the lists zome
The Rust HDK makes use of Rust macros to reduce the need for boilerplate code. The most important of which is the `define_zome!` macro. Every zome must use this to define the structure of the zome, what entries it contains, which functions it exposes and what to do on first start-up (genesis).
Open up `lib.rs` and replace its contents with the following:
This is the simplest possible zome with no entries and no exposed functions.
Adding some Entries
Unlike in holochain-proto, where you needed to define a JSON schema to validate entries, holochain entries in Rust map to a native struct type. We can define our list and listItem structs as follows:
You might notice that the `List` struct does not contain a field that holds a collection of `ListItem`s. This will be implemented using links, which we will discuss later.
Also be sure to add the following to the list of imports:
extern crate hdk;
extern crate serde_derive;
extern crate holochain_core_types_derive;
extern crate serde_json;
The `Serialize` and `Deserialize` derived traits allow the structs to be converted to and from JSON, which is how entries are managed internally in Holochain. The DefaultJson derived trait comes from the holochain hDK itself and allows for seamless converting between data stored in the DHT and rust structs.
These structs on their own are not yet valid Holochain entries. To create these we must include them in the `define_zome!` macro by using the `entry!` macro:
Take note of the `native_type` field of the macro which gives which Rust struct represents the entry type. The `validation_package` field is a function that defines what data should be passed to the validation function through the `ctx` argument. In this case we use a predefined function to only include the entry itself, but it is also possible to pass chain headers, chain entries or the full local chain. The validation field is a function that performs custom validation for the entry. In both our cases we are just returning `Ok(())`.
New in Holochain v0.0.2 is validation for link entries. As we will see later links are the main way to encode relational data in holochain. The `links` section of the entry macro defines what other types of entries are allowed to link to and from this type. This also includes a validation function for fine grain control over linking. See the entry macro documentation for more info.
Finally we need a way to interact with the hApp. We will define the following functions: `create_list`, `add_item` and `get_list`. `get_list` will retrieve a list and all the items linked to each list.
For each of these functions we must define a handler, which is a Rust function that will be executed when the container calls the function. (For more on containers, read Nico’s recent post.) It is best practice for functions to always return a JSON string (`serde_json::Value`). At the moment the handler function names cannot be the same as the function itself so we will prefix them with `handle_`. This will be fixed in an upcoming release. The handler for `create_list` could be written as:
The `hdk::commit_entry` function is how a zome can interact with holochain core to add entries to the DHT or local chain. This will trigger the validation function for the entry and if successful will store the entry and return its hash/address.
The `add_item` function requires the use of holochain links to associate two entries. In holochain-proto this required the use of a commit with a special Links entry but it can now be done using the HDK function `link_entries(address1, address2, tag)`. The add item handler accepts a `ListItem` and an address of a list, commits the `ListItem`, then links it to the list address:
At the moment there is no validation done on the link entries. This will be added soon with an additional validation callback.
Finally, `get_list` requires us to use the HDK function `get_links(base_address, tag)`. As you may have guessed, this will return the addresses of all the entries that are linked to the `base_address` with a given tag. As this only returns the addresses, we must then map over each of then and load the required entry.
Phew! That is all the handlers set up. Finally the function definitions must be added to the `define_zome!` macro. Before doing that, it is worth briefly discussing a new concept in Holochain, *function groups*. Zome function groups have a name and access setting which must be one of `Public`, `Agent`, `ApiKey` or `Zome`. Using this it is possible to limit access to certain functions. This feature is currently undergoing some changes and will likely be renamed in future versions.
The function field of our zome definition should be updated to:
and there we have it! The zome we created should now build if we run:
from the root directory. This will compile the Rust to WebAssembly and produce a `bundle.json` file which contains the compiled WASM code and the required metadata. This is the file that we can load and run using `hc`.
Opening up the `test/index.js` file you will see a skeleton test file already created:
This illustrates the `app.call` function that is exposed by the container for each app and that can be used to call our functions. Take note that the input-data should be a JSON object that matches the function signature. `call` will also return a JSON object.
Lets add some tests for our todo list:
Running `hc test` will build the test file and run it using `node` which is able to load and execute holochain hApps via the holochain node container. If everything has worked correctly you should see some test output with everything passing.
Pro tip: Pipe the output to tap-spec (which must be installed via npm first) to get beautifully formatted test output.
And there we have it! A simple zome created in holochain using the Rust HDK. Those with previous experience developing for holochain-proto will appreciate the reduced boilerplate, strong typing and vastly improved testing framework.
The complete working version of this project is available on github. This builds under the 0.021 release but as the API and HDK are changing it will likely fail under newer releases.