holochain 0.7.0-dev.22

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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use crate::conductor::api::error::ConductorApiError;
use crate::conductor::error::ConductorError;
use crate::core::ribosome::guest_callback::post_commit::PostCommitHostAccess;
use crate::core::ribosome::HostFnAccess;
use crate::core::ribosome::RibosomeError;
use crate::core::ribosome::RibosomeT;
use crate::core::ribosome::ZomeCallParamsSigned;
use crate::core::ribosome::{CallContext, HostContext, ZomeCallHostAccess};
use futures::future::join_all;
use holochain_nonce::fresh_nonce;
use holochain_types::prelude::*;
use holochain_wasmer_host::prelude::*;
use std::sync::Arc;
use wasmer::RuntimeError;

pub fn call(
    ribosome: Arc<impl RibosomeT>,
    call_context: Arc<CallContext>,
    inputs: Vec<Call>,
) -> Result<Vec<ZomeCallResponse>, RuntimeError> {
    let results: Vec<Result<ZomeCallResponse, RuntimeError>> =
        tokio_helper::block_forever_on(async move {
            join_all(inputs.into_iter().map(|input| async {
                match (
                    &input.target,
                    HostFnAccess::from(&call_context.host_context()),
                ) {
                    (
                        CallTarget::ConductorCell(_),
                        HostFnAccess {
                            write_workspace: Permission::Allow,
                            agent_info: Permission::Allow,
                            ..
                        },
                    )
                    | (
                        CallTarget::NetworkAgent(_),
                        HostFnAccess {
                            write_network: Permission::Allow,
                            agent_info: Permission::Allow,
                            ..
                        },
                    ) => execute_call(ribosome.clone(), call_context.clone(), input, false).await,
                    (
                        CallTarget::ConductorCell(_),
                        HostFnAccess {
                            write_workspace: Permission::Deny,
                            agent_info: Permission::Allow,
                            ..
                        },
                    ) => {
                        let zome_call_context =
                            call_context.switch_host_context(|context| match context {
                                HostContext::PostCommit(
                                    post_commit @ PostCommitHostAccess {
                                        call_zome_handle: Some(handle),
                                        ..
                                    },
                                ) => Ok(HostContext::ZomeCall(ZomeCallHostAccess::new(
                                    post_commit.workspace.clone(),
                                    post_commit.keystore.clone(),
                                    post_commit.network.clone(),
                                    post_commit.signal_tx.clone(),
                                    handle.clone(),
                                ))),
                                _ => Err(wasm_error!(WasmErrorInner::Host(
                                    RibosomeError::HostFnPermissions(
                                        call_context.zome.zome_name().clone(),
                                        call_context.function_name().clone(),
                                        "call".into(),
                                    )
                                    .to_string(),
                                ))
                                .into()),
                            })?;

                        execute_call(ribosome.clone(), Arc::new(zome_call_context), input, true)
                            .await
                    }
                    _ => Err(wasm_error!(WasmErrorInner::Host(
                        RibosomeError::HostFnPermissions(
                            call_context.zome.zome_name().clone(),
                            call_context.function_name().clone(),
                            "call".into(),
                        )
                        .to_string(),
                    ))
                    .into()),
                }
            }))
            .await
        });
    let results: Result<Vec<_>, _> = results.into_iter().collect();
    results
}

