addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', ], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('cloud_hosts', static function (Blueprint $table): void { $table->increments('id'); }); $schema->create('cloud_resource_pools', static function ( Blueprint $table ): void { $table->increments('id'); $table->integer('line_id'); }); $schema->create('cloud_host_agents', static function (Blueprint $table): void { $table->integer('host_id')->primary(); $table->boolean('enabled'); $table->integer('revision'); $table->integer('tested_revision'); $table->string('last_test_status'); $table->text('network_selectors_json'); $table->text('public_network_capabilities_json'); }); $schema->create('cloud_host_network_attachments', static function ( Blueprint $table ): void { $table->increments('id'); $table->string('attachment_key'); $table->integer('host_id'); $table->integer('line_id'); $table->string('network_selector'); $table->string('state'); $table->integer('priority')->default(0); $table->integer('config_revision')->default(1); $table->string('verification_state'); $table->integer('verified_agent_revision'); $table->integer('last_verified_at'); }); $schema->create('cloud_public_ip_pools', static function ( Blueprint $table ): void { $table->increments('id'); $table->string('pool_key'); $table->integer('host_id'); $table->integer('network_attachment_id'); $table->integer('address_family'); $table->string('cidr'); $table->string('gateway'); $table->boolean('gateway_on_link'); $table->integer('guest_prefix_length'); $table->text('dns_servers_json'); $table->text('reserved_ips_json'); $table->string('allocation_start')->nullable(); $table->string('allocation_end')->nullable(); $table->string('allocation_cursor')->nullable(); $table->integer('max_allocations'); $table->string('state'); $table->integer('priority')->default(0); $table->integer('desired_revision'); $table->integer('observed_revision'); $table->string('sync_state'); }); $schema->create('cloud_public_ips', static function (Blueprint $table): void { $table->increments('id'); $table->integer('host_id'); $table->integer('public_ip_pool_id'); $table->integer('address_family'); $table->string('address'); $table->binary('address_binary'); $table->integer('prefix_length'); $table->string('state'); $table->integer('reservation_id')->nullable(); $table->integer('reservation_ip_claim_id')->nullable(); $table->integer('instance_id')->nullable(); $table->string('operation_id')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); $table->unique(['address_family', 'address_binary']); }); $schema->create('cloud_resource_reservations', static function ( Blueprint $table ): void { $table->increments('id'); $table->integer('host_id'); $table->integer('resource_pool_id')->nullable(); $table->integer('user_id')->nullable(); $table->string('operation_id'); $table->string('state'); $table->integer('order_id')->nullable(); $table->integer('backup_restore_id')->nullable(); $table->integer('instance_id')->nullable(); $table->integer('network_attachment_id')->nullable(); $table->integer('public_ip_pool_id')->nullable(); $table->integer('public_ip_id')->nullable(); $table->integer('updated_at')->nullable(); }); $schema->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->integer('reservation_id'); $table->integer('host_id'); $table->integer('resource_pool_id'); $table->integer('user_id'); $table->text('spec_json'); }); $schema->create('cloud_instance_migrations', static function ( Blueprint $table ): void { $table->increments('id'); $table->integer('instance_id'); $table->integer('restore_id'); $table->integer('source_host_id'); $table->integer('source_reservation_id'); $table->integer('target_host_id'); $table->integer('target_reservation_id'); $table->string('state'); }); $schema->create('cloud_resource_reservation_ips', static function ( Blueprint $table ): void { $table->increments('id'); $table->integer('reservation_id'); $table->integer('address_family'); $table->integer('claim_generation')->default(1); $table->integer('active_claim')->nullable()->default(1); $table->integer('network_attachment_id'); $table->integer('public_ip_pool_id'); $table->integer('public_ip_id'); $table->string('operation_id'); $table->string('state'); $table->integer('created_at'); $table->integer('updated_at'); $table->integer('released_at')->nullable(); $table->string('release_reason')->default(''); $table->unique(['reservation_id', 'address_family', 'active_claim']); $table->unique(['reservation_id', 'claim_generation', 'address_family']); }); Capsule::table('cloud_host_network_attachments')->insert([ 'id' => 10, 'attachment_key' => str_repeat('1', 32), 'host_id' => 1, 'line_id' => 7, 'network_selector' => 'bridge:br0', 'state' => 'active', 'priority' => 0, 'config_revision' => 1, 'verification_state' => 'verified', 'verified_agent_revision' => 1, 'last_verified_at' => time(), ]); Capsule::table('cloud_hosts')->insert(['id' => 1]); Capsule::table('cloud_resource_pools')->insert([ ['id' => 30, 'line_id' => 7], ['id' => 31, 'line_id' => 8], ]); Capsule::table('cloud_host_agents')->insert([ 'host_id' => 1, 'enabled' => 1, 'revision' => 1, 'tested_revision' => 1, 'last_test_status' => 'healthy', 'network_selectors_json' => '["bridge:br0"]', 'public_network_capabilities_json' => json_encode([ 'contract' => 1, 'dual_stack_required' => true, 'project_owned_filter' => true, 'ndp_guard' => true, ], JSON_THROW_ON_ERROR), ]); $common = [ 'host_id' => 1, 'network_attachment_id' => 10, 'state' => 'active', 'priority' => 0, 'desired_revision' => 1, 'observed_revision' => 1, 'sync_state' => 'applied', ]; Capsule::table('cloud_public_ip_pools')->insert([ array_merge($common, [ 'id' => 100, 'pool_key' => str_repeat('a', 32), 'address_family' => 4, 'cidr' => '8.8.8.0/24', 'gateway' => '8.8.8.1', 'gateway_on_link' => 0, 'guest_prefix_length' => 24, 'dns_servers_json' => '["1.1.1.1"]', 'reserved_ips_json' => '[]', 'allocation_start' => null, 'allocation_end' => null, 'allocation_cursor' => null, 'max_allocations' => 0, ]), array_merge($common, [ 'id' => 200, 'pool_key' => str_repeat('b', 32), 'address_family' => 6, 'cidr' => '2404:c140:156:3a::/64', 'gateway' => '2404:c140:156::1', 'gateway_on_link' => 1, 'guest_prefix_length' => 64, 'dns_servers_json' => '["2606:4700:4700::1111"]', 'reserved_ips_json' => json_encode(array_map( static fn (int $suffix): string => '2404:c140:156:3a::' . dechex($suffix), range(0x1000, 0x1013) ), JSON_THROW_ON_ERROR), 'allocation_start' => '2404:c140:156:3a::1000', 'allocation_end' => '2404:c140:156:3a::1060', 'allocation_cursor' => null, 'max_allocations' => 4, ]), ]); $now = time(); foreach ([10, 11] as $suffix) { $address = '8.8.8.' . $suffix; Capsule::table('cloud_public_ips')->insert([ 'host_id' => 1, 'public_ip_pool_id' => 100, 'address_family' => 4, 'address' => $address, 'address_binary' => inet_pton($address), 'prefix_length' => 24, 'state' => 'available', 'created_at' => $now, 'updated_at' => $now, ]); } foreach (range(0x1014, 0x102c) as $suffix) { $address = '2404:c140:156:3a::' . dechex($suffix); Capsule::table('cloud_public_ips')->insert([ 'host_id' => 1, 'public_ip_pool_id' => 200, 'address_family' => 6, 'address' => $address, 'address_binary' => inet_pton($address), 'prefix_length' => 64, 'state' => 'disabled', 'created_at' => $now, 'updated_at' => $now, ]); } $operations = [ 500 => str_repeat('c', 32), 501 => str_repeat('d', 32), 502 => str_repeat('e', 32), 503 => str_repeat('f', 32), 504 => str_repeat('9', 32), ]; foreach ($operations as $id => $operationId) { Capsule::table('cloud_resource_reservations')->insert([ 'id' => $id, 'host_id' => 1, 'resource_pool_id' => 30, 'user_id' => 42, 'operation_id' => $operationId, 'state' => 'held', ]); } Capsule::table('cloud_instances')->insert([ [ 'id' => 900, 'reservation_id' => 500, 'host_id' => 1, 'resource_pool_id' => 30, 'user_id' => 42, 'spec_json' => '{}', ], [ 'id' => 901, 'reservation_id' => 504, 'host_id' => 1, 'resource_pool_id' => 30, 'user_id' => 42, 'spec_json' => '{}', ], ]); $materializedBefore = Capsule::table('cloud_public_ips')->count(); $first = CloudPublicNetworkAllocationService::reserveBundle( 1, 7, 500, $operations[500] ); $assert($first->address(4) === '8.8.8.10/24' && $first->address(6) === '2404:c140:156:3a::102d/64', 'The first atomic bundle did not contain one address from each family.'); $assert(Capsule::table('cloud_public_ips')->count() === $materializedBefore + 1, 'The sparse /64 allocator materialized more than the claimed IPv6 address.'); $assert(Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 500)->count() === 2, 'One reservation did not retain both address claims.'); $replay = CloudPublicNetworkAllocationService::reserveBundle( 1, 7, 500, $operations[500] ); $assert(hash_equals($first->fingerprint(), $replay->fingerprint()) && Capsule::table('cloud_public_ips')->count() === $materializedBefore + 1, 'Idempotent bundle reservation allocated a second address.'); $committed = CloudPublicNetworkAllocationService::commitBundle( 500, 900, $operations[500] ); $assert(hash_equals($first->fingerprint(), $committed->fingerprint()) && Capsule::table('cloud_public_ips')->where('instance_id', 900) ->where('state', 'assigned')->count() === 2, 'Commit did not bind the complete dual-stack identity to the instance.'); Capsule::table('cloud_resource_reservations')->where('id', 500)->update([ 'state' => 'committed', 'instance_id' => 900, ]); Capsule::table('cloud_instances')->where('id', 900)->update([ 'spec_json' => json_encode( ['public_network' => $committed->toArray()], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ), ]); $replacementAttempt = CloudPublicNetworkAllocationService::reserveBundle( 1, 7, 500, $operations[500] ); $assert(hash_equals($committed->fingerprint(), $replacementAttempt->fingerprint()) && Capsule::table('cloud_public_ips')->count() === $materializedBefore + 1, 'A normal reservation was reused to overwrite a committed source bundle.'); $recommitted = CloudPublicNetworkAllocationService::commitBundle( 500, 900, $operations[500] ); $assert(hash_equals($committed->fingerprint(), $recommitted->fingerprint()), 'An already committed bundle was not an idempotent success.'); $rejects(static fn () => CloudPublicNetworkAllocationService::releaseBundle( 500, $operations[500], 'confirmed_not_applied', true ), 'A committed public-network bundle was released.'); $assert(Capsule::table('cloud_public_ips')->where('instance_id', 900) ->where('state', 'assigned')->count() === 2, 'Rejecting a committed release changed its assigned address bundle.'); $second = CloudPublicNetworkAllocationService::reserveBundle( 1, 7, 501, $operations[501] ); $assert($second->address(4) === '8.8.8.11/24' && $second->address(6) === '2404:c140:156:3a::102e/64', 'The allocation cursor did not advance across a second reservation.'); CloudPublicNetworkAllocationService::fenceBundle(501, $operations[501]); Capsule::table('cloud_resource_reservations')->where('id', 501)->update([ 'state' => 'uncertain', ]); CloudPublicNetworkAllocationService::fenceBundle(501, $operations[501]); $assert(Capsule::table('cloud_public_ips')->where('reservation_id', 501) ->where('state', 'uncertain')->count() === 2, 'Unknown effect did not fence both public addresses.'); $rejects(static fn () => CloudPublicNetworkAllocationService::releaseBundle( 501, $operations[501], 'confirmed_not_applied', false ), 'An unconfirmed destructive result released a public address bundle.'); $rejects(static fn () => CloudPublicNetworkAllocationService:: reopenBundleForRetry( 501, $operations[501], false ), 'An unconfirmed destructive result reopened a public address bundle.'); Capsule::connection()->statement(<<<'SQL' CREATE TRIGGER reject_ipv6_bundle_reopen BEFORE UPDATE OF state ON cloud_resource_reservation_ips WHEN OLD.address_family = 6 AND NEW.state = 'reserved' BEGIN SELECT RAISE(ABORT, 'reject IPv6 reopen'); END SQL); $rejects(static fn () => CloudPublicNetworkAllocationService:: reopenBundleForRetry( 501, $operations[501], true ), 'An IPv6 reopen failure did not abort the dual-stack transaction.'); $assert((string) Capsule::table('cloud_resource_reservations') ->where('id', 501)->value('state') === 'uncertain' && Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 501)->where('state', 'uncertain') ->count() === 2 && Capsule::table('cloud_public_ips')->where('reservation_id', 501) ->where('state', 'uncertain')->count() === 2, 'A failed IPv6 reopen exposed a partial IPv4 transition.'); Capsule::connection()->statement('DROP TRIGGER reject_ipv6_bundle_reopen'); $reopened = CloudPublicNetworkAllocationService::reopenBundleForRetry( 501, $operations[501], true ); $assert(hash_equals($second->fingerprint(), $reopened->fingerprint()) && (string) Capsule::table('cloud_resource_reservations') ->where('id', 501)->value('state') === 'held' && Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 501)->where('state', 'reserved') ->count() === 2 && Capsule::table('cloud_public_ips')->where('reservation_id', 501) ->where('state', 'reserved')->count() === 2, 'Confirmed absence did not reopen the complete dual-stack bundle.'); $reopenedReplay = CloudPublicNetworkAllocationService::reopenBundleForRetry( 501, $operations[501], true ); $assert(hash_equals($reopened->fingerprint(), $reopenedReplay->fingerprint()), 'An already reopened bundle was not an idempotent success.'); $nextOperation = str_repeat('7', 32); Capsule::connection()->statement(<< CloudPublicNetworkAllocationService:: rotateHeldBundleOperation( 501, $operations[501], $nextOperation ), 'An IPv6 operation rotation failure did not abort the transaction.'); $assert(hash_equals( $operations[501], (string) Capsule::table('cloud_resource_reservations') ->where('id', 501)->value('operation_id') ) && Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 501)->where('active_claim', 1) ->where('operation_id', $operations[501])->count() === 2 && Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 501)->where('operation_id', $nextOperation) ->count() === 0 && Capsule::table('cloud_public_ips')->where('reservation_id', 501) ->where('operation_id', $operations[501])->count() === 2, 'A failed IPv6 operation rotation exposed a partial IPv4 generation.'); Capsule::connection()->statement('DROP TRIGGER reject_ipv6_bundle_rotation'); $rotated = CloudPublicNetworkAllocationService::rotateHeldBundleOperation( 501, $operations[501], $nextOperation ); $assert(hash_equals($reopened->fingerprint(), $rotated->fingerprint()) && hash_equals( $nextOperation, (string) Capsule::table('cloud_resource_reservations') ->where('id', 501)->value('operation_id') ) && Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 501)->where('active_claim', 1) ->where('claim_generation', 2) ->where('operation_id', $nextOperation)->count() === 2 && Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 501)->whereNull('active_claim') ->where('operation_id', $operations[501]) ->where('release_reason', 'migration_transfer_not_applied') ->count() === 2 && Capsule::table('cloud_public_ips')->where('reservation_id', 501) ->where('operation_id', $nextOperation)->count() === 2, 'Retry did not rotate both address families into one auditable generation.'); $rotatedReplay = CloudPublicNetworkAllocationService:: rotateHeldBundleOperation( 501, $operations[501], $nextOperation ); $assert(hash_equals($rotated->fingerprint(), $rotatedReplay->fingerprint()), 'A completed dual-stack operation rotation was not idempotent.'); CloudPublicNetworkAllocationService::releaseBundle( 501, $nextOperation, 'confirmed_not_applied', true ); $assert(Capsule::table('cloud_public_ips')->where('reservation_id', 501) ->count() === 0 && Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 501) ->where('release_reason', 'confirmed_not_applied')->count() === 2, 'Confirmed non-application did not release the complete bundle.'); $uncertainSuccess = CloudPublicNetworkAllocationService::reserveBundle( 1, 7, 504, $operations[504] ); CloudPublicNetworkAllocationService::fenceBundle(504, $operations[504]); Capsule::table('cloud_resource_reservations')->where('id', 504)->update([ 'state' => 'uncertain', ]); $rejects(static fn () => CloudPublicNetworkAllocationService::commitBundle( 504, 901, str_repeat('8', 32) ), 'An uncertain bundle accepted a different operation identity.'); $recoveredSuccess = CloudPublicNetworkAllocationService::commitBundle( 504, 901, $operations[504] ); $assert(hash_equals( $uncertainSuccess->fingerprint(), $recoveredSuccess->fingerprint() ) && Capsule::table('cloud_public_ips')->where('instance_id', 901) ->where('state', 'assigned')->count() === 2 && Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 504)->where('state', 'committed')->count() === 2, 'A recovered success did not atomically commit its uncertain dual-stack bundle.'); Capsule::table('cloud_resource_reservations')->where('id', 504)->update([ 'state' => 'committed', 'instance_id' => 901, ]); Capsule::table('cloud_instances')->where('id', 901)->update([ 'spec_json' => json_encode( ['public_network' => $recoveredSuccess->toArray()], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ), ]); $rejects(static fn () => CloudPublicNetworkAllocationService::commitBundle( 504, 902, $operations[504] ), 'A committed bundle was rebound to a different instance.'); Capsule::table('cloud_public_ip_pools')->where('id', 200)->update([ 'max_allocations' => 1, ]); $rejects(static fn () => CloudPublicNetworkAllocationService::reserveBundle( 1, 7, 502, $operations[502] ), 'A reusable sparse IPv6 row bypassed the pool allocation limit.'); $assert(Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 502)->count() === 0, 'A failed upper-bound check retained a partial address claim.'); Capsule::table('cloud_resource_reservations')->where('id', 500)->update([ 'state' => 'committed', 'instance_id' => 900, ]); $ipv6Claim = Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 500)->where('address_family', 6)->first(); $familySixOperation = str_repeat('b', 32); Capsule::table('cloud_resource_reservation_ips') ->where('id', (int) $ipv6Claim->id)->update([ 'claim_generation' => 2, 'operation_id' => $familySixOperation, ]); Capsule::table('cloud_public_ips') ->where('id', (int) $ipv6Claim->public_ip_id)->update([ 'operation_id' => $familySixOperation, ]); $mixedGeneration = CloudPublicNetworkAllocationService::currentClaimSet( 500, 'committed', 900 ); $assert($mixedGeneration->generation(4) === 1 && $mixedGeneration->generation(6) === 2 && strlen($mixedGeneration->digest()) === 64, 'A legal active pair required both families to share generation or operation.'); $frozenUuid = '123e4567-e89b-42d3-a456-426614174000'; $frozen = CloudFrozenPublicNetworkIdentity::fromClaimSet( $frozenUuid, $mixedGeneration ); $restoredFrozen = CloudFrozenPublicNetworkIdentity::fromStored( 500, 1, $frozenUuid, 10, $frozen->identityJson(), $frozen->identitySha256(), $frozen->publicNetworkJson(), $frozen->publicNetworkSha256(), $frozen->claimSetSha256(), $mixedGeneration ); $assert(hash_equals( $restoredFrozen->identitySha256(), $frozen->identitySha256() ) && hash_equals( $restoredFrozen->claimSetSha256(), $mixedGeneration->digest() ), 'A frozen identity did not round-trip its authoritative claim set.'); $tamperedTopLevel = json_decode( $frozen->identityJson(), true, 16, JSON_THROW_ON_ERROR ); $tamperedTopLevel['network_attachment_id'] = 11; $tamperedTopLevelJson = json_encode( $tamperedTopLevel, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ); $rejects(static fn () => CloudFrozenPublicNetworkIdentity::fromStored( 500, 1, $frozenUuid, 11, $tamperedTopLevelJson, hash('sha256', $tamperedTopLevelJson), $frozen->publicNetworkJson(), $frozen->publicNetworkSha256(), $frozen->claimSetSha256(), $mixedGeneration ), 'A self-consistent attachment rewrite bypassed the current claim set.'); $tamperedHost = json_decode( $frozen->identityJson(), true, 16, JSON_THROW_ON_ERROR ); $tamperedHost['host_id'] = 2; $tamperedHostJson = json_encode( $tamperedHost, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ); $rejects(static fn () => CloudFrozenPublicNetworkIdentity::fromStored( 500, 2, $frozenUuid, 10, $tamperedHostJson, hash('sha256', $tamperedHostJson), $frozen->publicNetworkJson(), $frozen->publicNetworkSha256(), $frozen->claimSetSha256(), $mixedGeneration ), 'A self-consistent host rewrite bypassed the current claim set.'); $tamperedNetwork = $mixedGeneration->publicNetwork()->toArray(); $tamperedNetwork['network_selector'] = 'bridge:br1'; $tamperedNetwork = CloudPublicNetworkSnapshot::fromArray($tamperedNetwork); $tamperedNetworkJson = json_encode( $tamperedNetwork->toArray(), JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ); $tamperedNetworkIdentity = json_decode( $frozen->identityJson(), true, 16, JSON_THROW_ON_ERROR ); $tamperedNetworkIdentity['public_network_sha256'] = $tamperedNetwork->fingerprint(); $tamperedNetworkIdentityJson = json_encode( $tamperedNetworkIdentity, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ); $rejects(static fn () => CloudFrozenPublicNetworkIdentity::fromStored( 500, 1, $frozenUuid, 10, $tamperedNetworkIdentityJson, hash('sha256', $tamperedNetworkIdentityJson), $tamperedNetworkJson, $tamperedNetwork->fingerprint(), $frozen->claimSetSha256(), $mixedGeneration ), 'A self-consistent network rewrite bypassed the current claim set.'); $otherGeneration = CloudPublicNetworkAllocationService::currentClaimSet( 504, 'committed', 901 ); $rejects(static fn () => CloudFrozenPublicNetworkIdentity::fromStored( 500, 1, $frozenUuid, 10, $frozen->identityJson(), $frozen->identitySha256(), $frozen->publicNetworkJson(), $frozen->publicNetworkSha256(), $frozen->claimSetSha256(), $otherGeneration ), 'A frozen identity accepted another reservation claim set.'); $activeRows = Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 500)->where('active_claim', 1) ->orderBy('address_family')->get()->all(); $activeAddresses = []; foreach ($activeRows as $activeRow) { $address = Capsule::table('cloud_public_ips') ->where('id', (int) $activeRow->public_ip_id)->first(); $activeAddresses[(int) $activeRow->public_ip_id] = $address; } $rejects(static fn () => CloudActivePublicNetworkClaimSet::fromLedger( 500, 2, $activeRows, $activeAddresses, $mixedGeneration->publicNetwork() ), 'A wrong-host claim set could be constructed for frozen identity input.'); $mismatchedSnapshot = $mixedGeneration->publicNetwork()->toArray(); $mismatchedSnapshot['ipv4']['address_cidr'] = '8.8.8.11/24'; $rejects(static fn () => CloudActivePublicNetworkClaimSet::fromLedger( 500, 1, $activeRows, $activeAddresses, CloudPublicNetworkSnapshot::fromArray($mismatchedSnapshot) ), 'A claim set accepted a snapshot describing another concrete address.'); Capsule::table('cloud_resource_reservation_ips') ->where('id', (int) $ipv6Claim->id)->update(['state' => 'uncertain']); Capsule::table('cloud_public_ips') ->where('id', (int) $ipv6Claim->public_ip_id)->update([ 'state' => 'uncertain', 'instance_id' => null, ]); $rejects(static fn () => CloudPublicNetworkAllocationService::currentClaimSet( 500, 'committed', 900 ), 'A mixed-state active pair was accepted as current authority.' ); Capsule::table('cloud_resource_reservation_ips') ->where('id', (int) $ipv6Claim->id)->update(['state' => 'committed']); Capsule::table('cloud_public_ips') ->where('id', (int) $ipv6Claim->public_ip_id)->update([ 'state' => 'assigned', 'instance_id' => 900, ]); $assertCurrentAuthorityRejects = static function ( callable $mutate, callable $restore, string $message ) use ($rejects): void { $mutate(); try { $rejects(static fn () => CloudPublicNetworkAllocationService::currentClaimSet( 500, 'committed', 900 ), $message ); } finally { $restore(); } }; $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_resource_reservations') ->where('id', 500)->update(['host_id' => 2]), static fn () => Capsule::table('cloud_resource_reservations') ->where('id', 500)->update(['host_id' => 1]), 'A claim set accepted a reservation bound to a missing host.' ); $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_resource_reservations') ->where('id', 500)->update([ 'state' => 'held', 'instance_id' => null, ]), static fn () => Capsule::table('cloud_resource_reservations') ->where('id', 500)->update([ 'state' => 'committed', 'instance_id' => 900, ]), 'A claim set accepted the wrong reservation lifecycle state.' ); $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_resource_reservations') ->where('id', 500)->update(['instance_id' => 999]), static fn () => Capsule::table('cloud_resource_reservations') ->where('id', 500)->update(['instance_id' => 900]), 'A claim set accepted the wrong reservation instance owner.' ); $instance900 = [ 'id' => 900, 'reservation_id' => 500, 'host_id' => 1, 'resource_pool_id' => 30, 'user_id' => 42, 'spec_json' => (string) Capsule::table('cloud_instances') ->where('id', 900)->value('spec_json'), ]; $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_instances') ->where('id', 900)->delete(), static fn () => Capsule::table('cloud_instances')->insert($instance900), 'A claim set accepted a missing reverse instance authority.' ); foreach ([ 'reservation_id' => 999, 'host_id' => 2, 'resource_pool_id' => 31, 'user_id' => 43, ] as $field => $invalidValue) { $expectedValue = $instance900[$field]; $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_instances') ->where('id', 900)->update([$field => $invalidValue]), static fn () => Capsule::table('cloud_instances') ->where('id', 900)->update([$field => $expectedValue]), 'A claim set accepted an instance with a broken ' . $field . ' reverse authority.' ); } $ipv4Claim = Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 500)->where('address_family', 4)->first(); $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_public_ips') ->where('id', (int) $ipv4Claim->public_ip_id) ->update(['instance_id' => 999]), static fn () => Capsule::table('cloud_public_ips') ->where('id', (int) $ipv4Claim->public_ip_id) ->update(['instance_id' => 900]), 'A claim set accepted an address owned by another instance.' ); $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_public_ips') ->where('id', (int) $ipv4Claim->public_ip_id) ->update(['reservation_ip_claim_id' => (int) $ipv6Claim->id]), static fn () => Capsule::table('cloud_public_ips') ->where('id', (int) $ipv4Claim->public_ip_id) ->update(['reservation_ip_claim_id' => (int) $ipv4Claim->id]), 'A claim set accepted an address pointing at another active claim.' ); $otherIpv4Id = (int) Capsule::table('cloud_public_ips') ->where('address_family', 4)->where('address', '8.8.8.11')->value('id'); $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_resource_reservation_ips') ->where('id', (int) $ipv4Claim->id) ->update(['public_ip_id' => $otherIpv4Id]), static fn () => Capsule::table('cloud_resource_reservation_ips') ->where('id', (int) $ipv4Claim->id) ->update(['public_ip_id' => (int) $ipv4Claim->public_ip_id]), 'A claim set accepted a claim pointing at another concrete address.' ); $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_public_ips') ->where('id', (int) $ipv4Claim->public_ip_id) ->update(['operation_id' => str_repeat('0', 32)]), static fn () => Capsule::table('cloud_public_ips') ->where('id', (int) $ipv4Claim->public_ip_id) ->update(['operation_id' => (string) $ipv4Claim->operation_id]), 'A claim set accepted an address with another operation identity.' ); Capsule::table('cloud_public_ips') ->where('id', (int) $ipv4Claim->public_ip_id) ->update(['instance_id' => 999]); try { $rejects( static fn () => CloudPublicNetworkAllocationService::currentClaimSet(500), 'Default current authority ignored the reservation instance owner.' ); } finally { Capsule::table('cloud_public_ips') ->where('id', (int) $ipv4Claim->public_ip_id) ->update(['instance_id' => 900]); } Capsule::table('cloud_resource_reservations')->where('id', 500)->update([ 'state' => 'held', 'instance_id' => null, ]); $rejects(static fn () => CloudPublicNetworkAllocationService::currentClaimSet(500, 'held'), 'A held reservation accepted committed active claims.' ); Capsule::table('cloud_resource_reservations')->where('id', 500)->update([ 'state' => 'committed', 'instance_id' => 900, ]); $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_host_network_attachments') ->where('id', 10)->update(['host_id' => 2]), static fn () => Capsule::table('cloud_host_network_attachments') ->where('id', 10)->update(['host_id' => 1]), 'A claim set accepted an attachment outside the reservation host.' ); $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_host_network_attachments') ->where('id', 10)->update(['line_id' => 8]), static fn () => Capsule::table('cloud_host_network_attachments') ->where('id', 10)->update(['line_id' => 7]), 'A claim set accepted an attachment outside the reservation line.' ); $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_host_network_attachments') ->where('id', 10)->update(['network_selector' => 'bridge:br1']), static fn () => Capsule::table('cloud_host_network_attachments') ->where('id', 10)->update(['network_selector' => 'bridge:br0']), 'A claim set accepted selector drift from the frozen instance snapshot.' ); $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_resource_reservations') ->where('id', 500)->update(['resource_pool_id' => 31]), static fn () => Capsule::table('cloud_resource_reservations') ->where('id', 500)->update(['resource_pool_id' => 30]), 'A claim set accepted a reservation pool from another commercial line.' ); $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_public_ip_pools') ->where('id', 100)->update(['host_id' => 2]), static fn () => Capsule::table('cloud_public_ip_pools') ->where('id', 100)->update(['host_id' => 1]), 'A claim set accepted a pool outside the reservation host.' ); $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_public_ip_pools') ->where('id', 100)->update(['network_attachment_id' => 11]), static fn () => Capsule::table('cloud_public_ip_pools') ->where('id', 100)->update(['network_attachment_id' => 10]), 'A claim set accepted a pool outside the active attachment.' ); $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_public_ips') ->where('id', (int) $ipv4Claim->public_ip_id) ->update(['host_id' => 2]), static fn () => Capsule::table('cloud_public_ips') ->where('id', (int) $ipv4Claim->public_ip_id) ->update(['host_id' => 1]), 'A claim set accepted an address outside the reservation host.' ); $assertCurrentAuthorityRejects( static fn () => Capsule::table('cloud_public_ips') ->where('id', (int) $ipv4Claim->public_ip_id) ->update(['address_binary' => inet_pton('8.8.8.250')]), static fn () => Capsule::table('cloud_public_ips') ->where('id', (int) $ipv4Claim->public_ip_id) ->update(['address_binary' => inet_pton('8.8.8.10')]), 'A claim set accepted an address with a forged canonical binary identity.' ); Capsule::table('cloud_resource_reservations')->where('id', 500)->update([ 'instance_id' => 999, ]); $rejects(static fn () => CloudPublicNetworkAllocationService::commitBundle( 500, 900, $operations[500] ), 'A committed replay ignored the locked reservation instance owner.'); $rejects(static fn () => CloudPublicNetworkAllocationService::releaseCommittedBundle( 500, 900, $operations[500], 'instance_deleted', true ), 'Committed release ignored the locked reservation instance owner.'); $assert(Capsule::table('cloud_public_ips')->where('instance_id', 900) ->where('state', 'assigned')->count() === 2 && Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 500)->where('active_claim', 1)->count() === 2, 'Rejecting a mismatched reservation owner released public-network state.'); Capsule::table('cloud_resource_reservations')->where('id', 500)->update([ 'instance_id' => 900, ]); $rejects(static fn () => CloudPublicNetworkAllocationService::releaseCommittedBundle( 500, 900, $operations[500], 'instance_deleted', false ), 'An assigned bundle was released before deletion was confirmed.'); Capsule::table('cloud_resource_reservations')->where('id', 500)->update([ 'state' => 'held', 'instance_id' => null, ]); $rejects(static fn () => CloudPublicNetworkAllocationService::releaseCommittedBundle( 500, 900, $operations[500], 'instance_deleted', true ), 'Committed claims were released through a held reservation state.'); Capsule::table('cloud_resource_reservations')->where('id', 500)->update([ 'state' => 'committed', 'instance_id' => 900, ]); CloudPublicNetworkAllocationService::releaseCommittedBundle( 500, 900, $operations[500], 'instance_deleted', true ); $assert(Capsule::table('cloud_public_ips')->where('instance_id', 900) ->count() === 0 && Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 500)->where('state', 'released')->count() === 2, 'Confirmed instance deletion did not release its committed dual-stack bundle.'); Capsule::table('cloud_public_ip_pools')->where('id', 100)->update([ 'cidr' => '198.51.100.0/24', 'gateway' => '198.51.100.1', ]); $invalidPoolError = null; try { CloudPublicNetworkAllocationService::reserveBundle( 1, 7, 503, $operations[503] ); } catch (CloudError $error) { $invalidPoolError = $error; } $assert($invalidPoolError instanceof CloudError && !$invalidPoolError instanceof CloudPublicNetworkCapacityUnavailable, 'Invalid pool configuration was downgraded to scheduler exhaustion.'); $assert(Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 503)->count() === 0, 'Rejecting a non-public pool retained a partial address claim.'); $addAgent = static function (int $hostId, array $selectors, array $overrides = []): void { Capsule::table('cloud_host_agents')->insert(array_replace([ 'host_id' => $hostId, 'enabled' => 1, 'revision' => 1, 'tested_revision' => 1, 'last_test_status' => 'healthy', 'network_selectors_json' => json_encode( $selectors, JSON_THROW_ON_ERROR ), 'public_network_capabilities_json' => json_encode([ 'contract' => 1, 'dual_stack_required' => true, 'project_owned_filter' => true, 'ndp_guard' => true, ], JSON_THROW_ON_ERROR), ], $overrides)); }; $addAttachment = static function (int $id, int $hostId, int $lineId, int $priority, array $overrides = []): void { Capsule::table('cloud_host_network_attachments')->insert(array_replace([ 'id' => $id, 'attachment_key' => str_pad(dechex($id), 32, '0', STR_PAD_LEFT), 'host_id' => $hostId, 'line_id' => $lineId, 'network_selector' => 'bridge:br' . $id, 'state' => 'active', 'priority' => $priority, 'config_revision' => 1, 'verification_state' => 'verified', 'verified_agent_revision' => 1, 'last_verified_at' => time(), ], $overrides)); }; $addPool = static function (int $id, int $hostId, int $attachmentId, int $family, int $priority, string $cidr, string $gateway, ?string $rangeStart = null, ?string $rangeEnd = null, int $maximum = 0): void { Capsule::table('cloud_public_ip_pools')->insert([ 'id' => $id, 'pool_key' => str_pad(dechex($id), 32, '0', STR_PAD_LEFT), 'host_id' => $hostId, 'network_attachment_id' => $attachmentId, 'address_family' => $family, 'cidr' => $cidr, 'gateway' => $gateway, 'gateway_on_link' => $family === 6 ? 1 : 0, 'guest_prefix_length' => (int) substr(strrchr($cidr, '/'), 1), 'dns_servers_json' => $family === 6 ? '["2606:4700:4700::1111"]' : '["1.1.1.1"]', 'reserved_ips_json' => '[]', 'allocation_start' => $rangeStart, 'allocation_end' => $rangeEnd, 'allocation_cursor' => null, 'max_allocations' => $maximum, 'state' => 'active', 'priority' => $priority, 'desired_revision' => 1, 'observed_revision' => 1, 'sync_state' => 'applied', ]); }; $addIp = static function (int $hostId, int $poolId, int $family, string $address, int $prefix, string $state = 'available'): void { $now = time(); $ipId = Capsule::table('cloud_public_ips')->insertGetId([ 'host_id' => $hostId, 'public_ip_pool_id' => $poolId, 'address_family' => $family, 'address' => $address, 'address_binary' => inet_pton($address), 'prefix_length' => $prefix, 'state' => 'available', 'reservation_id' => null, 'reservation_ip_claim_id' => null, 'instance_id' => null, 'operation_id' => null, 'created_at' => $now, 'updated_at' => $now, ]); if ($state !== 'assigned') { return; } $reservationId = 900000 + (int) $ipId; $operationId = str_pad(dechex($reservationId), 32, '0', STR_PAD_LEFT); $attachmentId = (int) Capsule::table('cloud_public_ip_pools') ->where('id', $poolId)->value('network_attachment_id'); Capsule::table('cloud_resource_reservations')->insert([ 'id' => $reservationId, 'host_id' => $hostId, 'operation_id' => $operationId, 'state' => 'committed', 'instance_id' => $reservationId, ]); $claimId = Capsule::table('cloud_resource_reservation_ips') ->insertGetId([ 'reservation_id' => $reservationId, 'address_family' => $family, 'claim_generation' => 1, 'active_claim' => 1, 'network_attachment_id' => $attachmentId, 'public_ip_pool_id' => $poolId, 'public_ip_id' => $ipId, 'operation_id' => $operationId, 'state' => 'committed', 'created_at' => $now, 'updated_at' => $now, 'released_at' => null, 'release_reason' => '', ]); Capsule::table('cloud_public_ips')->where('id', $ipId)->update([ 'state' => 'assigned', 'reservation_id' => $reservationId, 'reservation_ip_claim_id' => $claimId, 'instance_id' => $reservationId, 'operation_id' => $operationId, ]); }; $addReservation = static function (int $id, int $hostId, string $operationId): void { Capsule::table('cloud_resource_reservations')->insert([ 'id' => $id, 'host_id' => $hostId, 'operation_id' => $operationId, 'state' => 'held', ]); }; $assertHardFailure = static function (callable $callback, string $message) use ($assert): void { $failure = null; try { $callback(); } catch (CloudError $error) { $failure = $error; } $assert($failure instanceof CloudError && !$failure instanceof CloudPublicNetworkCapacityUnavailable, $message); }; $addPairHost = static function (int $hostId, int $lineId, int $attachmentId, int $ipv4PoolId, int $ipv6PoolId, string $ipv4Cidr, string $ipv6Cidr, array $agentOverrides = [], array $attachmentOverrides = []) use ( $addAgent, $addAttachment, $addPool, $addIp ): void { $ipv4Prefix = substr($ipv4Cidr, 0, strrpos($ipv4Cidr, '.')); $ipv6Network = substr($ipv6Cidr, 0, -3); $selector = 'bridge:br' . $attachmentId; Capsule::table('cloud_hosts')->insert(['id' => $hostId]); $addAgent($hostId, [$selector], $agentOverrides); $addAttachment( $attachmentId, $hostId, $lineId, 0, $attachmentOverrides ); $addPool( $ipv4PoolId, $hostId, $attachmentId, 4, 0, $ipv4Cidr, $ipv4Prefix . '.1' ); $addIp( $hostId, $ipv4PoolId, 4, $ipv4Prefix . '.10', 24 ); $addPool( $ipv6PoolId, $hostId, $attachmentId, 6, 0, $ipv6Cidr, '2404:c140:156::1', $ipv6Network . '10', $ipv6Network . '20', 4 ); }; // A complete attachment may have more than one pool per family. Exhaustion // of a higher-priority pool must not hide capacity in its next sibling. Capsule::table('cloud_hosts')->insert(['id' => 2]); $addAgent(2, ['bridge:br20', 'bridge:br30', 'bridge:br31']); $addAttachment(20, 2, 8, 0); $addPool(110, 2, 20, 4, 0, '9.9.9.0/24', '9.9.9.1'); Capsule::table('cloud_public_ip_pools')->where('id', 110)->update([ 'reserved_ips_json' => '["9.9.9.10"]', ]); $addPool(111, 2, 20, 4, 1, '11.0.0.0/24', '11.0.0.1'); $addIp(2, 111, 4, '11.0.0.10', 24); $addPool(210, 2, 20, 6, 0, '2404:c140:156:3b::/64', '2404:c140:156::1', '2404:c140:156:3b::10', '2404:c140:156:3b::20', 1); $addIp(2, 210, 6, '2404:c140:156:3b::10', 64, 'assigned'); $addPool(211, 2, 20, 6, 1, '2404:c140:156:3c::/64', '2404:c140:156::1', '2404:c140:156:3c::10', '2404:c140:156:3c::20', 2); $fallbackOperation = str_repeat('6', 32); $addReservation(600, 2, $fallbackOperation); $fallback = CloudPublicNetworkAllocationService::reserveBundle( 2, 8, 600, $fallbackOperation ); $assert($fallback->address(4) === '11.0.0.10/24' && $fallback->address(6) === '2404:c140:156:3c::10/64', 'Allocation did not skip exhausted higher-priority family pools.'); // The same deterministic search must continue with the next attachment when // the first complete attachment has no usable pair. $addAttachment(30, 2, 9, 0); $addPool(120, 2, 30, 4, 0, '12.0.0.0/24', '12.0.0.1'); $addIp(2, 120, 4, '12.0.0.10', 24); $addPool(220, 2, 30, 6, 0, '2404:c140:156:3d::/64', '2404:c140:156::1', '2404:c140:156:3d::10', '2404:c140:156:3d::20', 1); $addIp(2, 220, 6, '2404:c140:156:3d::10', 64, 'assigned'); $addAttachment(31, 2, 9, 1); $addPool(121, 2, 31, 4, 0, '13.0.0.0/24', '13.0.0.1'); $addIp(2, 121, 4, '13.0.0.10', 24); $addPool(221, 2, 31, 6, 0, '2404:c140:156:3e::/64', '2404:c140:156::1', '2404:c140:156:3e::10', '2404:c140:156:3e::20', 2); $attachmentFallbackOperation = str_repeat('5', 32); $addReservation(601, 2, $attachmentFallbackOperation); $attachmentFallback = CloudPublicNetworkAllocationService::reserveBundle( 2, 9, 601, $attachmentFallbackOperation ); $attachmentClaims = Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 601)->orderBy('address_family')->get(); $assert($attachmentFallback->address(4) === '13.0.0.10/24' && $attachmentFallback->address(6) === '2404:c140:156:3e::10/64' && $attachmentClaims->count() === 2 && $attachmentClaims->every( static fn ($claim): bool => (int) $claim->network_attachment_id === 31 ), 'Allocation did not continue with the next complete attachment.'); // An unavailable IPv6 leg must reject the whole bundle before the available // IPv4 leg is claimed. Capsule::table('cloud_hosts')->insert(['id' => 3]); $addAgent(3, ['bridge:br40']); $addAttachment(40, 3, 10, 0); $addPool(130, 3, 40, 4, 0, '14.0.0.0/24', '14.0.0.1'); $addIp(3, 130, 4, '14.0.0.10', 24); $addPool(230, 3, 40, 6, 0, '2404:c140:156:3f::/64', '2404:c140:156::1', '2404:c140:156:3f::10', '2404:c140:156:3f::20', 1); $addIp(3, 230, 6, '2404:c140:156:3f::10', 64, 'assigned'); $exhaustedOperation = str_repeat('4', 32); $addReservation(602, 3, $exhaustedOperation); $capacityUnavailable = false; try { CloudPublicNetworkAllocationService::reserveBundle( 3, 10, 602, $exhaustedOperation ); } catch (CloudPublicNetworkCapacityUnavailable) { $capacityUnavailable = true; } $assert($capacityUnavailable, 'Pair exhaustion did not produce the dedicated capacity result.'); $assert(Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 602)->count() === 0 && Capsule::table('cloud_public_ips')->where('public_ip_pool_id', 130) ->where('state', 'available')->whereNull('reservation_id')->count() === 1, 'A failed pair selection left a partial IPv4 claim.'); // Exact allocation must ignore an unverified high-priority attachment and // continue with a lower-priority attachment backed by the same locked current // Agent evidence. Capsule::table('cloud_hosts')->insert(['id' => 4]); $addAgent(4, ['bridge:br51']); $addAttachment(50, 4, 11, 0, [ 'verification_state' => 'pending', 'verified_agent_revision' => 0, ]); $addPool(140, 4, 50, 4, 0, '15.0.0.0/24', '15.0.0.1'); $addIp(4, 140, 4, '15.0.0.10', 24); $addPool(240, 4, 50, 6, 0, '2404:c140:156:40::/64', '2404:c140:156::1', '2404:c140:156:40::10', '2404:c140:156:40::20', 4); $addAttachment(51, 4, 11, 1); $addPool(141, 4, 51, 4, 0, '16.0.0.0/24', '16.0.0.1'); $addIp(4, 141, 4, '16.0.0.10', 24); $addPool(241, 4, 51, 6, 0, '2404:c140:156:41::/64', '2404:c140:156::1', '2404:c140:156:41::10', '2404:c140:156:41::20', 4); $verifiedFallbackOperation = str_repeat('3', 32); $addReservation(603, 4, $verifiedFallbackOperation); $verifiedFallback = CloudPublicNetworkAllocationService::reserveBundle( 4, 11, 603, $verifiedFallbackOperation ); $verifiedFallbackClaims = Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 603)->get(); $assert($verifiedFallback->address(4) === '16.0.0.10/24' && $verifiedFallbackClaims->count() === 2 && $verifiedFallbackClaims->every( static fn ($claim): bool => (int) $claim->network_attachment_id === 51 ), 'Allocator selected an unverified higher-priority attachment.'); // Missing capabilities, an untested selector and stale revision evidence are // configuration/evidence failures. They must never be downgraded to ordinary // address exhaustion and silently sent to the next host. $addPairHost( 5, 12, 60, 150, 250, '17.0.0.0/24', '2404:c140:156:50::/64' ); $evidenceCases = [ 'capabilities' => static function (): void { Capsule::table('cloud_host_agents')->where('host_id', 5)->update([ 'public_network_capabilities_json' => '{}', ]); }, 'selector' => static function (): void { Capsule::table('cloud_host_agents')->where('host_id', 5)->update([ 'network_selectors_json' => '[]', ]); }, 'agent revision' => static function (): void { Capsule::table('cloud_host_agents')->where('host_id', 5)->update([ 'tested_revision' => 0, ]); }, 'attachment revision' => static function (): void { Capsule::table('cloud_host_network_attachments')->where('id', 60) ->update(['verified_agent_revision' => 2]); }, ]; $validCapabilities = json_encode([ 'contract' => 1, 'dual_stack_required' => true, 'project_owned_filter' => true, 'ndp_guard' => true, ], JSON_THROW_ON_ERROR); $evidenceReservationId = 604; foreach ($evidenceCases as $label => $mutateEvidence) { Capsule::table('cloud_host_agents')->where('host_id', 5)->update([ 'enabled' => 1, 'revision' => 1, 'tested_revision' => 1, 'last_test_status' => 'healthy', 'network_selectors_json' => '["bridge:br60"]', 'public_network_capabilities_json' => $validCapabilities, ]); Capsule::table('cloud_host_network_attachments')->where('id', 60) ->update([ 'verification_state' => 'verified', 'verified_agent_revision' => 1, 'last_verified_at' => time(), ]); $mutateEvidence(); $operation = str_pad(dechex($evidenceReservationId), 32, 'a'); $addReservation($evidenceReservationId, 5, $operation); $assertHardFailure( static fn () => CloudPublicNetworkAllocationService::reserveBundle( 5, 12, $evidenceReservationId, $operation ), 'Stale ' . $label . ' evidence was treated as address exhaustion.' ); $assert(Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', $evidenceReservationId)->count() === 0, 'Stale ' . $label . ' evidence retained an address claim.'); $evidenceReservationId++; } // An active pool whose observed revision is ahead of the desired catalog is // not a capacity miss; it is a non-converged configuration that must stop the // paid placement transaction. Capsule::table('cloud_host_agents')->where('host_id', 5)->update([ 'tested_revision' => 1, 'network_selectors_json' => '["bridge:br60"]', 'public_network_capabilities_json' => $validCapabilities, ]); Capsule::table('cloud_host_network_attachments')->where('id', 60)->update([ 'verification_state' => 'verified', 'verified_agent_revision' => 1, ]); Capsule::table('cloud_public_ip_pools')->where('id', 150)->update([ 'observed_revision' => 2, ]); $revisionOperation = str_repeat('2', 32); $addReservation(608, 5, $revisionOperation); $assertHardFailure( static fn () => CloudPublicNetworkAllocationService::reserveBundle( 5, 12, 608, $revisionOperation ), 'A pool ahead of desired revision was treated as address exhaustion.' ); $assert(Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 608)->count() === 0, 'A non-converged pool retained an address claim.'); // Every materialized row is validated before capacity is evaluated. Shape, // topology and concrete address corruption must therefore hard-fail rather // than make a host look merely full. $addPairHost( 6, 13, 70, 160, 260, '18.0.0.0/24', '2404:c140:156:60::/64' ); $ledgerIpv4 = Capsule::table('cloud_public_ips') ->where('public_ip_pool_id', 160)->first(); $assert($ledgerIpv4 !== null, 'Ledger corruption fixture is missing IPv4.'); $ledgerBaseline = [ 'host_id' => 6, 'public_ip_pool_id' => 160, 'address_family' => 4, 'address' => '18.0.0.10', 'address_binary' => inet_pton('18.0.0.10'), 'prefix_length' => 24, 'state' => 'available', 'reservation_id' => null, 'instance_id' => null, 'operation_id' => null, ]; $ledgerCases = [ 'dirty available ownership' => [ 'reservation_id' => 9999, ], 'active row without ownership' => [ 'state' => 'reserved', ], 'wrong host' => [ 'host_id' => 999, ], 'wrong family' => [ 'address_family' => 6, ], 'wrong pool' => [ 'public_ip_pool_id' => 260, ], 'invalid prefix' => [ 'prefix_length' => 23, ], 'address outside pool' => [ 'address' => '19.0.0.10', 'address_binary' => inet_pton('19.0.0.10'), ], 'same text with different binary' => [ 'address_binary' => inet_pton('18.0.0.11'), ], ]; $ledgerReservationId = 609; foreach ($ledgerCases as $label => $mutation) { Capsule::table('cloud_public_ips')->where('id', (int) $ledgerIpv4->id) ->update(array_replace($ledgerBaseline, $mutation)); $operation = str_pad(dechex($ledgerReservationId), 32, 'b'); $addReservation($ledgerReservationId, 6, $operation); $assertHardFailure( static fn () => CloudPublicNetworkAllocationService::reserveBundle( 6, 13, $ledgerReservationId, $operation ), 'Corrupt ' . $label . ' ledger was treated as address exhaustion.' ); $assert(Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', $ledgerReservationId)->count() === 0, 'Corrupt ' . $label . ' ledger retained a partial claim.'); $ledgerReservationId++; } // Canonical text is part of the concrete identity just like the packed bytes. Capsule::table('cloud_public_ips')->where('id', (int) $ledgerIpv4->id) ->update($ledgerBaseline); $nonCanonicalIpv6Id = Capsule::table('cloud_public_ips')->insertGetId([ 'host_id' => 6, 'public_ip_pool_id' => 260, 'address_family' => 6, 'address' => '2404:C140:156:60::10', 'address_binary' => inet_pton('2404:c140:156:60::10'), 'prefix_length' => 64, 'state' => 'available', 'reservation_id' => null, 'instance_id' => null, 'operation_id' => null, 'created_at' => time(), 'updated_at' => time(), ]); $canonicalOperation = str_repeat('1', 32); $addReservation(617, 6, $canonicalOperation); $assertHardFailure( static fn () => CloudPublicNetworkAllocationService::reserveBundle( 6, 13, 617, $canonicalOperation ), 'Non-canonical concrete address text was treated as address exhaustion.' ); $assert(Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 617)->count() === 0, 'Rejecting non-canonical text retained a partial claim.'); Capsule::table('cloud_public_ips')->where('id', $nonCanonicalIpv6Id)->delete(); // A retained maintenance-migration source is a deliberate capacity debt, not // a corrupt assigned address. Accept only the exact migration + restore // reservation evidence, and keep that address unavailable to new placement. $addPairHost( 8, 15, 90, 180, 280, '21.0.0.0/24', '2404:c140:156:80::/64' ); foreach ([ ['21.0.0.14', 'changing', null, 8000, str_repeat('6', 32)], ['21.0.0.15', 'uncertain', null, 8001, str_repeat('7', 32)], ['21.0.0.16', 'disabled', null, null, null], ] as [$address, $state, $reservationId, $instanceId, $operationId]) { Capsule::table('cloud_public_ips')->insert([ 'host_id' => 8, 'public_ip_pool_id' => 180, 'address_family' => 4, 'address' => $address, 'address_binary' => inet_pton($address), 'prefix_length' => 24, 'state' => $state, 'reservation_id' => $reservationId, 'instance_id' => $instanceId, 'operation_id' => $operationId, 'created_at' => time(), 'updated_at' => time(), ]); } $retainedOperation = str_repeat('8', 32); $retainedIpId = Capsule::table('cloud_public_ips')->insertGetId([ 'host_id' => 8, 'public_ip_pool_id' => 180, 'address_family' => 4, 'address' => '21.0.0.11', 'address_binary' => inet_pton('21.0.0.11'), 'prefix_length' => 24, 'state' => 'assigned', 'reservation_id' => 700, 'instance_id' => null, 'operation_id' => $retainedOperation, 'created_at' => time(), 'updated_at' => time(), ]); Capsule::table('cloud_resource_reservations')->insert([ 'id' => 700, 'host_id' => 8, 'operation_id' => $retainedOperation, 'state' => 'committed', 'order_id' => null, 'backup_restore_id' => 800, 'instance_id' => null, 'network_attachment_id' => 90, 'public_ip_pool_id' => 180, 'public_ip_id' => $retainedIpId, ]); Capsule::table('cloud_instance_migrations')->insert([ 'id' => 800, 'instance_id' => 8000, 'restore_id' => 800, 'source_host_id' => 8, 'source_reservation_id' => 700, 'target_host_id' => 9, 'target_reservation_id' => 701, 'state' => 'source_retained', ]); $sourceRetainedOperation = str_repeat('9', 32); $addReservation(621, 8, $sourceRetainedOperation); $assertHardFailure(static fn () => CloudPublicNetworkAllocationService::reserveBundle( 8, 15, 621, $sourceRetainedOperation ), 'A pre-transition-ledger retained source bypassed active claim authority.' ); // The same debt remains valid while its cleanup mutation is in flight. Capsule::table('cloud_instance_migrations')->where('id', 800)->update([ 'state' => 'cleanup_running', ]); $addIp(8, 180, 4, '21.0.0.12', 24); $cleanupRunningOperation = str_repeat('a', 32); $addReservation(622, 8, $cleanupRunningOperation); $assertHardFailure(static fn () => CloudPublicNetworkAllocationService::reserveBundle( 8, 15, 622, $cleanupRunningOperation ), 'Cleanup-running legacy debt bypassed active claim authority.' ); // An orphan or any material identity mismatch must remain a hard ledger // failure. It must not become ordinary capacity exhaustion or claim a spare. $addIp(8, 180, 4, '21.0.0.13', 24); $retainedMigrationBaseline = (array) Capsule::table( 'cloud_instance_migrations' )->where('id', 800)->first(); $retainedReservationBaseline = (array) Capsule::table( 'cloud_resource_reservations' )->where('id', 700)->first(); $evidenceFailureReservationId = 623; $assertRetainedEvidenceFailure = static function ( string $label ) use ( &$evidenceFailureReservationId, $addReservation, $assertHardFailure, $assert ): void { $operation = str_pad( dechex($evidenceFailureReservationId), 32, 'b' ); $addReservation($evidenceFailureReservationId, 8, $operation); $assertHardFailure( static fn () => CloudPublicNetworkAllocationService::reserveBundle( 8, 15, $evidenceFailureReservationId, $operation ), $label . ' was downgraded to ordinary capacity exhaustion.' ); $assert(Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', $evidenceFailureReservationId) ->count() === 0, $label . ' retained a partial public-network claim.'); $evidenceFailureReservationId++; }; Capsule::table('cloud_instance_migrations')->where('id', 800)->delete(); $assertRetainedEvidenceFailure('Orphan retained-source address'); Capsule::table('cloud_instance_migrations')->insert( $retainedMigrationBaseline ); $retainedEvidenceMutations = [ 'Unsupported retained-source state' => static function (): void { Capsule::table('cloud_instance_migrations')->where('id', 800) ->update(['state' => 'completed']); }, 'Migration restore identity mismatch' => static function (): void { Capsule::table('cloud_instance_migrations')->where('id', 800) ->update(['restore_id' => 801]); }, 'Reservation host identity mismatch' => static function (): void { Capsule::table('cloud_resource_reservations')->where('id', 700) ->update(['host_id' => 999]); }, 'Reservation pool identity mismatch' => static function (): void { Capsule::table('cloud_resource_reservations')->where('id', 700) ->update(['public_ip_pool_id' => 280]); }, 'Reservation IP identity mismatch' => static function (): void { Capsule::table('cloud_resource_reservations')->where('id', 700) ->update(['public_ip_id' => 999999]); }, 'Reservation operation identity mismatch' => static function (): void { Capsule::table('cloud_resource_reservations')->where('id', 700) ->update(['operation_id' => str_repeat('f', 32)]); }, ]; foreach ($retainedEvidenceMutations as $label => $mutate) { Capsule::table('cloud_instance_migrations')->where('id', 800)->update( $retainedMigrationBaseline ); Capsule::table('cloud_resource_reservations')->where('id', 700)->update( $retainedReservationBaseline ); $mutate(); $assertRetainedEvidenceFailure($label); } Capsule::table('cloud_instance_migrations')->where('id', 800)->update( $retainedMigrationBaseline ); Capsule::table('cloud_resource_reservations')->where('id', 700)->update( $retainedReservationBaseline ); // Every state transition repeats the same concrete identity checks. A valid // text paired with different packed bytes, or an address moved to another // host, cannot be committed, fenced or released. $addPairHost( 7, 14, 80, 170, 270, '20.0.0.0/24', '2404:c140:156:70::/64' ); $commitIdentityOperation = str_repeat('c', 32); $addReservation(618, 7, $commitIdentityOperation); CloudPublicNetworkAllocationService::reserveBundle( 7, 14, 618, $commitIdentityOperation ); $commitIpv4 = Capsule::table('cloud_public_ips') ->where('reservation_id', 618)->where('address_family', 4)->first(); $assert($commitIpv4 !== null, 'Commit identity fixture is missing IPv4.'); Capsule::table('cloud_public_ips')->where('id', (int) $commitIpv4->id) ->update(['address_binary' => inet_pton('20.0.0.11')]); $assertHardFailure( static fn () => CloudPublicNetworkAllocationService::commitBundle( 618, 918, $commitIdentityOperation ), 'Commit accepted same-text/different-binary concrete identity.' ); Capsule::table('cloud_public_ips')->where('id', (int) $commitIpv4->id) ->update(['address_binary' => inet_pton('20.0.0.10')]); CloudPublicNetworkAllocationService::commitBundle( 618, 918, $commitIdentityOperation ); Capsule::table('cloud_resource_reservations')->where('id', 618)->update([ 'state' => 'committed', 'instance_id' => 918, ]); Capsule::table('cloud_public_ips')->where('id', (int) $commitIpv4->id) ->update(['host_id' => 999]); $assertHardFailure( static fn () => CloudPublicNetworkAllocationService::releaseCommittedBundle( 618, 918, $commitIdentityOperation, 'instance_deleted', true ), 'Committed release accepted a concrete address from another host.' ); Capsule::table('cloud_public_ips')->where('id', (int) $commitIpv4->id) ->update(['host_id' => 7]); CloudPublicNetworkAllocationService::releaseCommittedBundle( 618, 918, $commitIdentityOperation, 'instance_deleted', true ); $fenceIdentityOperation = str_repeat('d', 32); $addReservation(619, 7, $fenceIdentityOperation); CloudPublicNetworkAllocationService::reserveBundle( 7, 14, 619, $fenceIdentityOperation ); $fenceIpv4 = Capsule::table('cloud_public_ips') ->where('reservation_id', 619)->where('address_family', 4)->first(); $assert($fenceIpv4 !== null, 'Fence identity fixture is missing IPv4.'); Capsule::table('cloud_public_ips')->where('id', (int) $fenceIpv4->id) ->update(['host_id' => 999]); $assertHardFailure( static fn () => CloudPublicNetworkAllocationService::fenceBundle( 619, $fenceIdentityOperation ), 'Fence accepted a concrete address from another host.' ); Capsule::table('cloud_public_ips')->where('id', (int) $fenceIpv4->id) ->update(['host_id' => 7]); CloudPublicNetworkAllocationService::fenceBundle( 619, $fenceIdentityOperation ); Capsule::table('cloud_resource_reservations')->where('id', 619)->update([ 'state' => 'uncertain', ]); CloudPublicNetworkAllocationService::releaseBundle( 619, $fenceIdentityOperation, 'confirmed_not_applied', true ); $releaseIdentityOperation = str_repeat('e', 32); $addReservation(620, 7, $releaseIdentityOperation); CloudPublicNetworkAllocationService::reserveBundle( 7, 14, 620, $releaseIdentityOperation ); $releaseIpv4 = Capsule::table('cloud_public_ips') ->where('reservation_id', 620)->where('address_family', 4)->first(); $assert($releaseIpv4 !== null, 'Release identity fixture is missing IPv4.'); Capsule::table('cloud_public_ips')->where('id', (int) $releaseIpv4->id) ->update(['address_binary' => inet_pton('20.0.0.11')]); $assertHardFailure( static fn () => CloudPublicNetworkAllocationService::releaseBundle( 620, $releaseIdentityOperation, 'confirmed_not_applied', true ), 'Release accepted same-text/different-binary concrete identity.' ); Capsule::table('cloud_public_ips')->where('id', (int) $releaseIpv4->id) ->update(['address_binary' => inet_pton('20.0.0.10')]); CloudPublicNetworkAllocationService::releaseBundle( 620, $releaseIdentityOperation, 'confirmed_not_applied', true ); // A scheduler-frozen bundle must be allocated exactly, even when a different // attachment has higher ordinary-placement priority. Capsule::table('cloud_hosts')->insert(['id' => 9]); $addAgent(9, ['bridge:br100', 'bridge:br101', 'bridge:br102']); $addAttachment(100, 9, 16, 0); $addPool(190, 9, 100, 4, 0, '25.0.0.0/24', '25.0.0.1'); $addIp(9, 190, 4, '25.0.0.10', 24); $addPool(290, 9, 100, 6, 0, '2404:c140:156:90::/64', '2404:c140:156::1', '2404:c140:156:90::10', '2404:c140:156:90::20', 4); $addAttachment(101, 9, 16, 1); $addPool(191, 9, 101, 4, 0, '26.0.0.0/24', '26.0.0.1'); $addIp(9, 191, 4, '26.0.0.10', 24); $addPool(291, 9, 101, 6, 0, '2404:c140:156:91::/64', '2404:c140:156::1', '2404:c140:156:91::10', '2404:c140:156:91::20', 4); $addAttachment(102, 9, 16, 2); $addPool(192, 9, 102, 4, 0, '27.0.0.0/24', '27.0.0.1'); $addIp(9, 192, 4, '27.0.0.10', 24); $addPool(292, 9, 102, 6, 0, '2404:c140:156:92::/64', '2404:c140:156::1', '2404:c140:156:92::10', '2404:c140:156:92::20', 1); $addIp(9, 292, 6, '2404:c140:156:92::10', 64, 'assigned'); // Exact admission must not expand its lock/failure domain to an unrelated // active pool whose desired host configuration has not converged yet. $addPool(193, 9, 100, 4, 5, '28.0.0.0/24', '28.0.0.1'); Capsule::table('cloud_public_ip_pools')->where('id', 193)->update([ 'observed_revision' => 0, 'sync_state' => 'pending', ]); $bundleEvidence = static function (int $hostId, int $lineId, int $attachmentId, int $ipv4PoolId, int $ipv6PoolId): array { $attachment = Capsule::table('cloud_host_network_attachments') ->where('id', $attachmentId)->first(); $ipv4 = Capsule::table('cloud_public_ip_pools') ->where('id', $ipv4PoolId)->first(); $ipv6 = Capsule::table('cloud_public_ip_pools') ->where('id', $ipv6PoolId)->first(); return [ 'host_id' => $hostId, 'line_id' => $lineId, 'network_attachment_id' => $attachmentId, 'attachment_key' => (string) $attachment->attachment_key, 'network_selector' => (string) $attachment->network_selector, 'attachment_config_revision' => (int) $attachment->config_revision, 'attachment_verified_agent_revision' => (int) $attachment->verified_agent_revision, 'ipv4_pool' => [ 'public_ip_pool_id' => $ipv4PoolId, 'pool_key' => (string) $ipv4->pool_key, 'network_attachment_id' => $attachmentId, 'address_family' => 4, 'desired_revision' => (int) $ipv4->desired_revision, 'observed_revision' => (int) $ipv4->observed_revision, ], 'ipv6_pool' => [ 'public_ip_pool_id' => $ipv6PoolId, 'pool_key' => (string) $ipv6->pool_key, 'network_attachment_id' => $attachmentId, 'address_family' => 6, 'desired_revision' => (int) $ipv6->desired_revision, 'observed_revision' => (int) $ipv6->observed_revision, ], ]; }; $lowerPriorityEvidence = $bundleEvidence(9, 16, 101, 191, 291); $lowerPriorityConstraint = CloudPublicNetworkBundleConstraint::fromArray( $lowerPriorityEvidence ); $plannedOperation = str_repeat('1', 31) . '0'; $addReservation(9000, 9, $plannedOperation); $planned = CloudPublicNetworkAllocationService::reservePlannedBundle( 9, 16, 9000, $plannedOperation, $lowerPriorityConstraint ); $plannedClaims = Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 9000)->orderBy('address_family')->get(); $assert($planned->address(4) === '26.0.0.10/24' && $plannedClaims->count() === 2 && $plannedClaims->every( static fn ($claim): bool => (int) $claim->network_attachment_id === 101 ), 'Planned allocation fell back to a different attachment.'); $plannedReplay = CloudPublicNetworkAllocationService::reservePlannedBundle( 9, 16, 9000, $plannedOperation, $lowerPriorityConstraint ); $assert(hash_equals($planned->fingerprint(), $plannedReplay->fingerprint()) && Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 9000)->count() === 2, 'Exact planned replay allocated another bundle.'); $differentConstraint = CloudPublicNetworkBundleConstraint::fromArray( $bundleEvidence(9, 16, 100, 190, 290) ); $rejects( static fn () => CloudPublicNetworkAllocationService::reservePlannedBundle( 9, 16, 9000, $plannedOperation, $differentConstraint ), 'Exact replay accepted a different bundle constraint.' ); // Exhaustion of the selected IPv6 pool must not fall back to either complete // attachment, nor may it leave a partial IPv4 claim behind. $exhaustedPlannedOperation = str_repeat('2', 31) . '0'; $addReservation(9001, 9, $exhaustedPlannedOperation); $plannedCapacityFailure = false; try { CloudPublicNetworkAllocationService::reservePlannedBundle( 9, 16, 9001, $exhaustedPlannedOperation, CloudPublicNetworkBundleConstraint::fromArray( $bundleEvidence(9, 16, 102, 192, 292) ) ); } catch (CloudPublicNetworkCapacityUnavailable) { $plannedCapacityFailure = true; } $assert($plannedCapacityFailure && Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', 9001)->count() === 0 && Capsule::table('cloud_public_ips')->where('public_ip_pool_id', 192) ->where('state', 'available')->whereNull('reservation_id')->count() === 1, 'Planned pair exhaustion fell back or retained a partial claim.'); // Every immutable attachment/pool field is checked again under allocator // locks. Mutable advisory available_units is deliberately not in this value. $driftOperation = str_repeat('3', 31) . '0'; $addReservation(9002, 9, $driftOperation); $drifts = []; $drift = $bundleEvidence(9, 16, 100, 190, 290); $drift['attachment_key'] = str_repeat('f', 32); $drifts[] = $drift; $drift = $bundleEvidence(9, 16, 100, 190, 290); $drift['network_selector'] = 'bridge:br999'; $drifts[] = $drift; $drift = $bundleEvidence(9, 16, 100, 190, 290); $drift['attachment_config_revision']++; $drifts[] = $drift; $drift = $bundleEvidence(9, 16, 100, 190, 290); $drift['attachment_verified_agent_revision']++; $drifts[] = $drift; $drift = $bundleEvidence(9, 16, 100, 190, 290); $drift['ipv4_pool']['pool_key'] = str_repeat('e', 32); $drifts[] = $drift; $drift = $bundleEvidence(9, 16, 100, 190, 290); $drift['ipv6_pool']['desired_revision']++; $drift['ipv6_pool']['observed_revision']++; $drifts[] = $drift; foreach ($drifts as $driftedEvidence) { $assertHardFailure( static fn () => CloudPublicNetworkAllocationService::reservePlannedBundle( 9, 16, 9002, $driftOperation, CloudPublicNetworkBundleConstraint::fromArray( $driftedEvidence ) ), 'Planned allocation accepted drifted immutable bundle evidence.' ); } $invalidEvidence = $bundleEvidence(9, 16, 100, 190, 290); $invalidEvidence['host_id'] = '8'; $rejects( static fn () => CloudPublicNetworkBundleConstraint::fromArray( $invalidEvidence ), 'Bundle constraint accepted a string host identity.' ); $invalidEvidence = $bundleEvidence(9, 16, 100, 190, 290); $invalidEvidence['available_units'] = 1; $rejects( static fn () => CloudPublicNetworkBundleConstraint::fromArray( $invalidEvidence ), 'Bundle constraint accepted mutable advisory capacity evidence.' ); echo "Atomic dual-stack public network allocation tests passed.\n";