Skip to content

Commit

Permalink
pr fixes, take dna hash
Browse files Browse the repository at this point in the history
  • Loading branch information
freesig committed Nov 4, 2020
1 parent 9ee3abc commit 9339d10
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 102 deletions.
3 changes: 3 additions & 0 deletions crates/hdk/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ pub enum HdkError {

#[error(transparent)]
Wasm(#[from] holochain_wasmer_guest::WasmError),

#[error("Zome call was made which the caller was unauthorized to make")]
UnauthorizedZomeCall,
}
24 changes: 17 additions & 7 deletions crates/hdk/src/host_fn/call.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::*;
use holo_hash::AgentPubKey;
use holo_hash::{AgentPubKey, DnaHash};
use holochain_wasmer_guest::SerializedBytesError;
use holochain_zome_types::{
call::Call,
Expand All @@ -8,19 +8,29 @@ use holochain_zome_types::{

use crate::host_fn;

pub fn call(
pub fn call<I, O>(
to_agent: AgentPubKey,
to_dna: Option<DnaHash>,
zome_name: ZomeName,
fn_name: FunctionName,
cap_secret: Option<CapSecret>,
request: SerializedBytes,
) -> Result<ZomeCallResponse, SerializedBytesError> {
request: I,
) -> ExternResult<O>
where
I: TryInto<SerializedBytes, Error = SerializedBytesError>,
O: TryFrom<SerializedBytes, Error = SerializedBytesError>,
{
let request: SerializedBytes = request.try_into()?;
let provenance = agent_info!()?.agent_latest_pubkey;
host_fn!(
let out = host_fn!(
__call,
CallInput::new(Call::new(
to_agent, zome_name, fn_name, cap_secret, request, provenance
to_agent, to_dna, zome_name, fn_name, cap_secret, request, provenance
)),
CallOutput
)
)?;
match out {
ZomeCallResponse::Ok(e) => Ok(O::try_from(e.into_inner())?),
ZomeCallResponse::Unauthorized => Err(HdkError::UnauthorizedZomeCall),
}
}
15 changes: 10 additions & 5 deletions crates/holochain/src/conductor/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
//! SourceChain which has already undergone Genesis.

use super::{interface::SignalBroadcaster, manager::ManagedTaskAdd};
use crate::conductor::handle::ConductorHandle;
use crate::conductor::{api::error::ConductorApiError, entry_def_store::get_entry_def_from_ids};
use crate::core::queue_consumer::{spawn_queue_consumer_tasks, InitialQueueTriggers};
use crate::core::ribosome::ZomeCallInvocation;
use crate::{
conductor::api::CellConductorApiT,
core::workflow::produce_dht_ops_workflow::dht_op_light::light_to_op,
};
use crate::{conductor::handle::ConductorHandle, core::workflow::CallZomeWorkspaceType};
use call_zome_workflow::call_zome_workspace_lock::CallZomeWorkspaceLock;
use holochain_zome_types::validate::ValidationPackage;
use holochain_zome_types::zome::FunctionName;
Expand Down Expand Up @@ -768,10 +768,14 @@ impl Cell {

let arc = self.env();
let keystore = arc.keystore().clone();
let workspace = match workspace_lock {
Some(l) => CallZomeWorkspaceType::Used(l),
None => CallZomeWorkspaceType::Fresh(CallZomeWorkspace::new(arc.clone().into())?),

// If there is no existing zome call then this is the root zome call
let is_root_zome_call = workspace_lock.is_none();
let workspace_lock = match workspace_lock {
Some(l) => l,
None => CallZomeWorkspaceLock::new(CallZomeWorkspace::new(arc.clone().into())?),
};

let conductor_api = self.conductor_api.clone();
let signal_tx = self.signal_broadcaster().await;
let ribosome = self.get_ribosome().await?;
Expand All @@ -781,9 +785,10 @@ impl Cell {
invocation,
conductor_api,
signal_tx,
is_root_zome_call,
};
Ok(call_zome_workflow(
workspace,
workspace_lock,
self.holochain_p2p_cell.clone(),
keystore,
arc.clone().into(),
Expand Down
16 changes: 9 additions & 7 deletions crates/holochain/src/core/ribosome/host_fn/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ pub fn call(
input: CallInput,
) -> RibosomeResult<CallOutput> {
let call = input.into_inner();
let dna_hash = ribosome.dna_file().dna_hash().clone();
let to_agent = call.to_agent();
let dna_hash = call
.to_dna
.unwrap_or_else(|| ribosome.dna_file().dna_hash().clone());
let to_agent = call.to_agent;
let cell_id = CellId::new(dna_hash, to_agent);
let invocation = ZomeCallInvocation {
cell_id,
zome_name: call.zome_name(),
cap: call.cap(),
fn_name: call.fn_name(),
payload: ExternInput::new(call.request()),
provenance: call.provenance(),
zome_name: call.zome_name,
cap: call.cap,
fn_name: call.fn_name,
payload: ExternInput::new(call.request),
provenance: call.provenance,
};
let host_access = call_context.host_access();
let result: ZomeCallResponse = tokio_safe_block_on::tokio_safe_block_forever_on(async move {
Expand Down
42 changes: 13 additions & 29 deletions crates/holochain/src/core/workflow/call_zome_workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,26 @@ pub struct CallZomeWorkflowArgs<Ribosome: RibosomeT, C: CellConductorApiT> {
pub invocation: ZomeCallInvocation,
pub signal_tx: SignalBroadcaster,
pub conductor_api: C,
pub is_root_zome_call: bool,
}

#[instrument(skip(workspace, network, keystore, writer, args, trigger_produce_dht_ops))]
#[instrument(skip(
workspace_lock,
network,
keystore,
writer,
args,
trigger_produce_dht_ops
))]
pub async fn call_zome_workflow<'env, Ribosome: RibosomeT, C: CellConductorApiT>(
workspace: CallZomeWorkspaceType,
workspace_lock: CallZomeWorkspaceLock,
network: HolochainP2pCell,
keystore: KeystoreSender,
writer: OneshotWriter,
args: CallZomeWorkflowArgs<Ribosome, C>,
mut trigger_produce_dht_ops: TriggerSender,
) -> WorkflowResult<ZomeCallInvocationResult> {
let should_write = workspace.should_write();
let workspace_lock = workspace.into_lock();
let should_write = args.is_root_zome_call;
let result = call_zome_workflow_inner(workspace_lock.clone(), network, keystore, args).await?;

// --- END OF WORKFLOW, BEGIN FINISHER BOILERPLATE ---
Expand Down Expand Up @@ -86,6 +93,7 @@ async fn call_zome_workflow_inner<'env, Ribosome: RibosomeT, C: CellConductorApi
invocation,
signal_tx,
conductor_api,
..
} = args;

let call_zome_handle = conductor_api.clone().into_call_zome_handle();
Expand Down Expand Up @@ -249,15 +257,6 @@ pub struct CallZomeWorkspace {
pub meta_cache: MetadataBuf,
}

/// When zome calls are made from
/// other zome calls in the same cell
/// we need to use an existing workspace
/// and not write to it.
pub enum CallZomeWorkspaceType {
Fresh(CallZomeWorkspace),
Used(CallZomeWorkspaceLock),
}

impl<'a> CallZomeWorkspace {
pub fn new(env: EnvironmentRead) -> WorkspaceResult<Self> {
let source_chain = SourceChain::new(env.clone())?;
Expand Down Expand Up @@ -316,22 +315,6 @@ impl Workspace for CallZomeWorkspace {
}
}

impl CallZomeWorkspaceType {
pub fn into_lock(self) -> CallZomeWorkspaceLock {
match self {
CallZomeWorkspaceType::Fresh(workspace) => CallZomeWorkspaceLock::new(workspace),
CallZomeWorkspaceType::Used(lock) => lock,
}
}
pub fn should_write(&self) -> bool {
if let CallZomeWorkspaceType::Fresh(_) = self {
true
} else {
false
}
}
}

#[cfg(test)]
pub mod tests {
use super::*;
Expand Down Expand Up @@ -373,6 +356,7 @@ pub mod tests {
ribosome,
signal_tx: SignalBroadcaster::noop(),
conductor_api,
is_root_zome_call: true,
};
call_zome_workflow_inner(workspace.into(), network, keystore, args).await
}
Expand Down
23 changes: 11 additions & 12 deletions crates/test_utils/wasm/wasm_workspace/create_entry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,29 @@ fn init(_: ()) -> ExternResult<InitCallbackResult> {
// grant unrestricted access to accept_cap_claim so other agents can send us claims
let mut functions: GrantedFunctions = HashSet::new();
functions.insert((zome_info!()?.zome_name, "create_entry".into()));
create_cap_grant!(
CapGrantEntry {
tag: "".into(),
// empty access converts to unrestricted
access: ().into(),
functions,
}
)?;
create_cap_grant!(CapGrantEntry {
tag: "".into(),
// empty access converts to unrestricted
access: ().into(),
functions,
})?;

Ok(InitCallbackResult::Pass)
}

/// Create a post entry then
/// create another post through a
/// Create a post entry then
/// create another post through a
/// call
#[hdk_extern]
fn call_create_entry(_: ()) -> ExternResult<ZomeCallResponse> {
fn call_create_entry(_: ()) -> ExternResult<HeaderHash> {
let me = agent_info!()?.agent_latest_pubkey;
create_entry!(post())?;
Ok(call(
me,
None,
"create_entry".to_string().into(),
"create_entry".to_string().into(),
None,
().try_into()?,
(),
)?)
}
15 changes: 4 additions & 11 deletions crates/test_utils/wasm/wasm_workspace/whoami/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,12 @@ fn whoarethey(agent_pubkey: AgentPubKey) -> ExternResult<AgentInfo> {
// it's just that the output comes _from the opinion of the remote agent_
#[hdk_extern]
fn who_are_they_local(agent_pubkey: AgentPubKey) -> ExternResult<AgentInfo> {
let response: ZomeCallResponse = call(
call(
agent_pubkey,
None,
zome_info!()?.zome_name,
"whoami".to_string().into(),
None,
().try_into()?,
)?;

match response {
ZomeCallResponse::Ok(guest_output) => Ok(guest_output.into_inner().try_into()?),
// we're just panicking here because our simple tests can always call set_access before
// calling whoami, but in a real app you'd want to handle this by returning an `Ok` with
// something meaningful to the extern's client
ZomeCallResponse::Unauthorized => unreachable!(),
}
(),
)
}
41 changes: 10 additions & 31 deletions crates/zome_types/src/call.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
use crate::capability::CapSecret;
use crate::zome::FunctionName;
use crate::zome::ZomeName;
use holo_hash::AgentPubKey;
use holo_hash::{AgentPubKey, DnaHash};
use holochain_serialized_bytes::prelude::SerializedBytes;

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct Call {
to_agent: AgentPubKey,
zome_name: ZomeName,
fn_name: FunctionName,
cap: Option<CapSecret>,
request: SerializedBytes,
provenance: AgentPubKey,
pub to_agent: AgentPubKey,
pub to_dna: Option<DnaHash>,
pub zome_name: ZomeName,
pub fn_name: FunctionName,
pub cap: Option<CapSecret>,
pub request: SerializedBytes,
pub provenance: AgentPubKey,
}

impl Call {
pub fn new(
to_agent: AgentPubKey,
to_dna: Option<DnaHash>,
zome_name: ZomeName,
fn_name: FunctionName,
cap: Option<CapSecret>,
Expand All @@ -25,35 +27,12 @@ impl Call {
) -> Self {
Self {
to_agent,
to_dna,
zome_name,
fn_name,
cap,
request,
provenance,
}
}

pub fn to_agent(&self) -> AgentPubKey {
self.to_agent.clone()
}

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

pub fn fn_name(&self) -> FunctionName {
self.fn_name.clone()
}

pub fn cap(&self) -> Option<CapSecret> {
self.cap
}

pub fn request(&self) -> SerializedBytes {
self.request.clone()
}

pub fn provenance(&self) -> AgentPubKey {
self.provenance.clone()
}
}

1 comment on commit 9339d10

@thedavidmeister
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarking wasm_call_n/1: Analyzing
wasm_call_n/1           time:   [1.5689 ms 1.5702 ms 1.5718 ms]
                        thrpt:  [636.19   B/s 636.85   B/s 637.37   B/s]
                 change:
                        time:   [-1.5410% -0.4897% +0.4668%] (p = 0.36 > 0.05)
                        thrpt:  [-0.4646% +0.4921% +1.5651%]
                        No change in performance detected.
Found 19 outliers among 100 measurements (19.00%)
  2 (2.00%) high mild
  17 (17.00%) high severe
Benchmarking wasm_call_n/1000
Benchmarking wasm_call_n/1000: Analyzing
wasm_call_n/1000        time:   [1.5904 ms 1.5938 ms 1.5985 ms]
                        thrpt:  [610.93 KiB/s 612.74 KiB/s 614.02 KiB/s]
                 change:
                        time:   [-4.3865% -3.9550% -3.4942%] (p = 0.00 < 0.05)
                        thrpt:  [+3.6207% +4.1179% +4.5878%]
                        Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
  3 (3.00%) high mild
  5 (5.00%) high severe
Benchmarking wasm_call_n/1000000
Benchmarking wasm_call_n/1000000: Analyzing
wasm_call_n/1000000     time:   [26.878 ms 26.929 ms 26.988 ms]
                        thrpt:  [35.337 MiB/s 35.415 MiB/s 35.481 MiB/s]
                 change:
                        time:   [+0.8109% +1.1393% +1.4573%] (p = 0.00 < 0.05)
                        thrpt:  [-1.4363% -1.1265% -0.8044%]
                        Change within noise threshold.
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) high mild
  1 (1.00%) high severe```

Please sign in to comment.