First steps writing Holochain hApps with Rust

Willem Olding
Nov 19, 2018 · 7 min read

Now updated for Holochain v0.0.4-alpha!

If you have been following the development of Holochain you may be aware that a developer preview of the next iteration has been released.

Holochain hApps are made of compiled WebAssembly that encodes the rules of the hApp, the data it can store and how users will interact with it. This means that any language that can compile to WebAssembly can one day be used for Holochain.

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 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.

Requirements

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.

First steps

Let’s begin by generating an empty hApp skeleton by running:

hc init todo-list

This will generate the following directory structure:

todo-list/
├── app.json
├── test
│ └── …
└── zomes

Notice the `zomes` directory. All Holochain hApps are comprised of one or more zomes. They can be thought of as similar to modules in JavaScript, each one should provide some self-contained functionality. Every zome has its own build system so it is possible to combine zomes written in different languages to produce a single hApp.

We will create a single zome called `lists` that uses a Rust build system:

cd todo-list
hc generate zomes/lists rust

The project structure should now be as follows:

├── app.json
├── test
│ └── …
└── zomes
└── lists
├── code
│ ├── .hcbuild
│ ├── Cargo.toml
│ └── src
│ └── lib.rs
└── zome.json

Writing the lists zome

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

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:

#![feature(try_from)]
use std::convert::TryFrom;
#[macro_use]
extern crate hdk;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate holochain_core_types_derive;
use hdk::{
error::{ZomeApiResult, ZomeApiError},
holochain_core_types::{
hash::HashString,
error::HolochainError,
dna::entry_types::Sharing,
json::JsonString,
cas::content::Address,
entry::{AppEntryValue, Entry},
}
};

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(())`.

Take note also of the `links` field. 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.

Adding Functions

For each of these functions we must define a handler, which is a Rust function that will be executed when the conductor calls the function. (For more on conductors, read Nico’s recent post.) It is best practice for functions to always return a ZomeApiResult<T>, where T is the type the function should return if it runs without error. This is an extension of the Rust Result type and allows zome functions to abort early on errors using the ? operator. 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, *traits*. Traits allow functions to be grouped to control access and in the future will allow hApps to connect to other hApps that implement a particular trait. At this time the only trait we need to consider is the hc_public trait. This is a special named trait that exposes all of the contained functions to the outside world.

The function field of our zome definition should be updated to:

and there we have it! If you are coding along the full lib.rs should now look like this:

The zome we created should now build if we run:

hc package

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`.

Writing tests

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 conductor 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 conductor. 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.

Conclusion

The complete working version of this project is available on github. This builds under the 0.0.4-alpha release but as the API and HDK are changing it will likely fail under newer releases.

Holochain

Holochain enables a distributed web with user autonomy…

Holochain

Holochain enables a distributed web with user autonomy built directly into its architecture and protocols. Data is about remembering our lived and shared experiences. Distributing the storage and processing of that data can change how we coordinate and interact. www.holochain.org

Willem Olding

Written by

Holochain

Holochain enables a distributed web with user autonomy built directly into its architecture and protocols. Data is about remembering our lived and shared experiences. Distributing the storage and processing of that data can change how we coordinate and interact. www.holochain.org

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store