Skip to content

Commit 80a7a03

Browse files
committed
fix(tests): stabilize multi-gateway integration
1 parent f884c89 commit 80a7a03

File tree

3 files changed

+99
-18
lines changed

3 files changed

+99
-18
lines changed

apps/freenet-ping/app/tests/common/mod.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,23 @@ pub(crate) enum PackageType {
208208
Delegate,
209209
}
210210

211+
const CONTRACT_EXTRA_FEATURES: [&str; 1] = ["contract"];
212+
const NO_EXTRA_FEATURES: [&str; 0] = [];
213+
211214
impl PackageType {
212215
pub fn feature(&self) -> &'static str {
213216
match self {
214217
PackageType::Contract => "freenet-main-contract",
215218
PackageType::Delegate => "freenet-main-delegate",
216219
}
217220
}
221+
222+
pub fn extra_features(&self) -> &'static [&'static str] {
223+
match self {
224+
PackageType::Contract => &CONTRACT_EXTRA_FEATURES,
225+
PackageType::Delegate => &NO_EXTRA_FEATURES,
226+
}
227+
}
218228
}
219229

220230
impl std::fmt::Display for PackageType {
@@ -250,9 +260,10 @@ fn compile_options(cli_config: &BuildToolConfig) -> impl Iterator<Item = String>
250260
.iter()
251261
.flat_map(|s| {
252262
s.split(',')
253-
.filter(|p| *p != cli_config.package_type.feature())
263+
.filter(|p| *p != cli_config.package_type.feature() && *p != "contract")
254264
})
255-
.chain([cli_config.package_type.feature()]);
265+
.chain([cli_config.package_type.feature()])
266+
.chain(cli_config.package_type.extra_features().iter().copied());
256267
let features = [
257268
"--features".to_string(),
258269
feature_list.collect::<Vec<_>>().join(","),
@@ -262,7 +273,33 @@ fn compile_options(cli_config: &BuildToolConfig) -> impl Iterator<Item = String>
262273
.chain(release.iter().map(|s| s.to_string()))
263274
}
264275
// TODO: refactor so we share the implementation with fdev (need to extract to )
276+
fn ensure_target_dir_env() {
277+
if std::env::var(TARGET_DIR_VAR).is_err() {
278+
let workspace_dir = std::env::var("CARGO_WORKSPACE_DIR")
279+
.map(PathBuf::from)
280+
.unwrap_or_else(|_| find_workspace_root());
281+
let target_dir = workspace_dir.join("target");
282+
std::env::set_var(TARGET_DIR_VAR, &target_dir);
283+
}
284+
}
285+
286+
fn find_workspace_root() -> PathBuf {
287+
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
288+
manifest_dir
289+
.ancestors()
290+
.find(|dir| {
291+
let cargo_toml = dir.join("Cargo.toml");
292+
cargo_toml.exists()
293+
&& std::fs::read_to_string(&cargo_toml)
294+
.map(|contents| contents.contains("[workspace]"))
295+
.unwrap_or(false)
296+
})
297+
.expect("Could not determine workspace root from manifest directory")
298+
.to_path_buf()
299+
}
300+
265301
fn compile_contract(contract_path: &PathBuf) -> anyhow::Result<Vec<u8>> {
302+
ensure_target_dir_env();
266303
println!("module path: {contract_path:?}");
267304
let target = std::env::var(TARGET_DIR_VAR)
268305
.map_err(|_| anyhow::anyhow!("CARGO_TARGET_DIR should be set"))?;

apps/freenet-ping/app/tests/run_app.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,15 +1750,18 @@ async fn test_ping_partially_connected_network() -> TestResult {
17501750
i, NUM_GATEWAYS, num_connections);
17511751
}
17521752

1753-
// Load the ping contract
1753+
// Load the ping contract. Compile once to determine the code hash, then again with proper options.
17541754
let path_to_code = PathBuf::from(PACKAGE_DIR).join(PATH_TO_CONTRACT);
17551755
tracing::info!(path=%path_to_code.display(), "loading contract code");
1756-
let code = std::fs::read(path_to_code)
1757-
.ok()
1758-
.ok_or_else(|| anyhow!("Failed to read contract code"))?;
1759-
let code_hash = CodeHash::from_code(&code);
1760-
1761-
// Create ping contract options
1756+
let temp_options = PingContractOptions {
1757+
frequency: Duration::from_secs(3),
1758+
ttl: Duration::from_secs(60),
1759+
tag: APP_TAG.to_string(),
1760+
code_key: String::new(),
1761+
};
1762+
let temp_params = Parameters::from(serde_json::to_vec(&temp_options).unwrap());
1763+
let temp_container = common::load_contract(&path_to_code, temp_params)?;
1764+
let code_hash = CodeHash::from_code(temp_container.data());
17621765
let ping_options = PingContractOptions {
17631766
frequency: Duration::from_secs(3),
17641767
ttl: Duration::from_secs(60),
@@ -1767,7 +1770,7 @@ async fn test_ping_partially_connected_network() -> TestResult {
17671770
};
17681771

17691772
let params = Parameters::from(serde_json::to_vec(&ping_options).unwrap());
1770-
let container = ContractContainer::try_from((code, &params))?;
1773+
let container = common::load_contract(&path_to_code, params)?;
17711774
let contract_key = container.key();
17721775

17731776
// Choose a node to publish the contract

crates/core/src/node/network_bridge/p2p_protoc.rs

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,23 +1020,64 @@ impl P2pConnManager {
10201020
tracing::debug!(tx = %tx, "Blocked addresses: {:?}, peer addr: {}", blocked_addrs, peer.addr);
10211021
}
10221022
state.awaiting_connection.insert(peer.addr, callback);
1023-
let res = timeout(
1023+
match timeout(
10241024
Duration::from_secs(10),
10251025
handshake_handler_msg.establish_conn(peer.clone(), tx, is_gw),
10261026
)
10271027
.await
1028-
.inspect_err(|error| {
1029-
tracing::error!(tx = %tx, "Failed to establish connection: {:?}", error);
1030-
})?;
1031-
match res {
1032-
Ok(()) => {
1033-
tracing::debug!(tx = %tx,
1028+
{
1029+
Ok(Ok(())) => {
1030+
tracing::debug!(
1031+
tx = %tx,
10341032
"Successfully initiated connection process for peer: {:?}",
10351033
peer
10361034
);
10371035
Ok(())
10381036
}
1039-
Err(e) => Err(anyhow::Error::msg(e)),
1037+
Ok(Err(e)) => {
1038+
tracing::error!(
1039+
tx = %tx,
1040+
remote = %peer,
1041+
"Handshake handler failed while queuing connection request: {}",
1042+
e
1043+
);
1044+
if let Some(mut cb) = state.awaiting_connection.remove(&peer.addr) {
1045+
cb.send_result(Err(HandshakeError::ChannelClosed))
1046+
.await
1047+
.inspect_err(|err| {
1048+
tracing::debug!(
1049+
remote = %peer,
1050+
"Failed to notify caller about handshake failure: {:?}",
1051+
err
1052+
);
1053+
})
1054+
.ok();
1055+
}
1056+
Err(anyhow::Error::new(e))
1057+
}
1058+
Err(elapsed) => {
1059+
tracing::warn!(
1060+
tx = %tx,
1061+
remote = %peer,
1062+
elapsed = ?elapsed,
1063+
"Timed out while queuing handshake request; treating as connection failure"
1064+
);
1065+
if let Some(mut cb) = state.awaiting_connection.remove(&peer.addr) {
1066+
cb.send_result(Err(HandshakeError::ConnectionError(
1067+
ConnectionError::Timeout,
1068+
)))
1069+
.await
1070+
.inspect_err(|err| {
1071+
tracing::debug!(
1072+
remote = %peer,
1073+
"Failed to notify caller about handshake timeout: {:?}",
1074+
err
1075+
);
1076+
})
1077+
.ok();
1078+
}
1079+
Ok(())
1080+
}
10401081
}
10411082
}
10421083

0 commit comments

Comments
 (0)