use crate::{
conductor::broadcaster::Broadcaster,
config::{
serialize_configuration, Configuration, InterfaceConfiguration, InterfaceDriver,
NetworkConfig, StorageConfiguration,
},
context_builder::ContextBuilder,
dpki_instance::DpkiInstance,
error::HolochainInstanceError,
key_loaders::test_keystore,
keystore::{Keystore, PRIMARY_KEYBUNDLE_ID},
port_utils::{try_with_port, INTERFACE_CONNECT_ATTEMPTS_MAX},
Holochain,
};
use crossbeam_channel::{unbounded, Receiver, Sender};
use holochain_common::paths::DNA_EXTENSION;
use holochain_core::{
logger::Logger, network::handler::fetch::fetch_aspects_for_entry, signal::Signal,
};
use holochain_core_types::{
agent::AgentId,
dna::Dna,
error::{HcResult, HolochainError},
network::entry_aspect::EntryAspect,
};
use holochain_dpki::{key_bundle::KeyBundle, password_encryption::PwHashConfig};
use holochain_json_api::json::JsonString;
use holochain_locksmith::{Mutex, RwLock};
use holochain_logging::{rule::RuleFilter, FastLogger, FastLoggerBuilder};
use holochain_persistence_api::{cas::content::AddressableContent, hash::HashString};
use holochain_tracing as ht;
use jsonrpc_ws_server::jsonrpc_core::IoHandler;
use lib3h_protocol::types::AspectHash;
use std::{
clone::Clone,
collections::HashMap,
convert::TryFrom,
fs::{self, File},
io::prelude::*,
option::NoneError,
path::PathBuf,
str::FromStr,
sync::Arc,
thread,
time::Duration,
};
#[cfg(unix)]
use crate::conductor::passphrase_manager::PassphraseServiceUnixSocket;
use crate::{
conductor::passphrase_manager::{
PassphraseManager, PassphraseService, PassphraseServiceCmd, PassphraseServiceMock,
},
config::{AgentConfiguration, PassphraseServiceConfig, TracingConfiguration},
interface::{ConductorApiBuilder, InstanceMap, Interface},
port_utils::get_free_port,
signal_wrapper::SignalWrapper,
static_file_server::ConductorStaticFileServer,
static_server_impls::NickelStaticServer as StaticServer,
};
use boolinator::Boolinator;
use holochain_core::context::InstanceStats;
use holochain_core_types::dna::bridges::BridgePresence;
use holochain_net::p2p_config::{BackendConfig, P2pBackendKind, P2pConfig};
pub const MAX_DYNAMIC_PORT: u16 = std::u16::MAX;
fn magic_port_binding_string(interface_config_id: &str, port: u16) -> String {
format!(
"*** Bound interface '{}' to port: {}",
interface_config_id, port
)
}
lazy_static! {
pub static ref CONDUCTOR: Arc<Mutex<Option<Conductor>>> = Arc::new(Mutex::new(None));
}
pub fn mount_conductor_from_config(config: Configuration) {
let conductor = Conductor::from_config(config);
CONDUCTOR.lock().unwrap().replace(conductor);
}
type TraceReporterMap = HashMap<
String,
(
Receiver<ht::FinishedSpan>,
ht::reporter::JaegerCompactReporter,
),
>;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GetMetaOptions {}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GetMetaResponse {
headers: Vec<HashString>,
aspects: Vec<(AspectHash, String)>, link_add_count: u32,
link_remove_count: u32,
}
pub struct Conductor {
pub(in crate::conductor) instances: InstanceMap,
instance_signal_receivers: Arc<RwLock<HashMap<String, Receiver<Signal>>>>,
trace_reporters: Arc<RwLock<TraceReporterMap>>,
agent_keys: HashMap<String, Arc<Mutex<Keystore>>>,
pub(in crate::conductor) config: Configuration,
pub(in crate::conductor) static_servers: HashMap<String, StaticServer>,
pub(in crate::conductor) interface_threads: HashMap<String, Sender<()>>,
pub(in crate::conductor) interface_broadcasters: Arc<RwLock<HashMap<String, Broadcaster>>>,
signal_multiplexer_kill_switch: Option<Sender<()>>,
stats_thread_kill_switch: Option<Sender<()>>,
stats_signal_receiver: Option<Receiver<HashMap<String, InstanceStats>>>,
pub key_loader: KeyLoader,
pub(in crate::conductor) dna_loader: DnaLoader,
pub(in crate::conductor) ui_dir_copier: UiDirCopier,
signal_tx: Option<SignalSender>,
logger: FastLogger,
p2p_config: Option<P2pConfig>,
pub passphrase_manager: Arc<PassphraseManager>,
pub hash_config: Option<PwHashConfig>, }
impl Drop for Conductor {
fn drop(&mut self) {
self.shutdown()
.unwrap_or_else(|err| println!("Error during shutdown, continuing anyway: {:?}", err));
self.logger.flush();
}
}
type SignalSender = Sender<Signal>;
pub type KeyLoader = Arc<
Box<
dyn FnMut(
&PathBuf,
Arc<PassphraseManager>,
Option<PwHashConfig>,
) -> Result<Keystore, HolochainError>
+ Send
+ Sync,
>,
>;
pub type DnaLoader = Arc<Box<dyn FnMut(&PathBuf) -> Result<Dna, HolochainError> + Send + Sync>>;
pub type UiDirCopier =
Arc<Box<dyn FnMut(&PathBuf, &PathBuf) -> Result<(), HolochainError> + Send + Sync>>;
pub fn notify(msg: String) {
println!("{}", msg);
}
#[autotrace]
#[holochain_tracing_macros::newrelic_autotrace(HOLOCHAIN_CONDUCTOR_LIB)]
#[allow(clippy::ptr_arg)]
impl Conductor {
pub fn from_config(config: Configuration) -> Self {
lib3h_sodium::check_init();
let _rules = config.logger.rules.clone();
let mut logger_builder = FastLoggerBuilder::new();
logger_builder.set_level_from_str(&config.logger.logger_level.as_str());
for rule in config.logger.rules.rules.iter() {
logger_builder.add_rule_filter(RuleFilter::new(
rule.pattern.as_str(),
rule.exclude,
rule.color.as_ref().unwrap_or(&String::default()).as_str(),
));
}
let logger = logger_builder
.build()
.expect("Fail to instanciate the logging factory.");
if !config.ui_bundles.is_empty() || !config.ui_interfaces.is_empty() {
println!();
println!("{}", std::iter::repeat("!").take(20).collect::<String>());
println!("DEPRECATION WARNING - Hosting a static UI via the conductor will not be supported in future releases");
println!("{}", std::iter::repeat("!").take(20).collect::<String>());
println!();
}
let passphrase_service: Arc<Mutex<dyn PassphraseService + Send>> =
if let PassphraseServiceConfig::UnixSocket { path } = config.passphrase_service.clone()
{
#[cfg(not(unix))]
let _ = path;
#[cfg(not(unix))]
panic!("Unix domain sockets are not available on non-Unix systems. Can't create a PassphraseServiceUnixSocket.");
#[cfg(unix)]
Arc::new(Mutex::new(PassphraseServiceUnixSocket::new(path)))
} else {
match config.passphrase_service.clone() {
PassphraseServiceConfig::Cmd => Arc::new(Mutex::new(PassphraseServiceCmd {})),
PassphraseServiceConfig::Mock { passphrase } => {
Arc::new(Mutex::new(PassphraseServiceMock { passphrase }))
}
_ => unreachable!(),
}
};
Conductor {
instances: HashMap::new(),
instance_signal_receivers: Arc::new(RwLock::new(HashMap::new())),
trace_reporters: Arc::new(RwLock::new(HashMap::new())),
agent_keys: HashMap::new(),
interface_threads: HashMap::new(),
static_servers: HashMap::new(),
interface_broadcasters: Arc::new(RwLock::new(HashMap::new())),
signal_multiplexer_kill_switch: None,
stats_thread_kill_switch: None,
stats_signal_receiver: None,
config,
key_loader: Arc::new(Box::new(Self::load_key)),
dna_loader: Arc::new(Box::new(Self::load_dna)),
ui_dir_copier: Arc::new(Box::new(Self::copy_ui_dir)),
signal_tx: None,
logger,
p2p_config: None,
passphrase_manager: Arc::new(PassphraseManager::new(passphrase_service)),
hash_config: None,
}
}
pub fn spawn_stats_thread(&mut self) {
self.stop_stats_thread();
let instances = self.instances.clone();
let (kill_switch_tx, kill_switch_rx) = unbounded();
let (stats_tx, stats_rx) = unbounded();
self.stats_thread_kill_switch = Some(kill_switch_tx);
self.stats_signal_receiver = Some(stats_rx);
thread::Builder::new()
.name("stats".to_string())
.spawn(move || loop {
let mut instance_stats: HashMap<String, InstanceStats> = HashMap::new();
for (id, instance) in instances.iter() {
if let Err(error) = instance
.read()
.map_err(|_| {
HolochainInstanceError::InternalFailure(HolochainError::new(
"Could not get lock on instance",
))
})
.and_then(|instance| instance.context())
.and_then(|context| context.get_stats().map_err(|e| e.into()))
.and_then(|stats| {
instance_stats.insert(id.clone(), stats);
Ok(())
})
{
error!(
"Could not get stats for instance '{}'. Error: {:?}",
id, error
);
}
}
if let Err(e) = stats_tx.send(instance_stats) {
error!("Could not send stats signal over channel: {:?}", e);
}
if kill_switch_rx.try_recv().is_ok() {
break;
}
thread::sleep(Duration::from_millis(500));
})
.expect("");
}
pub fn add_agent_keystore(&mut self, agent_id: String, keystore: Keystore) {
self.agent_keys
.insert(agent_id, Arc::new(Mutex::new(keystore)));
}
pub fn with_signal_channel(mut self, signal_tx: Sender<Signal>) -> Self {
if !self.instances.is_empty() {
panic!("Cannot set a signal channel after having run from_config()");
}
self.signal_tx = Some(signal_tx);
self
}
pub fn config(&self) -> Configuration {
self.config.clone()
}
pub fn start_signal_multiplexer(&mut self) -> thread::JoinHandle<()> {
self.stop_signal_multiplexer();
let broadcasters = self.interface_broadcasters.clone();
let instance_signal_receivers = self.instance_signal_receivers.clone();
let signal_tx = self.signal_tx.clone();
let config = self.config.clone();
let (kill_switch_tx, kill_switch_rx) = unbounded();
self.signal_multiplexer_kill_switch = Some(kill_switch_tx);
self.spawn_stats_thread();
let stats_signal_receiver = self.stats_signal_receiver.clone().expect(
"Receiver must be Some after calling spawn_stats_thread() above which should set it",
);
debug!("starting signal loop");
thread::Builder::new()
.name("signal_multiplexer".to_string())
.spawn(move || loop {
let broadcasters = broadcasters.read().unwrap();
let admin_interfaces = config
.interfaces
.iter()
.filter(|interface_config| interface_config.admin)
.collect::<Vec<_>>();
{
for (instance_id, receiver) in instance_signal_receivers.read().unwrap().iter()
{
if let Ok(signal) = receiver.try_recv() {
signal_tx.clone().map(|s| s.send(signal.clone()));
let interfaces_with_instance: Vec<&InterfaceConfiguration> =
match signal {
Signal::Trace(_) => {
if config.signals.trace {
admin_interfaces.clone()
} else {
Vec::new()
}
}
Signal::Consistency(_) => {
if config.signals.consistency {
config
.interfaces
.iter()
.filter(|interface_config| interface_config.admin)
.collect()
} else {
Vec::new()
}
}
Signal::User(_) => {
println!(
"SIGNAL for instance[{}]: {:?}",
instance_id, signal
);
let interfaces = config
.interfaces
.iter()
.filter(|interface_config| {
let contains_instance = interface_config
.instances
.iter()
.any(|instance| instance.id == *instance_id);
let is_admin = interface_config.admin;
contains_instance || is_admin
})
.collect();
println!("INTERFACEs for SIGNAL: {:?}", interfaces);
interfaces
}
};
for interface in interfaces_with_instance {
if let Some(broadcaster) = broadcasters.get(&interface.id) {
if let Err(error) =
broadcaster.send(SignalWrapper::InstanceSignal {
signal: signal.clone(),
instance_id: instance_id.clone(),
})
{
notify(error.to_string());
}
};
}
}
}
}
while let Ok(instance_stats) = stats_signal_receiver.try_recv() {
for interface in &admin_interfaces {
if let Some(broadcaster) = broadcasters.get(&interface.id) {
if let Err(error) = broadcaster.send(SignalWrapper::InstanceStats {
instance_stats: instance_stats.clone(),
}) {
notify(error.to_string());
}
};
}
}
if kill_switch_rx.try_recv().is_ok() {
break;
}
thread::sleep(Duration::from_millis(1));
})
.expect("Must be able to spawn thread")
}
pub fn spawn_trace_reporter_thread(&mut self) -> thread::JoinHandle<()> {
let reporters = self.trace_reporters.clone();
thread::Builder::new()
.name("trace_reporter_thread".to_string())
.spawn(move || loop {
for (rx, reporter) in reporters.read().unwrap().values() {
if let Ok(span) = rx.try_recv() {
if let Err(e) = reporter.report(&[span]) {
warn!("Could not report span: {:?}", e);
}
}
}
thread::sleep(Duration::from_millis(1));
})
.expect("Must be able to spawn thread")
}
pub fn stop_stats_thread(&self) {
self.stats_thread_kill_switch
.as_ref()
.map(|kill_switch| kill_switch.send(()));
}
pub fn stop_signal_multiplexer(&self) {
self.signal_multiplexer_kill_switch
.as_ref()
.map(|kill_switch| kill_switch.send(()));
}
pub fn start_all_interfaces(&mut self) {
self.interface_threads = self
.config
.interfaces
.iter()
.map(|ic| (ic.id.clone(), self.spawn_interface_thread(ic.clone())))
.collect();
self.start_signal_multiplexer();
}
pub fn stop_all_interfaces(&mut self) {
for (id, kill_switch) in self.interface_threads.iter() {
notify(format!("Stopping interface {}", id));
kill_switch.send(()).unwrap_or_else(|err| {
let message = format!("Error stopping interface: {}", err);
notify(message);
});
}
}
pub fn stop_interface_by_id(&mut self, id: &String) -> Result<(), HolochainError> {
{
let kill_switch = self.interface_threads.get(id).ok_or_else(|| {
HolochainError::ErrorGeneric(format!("Interface {} not found.", id))
})?;
notify(format!("Stopping interface {}", id));
kill_switch.send(()).map_err(|err| {
let message = format!("Error stopping interface: {}", err);
notify(message.clone());
HolochainError::ErrorGeneric(message)
})?;
}
self.interface_threads.remove(id);
Ok(())
}
pub fn start_interface_by_id(&mut self, id: &String) -> Result<(), String> {
notify(format!("Start interface by id: {}", id));
self.config
.interface_by_id(id)
.ok_or_else(|| format!("Interface does not exist: {}", id))
.and_then(|config| self.start_interface(&config))
}
pub fn start_all_static_servers(&mut self) -> Result<(), String> {
notify("Starting all servers".into());
self.static_servers.iter_mut().for_each(|(id, server)| {
notify(format!("Starting server \"{}|\"", id));
server
.start()
.unwrap_or_else(|_| panic!("Couldn't start server {}", id));
notify(format!("Server started for \"{}\"", id))
});
Ok(())
}
pub fn instance_get_meta(
&mut self,
id: &String,
hash: HashString,
_options: GetMetaOptions,
) -> Result<GetMetaResponse, HolochainInstanceError> {
let instance = self.instances.get(id)?.read().unwrap();
let context = instance.context()?;
let headers = match context
.state()
.expect("Could not get state for handle_fetch_entry")
.get_headers(hash.clone())
{
Err(_error) => Vec::new(),
Ok(headers) => headers.into_iter().map(|h| h.address()).collect(),
};
let mut add_links = 0;
let mut remove_links = 0;
let aspects = fetch_aspects_for_entry(&hash.into(), context);
let aspects = aspects
.into_iter()
.map(|a| {
match a {
EntryAspect::LinkAdd(_, _) => add_links += 1,
EntryAspect::LinkRemove(_, _) => remove_links += 1,
_ => (),
}
(a.address().into(), a.type_hint())
})
.collect();
let response = GetMetaResponse {
headers,
aspects,
link_add_count: add_links,
link_remove_count: remove_links,
};
Ok(response)
}
pub fn start_instance(&mut self, id: &String) -> Result<(), HolochainInstanceError> {
let mut instance = self.instances.get(id)?.write().unwrap();
notify(format!("Starting instance \"{}\"...", id));
let dna =
instance
.state()?
.nucleus()
.dna()
.ok_or(HolochainInstanceError::InternalFailure(
HolochainError::DnaMissing,
))?;
for zome in dna.zomes.values() {
for bridge in zome.bridges.iter() {
if bridge.presence == BridgePresence::Required {
let handle = bridge.handle.clone();
let bridge_config = self
.config
.bridges
.iter()
.find(|b| b.handle == handle)
.ok_or_else(|| {
HolochainInstanceError::RequiredBridgeMissing(handle.clone())
})?;
self.instances
.get(&bridge_config.callee_id)
.ok_or_else(|| {
HolochainInstanceError::RequiredBridgeMissing(handle.clone())
})?
.read()
.unwrap()
.active()
.ok_or_else(|| HolochainInstanceError::RequiredBridgeMissing(handle))?;
}
}
}
instance.start()
}
pub fn stop_instance(&mut self, id: &String) -> Result<(), HolochainInstanceError> {
let instance = self.instances.get(id)?;
notify(format!("Stopping instance \"{}\"...", id));
instance.write().unwrap().stop()
}
pub fn start_all_instances(&mut self) -> Result<(), HolochainInstanceError> {
notify("Start all instances".to_string());
self.spawn_trace_reporter_thread();
self.config
.instances
.iter()
.map(|instance_config| instance_config.id.clone())
.collect::<Vec<String>>()
.iter()
.map(|id| {
let start_result = self.start_instance(&id);
if Err(HolochainInstanceError::InstanceAlreadyActive) == start_result {
Ok(())
} else {
start_result
}
})
.collect::<Result<Vec<()>, _>>()
.map(|_| ())
}
pub fn start_dpki_instance(&mut self) -> Result<(), HolochainInstanceError> {
let dpki_instance_id = &self.dpki_instance_id().unwrap();
let mut instance = self
.instantiate_from_config(dpki_instance_id)
.map_err(|err| {
HolochainInstanceError::InternalFailure(HolochainError::ErrorGeneric(err))
})?;
instance.start()?;
self.instances.insert(
dpki_instance_id.to_string(),
Arc::new(RwLock::new(instance)),
);
Ok(())
}
pub fn stop_all_instances(&mut self) -> Result<(), HolochainInstanceError> {
self.instances
.iter_mut()
.map(|(id, hc)| {
notify(format!("Stopping instance \"{}\"...", id));
hc.write()
.map(|mut lock| {
let _ = lock.stop();
})
.map_err(|_| {
notify(format!("Error stopping instance \"{}\": could not get a lock. Will ignore and proceed shutting down other instances...", id));
HolochainInstanceError::InternalFailure(HolochainError::new("Could not get lock on shutdown"))
})
})
.collect::<Result<Vec<()>, _>>()
.map(|_| ())
}
pub fn instances(&self) -> &InstanceMap {
&self.instances
}
pub fn shutdown(&mut self) -> Result<(), HolochainInstanceError> {
self.stop_all_interfaces();
let mut conductor_guard = CONDUCTOR.lock().unwrap();
std::mem::replace(&mut *conductor_guard, None);
self.stop_all_instances()?;
self.signal_multiplexer_kill_switch
.as_ref()
.map(|sender| sender.send(()));
self.instances = HashMap::new();
Ok(())
}
fn get_p2p_config(&self) -> P2pConfig {
self.p2p_config.clone().map(|p2p_config| {
let urls : Vec<url::Url> = self.instances.values().map(|instance| {
instance
.read()
.unwrap()
.context()
.unwrap()
.network_state()
.unwrap()
.network
.as_ref()
.expect("Network not initialized")
.p2p_endpoint()
}).collect();
match p2p_config.to_owned().backend_config {
BackendConfig::Memory(mut config) => {
config.bootstrap_nodes =
if config.bootstrap_nodes.is_empty() && !urls.is_empty()
{ vec![urls[0].clone().into()] }
else
{ config.bootstrap_nodes.clone() };
let mut p2p_config = p2p_config;
p2p_config.backend_config = BackendConfig::Memory(config);
p2p_config
},
_ => p2p_config
}
}).unwrap_or_else(|| {
println!("warn: instance_network_config called before p2p_config initialized! Using default in-memory network name.");
P2pConfig::new_with_unique_memory_backend()
})
}
fn initialize_p2p_config(&mut self) -> P2pConfig {
if self.config.network.is_none() {
return P2pConfig::new_with_unique_memory_backend();
}
match self.config.network.clone().unwrap() {
NetworkConfig::Memory(config) => P2pConfig {
backend_kind: P2pBackendKind::GhostEngineMemory,
backend_config: BackendConfig::Memory(config),
maybe_end_user_config: None,
},
NetworkConfig::Lib3h(config) => P2pConfig {
backend_kind: P2pBackendKind::LIB3H,
backend_config: BackendConfig::Lib3h(config),
maybe_end_user_config: None,
},
NetworkConfig::Sim2h(config) => P2pConfig {
backend_kind: P2pBackendKind::SIM2H,
backend_config: BackendConfig::Sim2h(config),
maybe_end_user_config: None,
},
}
}
pub fn boot_from_config(&mut self) -> Result<(), String> {
notify("conductor: boot_from_config".into());
self.config.check_consistency(&mut self.dna_loader)?;
if self.p2p_config.is_none() {
self.p2p_config = Some(self.initialize_p2p_config());
}
self.dpki_bootstrap()?;
for id in self.config.instance_ids_sorted_by_bridge_dependencies()? {
if !self.instances.contains_key(&id) {
let instance = self.instantiate_from_config(&id).map_err(|error| {
format!(
"Error while trying to create instance \"{}\": {}",
id, error
)
})?;
self.instances
.insert(id.clone(), Arc::new(RwLock::new(instance)));
}
}
for ui_interface_config in self.config.ui_interfaces.clone() {
notify(format!("adding ui interface {}", &ui_interface_config.id));
let bundle_config = self
.config
.ui_bundle_by_id(&ui_interface_config.bundle)
.ok_or_else(|| {
format!(
"UI interface {} references bundle with id {} but no such bundle found",
&ui_interface_config.id, &ui_interface_config.bundle
)
})?;
let connected_dna_interface = ui_interface_config
.clone()
.dna_interface
.map(|interface_id| self.config.interface_by_id(&interface_id).unwrap());
self.static_servers.insert(
ui_interface_config.id.clone(),
StaticServer::from_configs(
ui_interface_config,
bundle_config,
connected_dna_interface,
),
);
}
Ok(())
}
fn build_tracer_and_reporter_for_instance(
&self,
id: &str,
) -> Option<(
ht::Tracer,
Receiver<ht::FinishedSpan>,
ht::reporter::JaegerCompactReporter,
)> {
match self.config.tracing.clone().unwrap_or_default() {
TracingConfiguration::Jaeger(jaeger_config) => {
let (span_tx, span_rx) = crossbeam_channel::unbounded();
let service_name = format!("{}-{}", jaeger_config.service_name, id);
let mut reporter = ht::reporter::JaegerCompactReporter::new(&service_name).unwrap();
if let Some(s) = jaeger_config.socket_address {
let addr =
std::net::SocketAddr::from_str(&s).expect("Could not parse socket address");
reporter
.set_agent_addr(addr)
.expect("Could not set Jaeger socket address");
}
Some((
ht::Tracer::with_sender(ht::AllSampler, span_tx),
span_rx,
reporter,
))
}
TracingConfiguration::None => None,
}
}
pub fn instantiate_from_config(&mut self, id: &String) -> Result<Holochain, String> {
self.config.check_consistency(&mut self.dna_loader)?;
self.config
.instance_by_id(&id)
.ok_or_else(|| String::from("Instance not found in config"))
.and_then(|instance_config| {
let mut context_builder = ContextBuilder::new();
let instance_name = instance_config.id.clone();
let agent_id = &instance_config.agent;
let agent_config = self.config.agent_by_id(agent_id).unwrap();
let agent_address = self.agent_config_to_id(&agent_config)?;
if agent_config.test_agent.unwrap_or_default() {
self.config.update_agent_address_by_id(agent_id, &agent_address);
self.save_config()?;
}
context_builder = context_builder.with_agent(agent_address);
context_builder = context_builder.with_p2p_config(self.get_p2p_config());
let (sender, receiver) = unbounded();
self.instance_signal_receivers
.write()
.unwrap()
.insert(instance_name.clone(), receiver);
context_builder = context_builder.with_signals(sender);
if let Some((tracer, span_rx, reporter)) = self.build_tracer_and_reporter_for_instance(&instance_name) {
context_builder = context_builder.with_tracer(tracer);
self.trace_reporters.write().unwrap().insert(instance_name.clone(), (span_rx, reporter));
}
match instance_config.storage {
StorageConfiguration::File { path } => {
context_builder =
context_builder.with_file_storage(path).map_err(|hc_err| {
format!("Error creating context: {}", hc_err.to_string())
})?
}
StorageConfiguration::Memory => {
context_builder = context_builder.with_memory_storage()
}
StorageConfiguration::Pickle { path } => {
context_builder =
context_builder
.with_pickle_storage(path)
.map_err(|hc_err| {
format!("Error creating context: {}", hc_err.to_string())
})?
}
StorageConfiguration::Lmdb { path, initial_mmap_bytes } => {
context_builder =
context_builder
.with_lmdb_storage(path, initial_mmap_bytes)
.map_err(|hc_err| {
format!("Error creating context: {}", hc_err.to_string())
})?
}
}
let api = self.build_conductor_api(instance_config.id)?;
context_builder = context_builder.with_conductor_api(api);
if self.config.logger.state_dump {
context_builder = context_builder.with_state_dump_logging();
}
context_builder = context_builder.with_instance_name(&instance_name);
if let Some(metric_publisher_config) = &self.config.metric_publisher {
debug!("Setting metric publisher in context_builder to: {:?}", metric_publisher_config);
context_builder = context_builder.with_metric_publisher(&metric_publisher_config);
};
let context = context_builder.spawn();
let dna_config = self.config.dna_by_id(&instance_config.dna).unwrap();
let dna_file = PathBuf::from(&dna_config.file);
let mut dna = Arc::get_mut(&mut self.dna_loader).unwrap()(&dna_file).map_err(|_| {
HolochainError::ConfigError(format!(
"Could not load DNA file \"{}\"",
dna_config.file
))
})?;
match dna_config.uuid {
Some(uuid) => {
dna.uuid = uuid;
self.config.update_dna_hash_by_id(&dna_config.id, dna.address().to_string());
self.save_config()?;
},
None => {
let dna_hash_from_conductor_config = HashString::from(dna_config.hash);
let dna_hash_computed = &dna.address();
match Arc::get_mut(&mut self.dna_loader)
.expect("Fail to get a mutable reference to 'dna loader'.")(&dna_file) {
Ok(dna) => {
let dna_hash_computed_from_file = dna.address();
Conductor::check_dna_consistency_from_all_sources(
&context,
&dna_hash_from_conductor_config,
&dna_hash_computed,
&dna_hash_computed_from_file, &dna_file)?;
},
Err(_) => {
let msg = format!("Conductor: Could not load DNA file {:?}.", &dna_file);
log_error!(context, "{}", msg);
match Conductor::check_dna_consistency(
&dna_hash_from_conductor_config,
&dna_hash_computed) {
Ok(_) => (),
Err(e) => {
let msg = format!("\
Conductor: DNA hashes mismatch: 'Conductor config' != 'Conductor instance': \
'{}' != '{}'",
&dna_hash_from_conductor_config,
&dna_hash_computed);
log_error!(context, "{}", msg);
return Err(e.to_string());
}
}
}
}
}
};
let mut context_clone = context.clone();
let context = Arc::new(context);
Holochain::load(context)
.and_then(|hc| {
notify(format!(
"Successfully loaded instance {} from storage",
id.clone()
));
Ok(hc)
})
.or_else(|loading_error| {
context_clone.reset_instance();
let context = Arc::new(context_clone);
if loading_error == HolochainError::from(NoneError) {
notify("No chain found in the store".to_string());
} else {
notify(format!(
"Failed to load instance {} from storage: {:?}",
id.clone(),
loading_error
));
}
notify("Initializing new chain...".to_string());
Holochain::new(dna, context)
.map_err(|hc_err| hc_err.to_string())
})
})
}
pub fn build_conductor_api(
&mut self,
instance_id: String,
) -> Result<IoHandler, HolochainError> {
notify(format!(
"conductor: build_conductor_api instance_id={}, config={:?}",
instance_id, self.config
));
let instance_config = self.config.instance_by_id(&instance_id)?;
let agent_id = instance_config.agent.clone();
let agent_config = self.config.agent_by_id(&agent_id)?;
let mut api_builder = ConductorApiBuilder::new();
if let Some(true) = agent_config.holo_remote_key {
api_builder = api_builder.with_outsource_signing_callback(
self.agent_config_to_id(&agent_config)?,
self.config
.signing_service_uri
.clone()
.expect("holo_remote_key needs signing_service_uri set"),
);
api_builder = api_builder.with_outsource_signing_callback(
self.agent_config_to_id(&agent_config)?,
self.config
.encryption_service_uri
.clone()
.expect("holo_remote_key needs encryption_service_uri set"),
);
api_builder = api_builder.with_outsource_signing_callback(
self.agent_config_to_id(&agent_config)?,
self.config
.decryption_service_uri
.clone()
.expect("holo_remote_key needs decryption_service_uri set"),
);
} else {
api_builder = api_builder.with_agent_signature_callback(
self.get_keybundle_for_agent(&instance_config.agent)?,
);
api_builder = api_builder.with_agent_encryption_callback(
self.get_keybundle_for_agent(&instance_config.agent)?,
);
api_builder = api_builder.with_agent_decryption_callback(
self.get_keybundle_for_agent(&instance_config.agent)?,
);
let keystore = self.get_keystore_for_agent(&instance_config.agent)?;
api_builder = api_builder.with_agent_keystore_functions(keystore);
}
let id = instance_config.id;
for bridge in self.config.bridge_dependencies(id.clone()) {
assert_eq!(bridge.caller_id, id.clone());
let callee_config = self
.config
.instance_by_id(&bridge.callee_id)
.expect("config.check_consistency()? jumps out if config is broken");
let callee_instance = self.instances.get(&bridge.callee_id).expect(
r#"
We have to create instances ordered by bridge dependencies such that we
can expect the callee to be present here because we need it to create
the bridge API"#,
);
api_builder =
api_builder.with_named_instance(bridge.handle.clone(), callee_instance.clone());
api_builder =
api_builder.with_named_instance_config(bridge.handle.clone(), callee_config);
}
Ok(api_builder.spawn())
}
pub fn agent_config_to_id(
&mut self,
agent_config: &AgentConfiguration,
) -> Result<AgentId, HolochainError> {
Ok(if let Some(true) = agent_config.holo_remote_key {
AgentId::new(&agent_config.name, agent_config.public_address.clone())
} else {
let keybundle_arc = self.get_keybundle_for_agent(&agent_config.id)?;
let keybundle = keybundle_arc.lock().unwrap();
AgentId::new(&agent_config.name, keybundle.get_id())
})
}
pub fn check_load_key_for_agent(&mut self, agent_id: &String) -> Result<(), String> {
if let Some(true) = self
.config
.agent_by_id(agent_id)
.and_then(|a| a.holo_remote_key)
{
return Ok(());
}
self.get_keystore_for_agent(agent_id)?;
Ok(())
}
fn check_dna_consistency_from_all_sources(
ctx: &holochain_core::context::Context,
dna_hash_from_conductor_config: &HashString,
dna_hash_computed: &HashString,
dna_hash_computed_from_file: &HashString,
dna_file: &PathBuf,
) -> Result<(), HolochainError> {
match Conductor::check_dna_consistency(&dna_hash_from_conductor_config, &dna_hash_computed)
{
Ok(_) => (),
Err(e) => {
let msg = format!("\
err/Conductor: DNA hashes mismatch: 'Conductor config' != 'Conductor instance': \
'{}' != '{}'",
&dna_hash_from_conductor_config,
&dna_hash_computed);
log_debug!(ctx, "{}", msg);
return Err(e);
}
}
match Conductor::check_dna_consistency(
&dna_hash_from_conductor_config,
&dna_hash_computed_from_file,
) {
Ok(_) => (),
Err(e) => {
let msg = format!("\
err/Conductor: DNA hashes mismatch: 'Conductor config' != 'Hash computed from the file {:?}': \
'{}' != '{}'",
&dna_file,
&dna_hash_from_conductor_config,
&dna_hash_computed_from_file);
log_debug!(ctx, "{}", msg);
return Err(e);
}
}
match Conductor::check_dna_consistency(&dna_hash_computed, &dna_hash_computed_from_file) {
Ok(_) => (),
Err(e) => {
let msg = format!("\
err/Conductor: DNA hashes mismatch: 'Conductor instance' != 'Hash computed from the file {:?}': \
'{}' != '{}'",
&dna_file,
&dna_hash_computed,
&dna_hash_computed_from_file);
log_debug!(ctx, "{}", msg);
return Err(e);
}
}
Ok(())
}
fn check_dna_consistency(
dna_hash_a: &HashString,
dna_hash_b: &HashString,
) -> Result<(), HolochainError> {
if *dna_hash_a == *dna_hash_b {
Ok(())
} else {
Err(HolochainError::DnaHashMismatch(
dna_hash_a.clone(),
dna_hash_b.clone(),
))
}
}
pub fn get_keystore_for_agent(
&mut self,
agent_id: &String,
) -> Result<Arc<Mutex<Keystore>>, String> {
if !self.agent_keys.contains_key(agent_id) {
let agent_config = self
.config
.agent_by_id(agent_id)
.ok_or_else(|| format!("Agent '{}' not found", agent_id))?;
if let Some(true) = agent_config.holo_remote_key {
return Err("agent is holo_remote, no keystore".to_string());
}
let mut keystore = match agent_config.test_agent {
Some(true) => test_keystore(&agent_config.name),
_ => {
let keystore_file_path = PathBuf::from(agent_config.keystore_file.clone());
let keystore = Arc::get_mut(&mut self.key_loader).unwrap()(
&keystore_file_path,
self.passphrase_manager.clone(),
self.hash_config.clone(),
)
.map_err(|_| {
HolochainError::ConfigError(format!(
"Could not load keystore \"{}\"",
agent_config.keystore_file,
))
})?;
keystore
}
};
let keybundle = keystore
.get_keybundle(PRIMARY_KEYBUNDLE_ID)
.map_err(|err| format!("{}", err,))?;
if let Some(true) = agent_config.test_agent {
} else {
if agent_config.public_address != keybundle.get_id() {
return Err(format!(
"Key from file '{}' ('{}') does not match public address {} mentioned in config!",
agent_config.keystore_file,
keybundle.get_id(),
agent_config.public_address,
));
}
}
self.agent_keys
.insert(agent_id.clone(), Arc::new(Mutex::new(keystore)));
}
let keystore_ref = self.agent_keys.get(agent_id).unwrap();
Ok(keystore_ref.clone())
}
pub fn get_keybundle_for_agent(
&mut self,
agent_id: &String,
) -> Result<Arc<Mutex<KeyBundle>>, String> {
let keystore = self.get_keystore_for_agent(agent_id)?;
let mut keystore = keystore.lock().unwrap();
let keybundle = keystore
.get_keybundle(PRIMARY_KEYBUNDLE_ID)
.map_err(|err| format!("{}", err))?;
Ok(Arc::new(Mutex::new(keybundle)))
}
fn start_interface(&mut self, config: &InterfaceConfiguration) -> Result<(), String> {
if self.interface_threads.contains_key(&config.id) {
return Err(format!("Interface {} already started!", config.id));
}
notify(format!("Starting interface '{}'.", config.id));
let handle = self.spawn_interface_thread(config.clone());
self.interface_threads.insert(config.id.clone(), handle);
Ok(())
}
pub fn load_dna(file: &PathBuf) -> HcResult<Dna> {
notify(format!("Reading DNA from {}", file.display()));
let mut f = File::open(file)?;
let mut contents = String::new();
f.read_to_string(&mut contents)?;
let dna: Dna = Dna::try_from(JsonString::from_json(&contents))?;
dna.verify()?;
Ok(dna)
}
fn load_key(
file: &PathBuf,
passphrase_manager: Arc<PassphraseManager>,
hash_config: Option<PwHashConfig>,
) -> Result<Keystore, HolochainError> {
notify(format!("Reading keystore from {}", file.display()));
let keystore = Keystore::new_from_file(file.clone(), passphrase_manager, hash_config)?;
Ok(keystore)
}
fn copy_ui_dir(source: &PathBuf, dest: &PathBuf) -> Result<(), HolochainError> {
notify(format!(
"Copying UI from {} to {}",
source.display(),
dest.display()
));
fs::create_dir_all(dest).map_err(|_| {
HolochainError::ErrorGeneric(format!("Could not directory structure {:?}", dest))
})?;
fs_extra::dir::copy(&source, &dest, &fs_extra::dir::CopyOptions::new())
.map_err(|e| HolochainError::ErrorGeneric(e.to_string()))?;
Ok(())
}
fn make_interface_handler(&self, interface_config: &InterfaceConfiguration) -> IoHandler {
let mut conductor_api_builder = ConductorApiBuilder::new();
for instance_ref_config in interface_config.instances.iter() {
let id = &instance_ref_config.id;
let name = instance_ref_config.alias.as_ref().unwrap_or(id).clone();
let instance = self.instances.get(id);
let instance_config = self.config.instance_by_id(id);
if instance.is_none() || instance_config.is_none() {
continue;
}
let instance = instance.unwrap();
let instance_config = instance_config.unwrap();
conductor_api_builder = conductor_api_builder
.with_named_instance(name.clone(), instance.clone())
.with_named_instance_config(name.clone(), instance_config)
}
if interface_config.admin {
conductor_api_builder = conductor_api_builder
.with_admin_dna_functions()
.with_admin_ui_functions()
.with_test_admin_functions()
.with_debug_functions();
}
conductor_api_builder.spawn()
}
fn spawn_interface_thread(&self, interface_config: InterfaceConfiguration) -> Sender<()> {
let dispatcher = self.make_interface_handler(&interface_config);
let (kill_switch_tx, kill_switch_rx) = unbounded();
let (broadcaster, _handle) = run_interface(&interface_config, dispatcher, kill_switch_rx)
.map_err(|error| {
error!(
"conductor: Error running interface, even after {} attempts '{}': {}",
INTERFACE_CONNECT_ATTEMPTS_MAX, interface_config.id, error
);
error
})
.unwrap();
debug!("conductor: adding broadcaster to map {:?}", broadcaster);
{
self.interface_broadcasters
.write()
.unwrap()
.insert(interface_config.id, broadcaster);
}
kill_switch_tx
}
pub fn dna_dir_path(&self) -> PathBuf {
self.config.persistence_dir.join("dna")
}
pub fn config_path(&self) -> PathBuf {
self.config.persistence_dir.join("conductor-config.toml")
}
pub fn instance_storage_dir_path(&self) -> PathBuf {
self.config.persistence_dir.join("storage")
}
pub fn save_config(&self) -> Result<(), HolochainError> {
fs::create_dir_all(&self.config.persistence_dir).map_err(|_| {
HolochainError::ErrorGeneric(format!(
"Could not directory structure {:?}",
self.config.persistence_dir
))
})?;
let mut file = File::create(&self.config_path()).map_err(|_| {
HolochainError::ErrorGeneric(format!(
"Could not create file at {:?}",
self.config_path()
))
})?;
file.write(serialize_configuration(&self.config)?.as_bytes())
.map_err(|_| {
HolochainError::ErrorGeneric(format!(
"Could not save config to {:?}",
self.config_path()
))
})?;
Ok(())
}
pub fn save_dna(&self, dna: &Dna) -> Result<PathBuf, HolochainError> {
let file_path = self
.dna_dir_path()
.join(dna.address().to_string())
.with_extension(DNA_EXTENSION);
fs::create_dir_all(&self.dna_dir_path())?;
self.save_dna_to(dna, file_path)
}
pub fn save_dna_to(&self, dna: &Dna, path: PathBuf) -> Result<PathBuf, HolochainError> {
let file = File::create(&path).map_err(|e| {
HolochainError::ConfigError(format!(
"Error writing DNA to {}, {}",
path.to_str().unwrap().to_string(),
e.to_string()
))
})?;
serde_json::to_writer_pretty(&file, dna)?;
Ok(path)
}
pub fn using_dpki(&self) -> bool {
self.config.dpki.is_some()
}
pub fn dpki_instance_id(&self) -> Option<String> {
match self.config.dpki {
Some(ref dpki) => Some(dpki.instance_id.clone()),
None => None,
}
}
pub fn dpki_init_params(&self) -> Option<String> {
match self.config.dpki {
Some(ref dpki) => Some(dpki.init_params.clone()),
None => None,
}
}
pub fn dpki_bootstrap(&mut self) -> Result<(), HolochainError> {
if self.using_dpki() {
notify("DPKI configured. Starting DPKI instance...".to_string());
self.start_dpki_instance()
.map_err(|err| format!("Error starting DPKI instance: {:?}", err))?;
let dpki_instance_id = self
.dpki_instance_id()
.expect("We assume there is a DPKI instance since we just started it above..");
notify(format!(
"Instance '{}' running as DPKI instance.",
dpki_instance_id
));
let instance = self.instances.get(&dpki_instance_id)?;
let hc_lock = instance.clone();
let hc_lock_inner = hc_lock;
let mut hc = hc_lock_inner.write().unwrap();
if !hc.dpki_is_initialized()? {
notify("DPKI is not initialized yet (i.e. running for the first time). Calling 'init'...".to_string());
hc.dpki_init(self.dpki_init_params().unwrap())?;
notify("DPKI initialization done!".to_string());
}
}
Ok(())
}
}
fn _make_interface(interface_config: &InterfaceConfiguration) -> Box<dyn Interface> {
use crate::interface_impls::{http::HttpInterface, websocket::WebsocketInterface};
match interface_config.driver {
InterfaceDriver::Websocket { port } => Box::new(WebsocketInterface::new(port)),
InterfaceDriver::Http { port } => Box::new(HttpInterface::new(port)),
_ => unimplemented!(),
}
}
#[allow(dead_code)]
#[holochain_tracing_macros::newrelic_autotrace(HOLOCHAIN_CONDUCTOR_LIB)]
fn with_port_heuristic<T, F: FnOnce() -> T>(
wanted_port: u16,
find_free_port: bool,
f: F,
) -> Result<T, HolochainError> {
let port = if find_free_port {
get_free_port(wanted_port..MAX_DYNAMIC_PORT).ok_or_else(|| {
HolochainError::InitializationFailed(String::from("Couldn't find free port"))
})?
} else {
wanted_port
};
Ok(try_with_port(port, f))
}
#[holochain_tracing_macros::newrelic_autotrace(HOLOCHAIN_CONDUCTOR_LIB)]
fn run_interface(
interface_config: &InterfaceConfiguration,
handler: IoHandler,
kill_switch: Receiver<()>,
) -> Result<(Broadcaster, thread::JoinHandle<()>), String> {
use crate::interface_impls::{http::HttpInterface, websocket::WebsocketInterface};
match interface_config.driver {
InterfaceDriver::Websocket { port } => {
let port = if interface_config.choose_free_port.unwrap_or(false) {
0
} else {
port
};
let mut interface = WebsocketInterface::new(port);
let r = interface.run(handler, kill_switch);
let addr = interface
.bound_address()
.expect("Could not bind interface to address");
println!(
"{}",
magic_port_binding_string(&interface_config.id, addr.port())
);
r
}
InterfaceDriver::Http { port } => {
let port = if interface_config.choose_free_port.unwrap_or(false) {
0
} else {
port
};
let mut interface = HttpInterface::new(port);
let r = interface.run(handler, kill_switch);
let addr = interface
.bound_address()
.expect("Could not bind interface to address");
println!(
"{}",
magic_port_binding_string(&interface_config.id, addr.port())
);
r
}
_ => unimplemented!(),
}
}
#[derive(Clone, Debug)]
struct NullLogger {}
impl Logger for NullLogger {
fn log(&mut self, _msg: String) {}
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::{
conductor::{passphrase_manager::PassphraseManager, test_admin::ConductorTestAdmin},
config::load_configuration,
key_loaders::mock_passphrase_manager,
keystore::{test_hash_config, Keystore, Secret, PRIMARY_KEYBUNDLE_ID},
};
use holochain_core::{
action::Action, nucleus::actions::call_zome_function::make_cap_request_for_call,
signal::signal_channel,
};
use holochain_core_types::dna;
use holochain_dpki::{key_bundle::KeyBundle, password_encryption::PwHashConfig, SEED_SIZE};
use holochain_persistence_api::cas::content::Address;
use holochain_wasm_utils::wasm_target_dir;
use lib3h_sodium::secbuf::SecBuf;
use std::{
fs::{File, OpenOptions},
io::Write,
path::PathBuf,
};
use tempfile;
use self::tempfile::tempdir;
use holochain_core_types::dna::{
bridges::{Bridge, BridgeReference},
fn_declarations::{FnDeclaration, Trait, TraitFns},
};
use std::collections::BTreeMap;
use test_utils::*;
pub fn test_dna_loader() -> DnaLoader {
let loader = Box::new(|path: &PathBuf| {
Ok(match path.to_str().unwrap().as_ref() {
"bridge/callee.dna" => callee_dna(),
"bridge/caller.dna" => caller_dna(),
"bridge/caller_dna_ref.dna" => caller_dna_with_dna_reference(),
"bridge/caller_bogus_trait_ref.dna" => caller_dna_with_bogus_trait_reference(),
"bridge/caller_without_required.dna" => caller_dna_without_required(),
_ => Dna::try_from(JsonString::from_json(&example_dna_string())).unwrap(),
})
})
as Box<dyn FnMut(&PathBuf) -> Result<Dna, HolochainError> + Send + Sync>;
Arc::new(loader)
}
pub fn test_key_loader() -> KeyLoader {
let loader = Box::new(
|path: &PathBuf, _pm: Arc<PassphraseManager>, _hash_config: Option<PwHashConfig>| {
match path.to_str().unwrap().as_ref() {
"holo_tester1.key" => Ok(test_keystore(1)),
"holo_tester2.key" => Ok(test_keystore(2)),
"holo_tester3.key" => Ok(test_keystore(3)),
unknown => Err(HolochainError::ErrorGeneric(format!(
"No test keystore for {}",
unknown
))),
}
},
)
as Box<
dyn FnMut(
&PathBuf,
Arc<PassphraseManager>,
Option<PwHashConfig>,
) -> Result<Keystore, HolochainError>
+ Send
+ Sync,
>;
Arc::new(loader)
}
pub fn test_keystore(index: u8) -> Keystore {
let agent_name = format!("test-agent-{}", index);
let mut keystore = Keystore::new(
mock_passphrase_manager(agent_name.clone()),
test_hash_config(),
)
.unwrap();
let mut seed = SecBuf::with_insecure(SEED_SIZE);
let mock_seed: Vec<u8> = (1..SEED_SIZE).map(|e| e as u8 + index).collect();
seed.write(0, mock_seed.as_slice())
.expect("SecBuf must be writeable");
let secret = Arc::new(Mutex::new(Secret::Seed(seed)));
keystore.add("root_seed", secret).unwrap();
keystore
.add_keybundle_from_seed("root_seed", PRIMARY_KEYBUNDLE_ID)
.unwrap();
keystore
}
pub fn test_keybundle(index: u8) -> KeyBundle {
let mut keystore = test_keystore(index);
keystore.get_keybundle(PRIMARY_KEYBUNDLE_ID).unwrap()
}
pub fn test_toml(websocket_port: u16, http_port: u16) -> String {
format!(
r#"
[[agents]]
id = "test-agent-1"
name = "Holo Tester 1"
public_address = "{tkb1}"
keystore_file = "holo_tester1.key"
[[agents]]
id = "test-agent-2"
name = "Holo Tester 2"
public_address = "{tkb2}"
keystore_file = "holo_tester2.key"
[[agents]]
id = "test-agent-3"
name = "Holo Tester 3"
public_address = "{tkb3}"
keystore_file = "holo_tester3.key"
[[dnas]]
id = "test-dna"
file = "app_spec.dna.json"
hash = "QmaJiTs75zU7kMFYDkKgrCYaH8WtnYNkmYX3tPt7ycbtRq"
[[dnas]]
id = "bridge-callee"
file = "bridge/callee.dna"
hash = "{bridge_callee_hash}"
[[dnas]]
id = "bridge-caller"
file = "bridge/caller.dna"
hash = "{bridge_caller_hash}"
[[instances]]
id = "test-instance-1"
dna = "bridge-callee"
agent = "test-agent-1"
[instances.storage]
type = "memory"
[[instances]]
id = "test-instance-2"
dna = "test-dna"
agent = "test-agent-2"
[instances.storage]
type = "memory"
[[instances]]
id = "bridge-caller"
dna = "bridge-caller"
agent = "test-agent-3"
[instances.storage]
type = "memory"
[[interfaces]]
id = "test-interface-1"
admin = true
[interfaces.driver]
type = "websocket"
port = {ws_port}
[[interfaces.instances]]
id = "test-instance-1"
[[interfaces.instances]]
id = "test-instance-2"
[[interfaces]]
id = "test-interface-2"
[interfaces.driver]
type = "http"
port = {http_port}
[[interfaces.instances]]
id = "test-instance-1"
[[interfaces.instances]]
id = "test-instance-2"
[[bridges]]
caller_id = "bridge-caller"
callee_id = "test-instance-1"
handle = "DPKI"
[[bridges]]
caller_id = "bridge-caller"
callee_id = "test-instance-2"
handle = "happ-store"
[[bridges]]
caller_id = "bridge-caller"
callee_id = "test-instance-1"
handle = "test-callee"
"#,
tkb1 = test_keybundle(1).get_id(),
tkb2 = test_keybundle(2).get_id(),
tkb3 = test_keybundle(3).get_id(),
ws_port = websocket_port,
http_port = http_port,
bridge_callee_hash = callee_dna().address(),
bridge_caller_hash = caller_dna().address(),
)
}
pub fn test_conductor(websocket_port: u16, http_port: u16) -> Conductor {
let config =
load_configuration::<Configuration>(&test_toml(websocket_port, http_port)).unwrap();
let mut conductor = Conductor::from_config(config.clone());
conductor.dna_loader = test_dna_loader();
conductor.key_loader = test_key_loader();
conductor.boot_from_config().unwrap();
conductor
}
fn test_conductor_with_signals(signal_tx: SignalSender) -> Conductor {
let config = load_configuration::<Configuration>(&test_toml(8888, 8889)).unwrap();
let mut conductor = Conductor::from_config(config.clone()).with_signal_channel(signal_tx);
conductor.dna_loader = test_dna_loader();
conductor.key_loader = test_key_loader();
conductor.boot_from_config().unwrap();
conductor
}
pub fn example_dna_string() -> String {
r#"{
"name": "my dna",
"description": "",
"version": "",
"uuid": "00000000-0000-0000-0000-000000000001",
"dna_spec_version": "2.0",
"properties": {},
"zomes": {
"": {
"description": "",
"config": {},
"entry_types": {
"": {
"description": "",
"sharing": "public"
}
},
"traits": {
"test": {
"functions": ["test"]
}
},
"fn_declarations": [
{
"name": "test",
"inputs": [
{
"name": "post",
"type": "string"
}
],
"outputs" : [
{
"name": "hash",
"type": "string"
}
]
}
],
"code": {
"code": "AAECAw=="
},
"bridges": [
{
"presence": "optional",
"handle": "my favourite instance!",
"reference": {
"traits": {}
}
}
]
}
}
}"#
.to_string()
}
#[test]
fn test_default_dna_loader() {
let tempdir = tempdir().unwrap();
let file_path = tempdir.path().join("test.dna.json");
let mut tmp_file = File::create(file_path.clone()).unwrap();
writeln!(tmp_file, "{}", example_dna_string()).unwrap();
match Conductor::load_dna(&file_path) {
Ok(dna) => {
assert_eq!(dna.name, "my dna");
}
Err(_) => assert!(false),
}
}
#[test]
fn test_conductor_boot_from_config() {
let mut conductor = test_conductor(10001, 10002);
assert_eq!(conductor.instances.len(), 3);
conductor.start_all_instances().unwrap();
conductor.start_all_interfaces();
conductor.stop_all_instances().unwrap();
}
#[test]
fn test_check_dna_consistency() {
let toml = test_toml(10041, 10042);
let config = load_configuration::<Configuration>(&toml).unwrap();
let mut conductor = Conductor::from_config(config.clone());
conductor.dna_loader = test_dna_loader();
conductor.key_loader = test_key_loader();
assert_eq!(
conductor.boot_from_config(),
Ok(()),
"Conductor failed to boot from config"
);
let a = HashString::from("QmYRM4rh8zmSLaxyShYtv9PBDdQkXuyPieJTZ1e5GZqeeh");
let b = HashString::from("QmYRM4rh8zmSLaxyShYtv9PBDdQkXuyPieJTZ1e5GZqeeh");
assert_eq!(
Conductor::check_dna_consistency(&a, &b),
Ok(()),
"DNA consistency check Fail."
);
let b = HashString::from("QmQVLgFxUpd1ExVkBzvwASshpG6fmaJGxDEgf1cFf7S73a");
assert_ne!(
Conductor::check_dna_consistency(&a, &b),
Ok(()),
"DNA consistency check Fail."
);
}
#[test]
fn test_check_dna_consistency_err() {
let a = HashString::from("QmYRM4rh8zmSLaxyShYtv9PBDdQkXuyPieJTZ1e5GZqeeh");
let b = HashString::from("QmZAQkpkXhfRcSgBJX4NYyqWCyMnkvuF7X2RkPgqihGMrR");
assert_eq!(
Conductor::check_dna_consistency(&a, &b),
Err(HolochainError::DnaHashMismatch(a, b)),
"DNA consistency check Fail."
);
let a = HashString::from("QmYRM4rh8zmSLaxyShYtv9PBDdQkXuyPieJTZ1e5GZqeeh");
let b = HashString::from(String::default());
assert_eq!(
Conductor::check_dna_consistency(&a, &b),
Err(HolochainError::DnaHashMismatch(a, b)),
"DNA consistency check Fail."
)
}
#[test]
fn test_serialize_and_load_with_test_agents() {
let mut conductor = test_conductor(10091, 10092);
conductor
.add_test_agent("test-agent-id".into(), "test-agent-name".into())
.expect("could not add test agent");
let config_toml_string =
serialize_configuration(&conductor.config()).expect("Could not serialize config");
let serialized_config = load_configuration::<Configuration>(&config_toml_string)
.expect("Could not deserialize toml");
let mut reanimated_conductor = Conductor::from_config(serialized_config);
reanimated_conductor.dna_loader = test_dna_loader();
reanimated_conductor.key_loader = test_key_loader();
assert_eq!(
reanimated_conductor
.config()
.agents
.iter()
.filter_map(|agent| agent.test_agent)
.count(),
1
);
reanimated_conductor
.boot_from_config()
.expect("Could not boot the conductor with test agent")
}
#[test]
fn test_check_dna_consistency_from_dna_file() {
let fixture = String::from(
r#"{
"name": "my dna",
"description": "",
"version": "",
"uuid": "00000000-0000-0000-0000-000000000001",
"dna_spec_version": "2.0",
"properties": {},
"zomes": {
"": {
"description": "",
"config": {},
"entry_types": {
"": {
"description": "",
"sharing": "public"
}
},
"traits": {
"test": {
"functions": ["test"]
}
},
"fn_declarations": [
{
"name": "test",
"inputs": [
{
"name": "post",
"type": "string"
}
],
"outputs" : [
{
"name": "hash",
"type": "string"
}
]
}
],
"code": {
"code": "AAECAw=="
}
}
}
}"#,
);
let dna_hash_from_file = HashString::from(
Dna::try_from(JsonString::from_json(&fixture))
.expect(&format!("Fail to load DNA from raw string: {}", fixture))
.address(),
);
let dna_hash_computed = HashString::from("QmNPCDBhr6BDBBVWG4mBEVFfhyjsScURYdZoV3fDpzjzgb");
assert_eq!(
Conductor::check_dna_consistency(&dna_hash_from_file, &dna_hash_computed),
Ok(()),
"DNA consistency from DNA file check Fail."
);
}
fn _test_conductor_save_and_load_config_default_location() {
let conductor = test_conductor(10011, 10012);
assert_eq!(conductor.save_config(), Ok(()));
let mut toml = String::new();
let mut file = OpenOptions::new()
.read(true)
.open(&conductor.config_path())
.expect("Could not open config file");
file.read_to_string(&mut toml)
.expect("Could not read config file");
let restored_config =
load_configuration::<Configuration>(&toml).expect("could not load config");
assert_eq!(
serialize_configuration(&conductor.config),
serialize_configuration(&restored_config)
)
}
#[test]
fn test_conductor_signal_handler() {
let (signal_tx, signal_rx) = signal_channel();
let mut conductor = test_conductor_with_signals(signal_tx);
conductor.start_signal_multiplexer();
test_utils::expect_action(&signal_rx, |action| match action {
Action::InitializeChain(_) => true,
_ => false,
})
.unwrap();
test_utils::expect_action(&signal_rx, |action| match action {
Action::InitNetwork(_) => true,
_ => false,
})
.unwrap();
test_utils::expect_action(&signal_rx, |action| match action {
Action::InitNetwork(_) => true,
_ => false,
})
.unwrap();
}
pub fn callee_wat() -> String {
r#"
(module
(memory 1)
(export "memory" (memory 0))
(func
(export "__hdk_validate_app_entry")
(param $allocation i64)
(result i64)
(i64.const 0)
)
(func
(export "__hdk_validate_agent_entry")
(param $allocation i64)
(result i64)
(i64.const 0)
)
(func
(export "__hdk_validate_link")
(param $allocation i64)
(result i64)
(i64.const 0)
)
(func
(export "__hdk_get_validation_package_for_entry_type")
(param $allocation i64)
(result i64)
;; This writes "Entry" into memory
(i64.store (i32.const 0) (i64.const 34))
(i64.store (i32.const 1) (i64.const 69))
(i64.store (i32.const 2) (i64.const 110))
(i64.store (i32.const 3) (i64.const 116))
(i64.store (i32.const 4) (i64.const 114))
(i64.store (i32.const 5) (i64.const 121))
(i64.store (i32.const 6) (i64.const 34))
(i64.const 7)
)
(func
(export "__hdk_get_validation_package_for_link")
(param $allocation i64)
(result i64)
;; This writes "Entry" into memory
(i64.store (i32.const 0) (i64.const 34))
(i64.store (i32.const 1) (i64.const 69))
(i64.store (i32.const 2) (i64.const 110))
(i64.store (i32.const 3) (i64.const 116))
(i64.store (i32.const 4) (i64.const 114))
(i64.store (i32.const 5) (i64.const 121))
(i64.store (i32.const 6) (i64.const 34))
(i64.const 7)
)
(func
(export "__list_traits")
(param $allocation i64)
(result i64)
(i64.const 0)
)
(func
(export "__list_functions")
(param $allocation i64)
(result i64)
(i64.const 0)
)
(func
(export "hello")
(param $allocation i64)
(result i64)
;; This writes "Holo World" into memory
(i64.store (i32.const 0) (i64.const 72))
(i64.store (i32.const 1) (i64.const 111))
(i64.store (i32.const 2) (i64.const 108))
(i64.store (i32.const 3) (i64.const 111))
(i64.store (i32.const 4) (i64.const 32))
(i64.store (i32.const 5) (i64.const 87))
(i64.store (i32.const 6) (i64.const 111))
(i64.store (i32.const 7) (i64.const 114))
(i64.store (i32.const 8) (i64.const 108))
(i64.store (i32.const 9) (i64.const 100))
(i64.const 10)
)
)
"#
.to_string()
}
fn bridge_call_fn_declaration() -> FnDeclaration {
FnDeclaration {
name: String::from("hello"),
inputs: vec![],
outputs: vec![dna::fn_declarations::FnParameter {
name: String::from("greeting"),
parameter_type: String::from("String"),
}],
}
}
fn callee_dna() -> Dna {
let wat = &callee_wat();
let mut dna = create_test_dna_with_wat("greeter", Some(wat));
dna.uuid = String::from("basic_bridge_call");
let fn_declaration = bridge_call_fn_declaration();
{
let zome = dna.zomes.get_mut("greeter").unwrap();
zome.fn_declarations.push(fn_declaration.clone());
zome.traits
.get_mut("hc_public")
.unwrap()
.functions
.push(fn_declaration.name.clone());
zome.traits.insert(
String::from("greetable"),
TraitFns {
functions: vec![fn_declaration.name.clone()],
},
);
}
dna
}
fn caller_dna() -> Dna {
let mut path = PathBuf::new();
path.push(wasm_target_dir(
&String::from("conductor_lib").into(),
&String::from("test-bridge-caller").into(),
));
let wasm_path_component: PathBuf = [
String::from("wasm32-unknown-unknown"),
String::from("release"),
String::from("test_bridge_caller.wasm"),
]
.iter()
.collect();
path.push(wasm_path_component);
let wasm = create_wasm_from_file(&path);
let defs = create_test_defs_with_fn_names(vec![
"call_bridge".to_string(),
"call_bridge_error".to_string(),
]);
let mut dna = create_test_dna_with_defs("test_zome", defs, &wasm);
dna.uuid = String::from("basic_bridge_call");
{
let zome = dna.zomes.get_mut("test_zome").unwrap();
zome.bridges.push(Bridge {
presence: BridgePresence::Required,
handle: String::from("test-callee"),
reference: BridgeReference::Trait {
traits: btreemap! {
String::from("greetable") => Trait{
functions: vec![bridge_call_fn_declaration()]
}
},
},
});
zome.bridges.push(Bridge {
presence: BridgePresence::Optional,
handle: String::from("DPKI"),
reference: BridgeReference::Trait {
traits: BTreeMap::new(),
},
});
zome.bridges.push(Bridge {
presence: BridgePresence::Optional,
handle: String::from("happ-store"),
reference: BridgeReference::Trait {
traits: BTreeMap::new(),
},
});
}
dna
}
#[test]
fn basic_bridge_call_roundtrip() {
let config = load_configuration::<Configuration>(&test_toml(10021, 10022)).unwrap();
let mut conductor = Conductor::from_config(config.clone());
conductor.dna_loader = test_dna_loader();
conductor.key_loader = test_key_loader();
conductor
.boot_from_config()
.expect("Test config must be sane");
conductor
.start_all_instances()
.expect("Instances must be spawnable");
let caller_instance = conductor.instances["bridge-caller"].clone();
let instance = caller_instance.write().unwrap();
let cap_call = {
let context = instance.context().unwrap();
make_cap_request_for_call(
context.clone(),
Address::from(context.clone().agent_id.address()),
"call_bridge",
JsonString::empty_object(),
)
};
let result = Holochain::call_zome_function(
instance.context().unwrap(),
"test_zome",
cap_call,
"call_bridge",
"{}",
)
.unwrap();
assert_eq!(result, JsonString::from("Holo World"));
}
#[test]
fn basic_bridge_call_error() {
let config = load_configuration::<Configuration>(&test_toml(10041, 10042)).unwrap();
let mut conductor = Conductor::from_config(config.clone());
conductor.dna_loader = test_dna_loader();
conductor.key_loader = test_key_loader();
conductor
.boot_from_config()
.expect("Test config must be sane");
conductor
.start_all_instances()
.expect("Instances must be spawnable");
let caller_instance = conductor.instances["bridge-caller"].clone();
let instance = caller_instance.write().unwrap();
let cap_call = {
let context = instance.context().unwrap();
make_cap_request_for_call(
context.clone(),
Address::from(context.clone().agent_id.address()),
"call_bridge_error",
JsonString::empty_object(),
)
};
let result = Holochain::call_zome_function(
instance.context().unwrap(),
"test_zome",
cap_call,
"call_bridge_error",
"{}",
);
assert!(result.is_ok());
assert!(result
.unwrap()
.to_string()
.contains("Zome function \'non-existent-function\' not found in Zome \'greeter\'"));
}
#[test]
fn error_if_required_bridge_missing() {
let mut config = load_configuration::<Configuration>(&test_toml(10061, 10062)).unwrap();
config.bridges.clear();
let mut conductor = Conductor::from_config(config.clone());
conductor.dna_loader = test_dna_loader();
conductor.key_loader = test_key_loader();
let result = conductor.boot_from_config();
assert!(result.is_err());
assert_eq!(
result.err().unwrap(),
"Required bridge \'test-callee\' for instance \'bridge-caller\' missing",
);
}
fn caller_dna_with_dna_reference() -> Dna {
let mut dna = caller_dna();
{
let bridge = dna
.zomes
.get_mut("test_zome")
.unwrap()
.bridges
.get_mut(0)
.unwrap();
bridge.reference = BridgeReference::Address {
dna_address: Address::from("fake bridge reference"),
};
}
dna
}
fn caller_dna_with_bogus_trait_reference() -> Dna {
let mut dna = caller_dna();
{
let bridge = dna
.zomes
.get_mut("test_zome")
.unwrap()
.bridges
.get_mut(0)
.unwrap();
let mut fn_declaration = bridge_call_fn_declaration();
fn_declaration
.inputs
.push(dna::fn_declarations::FnParameter {
name: String::from("additional_parameter"),
parameter_type: String::from("String"),
});
bridge.reference = BridgeReference::Trait {
traits: btreemap! {
String::from("greetable") => Trait{
functions: vec![fn_declaration]
}
},
};
}
dna
}
fn caller_dna_without_required() -> Dna {
let mut dna = caller_dna();
{
let bridge = dna
.zomes
.get_mut("test_zome")
.unwrap()
.bridges
.get_mut(0)
.unwrap();
bridge.presence = BridgePresence::Optional;
bridge.reference = BridgeReference::Trait {
traits: BTreeMap::new(),
};
}
dna
}
pub fn bridge_dna_ref_test_toml(caller_dna: &str, callee_dna: &str) -> String {
format!(
r#"
[[agents]]
id = "test-agent-1"
name = "Holo Tester 1"
public_address = "{}"
keystore_file = "holo_tester1.key"
[[dnas]]
id = "bridge-callee"
file = "{}"
hash = "Qm328wyq38924y"
[[dnas]]
id = "bridge-caller"
file = "{}"
hash = "Qm328wyq38924y"
[[instances]]
id = "bridge-callee"
dna = "bridge-callee"
agent = "test-agent-1"
[instances.storage]
type = "memory"
[[instances]]
id = "bridge-caller"
dna = "bridge-caller"
agent = "test-agent-1"
[instances.storage]
type = "memory"
[[bridges]]
caller_id = "bridge-caller"
callee_id = "bridge-callee"
handle = "test-callee"
"#,
test_keybundle(1).get_id(),
callee_dna,
caller_dna,
)
}
#[test]
fn error_if_bridge_reference_dna_mismatch() {
let config = load_configuration::<Configuration>(&bridge_dna_ref_test_toml(
"bridge/caller_dna_ref.dna",
"bridge/callee_dna.dna",
))
.unwrap();
let mut conductor = Conductor::from_config(config.clone());
conductor.dna_loader = test_dna_loader();
conductor.key_loader = test_key_loader();
let result = conductor.boot_from_config();
assert!(result.is_err());
println!("{:?}", result);
assert!(result.err().unwrap().starts_with(
"Bridge \'test-callee\' of caller instance \'bridge-caller\' requires callee to be DNA with hash \'fake bridge reference\', but the configured instance \'bridge-callee\' runs DNA with hash"
));
}
#[test]
fn error_if_bridge_reference_trait_mismatch() {
let config = load_configuration::<Configuration>(&bridge_dna_ref_test_toml(
"bridge/caller_bogus_trait_ref.dna",
"bridge/callee_dna.dna",
))
.unwrap();
let mut conductor = Conductor::from_config(config.clone());
conductor.dna_loader = test_dna_loader();
conductor.key_loader = test_key_loader();
let result = conductor.boot_from_config();
assert!(result.is_err());
println!("{:?}", result);
assert_eq!(
result.err().unwrap(),
"Bridge \'test-callee\' of instance \'bridge-caller\' requires callee to to implement trait \'greetable\' with functions: [FnDeclaration { name: \"hello\", inputs: [FnParameter { parameter_type: \"String\", name: \"additional_parameter\" }], outputs: [FnParameter { parameter_type: \"String\", name: \"greeting\" }] }]",
);
}
#[test]
fn fails_if_key_address_does_not_match() {
let config = load_configuration::<Configuration>(r#"
[[agents]]
id = "test-agent-1"
name = "Holo Tester 1"
public_address = "HoloTester1-----------------------------------------------------------------------AAACZp4xHB"
keystore_file = "holo_tester1.key"
[[dnas]]
id = "test-dna"
file = "app_spec.dna.json"
hash = "QmZAQkpkXhfRcSgBJX4NYyqWCyMnkvuF7X2RkPgqihGMrR"
[[instances]]
id = "test-instance-1"
dna = "test-dna"
agent = "test-agent-1"
[instances.storage]
type = "memory"
"#
).unwrap();
let mut conductor = Conductor::from_config(config.clone());
conductor.dna_loader = test_dna_loader();
conductor.key_loader = test_key_loader();
assert_eq!(
conductor.boot_from_config(),
Err("Error while trying to create instance \"test-instance-1\": Key from file \'holo_tester1.key\' (\'HcSCI7T6wQ5t4nffbjtUk98Dy9fa79Ds6Uzg8nZt8Fyko46ikQvNwfoCfnpuy7z\') does not match public address HoloTester1-----------------------------------------------------------------------AAACZp4xHB mentioned in config!"
.to_string()),
);
}
#[test]
#[cfg(feature = "broken-tests")]
fn test_signals_through_admin_websocket() {
let mut conductor = test_conductor(10031, 10032);
let _ = conductor.start_all_instances();
conductor.start_all_interfaces();
thread::sleep(Duration::from_secs(2));
let signals: Arc<parking_lot::Mutex<Vec<String>>> =
Arc::new(parking_lot::Mutex::new(Vec::new()));
let signals_clone = signals.clone();
let websocket_thread = thread::spawn(|| {
connect("ws://127.0.0.1:10031", move |_| {
let s = signals_clone.clone();
move |msg: Message| {
s.lock().push(msg.to_string());
Ok(())
}
})
.unwrap();
});
let result = {
let lock = conductor.instances.get("bridge-caller").unwrap();
let mut bridge_caller = lock.write().unwrap();
let cap_call = {
let context = bridge_caller.context();
make_cap_request_for_call(
context.clone(),
Address::from(context.clone().agent_id.address()),
"call_bridge",
JsonString::empty_object(),
)
};
bridge_caller.call(
"test_zome",
cap_call,
"call_bridge",
&JsonString::empty_object().to_string(),
)
};
assert!(result.is_ok());
thread::sleep(Duration::from_secs(2));
conductor.stop_all_interfaces();
websocket_thread
.join()
.expect("Could not join websocket thread");
let received_signals = signals.lock().clone();
assert!(received_signals.len() >= 3);
assert!(received_signals[0]
.starts_with("{\"signal\":{\"Trace\":\"SignalZomeFunctionCall(ZomeFnCall {"));
assert!(received_signals[1]
.starts_with("{\"signal\":{\"Trace\":\"SignalZomeFunctionCall(ZomeFnCall {"));
assert!(received_signals[2].starts_with(
"{\"signal\":{\"Trace\":\"ReturnZomeFunctionResult(ExecuteZomeFnResponse {"
));
}
#[test]
fn test_start_stop_instance() {
let mut conductor = test_conductor(10051, 10052);
assert_eq!(
conductor.start_instance(&String::from("test-instance-1")),
Ok(()),
);
assert_eq!(
conductor.start_instance(&String::from("test-instance-1")),
Err(HolochainInstanceError::InstanceAlreadyActive),
);
assert_eq!(
conductor.start_instance(&String::from("non-existant-id")),
Err(HolochainInstanceError::NoSuchInstance),
);
assert_eq!(
conductor.stop_instance(&String::from("test-instance-1")),
Ok(())
);
assert_eq!(
conductor.stop_instance(&String::from("test-instance-1")),
Err(HolochainInstanceError::InstanceNotActiveYet),
);
}
}