-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
268 lines (224 loc) · 10 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, viewport-fit=cover"
/>
<meta name="Description" content="Put your description here." />
<base href="/" />
<script type="module">
import "reveal.js/dist/reveal.css";
import "reveal.js/dist/theme/black.css";
import "reveal.js/plugin/highlight/monokai.css";
import "@mythosthesia/reveal-course-preset/styles.css";
</script>
<style>
.row {
display: flex;
flex-direction: row;
}
</style>
<title>Holochain Lesson 4</title>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Anchors & Paths</h1>
</section>
<!-- prettier-ignore -->
<section language="markdown" animate="by-line with-ancestry">
### Anchors (I)
- Data in Holochain is best structured as a directed acyclic graph of linked actions, entries and public keys
- From a starting point ID (action, entry or agent), we typically want to retrieve certain data related to it
- For example, if our application will need to "get_comments_by_author(author: AgentPubKey)", then:
- When we create the comment,
- we also create a link
- from the public key of the author
- to the create action hash
- Inside "get_coments_by_author(author: AgentPubKey)"
- we get links from that agent public key
- to retrieve all the comments by that author
</section>
<!-- prettier-ignore -->
<section language="markdown" animate="by-line with-ancestry">
### Anchors (II)
- Problem: how do we build queries that don't have starting point?
- For example: "get_all_comments()", which gets all comments that any agent has created
- We can't go to all the cells and query all the comments that each cell has created as it's intractably expensive when the network grows
- Solution: hard code a deterministic entry in the source code
- Whenever we create a comment, we also create a link from the hash of that deterministic entry
- Then we can implement "get_all_comments()" by doing a get links from that hash that everyone knows
- These deterministic entries are called "anchors"
- Demo: https://holochain-gym.github.io/developers/intermediate/anchors/#try-it
</section>
<section>
<h3>Anchors (III)</h3>
<div class="row" style="justify-content: center">
<!-- prettier-ignore -->
<pre
class="fragment fade-in" style="flex-basis: 800px;"
><code class="rust" data-noescape data-trim
animate="by-line balanced with-ancestry separate-comments trailing-comments-in-popover strike-on-error-in-comment"
>
// Integrity
use hdi::prelude::*;
#[hdk_entry_helper]
struct Comment {
comment: String
}
#[hdk_entry_helper]
// We only need a string
struct Anchor(String);
#[hdk_entry_defs]
#[unit_enum(UnitEntryTypes)]
enum EntryTypes {
Comment(Comment),
Anchor(Anchor),
}
#[hdk_link_types]
enum LinkTypes {
AnchorToComment,
}
</code></pre>
<!-- prettier-ignore -->
<pre
class="fragment fade-in"
style="margin-left: 16px"
><code class="rust" data-noescape data-trim
animate="by-line balanced with-ancestry separate-comments trailing-comments-in-popover strike-on-error-in-comment"
>
// Coordinator
use hdk::prelude::*;
#[hdk_extern]
fn create_comment(comment: Comment) -> ExternResult<ActionHash> {
let comment_action_hash = create_entry(EntryTypes::Comment(comment))?;
let anchor = Anchor(String::from("all_comments"));
let _action_hash = create_entry(EntryTypes::Anchor(anchor))?; // This hash is not
// deterministic: it will be
// different every time this
// function is executed
let anchor_entry_hash = hash_entry(anchor)?; // This hash is deterministic: it will be the
// same independently of which cells executes
// this function
create_link(anchor_entry_hash, comment_action_hash, LinkTypes::AnchorToComment, ())?;
Ok(comment_action_hash)
}
#[hdk_extern]
fn get_all_comments_hashes(_: ()) -> ExternResult<Vec<ActionHash>> {
let anchor = Anchor(String::from("all_comments")); // Must use the same string as
// the create function
let anchor_hash = hash_entry(anchor)?;
let links = get_links(anchor_hash, LinkTypes::AnchorToComment, None)?; // Get all the links
// created above
let action_hashes: Vec<ActionHash> = links.into_iter()
.map(|link| ActionHash::from(link.target))
.collect(); // Extract the action hash from the links
Ok(action_hashes)
}
</code></pre>
</div>
</section>
<!-- prettier-ignore -->
<section language="markdown" animate="by-line with-ancestry">
### Paths (theory)
- Anchors are great, but they can introduce problems
- If a large number of links are attached to the same base, they become a burden on the agents responsible for those links
- This is know as a "DHT Hotspot"
- It's impractice to sort or filter an oversized result set
- For example, a query of the form "get_comment_by_date(start_date: Timestamp, end_date: Timestamp)"
- Paths solve these issues
- Paths are built-in to the HDK
- Paths are anchor trees
- Top level anchor acts as the root node
- This root node links to its children, which are also anchors
- This tree can be dynamically constructed and can have arbitrary depth
- Paths are composed of a vector of components
- Each component is an arbitrary byte array
- Normally constructed from a string
- Demo: https://holochain-gym.github.io/developers/intermediate/paths/#try-it
</section>
<section>
<h3>Paths (code example)</h3>
<!-- prettier-ignore -->
<pre
class="fragment fade-in"
><code class="rust" data-noescape data-trim
animate="by-line balanced with-ancestry separate-comments trailing-comments-in-popover strike-on-error-in-comment"
>
use hdk::prelude::*;
use integrity_zome::*; // Import the types defined in our integrity zome
use chrono::*; // Import the chrono crate for time related utilites
#[hdk_extern]
fn create_comment(comment: Comment) -> ExternResult<ActionHash> {
let comment_action_hash = create_entry(EntryTypes::Comment(comment))?;
let (secs, nsecs) = sys_time()?.as_seconds_and_nanos(); // Get the current timestamp
// in microseconds
let dt: DateTime<Utc> = DateTime::from_utc(NaiveDateTime::from_timestamp(secs, nsecs), Utc);
let path = Path::from(
format!("all_comments.{}.{}", dt.year(), dt.month()) // The '.' is the separator
// between components
); // Builds the path "all_comments.2022.7"
let typed_path = path.typed(LinkTypes::PathTimeIndex)?; // "TypedPath" is a path with all the
// links of a certain †ype
typed_path.ensure()?; // Creates the path tree structure, and all the necessary links
create_link(
typed_path.path_entry_hash()?, // Entry hash of the path
comment_action_hash,
LinkTypes::PathToComment,
()
)?;
Ok(comment_action_hash)
}
</code></pre>
</section>
<section>
<h3>Paths (roll-ups)</h3>
<!-- prettier-ignore -->
<pre
class="fragment fade-in"
><code class="rust" data-noescape data-trim
animate="by-line balanced with-ancestry separate-comments trailing-comments-in-popover strike-on-error-in-comment"
>
#[hdk_extern]
fn get_comments_by_year(year: u32) -> ExternResult<Vec<ActionHash>> {
let year_path = Path::from(
format!("all_comments.{}", year) // Build the anchor in the intermediate node
).typed(LinkTypes::PathTimeIndex);
let month_paths: Vec<Path> = year_path.children_paths()?; // Retrieves all the children
// node month paths
let mut all_links: Vec<Link> = vec![];
for path in month_paths {
let last_component: Component = path.leaf() // Returns an "Option<Component>" with
// the last component of this path
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from("The path is empty"))))?.clone();
let month = String::try_from(last_component) // Converts the component's byte array
// to a string
.map_err(|err| wasm_error!(err))?;
let mut links = get_links(
path.path_entry_hash()?, LinkTypes::PathToComment, None
)?; // Get all the links created above
all_links.append(&mut links); // Collect the links
}
let all_links: Vec<ActionHash> = links.into_iter()
.map(|link| ActionHash::from(link.target))
.collect(); // Extract the action hash from the links
Ok(action_hashes)
}
</code></pre>
</section>
<section>
<h1>That's it!</h1>
</section>
</div>
</div>
<script type="module">
import Reveal from "reveal.js";
import { plugins, config } from "@mythosthesia/reveal-course-preset";
let deck = new Reveal();
deck.initialize(config);
</script>
</body>
</html>