holochain 0.7.0-dev.21

Holochain, a framework for distributed applications
Documentation
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use crate::{
    conductor::{api::error::ConductorApiError, error::ConductorError, CellError},
    sweettest::*,
};
use holo_hash::ActionHash;
use holochain_conductor_api::CellInfo;
use holochain_types::prelude::*;
use holochain_wasm_test_utils::TestWasm;
use matches::matches;

#[tokio::test(flavor = "multi_thread")]
async fn create_clone_cell_without_modifiers_fails() {
    let conductor = SweetConductor::standard().await;
    let result = conductor
        .clone()
        .create_clone_cell(
            &"".into(),
            CreateCloneCellPayload {
                role_name: "".to_string(),
                modifiers: DnaModifiersOpt::none(),
                membrane_proof: None,
                name: None,
            },
        )
        .await;
    assert!(result.is_err());
}

#[tokio::test(flavor = "multi_thread")]
async fn create_clone_cell_with_wrong_app_or_role_name_fails() {
    let (dna, _, _) = SweetDnaFile::unique_from_test_wasms(vec![TestWasm::Create]).await;
    let role_name: RoleName = "dna_1".to_string();
    let mut conductor = SweetConductor::standard().await;
    let app = conductor
        .setup_app("app", [&(role_name.clone(), dna)])
        .await
        .unwrap();

    let result = conductor
        .clone()
        .create_clone_cell(
            &"wrong_app_id".into(),
            CreateCloneCellPayload {
                role_name: role_name.clone(),
                modifiers: DnaModifiersOpt::none().with_network_seed("seed".to_string()),
                membrane_proof: None,
                name: None,
            },
        )
        .await;
    assert!(result.is_err());

    let result = conductor
        .clone()
        .create_clone_cell(
            app.installed_app_id(),
            CreateCloneCellPayload {
                role_name: "wrong_role_name".to_string(),
                modifiers: DnaModifiersOpt::none().with_network_seed("seed".to_string()),
                membrane_proof: None,
                name: None,
            },
        )
        .await;
    assert!(result.is_err());
}

#[tokio::test(flavor = "multi_thread")]
async fn create_clone_cell_creates_callable_cell() {
    let (dna, _, _) = SweetDnaFile::unique_from_test_wasms(vec![TestWasm::Create]).await;
    let role_name: RoleName = "dna_1".to_string();
    let mut conductor = SweetConductor::standard().await;
    let app = conductor
        .setup_app("app", [&(role_name.clone(), dna.clone())])
        .await
        .unwrap();

    let clone_name = "test_name".to_string();
    let clone_cell = conductor
        .clone()
        .create_clone_cell(
            app.installed_app_id(),
            CreateCloneCellPayload {
                role_name: role_name.clone(),
                modifiers: DnaModifiersOpt::none().with_network_seed("seed".to_string()),
                membrane_proof: None,
                name: Some(clone_name.clone()),
            },
        )
        .await
        .unwrap();
    assert!(clone_cell.enabled);
    assert_eq!(clone_cell.name, clone_name);

    let zome = SweetZome::new(
        clone_cell.cell_id.clone(),
        TestWasm::Create.coordinator_zome_name(),
    );
    let zome_call_response: Result<ActionHash, _> = conductor
        .call_fallible(&zome, "call_create_entry", ())
        .await;
    assert!(zome_call_response.is_ok());
}

#[tokio::test(flavor = "multi_thread")]
async fn create_clone_cell_run_twice_returns_correct_clones() {
    let (dna, _, _) = SweetDnaFile::unique_from_test_wasms(vec![TestWasm::Create]).await;
    let role_name: RoleName = "dna_1".to_string();
    let mut conductor = SweetConductor::standard().await;
    let app = conductor
        .setup_app("app", [&(role_name.clone(), dna.clone())])
        .await
        .unwrap();

    let clone_cell_0 = conductor
        .clone()
        .create_clone_cell(
            app.installed_app_id(),
            CreateCloneCellPayload {
                role_name: role_name.clone(),
                modifiers: DnaModifiersOpt::none().with_network_seed("seed_1".to_string()),
                membrane_proof: None,
                name: None,
            },
        )
        .await
        .unwrap();
    assert_eq!(clone_cell_0.clone_id, CloneId::new(&role_name, 0)); // clone index starts at 0
    assert_eq!(clone_cell_0.original_dna_hash, dna.dna_hash().to_owned());

    let clone_cell_1 = conductor
        .clone()
        .create_clone_cell(
            app.installed_app_id(),
            CreateCloneCellPayload {
                role_name: role_name.clone(),
                modifiers: DnaModifiersOpt::none().with_network_seed("seed_2".to_string()),
                membrane_proof: None,
                name: None,
            },
        )
        .await
        .unwrap();
    assert_eq!(clone_cell_1.clone_id, CloneId::new(&role_name, 1));
    assert_eq!(clone_cell_1.original_dna_hash, dna.dna_hash().to_owned());
}

