games/voxels: chunk sync replaces sections instead of resurrecting blocks
- Commit
bc18c11e8b1b7a4c281ddeefd13cda46477914da- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 22:46 UTC (5 days ago)
- Parents
53fdbcc
decode_chunk only ever wrote sections present in the payload and never cleared ones it omitted. Harmless for disk load (which decodes into a fresh chunk), but network apply decodes into the already-populated resident chunk: breaking the last block of a section vanished it on the host, the full VOX3 chunk shipped, and the client kept the deleted block — worse, a later client edit of that chunk shipped the stale block back upstream, duplicating it onto the host. The codec now decodes into a fresh section array and commits only on success: a VOX3 payload is by definition the whole chunk, so absent sections are cleared, and a corrupt payload leaves the live chunk untouched instead of half-applied. Regression tests pin the emptied-section replacement and the failed-decode atomicity. Save/load, op queues, framing and chunk coordinate math were audited and cleared. Verified: cargo test -p voxels_engine 250 lib + 5 edge tests; :games:voxels:testDevUnitTest 218 tests and lint pass.
Diff
games/voxels/src/main/rust/src/world/save.rs
diff --git a/games/voxels/src/main/rust/src/world/save.rs b/games/voxels/src/main/rust/src/world/save.rsindex b3efae3afb..42215d5987 100644--- a/games/voxels/src/main/rust/src/world/save.rs+++ b/games/voxels/src/main/rust/src/world/save.rs@@ -51,6 +51,14 @@ pub fn decode_chunk(bytes: &[u8], chunk: &mut Chunk) -> std::io::Result<()> { if &bytes[0..4] != b"VOX3" { return Err(bad("bad magic")); } let sec_count = bytes[4] as usize; let mut off = 5usize;+ // Decode into a fresh section set and commit it only on success. A VOX3 payload is the+ // whole chunk (disk load starts from an empty chunk, and ChunkData sync is a full+ // replacement), so sections the payload no longer carries must be cleared rather than+ // left over from whatever the chunk held before — otherwise a block whose section was+ // emptied on the sender resurrects on the receiver. Decoding on the side also means a+ // corrupt payload leaves the live chunk untouched instead of half-applied.+ let mut sections: [Option<super::chunk::BlockSection>; SECTIONS_PER_CHUNK] =+ std::array::from_fn(|_| None); for sec_idx in 0..sec_count.min(SECTIONS_PER_CHUNK) { if off >= bytes.len() { break; } let flag = bytes[off]; off+=1;@@ -72,8 +80,9 @@ pub fn decode_chunk(bytes: &[u8], chunk: &mut Chunk) -> std::io::Result<()> { meta = Some(m); } let non_air = blocks.iter().filter(|&&b| b!=0).count();- if non_air>0 { chunk.sections[sec_idx] = Some(super::chunk::BlockSection{ blocks, meta, non_air }); }+ if non_air>0 { sections[sec_idx] = Some(super::chunk::BlockSection{ blocks, meta, non_air }); } }+ chunk.sections = sections; chunk.generated = true; chunk.dirty = false; chunk.mesh_dirty = true; Ok(()) }@@ -204,6 +213,38 @@ mod tests { assert_eq!(partial.get_block(1, 1, 1), 5); } + // A ChunkData payload is a full replacement, not a merge: the sending chunk has no+ // section where the last block was removed, so decoding it over a resident chunk+ // must clear that section or the deleted block resurrects on the receiver (and can+ // be shipped back upstream, duplicating it on the sender too).+ #[test]+ fn decode_replaces_sections_the_payload_no_longer_has() {+ let mut target = Chunk::new(ChunkPos(3, -2));+ target.set_block(1, 200, 1, 42); // section 12, the only block there+ target.set_block(2, 10, 2, 7); // section 0++ // The authoritative replacement kept the low block but removed the high one.+ let mut replacement = Chunk::new(ChunkPos(3, -2));+ replacement.set_block(2, 10, 2, 7);+ decode_chunk(&encode_chunk(&replacement), &mut target).expect("replacement must decode");++ assert_eq!(target.get_block(2, 10, 2), 7, "blocks still present must survive");+ assert_eq!(target.get_block(1, 200, 1), 0, "a block in a section the payload dropped must not linger");+ assert!(target.sections[12].is_none(), "the emptied section itself must be cleared");+ }++ // A corrupt payload must not half-apply: the network path only remeshes on success,+ // so a failed decode that mutated the chunk would leave invisible block changes.+ #[test]+ fn a_failed_decode_leaves_the_chunk_untouched() {+ let mut target = Chunk::new(ChunkPos(1, 1));+ target.set_block(3, 64, 3, 9);+ let good = encode_chunk(&target);++ assert!(decode_chunk(&good[..good.len() / 2], &mut target).is_err());+ assert_eq!(target.get_block(3, 64, 3), 9, "a truncated payload must not mutate the chunk");+ }+ // `ChunkMap::load_or_gen` treats a decode failure as "never generated" and rebuilds the chunk // from the seed, so a file it can't read silently erases whatever the player built. This drives // the real load path against a real file on disk rather than just the codec.