Validate versioned world checkpoints against installed rule packages

This commit is contained in:
faumaray committed 2026-12-02 21:37:26 +03:00
1 parent de38aff2ff
commit 15da3f4336
7 files changed
+744 -6

No files matched your search

@@ -0,0 +1,65 @@
#pragma once
#include "settlement/core/rules_manifest.hpp"
#include "settlement/core/world.hpp"
#include <Void/Core/Uuid.hpp>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <memory>
#include <span>
#include <string>
#include <string_view>
namespace settlement {
inline constexpr std::uint32_t checkpoint_version = 1;
inline constexpr std::size_t max_checkpoint_body_bytes = 128 * 1024 * 1024;
inline constexpr std::size_t max_checkpoint_metadata_bytes = 64 * 1024;
inline constexpr std::size_t max_checkpoint_bytes =
24 + max_checkpoint_metadata_bytes + max_checkpoint_body_bytes + 32;
enum class CheckpointError : std::uint8_t {
InvalidFrame,
UnsupportedVersion,
LimitExceeded,
IntegrityMismatch,
InvalidIdentity,
RulesMismatch,
InvalidWorld,
ResourceFailure
};
struct CheckpointDiagnostic {
CheckpointError code;
std::string field;
std::string package;
std::string message;
};
struct WorldCheckpoint {
std::unique_ptr<World> world;
Void::Uuid::Uuid world_id;
std::uint64_t city;
RulesManifest rules;
};
// Version 1 stores magic, schema, metadata length and body length, then world UUID,
// city, tick, rules fingerprint and the sorted complete package closure. Each
// package stores its ID, exact version and SHA-256. The body uses World's save
// codec. A final SHA-256 covers every preceding byte. Integers are little-endian.
// The caller holds a stable World for the entire synchronous encode operation.
[[nodiscard]] std::expected<std::string, CheckpointDiagnostic> encode_checkpoint(
const World& world, Void::Uuid::Uuid world_id, std::uint64_t city,
const RulesManifest& rules);
// Input is bounded and integrity checked before package or World decoding.
// Rules are resolved from the installed catalog and must match the complete
// saved closure exactly. The returned candidate is independent of any live world.
[[nodiscard]] std::expected<WorldCheckpoint, CheckpointDiagnostic> decode_checkpoint(
std::string_view bytes, std::span<const RulePackage> installed,
std::size_t workers = min_worker_count, ExecutionMode mode = ExecutionMode::Parallel);
} // namespace settlement
@@ -97,6 +97,7 @@ private:
std::span<const RulePackage> available, std::span<const std::string> selected,
const RulesLimits& limits = {});
[[nodiscard]] std::expected<std::vector<RulePackage>, RulesDiagnostic> builtin_rule_packages();
[[nodiscard]] std::expected<RulesManifest, RulesDiagnostic> default_rules_manifest();
[[nodiscard]] std::string rules_digest_hex(const RulesDigest& digest);