251 lines
12 KiB
Rust
251 lines
12 KiB
Rust
// file: crates/ksp-store-postgres-lib/tests/dependency_boundary.rs
|
|
// version: 13
|
|
|
|
#![warn(missing_docs)]
|
|
#![deny(unreachable_pub)]
|
|
#![forbid(unsafe_code)]
|
|
|
|
//! Dependency and ownership canaries for the physical PostgreSQL Store backend.
|
|
|
|
#[test]
|
|
fn pre_005_backend_owns_exact_physical_runtime_dependencies_without_reverse_facade_edge() {
|
|
let manifest = include_str!("../Cargo.toml");
|
|
for required in
|
|
["deadpool-postgres", "ksp-logging-lib", "ksp-store-api", "rustls", "rustls-native-certs", "sha2", "tokio-postgres", "tokio-postgres-rustls"]
|
|
{
|
|
assert!(manifest.contains(required), "missing PostgreSQL backend dependency: {required}");
|
|
}
|
|
for forbidden in ["ksp-store-lib", "ksp-config-lib", "ksp-materializer", "ksp-program", "ksp-onchain-transport-lib", "ksp-offchain-transport-lib", "sqlx"] {
|
|
assert!(!manifest.contains(forbidden), "forbidden PostgreSQL backend dependency detected: {forbidden}");
|
|
}
|
|
let migration = include_str!("../src/migration.rs");
|
|
let schema = include_str!("../src/schema.rs");
|
|
let bootstrap_sql = include_str!("../migrations/v000_bootstrap/tables/001_ksp_store_schema_migrations.sql");
|
|
assert!(migration.contains("crate::V000_RESOURCES"));
|
|
assert!(migration.contains("crate::V001_RESOURCES"));
|
|
assert!(schema.contains("../migrations/v000_bootstrap/tables/001_ksp_store_schema_migrations.sql"));
|
|
assert!(schema.contains("../migrations/v001_raw_transaction/tables/001_ksp_store_identity.sql"));
|
|
assert!(bootstrap_sql.contains("ksp_store_schema_migrations"));
|
|
for forbidden in ["RawTransaction", "RawAccountState", "raw_transaction", "raw_account", "CORE", "DECODE", "SPECIALIZED"] {
|
|
assert!(!bootstrap_sql.contains(forbidden), "business schema leaked into immutable V000 SQL: {forbidden}");
|
|
}
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn pre_005_backend_keeps_environment_sql_migrations_and_physical_types_private() {
|
|
let crate_root = include_str!("../src/lib.rs");
|
|
assert!(crate_root.contains("mod error;"));
|
|
assert!(crate_root.contains("mod health;"));
|
|
assert!(crate_root.contains("mod migration;"));
|
|
assert!(crate_root.contains("mod raw_transaction;"));
|
|
assert!(crate_root.contains("mod runtime;"));
|
|
assert!(crate_root.contains("mod schema;"));
|
|
assert!(crate_root.contains("const _: &str = crate::TRACING_TARGET;"));
|
|
for forbidden in [
|
|
"pub mod ",
|
|
"ksp_store_lib",
|
|
"ksp_config_lib",
|
|
"tokio_postgres::Client",
|
|
"tokio_postgres::Row",
|
|
"tokio_postgres::Statement",
|
|
"deadpool_postgres::Pool;",
|
|
] {
|
|
assert!(!crate_root.contains(forbidden), "forbidden PostgreSQL crate-root surface detected: {forbidden}");
|
|
}
|
|
let runtime = include_str!("../src/runtime.rs");
|
|
for forbidden in [
|
|
"std::env",
|
|
"dotenv",
|
|
"KSP_",
|
|
"KSPB_",
|
|
"PGHOST",
|
|
"PGPORT",
|
|
"PGUSER",
|
|
"PGPASSWORD",
|
|
".pgpass",
|
|
"CREATE TABLE",
|
|
"INSERT INTO",
|
|
"UPDATE ",
|
|
"DELETE FROM",
|
|
"SELECT ",
|
|
] {
|
|
assert!(!runtime.contains(forbidden), "forbidden PostgreSQL backend ownership/scope content detected: {forbidden}");
|
|
}
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn pre_007_health_probe_remains_foundation_only_and_private_sql() {
|
|
let health = include_str!("../src/health.rs");
|
|
assert!(health.contains("SELECT 1::BIGINT"));
|
|
assert!(health.contains("ksp_store_schema_migrations"));
|
|
for forbidden in
|
|
["RawTransaction", "RawAccountState", "raw_transaction", "raw_account", "CORE", "DECODE", "SPECIALIZED", "std::env", "dotenv", "KSP_SECRET_"]
|
|
{
|
|
assert!(!health.contains(forbidden), "forbidden health ownership/scope content detected: {forbidden}");
|
|
}
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn pre_003_fix_001_migration_engine_uses_split_schema_contract_and_binds_network_without_repository_scope() {
|
|
let migration = include_str!("../src/migration.rs");
|
|
let schema = include_str!("../src/schema.rs");
|
|
let identity_table = include_str!("../migrations/v001_raw_transaction/tables/001_ksp_store_identity.sql");
|
|
let raw_table = include_str!("../migrations/v001_raw_transaction/tables/002_ksp_raw_transactions.sql");
|
|
let observation_table = include_str!("../migrations/v001_raw_transaction/tables/003_ksp_raw_transaction_observations.sql");
|
|
let archive_table = include_str!("../migrations/v001_raw_transaction/tables/004_ksp_raw_transaction_archive_payloads.sql");
|
|
let index = include_str!("../migrations/v001_raw_transaction/indexes/001_ix_ksp_raw_transactions_slot_signature.sql");
|
|
assert!(migration.contains("const EMBEDDED_MIGRATIONS: &[EmbeddedMigration]"));
|
|
assert!(migration.contains("MigrationHook::StoreIdentity"));
|
|
assert!(migration.contains("MigrationHookContext::AppliedNow"));
|
|
assert!(migration.contains("MigrationHookContext::Existing"));
|
|
assert!(migration.contains("schema_autocreate"));
|
|
assert!(migration.contains("schema_autoupdate"));
|
|
assert!(migration.contains("INSERT INTO ksp_store_identity (singleton, network) VALUES (1, $1)"));
|
|
assert!(migration.contains("SELECT singleton, network FROM ksp_store_identity ORDER BY singleton LIMIT 2"));
|
|
assert!(migration.contains("ksp_store_api::RawNetworkId::new(stored_network)"));
|
|
assert!(schema.contains("SchemaResourceState"));
|
|
assert!(schema.contains("verify_v001_external_compatibility"));
|
|
assert!(identity_table.contains("CREATE TABLE IF NOT EXISTS ksp_store_identity"));
|
|
assert!(raw_table.contains("CREATE TABLE IF NOT EXISTS ksp_raw_transactions"));
|
|
assert!(observation_table.contains("CREATE TABLE IF NOT EXISTS ksp_raw_transaction_observations"));
|
|
assert!(archive_table.contains("CREATE TABLE IF NOT EXISTS ksp_raw_transaction_archive_payloads"));
|
|
assert!(index.contains("CREATE INDEX IF NOT EXISTS ix_ksp_raw_transactions_slot_signature"));
|
|
for forbidden in ["impl ksp_store_api::RawTransaction", "repository", "sqlx", "RawAccountState"] {
|
|
assert!(!migration.contains(forbidden), "repository/cross-scope implementation leaked into migration engine: {forbidden}");
|
|
assert!(!schema.contains(forbidden), "repository/cross-scope implementation leaked into schema contract: {forbidden}");
|
|
}
|
|
for removed in ["migrations/V000__bootstrap.sql", "migrations/V001__raw_transaction.sql"] {
|
|
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(removed);
|
|
assert!(!path.exists(), "obsolete monolithic migration must be deleted by pre.003-fix.001: {removed}");
|
|
}
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn pre_004_raw_read_sql_and_mapping_remain_backend_private() {
|
|
let crate_root = include_str!("../src/lib.rs");
|
|
let raw = include_str!("../src/raw_transaction.rs");
|
|
assert!(crate_root.contains("mod raw_transaction;"));
|
|
assert!(!crate_root.contains("pub mod raw_transaction"));
|
|
for required in [
|
|
"SELECT transaction_row.signature",
|
|
"ksp_raw_transaction_observations",
|
|
"slot::text AS slot_text",
|
|
"RawPayload::try_new",
|
|
"RawTransactionTombstone::try_new",
|
|
"PostgresBackendErrorKind::DataInvalid",
|
|
"PostgresBackendErrorKind::WrongNetwork",
|
|
] {
|
|
assert!(raw.contains(required), "missing private RAW read mapping contract: {required}");
|
|
}
|
|
for forbidden in ["std::env", "dotenv", "ksp_store_lib", "ksp_config_lib"] {
|
|
assert!(!raw.contains(forbidden), "RAW module contains forbidden ownership material: {forbidden}");
|
|
}
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn pre_005_raw_write_sql_is_atomic_idempotent_and_keeps_direct_trait_scope_closed() {
|
|
let raw = include_str!("../src/raw_transaction.rs");
|
|
for required in [
|
|
"INSERT INTO ksp_raw_transactions",
|
|
"ON CONFLICT (signature) DO NOTHING RETURNING signature",
|
|
"FOR UPDATE",
|
|
"INSERT INTO ksp_raw_transaction_observations",
|
|
"ON CONFLICT (observation_key) DO NOTHING RETURNING observation_key",
|
|
"REHYDRATE_TRANSACTION_SQL",
|
|
"RawEntityWriteOutcome::SkippedPurged",
|
|
"RawEntityWriteOutcome::Rehydrated",
|
|
"RawObservationWriteOutcome::NotRecorded",
|
|
"PostgresBackendErrorKind::Conflict",
|
|
"PostgresBackendErrorKind::ReferenceNotFound",
|
|
"PostgresBackendErrorKind::WriteFailed",
|
|
] {
|
|
assert!(raw.contains(required), "missing pre.005 RAW write contract: {required}");
|
|
}
|
|
for forbidden in ["impl ksp_store_api::RawTransactionWrite", "impl ksp_store_api::RawTransactionObservationWrite"] {
|
|
assert!(!raw.contains(forbidden), "pre.005 opened direct Store trait scope prematurely: {forbidden}");
|
|
}
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn pre_006_raw_pagination_is_keyset_cursor_bound_and_policy_free() {
|
|
let raw = include_str!("../src/raw_transaction.rs");
|
|
let cursor = include_str!("../src/raw_transaction/cursor.rs");
|
|
let index = include_str!("../migrations/v001_raw_transaction/indexes/001_ix_ksp_raw_transactions_slot_signature.sql");
|
|
for required in [
|
|
"LIST_TRANSACTIONS_ASC_SQL",
|
|
"LIST_TRANSACTIONS_DESC_SQL",
|
|
"retention_state <> 'purged'",
|
|
"(slot, signature) >",
|
|
"(slot, signature) <",
|
|
"ORDER BY slot ASC, signature ASC",
|
|
"ORDER BY slot DESC, signature DESC",
|
|
"LIMIT $5",
|
|
"list_raw_transactions",
|
|
] {
|
|
assert!(raw.contains(required), "missing pre.006 keyset pagination contract: {required}");
|
|
}
|
|
for required in [
|
|
"CURSOR_BYTES: usize = 109",
|
|
"CURSOR_MAGIC",
|
|
"b\"KSPT\"",
|
|
"CURSOR_VERSION: u8 = 1",
|
|
"KSP/raw-transaction-cursor/v1",
|
|
"sha2::Sha256",
|
|
"query.network().as_str()",
|
|
"query.direction()",
|
|
"query.slots().start_inclusive()",
|
|
"query.slots().end_inclusive()",
|
|
"last_slot",
|
|
"last_signature",
|
|
"PageLimitUnsupported",
|
|
"9_223_372_036_854_775_806",
|
|
] {
|
|
assert!(cursor.contains(required), "missing pre.006 cursor/binding contract: {required}");
|
|
}
|
|
assert!(index.contains("ON ksp_raw_transactions (slot, signature)"));
|
|
assert!(index.contains("WHERE retention_state <> 'purged'"));
|
|
for forbidden in [" OFFSET ", "limit.min(", "clamp(", "500", "1000"] {
|
|
assert!(!raw.contains(forbidden), "pre.006 contains forbidden pagination/policy/later-scope material: {forbidden}");
|
|
assert!(!cursor.contains(forbidden), "pre.006 cursor contains forbidden pagination/policy/later-scope material: {forbidden}");
|
|
}
|
|
return;
|
|
}
|
|
|
|
#[test]
|
|
fn pre_007_raw_retention_is_atomic_compare_and_transition_without_fake_compaction() {
|
|
let raw = include_str!("../src/raw_transaction.rs");
|
|
let runtime = include_str!("../src/runtime.rs");
|
|
for required in [
|
|
"LOCK_RETENTION_TRANSACTION_SQL",
|
|
"FOR UPDATE",
|
|
"INSERT_ARCHIVE_PAYLOAD_SQL",
|
|
"INSERT INTO ksp_raw_transaction_archive_payloads (signature, payload)",
|
|
"UPDATE_ARCHIVED_TRANSACTION_SQL",
|
|
"SET payload = NULL, retention_state = 'archived'",
|
|
"DELETE_ARCHIVE_PAYLOAD_SQL",
|
|
"DELETE FROM ksp_raw_transaction_archive_payloads",
|
|
"UPDATE_PURGED_TRANSACTION_SQL",
|
|
"SET block_time_unix_millis = NULL, payload = NULL, retention_state = 'purged'",
|
|
"if current == target",
|
|
"if current != expected",
|
|
"RawRetentionWriteOutcome::AlreadyAtTarget",
|
|
"RawRetentionWriteOutcome::ExpectedStateMismatch",
|
|
"RawRetentionWriteOutcome::Applied",
|
|
"RetentionCompactionUnsupported",
|
|
"transition_raw_transaction_retention",
|
|
] {
|
|
assert!(raw.contains(required), "missing pre.007 retention contract: {required}");
|
|
}
|
|
assert!(runtime.contains("pub async fn transition_raw_transaction_retention"));
|
|
for forbidden in ["retention_state = 'compacted'", "impl ksp_store_api::RawTransactionRetentionWrite", "flate", "zstd", "lz4", "snappy"] {
|
|
assert!(!raw.contains(forbidden), "pre.007 contains fake compaction/direct-trait scope: {forbidden}");
|
|
}
|
|
return;
|
|
}
|