Check source and capacity claims in authentic gathering and canceled work
This commit is contained in:
1 parent
67fba6e3df
commit
d613b73e1f
1 file changed
+56
-4
@@ -65,13 +65,30 @@ void reject(std::string bytes, std::string_view message="authenticated malformed
|
||||
require(!decode_checkpoint(bytes,*catalog,1,ExecutionMode::SerialReference), message);
|
||||
}
|
||||
struct CustodyOffsets {
|
||||
std::size_t extension, agent_state, agent_timer, receipts;
|
||||
std::size_t extension, agent_state, agent_timer, first_execution, receipts;
|
||||
std::vector<std::size_t> node_reserved, storage_reserved;
|
||||
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 prefix(std::as_bytes(std::span{bytes.data(),bytes.size()}));
|
||||
const auto skip=[&](std::size_t n){require(prefix.skip(n).has_value(),"incomplete historical prefix");};
|
||||
const auto count=[&]{return prefix.readLittle<std::uint32_t>().value();};
|
||||
const auto route_fields=[&]{const auto n=count();skip(static_cast<std::size_t>(n)*8);};
|
||||
skip(body+109+static_cast<std::size_t>(world.width())*world.height());
|
||||
const auto agents=count();
|
||||
for(std::uint32_t i=0;i<agents;++i){skip(25);route_fields();skip(28+4*item_type_count);route_fields();skip(14);}
|
||||
const auto jobs=count();skip(static_cast<std::size_t>(jobs)*25);
|
||||
const auto nodes=count();
|
||||
for(std::uint32_t i=0;i<nodes;++i){result.node_reserved.push_back(prefix.position()+25);skip(39);}
|
||||
const auto stores=count();
|
||||
for(std::uint32_t i=0;i<stores;++i){result.storage_reserved.push_back(prefix.position()+20);skip(25);const auto items=count();skip(static_cast<std::size_t>(items)*9);}
|
||||
const auto orders=count();
|
||||
for(std::uint32_t i=0;i<orders;++i){skip(18);const auto materials=count();skip(static_cast<std::size_t>(materials)*13);const auto stops=count();for(std::uint32_t j=0;j<stops;++j){skip(8);route_fields();}skip(18);}
|
||||
skip(37);result.extension=prefix.position();
|
||||
result.first_execution=result.extension+24+8*item_type_count+9;
|
||||
for(const auto& store:world.storages())result.first_execution+=8*store.items.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();
|
||||
@@ -85,8 +102,8 @@ CustodyOffsets offsets(const World& world, const std::string& bytes) {
|
||||
(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 receipt_count=receipts.readLittle<std::uint32_t>().value();
|
||||
for(std::uint32_t i=0;i<receipt_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();
|
||||
@@ -128,6 +145,7 @@ struct [[= Void::Testing::name("CustodyMigrationTests")]] CustodyMigrationTests
|
||||
[[= Void::Testing::tag("custody-mutation")]] Test full_receipt_ring_preserves_exact_results_and_evicts_old_ordinals();
|
||||
[[= Void::Testing::tag("custody-mutation")]] Test admission_result_reservations_are_distinct();
|
||||
[[= Void::Testing::tag("custody-mutation")]] Test result_effects_are_unique_with_retired_and_distinct_owners();
|
||||
[[= Void::Testing::tag("custody-mutation")]] Test source_less_harvesting_and_reserved_return_destination_are_rejected();
|
||||
};
|
||||
Test CustodyMigrationTests::authentic_historical_phases_keep_eighty_tick_projection() {
|
||||
for (const auto& original : custody_v14::fixtures) {
|
||||
@@ -376,4 +394,38 @@ Test CustodyMigrationTests::result_effects_are_unique_with_retired_and_distinct_
|
||||
auto duplicate=good;duplicate.replace(first+22,22,good.substr(first,22));
|
||||
reject(std::move(duplicate),"duplicate historical effect accepted");
|
||||
}
|
||||
Test CustodyMigrationTests::source_less_harvesting_and_reserved_return_destination_are_rejected() {
|
||||
auto gathering=load(unhex(fixture("harvest-gathering").checkpoint));
|
||||
const auto original=save(gathering); const auto location=offsets(*gathering.world,original);
|
||||
require(save(load(original))==original,"authentic harvesting original was rejected");
|
||||
auto missing=original;
|
||||
change(missing,location.agent_timer+4,std::uint64_t{0});
|
||||
change(missing,location.first_execution+17,std::uint64_t{0});
|
||||
change(missing,location.node_reserved.front(),std::uint32_t{0});
|
||||
// Remove the source claim together with its cached worker count, keeping the
|
||||
// real destination capacity. A mere cache mismatch must not explain refusal.
|
||||
require(gathering.world->agents().front().execution->reservations[0].kind==ReservationKind::Harvest,"source claim fixture changed");
|
||||
missing.replace(location.first_execution+43,47,47,'\0');
|
||||
reject(std::move(missing),"Gathering without source or source claim accepted");
|
||||
|
||||
auto returning=load(unhex(fixture("cabin-working").checkpoint));
|
||||
require(returning.world->cancel_production(returning.world->production_orders().front().id)==CommandStatus::Accepted,"return fixture cancellation failed");
|
||||
const auto good=save(returning);const auto cancelled=offsets(*returning.world,good);
|
||||
require(save(load(good))==good,"authentic cancellation original was rejected");
|
||||
require(returning.world->agents().front().execution->reservation_count==0,"cancel did not release all reservations");
|
||||
Void::Binary::Reader counter(std::as_bytes(std::span{good.data(),good.size()}).subspan(cancelled.extension+8));
|
||||
const auto next=counter.readLittle<std::uint64_t>().value();
|
||||
const auto destination=returning.world->storages().front().id;
|
||||
auto reserved=good;
|
||||
change(reserved,cancelled.extension+8,next+1);
|
||||
change(reserved,cancelled.first_execution+25,destination);
|
||||
change(reserved,cancelled.first_execution+42,std::uint8_t{1});
|
||||
change(reserved,cancelled.storage_reserved.front(),std::uint32_t{1});
|
||||
std::string capacity(47,'\0');
|
||||
change(capacity,0,next);change(capacity,8,ReservationKind::Capacity);
|
||||
change(capacity,9,OwnerKind::Storage);change(capacity,10,destination);change(capacity,27,std::uint32_t{1});
|
||||
reserved.insert(cancelled.first_execution+43,capacity);
|
||||
change(reserved,16,static_cast<std::uint64_t>(reserved.size()-32-body_begin(reserved)));
|
||||
reject(std::move(reserved),"Returning retained a destination and matching capacity reservation");
|
||||
}
|
||||
} // namespace
|
||||
Reference in new issue
Block a user