Skip to content

Commit 4747506

Browse files
committed
fix: load WASM routes in separate thread to avoid tokio runtime panic
1 parent 83bdfb8 commit 4747506

File tree

1 file changed

+101
-8
lines changed

1 file changed

+101
-8
lines changed

bins/validator-node/src/main.rs

Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ async fn main() -> Result<()> {
728728
if is_valid {
729729
let challenge_id_str = challenge_id.to_string();
730730
let wasm_key = StorageKey::new("wasm", &challenge_id_str);
731+
let wasm_data = data.clone(); // Clone for route loading later
731732
match storage.put(wasm_key, data, PutOptions::default()).await {
732733
Ok(metadata) => {
733734
info!(
@@ -756,13 +757,53 @@ async fn main() -> Result<()> {
756757
cs.register_wasm_challenge(wasm_config);
757758
}
758759

759-
// TODO: Route loading disabled due to tokio runtime nesting issues
760-
// Routes will be loaded on-demand when requested via RPC
761-
let _ = wasm_executor;
760+
// Load routes in a blocking thread to avoid tokio runtime issues
761+
if let Some(ref executor) = wasm_executor {
762+
let wasm_bytes = wasm_data.clone();
763+
let executor = executor.clone();
764+
let challenge_id_for_routes = challenge_id;
765+
let challenge_id_str_for_routes = challenge_id_str.clone();
766+
let chain_state_for_routes = chain_state.clone();
767+
768+
std::thread::spawn(move || {
769+
match executor.execute_get_routes_from_bytes(
770+
&challenge_id_str_for_routes,
771+
&wasm_bytes,
772+
&wasm_runtime_interface::NetworkPolicy::default(),
773+
&wasm_runtime_interface::SandboxPolicy::default(),
774+
) {
775+
Ok((routes_data, _)) => {
776+
if let Ok(routes) = bincode::deserialize::<Vec<platform_challenge_sdk_wasm::WasmRouteDefinition>>(&routes_data) {
777+
tracing::info!(
778+
challenge_id = %challenge_id_for_routes,
779+
routes_count = routes.len(),
780+
"Loaded WASM challenge routes (local upload)"
781+
);
782+
let route_infos: Vec<platform_core::ChallengeRouteInfo> = routes.iter().map(|r| {
783+
platform_core::ChallengeRouteInfo {
784+
method: r.method.clone(),
785+
path: r.path.clone(),
786+
description: r.description.clone(),
787+
requires_auth: r.requires_auth,
788+
}
789+
}).collect();
790+
let mut cs = chain_state_for_routes.write();
791+
cs.register_challenge_routes(challenge_id_for_routes, route_infos);
792+
}
793+
}
794+
Err(e) => {
795+
tracing::debug!(
796+
challenge_id = %challenge_id_for_routes,
797+
error = %e,
798+
"No routes exported by WASM module"
799+
);
800+
}
801+
}
802+
});
803+
}
762804
/*
763-
// Load and register routes using the WASM bytes we already have
805+
// Old code - kept for reference
764806
if let Some(ref executor) = wasm_executor {
765-
// Get the WASM bytes from distributed storage (just stored)
766807
let wasm_bytes_result = storage.get(
767808
&StorageKey::new("wasm", &challenge_id_str),
768809
platform_distributed_storage::GetOptions::default()
@@ -1434,9 +1475,61 @@ async fn handle_network_event(
14341475
cs.register_wasm_challenge(wasm_config);
14351476
}
14361477

1437-
// TODO: Route loading disabled due to tokio runtime nesting issues
1438-
// Routes will be loaded on-demand when requested via RPC
1439-
let _ = wasm_executor_ref;
1478+
// Load routes in a blocking thread to avoid tokio runtime issues
1479+
if let Some(ref executor) = wasm_executor_ref {
1480+
let wasm_bytes = update.data.clone();
1481+
let executor = executor.clone();
1482+
let challenge_id_for_routes = update.challenge_id;
1483+
let challenge_id_str_for_routes =
1484+
challenge_id_str.clone();
1485+
let chain_state_for_routes = chain_state.clone();
1486+
1487+
std::thread::spawn(move || {
1488+
match executor.execute_get_routes_from_bytes(
1489+
&challenge_id_str_for_routes,
1490+
&wasm_bytes,
1491+
&wasm_runtime_interface::NetworkPolicy::default(),
1492+
&wasm_runtime_interface::SandboxPolicy::default(),
1493+
) {
1494+
Ok((routes_data, _)) => {
1495+
if let Ok(routes) = bincode::deserialize::<Vec<platform_challenge_sdk_wasm::WasmRouteDefinition>>(&routes_data) {
1496+
tracing::info!(
1497+
challenge_id = %challenge_id_for_routes,
1498+
routes_count = routes.len(),
1499+
"Loaded WASM challenge routes (P2P)"
1500+
);
1501+
for route in &routes {
1502+
tracing::info!(
1503+
challenge_id = %challenge_id_for_routes,
1504+
method = %route.method,
1505+
path = %route.path,
1506+
" Route: {} {}",
1507+
route.method,
1508+
route.path
1509+
);
1510+
}
1511+
let route_infos: Vec<platform_core::ChallengeRouteInfo> = routes.iter().map(|r| {
1512+
platform_core::ChallengeRouteInfo {
1513+
method: r.method.clone(),
1514+
path: r.path.clone(),
1515+
description: r.description.clone(),
1516+
requires_auth: r.requires_auth,
1517+
}
1518+
}).collect();
1519+
let mut cs = chain_state_for_routes.write();
1520+
cs.register_challenge_routes(challenge_id_for_routes, route_infos);
1521+
}
1522+
}
1523+
Err(e) => {
1524+
tracing::debug!(
1525+
challenge_id = %challenge_id_for_routes,
1526+
error = %e,
1527+
"No routes exported by WASM module"
1528+
);
1529+
}
1530+
}
1531+
});
1532+
}
14401533
}
14411534
Err(e) => {
14421535
error!(

0 commit comments

Comments
 (0)