Skip to content

Commit

Permalink
adds zome_call_typed method to app_ws (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
peeech authored Jun 10, 2024
1 parent 2e0a70a commit 18255aa
Show file tree
Hide file tree
Showing 44 changed files with 861 additions and 911 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ holochain_websocket = "0.4.0-dev.2"
holofuel_types = "0.5.9"
sodoken = "0.0.11"
serde = { version = "1.0", features = ["derive", "rc"] } # { version = "1.0.193", features = ["derive"] }
rmp-serde = "1.1.1"
mr_bundle = { version = "0.4.0-dev.1" }
reqwest = { version = "0.12.4", features = ["json"]}

Expand Down
1 change: 1 addition & 0 deletions crates/configure-holochain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ url = "2.2"
holo_happ_manager = { version = "0.1.0", path = "../holo_happ_manager" }
hpos_hc_connect = { path = "../hpos_connect_hc" }
hpos-config-core = { workspace = true }
holochain_types = { workspace = true }

[dev-dependencies]
test-case = "2.2.2"
Expand Down
23 changes: 0 additions & 23 deletions crates/configure-holochain/src/hpos_holochain_api.rs

This file was deleted.

70 changes: 70 additions & 0 deletions crates/configure-holochain/src/jurisdictions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use anyhow::Result;
use holochain_types::{
dna::AgentPubKey,
prelude::{FunctionName, ZomeName},
};
use hpos_hc_connect::{app_connection::CoreAppRoleName, hha_agent::HHAAgent, holo_config::Config};
use serde::{Deserialize, Serialize};
use std::process::{Command, Output};

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct HostingCriteria {
id: String,
jurisdiction: String,
kyc: String,
}

pub async fn get_jurisdiction() -> Result<String> {
let output: Output = Command::new("hpos-holochain-client")
.args(["--url=http://localhost/holochain-api/", "hosting-criteria"])
.output()?;

let output_str = String::from_utf8_lossy(&output.stdout).to_string();

let hosting_criteria: HostingCriteria = serde_json::from_str(&output_str)?;

Ok(hosting_criteria.jurisdiction)
}

pub async fn update_jurisdiction_if_changed(
config: &Config,
hbs_jurisdiction: String,
) -> Result<()> {
let mut agent = HHAAgent::spawn(Some(config)).await?;

let host_pubkey = agent.pubkey().await?;

let hha_jurisdiction: Option<String> = agent
.app
.zome_call_typed(
CoreAppRoleName::HHA.into(),
ZomeName::from("hha"),
FunctionName::from("get_host_jurisdiction"),
host_pubkey.clone(),
)
.await?;

if hha_jurisdiction.is_none() || hha_jurisdiction.as_ref() != Some(&hbs_jurisdiction) {
#[derive(Debug, Serialize)]
pub struct SetHostJurisdictionInput {
pub pubkey: AgentPubKey,
pub jurisdiction: String,
}

let _: () = agent
.app
.zome_call_typed(
CoreAppRoleName::HHA.into(),
ZomeName::from("hha"),
FunctionName::from("set_host_jurisdiction"),
SetHostJurisdictionInput {
pubkey: host_pubkey,
jurisdiction: hbs_jurisdiction,
},
)
.await?;
}

Ok(())
}
10 changes: 5 additions & 5 deletions crates/configure-holochain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use hpos_hc_connect::{
use hpos_hc_connect::{hpos_agent::Agent, hpos_membrane_proof};
use std::sync::Arc;
use tracing::{debug, info, instrument, warn};
pub mod hpos_holochain_api;
pub mod jurisdictions;
mod utils;

#[instrument(err, skip(config))]
Expand Down Expand Up @@ -64,12 +64,12 @@ pub async fn install_happs(happ_file: &HappsFile, config: &Config) -> Result<()>
.await?;

if let Err(err) = admin_websocket
.install_and_activate_happ(happ, mem_proof_vec, agent.clone())
.install_and_activate_app(happ, mem_proof_vec, agent.clone())
.await
{
if err.to_string().contains("AppAlreadyInstalled") {
info!("app {} was previously installed, re-activating", &happ.id());
admin_websocket.activate_happ(happ).await?;
admin_websocket.activate_app(happ).await?;
} else {
return Err(err);
}
Expand Down Expand Up @@ -128,13 +128,13 @@ pub async fn update_host_jurisdiction_if_changed(config: &Config) -> Result<()>
}

// get current jurisdiction in hbs
let hbs_jurisdiction = match hpos_holochain_api::get_jurisdiction().await {
let hbs_jurisdiction = match jurisdictions::get_jurisdiction().await {
Ok(hbs_jurisdiction) => hbs_jurisdiction,
Err(e) => {
debug!("Failed to get jurisdiction from hbs {}", e);
return Ok(());
}
};

holo_happ_manager::update_jurisdiction_if_changed(config, hbs_jurisdiction).await
jurisdictions::update_jurisdiction_if_changed(config, hbs_jurisdiction).await
}
2 changes: 1 addition & 1 deletion crates/core_app_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ holochain_types = { workspace = true }
hpos_hc_connect = { path = "../hpos_connect_hc" }
serde = { workspace = true }
structopt = "0.3.0"
rmp-serde = "1.1.1"
rmp-serde = { workspace = true }
tokio = { version = "1.11", features = [ "full" ] }
holofuel_types = { workspace = true }
31 changes: 16 additions & 15 deletions crates/core_app_cli/src/actions/enable_happ_for_host.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use anyhow::Result;
use holochain_types::prelude::ActionHashB64;
use holochain_types::prelude::{ExternIO, FunctionName, ZomeName};
use hpos_hc_connect::{hha_types::HappAndHost, CoreAppAgent, CoreAppRoleName};
use holochain_types::prelude::{FunctionName, ZomeName};
use hpos_hc_connect::app_connection::CoreAppRoleName;
use hpos_hc_connect::hha_agent::HHAAgent;
use hpos_hc_connect::hha_types::HappAndHost;

pub async fn get(happ_id: String, host_id: String) -> Result<()> {
let mut agent = CoreAppAgent::connect().await?;
let mut agent = HHAAgent::spawn(None).await?;

let holo_hash = ActionHashB64::from_b64_str(&happ_id.clone())
.expect("Failed to serialize string into ActionHashB4");
Expand All @@ -14,24 +16,23 @@ pub async fn get(happ_id: String, host_id: String) -> Result<()> {
holoport_id: host_id.clone(),
};

let result = agent
.zome_call(
CoreAppRoleName::HHA,
let _: () = agent
.app
.zome_call_typed(
CoreAppRoleName::HHA.into(),
ZomeName::from("hha"),
FunctionName::from("enable_happ"),
ExternIO::encode(payload)?,
payload,
)
.await;
.await?;

if result.is_ok() {
println!("===================");
println!("Enabled Happ ID {} for Host {}: ", happ_id, host_id);
println!("Fetching happ preference hash...");
println!("===================");
println!("Enabled Happ ID {} for Host {}: ", happ_id, host_id);
println!("Fetching happ preference hash...");

crate::get_happ_pref_for_host::get(happ_id, host_id).await?;
crate::get_happ_pref_for_host::get(happ_id, host_id).await?;

println!("===================");
}
println!("===================");

Ok(())
}
19 changes: 10 additions & 9 deletions crates/core_app_cli/src/actions/get_happ_hosts.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use anyhow::Result;
use holochain_types::prelude::{ExternIO, FunctionName, ZomeName};
use hpos_hc_connect::{hha_types::HoloportDetails, CoreAppAgent, CoreAppRoleName};
use holochain_types::prelude::{FunctionName, ZomeName};
use hpos_hc_connect::{
app_connection::CoreAppRoleName, hha_agent::HHAAgent, hha_types::HoloportDetails,
};

pub async fn get(happ_id: String) -> Result<()> {
let mut agent = CoreAppAgent::connect().await?;
let mut agent = HHAAgent::spawn(None).await?;

let result = agent
.zome_call(
CoreAppRoleName::HHA,
let hosts: Vec<HoloportDetails> = agent
.app
.zome_call_typed(
CoreAppRoleName::HHA.into(),
ZomeName::from("hha"),
FunctionName::from("get_hosts"),
ExternIO::encode(happ_id.clone())?,
happ_id.clone(),
)
.await?;

let hosts: Vec<HoloportDetails> = rmp_serde::from_slice(result.as_bytes())?;

println!("===================");
println!("All Hosts for Happ ID {} are: ", happ_id);
println!("{:#?}", hosts);
Expand Down
19 changes: 10 additions & 9 deletions crates/core_app_cli/src/actions/get_happ_pref_for_host.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
// NB: This endpoint is used by the nightly tests. Any change to it's input or output should also be updated there.

use anyhow::Result;
use holochain_types::prelude::{ExternIO, FunctionName, ZomeName};
use hpos_hc_connect::{hha_types::HoloportDetails, CoreAppAgent, CoreAppRoleName};
use holochain_types::prelude::{FunctionName, ZomeName};
use hpos_hc_connect::{
app_connection::CoreAppRoleName, hha_agent::HHAAgent, hha_types::HoloportDetails,
};

pub async fn get(happ_id: String, host_id: String) -> Result<()> {
let mut agent = CoreAppAgent::connect().await?;
let mut agent = HHAAgent::spawn(None).await?;

let result = agent
.zome_call(
CoreAppRoleName::HHA,
let hosts: Vec<HoloportDetails> = agent
.app
.zome_call_typed(
CoreAppRoleName::HHA.into(),
ZomeName::from("hha"),
FunctionName::from("get_hosts"),
ExternIO::encode(happ_id.clone())?,
happ_id.clone(),
)
.await?;

let hosts: Vec<HoloportDetails> = rmp_serde::from_slice(result.as_bytes())?;

let found = hosts.into_iter().find(|h| h.holoport_id.0 == host_id);

if let Some(d) = found {
Expand Down
21 changes: 10 additions & 11 deletions crates/core_app_cli/src/actions/get_specific_happ_prefs.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
use anyhow::Result;
use holochain_types::prelude::{ActionHash, ActionHashB64, ExternIO, FunctionName, ZomeName};
use hpos_hc_connect::{hha_types::HappPreferences, CoreAppAgent, CoreAppRoleName};
use holochain_types::prelude::{ActionHash, ActionHashB64, FunctionName, ZomeName};
use hpos_hc_connect::{
app_connection::CoreAppRoleName, hha_agent::HHAAgent, hha_types::HappPreferences,
};

pub async fn get(pref_hash: String) -> Result<()> {
let mut agent = CoreAppAgent::connect().await?;
let mut agent = HHAAgent::spawn(None).await?;
let pref_holo_hash = ActionHashB64::from_b64_str(&pref_hash)
.expect("Failed to serialize string into ActionHashB4");
let hash = ActionHash::from(pref_holo_hash);

let result = agent
.zome_call(
CoreAppRoleName::HHA,
let prefs: HappPreferences = agent
.app
.zome_call_typed(
CoreAppRoleName::HHA.into(),
ZomeName::from("hha"),
FunctionName::from("get_specific_happ_preferences"),
ExternIO::encode(hash)?,
hash,
)
.await?;

println!("ZOME CALL RESULT: {:?}", result);

let prefs: HappPreferences = rmp_serde::from_slice(result.as_bytes())?;

println!("===================");
println!("All Hosts for Preference Hash {} are: ", pref_hash);
println!("{:#?}", prefs);
Expand Down
19 changes: 10 additions & 9 deletions crates/core_app_cli/src/actions/ledger.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use anyhow::Result;
use holochain_types::prelude::{ExternIO, FunctionName, ZomeName};
use holochain_types::prelude::{FunctionName, ZomeName};
use hpos_hc_connect::app_connection::CoreAppRoleName;
use hpos_hc_connect::hha_agent::HHAAgent;
use hpos_hc_connect::holofuel_types::Ledger;
use hpos_hc_connect::{CoreAppAgent, CoreAppRoleName};

pub async fn get() -> Result<()> {
let mut agent = CoreAppAgent::connect().await?;
let result = agent
.zome_call(
CoreAppRoleName::Holofuel,
let mut agent = HHAAgent::spawn(None).await?;

let ledger: Ledger = agent
.app
.zome_call_typed(
CoreAppRoleName::Holofuel.into(),
ZomeName::from("transactor"),
FunctionName::from("get_ledger"),
ExternIO::encode(())?,
(),
)
.await?;

let ledger: Ledger = rmp_serde::from_slice(result.as_bytes())?;

println!("===================");
println!("Your Ledger is: ");
println!("Balance: {:?}", ledger.balance);
Expand Down
20 changes: 11 additions & 9 deletions crates/core_app_cli/src/actions/list_all_my_happs.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use anyhow::Result;
use holochain_types::prelude::{ExternIO, FunctionName, ZomeName};
use hpos_hc_connect::{hha_types::PresentedHappBundle, CoreAppAgent, CoreAppRoleName};
use holochain_types::prelude::{FunctionName, ZomeName};
use hpos_hc_connect::{
app_connection::CoreAppRoleName, hha_agent::HHAAgent, hha_types::PresentedHappBundle,
};

pub async fn get() -> Result<()> {
let mut agent = CoreAppAgent::connect().await?;
let result = agent
.zome_call(
CoreAppRoleName::HHA,
let mut agent = HHAAgent::spawn(None).await?;

let happs: Vec<PresentedHappBundle> = agent
.app
.zome_call_typed(
CoreAppRoleName::HHA.into(),
ZomeName::from("hha"),
FunctionName::from("get_my_happs"),
ExternIO::encode(())?,
(),
)
.await?;

let happs: Vec<PresentedHappBundle> = rmp_serde::from_slice(result.as_bytes())?;

println!("===================");
println!("Your Published Happs is: ");
println!("{:?}", happs);
Expand Down
Loading

0 comments on commit 18255aa

Please sign in to comment.