Skip to content

Commit

Permalink
feature: add core_app zome calls (#191)
Browse files Browse the repository at this point in the history
### Updates:
- Adds the zome calls in response to the suggestion from the
[holo-auto-installer pr
#49](Holo-Host/holo-auto-installer#49)
  • Loading branch information
JettTech authored Jul 10, 2024
1 parent 4684411 commit f922ed2
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 4 deletions.
97 changes: 95 additions & 2 deletions crates/hpos_connect_hc/src/hha_agent.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use std::sync::Arc;

use crate::app_connection::CoreAppRoleName;
use crate::hha_types::{HappInput, HoloportDetails, PresentedHappBundle};
use crate::hha_types::{
HappAndHost, HappInput, HappPreferences, HoloportDetails, PresentedHappBundle,
ServiceloggerHappPreferences,
};
use crate::holo_config::{default_password, get_lair_url, Config, HappsFile, ADMIN_PORT};
use crate::holofuel_types::PendingTransaction;
use crate::{AdminWebsocket, AppConnection};
use anyhow::{anyhow, Context, Result};
use holochain_keystore::AgentPubKeyExt;
use holochain_types::dna::{ActionHashB64, AgentPubKey};
use holochain_types::prelude::{FunctionName, Signature, ZomeName};
use holochain_types::prelude::{ExternIO, FunctionName, Signature, ZomeName};

// NOTE: This should really be renamed CORE_APP_AGENT, as it related to the core app and therfore connects to BOTH hha and hf
/// Struct giving access to local instance of HHA on HPOS
/// `config` of type `holo_config::Config` represents CLI params and can be passed
/// to describe local running environment
Expand Down Expand Up @@ -43,6 +48,7 @@ impl HHAAgent {
Ok(Self { app })
}

// CORE_APP/HHA ZOME CALLS:
pub async fn pubkey(&self) -> Result<AgentPubKey> {
Ok(self
.app
Expand Down Expand Up @@ -97,6 +103,93 @@ impl HHAAgent {
.await
}

pub async fn get_host_preferences(&mut self) -> Result<HappPreferences> {
self.app
.zome_call_typed(
CoreAppRoleName::HHA.into(),
ZomeName::from("hha"),
FunctionName::from("get_default_happ_preferences"),
(),
)
.await
}

pub async fn get_happ_preferences(
&mut self,
happ_id: ActionHashB64,
) -> Result<ServiceloggerHappPreferences> {
self.app
.zome_call_typed(
CoreAppRoleName::HHA.into(),
ZomeName::from("hha"),
FunctionName::from("get_happ_preferences"),
happ_id,
)
.await
}

pub async fn get_publisher_jurisdiction(
&mut self,
pubkey: AgentPubKey,
) -> Result<Option<String>> {
self.app
.zome_call_typed(
CoreAppRoleName::HHA.into(),
ZomeName::from("hha"),
FunctionName::from("get_publisher_jurisdiction"),
pubkey,
)
.await
}

pub async fn holo_enable_happ(
&mut self,
happ_id: &ActionHashB64,
holoport_id: &String,
) -> Result<()> {
self.app
.zome_call_typed(
CoreAppRoleName::HHA.into(),
ZomeName::from("hha"),
FunctionName::from("enable_happ"),
ExternIO::encode(HappAndHost {
happ_id: happ_id.to_owned(),
holoport_id: holoport_id.to_owned(),
})?,
)
.await
}

pub async fn holo_disable_happ(
&mut self,
happ_id: &ActionHashB64,
holoport_id: &String,
) -> Result<()> {
self.app
.zome_call_typed(
CoreAppRoleName::HHA.into(),
ZomeName::from("hha"),
FunctionName::from("disable_happ"),
ExternIO::encode(HappAndHost {
happ_id: happ_id.to_owned(),
holoport_id: holoport_id.to_owned(),
})?,
)
.await
}

// CORE_APP/HF ZOME CALLS:
pub async fn get_pending_transactions(&mut self) -> Result<PendingTransaction> {
self.app
.zome_call_typed(
CoreAppRoleName::Holofuel.into(),
ZomeName::from("transactor"),
FunctionName::from("get_pending_transactions"),
(),
)
.await
}

/// Sign byte payload with holofuel agent's private key
/// Currently it is commented out, because I do not know what agent key shall i use
pub async fn sign_raw(&mut self, data: Arc<[u8]>) -> Result<Signature> {
Expand Down
36 changes: 35 additions & 1 deletion crates/hpos_connect_hc/src/hha_types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use holochain_types::prelude::{
holochain_serial, ActionHashB64, AgentPubKeyB64, SerializedBytes, Timestamp,
holochain_serial, ActionHashB64, AgentPubKey, AgentPubKeyB64, SerializedBytes, Timestamp,
};
use holofuel_types::fuel::Fuel;
use serde::{Deserialize, Serialize};
Expand All @@ -11,6 +11,12 @@ pub struct HappAndHost {
pub holoport_id: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ExclusivePreferences {
pub value: Vec<String>,
pub is_exclusion: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct HappPreferences {
pub timestamp: Timestamp,
Expand All @@ -19,6 +25,9 @@ pub struct HappPreferences {
pub price_storage: Fuel,
pub price_bandwidth: Fuel,
pub max_time_before_invoice: Duration,
pub invoice_due_in_days: u8, // how many days after an invoice is created it it due
pub jurisdiction_prefs: Option<ExclusivePreferences>,
pub categories_prefs: Option<ExclusivePreferences>,
}

impl Default for HappPreferences {
Expand All @@ -30,10 +39,26 @@ impl Default for HappPreferences {
price_storage: Fuel::new(0),
price_bandwidth: Fuel::new(0),
max_time_before_invoice: Duration::default(),
invoice_due_in_days: 7,
jurisdiction_prefs: None,
categories_prefs: None,
}
}
}

// NB: This struct is the same as the HappPreferences struct with the addition of the provider pubkey AND
// removal of the timestamp, jurisdiction_prefs and categories_prefs fields
#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct ServiceloggerHappPreferences {
pub provider_pubkey: AgentPubKey,
pub max_fuel_before_invoice: Fuel,
pub price_compute: Fuel,
pub price_storage: Fuel,
pub price_bandwidth: Fuel,
pub max_time_before_invoice: Duration,
pub invoice_due_in_days: u8,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SetHappPreferencesInput {
pub happ_id: ActionHashB64,
Expand All @@ -44,6 +69,13 @@ pub struct SetHappPreferencesInput {
pub max_time_before_invoice: Duration, // how much time to allow to pass before sending invoice even if fuel trigger not reached.
}

#[derive(Debug, Serialize, Deserialize, SerializedBytes, Clone)]
pub struct HostSettings {
is_enabled: bool,
pub is_host_disabled: bool,
is_auto_disabled: bool,
}

#[derive(Debug, Clone, Deserialize)]
pub struct HoloportId(pub String);

Expand Down Expand Up @@ -75,6 +107,8 @@ pub struct PresentedHappBundle {
pub publisher_pricing_pref: PublisherPricingPref,
pub login_config: LoginConfig,
pub special_installed_app_id: Option<String>,
pub host_settings: HostSettings,
pub last_edited: Timestamp,
}

#[derive(Debug, Serialize, Deserialize, SerializedBytes, Clone, PartialEq, Eq)]
Expand Down
18 changes: 17 additions & 1 deletion crates/hpos_connect_hc/src/holofuel_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ use serde::Serialize;
use std::time::Duration;
use tracing::debug;

#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct PendingTransaction {
pub invoice_pending: Vec<Transaction>,
pub promise_pending: Vec<Transaction>,
pub invoice_declined: Vec<Transaction>,
pub promise_declined: Vec<Transaction>,
pub accepted: Vec<Transaction>,
}

#[derive(Serialize, Deserialize, Debug, SerializedBytes)]
pub struct Ledger {
pub balance: String,
Expand All @@ -30,6 +39,13 @@ pub struct Actionable {
pub promise_actionable: Vec<Transaction>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, SerializedBytes)]
#[serde(rename_all = "snake_case")]
pub enum POS {
Hosting(CapSecret),
Redemption(String), // Contains wallet address
}

#[derive(Serialize, Deserialize, Debug, SerializedBytes)]
pub struct Transaction {
pub id: EntryHashB64,
Expand All @@ -42,7 +58,7 @@ pub struct Transaction {
pub direction: TransactionDirection,
pub status: TransactionStatus,
pub note: Option<String>,
pub proof_of_service_token: Option<CapSecret>,
pub proof_of_service: Option<POS>,
pub url: Option<String>,
pub expiration_date: Option<Timestamp>,
}
Expand Down

0 comments on commit f922ed2

Please sign in to comment.