Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Override sim2h_url with DNA property #1828

Merged
merged 6 commits into from
Nov 4, 2019
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
4 changes: 3 additions & 1 deletion CHANGELOG-UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
* Allows the HC CLI to generate zomes from template repos. This will by default use the default holochain template repos (holochain/rust-zome-template and holochain/rust-proc-zome-template) but can also point to custom templates in remote repos or locally (e.g. `hc generate zomes/my_zome https://github.com/org/some-custom-zome-template`). [#1565](https://github.com/holochain/holochain-rust/pull/1565)
* Adds option `--property` to `hc hash` that sets DNA properties for hash calculation. [#1807](https://github.com/holochain/holochain-rust/pull/1807)
* Adds a prelude module to the HDK. Adding the statement `use hdk::prelude::*` should be enough for 90% of zome development [#1816](https://github.com/holochain/holochain-rust/pull/1816)

* Adds a special DNA property sim2h_url that, if set, overrides the conductor wide setting for the network configuration variable sim2h_url. [PR#1828](https://github.com/holochain/holochain-rust/pull/1828)
### Changed

### Deprecated
Expand All @@ -18,5 +18,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Fixed

* Fixes handling if DNA properties during `hc package`. DNA properties mentioned in the DNA's JSON manifest are now included in the package. [PR#1828](https://github.com/holochain/holochain-rust/pull/1828)

### Security

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 crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ git2 = "=0.9.1"
tera = "=0.11.20"
glob = "=0.3.0"
rustyline = "=5.0.0"
json-patch = "=0.2.2"

[dev-dependencies]
tempfile = "=3.0.7"
Expand Down
6 changes: 5 additions & 1 deletion crates/cli/src/cli/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use holochain_core_types::dna::Dna;
use holochain_json_api::json::JsonString;
use holochain_persistence_api::cas::content::AddressableContent;
use ignore::WalkBuilder;
use json_patch::merge;
use serde_json::{self, Map, Value};
use std::{
convert::TryFrom,
Expand Down Expand Up @@ -83,11 +84,14 @@ impl Packager {
Packager::new(strip_meta).run(&output, properties)
}

fn run(&self, output: &PathBuf, properties: Value) -> DefaultResult<()> {
fn run(&self, output: &PathBuf, mut properties: Value) -> DefaultResult<()> {
let current_dir = std::env::current_dir()?;
let dir_obj_bundle = Value::from(
self.bundle_recurse(&current_dir)
.map(|mut val| {
if let Some(props_from_dir) = val.get("properties") {
merge(&mut properties, props_from_dir);
}
val.insert("properties".to_string(), properties);
val
})
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern crate holochain_json_api;
extern crate holochain_locksmith;
extern crate holochain_persistence_api;
extern crate holochain_persistence_file;
extern crate json_patch;
extern crate lib3h_sodium;
extern crate structopt;
#[macro_use]
Expand Down
54 changes: 48 additions & 6 deletions crates/core/src/network/reducers/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,56 @@ use crate::{
network::state::NetworkState,
state::State,
};
use holochain_net::{connection::net_connection::NetSend, p2p_network::P2pNetwork};
use holochain_net::{
connection::net_connection::NetSend, p2p_config::BackendConfig, p2p_network::P2pNetwork,
};
use holochain_persistence_api::cas::content::AddressableContent;
use lib3h_protocol::{data_types::SpaceData, protocol_client::Lib3hClientProtocol, Address};
use log::error;
use log::{debug, error, info};

pub fn reduce_init(state: &mut NetworkState, root_state: &State, action_wrapper: &ActionWrapper) {
let action = action_wrapper.action();
let network_settings = unwrap_to!(action => Action::InitNetwork);
let handler = network_settings.handler.clone();
let mut p2p_config = network_settings.p2p_config.clone();

// Handle magic DNA property sim2h_url:
// If our DNA sets a property with name "sim2h_url" and if this instance is configured
// to use sim2h networking, override the conductor wide sim2h_url setting from the
// conductor config with the DNA property's value.

// Get the property from the DNA:
let nucleus = root_state.nucleus();
let dna = nucleus
.dna
.as_ref()
.expect("No DNA found when initializing network!");
let maybe_sim2h_url_override = dna
.properties
.as_object()
.and_then(|props| props.get("sim2h_url"))
.and_then(|sim2h_url_value| sim2h_url_value.as_str())
.map(|sim2h_url_str| sim2h_url_str.to_string());

// If we found a "sim2h_url" property...
if let Some(sim2h_url) = maybe_sim2h_url_override {
// ..and we're configured to use sim2h...
if let BackendConfig::Sim2h(sim2h_config) = &mut p2p_config.backend_config {
info!(
"Found property 'sim2h_url' in DNA {} - overriding conductor wide sim2h URL with: {}",
dna.address(),
sim2h_url,
);
// ..override the conductor wide setting.
sim2h_config.sim2h_url = sim2h_url;
} else {
debug!("DNA has 'sim2h_url' override property set, but it's ignored as we are not running a sim2h network backend");
}
}

let mut network = P2pNetwork::new(
network_settings.handler.clone(),
network_settings.p2p_config.clone(),
handler,
p2p_config,
Some(Address::from(network_settings.agent_id.clone())),
Some(root_state.conductor_api.clone()),
)
Expand Down Expand Up @@ -58,7 +98,7 @@ pub mod test {
persister::SimplePersister,
state::{test_store, StateWrapper},
};
use holochain_core_types::agent::AgentId;
use holochain_core_types::{agent::AgentId, dna::Dna};
use holochain_locksmith::RwLock;
use holochain_net::{connection::net_connection::NetHandler, p2p_config::P2pConfig};
use holochain_persistence_api::cas::content::{Address, AddressableContent};
Expand Down Expand Up @@ -106,7 +146,9 @@ pub mod test {
let action_wrapper = ActionWrapper::new(Action::InitNetwork(network_settings));

let mut network_state = NetworkState::new();
let root_state = test_store(context.clone());
let mut root_state = test_store(context.clone());
root_state = root_state.reduce(ActionWrapper::new(Action::InitializeChain(Dna::new())));

let result = reduce_init(&mut network_state, &root_state, &action_wrapper);

assert_eq!(result, ());
Expand Down
3 changes: 2 additions & 1 deletion crates/core/src/network/reducers/send_direct_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ mod tests {
},
state::test_store,
};
use holochain_core_types::error::HolochainError;
use holochain_core_types::{dna::Dna, error::HolochainError};
use holochain_persistence_api::cas::content::Address;

#[test]
pub fn reduce_send_direct_message_timeout_test() {
let netname = Some("reduce_send_direct_message_timeout_test");
let context = test_context("alice", netname);
let mut store = test_store(context.clone());
store = store.reduce(ActionWrapper::new(Action::InitializeChain(Dna::new())));

let dna_address: Address = "reduce_send_direct_message_timeout_test".into();
let handler = create_handler(&context, dna_address.to_string());
Expand Down