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

Uninstall paid happs when kyc level is not 2 #29

Merged
merged 5 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 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 @@ -9,6 +9,7 @@ again = "0.1"
anyhow = "1.0"
isahc = "1.7.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_yaml = "0.8"
structopt = "0.3"
tempfile = "3.1"
Expand Down
14 changes: 14 additions & 0 deletions src/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ use std::fs::File;
use std::{collections::HashMap, env};
use tracing::trace;

#[derive(Deserialize, Debug, Clone)]
pub struct PublisherPricingPref {
pub cpu: Fuel,
pub storage: Fuel,
pub bandwidth: Fuel,
}
impl PublisherPricingPref {
pub fn is_free(&self) -> bool {
let zero_fuel = Fuel::new(0);
self.cpu == zero_fuel && self.storage == zero_fuel && self.bandwidth == zero_fuel
}
}

#[derive(Deserialize, Debug, Clone)]
pub struct DnaResource {
pub hash: String, // hash of the dna, not a stored dht address
Expand All @@ -26,6 +39,7 @@ pub struct PresentedHappBundle {
pub bundle_url: String,
pub name: String,
pub special_installed_app_id: Option<String>,
pub publisher_pricing_pref: PublisherPricingPref,
}

#[derive(Serialize, Debug, Clone)]
Expand Down
4 changes: 4 additions & 0 deletions src/get_apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ use holochain_types::prelude::{Nonce256Bits, Timestamp, ZomeCallUnsigned};
use std::time::Duration;
use tracing::trace;

use self::entries::PublisherPricingPref;

pub struct HappBundle {
pub happ_id: ActionHashB64,
pub bundle_url: String,
pub is_paused: bool,
pub special_installed_app_id: Option<String>,
pub publisher_pricing_pref: PublisherPricingPref,
}

pub async fn get_all_enabled_hosted_happs(
Expand Down Expand Up @@ -87,6 +90,7 @@ pub async fn get_all_enabled_hosted_happs(
bundle_url: happ.bundle_url,
is_paused: happ.is_paused,
special_installed_app_id: happ.special_installed_app_id,
publisher_pricing_pref: happ.publisher_pricing_pref,
}
})
.collect();
Expand Down
37 changes: 37 additions & 0 deletions src/get_kyc_level.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pub use crate::config;
pub use crate::entries;
pub use crate::get_apps;
pub use crate::AdminWebsocket;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::process::{Command, Output};

#[derive(Debug, Deserialize)]
struct HostingCriteria {
#[allow(dead_code)]
id: String,
#[allow(dead_code)]
jurisdiction: String,
kyc: KycLevel,
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub enum KycLevel {
#[serde(rename = "holo_kyc_1")]
Level1,
#[serde(rename = "holo_kyc_2")]
Level2,
#[serde(rename = "error")]
Error,
}

pub async fn get_kyc_level() -> Result<KycLevel> {
let output: Output = Command::new("/run/current-system/sw/bin/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.kyc)
}
6 changes: 6 additions & 0 deletions src/install_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use url::Url;
pub async fn install_holo_hosted_happs(
happs: &[get_apps::HappBundle],
config: &config::Config,
is_kyc_level_2: bool,
) -> Result<()> {
info!("Starting to install....");

Expand Down Expand Up @@ -61,6 +62,7 @@ pub async fn install_holo_hosted_happs(
bundle_url,
is_paused,
special_installed_app_id,
publisher_pricing_pref,
} in happs
{
// if special happ is installed and do nothing if it is installed
Expand All @@ -85,6 +87,10 @@ pub async fn install_holo_hosted_happs(
admin_websocket.deactivate_app(&happ_id.to_string()).await?;
}
}
// if kyc_level is not 2 and the happ is not free, we don't instal
else if !is_kyc_level_2 && !publisher_pricing_pref.is_free() {
trace!("Skipping non-free happ due to kyc level {}", happ_id);
}
// else installed the hosted happ read-only instance
else {
trace!("Load mem-proofs for {}", happ_id);
Expand Down
11 changes: 8 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ use get_apps::get_all_enabled_hosted_happs;
mod install_app;
use install_app::install_holo_hosted_happs;
mod uninstall_apps;
use tracing::info;
use tracing::{debug, info};
use uninstall_apps::uninstall_removed_happs;
mod get_kyc_level;
use get_kyc_level::{get_kyc_level, KycLevel};

/// gets all the enabled happs from HHA
/// installs new happs that were enabled or registered by its provider
/// and uninstalles old happs that were disabled or deleted by its provider
pub async fn run(core_happ: &config::Happ, config: &config::Config) -> Result<()> {
info!("Activating holo hosted apps");
let kyc_level = get_kyc_level().await?;
debug!("Got kyc level {:?}", &kyc_level);
let is_kyc_level_2 = kyc_level == KycLevel::Level2;
let list_of_happs = get_all_enabled_hosted_happs(core_happ, config).await?;
install_holo_hosted_happs(&list_of_happs, config).await?;
uninstall_removed_happs(&list_of_happs, config).await?;
install_holo_hosted_happs(&list_of_happs, config, is_kyc_level_2).await?;
uninstall_removed_happs(&list_of_happs, config, is_kyc_level_2).await?;
Ok(())
}
14 changes: 11 additions & 3 deletions src/uninstall_apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tracing::warn;
pub async fn uninstall_removed_happs(
happs: &[get_apps::HappBundle],
config: &config::Config,
is_kyc_level_2: bool,
) -> Result<()> {
info!("Checking to uninstall happs that were removed from the hosted list....");

Expand All @@ -28,9 +29,16 @@ pub async fn uninstall_removed_happs(
let happ_ids_to_uninstall = ano_happ
.into_iter()
.filter(|h| {
!happs
.iter()
.any(|get_apps::HappBundle { happ_id, .. }| &happ_id.to_string() == h)
!happs.iter().any(
|get_apps::HappBundle {
happ_id,
publisher_pricing_pref,
..
}| {
&happ_id.to_string() == h
|| !is_kyc_level_2 && !publisher_pricing_pref.is_free()
},
)
})
.collect();

Expand Down