#include "evo/server/postgres_action_ledger.hpp" #include namespace evo::server { std::expected, network::ActionLedgerError> PostgresActionLedger::find(const network::AccountId& account, std::uint64_t action_id) { const auto lease = lease_(); if (!lease) return std::unexpected(network::ActionLedgerError::unavailable); auto* db = db_->get(); if (!db) return std::unexpected(network::ActionLedgerError::unavailable); const auto found = persistence::find_region_action(*db, key_, *lease, account.bytes, action_id, timeout_); if (!found.has_value()) { const auto kind = found.error().kind; if (kind == persistence::LeaseStoreError::store || kind == persistence::LeaseStoreError::unknown) db_->broken(); // Reconnect next time. return std::unexpected(network::ActionLedgerError::unavailable); } const auto& record = found.value(); if (!record.has_value()) return std::optional{}; return std::optional{ network::ActionOutcome{action_id, network::ActionResult::committed_no_effect, record.value().tick, record.value().commit_sequence}}; } std::expected, network::ActionLedgerError> PostgresActionLedger::find_record(const network::AccountId& account, std::uint64_t action_id) { const auto lease = lease_(); if (!lease) return std::unexpected(network::ActionLedgerError::unavailable); auto* db = db_->get(); if (!db) return std::unexpected(network::ActionLedgerError::unavailable); const auto found = persistence::find_region_action_record(*db, key_, *lease, account.bytes, action_id, timeout_); if (!found.has_value()) { const auto kind = found.error().kind; if (kind == persistence::LeaseStoreError::store || kind == persistence::LeaseStoreError::unknown) db_->broken(); return std::unexpected(network::ActionLedgerError::unavailable); } if (!found.value()) return std::optional{}; const auto row = found->value_or(persistence::CommittedActionRecord{}); const auto result = static_cast(row.result); network::DurableActionRecord record{ network::ActionOutcome{action_id, result, row.action.tick, row.action.commit_sequence}, std::nullopt}; if (row.cellular_input) { const auto input = network::decode_cellular_action_input(*row.cellular_input); if (!input || input->tick != row.action.tick || result != network::ActionResult::committed) return std::unexpected(network::ActionLedgerError::unavailable); std::array canonical{}; std::ranges::copy(*row.cellular_input, canonical.begin()); record.cellular_input = canonical; } else if (result != network::ActionResult::committed_no_effect) { return std::unexpected(network::ActionLedgerError::unavailable); } return std::optional{record}; } std::expected>, network::ActionLedgerError> PostgresActionLedger::find_all_records(std::span keys) { std::vector> out(keys.size()); if (keys.empty()) return out; const auto lease = lease_(); if (!lease) return std::unexpected(network::ActionLedgerError::unavailable); auto* db = db_->get(); if (!db) return std::unexpected(network::ActionLedgerError::unavailable); std::vector refs; refs.reserve(keys.size()); for (const auto& key : keys) refs.push_back({key.account.bytes, key.action_id}); const auto found = persistence::find_region_action_records(*db, key_, *lease, refs, timeout_); if (!found.has_value()) { const auto kind = found.error().kind; if (kind == persistence::LeaseStoreError::store || kind == persistence::LeaseStoreError::unknown) db_->broken(); return std::unexpected(network::ActionLedgerError::unavailable); } if (found->size() != keys.size()) return std::unexpected(network::ActionLedgerError::unavailable); for (std::size_t i = 0; i < keys.size(); ++i) { if (!(*found)[i]) continue; const auto row = (*found)[i].value_or(persistence::CommittedActionRecord{}); const auto result = static_cast(row.result); network::DurableActionRecord record{network::ActionOutcome{keys[i].action_id, result, row.action.tick, row.action.commit_sequence}, std::nullopt}; if (row.cellular_input) { const auto input = network::decode_cellular_action_input(*row.cellular_input); if (!input || input->tick != row.action.tick || result != network::ActionResult::committed) return std::unexpected(network::ActionLedgerError::unavailable); std::array canonical{}; std::ranges::copy(*row.cellular_input, canonical.begin()); record.cellular_input = canonical; } else if (result != network::ActionResult::committed_no_effect) { return std::unexpected(network::ActionLedgerError::unavailable); } out[i] = record; } return out; } std::expected>, network::ActionLedgerError> PostgresActionLedger::find_all(std::span keys) { const auto lease = lease_(); if (!lease) return std::unexpected(network::ActionLedgerError::unavailable); auto* db = db_->get(); if (!db) return std::unexpected(network::ActionLedgerError::unavailable); std::vector refs; refs.reserve(keys.size()); for (const auto& k : keys) refs.push_back({k.account.bytes, k.action_id}); const auto found = persistence::find_region_actions(*db, key_, *lease, refs, timeout_); if (!found.has_value()) { const auto kind = found.error().kind; if (kind == persistence::LeaseStoreError::store || kind == persistence::LeaseStoreError::unknown) db_->broken(); // Reconnect next time. return std::unexpected(network::ActionLedgerError::unavailable); } std::vector> out; out.reserve(keys.size()); for (std::size_t i = 0; i < keys.size(); ++i) { const auto& record = (*found)[i]; if (record.has_value()) out.emplace_back(network::ActionOutcome{keys[i].action_id, network::ActionResult::committed_no_effect, record->tick, record->commit_sequence}); else out.emplace_back(); } return out; } } // namespace evo::server