voxels: network-applied chunks are marked edited so the host persists them
- Commit
852588264f8173133c139d7e3e46b536e6804f61- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 06:38 UTC (5 days ago)
- Parents
21e07a5
A chunk a client edited and shipped upstream as ChunkData was applied by the host in memory and rebroadcast — but never persisted: decode_chunk clears dirty and never sets edited (correct bookkeeping for a disk load, wrong for a network-applied edit), and persistence honours exactly those flags. save_dirty skipped the chunk, and unload_beyond dropped rather than parked it, so on the host's next save/reload (or after the chunk scrolled out of range) the client's edits silently reverted to generated terrain. The single-block edit paths mark edited+dirty and persisted fine, but the real client path ships whole chunks, so this hit the live flow. apply_chunk_data now marks the decoded chunk dirty and edited on success only (the recent clean-decode change made decode a full replacement; the applying layer owns re-marking what it changed). edited also matters client- side: it routes applied chunks to pending (discarded by the client branch of save_world) instead of the dirty-only unload branch that would write mirror chunks straight to disk. New test applies a client chunk, asserts the flags, a corrupt payload changing no flags, parking on scroll-away, and the block surviving save->reload (failed pre-fix at the flags assertion). Verified: cargo test 251 lib + 5 edge pass; :games:voxels:lint passes; the 6 testDevUnitTest failures are pre-existing Robolectric/JNI environment issues in sync-pump tests, unrelated to this Rust-only change.
Diff
games/voxels/src/main/rust/src/net.rs
diff --git a/games/voxels/src/main/rust/src/net.rs b/games/voxels/src/main/rust/src/net.rsindex 4851487fcb..98f47f6348 100644--- a/games/voxels/src/main/rust/src/net.rs+++ b/games/voxels/src/main/rust/src/net.rs@@ -360,7 +360,19 @@ fn apply_chunk_data(state: &mut EngineState, cx: i32, cz: i32, data: &[u8]) { let mut ok = false; if let Some(chunk) = state.chunks.get_mut(cp) { ok = crate::world::save::decode_chunk(data, chunk).is_ok();- if ok { chunk.mesh_dirty = true; }+ if ok {+ chunk.mesh_dirty = true;+ // `decode_chunk` clears `dirty` (a payload it just read is by+ // definition in sync with its source), but a chunk applied from+ // the network is a player edit as far as this peer's disk is+ // concerned. Without these marks `save_dirty` skips it and+ // `unload_beyond` drops it instead of parking it in `pending`,+ // so a host would silently revert a client's shipped edits on+ // the next save/reload. Local edits get both marks from+ // `ChunkMap::set_block_meta_world`; applied chunks must too.+ chunk.dirty = true;+ chunk.edited = true;+ } } if ok { dirty_chunk_and_neighbors(state, cx, cz); } }
games/voxels/src/main/rust/src/net_extra_tests.rs
diff --git a/games/voxels/src/main/rust/src/net_extra_tests.rs b/games/voxels/src/main/rust/src/net_extra_tests.rsindex d3f9741bbc..b04e8beda9 100644--- a/games/voxels/src/main/rust/src/net_extra_tests.rs+++ b/games/voxels/src/main/rust/src/net_extra_tests.rs@@ -57,3 +57,62 @@ fn host_applies_chunk_container_and_place_intents() { big(|| { apply_inbound(&mut c); set_role(ROLE_OFFLINE); }) }++// A chunk a client edited and shipped upstream is the host's authoritative+// copy the moment it is applied: the host rebroadcasts it and everyone plays+// on it. It must therefore be marked like any local edit (dirty + edited),+// or `save_world` skips it (`save_dirty` only writes dirty chunks) and+// `unload_beyond` drops it instead of parking it in `pending` — the client's+// work silently reverts to the generated terrain on the next host reload.+#[test]+fn host_applied_chunk_data_survives_save_and_reload() { big(|| {+ let _serial = crate::engine::GLOBAL_ENGINE_TEST.lock();+ let _ = drain_outbound_json();+ if let Ok(mut q) = inbox().lock() { q.clear(); }+ if let Ok(mut e) = edited().lock() { e.clear(); }+ set_role(ROLE_HOST);+ let dir = temp("persist");+ let mut s = crate::engine::new_state(dir.clone(), 55, 2);+ // Generation itself may legitimately dirty a chunk (the water pass writes+ // through `set_block`), so snapshot the corrupt target's flags up front.+ s.chunks.load_or_gen(ChunkPos(1, 1));+ let corrupt_before = s.chunks.get(ChunkPos(1, 1))+ .map(|c| (c.dirty, c.edited)).expect("corrupt target must be resident");++ // The client's edited version of chunk (0,0): one distinctive block high+ // above the terrain so generation/water can never reproduce it.+ let mut replacement = crate::world::chunk::Chunk::new(ChunkPos(0, 0));+ replacement.set_block(3, 200, 4, 42);+ push(NetMsg::ChunkData {+ dim: s.dim, cx: 0, cz: 0,+ data: crate::world::save::encode_chunk(&replacement),+ });+ // A corrupt payload for a neighbouring chunk must mark nothing.+ push(NetMsg::ChunkData { dim: s.dim, cx: 1, cz: 1, data: vec![0xFF, 0x00, 0x13] });+ apply_inbound(&mut s);++ assert_eq!(s.chunks.get_block_world(3, 200, 4), 42, "applied block must be live in memory");+ let applied = s.chunks.get(ChunkPos(0, 0)).expect("applied chunk must be resident");+ assert!(applied.dirty, "a network-applied chunk must be marked dirty so save_world writes it");+ assert!(applied.edited, "a network-applied chunk must be marked edited so unload parks it in pending");+ let corrupt = s.chunks.get(ChunkPos(1, 1)).expect("corrupt target chunk must be resident");+ assert_eq!(+ (corrupt.dirty, corrupt.edited), corrupt_before,+ "a failed decode must not mark the chunk for saving",+ );++ // Scroll far away: the edited chunk must be parked, not dropped, and the+ // next consistent save must write it.+ s.chunks.ensure_radius(20 * 16, 20 * 16);+ assert_eq!(s.chunks.pending_len(), 1, "the applied chunk must survive unload via pending");+ crate::engine::save_world(&mut s);+ assert_eq!(s.chunks.pending_len(), 0, "save_world must flush the parked applied chunk");++ // Reload from the same directory: the client's block is still there.+ let reloaded = crate::engine::new_state(dir, 55, 2);+ assert_eq!(+ reloaded.chunks.get_block_world(3, 200, 4), 42,+ "a client edit applied by the host must survive a host save + reload",+ );+ set_role(ROLE_OFFLINE);+}) }