async fn execute_call(
    ribosome: Arc<impl RibosomeT>,
    call_context: Arc<CallContext>,
    input: Call,
    fork: bool,
) -> Result<ZomeCallResponse, RuntimeError> {
    let Call {
        target,
        zome_name,
        fn_name,
        cap_secret,
        payload,
    } = input;

    let provenance = call_context
        .host_context
        .workspace()
        .source_chain()
        .as_ref()
        .expect("Must have source chain to know provenance")
        .agent_pubkey()
        .clone();
    let (nonce, expires_at) = fresh_nonce(Timestamp::now())
        .map_err(|e| -> RuntimeError { wasm_error!(WasmErrorInner::Host(e.to_string())).into() })?;

    let result: Result<ZomeCallResponse, RuntimeError> = match target {
        CallTarget::NetworkAgent(target_agent) => {
            let zome_call_params = ZomeCallParams {
                provenance: provenance.clone(),
                cell_id: CellId::new(
                    ribosome.dna_def_hashed().as_hash().clone(),
                    target_agent.clone(),
                ),
                zome_name,
                fn_name,
                cap_secret,
                payload,
                nonce,
                expires_at,
            };
            let zome_call_payload = ZomeCallParamsSigned::try_from_params(
                call_context.host_context.keystore(),
                zome_call_params.clone(),
            )
            .await
            .map_err(|e| -> RuntimeError {
                wasm_error!(WasmErrorInner::Host(e.to_string())).into()
            })?;
            match call_context
                .host_context()
                .network()
                .call_remote(
                    target_agent,
                    zome_call_payload.bytes,
                    zome_call_payload.signature,
                    Some((
                        call_context.zome.zome_name().clone(),
                        call_context.function_name().clone(),
                    )),
                )
                .await
            {
                Ok(serialized_bytes) => ZomeCallResponse::try_from(serialized_bytes)
                    .map_err(|e| -> RuntimeError { wasm_error!(e).into() }),
                Err(e) => Ok(ZomeCallResponse::NetworkError(e.to_string())),
            }
        }
        CallTarget::ConductorCell(target_cell) => {
            let cell_id_result: Result<CellId, RuntimeError> = match target_cell {
                CallTargetCell::OtherRole(role_name) => {
                    let this_cell_id = call_context
                        .host_context()
                        .call_zome_handle()
                        .cell_id()
                        .clone();
                    call_context
                        .host_context()
                        .call_zome_handle()
                        .find_cell_with_role_alongside_cell(&this_cell_id, &role_name)
                        .await
                        .map_err(|e| -> RuntimeError { wasm_error!(e).into() })
                        .and_then(|c| {
                            c.ok_or_else(|| {
                                wasmer::RuntimeError::from(wasm_error!(WasmErrorInner::Host(
                                    format!("Role not found: {role_name}")
                                )))
                            })
                        })
                }
                CallTargetCell::OtherCell(cell_id) => Ok(cell_id),
                CallTargetCell::Local => Ok(call_context
                    .host_context()
                    .call_zome_handle()
                    .cell_id()
                    .clone()),
            };
            match cell_id_result {
                Ok(cell_id) => {
                    let zome_call_params = ZomeCallParams {
                        cell_id,
                        zome_name,
                        fn_name,
                        payload,
                        cap_secret,
                        provenance,
                        nonce,
                        expires_at,
                    };

                    let outcome = if fork {
                        call_context
                            .host_context()
                            .call_zome_handle()
                            .call_zome(zome_call_params)
                            .await
                    } else {
                        call_context
                            .host_context()
                            .call_zome_handle()
                            .call_zome_with_workspace(
                                zome_call_params,
                                call_context
                                    .host_context()
                                    .workspace_write()
                                    .clone()
                                    .try_into()
                                    .expect("Must have source chain to make zome call"),
                            )
                            .await
                    };

                    match outcome {
                        Ok(Ok(zome_call_response)) => Ok(zome_call_response),
                        Ok(Err(ribosome_error)) => Err(wasm_error!(WasmErrorInner::Host(
                            ribosome_error.to_string()
                        ))
                        .into()),
                        Err(ConductorApiError::ConductorError(ConductorError::CellMissing(
                            cell_id,
                        ))) => Err(wasm_error!(WasmErrorInner::Host(format!(
                            "Call to missing cell: Cell with ID {cell_id} not found"
                        )))
                        .into()),
                        Err(conductor_api_error) => Err(wasm_error!(WasmErrorInner::Host(
                            conductor_api_error.to_string()
                        ))
                        .into()),
                    }
                }
                Err(e) => Err(e),
            }
        }
    };
    result
}

