A gym tool Holochain Gym Concepts Developers What's next Glossary of Terms Toggle darkmode Give us Feedback!

Intermediate: Anchors

What is an anchor?

You can think of anchors as an entry that every user knows the hash of. As the name implies, it acts as an 'anchor' with which we can link other entries to in order to create queries for things every user will need to access, for example, every user or post in the system.

Try it!

Here you can try out a system where every post that is created gets linked to an anchor for posts.

Keep in mind that anchors are already incorporated in the core hdk, so you don't need to import them from an external library. Although it is necessary to define them as an entry definition in your zome like this:
entry_defs![
    Anchor::entry_def(),
    ...
];

Exercise

For this exercise we will be implementing the above example.

  1. create_post: creates a post and attaches it to an anchor for posts.
  • Define an anchor with anchor type = ALL_POSTS and text = ALL_POSTS. This will already internally create the anchor for you.
  • Create the post entry, and calculate its hash.
  • Create a link from the anchor entry to the post entry.
  1. get_all_posts queries the links of the posts anchor and returns all the posts.
  • Calculate the anchor entry hash.
  • Get the links from the anchor entry to all the posts.
  • For every target in these links, do a get to retrieve the posts.
  1. Check if you are still inside the nix-shell. Your terminal should look similar to this [nix-shell:~/path-to-workspace/developer-exercises/path-to-exercise]$
  2. Implement create_post, get_all_posts.
  3. Compile and test your code: cd tests && npm test.
  4. Don't stop until the tests run green.