Bound save expansion and test authentic custody migration and journal wrap
This commit is contained in:
1 parent
539cea0608
commit
a76bf87787
4 files changed
+152
-7
No files matched your search
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
@@ -49,7 +50,8 @@ std::size_t body_begin(std::string_view bytes) {
|
||||
}
|
||||
template<class T> void change(std::string& bytes, std::size_t offset, T value) {
|
||||
Void::Binary::Writer writer(std::as_writable_bytes(std::span{bytes.data(), bytes.size()}).subspan(offset));
|
||||
require(writer.writeLittle(value).has_value(), "mutation exceeded frame");
|
||||
if constexpr(std::is_enum_v<T>) require(writer.writeLittle(static_cast<std::underlying_type_t<T>>(value)).has_value(), "mutation exceeded frame");
|
||||
else require(writer.writeLittle(value).has_value(), "mutation exceeded frame");
|
||||
}
|
||||
void rehash(std::string& bytes) {
|
||||
auto hasher = Void::Crypto::Hasher::create(Void::Crypto::HashAlgorithm::Sha256);
|
||||
@@ -62,6 +64,37 @@ void reject(std::string bytes) {
|
||||
auto catalog = builtin_rule_packages();
|
||||
require(!decode_checkpoint(bytes,*catalog,1,ExecutionMode::SerialReference), "authenticated malformed custody was accepted");
|
||||
}
|
||||
struct CustodyOffsets {
|
||||
std::size_t extension, agent_state, agent_timer, receipts;
|
||||
std::vector<std::pair<std::size_t,std::size_t>> entries;
|
||||
};
|
||||
CustodyOffsets offsets(const World& world, const std::string& bytes) {
|
||||
CustodyOffsets result{};
|
||||
const auto body=body_begin(bytes);
|
||||
result.extension=body+raw(world,WorldSaveFormat::Current14).size();
|
||||
Void::Binary::Reader reader(std::as_bytes(std::span{bytes.data(),bytes.size()}));
|
||||
require(reader.skip(body+109+static_cast<std::size_t>(world.width())*world.height()+4+16).has_value(),"missing agent state");
|
||||
result.agent_state=reader.position();
|
||||
require(reader.skip(9).has_value(),"missing agent route");
|
||||
const auto route=reader.readLittle<std::uint32_t>().value();
|
||||
require(reader.skip(static_cast<std::size_t>(route)*8+8).has_value(),"missing agent timer");
|
||||
result.agent_timer=reader.position();
|
||||
std::size_t extension_size=24+world.production_orders().size()*(12*item_type_count+1)+64*item_type_count;
|
||||
for(const auto& store:world.storages()) extension_size+=store.items.size()*8;
|
||||
for(const auto& agent:world.agents()) extension_size+=8*item_type_count+9+
|
||||
(agent.execution?43+47*agent.execution->reservation_count:0);
|
||||
result.receipts=result.extension+extension_size;
|
||||
Void::Binary::Reader receipts(std::as_bytes(std::span{bytes.data(),bytes.size()}).subspan(result.receipts));
|
||||
const auto count=receipts.readLittle<std::uint32_t>().value();
|
||||
for(std::uint32_t i=0;i<count;++i){
|
||||
const auto request=result.receipts+receipts.position();
|
||||
const auto request_size=receipts.readLittle<std::uint16_t>().value();require(receipts.skip(request_size).has_value(),"bad request fixture");
|
||||
const auto response=result.receipts+receipts.position();
|
||||
const auto result_size=receipts.readLittle<std::uint16_t>().value();require(receipts.skip(result_size).has_value(),"bad result fixture");
|
||||
result.entries.emplace_back(request,response);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
void legacy_reject(const World& world) {
|
||||
for (auto format : {WorldSaveFormat::Legacy13, WorldSaveFormat::Current14}) {
|
||||
std::ostringstream out(std::ios::binary);
|
||||
@@ -78,6 +111,10 @@ struct [[= Void::Testing::name("CustodyMigrationTests")]] CustodyMigrationTests
|
||||
[[= Void::Testing::tag("custody-migration")]] Test completed_receipts_keep_retired_stock_and_order_references();
|
||||
[[= Void::Testing::tag("custody-migration")]] Test single_finished_material_and_partial_return_use_modern_format();
|
||||
[[= Void::Testing::tag("custody-migration")]] Test suspended_sites_and_zero_completion_use_modern_format();
|
||||
[[= Void::Testing::tag("custody-mutation")]] Test authenticated_phase_and_receipt_mutations_are_rejected();
|
||||
[[= Void::Testing::tag("custody-mutation")]] Test retired_zero_effect_and_bounded_receipt_fields();
|
||||
[[= Void::Testing::tag("custody-migration")]] Test truncated_collection_counts_allocate_incrementally();
|
||||
[[= Void::Testing::tag("custody-mutation")]] Test full_receipt_ring_preserves_exact_results_and_evicts_old_ordinals();
|
||||
};
|
||||
Test CustodyMigrationTests::authentic_historical_phases_keep_eighty_tick_projection() {
|
||||
for (const auto& original : custody_v14::fixtures) {
|
||||
@@ -211,4 +248,84 @@ Test CustodyMigrationTests::suspended_sites_and_zero_completion_use_modern_forma
|
||||
require(save(pending)==encoded,"pending zero timer did not roundtrip");
|
||||
pending.world->step();pending.world->validate_custody();
|
||||
}
|
||||
Test CustodyMigrationTests::authenticated_phase_and_receipt_mutations_are_rejected() {
|
||||
auto metadata=load(unhex(fixture("cabin-before-pickup").checkpoint));
|
||||
metadata.world->step();
|
||||
const auto good=save(metadata); const auto location=offsets(*metadata.world,good);
|
||||
require(location.entries.size()==1,"pickup should produce one retained operation");
|
||||
const auto [request,result]=location.entries.front();
|
||||
auto wrong_transfer=good;
|
||||
change(wrong_transfer,location.extension+16,std::uint64_t{3});
|
||||
change(wrong_transfer,result+3,std::uint64_t{2});reject(std::move(wrong_transfer));
|
||||
auto future=good;change(future,request+11,std::numeric_limits<std::uint64_t>::max());reject(std::move(future));
|
||||
auto source=good;change(source,request+29,std::numeric_limits<std::uint64_t>::max());reject(std::move(source));
|
||||
metadata.world=std::make_unique<World>(43,20,8,1,ExecutionMode::SerialReference);
|
||||
static_cast<void>(metadata.world->spawn_agent({1,3}));
|
||||
const auto idle=save(metadata); const auto idle_locations=offsets(*metadata.world,idle);
|
||||
for(auto state:{AgentState::Working,AgentState::Gathering,AgentState::FetchingMaterials}) {
|
||||
auto bad=idle;change(bad,idle_locations.agent_state,state);reject(std::move(bad));
|
||||
}
|
||||
auto timer=idle;change(timer,idle_locations.agent_timer,std::uint32_t{1});reject(std::move(timer));
|
||||
}
|
||||
Test CustodyMigrationTests::retired_zero_effect_and_bounded_receipt_fields() {
|
||||
auto metadata=load(unhex(fixture("meal-eating").checkpoint));
|
||||
for(int tick=0;tick<8;++tick)metadata.world->step();
|
||||
const auto good=save(metadata); const auto location=offsets(*metadata.world,good);
|
||||
require(!location.entries.empty(),"meal did not produce historical result");
|
||||
const auto [request,result]=location.entries.back();
|
||||
auto loaded=load(good);
|
||||
const auto receipt=metadata.world->custody_receipts().back();
|
||||
require(receipt->result.effect_count==1&&receipt->result.effects[0].count==0,"terminal consumption lost retired zero-count effect");
|
||||
require(loaded.world->custody_request(receipt->request)==receipt->result,"zero-count historical effect changed after restore");
|
||||
auto oversized=good;change(oversized,request,std::uint16_t{1025});reject(std::move(oversized));
|
||||
auto too_many=good;change(too_many,location.receipts,std::uint32_t{4097});reject(std::move(too_many));
|
||||
auto bad_item=good;change(bad_item,result+2+12+17,ItemType::None);reject(std::move(bad_item));
|
||||
auto bad_owner=good;change(bad_owner,result+2+12,OwnerKind::None);reject(std::move(bad_owner));
|
||||
auto count=good;change(count,result+2+10,std::uint8_t{12});reject(std::move(count));
|
||||
}
|
||||
Test CustodyMigrationTests::truncated_collection_counts_allocate_incrementally() {
|
||||
auto original=unhex(fixture("cabin-before-pickup").checkpoint);
|
||||
const auto begin=body_begin(original);
|
||||
std::string body=original.substr(begin,original.size()-begin-32);
|
||||
constexpr std::size_t agents_count=109+20*8;
|
||||
body.resize(agents_count+4);
|
||||
for(auto count:{1'000'000U,1'000'001U}) {
|
||||
change(body,agents_count,count);
|
||||
std::istringstream input(body,std::ios::binary);
|
||||
bool failed=false;
|
||||
try{static_cast<void>(World::read_save(input,1,ExecutionMode::SerialReference));}
|
||||
catch(const std::runtime_error&){failed=true;}
|
||||
require(failed,"truncated huge agent declaration was accepted");
|
||||
}
|
||||
// Many byte-light historical agents remain valid despite larger modern
|
||||
// in-memory Agent records; the wire ceiling does not become a sizeof cap.
|
||||
World world(43,3,3,1,ExecutionMode::SerialReference);
|
||||
for(int i=0;i<2000;++i)static_cast<void>(world.spawn_agent({1,1}));
|
||||
const auto bytes=raw(world,WorldSaveFormat::Current14);
|
||||
std::istringstream input(bytes,std::ios::binary);
|
||||
auto loaded=World::read_save(input,1,ExecutionMode::SerialReference);
|
||||
require(loaded->agents().size()==2000&&raw(*loaded,WorldSaveFormat::Current14)==bytes,"byte-light history failed migration");
|
||||
}
|
||||
Test CustodyMigrationTests::full_receipt_ring_preserves_exact_results_and_evicts_old_ordinals() {
|
||||
auto metadata=load(unhex(fixture("meal-eating").checkpoint));
|
||||
metadata.world=std::make_unique<World>(43,20,8,1,ExecutionMode::SerialReference);
|
||||
const auto storage=metadata.world->add_storage({3,3},2050);
|
||||
metadata.world->stock_storage(storage,ItemType::Wood,2050);
|
||||
static_cast<void>(metadata.world->spawn_agent({3,3}));
|
||||
for(int i=0;i<2050;++i)metadata.world->queue_production({3,3},RecipeId::SawPlank);
|
||||
for(int tick=0;tick<30000&&metadata.world->custody_receipts().size()<4096;++tick)metadata.world->step();
|
||||
require(metadata.world->custody_receipts().size()==4096,"real production did not fill global receipt ring");
|
||||
const auto remembered=metadata.world->custody_receipts().front();
|
||||
const auto full=save(metadata);auto loaded=load(full);
|
||||
require(save(loaded)==full&&loaded.world->custody_receipts().size()==4096,"full receipt ring was truncated or reordered");
|
||||
require(full.size()<2*1024*1024,"typed receipt ring exceeded its proven wire budget");
|
||||
require(loaded.world->custody_request(remembered->request)==remembered->result,"full ring lost exact replay");
|
||||
auto conflicting=remembered->request;conflicting.destination=0;
|
||||
require(loaded.world->custody_request(conflicting).status==CustodyStatus::Conflict,"exact request conflict was not detected");
|
||||
for(int tick=0;tick<30;++tick)loaded.world->step();
|
||||
require(loaded.world->custody_request(remembered->request).status==CustodyStatus::Stale,"evicted admission was eligible for replay");
|
||||
const auto wrapped=save(loaded);auto copy=load(wrapped);
|
||||
require(save(copy)==wrapped&©.world->custody_request(remembered->request).status==CustodyStatus::Stale,"wrapped ring changed order or stale fence on restore");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
Reference in new issue
Block a user