#[cfg(test)]
pub mod wasm_test {
    use crate::conductor::api::error::ConductorApiError;
    use crate::conductor::CellError;
    use crate::core::ribosome::error::RibosomeError;
    use crate::core::workflow::WorkflowError;
    use crate::sweettest::SweetConductor;
    use crate::sweettest::SweetDnaFile;
    use hdk::prelude::AgentInfo;
    use hdk::prelude::WasmError;
    use hdk::prelude::WasmErrorInner;
    use holo_hash::ActionHash;
    use holochain_types::prelude::*;
    use holochain_wasm_test_utils::TestWasm;
    use matches::assert_matches;
    use rusqlite::named_params;

    use crate::test_utils::new_zome_call_params;
    use crate::test_utils::RibosomeTestFixture;
    use holochain_sqlite::prelude::DatabaseResult;

    #[tokio::test(flavor = "multi_thread")]
    async fn call_test() {
        holochain_trace::test_run();
        let test_wasm = TestWasm::WhoAmI;
        let (dna_file_1, _, _) = SweetDnaFile::unique_from_test_wasms(vec![test_wasm]).await;

        let dna_file_2 = dna_file_1
            .clone()
            .with_network_seed("CLONE".to_string())
            .await;

        let mut conductor = SweetConductor::standard().await;

        let app = conductor
            .setup_app(
                "app-",
                [
                    &("role1".to_string(), dna_file_1),
                    &("role2".to_string(), dna_file_2),
                ],
            )
            .await
            .unwrap();

        let agent_pubkey = app.agent().clone();
        let (cell1, cell2) = app.into_tuple();

        let zome1 = cell1.zome(test_wasm);
        let zome2 = cell2.zome(test_wasm);

        let _: () = conductor.call(&zome2, "set_access", ()).await;

        {
            let agent_info: AgentInfo = conductor
                .call(&zome1, "who_are_they_local", cell2.cell_id())
                .await;
            assert_eq!(agent_info.agent_initial_pubkey, agent_pubkey);
        }
        {
            let agent_info: AgentInfo = conductor.call(&zome1, "who_are_they_role", "role2").await;
            assert_eq!(agent_info.agent_initial_pubkey, agent_pubkey);
        }
    }

    /// When calling the same cell we need to make sure
    /// the "as at" doesn't cause the original zome call to fail
    /// when they are both writing (moving the source chain forward)
    #[tokio::test(flavor = "multi_thread")]
    async fn call_the_same_cell() {
        holochain_trace::test_run();

        let zomes = vec![TestWasm::WhoAmI, TestWasm::Create];
        let (dna, _, _) = SweetDnaFile::unique_from_test_wasms(zomes).await;
        let mut conductor = SweetConductor::standard().await;
        let (alice,) = conductor
            .setup_app("app", &[dna])
            .await
            .unwrap()
            .into_tuple();

        let handle = conductor.raw_handle();

        let zome_call_params =
            new_zome_call_params(alice.cell_id(), "call_create_entry", (), TestWasm::Create)
                .unwrap();
        let result = handle.call_zome(zome_call_params).await;
        assert_matches!(result, Ok(Ok(ZomeCallResponse::Ok(_))));

        // Get the action hash of that entry
        let action_hash: ActionHash =
            unwrap_to::unwrap_to!(result.unwrap().unwrap() => ZomeCallResponse::Ok)
                .decode()
                .unwrap();

        // Check alice's source chain contains the new value
        let has_hash: bool = handle
            .get_spaces()
            .get_or_create_authored_db(alice.dna_hash(), alice.agent_pubkey().clone())
            .unwrap()
            .read_async(move |txn| -> DatabaseResult<bool> {
                Ok(txn.query_row(
                    "SELECT EXISTS(SELECT 1 FROM DhtOp WHERE action_hash = :hash)",
                    named_params! {
                        ":hash": action_hash
                    },
                    |row| row.get(0),
                )?)
            })
            .await
            .unwrap();
        assert!(has_hash);
    }

