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

Fix/Avoid duplicate app interface creation + clean-up #192

Merged
merged 7 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 4 additions & 2 deletions crates/configure-holochain/src/jurisdictions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use holochain_types::{
dna::AgentPubKey,
prelude::{FunctionName, ZomeName},
};
use hpos_hc_connect::{app_connection::CoreAppRoleName, hha_agent::HHAAgent, holo_config::Config};
use hpos_hc_connect::{
app_connection::CoreAppRoleName, hha_agent::CoreAppAgent, holo_config::Config,
};
use serde::{Deserialize, Serialize};
use std::process::{Command, Output};

Expand Down Expand Up @@ -31,7 +33,7 @@ pub async fn update_jurisdiction_if_changed(
config: &Config,
hbs_jurisdiction: String,
) -> Result<()> {
let mut agent = HHAAgent::spawn(Some(config)).await?;
let mut agent = CoreAppAgent::spawn(Some(config)).await?;

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

Expand Down
20 changes: 13 additions & 7 deletions crates/configure-holochain/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ use test_case::test_case;
/// Tests cannot run in parallel because they are all accessing same /tmp dir

async fn run_configure_holochain(f_r_a_k: &str, r_o_m_p: &str) {
// Uncomment those lines if you need logging
// but this will work only for one test case ran at the time
// Uncomment these lines if you need logging
// but this will work only for one test case run at the time
// because tracing subscribes here sets a global subscriber for each test
// use tracing_subscriber::EnvFilter;
// let filter = EnvFilter::from_default_env().add_directive("again=trace".parse().unwrap());
Expand Down Expand Up @@ -84,7 +84,7 @@ async fn run_configure_holochain(f_r_a_k: &str, r_o_m_p: &str) {
// Holoports do not force read-only memproof
set_var("READ_ONLY_MEM_PROOF", r_o_m_p);

// devNet HBS server url, because given hpos-config is registered in devNet database
// Use devNet HBS server url, because given hpos-config is registered in devNet database
set_var(
"MEM_PROOF_SERVER_URL",
"https://membrane-proof.dev.holotest.net",
Expand All @@ -100,7 +100,7 @@ async fn run_configure_holochain(f_r_a_k: &str, r_o_m_p: &str) {
};

// spin up lair
println!("Starting lair-keystore");
println!("\nStarting lair-keystore");
let (_lair, lair_config, _) =
holochain_env_setup::lair::spawn(&tmp_dir, &log_dir, Some(&device_bundle), None)
.await
Expand All @@ -125,17 +125,23 @@ async fn run_configure_holochain(f_r_a_k: &str, r_o_m_p: &str) {
set_var("IS_INTEGRATION_TEST", "TRUE");

println!("Run configure holochain script");
configure_holochain::run(config.clone()).await.unwrap();
configure_holochain::run(config.clone())
.await
.expect("Failed when running configure holochain script the first time");

// Second run should not error out
configure_holochain::run(config.clone()).await.unwrap();
configure_holochain::run(config.clone())
.await
.expect("Failed when running configure holochain script the second time");

// Delete memproof which is an equivalent of changing DEV_UID_OVERRIDE for holoport
// which was creating a bug https://github.com/Holo-Host/hpos-configure-holochain/issues/136
delete_mem_proof_file().unwrap();

// Third run should not error out
configure_holochain::run(config).await.unwrap();
configure_holochain::run(config)
.await
.expect("Failed when running configure holochain script the third time");

let mut connection = configure_holochain::AdminWebsocket::connect(4444)
.await
Expand Down
4 changes: 2 additions & 2 deletions crates/core_app_cli/src/actions/enable_happ_for_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use anyhow::Result;
use holochain_types::prelude::ActionHashB64;
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_agent::CoreAppAgent;
use hpos_hc_connect::hha_types::HappAndHost;

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

let holo_hash = ActionHashB64::from_b64_str(&happ_id.clone())
.expect("Failed to serialize string into ActionHashB4");
Expand Down
4 changes: 2 additions & 2 deletions crates/core_app_cli/src/actions/get_happ_hosts.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use anyhow::Result;
use holochain_types::prelude::{FunctionName, ZomeName};
use hpos_hc_connect::{
app_connection::CoreAppRoleName, hha_agent::HHAAgent, hha_types::HoloportDetails,
app_connection::CoreAppRoleName, hha_agent::CoreAppAgent, hha_types::HoloportDetails,
};

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

let hosts: Vec<HoloportDetails> = agent
.app
Expand Down
4 changes: 2 additions & 2 deletions crates/core_app_cli/src/actions/get_happ_pref_for_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use anyhow::Result;
use holochain_types::prelude::{FunctionName, ZomeName};
use hpos_hc_connect::{
app_connection::CoreAppRoleName, hha_agent::HHAAgent, hha_types::HoloportDetails,
app_connection::CoreAppRoleName, hha_agent::CoreAppAgent, hha_types::HoloportDetails,
};

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

let hosts: Vec<HoloportDetails> = agent
.app
Expand Down
4 changes: 2 additions & 2 deletions crates/core_app_cli/src/actions/get_specific_happ_prefs.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use anyhow::Result;
use holochain_types::prelude::{ActionHash, ActionHashB64, FunctionName, ZomeName};
use hpos_hc_connect::{
app_connection::CoreAppRoleName, hha_agent::HHAAgent, hha_types::HappPreferences,
app_connection::CoreAppRoleName, hha_agent::CoreAppAgent, hha_types::HappPreferences,
};

pub async fn get(pref_hash: String) -> Result<()> {
let mut agent = HHAAgent::spawn(None).await?;
let mut agent = CoreAppAgent::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);
Expand Down
4 changes: 2 additions & 2 deletions crates/core_app_cli/src/actions/ledger.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use anyhow::Result;
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_agent::CoreAppAgent;
use hpos_hc_connect::holofuel_types::Ledger;

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

let ledger: Ledger = agent
.app
Expand Down
4 changes: 2 additions & 2 deletions crates/core_app_cli/src/actions/list_all_my_happs.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use anyhow::Result;
use holochain_types::prelude::{FunctionName, ZomeName};
use hpos_hc_connect::{
app_connection::CoreAppRoleName, hha_agent::HHAAgent, hha_types::PresentedHappBundle,
app_connection::CoreAppRoleName, hha_agent::CoreAppAgent, hha_types::PresentedHappBundle,
};

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

let happs: Vec<PresentedHappBundle> = agent
.app
Expand Down
4 changes: 2 additions & 2 deletions crates/core_app_cli/src/actions/list_all_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use anyhow::Result;
use holochain_types::prelude::{FunctionName, ZomeName};
use hpos_hc_connect::{
app_connection::CoreAppRoleName,
hha_agent::HHAAgent,
hha_agent::CoreAppAgent,
holofuel_types::{Actionable, Pending, Transaction},
};

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

let txs: Pending = agent
.app
Expand Down
4 changes: 2 additions & 2 deletions crates/core_app_cli/src/actions/pay_invoices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use holochain_types::prelude::{
holochain_serial, EntryHashB64, FunctionName, SerializedBytes, ZomeName,
};
use hpos_hc_connect::app_connection::CoreAppRoleName;
use hpos_hc_connect::hha_agent::HHAAgent;
use hpos_hc_connect::hha_agent::CoreAppAgent;
use hpos_hc_connect::holofuel_types::Pending;
use serde::{Deserialize, Serialize};

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

let txs: Pending = agent
.app
Expand Down
4 changes: 2 additions & 2 deletions crates/core_app_cli/src/actions/profile.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use anyhow::Result;
use holochain_types::prelude::{FunctionName, ZomeName};
use hpos_hc_connect::{
app_connection::CoreAppRoleName, hha_agent::HHAAgent, holofuel_types::Profile,
app_connection::CoreAppRoleName, hha_agent::CoreAppAgent, holofuel_types::Profile,
};

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

let profile: Profile = agent
.app
Expand Down
4 changes: 2 additions & 2 deletions crates/core_app_cli/src/actions/set_happ_prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use holochain_types::prelude::{ActionHashB64, FunctionName, ZomeName};
use holofuel_types::fuel::Fuel;
use hpos_hc_connect::{
app_connection::CoreAppRoleName,
hha_agent::HHAAgent,
hha_agent::CoreAppAgent,
hha_types::{HappPreferences, SetHappPreferencesInput},
};
use std::{str::FromStr, time::Duration};
Expand All @@ -17,7 +17,7 @@ pub async fn get(
max_time_before_invoice_sec: String,
max_time_before_invoice_ms: String,
) -> Result<()> {
let mut agent = HHAAgent::spawn(None).await?;
let mut agent = CoreAppAgent::spawn(None).await?;

let max_time_sec = max_time_before_invoice_sec
.parse::<u64>()
Expand Down
4 changes: 2 additions & 2 deletions crates/holo_happ_manager/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{env, fs};

use anyhow::{Context, Result};
use hpos_hc_connect::hha_agent::HHAAgent;
use hpos_hc_connect::hha_agent::CoreAppAgent;
pub use hpos_hc_connect::{
hha_types::HappInput,
holo_config::{Config, Happ, HappsFile},
Expand All @@ -11,7 +11,7 @@ use tracing::{debug, info};
pub async fn run(config: &Config) -> Result<()> {
info!("Running happ manager");

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

let apps = happ_to_be_published()?;

Expand Down
4 changes: 2 additions & 2 deletions crates/holo_happ_manager/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use holo_happ_manager;
use hpos_hc_connect::hha_agent::HHAAgent;
use hpos_hc_connect::hha_agent::CoreAppAgent;

#[tokio::test]
async fn run_happ_manager() {
Expand Down Expand Up @@ -83,7 +83,7 @@ async fn run_happ_manager() {
println!("Run holo happ manager script");
holo_happ_manager::run(&config).await.unwrap();

let mut hha = HHAAgent::spawn(Some(&config)).await.unwrap();
let mut hha = CoreAppAgent::spawn(Some(&config)).await.unwrap();

let published_happ = hha.get_my_happs().await.unwrap();

Expand Down
10 changes: 9 additions & 1 deletion crates/hpos_connect_hc/src/admin_ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::hpos_membrane_proof::MembraneProofs;
use anyhow::{anyhow, Context, Result};
use holochain_conductor_api::{
AdminRequest, AdminResponse, AppAuthenticationToken, AppAuthenticationTokenIssued, AppInfo,
AppStatusFilter, IssueAppAuthenticationTokenPayload,
AppInterfaceInfo, AppStatusFilter, IssueAppAuthenticationTokenPayload,
};
use holochain_types::{
app::{InstallAppPayload, InstalledAppId},
Expand Down Expand Up @@ -60,6 +60,14 @@ impl AdminWebsocket {
}
}

pub async fn list_app_interfaces(&mut self) -> Result<Vec<AppInterfaceInfo>> {
debug!("listing app interfaces");
match self.send(AdminRequest::ListAppInterfaces, None).await? {
AdminResponse::AppInterfacesListed(app_interfaces) => Ok(app_interfaces),
_ => Err(anyhow!("Failed to fetch list of attached app interfaces")),
}
}

pub async fn issue_app_auth_token(&mut self, app_id: String) -> Result<AppAuthenticationToken> {
debug!("issuing app authentication token for app {:?}", app_id);
let msg = AdminRequest::IssueAppAuthenticationToken(IssueAppAuthenticationTokenPayload {
Expand Down
33 changes: 28 additions & 5 deletions crates/hpos_connect_hc/src/app_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,59 @@ pub struct AppConnection {
}

impl AppConnection {
// Connect to app interface for given installed app id if one already exists,
// otherwise attach a new app interface and then establish connection.
pub async fn connect(
admin_ws: &mut AdminWebsocket,
keystore: MetaLairClient,
app_id: String,
) -> Result<Self> {
let app_port = admin_ws
.attach_app_interface(None)
let attached_app_interfaces = admin_ws
.list_app_interfaces()
.await
.context("failed to start app interface for core app")?;
.context("failed to start fetch app interfaces during app connection setup")?;

let token = admin_ws.issue_app_auth_token(app_id.clone()).await?;
let app_interface = attached_app_interfaces.into_iter().find(|a| {
a.installed_app_id.is_some() && a.installed_app_id.to_owned().unwrap() == app_id
});
println!(
"FOUND APP INTERFACE: >> ?? app_interface: {:?} ",
app_interface
);

let app_port = match app_interface {
Some(a) => a.port,
None => admin_ws
.attach_app_interface(None)
JettTech marked this conversation as resolved.
Show resolved Hide resolved
.await
.context("failed to start app interface for core app")?,
};

println!("USING APP PORT: >> ?? app_port: {} ", app_port);

let token = admin_ws.issue_app_auth_token(app_id.clone()).await?;
let websocket_config = Arc::new(WebsocketConfig::CLIENT_DEFAULT);
let socket_addr = format!("localhost:{app_port}");
let addr = socket_addr
.to_socket_addrs()?
.next()
.context("invalid websocket address")?;
let websocket_config = Arc::new(WebsocketConfig::CLIENT_DEFAULT);

let (tx, rx) = again::retry(|| {
let websocket_config = Arc::clone(&websocket_config);
connect(websocket_config, ConnectRequest::new(addr))
})
.await?;

let rx = WsPollRecv::new::<AppResponse>(rx).into();

// Websocket connection needs authentication via token previously obtained from Admin Interface
tx.authenticate(AppAuthenticationRequest { token })
.await
.map_err(|err| anyhow!("Failed to send authentication: {err:?}"))?;

println!("CONNECTED TO APP INTEFACE");

Ok(Self {
ws: AppWebsocket { tx, rx },
keystore,
Expand Down
1 change: 1 addition & 0 deletions crates/hpos_connect_hc/src/hf_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::sync::Arc;
/// 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
#[derive(Clone)]
JettTech marked this conversation as resolved.
Show resolved Hide resolved
pub struct HfAgent {
pub app: AppConnection,
}
Expand Down
5 changes: 3 additions & 2 deletions crates/hpos_connect_hc/src/hha_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ use holochain_types::prelude::{ExternIO, FunctionName, Signature, ZomeName};
/// 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
pub struct HHAAgent {
#[derive(Clone)]
pub struct CoreAppAgent {
pub app: AppConnection,
}

impl HHAAgent {
impl CoreAppAgent {
pub async fn spawn(config: Option<&Config>) -> Result<Self> {
let mut admin_ws = AdminWebsocket::connect(ADMIN_PORT)
.await
Expand Down
2 changes: 1 addition & 1 deletion crates/hpos_connect_hc/src/hha_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ 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)]
#[derive(Debug, Serialize, Deserialize, SerializedBytes, Clone, Default)]
pub struct HostSettings {
is_enabled: bool,
pub is_host_disabled: bool,
Expand Down
Loading
Loading