Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update/clean up #49

Merged
merged 12 commits into from
Jul 10, 2024
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ holo auto installer does 2 main things.
- Install a happs that are supposed to be installed on the holoport but are not installed
`install_holo_hosted_happs`
- Uninstall happs that are not supposed to be installed on the holoport but are installed
`uninstall_ineligible_happs`
`handle_ineligible_happs`

Generally if you want to restrict something so it is not installed on the holoport you can use the function inside
`uninstall_apps.rs` called `should_be_installed` If this returns a `false` the happ will be uninstalled form the holoport.
Expand Down
80 changes: 0 additions & 80 deletions src/entries.rs

This file was deleted.

113 changes: 75 additions & 38 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,81 +1,118 @@
// TODO: https://github.com/tokio-rs/tracing/issues/843
#![allow(clippy::unit_arg)]
pub mod entries;
use std::collections::HashMap;
pub mod types;
mod utils;

use anyhow::Result;
use holochain_types::dna::{hash_type::Agent, HoloHash};
use hpos_hc_connect::hha_agent::HHAAgent;
use hpos_hc_connect::holo_config::Config;
pub use hpos_hc_connect::AdminWebsocket;
pub mod transaction_types;
mod utils;
use tracing::{debug, error, info};
use std::collections::HashMap;
use tracing::{debug, error, info, warn};
use types::hbs::{HbsClient, KycLevel};
use types::PublishedHappDetails;
use utils::core_app::{
get_happ_preferences, get_host_preferences, get_pending_transactions,
get_publisher_jurisdiction,
};
use utils::{
get_all_published_hosted_happs, get_happ_preferences, get_hosting_preferences,
get_pending_transactions, get_publisher_jurisdiction, install_holo_hosted_happs,
suspend_unpaid_happs, uninstall_ineligible_happs,
get_all_published_hosted_happs, handle_ineligible_happs, install_holo_hosted_happs,
suspend_unpaid_happs,
};
mod hbs;
use hbs::{HbsClient, KycLevel};
use hpos_hc_connect::holo_config::Config;

/// gets all the enabled happs from HHA
/// installs and enables new happs that were registered by a provider and holochain disables those paused by provider in hha
/// then uninstalls happs that are ineligible for host (eg: holo-disabled, unallowed pricing for kyc level)
/// 1. Gets all the holo-enabled happs from HHA
/// 2. Suspends happs with overdue payments
/// 3. Installs and enables (enables in holochain and holo) all new happs that were registered by a provider and holochain-disables those paused by provider in hha
/// 4. Uninstalls happs that are ineligible for host (eg: holo-disabled, unallowed pricing for kyc level, incongruent price settings with publisher/happ)
pub async fn run(config: &Config) -> Result<()> {
info!("Activating holo hosted apps");
let hbs_connect = HbsClient::connect()?;
let hosting_criteria = match hbs_connect.get_hosting_criteria().await {
let host_credentials = match hbs_connect.get_host_hosting_criteria().await {
Some(v) => v,
None => {
error!("Unable to get hosting criteria from HBS. Exiting...");
return Err(anyhow::anyhow!("Unable to get hosting criteria"));
}
};
let kyc_level = hosting_criteria.kyc;
debug!("Got kyc level {:?}", &kyc_level);
let jurisdiction = hosting_criteria.jurisdiction;
debug!("Got jurisdiction from hbs {:?}", jurisdiction);

let is_kyc_level_2 = kyc_level == KycLevel::Level2;
debug!("Got host credentials from hbs {:?}", host_credentials);

let mut core_app = HHAAgent::spawn(Some(config)).await?;

// suspend happs that have overdue payments
// Suspend happs that have overdue payments
let pending_transactions = get_pending_transactions(&mut core_app).await?;
let suspended_happs = suspend_unpaid_happs(&mut core_app, pending_transactions).await?;
let hosting_preference = get_hosting_preferences(&mut core_app).await?;

let list_of_happs = get_all_published_hosted_happs(&mut core_app).await?;
let published_happs = get_all_published_hosted_happs(&mut core_app).await?;

// Get happ jurisdictions AND publisher jurisdiction for each happ
let mut published_happ_details: HashMap<String, PublishedHappDetails> = HashMap::new();
let mut publisher_jurisdictions: HashMap<HoloHash<Agent>, Option<String>> = HashMap::new();
let mut happ_jurisdictions: HashMap<String, Option<String>> = HashMap::new();
// get publisher jurisdiction for each happ
for happ in list_of_happs.iter() {

for happ in published_happs.iter() {
let happ_prefs = get_happ_preferences(&mut core_app, happ.happ_id.clone()).await?;
let publisher_pubkey = happ_prefs.provider_pubkey;

// If already have publisher pubkey stored in `publisher_jurisdictions` map, then grab the jurisdiction value and set value in `published_happ_details` map
// otherwise, make a call to hha to fetch the publisher jurisdiction and set in both the `published_happ_details` map and `publisher_jurisdictions` map
match publisher_jurisdictions.get(&publisher_pubkey) {
Some(jurisdiction) => {
happ_jurisdictions
.insert(happ.happ_id.clone().to_string(), (*jurisdiction).clone());
published_happ_details.insert(
happ.happ_id.clone().to_string(),
PublishedHappDetails {
publisher_jurisdiction: (*jurisdiction).clone(),
happ_jurisdictions: happ.jurisdictions.clone(),
should_exclude_happ_jurisdictions: happ.exclude_jurisdictions,
happ_categories: happ.categories.clone(),
is_disabled_by_host: happ.is_host_disabled,
},
);
}
None => {
let jurisdiction =
get_publisher_jurisdiction(&mut core_app, publisher_pubkey.clone()).await?;
publisher_jurisdictions.insert(publisher_pubkey, jurisdiction.clone());
happ_jurisdictions.insert(happ.happ_id.clone().to_string(), jurisdiction);
published_happ_details.insert(
happ.happ_id.clone().to_string(),
PublishedHappDetails {
publisher_jurisdiction: jurisdiction,
happ_jurisdictions: happ.jurisdictions.clone(),
should_exclude_happ_jurisdictions: happ.exclude_jurisdictions,
happ_categories: happ.categories.clone(),
is_disabled_by_host: happ.is_host_disabled,
},
);
}
}
}

install_holo_hosted_happs(config, &list_of_happs, is_kyc_level_2).await?;
uninstall_ineligible_happs(
config,
&list_of_happs,
is_kyc_level_2,
let host_happ_preferences = get_host_preferences(&mut core_app).await?;

let is_host_kyc_level_2 = match host_credentials.clone().kyc {
Some(kyc) => kyc == KycLevel::Level2,
None => {
// If no host kyc is found, default to hosting being ineligible
// NB: Hosting is only valid (despite price prefs) if the host is >= kyc level 2
warn!("No host kyc found. Defaulting to KYC 1.");
false
}
};

install_holo_hosted_happs(
&mut core_app,
config.admin_port,
&published_happs,
is_host_kyc_level_2,
)
.await?;

handle_ineligible_happs(
&mut core_app,
config.admin_port,
suspended_happs,
jurisdiction,
hosting_preference,
happ_jurisdictions,
host_credentials,
host_happ_preferences,
published_happ_details,
)
.await?;
Ok(())
Expand Down
Loading
Loading