#include "evo/server/cellular_session_batch.hpp" #include #include #include #include #include #include #include namespace { using namespace evo; using namespace evo::server; void check(bool value, std::source_location at = std::source_location::current()) { if (!value) throw std::runtime_error("cellular session batch line " + std::to_string(at.line())); } #define CHECK(...) check(static_cast(__VA_ARGS__)) accounting::Inventory founder_price(std::uint64_t count) { const auto endowment = biology::primitive_endowment(); CHECK(endowment); auto funding = endowment->body; funding.energy.value = endowment->reserve.energy.value + endowment->construction.value; funding.energy.value *= count; for (auto& channel : funding.matter) channel.value *= count; return funding; } void actions_bind_to_owned_life_and_preserve_other_inputs() { auto primitive = PrimitiveLives::create({90, 1, 7, 17, founder_price(2), {{0, 0}, {20, 0}}}); CHECK(primitive); LifeRequest request{}; request.account.back() = 1; request.action = 1; CHECK((*primitive)->spawn(request, {1, 101, 201, 301, {1}})); request.account.back() = 2; CHECK((*primitive)->spawn(request, {2, 102, 202, 302, {1}})); biology::CellularHabitatConfig habitat_config{ 90, 1, 7, 20'000, 20'000, {{{0, 0}, 29, {{10}, {{{10}, {10}, {10}, {10}}}}}, {{15'000, 15'000}, 30, {{10}, {{{10}, {10}, {10}, {10}}}}}}}; auto habitat = biology::CellularHabitat::create(std::move(habitat_config)); CHECK(habitat); auto state = CellularLives::create(**primitive, std::move(*habitat), {{20'000, 20'000}, 1, 293'150}); CHECK(state && (*state)->lives().size() == 2); const auto& controlled = (*state)->lives()[0]; const auto& other = (*state)->lives()[1]; const auto visible = (*state)->perceive(controlled.identity.life); CHECK(visible && visible->size() == 1 && visible->front().address == (biology::CellularPatchAddress{0, 0})); const std::array baseline{CellularLifeIntent{controlled.identity.life, {{5, 6}, {}}}, CellularLifeIntent{other.identity.life, {{7, 8}, {}}}}; const auto input = network::encode_cellular_action_input({2, -3, 4, true, 0, 0}); network::CarriedAction action{network::AccountId{controlled.account}, 77, 2, network::ActionKind::cellular, input}; const std::array actions{action}; const auto prepared = prepare_cellular_session_tick(**state, 2, baseline, actions); CHECK(prepared && prepared->intents.lives.size() == 2 && prepared->actions.size() == 1); CHECK(prepared->intents.lives[0] == CellularLifeIntent{controlled.identity.life, {{-3, 4}, biology::CellularPatchAddress{0, 0}}}); CHECK(prepared->intents.lives[1] == baseline[1]); CHECK(prepared->actions[0] == CellularActionRecord{controlled.account, 77, input}); const auto guessed_input = network::encode_cellular_action_input({2, 0, 0, true, 15'000, 15'000}); const network::CarriedAction guessed{network::AccountId{controlled.account}, 78, 2, network::ActionKind::cellular, guessed_input}; const std::array guessed_actions{guessed}; CHECK(prepare_cellular_session_tick(**state, 2, baseline, guessed_actions).error() == CellularSessionBatchError::unperceived_target); CellularBatch batch; batch.header = {(*state)->world(), (*state)->region(), (*state)->frame(), {}, {}, 1, 2}; batch.header.predecessor.fill(1); batch.header.predecessor_digest.fill(2); batch.ticks.push_back(prepared->intents); CHECK(match_cellular_actions_to_trace(**state, batch, prepared->actions)); persistence::RegionHead head{}; head.frame = (*state)->frame(); head.sequence = 4; head.end_tick = (*state)->tick(); head.commit.fill(1); head.digest.fill(2); auto builder = CellularSessionBatchBuilder::create(**state, head); CHECK(builder); CHECK(builder->append(2, baseline, actions)); std::vector next_baseline; for (const auto& life : builder->candidate().lives()) next_baseline.push_back({life.identity.life, {}}); CHECK(builder->append(3, next_baseline, {})); CHECK(builder->batch().header.begin_tick == 1 && builder->batch().header.end_tick == 3 && builder->batch().ticks.size() == 2); CHECK(builder->actions().size() == 1 && builder->actions()[0] == prepared->actions[0]); const auto replayed = replay_cellular_batch(**state, builder->batch()); CHECK(replayed && (*replayed)->snapshot() == builder->candidate().snapshot()); const auto repeated_input = network::encode_cellular_action_input({4, 0, 0, false, 0, 0}); const network::CarriedAction repeated{network::AccountId{controlled.account}, 77, 4, network::ActionKind::cellular, repeated_input}; const std::array repeated_actions{repeated}; CHECK(builder->append(4, next_baseline, repeated_actions).error() == CellularSessionBatchError::duplicate_action); CHECK(builder->append(5, next_baseline, {}).error() == CellularSessionBatchError::tick); auto wrong_head = head; ++wrong_head.end_tick; CHECK(CellularSessionBatchBuilder::create(**state, wrong_head).error() == CellularSessionBatchError::head); auto altered = baseline; altered[1].intent.movement.x = 10; const auto preserved = prepare_cellular_session_tick(**state, 2, altered, actions); CHECK(preserved && preserved->intents.lives[1].intent.movement.x == 10); CHECK(prepare_cellular_session_tick(**state, 3, baseline, actions).error() == CellularSessionBatchError::tick); CHECK( prepare_cellular_session_tick(**state, 2, std::span{baseline}.first(1), actions).error() == CellularSessionBatchError::intents); const std::array duplicate{action, action}; CHECK(prepare_cellular_session_tick(**state, 2, baseline, duplicate).error() == CellularSessionBatchError::duplicate_action); auto unknown = action; unknown.account.bytes.back() = 3; const std::array unknown_actions{unknown}; CHECK(prepare_cellular_session_tick(**state, 2, baseline, unknown_actions).error() == CellularSessionBatchError::life); auto malformed = action; malformed.cellular_input = std::array{}; const std::array malformed_actions{malformed}; CHECK(prepare_cellular_session_tick(**state, 2, baseline, malformed_actions).error() == CellularSessionBatchError::action); } void builder_batches_reserved_birth_after_empty_tick() { auto primitive = PrimitiveLives::create({93, 1, 1, 17, founder_price(1), {{0, 0}}}); CHECK(primitive); auto habitat = biology::CellularHabitat::create({93, 1, 1, 1'000, 1'000, {}}); CHECK(habitat); auto state = CellularLives::create(**primitive, std::move(*habitat), {{1'000, 1'000}, 0, 293'150}); CHECK(state && (*state)->lives().empty()); persistence::RegionHead head{}; head.frame = 1; head.commit.fill(1); head.digest.fill(2); auto builder = CellularSessionBatchBuilder::create(**state, head); CHECK(builder); CellularTickIntents::Birth birth{}; birth.account.back() = 8; birth.request = 13; birth.generation = 4; birth.identity = 901; const std::array births{birth}; const std::span no_lives{}; CHECK(builder->append(1, no_lives, {}, births)); CHECK(builder->candidate().tick() == 1 && builder->candidate().lives().size() == 1 && builder->batch().ticks[0].births == std::vector{birth} && (*state)->lives().empty()); const auto replayed = replay_cellular_batch(**state, builder->batch()); CHECK(replayed && (*replayed)->snapshot() == builder->candidate().snapshot()); const auto snapshot = builder->candidate().snapshot(); const std::array next_lives{CellularLifeIntent{901, {}}}; CHECK(builder->append(2, next_lives, {}, births).error() == CellularSessionBatchError::duplicate_birth); CHECK(builder->candidate().snapshot() == snapshot && builder->batch().ticks.size() == 1); } } // namespace int main() try { actions_bind_to_owned_life_and_preserve_other_inputs(); builder_batches_reserved_birth_after_empty_tick(); std::puts("cellular session batch: ok"); } catch (const std::exception& e) { std::fprintf(stderr, "%s\n", e.what()); return 1; }