Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More config fixes #1108

Merged
merged 5 commits into from
Jun 2, 2024
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
1 change: 0 additions & 1 deletion crates/core/src/client_events/combinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ impl<Fut: Future + Unpin, const N: usize> Future for SelectAll<Fut, N> {
.flatten();
match item {
Some((idx, res)) => {
eprintln!("polled {idx}");
self.inner[idx] = None;
let rest = std::mem::replace(&mut self.inner, [(); N].map(|_| None));
return Poll::Ready((res, idx, rest));
Expand Down
4 changes: 3 additions & 1 deletion crates/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub struct ConfigArgs {

/// An arbitrary identifier for the node, mostly for debugging or testing purposes.
#[clap(long)]
#[serde(skip)]
pub id: Option<String>,
}

Expand Down Expand Up @@ -518,7 +519,8 @@ impl ConfigPathsArgs {
pub fn app_data_dir(id: Option<&str>) -> std::io::Result<PathBuf> {
let project_dir = ProjectDirs::from(QUALIFIER, ORGANIZATION, APPLICATION)
.ok_or(std::io::ErrorKind::NotFound)?;
let app_data_dir: PathBuf = if cfg!(any(test, debug_assertions)) {
// if id is set, most likely we are running tests or in simulated mode
let app_data_dir: PathBuf = if cfg!(any(test, debug_assertions)) || id.is_some() {
std::env::temp_dir().join(if let Some(id) = id {
format!("freenet-{id}")
} else {
Expand Down
32 changes: 30 additions & 2 deletions crates/core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,11 +845,27 @@ impl PeerId {
}
}

thread_local! {
static PEER_ID: std::cell::RefCell<Option<TransportPublicKey>> = std::cell::RefCell::new(None);
}

#[cfg(test)]
impl<'a> arbitrary::Arbitrary<'a> for PeerId {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let addr: ([u8; 4], u16) = u.arbitrary()?;
let pub_key = TransportKeypair::new().public().clone(); // TODO: impl arbitrary for TransportPublicKey

let pub_key = PEER_ID.with(|peer_id| {
let mut peer_id = peer_id.borrow_mut();
match &*peer_id {
Some(k) => k.clone(),
None => {
let key = TransportKeypair::new().public().clone();
peer_id.replace(key.clone());
key
}
}
});

Ok(Self {
addr: addr.into(),
pub_key,
Expand All @@ -863,7 +879,19 @@ impl PeerId {
let mut addr = [0; 4];
rand::thread_rng().fill(&mut addr[..]);
let port = crate::util::get_free_port().unwrap();
let pub_key = TransportKeypair::new().public().clone();

let pub_key = PEER_ID.with(|peer_id| {
let mut peer_id = peer_id.borrow_mut();
match &*peer_id {
Some(k) => k.clone(),
None => {
let key = TransportKeypair::new().public().clone();
peer_id.replace(key.clone());
key
}
}
});

Self {
addr: (addr, port).into(),
pub_key,
Expand Down
36 changes: 25 additions & 11 deletions crates/core/src/node/network_bridge/p2p_protoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl P2pConnManager {
) -> Result<(), anyhow::Error> {
use ConnMngrActions::*;

tracing::info!(%self.listening_port, %self.is_gateway, key = ?self.key_pair.public(), "Open network listener");
tracing::info!(%self.listening_port, %self.is_gateway, key = %self.key_pair.public(), "Open network listener");

let (mut outbound_conn_handler, mut inbound_conn_handler) =
create_connection_handler::<UdpSocket>(
Expand All @@ -168,15 +168,6 @@ impl P2pConnManager {
let mut gw_inbound_pending_connections = HashSet::new();

loop {
tracing::info!(
"This peer {:?} has active connections with peers: {:?}",
self.bridge.op_manager.ring.get_peer_key().unwrap(),
gw_inbound_pending_connections
.iter()
.chain(self.connections.keys().map(|p| &p.addr))
.collect::<Vec<_>>()
);

let notification_msg = notification_channel.0.recv().map(|m| match m {
None => Ok(Right(ClosedChannel)),
Some(Left(msg)) => Ok(Left((msg, None))),
Expand Down Expand Up @@ -422,8 +413,10 @@ impl P2pConnManager {
"Established connection with peer",
);
gw_inbound_pending_connections.insert(remote_addr);

self.connections.insert(peer_id.clone(), msg_sender);
self.print_connected_peers(
gw_inbound_pending_connections.iter(),
);
} else if joiner.is_none() {
tracing::error!(
"Joiner unexpectedly not set for connection request."
Expand Down Expand Up @@ -519,6 +512,7 @@ impl P2pConnManager {
peer_conn,
})) => {
gw_inbound_pending_connections.insert(remote_addr);
self.print_connected_peers(gw_inbound_pending_connections.iter());
let (tx, rx) = mpsc::channel(10);
pending_inbound_gw_conns.insert(remote_addr, tx);
let task = peer_connection_listener(rx, peer_conn).boxed();
Expand All @@ -527,6 +521,7 @@ impl P2pConnManager {
Ok(Right(ConnectionEstablished { peer, peer_conn })) => {
let (tx, rx) = mpsc::channel(10);
self.connections.insert(peer.clone(), tx);
self.print_connected_peers(gw_inbound_pending_connections.iter());

// Spawn a task to handle the connection messages (inbound and outbound)
let task = peer_connection_listener(rx, peer_conn).boxed();
Expand Down Expand Up @@ -699,6 +694,25 @@ impl P2pConnManager {

Ok(connection.map(Either::Right).unwrap_or(Either::Left(())))
}

#[inline]
fn print_connected_peers<'a>(
&self,
gw_inbound_pending_connections: impl Iterator<Item = &'a SocketAddr>,
) {
tracing::debug!(
"This peer {:?} has active connections with peers: {:?}",
self.bridge.op_manager.ring.get_peer_key().as_ref(),
self.connections.keys().map(|p| &p.addr).collect::<Vec<_>>()
);
if self.is_gateway {
tracing::debug!(
"This gateway {:?} has pending connections with peers: {:?}",
self.bridge.op_manager.ring.get_peer_key().as_ref(),
gw_inbound_pending_connections.collect::<Vec<_>>()
);
}
}
}

enum ConnMngrActions {
Expand Down
8 changes: 6 additions & 2 deletions crates/core/src/node/testing_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ impl SimNetwork {
let id = PeerId::new((Ipv6Addr::LOCALHOST, port).into(), keypair.public().clone());
let location = Location::random();

let mut config = NodeConfig::new(ConfigArgs::default().build().unwrap());
let mut config_args = ConfigArgs::default();
config_args.id = Some(format!("{label}"));
let mut config = NodeConfig::new(config_args.build().unwrap());
config.key_pair = keypair;
config.network_listener_ip = Ipv6Addr::LOCALHOST.into();
config.network_listener_port = port;
Expand Down Expand Up @@ -444,7 +446,9 @@ impl SimNetwork {
let label = NodeLabel::node(node_no);
let peer = PeerId::random();

let mut config = NodeConfig::new(ConfigArgs::default().build().unwrap());
let mut config_args = ConfigArgs::default();
config_args.id = Some(format!("{label}"));
let mut config = NodeConfig::new(config_args.build().unwrap());
for GatewayConfig { id, location, .. } in &gateways {
config.add_gateway(InitPeerNode::new(id.clone(), *location));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/node/testing_impl/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl NetworkPeer {
.await
.expect("Failed to connect to supervisor");

let config_url = format!("http://localhost:3000/config/{}", peer_id);
let config_url = format!("http://127.0.0.1:3000/config/{}", peer_id);
let response = reqwest::get(&config_url).await?;
let peer_config = response.json::<crate::node::NodeConfig>().await?;

Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/operations/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ where
return Err(OpError::ConnError(ConnectionError::FailedConnectOp));
}
let tx_id = Transaction::new::<ConnectMsg>();
tracing::info!(%gateway.peer, ?peer_pub_key, "Attempting network join");
tracing::info!(%gateway.peer, %peer_pub_key, "Attempting network join");
let mut op = initial_request(
peer_pub_key,
gateway.clone(),
Expand Down
6 changes: 4 additions & 2 deletions crates/core/src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,8 @@ pub(super) mod test {

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn event_register_read_write() -> Result<(), DynError> {
// use tracing::level_filters::LevelFilter;
// crate::config::set_logger(Some(LevelFilter::TRACE));
use std::time::Duration;
let temp_dir = tempfile::tempdir()?;
let log_path = temp_dir.path().join("event_log");
Expand All @@ -1275,10 +1277,10 @@ pub(super) mod test {
for _ in 0..TEST_LOGS {
let tx: Transaction = gen.arbitrary()?;
transactions.push(tx);
let peer: PeerId = gen.arbitrary()?;
let peer: PeerId = PeerId::random();
peers.push(peer);
}
let mut total_route_events = 0;
let mut total_route_events: usize = 0;
for i in 0..TEST_LOGS {
let kind: EventKind = gen.arbitrary()?;
if matches!(kind, EventKind::Route(_)) {
Expand Down
16 changes: 16 additions & 0 deletions crates/core/src/transport/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ impl TransportPublicKey {
}
}

impl std::fmt::Display for TransportPublicKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let encoded = self.0.to_pkcs1_der().map_err(|_| std::fmt::Error)?;
write!(
f,
"{}",
bs58::encode(if encoded.as_bytes().len() > 16 {
&encoded.as_bytes()[..16]
} else {
encoded.as_bytes()
})
.into_string()
)
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct TransportSecretKey(RsaPrivateKey);

Expand Down
8 changes: 7 additions & 1 deletion crates/fdev/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ pub enum TestMode {
}

pub(crate) async fn test_framework(base_config: TestConfig) -> anyhow::Result<(), Error> {
let (server, changes_recorder) = if !base_config.disable_metrics {
let disable_metrics = base_config.disable_metrics || {
match &base_config.command {
TestMode::Network(config) => matches!(config.mode, network::Process::Peer),
_ => false,
}
};
let (server, changes_recorder) = if !disable_metrics {
let (s, r) = start_server(&ServerConfig {
log_directory: base_config.execution_data.clone(),
})
Expand Down
2 changes: 2 additions & 0 deletions crates/fdev/src/testing/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ async fn start_child(config: &TestConfig, cmd_config: &NetworkProcessConfig) ->
if let Some(peer_id) = &cmd_config.id {
let peer = NetworkPeer::new(peer_id.clone()).await?;
peer.run(config, peer_id.clone()).await?;
} else {
bail!("Peer id not set");
}
Ok(())
}
Expand Down
Loading