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
466
467
468
469
470
471
472
473
474
475
476
#![allow(missing_docs)]

use crate::conductor::api::CellConductorApi;
use crate::conductor::api::CellConductorApiT;
use crate::conductor::api::CellConductorReadHandle;
use crate::conductor::interface::SignalBroadcaster;
use crate::conductor::ConductorHandle;
use crate::core::ribosome::host_fn;
use crate::core::ribosome::real_ribosome::RealRibosome;
use crate::core::ribosome::CallContext;
use crate::core::ribosome::HostContext;
use crate::core::ribosome::InvocationAuth;
use crate::core::ribosome::RibosomeT;
use crate::core::ribosome::ZomeCallHostAccess;
use crate::core::ribosome::ZomeCallInvocation;
use crate::core::workflow::call_zome_function_authorized;
use hdk::prelude::*;
use holo_hash::ActionHash;
use holo_hash::AgentPubKey;
use holo_hash::AnyDhtHash;
use holochain_keystore::MetaLairClient;
use holochain_p2p::actor::GetLinksOptions;
use holochain_p2p::actor::HolochainP2pRefToDna;
use holochain_p2p::HolochainP2pDna;
use holochain_state::host_fn_workspace::HostFnWorkspace;
use holochain_types::db_cache::DhtDbQueryCache;
use holochain_types::prelude::*;
use holochain_wasm_test_utils::TestWasmPair;
use holochain_zome_types::AgentActivity;
use std::sync::Arc;
use unwrap_to::unwrap_to;

// Commit entry types //
// Useful for when you want to commit something
// that will match entry defs
pub const POST_ID: &str = "post";
pub const POST_INDEX: EntryDefIndex = EntryDefIndex(0);
pub const MSG_ID: &str = "msg";
pub const MSG_INDEX: EntryDefIndex = EntryDefIndex(1);
pub const VALID_ID: &str = "always_validates";
pub const INVALID_ID: &str = "never_validates";

#[derive(
    Default, Debug, PartialEq, Clone, SerializedBytes, serde::Serialize, serde::Deserialize,
)]
#[repr(transparent)]
#[serde(transparent)]
pub struct Post(pub String);
#[derive(
    Default, Debug, PartialEq, Clone, SerializedBytes, serde::Serialize, serde::Deserialize,
)]
#[repr(transparent)]
#[serde(transparent)]
pub struct Msg(pub String);

/// A CellId plus ZomeName: the full "path" to a zome in the conductor
#[derive(Clone, Debug, derive_more::From, derive_more::Into)]
pub struct ZomePath(CellId, ZomeName);

impl ZomePath {
    pub fn cell_id(&self) -> &CellId {
        &self.0
    }

    pub fn zome_name(&self) -> &ZomeName {
        &self.1
    }
}

/// Type from the validate wasm
// TODO: Maybe we can dry this up by putting the wasm types
// somewhere outside the wasm?
#[derive(Deserialize, Serialize, SerializedBytes, Debug, Clone)]
pub enum ThisWasmEntry {
    AlwaysValidates,
    NeverValidates,
}

#[derive(Deserialize, Serialize, SerializedBytes, Debug, Clone)]
pub enum MaybeLinkable {
    AlwaysLinkable,
    NeverLinkable,
}

/// A freely callable version of the host fn api, so that host functions
/// can be called from Rust instead of Wasm
#[derive(Clone)]
pub struct HostFnCaller {
    pub authored_db: DbWrite<DbKindAuthored>,
    pub dht_db: DbWrite<DbKindDht>,
    pub dht_db_cache: DhtDbQueryCache,
    pub cache: DbWrite<DbKindCache>,
    pub ribosome: RealRibosome,
    pub zome_path: ZomePath,
    pub network: HolochainP2pDna,
    pub keystore: MetaLairClient,
    pub signal_tx: SignalBroadcaster,
    pub call_zome_handle: CellConductorReadHandle,
}

impl HostFnCaller {
    /// Create HostFnCaller for the first zome.
    // #[deprecated = "use create_for_zome"]
    pub async fn create(
        cell_id: &CellId,
        handle: &ConductorHandle,
        dna_file: &DnaFile,
    ) -> HostFnCaller {
        Self::create_for_zome(cell_id, handle, dna_file, 0).await
    }

    /// Create HostFnCaller for a specific zome if there are multiple.
    pub async fn create_for_zome(
        cell_id: &CellId,
        handle: &ConductorHandle,
        dna_file: &DnaFile,
        zome_index: usize,
    ) -> HostFnCaller {
        let authored_db = handle.get_authored_db(cell_id.dna_hash()).unwrap();
        let dht_db = handle.get_dht_db(cell_id.dna_hash()).unwrap();
        let dht_db_cache = handle.get_dht_db_cache(cell_id.dna_hash()).unwrap();
        let cache = handle.get_cache_db(cell_id).await.unwrap();
        let keystore = handle.keystore().clone();
        let network = handle
            .holochain_p2p()
            .to_dna(cell_id.dna_hash().clone(), None);

        let zome_path = (
            cell_id.clone(),
            dna_file
                .dna()
                .integrity_zomes
                .get(zome_index)
                .unwrap()
                .0
                .clone(),
        )
            .into();
        let ribosome = handle.get_ribosome(dna_file.dna_hash()).unwrap();
        let signal_tx = handle.signal_broadcaster();
        let call_zome_handle =
            CellConductorApi::new(handle.clone(), cell_id.clone()).into_call_zome_handle();
        HostFnCaller {
            authored_db,
            dht_db,
            dht_db_cache,
            cache,
            ribosome,
            zome_path,
            network,
            keystore,
            signal_tx,
            call_zome_handle,
        }
    }

