Format M11 cellular code with clang-format 19
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m52s
Foundation / checks (client) (push) Failing after 4m25s
Foundation / checks (network) (push) Failing after 3m2s
Foundation / checks (tsan) (push) Failing after 33s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 5m47s
Foundation / checks (postgres) (push) Canceled after 1m24s

This commit is contained in:
dingo
2026-10-11 02:59:41 -04:00
parent b8a1288ee9
commit c81c1bfc56
25 changed files with 371 additions and 356 deletions
+18 -21
View File
@@ -144,11 +144,9 @@ IntentAdmission::scheduled_cellular_intentions(std::uint64_t tick) const {
return out;
}
ActionReceipt IntentAdmission::screen(Session& s, const SessionAction& m,
const AdmissionFacts& facts, bool& check,
std::optional<std::array<std::uint8_t,
cellular_action_input_bytes>>
cellular_input) {
ActionReceipt IntentAdmission::screen(
Session& s, const SessionAction& m, const AdmissionFacts& facts, bool& check,
std::optional<std::array<std::uint8_t, cellular_action_input_bytes>> cellular_input) {
ActionReceipt r{m.action_id, AdmitStatus::admitted, facts.server_tick, m.tick};
check = false;
count_window(s, facts.server_tick);
@@ -174,15 +172,14 @@ ActionReceipt IntentAdmission::screen(Session& s, const SessionAction& m,
m.kind != static_cast<std::uint16_t>(ActionKind::cellular)) ||
(m.kind == static_cast<std::uint16_t>(ActionKind::cellular)) !=
cellular_input.has_value() ||
m.action_id == 0 ||
(cellular_input && !decode_cellular_action_input(*cellular_input)))
m.action_id == 0 || (cellular_input && !decode_cellular_action_input(*cellular_input)))
r.status = AdmitStatus::invalid_action;
else if (known_action != actions_.end() || known_final != finals_.end()) {
const auto same = [&](ActionKind kind,
const std::optional<std::array<std::uint8_t,
cellular_action_input_bytes>>& bytes) {
return static_cast<std::uint16_t>(kind) == m.kind && bytes == cellular_input;
};
const auto same =
[&](ActionKind kind,
const std::optional<std::array<std::uint8_t, cellular_action_input_bytes>>& bytes) {
return static_cast<std::uint16_t>(kind) == m.kind && bytes == cellular_input;
};
if (known_action != actions_.end()) {
r.tick = known_action->carried.tick;
r.status = same(known_action->carried.kind, known_action->carried.cellular_input)
@@ -216,8 +213,8 @@ void IntentAdmission::decide(
}
r.status = AdmitStatus::duplicate;
r.tick = found.outcome.tick;
remember(account, found.outcome,
cellular_input ? ActionKind::cellular : ActionKind::probe, cellular_input);
remember(account, found.outcome, cellular_input ? ActionKind::cellular : ActionKind::probe,
cellular_input);
}
}
@@ -250,8 +247,8 @@ ActionReceipt IntentAdmission::action(std::uint64_t session, const CellularActio
if (check)
decide(s->account, r, lookup(s->account, m.action_id), bytes);
if (r.status == AdmitStatus::admitted)
actions_.push_back({{s->account, m.action_id, m.tick, ActionKind::cellular, bytes},
Stage::scheduled});
actions_.push_back(
{{s->account, m.action_id, m.tick, ActionKind::cellular, bytes}, Stage::scheduled});
return r;
}
@@ -277,9 +274,8 @@ std::optional<ActionReceipt> IntentAdmission::admit(std::uint64_t session, const
return r;
}
std::optional<ActionReceipt> IntentAdmission::admit(std::uint64_t session,
const CellularAction& m,
const AdmissionFacts& facts) {
std::optional<ActionReceipt> IntentAdmission::admit(std::uint64_t session, const CellularAction& m,
const AdmissionFacts& facts) {
const SessionAction base{m.session, m.epoch, m.action_id,
static_cast<std::uint16_t>(ActionKind::cellular), m.tick};
const auto bytes = encode_cellular_action_input(
@@ -452,8 +448,9 @@ std::vector<AccountOutcome> IntentAdmission::committed(std::span<const CarriedAc
}
for (std::size_t i = 0; i < actions.size(); ++i) {
const auto& c = actions[i];
const auto local_result = c.kind == ActionKind::cellular ? ActionResult::committed
: ActionResult::committed_no_effect;
const auto local_result = c.kind == ActionKind::cellular
? ActionResult::committed
: ActionResult::committed_no_effect;
ActionOutcome o{c.action_id, local_result, c.tick, commit_sequence};
if (records.has_value())
if (const auto& record = (*records)[i]; record.has_value())
+63 -62
View File
@@ -23,16 +23,16 @@ CellularPublicationFailure failure(PublicationError kind, ObjectError object = {
CellularPreparedCommit prepared = {}) {
return {kind, store, object, std::move(prepared)};
}
std::expected<void, CellularPublicationFailure>
put_verified(const FilesystemObjectStore& objects, std::span<const std::uint8_t> bytes,
VerifiedObject expected) {
std::expected<void, CellularPublicationFailure> put_verified(const FilesystemObjectStore& objects,
std::span<const std::uint8_t> bytes,
VerifiedObject expected) {
auto stored = objects.put(bytes);
if (!stored)
return std::unexpected{failure(PublicationError::upload, stored.error())};
auto readback = objects.get(*stored);
if (!readback || *stored != expected || !std::ranges::equal(*readback, bytes))
return std::unexpected{failure(PublicationError::verify,
readback ? ObjectError::corrupt : readback.error())};
return std::unexpected{
failure(PublicationError::verify, readback ? ObjectError::corrupt : readback.error())};
return {};
}
std::expected<CommittedCheckpoint, CellularPublicationFailure>
@@ -46,28 +46,29 @@ confirm(const RegionHead& head, const CellularPreparedCommit& prepared,
return CommittedCheckpoint{head, prepared.manifest};
}
bool prepared_is_canonical(const CellularPreparedCommit& prepared,
const CheckpointManifest& manifest,
const server::CellularBatch& trace) {
const CheckpointManifest& manifest, const server::CellularBatch& trace) {
const auto decoded_actions = server::decode_cellular_action_block(
prepared.actions, manifest.begin_tick, manifest.end_tick);
const auto encoded_actions = decoded_actions
? server::encode_cellular_action_block(*decoded_actions, manifest.begin_tick,
manifest.end_tick)
: std::expected<std::vector<std::uint8_t>, server::CellularActionBlockError>{
std::unexpected{server::CellularActionBlockError::format}};
const auto encoded_actions =
decoded_actions
? server::encode_cellular_action_block(*decoded_actions, manifest.begin_tick,
manifest.end_tick)
: std::expected<std::vector<std::uint8_t>, server::CellularActionBlockError>{
std::unexpected{server::CellularActionBlockError::format}};
const auto encoded_trace = server::encode_cellular_batch(trace);
const auto digest = sha256(prepared.trace);
return manifest.profile == CheckpointProfile::cellular_lives_v1 &&
manifest.key == prepared.key && manifest.commit == prepared.commit &&
manifest.sequence > 0 && manifest.batch.trace_bytes == prepared.trace.size() &&
digest && *digest == manifest.batch.trace_sha256 &&
manifest.batch.decision_count == 0 &&
manifest.batch.decisions_sha256 == Sha256Digest{} && trace.header.world == prepared.key.world &&
trace.header.region == prepared.key.region && trace.header.frame == manifest.frame &&
manifest.sequence > 0 && manifest.batch.trace_bytes == prepared.trace.size() && digest &&
*digest == manifest.batch.trace_sha256 && manifest.batch.decision_count == 0 &&
manifest.batch.decisions_sha256 == Sha256Digest{} &&
trace.header.world == prepared.key.world && trace.header.region == prepared.key.region &&
trace.header.frame == manifest.frame &&
trace.header.predecessor == manifest.predecessor_commit &&
trace.header.predecessor_digest == manifest.predecessor_digest &&
trace.header.begin_tick == manifest.begin_tick && trace.header.end_tick == manifest.end_tick &&
encoded_actions && *encoded_actions == prepared.actions && encoded_trace &&
trace.header.begin_tick == manifest.begin_tick &&
trace.header.end_tick == manifest.end_tick && encoded_actions &&
*encoded_actions == prepared.actions && encoded_trace &&
*encoded_trace == prepared.trace;
}
} // namespace
@@ -77,16 +78,17 @@ provision_cellular_region(Postgres& owner, const FilesystemObjectStore& objects,
std::uint64_t frame, const server::CellularLives& opening, Uuid commit,
const Sha256Digest& build, milliseconds timeout) {
const auto payload = opening.snapshot();
if (opening.world() != key.world || opening.region() != key.region || opening.frame() != frame ||
opening.tick() != 0 || !opening.lives().empty() || !opening.corpses().empty() ||
opening.archive().recorded() != 0 || opening.archive().archived() != 0)
if (opening.world() != key.world || opening.region() != key.region ||
opening.frame() != frame || opening.tick() != 0 || !opening.lives().empty() ||
!opening.corpses().empty() || opening.archive().recorded() != 0 ||
opening.archive().archived() != 0)
return std::unexpected{failure(PublicationError::candidate)};
const auto digest = sha256(payload);
if (!digest)
return std::unexpected{failure(PublicationError::digest)};
auto manifest = genesis_manifest(key, frame, commit, biology::cellular_motion_tick_microseconds,
build, payload.size(), *digest,
CheckpointProfile::cellular_lives_v1);
auto manifest =
genesis_manifest(key, frame, commit, biology::cellular_motion_tick_microseconds, build,
payload.size(), *digest, CheckpointProfile::cellular_lives_v1);
auto encoded = manifest ? encode_manifest(*manifest)
: std::expected<std::vector<std::uint8_t>, CheckpointError>{
std::unexpected{CheckpointError::format}};
@@ -140,10 +142,10 @@ retry_cellular_prepared(Postgres& db, const CellularPreparedCommit& prepared,
auto result = publish_cellular_checkpoint(db, prepared.manifest, {}, prepared.actions,
prepared.trace, timeout);
if (!result) {
return std::unexpected{failure(
result.error().kind == LeaseStoreError::unknown ? PublicationError::unknown
: PublicationError::rejected,
{}, result.error(), prepared)};
return std::unexpected{failure(result.error().kind == LeaseStoreError::unknown
? PublicationError::unknown
: PublicationError::rejected,
{}, result.error(), prepared)};
}
return confirm(*result, prepared, *manifest);
}
@@ -155,29 +157,29 @@ resolve_cellular_prepared(Postgres& db, const CellularPreparedCommit& prepared,
auto trace = server::decode_cellular_batch(prepared.trace);
if (!manifest || !trace || !prepared_is_canonical(prepared, *manifest, *trace))
return std::unexpected{LeaseStoreFailure{LeaseStoreError::invalid, {}}};
return resolve_cellular_region_commit(db, prepared.key, prepared.commit, prepared.manifest,
{}, prepared.actions, prepared.trace, timeout);
return resolve_cellular_region_commit(db, prepared.key, prepared.commit, prepared.manifest, {},
prepared.actions, prepared.trace, timeout);
}
std::expected<PublishedCellularBatch, CellularPublicationFailure>
publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects,
const LeaseGrant& lease, const CommittedCheckpoint& predecessor,
const server::CellularBatch& batch, const server::CellularLives& candidate,
Uuid commit, const Sha256Digest& build, milliseconds timeout,
const PublishHooks& hooks,
publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
const CommittedCheckpoint& predecessor, const server::CellularBatch& batch,
const server::CellularLives& candidate, Uuid commit,
const Sha256Digest& build, milliseconds timeout, const PublishHooks& hooks,
std::span<const server::CellularActionRecord> actions) {
const auto previous = decode_manifest(predecessor.manifest);
const auto predecessor_digest = sha256(predecessor.manifest);
const auto trace = server::encode_cellular_batch(batch);
const auto action_block = server::encode_cellular_action_block(
actions, batch.header.begin_tick, batch.header.end_tick);
const auto action_block = server::encode_cellular_action_block(actions, batch.header.begin_tick,
batch.header.end_tick);
if (!previous || !predecessor_digest || !trace || !action_block ||
previous->profile != CheckpointProfile::cellular_lives_v1 ||
predecessor.head.digest != *predecessor_digest ||
previous->sequence != predecessor.head.sequence || previous->commit != predecessor.head.commit ||
previous->end_tick != predecessor.head.end_tick || batch.header.world != previous->key.world ||
batch.header.region != previous->key.region || batch.header.frame != previous->frame ||
batch.header.predecessor != previous->commit ||
previous->sequence != predecessor.head.sequence ||
previous->commit != predecessor.head.commit ||
previous->end_tick != predecessor.head.end_tick ||
batch.header.world != previous->key.world || batch.header.region != previous->key.region ||
batch.header.frame != previous->frame || batch.header.predecessor != previous->commit ||
batch.header.predecessor_digest != *predecessor_digest ||
batch.header.begin_tick != previous->end_tick || candidate.world() != previous->key.world ||
candidate.region() != previous->key.region || candidate.frame() != previous->frame ||
@@ -201,8 +203,8 @@ publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects,
if (!payload_digest || !trace_digest)
return std::unexpected{failure(PublicationError::digest)};
auto manifest = successor_manifest(*previous, *predecessor_digest, commit, lease,
batch.header.end_tick, build, payload.size(), *payload_digest,
{trace->size(), *trace_digest, 0, {}});
batch.header.end_tick, build, payload.size(),
*payload_digest, {trace->size(), *trace_digest, 0, {}});
auto manifest_bytes = manifest ? encode_manifest(*manifest)
: std::expected<std::vector<std::uint8_t>, CheckpointError>{
std::unexpected{CheckpointError::format}};
@@ -224,25 +226,24 @@ publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects,
std::expected<RecoveredCellularCheckpoint, RecoveryFailure>
recover_cellular_checkpoint(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
const LeaseGrant& lease,
std::span<const Sha256Digest> retained_builds,
const LeaseGrant& lease, std::span<const Sha256Digest> retained_builds,
milliseconds timeout) {
const auto fail_with = [](RecoveryError kind) {
return std::unexpected{RecoveryFailure{kind, {}}};
};
auto committed = load_region_checkpoint(db, key, timeout);
if (!committed)
return std::unexpected{RecoveryFailure{
committed.error().kind == LeaseStoreError::not_provisioned
? RecoveryError::not_provisioned
: RecoveryError::store,
committed.error()}};
return std::unexpected{
RecoveryFailure{committed.error().kind == LeaseStoreError::not_provisioned
? RecoveryError::not_provisioned
: RecoveryError::store,
committed.error()}};
const auto digest = sha256(committed->manifest);
auto manifest = decode_manifest(committed->manifest);
if (!digest || *digest != committed->head.digest || !manifest || manifest->key != key ||
manifest->sequence != committed->head.sequence ||
manifest->commit != committed->head.commit || manifest->end_tick != committed->head.end_tick ||
manifest->frame != committed->head.frame)
manifest->commit != committed->head.commit ||
manifest->end_tick != committed->head.end_tick || manifest->frame != committed->head.frame)
return fail_with(RecoveryError::manifest);
if (!supported(*manifest) || manifest->profile != CheckpointProfile::cellular_lives_v1 ||
std::ranges::find(retained_builds, manifest->build) == retained_builds.end())
@@ -253,14 +254,15 @@ recover_cellular_checkpoint(Postgres& db, const FilesystemObjectStore& objects,
auto payload = objects.get({manifest->payload_sha256, manifest->payload_bytes});
if (!payload)
return fail_with(payload.error() == ObjectError::io ? RecoveryError::store
: RecoveryError::corrupt_payload);
: RecoveryError::corrupt_payload);
auto state = restore_cellular_payload(*payload, *manifest);
if (!state)
return fail_with(RecoveryError::payload);
std::vector<server::CellularActionRecord> actions;
if (manifest->sequence != 0) {
auto action_bytes = load_cellular_publication_actions(db, key, lease, manifest->commit, timeout);
auto action_bytes =
load_cellular_publication_actions(db, key, lease, manifest->commit, timeout);
if (!action_bytes)
return std::unexpected{RecoveryFailure{RecoveryError::store, action_bytes.error()}};
if (!*action_bytes)
@@ -281,17 +283,15 @@ recover_cellular_checkpoint(Postgres& db, const FilesystemObjectStore& objects,
trace->header.end_tick != manifest->end_tick)
return fail_with(RecoveryError::tail);
auto committed_births = load_cellular_publication_births(db, key, lease,
manifest->commit, timeout);
auto committed_births =
load_cellular_publication_births(db, key, lease, manifest->commit, timeout);
if (!committed_births)
return std::unexpected{RecoveryFailure{RecoveryError::store,
committed_births.error()}};
return std::unexpected{RecoveryFailure{RecoveryError::store, committed_births.error()}};
std::vector<CellularBirthBinding> traced_births;
for (std::size_t tick_index = 0; tick_index < trace->ticks.size(); ++tick_index) {
for (const auto& birth : trace->ticks[tick_index].births) {
traced_births.push_back({birth.account, manifest->commit, birth.request,
birth.identity,
manifest->begin_tick + tick_index + 1,
birth.identity, manifest->begin_tick + tick_index + 1,
manifest->sequence});
}
}
@@ -319,7 +319,8 @@ recover_cellular_checkpoint(Postgres& db, const FilesystemObjectStore& objects,
return fail_with(RecoveryError::tail);
auto batch_actions = server::decode_cellular_action_block(
**action_bytes, manifest->begin_tick, manifest->end_tick);
if (!batch_actions || !server::match_cellular_actions_to_trace(**base, *trace, *batch_actions))
if (!batch_actions ||
!server::match_cellular_actions_to_trace(**base, *trace, *batch_actions))
return fail_with(RecoveryError::history);
auto replay = server::replay_cellular_batch(**base, *trace);
if (!replay || (*replay)->snapshot() != *payload)
+16 -11
View File
@@ -169,12 +169,17 @@ encode_manifest(const CheckpointManifest& m) {
w.u(m.tick_microseconds, 8);
const auto versions =
m.profile == CheckpointProfile::cellular_lives_v1
? std::array{m.cellular_versions.rules, m.cellular_versions.envelope,
m.cellular_versions.habitat, m.cellular_versions.coordinate,
m.cellular_versions.genome, m.cellular_versions.development,
m.cellular_versions.founder, m.cellular_versions.archive, 0U}
: std::array{m.versions.rules, m.versions.envelope, m.versions.resources,
m.versions.fields, m.versions.terrain, m.versions.coordinate,
? std::array{m.cellular_versions.rules,
m.cellular_versions.envelope,
m.cellular_versions.habitat,
m.cellular_versions.coordinate,
m.cellular_versions.genome,
m.cellular_versions.development,
m.cellular_versions.founder,
m.cellular_versions.archive,
0U}
: std::array{m.versions.rules, m.versions.envelope, m.versions.resources,
m.versions.fields, m.versions.terrain, m.versions.coordinate,
m.versions.generator, m.versions.material, m.versions.interval_adapter};
for (const auto v : versions)
w.u(v, 4);
@@ -225,13 +230,13 @@ decode_manifest(std::span<const std::uint8_t> bytes) {
for (auto& v : versions)
v = static_cast<std::uint32_t>(r.u(4));
if (m.profile == CheckpointProfile::cellular_lives_v1) {
m.cellular_versions = {versions[0], versions[1], versions[2], versions[3], versions[4],
versions[5], versions[6], versions[7]};
m.cellular_versions = {versions[0], versions[1], versions[2], versions[3],
versions[4], versions[5], versions[6], versions[7]};
if (versions[8] != 0)
return std::unexpected{CheckpointError::format};
} else {
m.versions = {versions[0], versions[1], versions[2], versions[3], versions[4],
versions[5], versions[6], versions[7], versions[8]};
versions[5], versions[6], versions[7], versions[8]};
}
m.build = r.bytes<32>();
m.rng_streams = static_cast<std::uint32_t>(r.u(4));
@@ -424,8 +429,8 @@ restore_cellular_payload(std::span<const std::uint8_t> payload, const Checkpoint
return std::unexpected{CheckpointError::unsupported};
if (payload.size() != m.payload_bytes)
return std::unexpected{CheckpointError::payload};
auto restored = server::CellularLives::restore(payload, m.key.world, m.key.region, m.frame,
m.end_tick);
auto restored =
server::CellularLives::restore(payload, m.key.world, m.key.region, m.frame, m.end_tick);
if (!restored)
return std::unexpected{restored.error() == server::CellularLivesError::version
? CheckpointError::unsupported
+44 -48
View File
@@ -51,8 +51,8 @@ std::vector<CellularTickIntents::Birth> births_in(const CellularBatch& batch) {
}
std::optional<persistence::Uuid> commit_id(const persistence::CommittedCheckpoint& previous,
std::span<const std::uint8_t> trace,
std::span<const std::uint8_t> actions) {
std::span<const std::uint8_t> trace,
std::span<const std::uint8_t> actions) {
std::vector<std::uint8_t> identity;
identity.reserve(previous.head.digest.size() + 16 + trace.size() + actions.size());
identity.insert(identity.end(), previous.head.digest.begin(), previous.head.digest.end());
@@ -76,9 +76,8 @@ std::optional<persistence::Uuid> commit_id(const persistence::CommittedCheckpoin
}
} // namespace
CellularRegionHost::CellularRegionHost(
CellularHostConfig config, CellularConnect connect,
std::unique_ptr<persistence::FilesystemObjectStore> objects)
CellularRegionHost::CellularRegionHost(CellularHostConfig config, CellularConnect connect,
std::unique_ptr<persistence::FilesystemObjectStore> objects)
: config_{std::move(config)}, connect_{std::move(connect)}, objects_{std::move(objects)},
authority_{config_.lease} {}
@@ -109,9 +108,8 @@ void CellularRegionHost::acquire() {
if (!database())
return;
const auto sent = LeaseClock::now();
auto lease = persistence::acquire_region_lease(*db_, config_.key, config_.build,
config_.lease.duration,
config_.lease.renew_timeout);
auto lease = persistence::acquire_region_lease(
*db_, config_.key, config_.build, config_.lease.duration, config_.lease.renew_timeout);
if (!lease) {
if (lease.error().kind == LeaseStoreError::not_provisioned ||
lease.error().kind == LeaseStoreError::unsupported) {
@@ -122,14 +120,13 @@ void CellularRegionHost::acquire() {
}
return;
}
if (lease->frame != config_.frame ||
!authority_.acquired(*lease, sent, LeaseClock::now())) {
if (lease->frame != config_.frame || !authority_.acquired(*lease, sent, LeaseClock::now())) {
status_.phase = HostPhase::quarantined;
return;
}
auto recovered = persistence::recover_cellular_checkpoint(
*db_, *objects_, config_.key, *lease, config_.retained_builds,
config_.lease.commit_timeout);
auto recovered = persistence::recover_cellular_checkpoint(*db_, *objects_, config_.key, *lease,
config_.retained_builds,
config_.lease.commit_timeout);
if (!recovered) {
if (recovered.error().kind == persistence::RecoveryError::store) {
db_.reset();
@@ -204,8 +201,8 @@ void CellularRegionHost::resolve() {
++status_.fences;
return;
}
const auto resolved = persistence::resolve_cellular_prepared(
*db_, pending_->prepared, config_.lease.commit_timeout);
const auto resolved = persistence::resolve_cellular_prepared(*db_, pending_->prepared,
config_.lease.commit_timeout);
if (!resolved) {
if (resolved.error().kind == LeaseStoreError::fenced) {
authority_.fence();
@@ -233,8 +230,8 @@ void CellularRegionHost::resolve() {
if (resolved->status == persistence::Resolution::absent &&
!authority_.may_begin_commit(LeaseClock::now()))
return;
auto retried = persistence::retry_cellular_prepared(
*db_, pending_->prepared, config_.lease.commit_timeout);
auto retried = persistence::retry_cellular_prepared(*db_, pending_->prepared,
config_.lease.commit_timeout);
if (!retried) {
if (retried.error().kind == persistence::PublicationError::unknown) {
db_.reset();
@@ -278,11 +275,10 @@ void CellularRegionHost::refresh() {
authority_.may_originate(LeaseClock::now());
status_.epoch = grant ? grant->epoch : 0;
status_.lease_remaining_ms =
status_.ready
? static_cast<std::uint64_t>(std::chrono::duration_cast<milliseconds>(
authority_.deadline() - LeaseClock::now())
.count())
: 0;
status_.ready ? static_cast<std::uint64_t>(std::chrono::duration_cast<milliseconds>(
authority_.deadline() - LeaseClock::now())
.count())
: 0;
}
const HostStatus& CellularRegionHost::step() {
@@ -301,18 +297,16 @@ const HostStatus& CellularRegionHost::step() {
std::expected<persistence::PublishedCellularBatch, CellularHostError>
CellularRegionHost::publish(const CellularBatch& batch,
std::span<const CellularActionRecord> actions) {
if (!status_.ready || !state_ || !committed_ ||
!authority_.may_begin_commit(LeaseClock::now()))
if (!status_.ready || !state_ || !committed_ || !authority_.may_begin_commit(LeaseClock::now()))
return std::unexpected{CellularHostError::not_ready};
auto candidate = replay_cellular_batch(*state_, batch);
if (!candidate)
return std::unexpected{CellularHostError::invalid_batch};
auto trace = encode_cellular_batch(batch);
auto action_block = encode_cellular_action_block(actions, batch.header.begin_tick,
batch.header.end_tick);
const auto commit = trace && action_block
? commit_id(*committed_, *trace, *action_block)
: std::nullopt;
auto action_block =
encode_cellular_action_block(actions, batch.header.begin_tick, batch.header.end_tick);
const auto commit =
trace && action_block ? commit_id(*committed_, *trace, *action_block) : std::nullopt;
if (!commit)
return std::unexpected{CellularHostError::invalid_batch};
auto published = persistence::publish_cellular_batch(
@@ -320,9 +314,10 @@ CellularRegionHost::publish(const CellularBatch& batch,
config_.build, config_.lease.commit_timeout, {}, actions);
if (!published) {
if (published.error().kind == persistence::PublicationError::unknown) {
pending_ = std::make_unique<Pending>(
Pending{published.error().prepared, std::move(*candidate),
{actions.begin(), actions.end()}, births_in(batch)});
pending_ = std::make_unique<Pending>(Pending{published.error().prepared,
std::move(*candidate),
{actions.begin(), actions.end()},
births_in(batch)});
status_.phase = HostPhase::resolving;
status_.ready = false;
++status_.unknown_outcomes;
@@ -367,22 +362,21 @@ void CellularRegionHost::finalize_births(std::span<const CellularTickIntents::Bi
});
}
network::PrimitiveLifeOutcome CellularRegionHost::query_birth(
const persistence::Uuid& account, std::uint64_t generation, std::uint64_t request,
bool enqueue) {
network::PrimitiveLifeOutcome CellularRegionHost::query_birth(const persistence::Uuid& account,
std::uint64_t generation,
std::uint64_t request, bool enqueue) {
using network::PrimitiveLifeResult;
const auto unknown = [&] {
return network::PrimitiveLifeOutcome{request, PrimitiveLifeResult::unknown, 0, 0};
};
if (!status_.ready || !authority_.may_begin_commit(LeaseClock::now()) || account ==
persistence::Uuid{} || generation == 0 || request == 0 || !database())
if (!status_.ready || !authority_.may_begin_commit(LeaseClock::now()) ||
account == persistence::Uuid{} || generation == 0 || request == 0 || !database())
return unknown();
const auto grant = authority_.grant();
if (!grant)
return unknown();
auto queried = persistence::query_primitive_life(*db_, config_.key, *grant, account,
generation, request,
config_.lease.commit_timeout);
auto queried = persistence::query_primitive_life(*db_, config_.key, *grant, account, generation,
request, config_.lease.commit_timeout);
if (!queried) {
if (queried.error().kind == persistence::LifeReservationError::unknown ||
queried.error().kind == persistence::LifeReservationError::store)
@@ -407,8 +401,9 @@ network::PrimitiveLifeOutcome CellularRegionHost::query_birth(
return {request, result, outcome.reservation.identity, 0};
}
network::PrimitiveLifeOutcome CellularRegionHost::create_primitive_life(
const persistence::Uuid& account, std::uint64_t generation, std::uint64_t request) {
network::PrimitiveLifeOutcome
CellularRegionHost::create_primitive_life(const persistence::Uuid& account,
std::uint64_t generation, std::uint64_t request) {
using network::PrimitiveLifeResult;
const auto existing = query_birth(account, generation, request, true);
if (existing.result != PrimitiveLifeResult::unknown)
@@ -419,8 +414,8 @@ network::PrimitiveLifeOutcome CellularRegionHost::create_primitive_life(
if (!grant)
return {request, PrimitiveLifeResult::unknown, 0, 0};
auto reserved = persistence::reserve_primitive_life(
*db_, config_.key, *grant, account, generation, request,
biology::founder_life_rules, config_.lease.commit_timeout);
*db_, config_.key, *grant, account, generation, request, biology::founder_life_rules,
config_.lease.commit_timeout);
if (!reserved) {
if (reserved.error().kind == persistence::LifeReservationError::unknown ||
reserved.error().kind == persistence::LifeReservationError::store)
@@ -439,8 +434,9 @@ network::PrimitiveLifeOutcome CellularRegionHost::create_primitive_life(
return {request, result, reserved->identity, 0};
}
network::PrimitiveLifeOutcome CellularRegionHost::query_primitive_life(
const persistence::Uuid& account, std::uint64_t generation, std::uint64_t request) {
network::PrimitiveLifeOutcome
CellularRegionHost::query_primitive_life(const persistence::Uuid& account, std::uint64_t generation,
std::uint64_t request) {
return query_birth(account, generation, request, true);
}
@@ -454,8 +450,8 @@ void CellularRegionHost::drain() {
pending_.reset();
const auto grant = authority_.grant();
if (grant && authority_.may_originate(LeaseClock::now()) && database())
static_cast<void>(persistence::release_region_lease(
*db_, config_.key, *grant, config_.lease.renew_timeout));
static_cast<void>(persistence::release_region_lease(*db_, config_.key, *grant,
config_.lease.renew_timeout));
authority_.fence();
state_.reset();
committed_.reset();
+33 -37
View File
@@ -30,8 +30,7 @@ bool all_zero(std::span<const std::uint8_t> bytes) {
bool valid_header(const CellularBatchHeader& h, std::size_t ticks) {
return h.world != 0 && h.region != 0 && h.frame != 0 && !all_zero(h.predecessor) &&
!all_zero(h.predecessor_digest) && h.end_tick > h.begin_tick &&
h.end_tick - h.begin_tick == ticks && ticks > 0 &&
ticks <= max_cellular_batch_ticks;
h.end_tick - h.begin_tick == ticks && ticks > 0 && ticks <= max_cellular_batch_ticks;
}
bool valid_intent(const CellularLifeIntent& life) {
const auto& step = life.intent;
@@ -48,16 +47,14 @@ bool valid_ticks(std::span<const CellularTickIntents> ticks) {
return false;
for (std::size_t i = 0; i < tick.lives.size(); ++i) {
const auto& life = tick.lives[i];
if (!valid_intent(life) ||
(i > 0 && tick.lives[i - 1].life_id >= life.life_id))
if (!valid_intent(life) || (i > 0 && tick.lives[i - 1].life_id >= life.life_id))
return false;
}
for (std::size_t i = 0; i < tick.births.size(); ++i) {
const auto& birth = tick.births[i];
if (all_zero(birth.account) || birth.request == 0 || birth.generation == 0 ||
birth.identity == 0 ||
(i > 0 && std::tie(tick.births[i - 1].account,
tick.births[i - 1].request) >=
(i > 0 && std::tie(tick.births[i - 1].account, tick.births[i - 1].request) >=
std::tie(birth.account, birth.request)))
return false;
if (!identities.insert(birth.identity).second || !accounts.insert(birth.account).second)
@@ -73,22 +70,19 @@ bool valid_action_input(const CellularActionRecord& action, std::uint64_t begin_
std::uint64_t end_tick) {
const auto input = std::span<const std::uint8_t>{action.input};
const auto tick = action_tick(action);
const auto move_x = std::bit_cast<std::int16_t>(
static_cast<std::uint16_t>(get(input, 8, 2)));
const auto move_y = std::bit_cast<std::int16_t>(
static_cast<std::uint16_t>(get(input, 10, 2)));
const auto move_x = std::bit_cast<std::int16_t>(static_cast<std::uint16_t>(get(input, 8, 2)));
const auto move_y = std::bit_cast<std::int16_t>(static_cast<std::uint16_t>(get(input, 10, 2)));
return !all_zero(action.account) && action.action_id != 0 && tick > begin_tick &&
tick <= end_tick && move_x >= -movement_limit && move_x <= movement_limit &&
move_y >= -movement_limit && move_y <= movement_limit && input[12] <= 1 &&
all_zero(input.subspan(13, 3)) &&
(input[12] == 1 || all_zero(input.subspan(16, 8)));
all_zero(input.subspan(13, 3)) && (input[12] == 1 || all_zero(input.subspan(16, 8)));
}
bool action_less(const CellularActionRecord& a, const CellularActionRecord& b) {
const auto a_tick = action_tick(a);
const auto b_tick = action_tick(b);
return a_tick < b_tick ||
(a_tick == b_tick && (a.account < b.account ||
(a.account == b.account && a.action_id < b.action_id)));
(a_tick == b_tick &&
(a.account < b.account || (a.account == b.account && a.action_id < b.action_id)));
}
bool valid_actions(std::span<const CellularActionRecord> actions, std::uint64_t begin_tick,
std::uint64_t end_tick) {
@@ -215,14 +209,14 @@ decode_cellular_batch(std::span<const std::uint8_t> bytes) {
return std::unexpected{CellularBatchError::format};
CellularLifeIntent life;
life.life_id = get(record, 0, 8);
life.intent.movement.x = std::bit_cast<std::int16_t>(
static_cast<std::uint16_t>(get(record, 8, 2)));
life.intent.movement.y = std::bit_cast<std::int16_t>(
static_cast<std::uint16_t>(get(record, 10, 2)));
life.intent.movement.x =
std::bit_cast<std::int16_t>(static_cast<std::uint16_t>(get(record, 8, 2)));
life.intent.movement.y =
std::bit_cast<std::int16_t>(static_cast<std::uint16_t>(get(record, 10, 2)));
if (record[12] == 1) {
life.intent.intake_target = biology::CellularPatchAddress{
static_cast<std::uint32_t>(get(record, 16, 4)),
static_cast<std::uint32_t>(get(record, 20, 4))};
life.intent.intake_target =
biology::CellularPatchAddress{static_cast<std::uint32_t>(get(record, 16, 4)),
static_cast<std::uint32_t>(get(record, 20, 4))};
} else if (!all_zero(record.subspan(16, 8))) {
return std::unexpected{CellularBatchError::format};
}
@@ -275,8 +269,11 @@ replay_cellular_batch(const CellularLives& base, const CellularBatch& batch) {
return std::unexpected{CellularBatchError::state};
for (const auto& birth : tick.births) {
const LifeRequest request{birth.account, birth.request};
const LifeAllocation allocation{birth.identity, birth.identity, birth.identity,
birth.identity, {(*replay)->tick()}};
const LifeAllocation allocation{birth.identity,
birth.identity,
birth.identity,
birth.identity,
{(*replay)->tick()}};
auto born = (*replay)->birth_candidate(request, allocation);
if (!born)
return std::unexpected{CellularBatchError::state};
@@ -331,28 +328,27 @@ match_cellular_actions_to_trace(const CellularLives& predecessor, const Cellular
predecessor.frame() != batch.header.frame || predecessor.tick() != batch.header.begin_tick)
return std::unexpected{CellularActionBlockError::mismatch};
for (const auto& action : actions) {
const auto life = std::ranges::find(predecessor.lives(), action.account,
&CellularOwnedLife::account);
const auto life =
std::ranges::find(predecessor.lives(), action.account, &CellularOwnedLife::account);
if (life == predecessor.lives().end())
return std::unexpected{CellularActionBlockError::mismatch};
const auto index = static_cast<std::size_t>(action_tick(action) - batch.header.begin_tick - 1);
const auto index =
static_cast<std::size_t>(action_tick(action) - batch.header.begin_tick - 1);
const auto& intents = batch.ticks[index].lives;
const auto intent = std::ranges::lower_bound(
intents, life->identity.life, {}, [](const CellularLifeIntent& value) {
return value.life_id;
});
const auto intent =
std::ranges::lower_bound(intents, life->identity.life, {},
[](const CellularLifeIntent& value) { return value.life_id; });
if (intent == intents.end() || intent->life_id != life->identity.life)
return std::unexpected{CellularActionBlockError::mismatch};
const auto input = std::span<const std::uint8_t>{action.input};
std::optional<biology::CellularPatchAddress> target;
if (input[12] != 0)
target = biology::CellularPatchAddress{
static_cast<std::uint32_t>(get(input, 16, 4)),
static_cast<std::uint32_t>(get(input, 20, 4))};
if (intent->intent.movement.x != std::bit_cast<std::int16_t>(
static_cast<std::uint16_t>(get(input, 8, 2))) ||
intent->intent.movement.y != std::bit_cast<std::int16_t>(
static_cast<std::uint16_t>(get(input, 10, 2))) ||
target = biology::CellularPatchAddress{static_cast<std::uint32_t>(get(input, 16, 4)),
static_cast<std::uint32_t>(get(input, 20, 4))};
if (intent->intent.movement.x !=
std::bit_cast<std::int16_t>(static_cast<std::uint16_t>(get(input, 8, 2))) ||
intent->intent.movement.y !=
std::bit_cast<std::int16_t>(static_cast<std::uint16_t>(get(input, 10, 2))) ||
intent->intent.intake_target != target)
return std::unexpected{CellularActionBlockError::mismatch};
}
+19 -22
View File
@@ -20,8 +20,7 @@ prepare_cellular_session_tick(const CellularLives& state, std::uint64_t tick,
std::ranges::sort(prepared.intents.lives, {}, &CellularLifeIntent::life_id);
for (std::size_t i = 0; i < state.lives().size(); ++i)
if (prepared.intents.lives[i].life_id != state.lives()[i].identity.life ||
(i != 0 && prepared.intents.lives[i - 1].life_id ==
prepared.intents.lives[i].life_id))
(i != 0 && prepared.intents.lives[i - 1].life_id == prepared.intents.lives[i].life_id))
return std::unexpected{CellularSessionBatchError::intents};
prepared.actions.reserve(actions.size());
@@ -33,16 +32,16 @@ prepare_cellular_session_tick(const CellularLives& state, std::uint64_t tick,
if (!input || input->tick != tick)
return std::unexpected{CellularSessionBatchError::action};
const auto life = std::ranges::find(state.lives(), action.account.bytes,
&CellularOwnedLife::account);
const auto life =
std::ranges::find(state.lives(), action.account.bytes, &CellularOwnedLife::account);
if (life == state.lives().end())
return std::unexpected{CellularSessionBatchError::life};
if (input->has_intake_target) {
const auto view = state.perceive(life->identity.life);
const biology::CellularPatchAddress target{input->intake_x_um, input->intake_y_um};
if (!view || std::ranges::find(*view, target,
&biology::CellularPatchPerception::address) ==
view->end())
if (!view ||
std::ranges::find(*view, target, &biology::CellularPatchPerception::address) ==
view->end())
return std::unexpected{CellularSessionBatchError::unperceived_target};
}
const auto owned_intent = std::ranges::lower_bound(
@@ -67,12 +66,12 @@ prepare_cellular_session_tick(const CellularLives& state, std::uint64_t tick,
record.input = *action.cellular_input;
prepared.actions.push_back(record);
}
std::ranges::sort(prepared.actions, [](const CellularActionRecord& left,
const CellularActionRecord& right) {
if (left.account != right.account)
return left.account < right.account;
return left.action_id < right.action_id;
});
std::ranges::sort(prepared.actions,
[](const CellularActionRecord& left, const CellularActionRecord& right) {
if (left.account != right.account)
return left.account < right.account;
return left.action_id < right.action_id;
});
return prepared;
}
@@ -93,7 +92,7 @@ CellularSessionBatchBuilder::create(const CellularLives& committed_state,
CellularSessionBatchBuilder builder{std::move(*working)};
builder.batch_.header = {committed_state.world(), committed_state.region(),
committed_state.frame(), committed_head.commit,
committed_head.digest, committed_state.tick(),
committed_head.digest, committed_state.tick(),
committed_state.tick()};
return builder;
}
@@ -112,8 +111,7 @@ CellularSessionBatchBuilder::append(std::uint64_t tick,
return std::unexpected{prepared.error()};
for (const auto& action : prepared->actions)
if (std::ranges::find_if(actions_, [&](const CellularActionRecord& previous) {
return previous.account == action.account &&
previous.action_id == action.action_id;
return previous.account == action.account && previous.action_id == action.action_id;
}) != actions_.end())
return std::unexpected{CellularSessionBatchError::duplicate_action};
if (births.size() > biology::max_cellular_tick_lives)
@@ -133,21 +131,20 @@ CellularSessionBatchBuilder::append(std::uint64_t tick,
return std::unexpected{CellularSessionBatchError::duplicate_birth};
for (const auto& previous_tick : batch_.ticks)
if (std::ranges::any_of(previous_tick.births, [&](const auto& previous) {
return previous.account == birth.account ||
previous.identity == birth.identity;
return previous.account == birth.account || previous.identity == birth.identity;
}))
return std::unexpected{CellularSessionBatchError::duplicate_birth};
}
auto next = CellularLives::restore(working_->snapshot(), working_->world(),
working_->region(), working_->frame(), working_->tick());
auto next = CellularLives::restore(working_->snapshot(), working_->world(), working_->region(),
working_->frame(), working_->tick());
if (!next || !(*next)->advance_tick(prepared->intents.lives))
return std::unexpected{CellularSessionBatchError::biology};
for (const auto& birth : prepared->intents.births) {
const LifeRequest request{birth.account, birth.request};
const LifeAllocation allocation{birth.identity, birth.identity, birth.identity,
birth.identity, {tick}};
const LifeAllocation allocation{
birth.identity, birth.identity, birth.identity, birth.identity, {tick}};
auto created = (*next)->birth_candidate(request, allocation);
if (!created)
return std::unexpected{CellularSessionBatchError::invalid_birth};
+8 -10
View File
@@ -36,8 +36,8 @@ PostgresActionLedger::find_record(const network::AccountId& account, std::uint64
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_);
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 ||
@@ -54,8 +54,7 @@ PostgresActionLedger::find_record(const network::AccountId& account, std::uint64
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)
if (!input || input->tick != row.action.tick || result != network::ActionResult::committed)
return std::unexpected(network::ActionLedgerError::unavailable);
std::array<std::uint8_t, network::cellular_action_input_bytes> canonical{};
std::ranges::copy(*row.cellular_input, canonical.begin());
@@ -66,8 +65,7 @@ PostgresActionLedger::find_record(const network::AccountId& account, std::uint64
return std::optional<network::DurableActionRecord>{std::move(record)};
}
std::expected<std::vector<std::optional<network::DurableActionRecord>>,
network::ActionLedgerError>
std::expected<std::vector<std::optional<network::DurableActionRecord>>, network::ActionLedgerError>
PostgresActionLedger::find_all_records(std::span<const network::ActionKey> keys) {
std::vector<std::optional<network::DurableActionRecord>> out(keys.size());
if (keys.empty())
@@ -97,10 +95,10 @@ PostgresActionLedger::find_all_records(std::span<const network::ActionKey> keys)
continue;
const auto& row = *(*found)[i];
const auto result = static_cast<network::ActionResult>(row.result);
network::DurableActionRecord record{
network::ActionOutcome{keys[i].action_id, result, row.action.tick,
row.action.commit_sequence},
std::nullopt};
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 ||