#include "evo/network/intent_admission.hpp" #include namespace evo::network { namespace { constexpr std::int32_t max_component = 1000; bool valid_vector(std::int16_t x, std::int16_t y) { const std::int32_t ix = x, iy = y; return ix * ix + iy * iy <= max_component * max_component; } bool in_window(std::uint64_t tick, std::uint64_t server_tick, std::uint32_t window) { return tick > server_tick && tick - server_tick <= window; } } // namespace std::expected IntentAdmission::create(IntentLimits limits) { if (limits.intent_window == 0 || limits.intent_window > 64 || limits.window_ticks == 0 || limits.max_intents_per_window == 0 || limits.max_actions_per_window == 0 || limits.max_unresolved == 0 || limits.max_unresolved > 4096 || limits.max_sessions == 0 || limits.max_sessions > 1024 || limits.max_finals == 0 || limits.watermark_every == 0) return std::unexpected(IntentConfigError::invalid); IntentAdmission admission{limits}; admission.sessions_.reserve(limits.max_sessions); return admission; } IntentAdmission::Session* IntentAdmission::find(std::uint64_t session) { const auto it = std::ranges::find_if(sessions_, [&](const Session& s) { return s.id == session; }); return it == sessions_.end() ? nullptr : &*it; } const IntentAdmission::Session* IntentAdmission::find(std::uint64_t session) const { const auto it = std::ranges::find_if(sessions_, [&](const Session& s) { return s.id == session; }); return it == sessions_.end() ? nullptr : &*it; } bool IntentAdmission::open(std::uint64_t session, const AccountId& account) { if (session == 0 || find(session) || sessions_.size() >= limits_.max_sessions) return false; Session s; s.id = session; s.account = account; sessions_.push_back(std::move(s)); return true; } void IntentAdmission::close(std::uint64_t session) { // Unsimulated intentions die with the session; actions belong to the account and stay. std::erase_if(sessions_, [&](const Session& s) { return s.id == session; }); } void IntentAdmission::count_window(Session& s, std::uint64_t server_tick) const { if (server_tick < s.window_start || server_tick - s.window_start >= limits_.window_ticks) { s.window_start = server_tick; s.intent_attempts = 0; s.action_attempts = 0; } } std::size_t IntentAdmission::unresolved_of(const Session& s) const { return s.pending.size() + static_cast(std::ranges::count_if( actions_, [&](const Action& a) { return a.carried.account == s.account; })); } std::size_t IntentAdmission::unresolved(std::uint64_t session) const { const auto* s = find(session); return s ? unresolved_of(*s) : 0; } IntentionReceipt IntentAdmission::intention(std::uint64_t session, const SessionIntention& m, const AdmissionFacts& facts) { IntentionReceipt r{m.sequence, AdmitStatus::admitted, facts.server_tick}; auto* s = find(session); if (!s) { r.status = AdmitStatus::unknown_session; return r; } count_window(*s, facts.server_tick); const bool over = s->intent_attempts >= limits_.max_intents_per_window; if (!over) ++s->intent_attempts; const std::uint64_t last_tick = s->pending.empty() ? 0 : s->pending.back().tick; if (over) r.status = AdmitStatus::rate_limited; else if (m.session != session) r.status = AdmitStatus::unknown_session; else if (!facts.ready) r.status = AdmitStatus::not_ready; else if (m.epoch != facts.epoch) r.status = AdmitStatus::stale_epoch; else if (!valid_vector(m.move_x, m.move_y)) r.status = AdmitStatus::invalid_vector; else if (m.sequence <= s->last_sequence) r.status = AdmitStatus::replayed; // One intention per tick (ADR-0089): ticks advance with the sequence. else if (!in_window(m.tick, facts.server_tick, limits_.intent_window) || m.tick <= last_tick) r.status = AdmitStatus::outside_window; else if (unresolved_of(*s) >= limits_.max_unresolved) r.status = AdmitStatus::too_many_unresolved; if (r.status == AdmitStatus::admitted) { s->last_sequence = m.sequence; s->pending.push_back({m.sequence, m.tick}); } return r; } ActionReceipt IntentAdmission::action(std::uint64_t session, const SessionAction& m, const AdmissionFacts& facts) { ActionReceipt r{m.action_id, AdmitStatus::admitted, facts.server_tick, m.tick}; auto* s = find(session); if (!s) { r.status = AdmitStatus::unknown_session; return r; } count_window(*s, facts.server_tick); const bool over = s->action_attempts >= limits_.max_actions_per_window; if (!over) ++s->action_attempts; const auto& account = s->account; const auto known_action = std::ranges::find_if(actions_, [&](const Action& a) { return a.carried.account == account && a.carried.action_id == m.action_id; }); const auto known_final = std::ranges::find_if(finals_, [&](const AccountOutcome& f) { return f.account == account && f.outcome.action_id == m.action_id; }); if (over) r.status = AdmitStatus::rate_limited; else if (m.session != session) r.status = AdmitStatus::unknown_session; else if (!facts.ready) r.status = AdmitStatus::not_ready; else if (m.epoch != facts.epoch) r.status = AdmitStatus::stale_epoch; else if (m.kind != static_cast(ActionKind::probe) || m.action_id == 0) r.status = AdmitStatus::invalid_action; else if (known_action != actions_.end() || known_final != finals_.end()) { // Never admitted twice; the receipt names the tick it was scheduled for. r.status = AdmitStatus::duplicate; r.tick = known_action != actions_.end() ? known_action->carried.tick : known_final->outcome.tick; } else if (!in_window(m.tick, facts.server_tick, limits_.intent_window)) r.status = AdmitStatus::outside_window; else if (unresolved_of(*s) >= limits_.max_unresolved) r.status = AdmitStatus::too_many_unresolved; else if (ledger_) { // Committed earlier (another epoch, process or region): never admitted again. const auto found = lookup(account, m.action_id); if (found.state == Record::unavailable) { r.status = AdmitStatus::not_ready; // Uniqueness cannot be confirmed now. } else if (found.state == Record::committed) { r.status = AdmitStatus::duplicate; r.tick = found.outcome.tick; remember(account, found.outcome); } } if (r.status == AdmitStatus::admitted) actions_.push_back({{account, m.action_id, m.tick, ActionKind::probe}, Stage::scheduled}); return r; } ActionOutcome IntentAdmission::query(std::uint64_t session, const ActionQuery& m, const AdmissionFacts& facts) { ActionOutcome o{m.action_id, ActionResult::unknown, 0, 0}; const auto* s = find(session); if (!s) return o; const auto account = s->account; const auto pending = std::ranges::find_if(actions_, [&](const Action& a) { return a.carried.account == account && a.carried.action_id == m.action_id; }); if (pending != actions_.end()) { o.result = ActionResult::pending; o.tick = pending->carried.tick; // A batch outcome left unknown by a lost authority is decided by the durable record // once this authority is ready: every earlier epoch's commits are then final. if (pending->unknown && ledger_ && facts.ready) if (const auto found = lookup(account, m.action_id); found.state != Record::unavailable) { o = found.state == Record::committed ? found.outcome : ActionOutcome{m.action_id, ActionResult::not_committed, 0, 0}; actions_.erase(pending); remember(account, o); } return o; } const auto final = std::ranges::find_if(finals_, [&](const AccountOutcome& f) { return f.account == account && f.outcome.action_id == m.action_id; }); if (final != finals_.end()) return final->outcome; // No longer in memory: a committed action is still on record. if (ledger_ && facts.ready) if (const auto found = lookup(account, m.action_id); found.state == Record::committed) { remember(account, found.outcome); return found.outcome; } return o; } std::vector> IntentAdmission::simulated(const AdmissionFacts& facts) { std::vector> out; for (auto& s : sessions_) { while (!s.pending.empty() && s.pending.front().tick <= facts.server_tick) { s.watermark = s.pending.front().sequence; s.pending.pop_front(); } const bool changed = s.watermark != s.sent_watermark; const bool repeat = s.watermark != 0 && (facts.server_tick < s.sent_at_tick || facts.server_tick - s.sent_at_tick >= limits_.watermark_every); if (changed || repeat) { s.sent_watermark = s.watermark; s.sent_at_tick = facts.server_tick; out.emplace_back(s.id, SimulatedThrough{facts.epoch, s.watermark, facts.server_tick}); } } return out; } std::vector IntentAdmission::carry(std::uint64_t tick) { std::vector out; for (auto& a : actions_) if (a.stage == Stage::scheduled && a.carried.tick <= tick) { a.stage = Stage::carried; out.push_back(a.carried); } return out; } void IntentAdmission::remember(const AccountId& account, const ActionOutcome& outcome) { finals_.push_back({account, outcome}); while (finals_.size() > limits_.max_finals) finals_.pop_front(); } std::vector IntentAdmission::committed(std::span actions, std::uint64_t commit_sequence) { std::vector out; for (const auto& c : actions) { ActionOutcome o{c.action_id, ActionResult::committed_no_effect, c.tick, commit_sequence}; // The durable record is the authority: an id carried by two batches (an account that // moved regions with the action pending) keeps the record of the first commit. if (ledger_) if (const auto found = lookup(c.account, c.action_id); found.state == Record::committed) o = found.outcome; std::erase_if(actions_, [&](const Action& a) { return a.carried.account == c.account && a.carried.action_id == c.action_id; }); out.push_back({c.account, o}); remember(c.account, o); } return out; } std::vector IntentAdmission::dropped(std::span actions, bool final) { std::vector out; for (const auto& c : actions) { const auto it = std::ranges::find_if(actions_, [&](const Action& a) { return a.carried.account == c.account && a.carried.action_id == c.action_id; }); if (!final) { if (it != actions_.end()) it->unknown = true; continue; } if (it != actions_.end()) actions_.erase(it); const ActionOutcome o{c.action_id, ActionResult::not_committed, 0, 0}; out.push_back({c.account, o}); remember(c.account, o); } return out; } std::vector IntentAdmission::abandon() { std::vector out; for (auto& a : actions_) if (a.stage == Stage::carried) a.unknown = true; // Decided by a commit report, a drop report or its record. std::erase_if(actions_, [&](const Action& a) { if (a.stage != Stage::scheduled) return false; const ActionOutcome o{a.carried.action_id, ActionResult::not_committed, 0, 0}; out.push_back({a.carried.account, o}); remember(a.carried.account, o); return true; }); for (auto& s : sessions_) s.pending.clear(); // Superseding intentions of a lost epoch are simply dropped. return out; } IntentAdmission::Found IntentAdmission::lookup(const AccountId& account, std::uint64_t action_id) const { Found f; if (!ledger_) return f; const auto answer = ledger_->find(account, action_id); if (!answer.has_value()) return f; const auto& record = answer.value(); f.state = record.has_value() ? Record::committed : Record::absent; if (record.has_value()) f.outcome = record.value(); return f; } std::optional IntentAdmission::session_of(const AccountId& account) const { const auto it = std::ranges::find_if(sessions_, [&](const Session& s) { return s.account == account; }); if (it == sessions_.end()) return std::nullopt; return it->id; } } // namespace evo::network