Skip to content

Commit a982a6e

Browse files
committed
fix: reload WASM routes directly from upload bytes in cache invalidator
- Pass wasm_bytes to invalidator callback so routes are reloaded immediately - Use execute_get_routes_from_bytes instead of loading from storage - Fixes routes not updating after sudo upload (P2P channel not consumed)
1 parent df94fca commit a982a6e

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

bins/validator-node/src/main.rs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -866,31 +866,46 @@ async fn main() -> Result<()> {
866866
if let Some(ref executor) = wasm_executor {
867867
let exec = Arc::clone(executor);
868868
let cs_for_invalidator = Arc::clone(&chain_state);
869-
let invalidator: Arc<dyn Fn(&str) + Send + Sync> = Arc::new(move |challenge_id: &str| {
869+
let invalidator: Arc<dyn Fn(&str, &[u8]) + Send + Sync> = Arc::new(move |challenge_id: &str, wasm_bytes: &[u8]| {
870870
exec.invalidate_cache(challenge_id);
871-
// Reload route definitions from the new WASM module
872-
let net_policy = wasm_runtime_interface::NetworkPolicy::default();
873-
let sandbox_policy = wasm_runtime_interface::SandboxPolicy::default();
874-
if let Ok((bytes, _metrics)) = exec.execute_get_routes(challenge_id, &net_policy, &sandbox_policy) {
875-
if let Ok(routes) = bincode::deserialize::<Vec<platform_challenge_sdk_wasm::WasmRouteDefinition>>(&bytes) {
876-
let route_infos: Vec<platform_core::ChallengeRouteInfo> = routes
877-
.iter()
878-
.map(|r| platform_core::ChallengeRouteInfo {
879-
method: r.method.clone(),
880-
path: r.path.clone(),
881-
description: r.description.clone(),
882-
requires_auth: r.requires_auth,
883-
})
884-
.collect();
885-
if let Ok(cid) = uuid::Uuid::parse_str(challenge_id) {
886-
let mut state = cs_for_invalidator.write();
887-
state.register_challenge_routes(
888-
platform_core::ChallengeId::from_uuid(cid),
889-
route_infos,
890-
);
891-
info!(challenge_id = challenge_id, "Reloaded WASM routes after upload");
871+
if wasm_bytes.is_empty() {
872+
return;
873+
}
874+
// Reload route definitions from the new WASM bytes
875+
match exec.execute_get_routes_from_bytes(
876+
challenge_id,
877+
wasm_bytes,
878+
&wasm_runtime_interface::NetworkPolicy::development(),
879+
&wasm_runtime_interface::SandboxPolicy::default(),
880+
) {
881+
Ok((routes_data, _)) => {
882+
if let Ok(routes) = bincode::deserialize::<Vec<platform_challenge_sdk_wasm::WasmRouteDefinition>>(&routes_data) {
883+
let route_infos: Vec<platform_core::ChallengeRouteInfo> = routes
884+
.iter()
885+
.map(|r| platform_core::ChallengeRouteInfo {
886+
method: r.method.clone(),
887+
path: r.path.clone(),
888+
description: r.description.clone(),
889+
requires_auth: r.requires_auth,
890+
})
891+
.collect();
892+
if let Ok(cid) = uuid::Uuid::parse_str(challenge_id) {
893+
let mut state = cs_for_invalidator.write();
894+
state.register_challenge_routes(
895+
platform_core::ChallengeId::from_uuid(cid),
896+
route_infos.clone(),
897+
);
898+
info!(
899+
challenge_id = challenge_id,
900+
routes_count = route_infos.len(),
901+
"Reloaded WASM routes after upload"
902+
);
903+
}
892904
}
893905
}
906+
Err(e) => {
907+
warn!(challenge_id = challenge_id, error = %e, "Failed to reload routes after upload");
908+
}
894909
}
895910
});
896911
*rpc_server.rpc_handler().wasm_cache_invalidator.write() = Some(invalidator);

crates/rpc-server/src/jsonrpc.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ pub struct RpcHandler {
194194
pub keypair: Arc<RwLock<Option<platform_core::Keypair>>>,
195195
/// Real-time weight computation handler
196196
pub get_weights_handler: Arc<RwLock<Option<GetWeightsHandler>>>,
197-
/// Callback to invalidate WASM cache for a challenge (called on wasm_upload)
198-
pub wasm_cache_invalidator: Arc<RwLock<Option<Arc<dyn Fn(&str) + Send + Sync>>>>,
197+
/// Callback to invalidate WASM cache and reload routes (called on wasm_upload)
198+
/// Args: (challenge_id, wasm_bytes)
199+
pub wasm_cache_invalidator: Arc<RwLock<Option<Arc<dyn Fn(&str, &[u8]) + Send + Sync>>>>,
199200
}
200201

201202
impl RpcHandler {

crates/rpc-server/src/server.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,10 +1238,14 @@ async fn sudo_challenge_handler(
12381238
"Challenge update broadcast initiated"
12391239
);
12401240

1241-
// Invalidate local WASM cache so new module is loaded on next request
1241+
// Invalidate local WASM cache and reload routes after upload
12421242
if request.action == "wasm_upload" {
12431243
if let Some(ref invalidator) = *handler.wasm_cache_invalidator.read() {
1244-
invalidator(&request.challenge_id);
1244+
// Decode wasm bytes again for the invalidator callback
1245+
let wasm_bytes = request.data.as_ref()
1246+
.and_then(|d| base64::Engine::decode(&base64::engine::general_purpose::STANDARD, d).ok())
1247+
.unwrap_or_default();
1248+
invalidator(&request.challenge_id, &wasm_bytes);
12451249
info!(challenge_id = %request.challenge_id, "WASM cache invalidated after upload");
12461250
}
12471251
}

0 commit comments

Comments
 (0)