Implement M5.6 canonical world snapshots and edit journals
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
#include "evo/world/chunks.hpp"
|
||||
#include "fixtures/world_empty_v1.hpp"
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace {
|
||||
using namespace evo::world;
|
||||
constexpr WorldIdentity identity{7, 9};
|
||||
constexpr std::size_t budget = 2000000;
|
||||
void check(bool condition, std::source_location at = std::source_location::current()) {
|
||||
if (!condition) {
|
||||
throw std::runtime_error("world codec line " + std::to_string(at.line()));
|
||||
}
|
||||
}
|
||||
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
||||
std::unique_ptr<ChunkStore> store() {
|
||||
auto made = ChunkStore::create({}, 4, budget, {{VolumeUnits{1000000}, VolumeUnits{1000000}}});
|
||||
CHECK(made);
|
||||
return std::move(*made);
|
||||
}
|
||||
void put(WorldBytes& b, std::size_t offset, std::uint64_t n, unsigned width) {
|
||||
for (unsigned i = 0; i < width; ++i) {
|
||||
b.at(offset + i) = static_cast<std::uint8_t>(n & 255U);
|
||||
n >>= 8U;
|
||||
}
|
||||
}
|
||||
void seal(WorldBytes& bytes) {
|
||||
put(bytes, 12, bytes.size(), 4);
|
||||
std::uint64_t h = 14695981039346656037ULL;
|
||||
for (std::size_t i = 0; i < bytes.size() - 8; ++i) {
|
||||
h = (h ^ bytes[i]) * 1099511628211ULL;
|
||||
}
|
||||
put(bytes, bytes.size() - 8, h, 8);
|
||||
}
|
||||
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;
|
||||
}
|
||||
void round_trip_and_history() {
|
||||
auto s = store();
|
||||
CHECK(s->load({-1, 0, -2}));
|
||||
CHECK(s->load({0, 0, -2}));
|
||||
std::array<CellCut, 2> first{};
|
||||
std::array<CellCut, 2> latest{};
|
||||
EditReceipt receipt{};
|
||||
for (std::uint64_t id = 1; id <= 65; ++id) {
|
||||
latest = {CellCut{{-1, 0, -2}, {15, 0, 0}, s->view({-1, 0, -2})->revision(), 1},
|
||||
CellCut{{0, 0, -2}, {0, 0, 0}, s->view({0, 0, -2})->revision(), 2}};
|
||||
if (id == 1) {
|
||||
first = latest;
|
||||
}
|
||||
const auto done = s->excavate(request(id, latest));
|
||||
CHECK(done);
|
||||
receipt = *done;
|
||||
}
|
||||
const auto pinned = s->load({5, 5, 5});
|
||||
CHECK(pinned && s->mark_dirty(*pinned));
|
||||
const auto clean = s->load({-5, -5, 5});
|
||||
CHECK(clean);
|
||||
const auto bytes = s->snapshot(identity);
|
||||
CHECK(bytes);
|
||||
auto restored = ChunkStore::restore(*bytes, identity, budget);
|
||||
CHECK(restored);
|
||||
auto& copy = **restored;
|
||||
CHECK(copy.snapshot(identity) == bytes);
|
||||
CHECK(copy.debris({1}) == s->debris({1}));
|
||||
CHECK(*copy.excavate(request(65, latest)) == receipt);
|
||||
const auto stale = copy.excavate(request(1, first));
|
||||
CHECK(!stale && stale.error() == ChunkError::stale_command);
|
||||
latest[0].removed_fill = 2;
|
||||
const auto conflict = copy.excavate(request(65, latest));
|
||||
CHECK(!conflict && conflict.error() == ChunkError::command_conflict);
|
||||
latest[0].expected_revision = copy.view({-1, 0, -2})->revision();
|
||||
latest[1].expected_revision = copy.view({0, 0, -2})->revision();
|
||||
CHECK(copy.excavate(request(66, latest)) == s->excavate(request(66, latest)));
|
||||
CHECK(copy.snapshot(identity) == s->snapshot(identity));
|
||||
CHECK(!copy.evict(*copy.view({-1, 0, -2})));
|
||||
CHECK(copy.evict(*copy.view({-5, -5, 5})));
|
||||
}
|
||||
void journals() {
|
||||
auto s = store();
|
||||
CHECK(s->load({-1, 0, -2}));
|
||||
CHECK(s->load({0, 0, -2}));
|
||||
const auto base = s->snapshot(identity);
|
||||
CHECK(base);
|
||||
std::array<CellCut, 2> a{{{{-1, 0, -2}, {15, 0, 0}, s->view({-1, 0, -2})->revision(), 10},
|
||||
{{0, 0, -2}, {0, 0, 0}, s->view({0, 0, -2})->revision(), 20}}};
|
||||
const auto one = request(3, a);
|
||||
CHECK(s->excavate(one));
|
||||
auto b = a;
|
||||
for (auto& cut : b) {
|
||||
cut.expected_revision = s->view(cut.chunk)->revision();
|
||||
}
|
||||
const auto two = request(8, b);
|
||||
CHECK(s->excavate(two));
|
||||
const std::array<ExcavationRequest, 2> batch{one, two};
|
||||
const auto journal = make_edit_journal(*base, identity, batch, budget);
|
||||
CHECK(journal);
|
||||
auto replay = replay_edit_journal(*base, *journal, identity, budget);
|
||||
CHECK(replay);
|
||||
CHECK((**replay).snapshot(identity) == s->snapshot(identity));
|
||||
const auto nextbase = (**replay).snapshot(identity);
|
||||
CHECK(nextbase);
|
||||
const auto empty = make_edit_journal(*nextbase, identity, {}, budget);
|
||||
CHECK(empty);
|
||||
const auto unchanged = replay_edit_journal(*nextbase, *empty, identity, budget);
|
||||
CHECK(unchanged);
|
||||
CHECK((**unchanged).snapshot(identity) == nextbase);
|
||||
CHECK(!replay_edit_journal(*nextbase, *journal, identity, budget));
|
||||
CHECK(!make_edit_journal(*nextbase, identity, batch, budget));
|
||||
for (std::size_t n = 0; n < journal->size(); ++n) {
|
||||
CHECK(!replay_edit_journal(*base, std::span{*journal}.first(n), identity, budget));
|
||||
}
|
||||
auto corrupt = *journal;
|
||||
corrupt[corrupt.size() - 16] ^= 1;
|
||||
seal(corrupt);
|
||||
CHECK(!replay_edit_journal(*base, corrupt, identity, budget));
|
||||
// A valid first request followed by stale state must not publish a partial candidate.
|
||||
const std::array<ExcavationRequest, 2> bad{one, request(8, a)};
|
||||
CHECK(!make_edit_journal(*base, identity, bad, budget));
|
||||
CHECK(ChunkStore::restore(*base, identity, budget));
|
||||
}
|
||||
void malformed() {
|
||||
auto s = store();
|
||||
const auto v = s->load({0, 0, -2});
|
||||
CHECK(v);
|
||||
const std::array<CellCut, 1> cuts{{{{0, 0, -2}, {}, v->revision(), 100}}};
|
||||
CHECK(s->excavate(request(1, cuts)));
|
||||
const auto saved = s->snapshot(identity);
|
||||
CHECK(saved);
|
||||
for (std::size_t n = 0; n < saved->size(); ++n) {
|
||||
CHECK(!ChunkStore::restore(std::span{*saved}.first(n), identity, budget));
|
||||
}
|
||||
CHECK(!ChunkStore::restore(*saved, {8, 9}, budget));
|
||||
CHECK(!s->snapshot({0, 9}));
|
||||
CHECK(!ChunkStore::restore(*saved, identity, *ChunkStore::minimum_bytes(4)));
|
||||
auto corrupt = *saved;
|
||||
corrupt[200] ^= 1;
|
||||
CHECK(!ChunkStore::restore(corrupt, identity, budget));
|
||||
// Semantic failures must reject even when the corruption checksum is recomputed.
|
||||
for (const auto offset : {8U, 40U, 44U, 48U, 52U}) {
|
||||
corrupt = *saved;
|
||||
put(corrupt, offset, 99, 4);
|
||||
seal(corrupt);
|
||||
CHECK(!ChunkStore::restore(corrupt, identity, budget));
|
||||
}
|
||||
for (const auto offset : {56U, 60U, 120U}) {
|
||||
corrupt = *saved;
|
||||
put(corrupt, offset, 0xffffffffU, 4);
|
||||
seal(corrupt);
|
||||
CHECK(!ChunkStore::restore(corrupt, identity, budget));
|
||||
}
|
||||
corrupt = *saved;
|
||||
put(corrupt, 104, 101, 8);
|
||||
seal(corrupt);
|
||||
CHECK(!ChunkStore::restore(corrupt, identity, budget));
|
||||
corrupt = *saved;
|
||||
put(corrupt, 80, 0, 8);
|
||||
seal(corrupt);
|
||||
CHECK(!ChunkStore::restore(corrupt, identity, budget));
|
||||
corrupt = *saved;
|
||||
put(corrupt, 124, 65536, 8);
|
||||
seal(corrupt);
|
||||
CHECK(!ChunkStore::restore(corrupt, identity, budget));
|
||||
corrupt = *saved;
|
||||
corrupt[156] = 0;
|
||||
seal(corrupt);
|
||||
CHECK(!ChunkStore::restore(corrupt, identity, budget));
|
||||
corrupt = *saved;
|
||||
put(corrupt, 157, 3, 2);
|
||||
seal(corrupt);
|
||||
CHECK(!ChunkStore::restore(corrupt, identity, budget));
|
||||
corrupt = *saved;
|
||||
corrupt.insert(corrupt.end() - 8, 0);
|
||||
seal(corrupt);
|
||||
CHECK(!ChunkStore::restore(corrupt, identity, budget));
|
||||
// First retained record follows the one canonical chunk. Preserve checksum
|
||||
// validity while breaking receipt accounting or revision chronology.
|
||||
constexpr std::size_t history_start = 124 + 16417;
|
||||
corrupt = *saved;
|
||||
put(corrupt, history_start + 24 + 48, 101, 8);
|
||||
seal(corrupt);
|
||||
CHECK(!ChunkStore::restore(corrupt, identity, budget));
|
||||
corrupt = *saved;
|
||||
put(corrupt, history_start + 24 + 48 + 20 + 24, 1, 8);
|
||||
seal(corrupt);
|
||||
CHECK(!ChunkStore::restore(corrupt, identity, budget));
|
||||
// Removing retry history must not yield an apparently valid genesis snapshot
|
||||
// while edited geometry and its debris remain.
|
||||
corrupt = *saved;
|
||||
corrupt.resize(history_start + 8);
|
||||
put(corrupt, 72, 0, 8);
|
||||
put(corrupt, 80, 0, 8);
|
||||
put(corrupt, 120, 0, 4);
|
||||
seal(corrupt);
|
||||
CHECK(!ChunkStore::restore(corrupt, identity, budget));
|
||||
WorldBytes huge(max_world_snapshot_bytes + 1, 0);
|
||||
CHECK(!ChunkStore::restore(huge, identity, budget));
|
||||
CHECK(s->snapshot(identity) == saved);
|
||||
}
|
||||
void empty_and_clean() {
|
||||
auto s = store();
|
||||
const auto empty = s->snapshot(identity);
|
||||
CHECK(empty && *empty == WorldBytes(world_empty_v1.begin(), world_empty_v1.end()));
|
||||
CHECK(ChunkStore::restore(world_empty_v1, identity, budget));
|
||||
auto restored = ChunkStore::restore(*empty, identity, budget);
|
||||
CHECK(restored && (**restored).snapshot(identity) == empty);
|
||||
CHECK(s->load({0, 0, 0}));
|
||||
CHECK(s->load({-3, 7, -5}));
|
||||
const auto clean = s->snapshot(identity);
|
||||
CHECK(clean);
|
||||
restored = ChunkStore::restore(*clean, identity, budget);
|
||||
CHECK(restored && (**restored).snapshot(identity) == clean);
|
||||
}
|
||||
} // namespace
|
||||
int main() try {
|
||||
empty_and_clean();
|
||||
round_trip_and_history();
|
||||
journals();
|
||||
malformed();
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::fprintf(stderr, "%s\n", e.what());
|
||||
return 1;
|
||||
} catch (...) {
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user