Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 40s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (client) (push) Canceled after 1m27s
Foundation / checks (ecs) (push) Canceled after 1m19s
Foundation / checks (network) (push) Canceled after 54s
108 lines
4.5 KiB
C++
108 lines
4.5 KiB
C++
#include "evo/server/cellular_session_batch.hpp"
|
|
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
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<bool>(__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, 3'000, 2'000, {{{0, 0}, 29, {{10}, {{{10}, {10}, {10}, {10}}}}}}};
|
|
auto habitat = biology::CellularHabitat::create(std::move(habitat_config));
|
|
CHECK(habitat);
|
|
auto state = CellularLives::create(**primitive, std::move(*habitat),
|
|
{{3'000, 2'000}, 1, 293'150});
|
|
CHECK(state && (*state)->lives().size() == 2);
|
|
|
|
const auto& controlled = (*state)->lives()[0];
|
|
const auto& other = (*state)->lives()[1];
|
|
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});
|
|
|
|
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));
|
|
|
|
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<std::uint8_t, network::cellular_action_input_bytes>{};
|
|
const std::array malformed_actions{malformed};
|
|
CHECK(prepare_cellular_session_tick(**state, 2, baseline, malformed_actions).error() ==
|
|
CellularSessionBatchError::action);
|
|
}
|
|
} // namespace
|
|
|
|
int main() try {
|
|
actions_bind_to_owned_life_and_preserve_other_inputs();
|
|
std::puts("cellular session batch: ok");
|
|
} catch (const std::exception& e) {
|
|
std::fprintf(stderr, "%s\n", e.what());
|
|
return 1;
|
|
}
|