    pub fn authored_db(&self) -> DbWrite<DbKindAuthored> {
        self.authored_db.clone()
    }

    pub fn dht_db(&self) -> DbWrite<DbKindDht> {
        self.dht_db.clone()
    }

    pub async fn unpack(&self) -> (Arc<RealRibosome>, Arc<CallContext>, HostFnWorkspace) {
        let HostFnCaller {
            authored_db,
            dht_db,
            cache,
            network,
            keystore,
            ribosome,
            signal_tx,
            zome_path,
            call_zome_handle,
            dht_db_cache,
        } = self.clone();

        let (cell_id, zome_name) = zome_path.into();

        let workspace_lock = HostFnWorkspace::new(
            authored_db,
            dht_db,
            dht_db_cache,
            cache,
            keystore.clone(),
            Some(cell_id.agent_pubkey().clone()),
            Arc::new(ribosome.dna_def().as_content().clone()),
        )
        .await
        .unwrap();
        let host_access = ZomeCallHostAccess::new(
            workspace_lock.clone(),
            keystore,
            network,
            signal_tx,
            call_zome_handle,
        );
        let ribosome = Arc::new(ribosome);
        let zome = ribosome.dna_def().get_zome(&zome_name).unwrap();
        let call_context = Arc::new(CallContext::new(
            zome,
            FunctionName::new("not_sure_what_should_be_here"),
            host_access.into(),
            // Auth as the author.
            InvocationAuth::Cap(cell_id.agent_pubkey().clone(), None),
        ));
        (ribosome, call_context, workspace_lock)
    }
}

impl HostFnCaller {
    pub fn get_entry_type(
        &self,
        zome: impl Into<TestWasmPair<ZomeName>>,
        index: impl Into<EntryDefIndex>,
    ) -> ScopedEntryDefIndex {
        let TestWasmPair { integrity, .. } = zome.into();
        let zome_index = self
            .ribosome
            .dna_def()
            .integrity_zomes
            .iter()
            .position(|(z, _)| *z == integrity)
            .unwrap();
        let zome_types = self
            .ribosome
            .zome_types()
            .in_scope_subset(&[ZomeIndex(zome_index as u8)]);
        zome_types
            .entries
            .get(ZomeTypesKey {
                zome_index: 0.into(),
                type_index: index.into(),
            })
            .unwrap()
    }
    pub fn get_entry_link(
        &self,
        zome: impl Into<TestWasmPair<ZomeName>>,
        index: impl Into<LinkType>,
    ) -> ScopedLinkType {
        let TestWasmPair { integrity, .. } = zome.into();
        let zome_index = self
            .ribosome
            .dna_def()
            .integrity_zomes
            .iter()
            .position(|(z, _)| *z == integrity)
            .unwrap();
        let zome_types = self
            .ribosome
            .zome_types()
            .in_scope_subset(&[ZomeIndex(zome_index as u8)]);
        zome_types
            .links
            .get(ZomeTypesKey {
                zome_index: 0.into(),
                type_index: index.into(),
            })
            .unwrap()
    }
    pub async fn commit_entry<E: Into<EntryDefLocation>>(
        &self,
        entry: Entry,
        entry_def_id: E,
        visibility: EntryVisibility,
    ) -> ActionHash {
        let (ribosome, call_context, workspace_lock) = self.unpack().await;
        let input = CreateInput::new(
            entry_def_id.into(),
            visibility,
            entry,
            ChainTopOrdering::default(),
        );
        let output = host_fn::create::create(ribosome, call_context, input).unwrap();

        // Write
        workspace_lock.flush(&self.network).await.unwrap();

        output
    }

    pub async fn delete_entry<'env>(&self, input: DeleteInput) -> ActionHash {
        let (ribosome, call_context, workspace_lock) = self.unpack().await;
        let output = {
            let r = host_fn::delete::delete(ribosome, call_context, input);
            let r = r.map_err(|e| {
                debug!(%e);
                e
            });
            r.unwrap()
        };

        // Write
        workspace_lock.flush(&self.network).await.unwrap();

