//! A device's standing is read from that device's facts, not from every fact //! in the file. //! //! The chokepoint asks [`cotton_store::revocation::device_standing`] on every //! signed request — twice for an owner's device in a project database, once //! there and once in the personal one. Until schema 96 the only index on //! `cotton_revocations` led with `workspace_id`, which no reader of a //! subject's facts names. use cotton_store::fixtures::{file_at_version, seed_workspace_row, NOW, WORKSPACE}; use cotton_store::revocation::{device_standing, Standing}; use cotton_store::schema::ddl::SCHEMA_VERSION; use cotton_store::Driver; use rusqlite::types::Value as SqlValue; const KEY: &str = "ddddddddddddddddddddddddddddddddddddddddddd"; /// A file holding one live device and `others` facts about other things — the /// pruned runner registrations and spent tokens a served file fills with. fn file_with(others: i64) -> Driver { let db = file_at_version(SCHEMA_VERSION); seed_workspace_row(&db); let text = |s: &str| SqlValue::Text(s.to_string()); db.run( "INSERT INTO cotton_devices (id, workspace_id, device_id, name, approved_at, approved_by_device, created_at, updated_at) \ VALUES ('device-row-1', ?, ?, 'A phone', ?, 'self', ?, ?)", &[text(WORKSPACE), text(KEY), text(NOW), text(NOW), text(NOW)], ) .expect("a device"); db.run( "WITH RECURSIVE n(i) AS (SELECT 0 UNION ALL SELECT i + 1 FROM n WHERE i + 1 < ?1) \ INSERT INTO cotton_revocations (id, workspace_id, subject_table, subject_id, revoked_at, actor, reason) \ SELECT printf('fact-%08d', i), ?2, 'cotton_repo_runners', printf('runner-%08d', i), ?3, 'job:prune', 'pruned' FROM n", &[SqlValue::Integer(others), text(WORKSPACE), text(NOW)], ) .expect("the other facts"); db } fn steps_to_read_a_standing(db: &Driver) -> u64 { let before = db.steps(); assert_eq!(device_standing(db, KEY).expect("a standing"), Standing::Live); db.steps() - before } /// RULE: A device's standing is read through an index that leads with the subject, so admitting a request costs the same in a file of 20,000 revocation facts as in one of 200 — never a read of every fact. /// Measured 11 Oct 2026 on this fixture, in SQLite's own steps (`Driver::steps`): 82 with the index among 200 facts and 82 among 20,000; 200,073 among 20,000 without it. On the owner's personal database (17,378 facts) it was 52,000 to 104,000 steps a request. #[test] fn a_standing_reads_the_devices_own_facts_and_never_the_table() { let small = steps_to_read_a_standing(&file_with(200)); let large = steps_to_read_a_standing(&file_with(20_000)); assert!(large <= small + small / 2, "a standing read {large} steps among 20,000 facts and {small} among 200"); // And the measure sees the read it guards against: without the index the // same standing reads every fact. let unindexed = file_with(20_000); unindexed.exec("DROP INDEX idx_cotton_revocations_of_subject").expect("the index is there to drop"); let scanned = steps_to_read_a_standing(&unindexed); assert!(scanned > large * 100, "without the index the standing read {scanned} steps, with it {large}"); }