Skip to content

Commit

Permalink
first compiling draft
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Jan 22, 2024
1 parent 1a7000b commit 497732e
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 134 deletions.
107 changes: 86 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions applications/minotari_console_wallet/src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

#![allow(dead_code, unused)]

use std::{fs, path::PathBuf, str::FromStr, sync::Arc};
use std::time::Instant;
use std::{fs, path::PathBuf, str::FromStr, sync::Arc, time::Instant};

use log::*;
use minotari_app_utilities::identity_management::setup_node_identity;
Expand Down Expand Up @@ -466,13 +465,6 @@ pub async fn init_wallet(
WalletError::CommsInitializationError(cie) => cie.to_exit_error(),
e => ExitError::new(ExitCode::WalletError, format!("Error creating Wallet Container: {}", e)),
})?;
// TODO: fix this
// if let Some(hs) = wallet.comms.hidden_service() {
// wallet
// .db
// .set_tor_identity(hs.tor_identity().clone())
// .map_err(|e| ExitError::new(ExitCode::WalletError, format!("Problem writing tor identity. {}", e)))?;
// }

error!(
target: LOG_TARGET,
Expand Down
21 changes: 13 additions & 8 deletions applications/minotari_node/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,20 @@ where B: BlockchainBackend + 'static

let comms = comms.add_protocol_extension(mempool_protocol);
let comms = Self::setup_rpc_services(comms, &handles, self.db.into(), &p2p_config);
let comms = initialization::spawn_comms_using_transport(comms, p2p_config.transport.clone())
.await
.map_err(|e| e.to_exit_error())?;

let comms = if p2p_config.transport.transport_type == TransportType::Tor {
let path = base_node_config.tor_identity_file.clone();
let after_comms = move |identity| {
let _result = identity_management::save_as_json(&path, &identity);
trace!(target: LOG_TARGET, "resave the chat tor identity {:?}", identity);
};
initialization::spawn_comms_using_transport(comms, p2p_config.transport.clone(), after_comms).await
} else {
let after_comms = |_identity| {};
initialization::spawn_comms_using_transport(comms, p2p_config.transport.clone(), after_comms).await
};

let comms = comms.map_err(|e| e.to_exit_error())?;
// Save final node identity after comms has initialized. This is required because the public_address can be
// changed by comms during initialization when using tor.
match p2p_config.transport.transport_type {
Expand All @@ -177,11 +187,6 @@ where B: BlockchainBackend + 'static
.map_err(|e| ExitError::new(ExitCode::IdentityError, e))?;
},
};
todo!("Fix this");
// if let Some(hs) = comms.hidden_service() {
// identity_management::save_as_json(&base_node_config.tor_identity_file, hs.tor_identity())
// .map_err(|e| ExitError::new(ExitCode::IdentityError, e))?;
// }

handles.register(comms);