    /// Test calling a cell in a different app, a so-called bridge call.
    #[tokio::test(flavor = "multi_thread")]
    async fn bridge_call() {
        holochain_trace::test_run();

        // The init function of this test wasm creates an unrestricted cap grant for the `create_entry` function.
        // When Bob calls the function that makes the bridge call to the `create_entry` function, it succeeds
        // because of this cap grant.
        let (dna_file, _, _) = SweetDnaFile::unique_from_test_wasms(vec![TestWasm::Create]).await;

        let mut conductor = SweetConductor::standard().await;

        let apps = conductor.setup_apps("app", 2, &[dna_file]).await.unwrap();
        let ((alice,), (bobbo,)) = apps.into_tuples();
        let bob_pubkey = bobbo.agent_pubkey().clone();

        let (dna_file, _, _) = SweetDnaFile::unique_from_test_wasms(vec![TestWasm::WhoAmI]).await;
        let apps = conductor
            .setup_app_for_agents("app2", &[bob_pubkey], &[dna_file])
            .await
            .unwrap();
        let ((bobbo2,),) = apps.into_tuples();
        let action_hash: ActionHash = conductor
            .call(
                &bobbo2.zome(TestWasm::WhoAmI),
                "call_create_entry",
                alice.cell_id().clone(),
            )
            .await;

        // Check alice's source chain contains the new value
        let has_hash: bool = alice
            .authored_db()
            .read_async(move |txn| -> DatabaseResult<bool> {
                Ok(txn.query_row(
                    "SELECT EXISTS(SELECT 1 FROM DhtOp WHERE action_hash = :hash)",
                    named_params! {
                        ":hash": action_hash
                    },
                    |row| row.get(0),
                )?)
            })
            .await
            .unwrap();
        assert!(has_hash);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn bridge_call_returns_specific_error_when_cell_missing() {
        let dna_file = SweetDnaFile::unique_from_test_wasms(vec![TestWasm::WhoAmI])
            .await
            .0;
        let mut conductor = SweetConductor::standard().await;
        let app = conductor.setup_app("app", &[dna_file]).await.unwrap();
        let missing_cell_id = ::fixt::fixt!(CellId);
        let error = conductor
            .call_fallible::<_, ActionHash>(
                &app.cells()[0].zome(TestWasm::WhoAmI.coordinator_zome_name()),
                "call_create_entry",
                missing_cell_id.clone(),
            )
            .await
            .unwrap_err();
        if let ConductorApiError::CellError(CellError::WorkflowError(wf_err)) = error {
            if let WorkflowError::RibosomeError(RibosomeError::WasmRuntimeError(err)) = *wf_err {
                let actual_error = err.downcast::<WasmError>().unwrap().error;
                assert_eq!(
                    actual_error,
                    WasmErrorInner::Host(format!(
                        "Call to missing cell: Cell with ID {missing_cell_id} not found"
                    ))
                );
            } else {
                panic!("unexpected error: {wf_err}");
            }
        } else {
            panic!("unexpected error: {error}");
        }
    }

    /// we can call a fn on a remote
    #[tokio::test(flavor = "multi_thread")]
    async fn call_remote_test() {
        holochain_trace::test_run();
        let RibosomeTestFixture {
            conductor,
            alice,
            bob,
            bob_pubkey,
            ..
        } = RibosomeTestFixture::new(TestWasm::WhoAmI).await;

        let _: () = conductor.call(&bob, "set_access", ()).await;
        let agent_info: AgentInfo = conductor
            .call(&alice, "whoarethey", bob_pubkey.clone())
            .await;
        assert_eq!(agent_info.agent_initial_pubkey, bob_pubkey);
    }
}