#[tokio::test(flavor = "multi_thread")]
async fn create_identical_clone_cell_twice_fails() {
    let (dna, _, _) = SweetDnaFile::unique_from_test_wasms(vec![TestWasm::Create]).await;
    let role_name: RoleName = "dna_1".to_string();
    let mut conductor = SweetConductor::standard().await;
    let apps = conductor
        .setup_apps("app", 2, [&(role_name.clone(), dna.clone())])
        .await
        .unwrap()
        .into_inner();
    let alice_app = &apps[0];
    let bob_app = &apps[1];
    let clone_cell_payload = CreateCloneCellPayload {
        role_name: role_name.clone(),
        modifiers: DnaModifiersOpt::none().with_network_seed("seed".to_string()),
        membrane_proof: None,
        name: None,
    };

    let alice_clone_cell = conductor
        .clone()
        .create_clone_cell(alice_app.installed_app_id(), clone_cell_payload.clone())
        .await
        .unwrap();

    let identical_clone_cell_err = conductor
        .clone()
        .create_clone_cell(alice_app.installed_app_id(), clone_cell_payload.clone())
        .await;
    matches!(
        identical_clone_cell_err,
        Err(ConductorError::AppError(AppError::DuplicateCellId(cell_id))) if cell_id == alice_clone_cell.cell_id
    );

    // disable clone cell and try again to create an identical clone
    conductor
        .clone()
        .disable_clone_cell(
            alice_app.installed_app_id(),
            &DisableCloneCellPayload {
                clone_cell_id: CloneCellId::DnaHash(alice_clone_cell.cell_id.dna_hash().clone()),
            },
        )
        .await
        .unwrap();
    let identical_clone_cell_err = conductor
        .clone()
        .create_clone_cell(alice_app.installed_app_id(), clone_cell_payload.clone())
        .await;
    matches!(
        identical_clone_cell_err,
        Err(ConductorError::AppError(AppError::DuplicateCellId(cell_id))) if cell_id == alice_clone_cell.cell_id
    );

    // ensure that bob can clone cell with identical hash in same conductor
    let bob_clone_cell = conductor
        .clone()
        .create_clone_cell(bob_app.installed_app_id(), clone_cell_payload)
        .await
        .unwrap();
    assert_eq!(
        alice_clone_cell.original_dna_hash,
        bob_clone_cell.original_dna_hash
    );
    assert_eq!(
        alice_clone_cell.cell_id.dna_hash(),
        bob_clone_cell.cell_id.dna_hash()
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn clone_cell_deletion() {
    let (dna, _, _) = SweetDnaFile::unique_from_test_wasms(vec![TestWasm::Create]).await;
    let role_name: RoleName = "dna_1".to_string();
    let mut conductor = SweetConductor::standard().await;
    let app_id = "app".to_string();
    conductor
        .setup_app(&app_id, [&(role_name.clone(), dna)])
        .await
        .unwrap();
    let clone_cell = conductor
        .clone()
        .create_clone_cell(
            &app_id,
            CreateCloneCellPayload {
                role_name: role_name.clone(),
                modifiers: DnaModifiersOpt::none().with_network_seed("seed_1".to_string()),
                membrane_proof: None,
                name: None,
            },
        )
        .await
        .unwrap();
    let clone_id = CloneCellId::CloneId(clone_cell.clone().clone_id);

    // disable clone cell
    conductor
        .raw_handle()
        .disable_clone_cell(
            &app_id,
            &DisableCloneCellPayload {
                clone_cell_id: clone_id.clone(),
            },
        )
        .await
        .unwrap();

    // calling the cell after disabling fails
    let zome = SweetZome::new(
        clone_cell.cell_id.clone(),
        TestWasm::Create.coordinator_zome_name(),
    );
    let zome_call_response: Result<ActionHash, _> = conductor
        .call_fallible(&zome, "call_create_entry", ())
        .await;
    assert!(zome_call_response.is_err());

    // enable the disabled clone cell by clone id
    let enabled_clone_cell = conductor
        .raw_handle()
        .enable_clone_cell(
            &app_id,
            &DisableCloneCellPayload {
                clone_cell_id: clone_id.clone(),
            },
        )
        .await
        .unwrap();

    // assert the enabled clone cell is the previously created clone cell
    assert_eq!(enabled_clone_cell, clone_cell);

    // assert that the cell appears in app info's cell data again
    let app_info = conductor.get_app_info(&app_id).await.unwrap().unwrap();
    let cell_info_for_role = app_info.cell_info.get(&role_name).unwrap();
    assert!(cell_info_for_role
        .iter()
        .any(|cell_info| if let CellInfo::Cloned(cell) = cell_info {
            cell.cell_id == clone_cell.cell_id.clone()
        } else {
            false
        }));

    // calling the cell after restoring succeeds
    let zome_call_response: Result<ActionHash, _> = conductor
        .call_fallible(&zome, "call_create_entry", ())
        .await;
    assert!(zome_call_response.is_ok());

    // disable clone cell again
    conductor
        .raw_handle()
        .disable_clone_cell(
            &app_id,
            &DisableCloneCellPayload {
                clone_cell_id: clone_id.clone(),
            },
        )
        .await
        .unwrap();

    // enable clone cell by cell id
    let enabled_clone_cell = conductor
        .raw_handle()
        .enable_clone_cell(
            &app_id,
            &DisableCloneCellPayload {
                clone_cell_id: CloneCellId::DnaHash(clone_cell.cell_id.dna_hash().clone()),
            },
        )
        .await
        .unwrap();

    // assert the enabled clone cell is the previously created clone cell
    assert_eq!(enabled_clone_cell, clone_cell);

    // disable and delete clone cell
    conductor
        .raw_handle()
        .disable_clone_cell(
            &app_id,
            &DisableCloneCellPayload {
                clone_cell_id: clone_id.clone(),
            },
        )
        .await
        .unwrap();
    conductor
        .raw_handle()
        .delete_clone_cell(&DeleteCloneCellPayload {
            app_id: app_id.clone(),
            clone_cell_id: CloneCellId::DnaHash(clone_cell.cell_id.dna_hash().clone()),
        })
        .await
        .unwrap();
    // assert the deleted cell cannot be enabled
    let disable_result = conductor
        .raw_handle()
        .enable_clone_cell(
            &app_id,
            &DisableCloneCellPayload {
                clone_cell_id: clone_id.clone(),
            },
        )
        .await;
    assert!(disable_result.is_err());
}

#[tokio::test(flavor = "multi_thread")]
async fn conductor_can_startup_with_cloned_cell() {
    let (dna, _, _) = SweetDnaFile::unique_from_test_wasms(vec![TestWasm::Create]).await;
    let role_name: RoleName = "dna_1".to_string();
    let mut conductor = SweetConductor::standard().await;
    let app = conductor
        .setup_app("app", [&(role_name.clone(), dna)])
        .await
        .unwrap();

    let clone_cell = conductor
        .clone()
        .create_clone_cell(
            app.installed_app_id(),
            CreateCloneCellPayload {
                role_name: role_name.clone(),
                modifiers: DnaModifiersOpt::none().with_network_seed("seed_1".to_string()),
                membrane_proof: None,
                name: None,
            },
        )
        .await
        .unwrap();

    // calling the cell works
    let zome = SweetZome::new(
        clone_cell.cell_id.clone(),
        TestWasm::Create.coordinator_zome_name(),
    );
    let zome_call_response: Result<ActionHash, _> = conductor
        .call_fallible(&zome, "call_create_entry", ())
        .await;
    assert!(zome_call_response.is_ok());

    conductor.shutdown().await;
    conductor.startup(false).await;

    // calling the cell works after restart
    let zome = SweetZome::new(
        clone_cell.cell_id.clone(),
        TestWasm::Create.coordinator_zome_name(),
    );
    let zome_call_response: Result<ActionHash, _> = conductor
        .call_fallible(&zome, "call_create_entry", ())
        .await;
    assert!(zome_call_response.is_ok());

    conductor
        .clone()
        .disable_clone_cell(
            app.installed_app_id(),
            &DisableCloneCellPayload {
                clone_cell_id: CloneCellId::DnaHash(clone_cell.cell_id.dna_hash().clone()),
            },
        )
        .await
        .unwrap();

    // calling the cell after disabling fails
    let zome = SweetZome::new(
        clone_cell.cell_id.clone(),
        TestWasm::Create.coordinator_zome_name(),
    );
    let zome_call_response: Result<ActionHash, _> = conductor
        .call_fallible(&zome, "call_create_entry", ())
        .await;
    matches!(zome_call_response, Err(ConductorApiError::CellError(CellError::CellDisabled(cell_id))) if cell_id == clone_cell.cell_id.clone());

    conductor.shutdown().await;
    conductor.startup(false).await;

    // calling the cell still fails after restart, cell still disabled
    let zome = SweetZome::new(
        clone_cell.cell_id.clone(),
        TestWasm::Create.coordinator_zome_name(),
    );
    let zome_call_response: Result<ActionHash, _> = conductor
        .call_fallible(&zome, "call_create_entry", ())
        .await;
    matches!(zome_call_response, Err(ConductorApiError::CellError(CellError::CellDisabled(cell_id))) if cell_id == clone_cell.cell_id.clone());
}