Expand Down
23 changes: 13 additions & 10 deletions base_layer/contacts/src/chat_client/src/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub use tari_comms::{
multiaddr::Multiaddr,
peer_manager::{NodeIdentity, PeerFeatures},
};
use tari_comms::{peer_manager::Peer, CommsNode, UnspawnedCommsNode};
use tari_comms::{peer_manager::Peer, CommsNode, UnspawnedCommsNode};
use tari_contacts::contacts_service::{handle::ContactsServiceHandle, ContactsServiceInitializer};
use tari_p2p::{
comms_connector::pubsub_connector,
Expand Down Expand Up @@ -109,10 +109,17 @@ pub async fn start(
for peer in seed_peers {
peer_manager.add_peer(peer).await?;
}

let comms = spawn_comms_using_transport(comms, p2p_config.transport.clone()).await?;

// Save final node identity after comms has initialized. This is required because the public_address can be
let comms = if p2p_config.transport.transport_type == TransportType::Tor {
let path = config.chat_client.tor_identity_file.clone();
let after_comms = move |identity| {
let _result = identity_management::save_as_json(&path, &identity);
trace!(target: LOG_TARGET, "resave the chat tor identity {:?}", identity);
};
spawn_comms_using_transport(comms, p2p_config.transport.clone(), after_comms).await?
} else {
let after_comms = |_identity| {};
spawn_comms_using_transport(comms, p2p_config.transport.clone(), after_comms).await?
};
// changed by comms during initialization when using tor.
match p2p_config.transport.transport_type {
TransportType::Tcp => {}, // Do not overwrite TCP public_address in the base_node_id!
Expand All @@ -121,11 +128,7 @@ pub async fn start(
trace!(target: LOG_TARGET, "save chat identity file");
},
};
todo!("Fix this");
// if let Some(hs) = comms.hidden_service() {
// identity_management::save_as_json(&config.chat_client.tor_identity_file, hs.tor_identity())?;
// trace!(target: LOG_TARGET, "resave the chat tor identity {:?}", hs.tor_identity());
// }

handles.register(comms);

let comms = handles.expect_handle::<CommsNode>();
Expand Down
24 changes: 14 additions & 10 deletions base_layer/p2p/src/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ use tari_comms::{
ProtocolId,
},
tor,
tor::HiddenServiceControllerError,
transports::{predicate::FalsePredicate, MemoryTransport, SocksConfig, SocksTransport, TcpWithTorTransport},
tor::{HiddenServiceControllerError, TorIdentity},
transports::{
predicate::FalsePredicate,
HiddenServiceTransport,
MemoryTransport,
SocksConfig,
SocksTransport,
TcpWithTorTransport,
},
utils::cidr::parse_cidrs,
CommsBuilder,
CommsBuilderError,
Expand All @@ -70,7 +77,6 @@ use tari_storage::{
use thiserror::Error;
use tokio::sync::{broadcast, mpsc};
use tower::ServiceBuilder;
use tari_comms::transports::HiddenServiceTransport;

use crate::{
comms_connector::{InboundDomainConnector, PubsubDomainConnector},
Expand Down Expand Up @@ -210,9 +216,10 @@ pub async fn initialize_local_test_comms<P: AsRef<Path>>(
Ok((comms, dht, event_sender))
}

pub async fn spawn_comms_using_transport(
pub async fn spawn_comms_using_transport<F: Fn(TorIdentity) + Send + Sync + Unpin + Clone + 'static>(
comms: UnspawnedCommsNode,
transport_config: TransportConfig,
after_comms: F,
) -> Result<CommsNode, CommsInitializationError> {
let comms = match transport_config.transport_type {
TransportType::Memory => {
Expand Down Expand Up @@ -250,19 +257,16 @@ pub async fn spawn_comms_using_transport(
let tor_config = transport_config.tor;
debug!(target: LOG_TARGET, "Building TOR comms stack ({:?})", tor_config);
let listener_address_override = tor_config.listener_address_override.clone();
let mut hidden_service_ctl = initialize_hidden_service(tor_config)?;
let hidden_service_ctl = initialize_hidden_service(tor_config)?;
// Set the listener address to be the address (usually local) to which tor will forward all traffic
let instant = Instant::now();
let transport = HiddenServiceTransport::new(hidden_service_ctl);
error!(target: LOG_TARGET, "TOR transport initialized in {:.0?}", instant.elapsed());


let transport = HiddenServiceTransport::new(hidden_service_ctl, after_comms);
debug!(target: LOG_TARGET, "TOR transport initialized in {:.0?}", instant.elapsed());

comms
.with_listener_address(
listener_address_override.unwrap_or_else(|| multiaddr![Ip4([127, 0, 0, 1]), Tcp(0u16)]),
)
// .with_hidden_service_controller(hidden_service_ctl)
.spawn_with_transport(transport)
.await?
},
Expand Down
Loading

0 comments on commit 497732e

Please sign in to comment.