        output
    }

    pub async fn update_entry(
        &self,
        entry: Entry,
        original_action_address: ActionHash,
    ) -> ActionHash {
        let (ribosome, call_context, workspace_lock) = self.unpack().await;
        let input = UpdateInput {
            original_action_address,
            entry,
            chain_top_ordering: Default::default(),
        };

        let output = { host_fn::update::update(ribosome, call_context, input).unwrap() };

        // Write
        workspace_lock.flush(&self.network).await.unwrap();

        output
    }

    pub async fn get(&self, entry_hash: AnyDhtHash, options: GetOptions) -> Vec<Option<Record>> {
        let (ribosome, call_context, _) = self.unpack().await;
        let input = GetInput::new(entry_hash, options);
        host_fn::get::get(ribosome, call_context, vec![input]).unwrap()
    }

    pub async fn get_details<'env>(
        &self,
        entry_hash: AnyDhtHash,
        options: GetOptions,
    ) -> Vec<Option<Details>> {
        let (ribosome, call_context, _) = self.unpack().await;
        let input = GetInput::new(entry_hash, options);
        host_fn::get_details::get_details(ribosome, call_context, vec![input]).unwrap()
    }

    pub async fn create_link<'env>(
        &self,
        base: AnyLinkableHash,
        target: AnyLinkableHash,
        zome_index: impl Into<ZomeIndex>,
        link_type: impl Into<LinkType>,
        link_tag: LinkTag,
    ) -> ActionHash {
        let (ribosome, call_context, workspace_lock) = self.unpack().await;
        let input = CreateLinkInput::new(
            base,
            target,
            zome_index.into(),
            link_type.into(),
            link_tag,
            ChainTopOrdering::default(),
        );
        let output = { host_fn::create_link::create_link(ribosome, call_context, input).unwrap() };

        // Write
        workspace_lock.flush(&self.network).await.unwrap();

        output
    }

    pub async fn delete_link<'env>(&self, link_add_hash: ActionHash) -> ActionHash {
        let (ribosome, call_context, workspace_lock) = self.unpack().await;
        let output = {
            host_fn::delete_link::delete_link(
                ribosome,
                call_context,
                DeleteLinkInput::new(link_add_hash, ChainTopOrdering::default()),
            )
            .unwrap()
        };

        // Write
        workspace_lock.flush(&self.network).await.unwrap();

        output
    }

    pub async fn get_links<'env>(
        &self,
        base: AnyLinkableHash,
        type_query: LinkTypeFilter,
        link_tag: Option<LinkTag>,
        _options: GetLinksOptions,
    ) -> Vec<Link> {
        let (ribosome, call_context, workspace_lock) = self.unpack().await;
        let input = GetLinksInput::new(base, type_query, link_tag);
        let output = {
            host_fn::get_links::get_links(ribosome, call_context, vec![input])
                .unwrap()
                .into_iter()
                .next()
                .unwrap()
        };

        // Write
        workspace_lock.flush(&self.network).await.unwrap();

        output
    }

    pub async fn get_link_details<'env>(
        &self,
        base: AnyLinkableHash,
        type_query: LinkTypeFilter,
        tag: LinkTag,
        _options: GetLinksOptions,
    ) -> Vec<(SignedActionHashed, Vec<SignedActionHashed>)> {
        let (ribosome, call_context, workspace_lock) = self.unpack().await;
        let input = GetLinksInput::new(base, type_query, Some(tag));
        let output = {
            host_fn::get_link_details::get_link_details(ribosome, call_context, vec![input])
                .unwrap()
                .into_iter()
                .next()
                .unwrap()
        };

        // Write
        workspace_lock.flush(&self.network).await.unwrap();

        output.into()
    }

    pub async fn get_agent_activity(
        &self,
        agent: &AgentPubKey,
        query: &ChainQueryFilter,
        request: ActivityRequest,
    ) -> AgentActivity {
        let (ribosome, call_context, _) = self.unpack().await;
        let input = GetAgentActivityInput::new(agent.clone(), query.clone(), request);
        host_fn::get_agent_activity::get_agent_activity(ribosome, call_context, input).unwrap()
    }

    pub async fn call_zome_direct(&self, invocation: ZomeCallInvocation) -> ExternIO {
        let (ribosome, call_context, workspace_lock) = self.unpack().await;

        let (_, output) = {
            let host_access = call_context.host_context();
            let zcha = unwrap_to!(host_access => HostContext::ZomeCall).clone();
            call_zome_function_authorized((*ribosome).clone(), zcha, invocation)
                .await
                .unwrap()
        };

        // Write
        workspace_lock.flush(&self.network).await.unwrap();
        unwrap_to!(output.unwrap() => ZomeCallResponse::Ok).to_owned()
    }
}

#[macro_export]
macro_rules! test_entry_impl {
    ($type:ident) => {
        impl TryFrom<$type> for Entry {
            type Error = EntryError;
            fn try_from(n: $type) -> Result<Self, Self::Error> {
                Ok(Entry::App(SerializedBytes::try_from(n)?.try_into()?))
            }
        }

        impl TryFrom<Entry> for $type {
            type Error = SerializedBytesError;
            fn try_from(entry: Entry) -> Result<Self, Self::Error> {
                let entry = unwrap_to!(entry => Entry::App).clone();
                $type::try_from(entry.into_sb())
            }
        }
    };
}

test_entry_impl!(ThisWasmEntry);
test_entry_impl!(Post);
test_entry_impl!(Msg);
test_entry_impl!(MaybeLinkable);