Skip to content

Commit df94fca

Browse files
committed
fix: invalidate WASM cache on upload + wire cache invalidator to RPC
- Always invalidate WASM cache on any ChallengeUpdate (including wasm_upload) - Add wasm_cache_invalidator callback to RpcHandler - Invalidate cache immediately in RPC upload handler (self-broadcast not received) - Fixes stale WASM module after sudo upload
1 parent b7c2a37 commit df94fca

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

bins/validator-node/src/main.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,40 @@ async fn main() -> Result<()> {
862862
rpc_server.rpc_handler().set_route_handler(handler);
863863
}
864864

865+
// Wire WASM cache invalidation callback for uploads
866+
if let Some(ref executor) = wasm_executor {
867+
let exec = Arc::clone(executor);
868+
let cs_for_invalidator = Arc::clone(&chain_state);
869+
let invalidator: Arc<dyn Fn(&str) + Send + Sync> = Arc::new(move |challenge_id: &str| {
870+
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");
892+
}
893+
}
894+
}
895+
});
896+
*rpc_server.rpc_handler().wasm_cache_invalidator.write() = Some(invalidator);
897+
}
898+
865899
// Wire real-time weight computation for subnet_getWeights RPC
866900
{
867901
let uid_map = Arc::clone(&shared_uid_map);
@@ -3064,11 +3098,9 @@ async fn handle_network_event(
30643098
}
30653099
}
30663100

3067-
// Invalidate WASM cache (skip for wasm_upload - module was just compiled fresh)
3068-
if update.update_type != "wasm_upload" {
3069-
if let Some(ref executor) = wasm_executor_ref {
3070-
executor.invalidate_cache(&challenge_id_str);
3071-
}
3101+
// Always invalidate WASM cache on any update (including wasm_upload)
3102+
if let Some(ref executor) = wasm_executor_ref {
3103+
executor.invalidate_cache(&challenge_id_str);
30723104
}
30733105
} else {
30743106
warn!(

crates/rpc-server/src/jsonrpc.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ 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>>>>,
197199
}
198200

199201
impl RpcHandler {
@@ -209,6 +211,7 @@ impl RpcHandler {
209211
broadcast_tx: Arc::new(RwLock::new(None)),
210212
keypair: Arc::new(RwLock::new(None)),
211213
get_weights_handler: Arc::new(RwLock::new(None)),
214+
wasm_cache_invalidator: Arc::new(RwLock::new(None)),
212215
}
213216
}
214217

crates/rpc-server/src/server.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +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
1242+
if request.action == "wasm_upload" {
1243+
if let Some(ref invalidator) = *handler.wasm_cache_invalidator.read() {
1244+
invalidator(&request.challenge_id);
1245+
info!(challenge_id = %request.challenge_id, "WASM cache invalidated after upload");
1246+
}
1247+
}
1248+
12411249
(
12421250
StatusCode::OK,
12431251
Json(serde_json::json!({

0 commit comments

Comments
 (0)