Implement M5.5 atomic terrain excavation and debris accounting
This commit is contained in:
+7
-1
@@ -34,7 +34,7 @@ target_include_directories(evo_simulation PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/in
|
||||
target_compile_features(evo_simulation PUBLIC cxx_std_23)
|
||||
evo_owned_target(evo_simulation)
|
||||
|
||||
add_library(evo_world STATIC src/world/coordinates.cpp src/world/generation.cpp src/world/chunks.cpp)
|
||||
add_library(evo_world STATIC src/world/coordinates.cpp src/world/generation.cpp src/world/chunks.cpp src/world/edits.cpp)
|
||||
add_library(evo::world ALIAS evo_world)
|
||||
target_include_directories(evo_world PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
|
||||
target_compile_features(evo_world PUBLIC cxx_std_23)
|
||||
@@ -71,6 +71,12 @@ evo_owned_target(evo-sim)
|
||||
|
||||
include(CTest)
|
||||
if(BUILD_TESTING)
|
||||
add_executable(evo-world-edit-tests tests/world_edit_tests.cpp)
|
||||
target_link_libraries(evo-world-edit-tests PRIVATE evo::world)
|
||||
evo_owned_target(evo-world-edit-tests)
|
||||
add_test(NAME world-edit-contracts COMMAND evo-world-edit-tests)
|
||||
set_tests_properties(world-edit-contracts PROPERTIES TIMEOUT 60)
|
||||
|
||||
add_executable(evo-world-chunk-tests tests/world_chunk_tests.cpp)
|
||||
target_link_libraries(evo-world-chunk-tests PRIVATE evo::world)
|
||||
evo_owned_target(evo-world-chunk-tests)
|
||||
|
||||
@@ -14,8 +14,9 @@ network service. Dev verification manifests are separate from the simulation.
|
||||
- [System overview and module boundaries](docs/architecture/SYSTEM_OVERVIEW.md)
|
||||
- [Engineering constitution](docs/engineering/ENGINEERING_CONSTITUTION.md)
|
||||
- [Architecture decisions and proposals](docs/adr/README.md)
|
||||
- [Actionable milestones 0–4 and later gates](docs/ROADMAP.md)
|
||||
- [Actionable milestones 0–4 and later gates](docs/ROADMAP.md)
|
||||
- [Open design questions](docs/architecture/OPEN_QUESTIONS.md)
|
||||
- [Transactional terrain excavation](docs/world/TERRAIN_EDITS.md)
|
||||
- [Bounded chunk ownership](docs/world/CHUNK_OWNERSHIP.md)
|
||||
- [Versioned abiotic base generation](docs/world/BASE_GENERATION.md)
|
||||
- [Actual validation results](docs/engineering/VALIDATION.md)
|
||||
|
||||
+3
-1
@@ -264,7 +264,9 @@ M5.3 is complete under [ADR-0044](adr/0044-abiotic-generation.md):
|
||||
[versioned abiotic generation](world/BASE_GENERATION.md) with frozen reference fixtures.
|
||||
M5.4 is complete under [ADR-0045](adr/0045-bounded-chunk-ownership.md):
|
||||
[bounded ownership](world/CHUNK_OWNERSHIP.md), compact storage, validated revision tokens
|
||||
and dirty eviction refusal. Next is 5.5: accounted transactional terrain edits.
|
||||
and dirty eviction refusal. M5.5 now implements [atomic excavation](world/TERRAIN_EDITS.md)
|
||||
with per-material volume conservation under [ADR-0046](adr/0046-transactional-excavation.md).
|
||||
Next is 5.6: canonical snapshots and edit journals.
|
||||
|
||||
**Decisions still open:** final spherical layout, region dimensions, adaptive LOD,
|
||||
field quantization beyond the initial experiment, hydrology and surface extraction.
|
||||
|
||||
+10
-2
@@ -7,7 +7,7 @@ persistent simulation server. The founding vision remains unchanged.
|
||||
## Completed implementation
|
||||
|
||||
- Vision, architectural pillars, risks, open-question register, architecture
|
||||
diagrams, biological contracts, engineering constitution and 45 ADRs.
|
||||
diagrams, biological contracts, engineering constitution and 46 ADRs.
|
||||
Proposed/deferred choices remain distinct from accepted proof decisions.
|
||||
- M0–M4 task breakdowns with dependencies, acceptance evidence and risks,
|
||||
plus M5–M14 gates in [ROADMAP.md](ROADMAP.md).
|
||||
@@ -61,7 +61,7 @@ for the bounded proof; the general production ECS remains a later measured choic
|
||||
|
||||
| Priority | Work | Completion evidence |
|
||||
|---|---|---|
|
||||
| 1 | M5.5 accounted terrain edits | Transaction identity, revisions, matter destinations, atomic changes and retry validation |
|
||||
| 1 | M5.6 snapshots and edit journals | Bounded canonical format, geometry/debris/retry-state consistency and malformed-input rejection |
|
||||
| Research | Improve aggregate fidelity and measure Linux/target-host costs | M4.6 found no qualifying width; see the aggregate budget report |
|
||||
| 2 | Revisit the ADR-0031/0032 reproduction cost model | Reproduction is affordable only at the genome schema's floor; decide whether that is intended before M4 builds cohorts on it |
|
||||
| Delivery gate | Add/update the dev workload when a deployable increment needs it; Argo is ready | Observe Job/pod completion separately from image publication; no World0 changes |
|
||||
@@ -568,3 +568,11 @@ Added fixed-slot, budgeted ChunkStore and immutable uniform/palette/dense Compac
|
||||
Revision tokens reject stale and foreign reads. Clean chunks regenerate; dirty pins
|
||||
refuse eviction. [Ownership contract](world/CHUNK_OWNERSHIP.md) documents memory/lifetime
|
||||
limits and the absence of cell edits or durability. Next: M5.5 accounted terrain edits.
|
||||
|
||||
## M5.5 transactional terrain excavation
|
||||
|
||||
Added up to 64 cuts across four loaded chunks, atomically transferring occupied
|
||||
volume into finite per-material debris. Revision/digest/full-payload retry checks,
|
||||
64-command retention and peak allocation backpressure are explicit. The
|
||||
[edit contract](world/TERRAIN_EDITS.md) records excavation-only scope and persistence
|
||||
obligations. No mass conversion or durable acknowledgement is claimed. Next: M5.6.
|
||||
|
||||
@@ -61,3 +61,8 @@ apply backpressure rather than dropping state. Larger workloads require measurem
|
||||
Test far-apart chunks, uniform/palette/dense equality, exact budget boundaries,
|
||||
slot exhaustion, invalid input, stale/foreign views and dirty eviction refusal.
|
||||
Retain existing generator goldens and simulation formats. Next is M5.5.
|
||||
|
||||
## M5.5 follow-up
|
||||
|
||||
ADR-0046 adds atomic excavation and fixed replay metadata. Its 32 KiB scratch
|
||||
reservation supersedes the original 17 KiB allowance; dirty clearing remains absent.
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
# ADR-0046: Atomic local excavation and material volume accounting
|
||||
|
||||
## Status
|
||||
Accepted for M5.5 bounded excavation only. Not mass, energy, chemistry or durability.
|
||||
|
||||
## Context
|
||||
Cell fill denotes volume, not kilograms. Excavation must not erase occupied material.
|
||||
M5.4 owns immutable chunks and rejects dirty eviction; transactions can prepare new
|
||||
payloads before publishing. Distributed edits and persistence are later boundaries.
|
||||
|
||||
## Decision
|
||||
Implement excavation only, with 1..64 unique ordered cuts touching at most four loaded
|
||||
chunks in one store. Each cut names chunk, local cell, expected local revision and
|
||||
positive removed fill numerator. Require lexicographic chunk order then X-fast cell
|
||||
ordinal, no duplicates. Coordinates, fill bounds, material membership, revision and
|
||||
source supply are checked. No implicit loading, deposits, conversions or unbounded edits.
|
||||
|
||||
One named destination (debris=1) is owned by each store. Creation specifies separate
|
||||
finite rock/regolith capacities, initially empty; defaults are zero. Credit exactly
|
||||
removed fill units by material. One unit is 1/65535 cubic metre at the v1 1m pitch.
|
||||
No density/mass conversion is assumed, and debris is not a second copy of terrain.
|
||||
Invariant per material: occupied volume + debris volume unchanged by every commit.
|
||||
M6 must explicitly reconcile these inventories with its material/density/composition
|
||||
model; no biological ledger femtogram values are fabricated from volume.
|
||||
|
||||
Request carries nonzero monotonically increasing command ID, destination, cuts and
|
||||
server-verified FNV-1a request digest over explicit fields (see contract). This is not
|
||||
cryptography/authentication. Retain 64 successful requests and receipts, including
|
||||
full payload equality so hash collisions cannot authorize changed-payload retries.
|
||||
Same retained ID/exact request returns the original receipt before checking now-stale
|
||||
revisions; changed payload is rejected. IDs <= high-water mark outside retention
|
||||
are stale, never executed again. Failed requests consume no ID; no out-of-order commits.
|
||||
|
||||
Prepare all compact replacement chunks and validate destination capacity and peak
|
||||
budget while originals remain owned. Publish geometry, debris, revisions, dirty flags,
|
||||
high-water mark and replay record in one nonthrowing single-owner phase. Allocation
|
||||
failure before publish leaves state intact. Every touched chunk gets a new revision
|
||||
and becomes dirty. No durable acknowledgement or dirty clearing is introduced.
|
||||
|
||||
Charge the fixed replay directory plus a conservative 32 KiB scratch allowance
|
||||
(replacing 17 KiB), and all old plus new payloads at preparation peak. No automatic
|
||||
cache eviction on memory failure. Caller buffers and allocator overhead remain excluded.
|
||||
Retain current tokens only by re-query; receipts contain historical revisions.
|
||||
|
||||
## Alternatives Considered
|
||||
A sink destroys matter. A client-owned destination cannot be atomically credited with
|
||||
private chunk state. Reusing the biological mass ledger would invent a density model.
|
||||
Generic deposit/replacement edits enlarge the first mutation surface without evidence.
|
||||
Digest-only replay is unsafe even with a strong hash; exact payloads are bounded here.
|
||||
|
||||
## Benefits
|
||||
Real multi-chunk excavation, conserved typed volume and atomic failure/retry behavior.
|
||||
No persistence fiction or cross-domain unit conversion.
|
||||
|
||||
## Costs
|
||||
Bounded fixed replay metadata, conservative peak memory, ordered commands and only one
|
||||
destination. Same-store single owner only. Debris cannot be withdrawn in this gate.
|
||||
|
||||
## Risks
|
||||
Crashes lose this entire in-memory state; M5.6/5.7 must persist debris, revisions,
|
||||
command high-water/retention and geometry consistently. FNV is not authentication.
|
||||
Allocation exceptions still require host handling; budgets are not RSS limits.
|
||||
|
||||
## Consequences
|
||||
Test seam-crossing cuts, partial fill, capacity/source/stale failure with no partial
|
||||
publication, same/changed retries, retention expiry, unknown destination/material,
|
||||
invalid/duplicate/order/size requests and peak-memory rejection. Keep existing goldens.
|
||||
@@ -49,3 +49,5 @@ design. No unadopted candidate is a link dependency of the proof.
|
||||
- [ADR-0044: Abiotic generation](0044-abiotic-generation.md) - accepted for M5.3 synthetic integer base, version retention and material table.
|
||||
|
||||
- [ADR-0045: Bounded chunk ownership](0045-bounded-chunk-ownership.md) - accepted for M5.4 local memory, revision tokens and dirty eviction guards.
|
||||
|
||||
- [ADR-0046: Transactional excavation](0046-transactional-excavation.md) - accepted for M5.5 atomic local volume transfers and bounded exact-request replay.
|
||||
|
||||
@@ -132,3 +132,12 @@ fixed-slot, byte-budgeted local store. [The contract](../world/CHUNK_OWNERSHIP.m
|
||||
specifies construction scratch accounting, current-revision read tokens, regeneration
|
||||
and dirty eviction refusal. No database, distributed lease or durable acknowledgement
|
||||
is implied. Cell edits remain M5.5 and persistence M5.6/5.7.
|
||||
|
||||
## Implemented M5.5 edit boundary
|
||||
|
||||
[ADR-0046](../adr/0046-transactional-excavation.md) implements bounded single-store
|
||||
atomic excavation, per-material occupied-volume transfer to debris, current revisions,
|
||||
request digest plus full-payload retry identity, and bounded history with a command
|
||||
high-water mark. [The contract](../world/TERRAIN_EDITS.md) specifies failure semantics.
|
||||
No mass/density model or persistence is implied. Debris/retry state must accompany
|
||||
geometry in M5.6/5.7 recovery, and M6 must reconcile volume with chemical inventories.
|
||||
|
||||
@@ -276,3 +276,11 @@ fingerprint. Conservation holding over a million ticks demonstrates arithmetic,
|
||||
not ecological plausibility. Strategy constants, lifespans and the targeting rule
|
||||
are chosen inputs, not derived biology, and must not become gameplay by having
|
||||
been measured. Real populations still require M4 aggregation.
|
||||
|
||||
## M5.5 terrain volume boundary
|
||||
|
||||
[Transactional excavation](../world/TERRAIN_EDITS.md) conserves occupied volume by
|
||||
material between terrain fill and a finite debris inventory. VolumeUnits uses
|
||||
1/65535 m^3 at the experimental 1m pitch, not femtograms. No conversion into the
|
||||
biological ledger is allowed until M6 specifies density/composition and reconciles
|
||||
both representations. Debris is the destination of removed terrain, not new matter.
|
||||
|
||||
@@ -1502,3 +1502,33 @@ accounting excludes allocator/runtime overhead and is not an RSS cap.
|
||||
|
||||
Linux CI/UBSan and target-host performance have not been run locally. No deployment
|
||||
or World0 change occurred. Next: M5.5 accounted transactional edits.
|
||||
|
||||
## M5.5 transactional excavation - 2026-09-26
|
||||
|
||||
Added bounded atomic excavation, finite per-material debris volume, full-payload
|
||||
retry identity and a 64-command replay ring under ADR-0046. Existing biology/world
|
||||
base goldens and persisted formats were not changed. Formatter coverage now includes
|
||||
private headers under src, including the shared chunk-state definitions.
|
||||
|
||||
Executed on Windows x64 / MSVC 19.44:
|
||||
|
||||
- `tools/windows-check.ps1 -Preset release -Action check`: 44/44 passed, including
|
||||
the final late-source-failure test and nonthrowing commit static assertions.
|
||||
- `tools/windows-check.ps1 -Preset analysis -Action check`: clang-tidy 19 build and
|
||||
44/44 passed. After the final targeted additions, rebuilt analysis and reran
|
||||
`ctest --test-dir build/analysis -R world-edit-contracts --output-on-failure`: passed.
|
||||
- `tools/windows-check.ps1 -Preset asan -Action check`: 44/44 passed. After those
|
||||
additions, rebuilt ASan and reran the same targeted test in build/asan: passed.
|
||||
- `tools/windows-check.ps1 -Preset release -Action format`: passed.
|
||||
- `git diff --check`: passed.
|
||||
|
||||
Coverage includes seam-crossing excavation, four chunks/64 cuts, partial fills,
|
||||
rock/regolith volume conservation, all-or-nothing capacity/memory/source failures,
|
||||
missing/stale revisions, duplicate/changed requests, malformed bounds and retention
|
||||
expiry. Late source failure verifies an earlier prepared chunk is not published.
|
||||
No allocator failure injection, revision exhaustion injection or crash recovery is
|
||||
claimed. The source/replacement commit operations have compile-time nonthrowing guards.
|
||||
|
||||
Linux CI and UBSan were not run locally. These are typed volume transfers, not a
|
||||
validated density/mass/chemistry model. No deployment or World0 modification occurred.
|
||||
Next: M5.6 canonical snapshots/edit journals preserving geometry, debris and retry state.
|
||||
|
||||
@@ -14,7 +14,7 @@ The owning unique_ptr may move, but the store itself cannot move or copy.
|
||||
|
||||
| Operation | Success | Refusal / invariant |
|
||||
|---|---|---|
|
||||
| minimum_bytes(slots) | Store object + fixed directory + 17 KiB scratch reservation | Invalid slot count rejected before multiplication |
|
||||
| minimum_bytes(slots) | Store object + fixed directory + replay records + 32 KiB scratch reservation | Invalid slot count rejected before multiplication |
|
||||
| create | Empty store with validated descriptor | Invalid config/version/budget before directory allocation |
|
||||
| load(address) | Return current token or regenerate and publish one base chunk | Invalid address, slot or byte exhaustion, revision exhaustion; no existing state changes |
|
||||
| view(address) | Token for loaded address | Missing is explicit; no implicit load |
|
||||
@@ -29,7 +29,7 @@ an object later allocated at the same address. They cannot authorize persistence
|
||||
cross-process operations. Epoch exhaustion is checked before increment; no wraparound.
|
||||
|
||||
Dirty marking currently changes only lifecycle metadata, not geometry. There is no
|
||||
clear-dirty or cell-edit API. M5.5 must introduce accounted edit transactions; M5.6/5.7
|
||||
clear-dirty API. M5.5 now adds [accounted excavation](TERRAIN_EDITS.md); M5.6/5.7
|
||||
must establish durable revision coverage before permitting cleanup. Destruction at
|
||||
host shutdown frees memory and is not a save operation. No production host should
|
||||
use this in-memory store as its sole durable copy of changes.
|
||||
@@ -62,8 +62,8 @@ not a wire format. M5.6 must specify canonical serialization independently.
|
||||
|
||||
Fixed directory bytes include optional payload objects and lifecycle metadata for
|
||||
all configured slots, occupied or not. Accounted bytes add exact allocated payload
|
||||
array element sizes plus a permanent 17 KiB allowance for load construction scratch:
|
||||
4096 logical words and 256 palette keys. Load generates through point queries to avoid
|
||||
array element sizes plus a permanent 32 KiB conservative scratch allowance and fixed replay records
|
||||
under ADR-0046. Loads use 4096 logical words and 256 palette keys. Load generates through point queries to avoid
|
||||
an extra dense vector, computes required payload before allocation, then publishes
|
||||
without throwing. On allocator failure existing state is unchanged; allocation
|
||||
exceptions propagate to the host. Clean eviction subtracts payload only.
|
||||
@@ -84,6 +84,6 @@ tokens, conservative dirty pinning and rejection without changing resident state
|
||||
Existing generator goldens remain active. Counter exhaustion is guarded in code but
|
||||
not forced via a public test-only state injection API.
|
||||
|
||||
M5.5 adds transaction IDs, payload identity, expected revisions, material destinations
|
||||
and atomic edits. No transactional edit, snapshot, crash recovery, threading, automatic
|
||||
LRU, renderer or Kubernetes integration is provided by M5.4.
|
||||
M5.5 now adds transaction IDs, full request identity, expected revisions, a finite debris
|
||||
destination and atomic excavation; see TERRAIN_EDITS.md. Snapshot/crash recovery,
|
||||
threading, automatic LRU, renderer and Kubernetes integration remain later work.
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
# M5.5 transactional excavation
|
||||
|
||||
[ADR-0046](../adr/0046-transactional-excavation.md) scopes the first terrain edit to
|
||||
excavation. Public contracts are in `evo/world/chunks.hpp`; implementation is in
|
||||
`src/world/edits.cpp`. All calls require one sequential owner and stable request memory
|
||||
through return. Inputs are intentions, never trusted geometry or authoritative balances.
|
||||
|
||||
## Request and atomic scope
|
||||
|
||||
An ExcavationRequest names a nonzero command ID, destination 1 (this store's debris),
|
||||
a server-verified digest and 1..64 CellCuts. Cuts touch at most four loaded chunks in
|
||||
one store. Each names chunk/local address, expected revision and removed fill in
|
||||
[1,65535]. Sort lexicographically by chunk x/y/z, then X-fast local ordinal; duplicate
|
||||
cells and inconsistent revisions within one chunk are rejected. No implicit load or
|
||||
cross-store/world/region transaction occurs. No deposit, arbitrary replacement,
|
||||
material transmutation, biological cost or external inventory is implemented.
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant Host
|
||||
participant Store
|
||||
participant Prepared
|
||||
Host->>Store: command + digest + ordered cuts + expected revisions
|
||||
Store->>Store: Validate shape, digest, replay identity, ownership and revisions
|
||||
Store->>Prepared: Copy logical cells and stage compact replacements
|
||||
Store->>Store: Check source volume, debris capacity, old+new peak bytes
|
||||
alt all checks and allocations succeed
|
||||
Store->>Store: Publish all chunks + debris + revisions + replay receipt
|
||||
Store-->>Host: receipt
|
||||
else any refusal or allocation failure
|
||||
Store-->>Host: error; original state remains intact
|
||||
end
|
||||
```
|
||||
|
||||
Published chunks get distinct increasing local revisions and dirty flags. Previous
|
||||
views become stale; query `view` for current read tokens. The returned receipt lists
|
||||
historical committed revisions and removed volume by material, not current live views.
|
||||
Dirty chunks remain unevictable. Mutation of already-dirty chunks is allowed with
|
||||
current revisions. Geometry and debris commit together without throwing; all fallible
|
||||
allocations precede publication. Host must handle allocation exceptions. This is
|
||||
in-process atomicity, not crash consistency, client authentication or a durable ACK.
|
||||
|
||||
## Volume conservation and explicit destination
|
||||
|
||||
`VolumeUnits{n}` means n/65535 cubic metres at the v1 1m pitch. A CellSample's fill
|
||||
is already in these units. DebrisCapacity specifies independent uint64 capacities for
|
||||
rock and regolith at store creation; defaults are zero and balances start empty.
|
||||
Unknown destination IDs are rejected. No public capacity change, withdrawal or balance
|
||||
setter exists. Material keys alone never grant debris supply.
|
||||
|
||||
For each material m and committed command:
|
||||
|
||||
`sum(terrain fill[m])_after + debris[m]_after = sum(terrain fill[m])_before + debris[m]_before`.
|
||||
|
||||
Partial excavation reduces fill with the same material ID. Removing the full fill
|
||||
produces structurally valid void. Rock cannot credit regolith or vice versa. Compare
|
||||
credits to capacity minus balance before adding, avoiding unsigned overflow. Bounded
|
||||
commands remove at most 64*65535 units. Geometry of the removed sub-cell portion is
|
||||
unknown; no collision shape or smooth surface is claimed.
|
||||
|
||||
These inventories are volume, not kg/femtograms. M6 must define density/composition
|
||||
and reconcile them without double-counting before coupling to biology. No value in
|
||||
the existing biological ledger is reinterpreted or modified by this gate.
|
||||
|
||||
## Retry identity and compatibility
|
||||
|
||||
The [request digest schema](../../schemas/world-edits.md) defines the explicit byte
|
||||
order. FNV-1a is an accidental-corruption/identity aid, never security. Retained command
|
||||
IDs compare all original fields as well as digest, so collisions cannot authorize a
|
||||
changed request. For valid request shape and digest:
|
||||
|
||||
- Same retained ID and exact payload returns the original receipt without reapplying,
|
||||
even after newer edits made its expected revisions stale.
|
||||
- Same retained ID with different payload returns command_conflict.
|
||||
- An ID <= the high-water mark with no retained record returns stale_command.
|
||||
- A fresh ID must exceed the high-water mark; gaps are permitted. Failures consume no
|
||||
ID and mutate no cells, debris, revision, dirty flag or history record.
|
||||
|
||||
Only the last 64 successful commands are retained in a fixed ring. This is bounded
|
||||
local retry history, not event retention. M5.6/5.7 must persist geometry, debris,
|
||||
capacities, revisions, command high-water and replay records consistently. A restart
|
||||
that retains only geometry would duplicate or lose material and is not supported.
|
||||
|
||||
## Memory and tests
|
||||
|
||||
minimum_bytes now includes the fixed 64-record replay array and 32 KiB conservative
|
||||
scratch reservation, superseding M5.4's 17 KiB allowance. New replacement payloads
|
||||
must fit beside all old payloads; old storage is released only during commit. Unused
|
||||
replay fields remain bounded metadata. No cache eviction occurs to accommodate edits.
|
||||
Allocator/runtime overhead and caller buffers remain outside accounted bytes.
|
||||
|
||||
CTest world-edit-contracts covers a seam crossing, all four chunks/64 cuts, partial
|
||||
fill, rock/regolith conservation, destination and second-replacement memory failures,
|
||||
missing/stale/source failures, dirty eviction refusal, malformed request bounds,
|
||||
same/changed retries and expiry after 65 commands. Existing generator and simulation
|
||||
goldens remain active. Allocator fault injection and crash recovery remain future work.
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "evo/world/generation.hpp"
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
@@ -18,9 +19,51 @@ enum class ChunkError : std::uint8_t {
|
||||
stale_view,
|
||||
invalid_local,
|
||||
dirty,
|
||||
revision_exhausted
|
||||
revision_exhausted,
|
||||
invalid_edit,
|
||||
digest_mismatch,
|
||||
command_conflict,
|
||||
stale_command,
|
||||
destination_full,
|
||||
insufficient_material
|
||||
};
|
||||
enum class CellLayout : std::uint8_t { uniform, palette, dense };
|
||||
// Exact occupied-volume quanta, NOT mass: one unit = 1/65535 m^3 for v1.
|
||||
struct VolumeUnits {
|
||||
std::uint64_t value{};
|
||||
auto operator<=>(const VolumeUnits&) const = default;
|
||||
};
|
||||
struct DebrisCapacity {
|
||||
std::array<VolumeUnits, 2> materials{};
|
||||
}; // rock, regolith
|
||||
struct CellCut {
|
||||
ChunkAddress chunk{};
|
||||
LocalCell local{};
|
||||
std::uint64_t expected_revision{};
|
||||
std::uint32_t removed_fill{};
|
||||
auto operator<=>(const CellCut&) const = default;
|
||||
};
|
||||
struct ExcavationRequest {
|
||||
std::uint64_t command{};
|
||||
std::uint32_t destination{1}; // the store's finite debris inventory
|
||||
std::span<const CellCut> cuts;
|
||||
std::uint64_t digest{};
|
||||
};
|
||||
struct EditedChunk {
|
||||
ChunkAddress address{};
|
||||
std::uint64_t revision{};
|
||||
auto operator<=>(const EditedChunk&) const = default;
|
||||
};
|
||||
struct EditReceipt {
|
||||
std::uint64_t command{}, digest{};
|
||||
std::array<VolumeUnits, 2> removed{};
|
||||
std::array<EditedChunk, 4> chunks{};
|
||||
std::size_t chunk_count{};
|
||||
auto operator<=>(const EditReceipt&) const = default;
|
||||
};
|
||||
// Validates request shape; digest excludes the digest field itself. Not authentication.
|
||||
[[nodiscard]] std::expected<std::uint64_t, ChunkError>
|
||||
excavation_digest(const ExcavationRequest& request) noexcept;
|
||||
class ChunkStore;
|
||||
// Immutable owned geometry. Construction does not authorize a terrain edit.
|
||||
class CompactCells {
|
||||
@@ -66,9 +109,11 @@ class ChunkView {
|
||||
class ChunkStore {
|
||||
public:
|
||||
static constexpr std::size_t max_slots = 4096;
|
||||
static constexpr std::size_t scratch_bytes = (4096 + 256) * sizeof(std::uint32_t);
|
||||
static constexpr std::size_t scratch_bytes = 32768;
|
||||
static constexpr std::size_t retained_commands = 64;
|
||||
[[nodiscard]] static std::expected<std::unique_ptr<ChunkStore>, ChunkError>
|
||||
create(BaseDescriptor descriptor, std::size_t slots, std::size_t byte_budget);
|
||||
create(BaseDescriptor descriptor, std::size_t slots, std::size_t byte_budget,
|
||||
DebrisCapacity debris = {});
|
||||
ChunkStore(const ChunkStore&) = delete;
|
||||
ChunkStore& operator=(const ChunkStore&) = delete;
|
||||
ChunkStore(ChunkStore&&) = delete;
|
||||
@@ -83,6 +128,8 @@ class ChunkStore {
|
||||
// Conservative pin only: no geometry edit or durability acknowledgement.
|
||||
[[nodiscard]] std::expected<ChunkView, ChunkError> mark_dirty(ChunkView view) noexcept;
|
||||
[[nodiscard]] std::expected<void, ChunkError> evict(ChunkView view) noexcept;
|
||||
[[nodiscard]] std::expected<EditReceipt, ChunkError> excavate(const ExcavationRequest& request);
|
||||
[[nodiscard]] std::expected<VolumeUnits, ChunkError> debris(MaterialId material) const noexcept;
|
||||
[[nodiscard]] std::size_t accounted_bytes() const noexcept {
|
||||
return bytes_;
|
||||
}
|
||||
@@ -92,11 +139,18 @@ class ChunkStore {
|
||||
|
||||
private:
|
||||
struct Entry;
|
||||
ChunkStore(BaseDescriptor descriptor, std::size_t slots, std::size_t budget, std::size_t bytes);
|
||||
struct History;
|
||||
ChunkStore(BaseDescriptor descriptor, std::size_t slots, std::size_t budget, std::size_t bytes,
|
||||
DebrisCapacity debris);
|
||||
[[nodiscard]] bool current(ChunkView view) const noexcept;
|
||||
BaseDescriptor descriptor_;
|
||||
std::size_t slots_, budget_, bytes_, size_{};
|
||||
std::uint64_t revision_{};
|
||||
std::unique_ptr<Entry[]> entries_;
|
||||
DebrisCapacity debris_capacity_;
|
||||
std::array<VolumeUnits, 2> debris_{};
|
||||
std::uint64_t last_command_{};
|
||||
std::size_t history_next_{};
|
||||
std::unique_ptr<History[]> history_;
|
||||
};
|
||||
} // namespace evo::world
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# Excavation request digest v1
|
||||
|
||||
This defines digest input only, not a persisted file or network envelope. Never dump
|
||||
native C++ structures. Request shape is validated before hashing; total input is bounded.
|
||||
FNV-1a uint64 offset=14695981039346656037, prime=1099511628211; for each byte b:
|
||||
`h = (h xor b) * prime modulo 2^64`. Every integer below is little endian.
|
||||
|
||||
| Order | Field | Width |
|
||||
|---|---|---|
|
||||
| 1 | Request contract version, literal 1 | u32 |
|
||||
| 2 | Command ID | u64 |
|
||||
| 3 | Destination ID, literal 1 | u32 |
|
||||
| 4 | Cut count | u32 |
|
||||
| 5, repeated in canonical cut order | Chunk x, y, z | three u64 conversions of signed coordinates modulo 2^64 |
|
||||
| 6, per cut | Local x, y, z | three u32 |
|
||||
| 7, per cut | Expected revision | u64 |
|
||||
| 8, per cut | Removed fill | u32 |
|
||||
|
||||
Digest field is excluded. Canonical order is chunk lexicographic x/y/z then X-fast
|
||||
local ordinal, with no duplicates. Store identity/descriptor is bound by the target
|
||||
store, not this digest; neither digest nor revision authenticates a caller. M5.6 must
|
||||
add world/frame/base versions, bounds, integrity checks and transaction framing to
|
||||
its separately reviewed persistent format. Geometry, debris and retry metadata form
|
||||
one consistent persistence unit; recording only a request digest cannot recover it.
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
#include "evo/world/chunks.hpp"
|
||||
#include <optional>
|
||||
namespace evo::world {
|
||||
struct ChunkStore::Entry {
|
||||
ChunkAddress address{};
|
||||
std::optional<CompactCells> cells;
|
||||
std::uint64_t revision{};
|
||||
bool dirty{};
|
||||
};
|
||||
struct ChunkStore::History {
|
||||
std::array<CellCut, 64> cuts{};
|
||||
std::size_t count{};
|
||||
std::uint32_t destination{};
|
||||
EditReceipt receipt{};
|
||||
};
|
||||
} // namespace evo::world
|
||||
+11
-13
@@ -1,4 +1,4 @@
|
||||
#include "evo/world/chunks.hpp"
|
||||
#include "chunk_state.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <limits>
|
||||
@@ -98,25 +98,23 @@ std::expected<CellSample, ChunkError> CompactCells::read(std::uint32_t index) co
|
||||
}
|
||||
return *CellSample::create({static_cast<std::uint16_t>(word & 65535U)}, word >> 16U);
|
||||
}
|
||||
struct ChunkStore::Entry {
|
||||
ChunkAddress address{};
|
||||
std::optional<CompactCells> cells;
|
||||
std::uint64_t revision{};
|
||||
bool dirty{};
|
||||
};
|
||||
ChunkStore::ChunkStore(BaseDescriptor descriptor, std::size_t slots, std::size_t budget,
|
||||
std::size_t bytes)
|
||||
std::size_t bytes, DebrisCapacity debris)
|
||||
: descriptor_{descriptor}, slots_{slots}, budget_{budget}, bytes_{bytes},
|
||||
entries_{std::make_unique<Entry[]>(slots)} {}
|
||||
entries_{std::make_unique<Entry[]>(slots)}, debris_capacity_{debris},
|
||||
history_{std::make_unique<History[]>(retained_commands)} {}
|
||||
ChunkStore::~ChunkStore() = default;
|
||||
std::expected<std::size_t, ChunkError> ChunkStore::minimum_bytes(std::size_t slots) noexcept {
|
||||
if (slots == 0 || slots > max_slots) {
|
||||
return std::unexpected{ChunkError::invalid_config};
|
||||
}
|
||||
return sizeof(ChunkStore) + slots * sizeof(Entry) + scratch_bytes;
|
||||
return sizeof(ChunkStore) + slots * sizeof(Entry) + scratch_bytes +
|
||||
retained_commands * sizeof(History);
|
||||
}
|
||||
std::expected<std::unique_ptr<ChunkStore>, ChunkError>
|
||||
ChunkStore::create(BaseDescriptor descriptor, std::size_t slots, std::size_t budget) {
|
||||
std::expected<std::unique_ptr<ChunkStore>, ChunkError> ChunkStore::create(BaseDescriptor descriptor,
|
||||
std::size_t slots,
|
||||
std::size_t budget,
|
||||
DebrisCapacity debris) {
|
||||
const auto minimum = minimum_bytes(slots);
|
||||
if (!minimum) {
|
||||
return std::unexpected{minimum.error()};
|
||||
@@ -127,7 +125,7 @@ ChunkStore::create(BaseDescriptor descriptor, std::size_t slots, std::size_t bud
|
||||
if (budget < *minimum) {
|
||||
return std::unexpected{ChunkError::memory_budget};
|
||||
}
|
||||
return std::unique_ptr<ChunkStore>{new ChunkStore(descriptor, slots, budget, *minimum)};
|
||||
return std::unique_ptr<ChunkStore>{new ChunkStore(descriptor, slots, budget, *minimum, debris)};
|
||||
}
|
||||
bool ChunkStore::current(ChunkView v) const noexcept {
|
||||
return v.owner_ == this && v.slot_ < slots_ && entries_[v.slot_].cells &&
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
#include "chunk_state.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::world {
|
||||
namespace {
|
||||
void hash_field(std::uint64_t& hash, std::uint64_t value, unsigned bytes) noexcept {
|
||||
for (unsigned i = 0; i < bytes; ++i) {
|
||||
hash = (hash ^ (value & 255U)) * 1099511628211ULL;
|
||||
value >>= 8U;
|
||||
}
|
||||
}
|
||||
std::uint32_t pack_cell(CellSample cell) noexcept {
|
||||
return cell.material().value | (static_cast<std::uint32_t>(cell.fill()) << 16U);
|
||||
}
|
||||
} // namespace
|
||||
std::expected<std::uint64_t, ChunkError> excavation_digest(const ExcavationRequest& r) noexcept {
|
||||
if (r.command == 0 || r.destination != 1 || r.cuts.empty() || r.cuts.size() > 64) {
|
||||
return std::unexpected{ChunkError::invalid_edit};
|
||||
}
|
||||
std::uint64_t hash = 14695981039346656037ULL;
|
||||
hash_field(hash, 1, 4); // request contract version
|
||||
hash_field(hash, r.command, 8);
|
||||
hash_field(hash, r.destination, 4);
|
||||
hash_field(hash, r.cuts.size(), 4);
|
||||
std::size_t chunks = 0;
|
||||
for (std::size_t i = 0; i < r.cuts.size(); ++i) {
|
||||
const auto& c = r.cuts[i];
|
||||
const auto index = ordinal(c.local, base_chunk_edge);
|
||||
if (!index || !join({c.chunk, c.local}, base_chunk_edge) || c.expected_revision == 0 ||
|
||||
c.removed_fill == 0 || c.removed_fill > fill_scale) {
|
||||
return std::unexpected{ChunkError::invalid_edit};
|
||||
}
|
||||
if (i == 0 || c.chunk != r.cuts[i - 1].chunk) {
|
||||
++chunks;
|
||||
}
|
||||
if (chunks > 4) {
|
||||
return std::unexpected{ChunkError::invalid_edit};
|
||||
}
|
||||
if (i > 0) {
|
||||
const auto& previous = r.cuts[i - 1];
|
||||
if (c.chunk < previous.chunk || (c.chunk == previous.chunk &&
|
||||
(*index <= *ordinal(previous.local, base_chunk_edge) ||
|
||||
c.expected_revision != previous.expected_revision))) {
|
||||
return std::unexpected{ChunkError::invalid_edit};
|
||||
}
|
||||
}
|
||||
hash_field(hash, static_cast<std::uint64_t>(c.chunk.x), 8);
|
||||
hash_field(hash, static_cast<std::uint64_t>(c.chunk.y), 8);
|
||||
hash_field(hash, static_cast<std::uint64_t>(c.chunk.z), 8);
|
||||
hash_field(hash, c.local.x, 4);
|
||||
hash_field(hash, c.local.y, 4);
|
||||
hash_field(hash, c.local.z, 4);
|
||||
hash_field(hash, c.expected_revision, 8);
|
||||
hash_field(hash, c.removed_fill, 4);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
std::expected<VolumeUnits, ChunkError> ChunkStore::debris(MaterialId material) const noexcept {
|
||||
if (material.value < 1 || material.value > 2) {
|
||||
return std::unexpected{ChunkError::invalid_edit};
|
||||
}
|
||||
return debris_[material.value - 1];
|
||||
}
|
||||
std::expected<EditReceipt, ChunkError> ChunkStore::excavate(const ExcavationRequest& r) {
|
||||
const auto digest = excavation_digest(r);
|
||||
if (!digest) {
|
||||
return std::unexpected{digest.error()};
|
||||
}
|
||||
if (*digest != r.digest) {
|
||||
return std::unexpected{ChunkError::digest_mismatch};
|
||||
}
|
||||
if (r.command <= last_command_) {
|
||||
for (std::size_t i = 0; i < retained_commands; ++i) {
|
||||
const auto& old = history_[i];
|
||||
if (old.receipt.command == r.command) {
|
||||
if (old.receipt.digest != r.digest || old.destination != r.destination ||
|
||||
old.count != r.cuts.size() ||
|
||||
!std::equal(r.cuts.begin(), r.cuts.end(), old.cuts.begin())) {
|
||||
return std::unexpected{ChunkError::command_conflict};
|
||||
}
|
||||
return old.receipt;
|
||||
}
|
||||
}
|
||||
return std::unexpected{ChunkError::stale_command};
|
||||
}
|
||||
EditReceipt receipt{};
|
||||
receipt.command = r.command;
|
||||
receipt.digest = r.digest;
|
||||
std::array<std::size_t, 4> slots{};
|
||||
for (const auto& cut : r.cuts) {
|
||||
if (receipt.chunk_count == 0 ||
|
||||
receipt.chunks[receipt.chunk_count - 1].address != cut.chunk) {
|
||||
const auto token = view(cut.chunk);
|
||||
if (!token) {
|
||||
return std::unexpected{token.error()};
|
||||
}
|
||||
if (token->revision() != cut.expected_revision) {
|
||||
return std::unexpected{ChunkError::stale_view};
|
||||
}
|
||||
slots[receipt.chunk_count] = token->slot_;
|
||||
receipt.chunks[receipt.chunk_count++].address = cut.chunk;
|
||||
}
|
||||
}
|
||||
if (revision_ > std::numeric_limits<std::uint64_t>::max() - receipt.chunk_count) {
|
||||
return std::unexpected{ChunkError::revision_exhausted};
|
||||
}
|
||||
std::array<std::optional<CompactCells>, 4> replacements;
|
||||
std::size_t new_bytes = 0, old_bytes = 0, next_cut = 0;
|
||||
for (std::size_t chunk = 0; chunk < receipt.chunk_count; ++chunk) {
|
||||
const auto& entry = entries_[slots[chunk]];
|
||||
if (!entry.cells) {
|
||||
return std::unexpected{ChunkError::missing};
|
||||
}
|
||||
old_bytes += entry.cells->payload_bytes();
|
||||
std::array<std::uint32_t, base_chunk_cells> words{};
|
||||
for (std::uint32_t i = 0; i < base_chunk_cells; ++i) {
|
||||
words[i] = pack_cell(*entry.cells->read(i));
|
||||
}
|
||||
while (next_cut < r.cuts.size() && r.cuts[next_cut].chunk == entry.address) {
|
||||
const auto& cut = r.cuts[next_cut++];
|
||||
const auto index = *ordinal(cut.local, base_chunk_edge);
|
||||
const auto material = words[index] & 65535U;
|
||||
const auto fill = words[index] >> 16U;
|
||||
if (material < 1 || material > 2 || fill < cut.removed_fill) {
|
||||
return std::unexpected{ChunkError::insufficient_material};
|
||||
}
|
||||
const auto remaining = fill - cut.removed_fill;
|
||||
words[index] = remaining == 0 ? 0 : material | (remaining << 16U);
|
||||
receipt.removed[material - 1].value += cut.removed_fill;
|
||||
}
|
||||
for (std::size_t material = 0; material < 2; ++material) {
|
||||
if (receipt.removed[material].value >
|
||||
debris_capacity_.materials[material].value - debris_[material].value) {
|
||||
return std::unexpected{ChunkError::destination_full};
|
||||
}
|
||||
}
|
||||
new_bytes += CompactCells::required_bytes(words);
|
||||
if (new_bytes > budget_ - bytes_) {
|
||||
return std::unexpected{ChunkError::memory_budget};
|
||||
}
|
||||
replacements[chunk].emplace(CompactCells::encode(words));
|
||||
receipt.chunks[chunk].revision = revision_ + chunk + 1;
|
||||
}
|
||||
static_assert(std::is_nothrow_move_assignable_v<std::optional<CompactCells>>);
|
||||
static_assert(std::is_nothrow_copy_assignable_v<History>);
|
||||
// All rejection and allocation precede this single-owner, nonthrowing commit.
|
||||
for (std::size_t chunk = 0; chunk < receipt.chunk_count; ++chunk) {
|
||||
auto& entry = entries_[slots[chunk]];
|
||||
entry.cells = std::move(replacements[chunk]);
|
||||
entry.revision = receipt.chunks[chunk].revision;
|
||||
entry.dirty = true;
|
||||
}
|
||||
bytes_ = bytes_ - old_bytes + new_bytes;
|
||||
revision_ += receipt.chunk_count;
|
||||
for (std::size_t material = 0; material < 2; ++material) {
|
||||
debris_[material].value += receipt.removed[material].value;
|
||||
}
|
||||
auto& record = history_[history_next_];
|
||||
std::copy(r.cuts.begin(), r.cuts.end(), record.cuts.begin());
|
||||
record.count = r.cuts.size();
|
||||
record.destination = r.destination;
|
||||
record.receipt = receipt;
|
||||
history_next_ = (history_next_ + 1) % retained_commands;
|
||||
last_command_ = r.command;
|
||||
return receipt;
|
||||
}
|
||||
} // namespace evo::world
|
||||
@@ -0,0 +1,241 @@
|
||||
#include "evo/world/chunks.hpp"
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
using namespace evo::world;
|
||||
void check(bool condition, std::source_location at = std::source_location::current()) {
|
||||
if (!condition) {
|
||||
throw std::runtime_error("world edit line " + std::to_string(at.line()));
|
||||
}
|
||||
}
|
||||
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
||||
template <typename T> bool refused(const std::expected<T, ChunkError>& r, ChunkError e) {
|
||||
return !r && r.error() == e;
|
||||
}
|
||||
std::unique_ptr<ChunkStore> store(std::uint64_t capacity = 1000000, std::size_t extra = 200000) {
|
||||
const auto minimum = ChunkStore::minimum_bytes(4);
|
||||
CHECK(minimum);
|
||||
const auto cap = DebrisCapacity{{VolumeUnits{capacity}, VolumeUnits{capacity}}};
|
||||
auto result = ChunkStore::create({}, 4, *minimum + extra, cap);
|
||||
CHECK(result);
|
||||
return std::move(*result);
|
||||
}
|
||||
ExcavationRequest request(std::uint64_t id, std::span<const CellCut> cuts) {
|
||||
ExcavationRequest r{id, 1, cuts, 0};
|
||||
const auto hash = excavation_digest(r);
|
||||
CHECK(hash);
|
||||
r.digest = *hash;
|
||||
return r;
|
||||
}
|
||||
std::array<CellCut, 2> seam(ChunkStore& s, std::uint32_t amount = 65535) {
|
||||
const auto a = s.load({-1, 0, -2});
|
||||
const auto b = s.load({0, 0, -2});
|
||||
CHECK(a && b);
|
||||
return {{{{-1, 0, -2}, {15, 0, 0}, a->revision(), amount},
|
||||
{{0, 0, -2}, {0, 0, 0}, b->revision(), amount}}};
|
||||
}
|
||||
std::uint64_t volume(ChunkStore& s, ChunkAddress address, std::uint16_t material) {
|
||||
const auto view = s.view(address);
|
||||
CHECK(view);
|
||||
std::uint64_t total = 0;
|
||||
for (std::uint32_t i = 0; i < 4096; ++i) {
|
||||
const auto c = s.read(*view, *local_from_ordinal(i, 16));
|
||||
CHECK(c);
|
||||
if (c->material().value == material) {
|
||||
total += c->fill();
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
void conservation_and_retry() {
|
||||
auto s = store();
|
||||
const auto cuts = seam(*s);
|
||||
const auto before = volume(*s, cuts[0].chunk, 1) + volume(*s, cuts[1].chunk, 1);
|
||||
const auto old = s->view(cuts[0].chunk);
|
||||
CHECK(old);
|
||||
const auto r = request(1, cuts);
|
||||
const auto result = s->excavate(r);
|
||||
CHECK(result);
|
||||
CHECK(result->chunk_count == 2 && result->removed[0].value == 131070 &&
|
||||
result->removed[1].value == 0);
|
||||
CHECK(volume(*s, cuts[0].chunk, 1) + volume(*s, cuts[1].chunk, 1) + s->debris({1})->value ==
|
||||
before);
|
||||
CHECK(refused(s->read(*old, {}), ChunkError::stale_view));
|
||||
for (const auto& c : cuts) {
|
||||
const auto current = s->view(c.chunk);
|
||||
CHECK(current);
|
||||
const auto value = s->read(*current, c.local);
|
||||
CHECK(value && value->material().value == 0 && value->fill() == 0);
|
||||
CHECK(refused(s->evict(*current), ChunkError::dirty));
|
||||
}
|
||||
const auto used = s->accounted_bytes();
|
||||
const auto repeated = s->excavate(r);
|
||||
CHECK(repeated && *repeated == *result);
|
||||
CHECK(s->debris({1})->value == 131070 && s->accounted_bytes() == used);
|
||||
auto changed = cuts;
|
||||
changed[0].removed_fill = 1;
|
||||
CHECK(refused(s->excavate(request(1, changed)), ChunkError::command_conflict));
|
||||
auto invalid = r;
|
||||
invalid.digest ^= 1;
|
||||
CHECK(refused(s->excavate(invalid), ChunkError::digest_mismatch));
|
||||
CHECK(refused(s->excavate(request(2, cuts)), ChunkError::stale_view));
|
||||
auto fresh = cuts;
|
||||
for (auto& c : fresh) {
|
||||
c.expected_revision = s->view(c.chunk)->revision();
|
||||
}
|
||||
CHECK(refused(s->excavate(request(2, fresh)), ChunkError::insufficient_material));
|
||||
// Failed ID 2 is reusable; change to a populated cell and partial excavation.
|
||||
fresh[0].local = {14, 0, 0};
|
||||
fresh[0].removed_fill = 100;
|
||||
fresh[1].local = {1, 0, 0};
|
||||
fresh[1].removed_fill = 200;
|
||||
const auto partial = s->excavate(request(2, fresh));
|
||||
CHECK(partial);
|
||||
CHECK(s->read(*s->view(fresh[0].chunk), fresh[0].local)->fill() == 65435);
|
||||
CHECK(s->read(*s->view(fresh[1].chunk), fresh[1].local)->fill() == 65335);
|
||||
CHECK(volume(*s, cuts[0].chunk, 1) + volume(*s, cuts[1].chunk, 1) + s->debris({1})->value ==
|
||||
before);
|
||||
CHECK(*s->excavate(r) == *result); // receipt is historical even after newer edits
|
||||
}
|
||||
void atomic_rejection() {
|
||||
for (const auto memory_limited : {false, true}) {
|
||||
auto s = store(memory_limited ? 1000000 : 100000, memory_limited ? 4104 : 200000);
|
||||
const auto cuts = seam(*s);
|
||||
const auto before = s->accounted_bytes();
|
||||
const auto old = s->view(cuts[0].chunk);
|
||||
CHECK(old);
|
||||
CHECK(refused(s->excavate(request(1, cuts)),
|
||||
memory_limited ? ChunkError::memory_budget : ChunkError::destination_full));
|
||||
CHECK(s->accounted_bytes() == before && s->debris({1})->value == 0);
|
||||
for (const auto& c : cuts) {
|
||||
const auto view = s->view(c.chunk);
|
||||
CHECK(view && view->revision() == c.expected_revision);
|
||||
CHECK(s->read(*view, c.local)->fill() == 65535);
|
||||
}
|
||||
CHECK(s->read(*old, {}));
|
||||
CHECK(s->evict(*old)); // rejection did not set dirty
|
||||
}
|
||||
auto s = store();
|
||||
auto cuts = seam(*s);
|
||||
cuts[1].chunk = {1, 0, -2};
|
||||
CHECK(refused(s->excavate(request(1, cuts)), ChunkError::missing));
|
||||
CHECK(s->debris({1})->value == 0);
|
||||
}
|
||||
void late_source_failure() {
|
||||
auto s = store();
|
||||
auto cuts = seam(*s);
|
||||
std::array<CellCut, 1> precondition{cuts[1]};
|
||||
precondition[0].removed_fill = 65534;
|
||||
CHECK(s->excavate(request(1, precondition)));
|
||||
cuts = seam(*s, 2);
|
||||
const auto bytes = s->accounted_bytes();
|
||||
CHECK(refused(s->excavate(request(2, cuts)), ChunkError::insufficient_material));
|
||||
CHECK(s->accounted_bytes() == bytes && s->debris({1})->value == 65534);
|
||||
for (const auto& c : cuts) {
|
||||
CHECK(s->view(c.chunk)->revision() == c.expected_revision);
|
||||
}
|
||||
CHECK(s->read(*s->view(cuts[0].chunk), cuts[0].local)->fill() == 65535);
|
||||
CHECK(s->read(*s->view(cuts[1].chunk), cuts[1].local)->fill() == 1);
|
||||
CHECK(s->evict(*s->view(cuts[0].chunk)));
|
||||
}
|
||||
void shape_and_retention() {
|
||||
auto s = store();
|
||||
auto cuts = seam(*s, 1);
|
||||
auto r = request(1, cuts);
|
||||
r.destination = 2;
|
||||
CHECK(refused(s->excavate(r), ChunkError::invalid_edit));
|
||||
r = request(1, cuts);
|
||||
r.command = 0;
|
||||
CHECK(refused(s->excavate(r), ChunkError::invalid_edit));
|
||||
CHECK(refused(excavation_digest({1, 1, {}, 0}), ChunkError::invalid_edit));
|
||||
auto bad = cuts;
|
||||
bad[1] = bad[0];
|
||||
CHECK(refused(excavation_digest({1, 1, bad, 0}), ChunkError::invalid_edit));
|
||||
bad = {cuts[1], cuts[0]};
|
||||
CHECK(refused(excavation_digest({1, 1, bad, 0}), ChunkError::invalid_edit));
|
||||
for (const auto amount : {0U, 65536U, std::numeric_limits<std::uint32_t>::max()}) {
|
||||
bad = cuts;
|
||||
bad[0].removed_fill = amount;
|
||||
CHECK(refused(excavation_digest({1, 1, bad, 0}), ChunkError::invalid_edit));
|
||||
}
|
||||
bad = cuts;
|
||||
bad[0].local.x = 16;
|
||||
CHECK(refused(excavation_digest({1, 1, bad, 0}), ChunkError::invalid_edit));
|
||||
bad = cuts;
|
||||
bad[0].chunk.x = std::numeric_limits<std::int64_t>::min();
|
||||
CHECK(refused(excavation_digest({1, 1, bad, 0}), ChunkError::invalid_edit));
|
||||
std::vector<CellCut> oversized(65, cuts[0]);
|
||||
CHECK(refused(excavation_digest({1, 1, oversized, 0}), ChunkError::invalid_edit));
|
||||
CHECK(refused(s->debris({0}), ChunkError::invalid_edit));
|
||||
const auto original = request(1, cuts);
|
||||
CHECK(s->excavate(original));
|
||||
for (std::uint64_t id = 2; id <= 65; ++id) {
|
||||
auto current = cuts;
|
||||
for (auto& c : current) {
|
||||
c.expected_revision = s->view(c.chunk)->revision();
|
||||
}
|
||||
const auto command = request(id, current);
|
||||
const auto done = s->excavate(command);
|
||||
CHECK(done);
|
||||
CHECK(*s->excavate(command) == *done);
|
||||
}
|
||||
CHECK(refused(s->excavate(original), ChunkError::stale_command));
|
||||
CHECK(s->debris({1})->value == 130);
|
||||
}
|
||||
void four_chunks_and_materials() {
|
||||
auto s = store();
|
||||
std::array<CellCut, 64> cuts{};
|
||||
for (std::size_t chunk = 0; chunk < 4; ++chunk) {
|
||||
const ChunkAddress address{static_cast<std::int64_t>(chunk), 0, -2};
|
||||
const auto v = s->load(address);
|
||||
CHECK(v);
|
||||
for (std::size_t i = 0; i < 16; ++i) {
|
||||
cuts[chunk * 16 + i] = {
|
||||
address, {static_cast<std::uint32_t>(i), 0, 0}, v->revision(), 1};
|
||||
}
|
||||
}
|
||||
const auto done = s->excavate(request(1, cuts));
|
||||
CHECK(done && done->chunk_count == 4 && done->removed[0].value == 64);
|
||||
std::array<CellCut, 5> excessive{};
|
||||
for (std::size_t i = 0; i < 5; ++i) {
|
||||
excessive[i] = {{static_cast<std::int64_t>(i), 0, -2}, {}, 1, 1};
|
||||
}
|
||||
CHECK(refused(excavation_digest({2, 1, excessive, 0}), ChunkError::invalid_edit));
|
||||
auto regolith = store();
|
||||
const auto v = regolith->load({0, 0, -1});
|
||||
CHECK(v);
|
||||
for (std::uint32_t i = 0; i < 4096; ++i) {
|
||||
const auto local = *local_from_ordinal(i, 16);
|
||||
const auto c = regolith->read(*v, local);
|
||||
CHECK(c);
|
||||
if (c->material().value == 2) {
|
||||
const std::array<CellCut, 1> cut{{{{0, 0, -1}, local, v->revision(), 123}}};
|
||||
const auto before = volume(*regolith, {0, 0, -1}, 2);
|
||||
CHECK(regolith->excavate(request(1, cut)));
|
||||
CHECK(regolith->debris({1})->value == 0 && regolith->debris({2})->value == 123);
|
||||
CHECK(volume(*regolith, {0, 0, -1}, 2) + 123 == before);
|
||||
return;
|
||||
}
|
||||
}
|
||||
CHECK(false);
|
||||
}
|
||||
} // namespace
|
||||
int main() try {
|
||||
conservation_and_retry();
|
||||
atomic_rejection();
|
||||
late_source_failure();
|
||||
shape_and_retention();
|
||||
four_chunks_and_materials();
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::fprintf(stderr, "%s\n", e.what());
|
||||
return 1;
|
||||
} catch (...) {
|
||||
return 1;
|
||||
}
|
||||
@@ -6,7 +6,7 @@ if(NOT result EQUAL 0 OR NOT version MATCHES "version 19\\.")
|
||||
message(FATAL_ERROR "Expected clang-format major 19, got ${version}")
|
||||
endif()
|
||||
file(GLOB_RECURSE sources
|
||||
"${SOURCE_ROOT}/include/*.hpp" "${SOURCE_ROOT}/src/*.cpp"
|
||||
"${SOURCE_ROOT}/include/*.hpp" "${SOURCE_ROOT}/src/*.cpp" "${SOURCE_ROOT}/src/*.hpp"
|
||||
"${SOURCE_ROOT}/tests/*.hpp" "${SOURCE_ROOT}/apps/*.cpp" "${SOURCE_ROOT}/tests/*.cpp"
|
||||
"${SOURCE_ROOT}/experiments/*.cpp")
|
||||
execute_process(COMMAND "${FORMATTER}" --dry-run --Werror ${sources} RESULT_VARIABLE result)
|
||||
|
||||
Reference in New Issue
Block a user