Skip to content

Commit

Permalink
Merge branch 'develop' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Nov 4, 2024
2 parents 5d04b15 + c50027b commit 1ec2477
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 66 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

### Changed

- Gossip arc clamp is not a setting part of `HolochainPluginConfig`.
25 changes: 24 additions & 1 deletion 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 crates/hc-pilot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ fn main() {
wan_network_config,
holochain_dir: conductor_dir,
admin_port: args.admin_port,
gossip_arc_clamp: None,
},
))
.setup(|app| {
Expand Down
4 changes: 2 additions & 2 deletions crates/scaffold-tauri-happ/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e

DIR=$(pwd)

nix shell --accept-flake-config --refresh github:holochain/scaffolding/develop#hc-scaffold --command bash -c "
nix shell --accept-flake-config --refresh github:holochain/holonix/main-0.3#hc-scaffold --command bash -c "
cd /tmp
rm -rf forum-scaffold-tauri-happ
Expand All @@ -23,7 +23,7 @@ set -e
npm install
npm run tauri icon $DIR/examples/end-user-happ/src-tauri/icons/icon.png
cd src-tauri
cargo add -p forum-scaffold-tauri-happ --path $DIR/crates/tauri-plugin-holochain
cargo add -p forum-scaffold-tauri-happ-tauri --path $DIR/crates/tauri-plugin-holochain
cd ..
npm run build:happ
npm run tauri build -- --no-bundle
Expand Down
22 changes: 7 additions & 15 deletions crates/scaffold-tauri-happ/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn scaffold_tauri_happ(
};

// - In package.json
// - Add "start", "network", "local-services", "build:zomes"
// - Add "start", "network", "build:zomes"
let root_package_json_path = PathBuf::from("package.json");
map_file(
&mut file_tree,
Expand Down Expand Up @@ -183,26 +183,19 @@ pub fn scaffold_tauri_happ(
let package_json_content = add_npm_script_to_package(
&(root_package_json_path.clone(), package_json_content),
&String::from("network"),
&format!("{} && BOOTSTRAP_PORT=$(port) SIGNAL_PORT=$(port) INTERNAL_IP=$(internal-ip --ipv4) concurrently -k \"{}\" \"UI_PORT=1420 {}\" \"{}\"",
&format!("{} && concurrently -k \"UI_PORT=1420 {}\" \"{}\"",

package_manager.run_script_command(String::from("build:happ"), None),
package_manager.run_script_command(String::from("local-services"), None ),
package_manager.run_script_command(String::from("start"), Some(ui_package.clone())),
package_manager.run_script_command(String::from("launch"), None)
),
)?;
let package_json_content = add_npm_script_to_package(
&(root_package_json_path.clone(), package_json_content),
&String::from("local-services"),
&format!("hc run-local-services --bootstrap-interface $INTERNAL_IP --bootstrap-port $BOOTSTRAP_PORT --signal-interfaces $INTERNAL_IP --signal-port $SIGNAL_PORT"),
)?;

let package_json_content = add_npm_script_to_package(
&(root_package_json_path.clone(), package_json_content),
&String::from("network:android"),
&format!("{} && BOOTSTRAP_PORT=$(port) SIGNAL_PORT=$(port) INTERNAL_IP=$(internal-ip --ipv4) concurrently -k \"{}\" \"UI_PORT=1420 {}\" \"{}\" \"{}\"",
&format!("{} && concurrently -k \"UI_PORT=1420 {}\" \"{}\" \"{}\"",
package_manager.run_script_command(String::from("build:happ"), None),
package_manager.run_script_command(String::from("local-services"), None),
package_manager.run_script_command(String::from("start"), Some(ui_package.clone())),
package_manager.run_script_command(String::from("tauri dev"), None),
package_manager.run_script_command(String::from("tauri android dev"), None),
Expand All @@ -212,7 +205,7 @@ pub fn scaffold_tauri_happ(
let package_json_content = add_npm_script_to_package(
&(root_package_json_path.clone(), package_json_content),
&String::from("build:zomes"),
&format!("CARGO_TARGET_DIR=target cargo build --release --target wasm32-unknown-unknown --workspace --exclude {app_name}"),
&format!("CARGO_TARGET_DIR=target cargo build --release --target wasm32-unknown-unknown --workspace --exclude {app_name}-tauri"),
)?;
let package_json_content = add_npm_script_to_package(
&(root_package_json_path.clone(), package_json_content),
Expand Down Expand Up @@ -387,10 +380,9 @@ mod tests {
"scripts": {
"build:happ": "npm run build:zomes && hc app pack workdir2",
"start": "AGENTS=2 npm run network",
"network": "npm run build:happ && BOOTSTRAP_PORT=$(port) SIGNAL_PORT=$(port) INTERNAL_IP=$(internal-ip --ipv4) concurrently -k \"npm run local-services\" \"UI_PORT=1420 npm run -w package1 start\" \"npm run launch\"",
"local-services": "hc run-local-services --bootstrap-interface $INTERNAL_IP --bootstrap-port $BOOTSTRAP_PORT --signal-interfaces $INTERNAL_IP --signal-port $SIGNAL_PORT",
"network:android": "npm run build:happ && BOOTSTRAP_PORT=$(port) SIGNAL_PORT=$(port) INTERNAL_IP=$(internal-ip --ipv4) concurrently -k \"npm run local-services\" \"UI_PORT=1420 npm run -w package1 start\" \"npm run tauri dev\" \"npm run tauri android dev\"",
"build:zomes": "CARGO_TARGET_DIR=target cargo build --release --target wasm32-unknown-unknown --workspace --exclude myhapp",
"network": "npm run build:happ && concurrently -k \"UI_PORT=1420 npm run -w package1 start\" \"npm run launch\"",
"network:android": "npm run build:happ && concurrently -k \"UI_PORT=1420 npm run -w package1 start\" \"npm run tauri dev\" \"npm run tauri android dev\"",
"build:zomes": "CARGO_TARGET_DIR=target cargo build --release --target wasm32-unknown-unknown --workspace --exclude myhapp-tauri",
"launch": "concurrently-repeat \"npm run tauri dev --no-watch\" $AGENTS",
"tauri": "tauri"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "{{app_name}}"
name = "{{app_name}}-tauri"
version = "0.0.0"
description = "{{title_case app_name}}"
authors = ["you"]
Expand Down
7 changes: 1 addition & 6 deletions crates/tauri-plugin-holochain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,8 @@ one_err = "0"
base64 = "0.22"
anyhow = "1"
async-trait = "0.1"
ctrlc = "3.4"

[build-dependencies]
tauri-plugin = { version = "2.0.0", features = ["build"] }

[features]
default = ["gossip_arc_normal"]

gossip_arc_empty = []
gossip_arc_full = []
gossip_arc_normal = []
3 changes: 3 additions & 0 deletions crates/tauri-plugin-holochain/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub enum Error {
#[error(transparent)]
FileSystemError(#[from] FileSystemError),

#[error(transparent)]
CtrclError(#[from] ctrlc::Error),

#[error("JSON serialization error: {0}")]
SerdeJsonError(#[from] serde_json::Error),

Expand Down
15 changes: 0 additions & 15 deletions crates/tauri-plugin-holochain/src/features.rs

This file was deleted.

23 changes: 23 additions & 0 deletions crates/tauri-plugin-holochain/src/hc_live_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::{env::current_dir, fs};

pub fn create_hc_live_file(admin_port: u16) -> crate::Result<()> {
let mut dir = current_dir()?;
if dir.ends_with("src-tauri") {
dir.pop();
}
let file = dir.join(format!(".hc_live_{admin_port}"));
std::fs::write(&file, format!("{admin_port}"))?;

Ok(())
}

pub fn delete_hc_live_file(admin_port: u16) -> crate::Result<()> {
let mut dir = current_dir()?;
if dir.ends_with("src-tauri") {
dir.pop();
}
let file = dir.join(format!(".hc_live_{admin_port}"));
fs::remove_file(file)?;

Ok(())
}
26 changes: 5 additions & 21 deletions crates/tauri-plugin-holochain/src/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use holochain_client::AdminWebsocket;
use crate::{
filesystem::FileSystem,
launch::signal::{can_connect_to_signal_server, run_local_signal_service},
HolochainPluginConfig, HolochainRuntime,
GossipArcClamp, HolochainPluginConfig, HolochainRuntime,
};

mod mdns;
Expand All @@ -20,25 +20,6 @@ use mdns::spawn_mdns_bootstrap;

pub const DEVICE_SEED_LAIR_KEYSTORE_TAG: &'static str = "DEVICE_SEED";

#[cfg(feature = "gossip_arc_empty")]
fn override_gossip_arc_clamping() -> Option<String> {
Some(String::from("empty"))
}

#[cfg(feature = "gossip_arc_full")]
fn override_gossip_arc_clamping() -> Option<String> {
Some(String::from("full"))
}

#[cfg(feature = "gossip_arc_normal")]
fn override_gossip_arc_clamping() -> Option<String> {
if cfg!(mobile) {
Some(String::from("empty"))
} else {
None
}
}

// pub static RUNNING_HOLOCHAIN: RwLock<Option<RunningHolochainInfo>> = RwLock::const_new(None);

/// Launch the holochain conductor in the background
Expand Down Expand Up @@ -83,7 +64,10 @@ pub async fn launch_holochain_runtime(
filesystem.keystore_dir().into(),
wan_network_config,
local_signal_url,
override_gossip_arc_clamping(),
config.gossip_arc_clamp.map(|n| match n {
GossipArcClamp::Full => "full".to_string(),
GossipArcClamp::Empty => "empty".to_string(),
}),
);

let keystore =
Expand Down
62 changes: 59 additions & 3 deletions crates/tauri-plugin-holochain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tauri::{
http::response,
ipc::CapabilityBuilder,
plugin::{Builder, TauriPlugin},
AppHandle, Emitter, Manager, Runtime, WebviewUrl, WebviewWindowBuilder,
AppHandle, Emitter, Manager, RunEvent, Runtime, WebviewUrl, WebviewWindowBuilder,
};

use holochain::{
Expand All @@ -29,15 +29,16 @@ use url2::Url2;
mod commands;
mod config;
mod error;
mod features;
mod filesystem;
mod hc_live_file;
mod http_server;
mod lair_signer;
mod launch;

use commands::install_web_app::{install_app, install_web_app, update_app, UpdateAppError};
pub use error::{Error, Result};
use filesystem::{AppBundleStore, BundleStore, FileSystem};
use hc_live_file::*;

const ZOME_CALL_SIGNER_INITIALIZATION_SCRIPT: &'static str = include_str!("../zome-call-signer.js");

Expand Down Expand Up @@ -520,6 +521,11 @@ pub struct WANNetworkConfig {
pub ice_servers_urls: Vec<Url2>,
}

pub enum GossipArcClamp {
Full,
Empty,
}

pub struct HolochainPluginConfig {
/// The directory where the holochain files and databases will be stored in
pub holochain_dir: PathBuf,
Expand All @@ -528,21 +534,47 @@ pub struct HolochainPluginConfig {
pub wan_network_config: Option<WANNetworkConfig>,
/// Force the conductor to run at this admin port
pub admin_port: Option<u16>,
/// Force the conductor to always have a "full", or "empty" Gossip Arc for all DNAs.
/// The Gossip Arc is the subsection of the DHT that you aim to store and serve to others.
///
/// A Full Gossip Arc means that your peer will always try to hold the full DHT state,
/// and serve it to others.
///
/// An Empty Gossip Arc means that your peer will always go to the network to fetch DHT data,
/// unless they authored it.
pub gossip_arc_clamp: Option<GossipArcClamp>,
}

fn default_gossip_arc_clamp() -> Option<GossipArcClamp> {
if cfg!(mobile) {
Some(GossipArcClamp::Empty)
} else {
None
}
}

impl HolochainPluginConfig {
pub fn new(holochain_dir: PathBuf, wan_network_config: Option<WANNetworkConfig>) -> Self {
pub fn new(
holochain_dir: PathBuf,
wan_network_config: Option<WANNetworkConfig>,
) -> Self {
HolochainPluginConfig {
holochain_dir,
wan_network_config,
admin_port: None,
gossip_arc_clamp: default_gossip_arc_clamp(),
}
}

pub fn admin_port(mut self, admin_port: u16) -> Self {
self.admin_port = Some(admin_port);
self
}

pub fn gossip_arc_clamp(mut self, gossip_arc_clamp: GossipArcClamp) -> Self {
self.gossip_arc_clamp = Some(gossip_arc_clamp);
self
}
}

fn plugin_builder<R: Runtime>() -> Builder<R> {
Expand Down Expand Up @@ -644,6 +676,18 @@ fn plugin_builder<R: Runtime>() -> Builder<R> {
r
})
})
.on_event(|app, event| match event {
RunEvent::Exit => {
if tauri::is_dev() {
if let Ok(h) = app.holochain() {
if let Err(err) = delete_hc_live_file(h.holochain_runtime.admin_port) {
log::error!("Failed to delete hc live file: {err:?}");
}
}
}
}
_ => {}
})
}

/// Initializes the plugin, waiting for holochain to launch before finishing the app's setup.
Expand Down Expand Up @@ -696,6 +740,18 @@ async fn launch_and_setup_holochain<R: Runtime>(

let holochain_runtime = launch_holochain_runtime(passphrase, config).await?;

#[cfg(desktop)]
if tauri::is_dev() {
create_hc_live_file(holochain_runtime.admin_port)?;

ctrlc::set_handler(move || {
if let Err(err) = delete_hc_live_file(holochain_runtime.admin_port) {
log::error!("Failed to delete hc live file: {err:?}");
}
std::process::exit(0);
})?;
}

let p = HolochainPlugin::<R> {
app_handle: app_handle.clone(),
holochain_runtime,
Expand Down
Loading

0 comments on commit 1ec2477

Please sign in to comment.