This commit is contained in:
2026-07-23 16:37:12 +02:00
parent 99c345f2f2
commit 0da75c1311
2159 changed files with 230833 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
# file: sql/validation/.gitignore
# version: 1

View File

@@ -0,0 +1,107 @@
-- file: sql/validation/000_core_integrity.sql
-- version: 5
-- Contrôles d'intégrité raw/core/ledger pour l'extracteur 0.3.4.
-- 1. Aucune signature raw en double.
SELECT signature, COUNT(*) AS duplicate_count
FROM kb_sol_raw_transactions
GROUP BY signature
HAVING COUNT(*) > 1;
-- 2. Aucune transaction core sans lineage raw valide.
SELECT core.signature, core.raw_transaction_id
FROM kb_sol_core_transactions core
LEFT JOIN kb_sol_raw_transactions raw ON raw.id = core.raw_transaction_id
WHERE core.raw_transaction_id IS NULL OR raw.id IS NULL;
-- 3. Aucune ligne fille orpheline.
SELECT 'account_keys' AS child_table, child.id
FROM kb_sol_core_account_keys child
LEFT JOIN kb_sol_core_transactions core ON core.id = child.transaction_id
WHERE core.id IS NULL
UNION ALL
SELECT 'instructions', child.id
FROM kb_sol_core_instructions child
LEFT JOIN kb_sol_core_transactions core ON core.id = child.transaction_id
WHERE core.id IS NULL
UNION ALL
SELECT 'inner_instructions', child.id
FROM kb_sol_core_inner_instructions child
LEFT JOIN kb_sol_core_transactions core ON core.id = child.transaction_id
WHERE core.id IS NULL
UNION ALL
SELECT 'logs', child.id
FROM kb_sol_core_logs child
LEFT JOIN kb_sol_core_transactions core ON core.id = child.transaction_id
WHERE core.id IS NULL
UNION ALL
SELECT 'balance_changes', child.id
FROM kb_sol_core_balance_changes child
LEFT JOIN kb_sol_core_transactions core ON core.id = child.transaction_id
WHERE core.id IS NULL;
-- 4. Aucune duplication des indices et chemins stables.
SELECT signature, account_index::TEXT AS stable_key, COUNT(*) AS duplicate_count
FROM kb_sol_core_account_keys
GROUP BY signature, account_index
HAVING COUNT(*) > 1
UNION ALL
SELECT signature, instruction_path, COUNT(*)
FROM kb_sol_core_instructions
GROUP BY signature, instruction_path
HAVING COUNT(*) > 1
UNION ALL
SELECT signature, instruction_path, COUNT(*)
FROM kb_sol_core_inner_instructions
GROUP BY signature, instruction_path
HAVING COUNT(*) > 1
UNION ALL
SELECT signature, log_index::TEXT, COUNT(*)
FROM kb_sol_core_logs
GROUP BY signature, log_index
HAVING COUNT(*) > 1
UNION ALL
SELECT signature, balance_change_index::TEXT, COUNT(*)
FROM kb_sol_core_balance_changes
GROUP BY signature, balance_change_index
HAVING COUNT(*) > 1;
-- 5. Toute extraction réussie doit avoir une transaction core et une ligne raw core_extracted.
SELECT ledger.input_key AS signature, raw.processing_state, core.id AS core_transaction_id
FROM kb_sol_ops_processing_ledger ledger
LEFT JOIN kb_sol_raw_transactions raw ON raw.signature = ledger.input_key
LEFT JOIN kb_sol_core_transactions core ON core.signature = ledger.input_key
WHERE ledger.stage = 'core_extraction'
AND ledger.processor_name = 'canonical_to_core'
AND ledger.status = 'succeeded'
AND (raw.processing_state <> 'core_extracted' OR core.id IS NULL);
-- 6. Toute ligne raw core_extracted doit avoir un graphe core et un succès ledger correspondant au hash courant.
SELECT raw.signature, raw.canonical_json_hash
FROM kb_sol_raw_transactions raw
LEFT JOIN kb_sol_core_transactions core ON core.raw_transaction_id = raw.id
LEFT JOIN kb_sol_ops_processing_ledger ledger
ON ledger.stage = 'core_extraction'
AND ledger.processor_name = 'canonical_to_core'
AND ledger.input_key = raw.signature
AND ledger.input_hash = raw.canonical_json_hash
AND ledger.status = 'succeeded'
WHERE raw.processing_state = 'core_extracted'
AND (core.id IS NULL OR ledger.id IS NULL);
-- 7. Les chemins inner doivent référencer un parent top-level existant.
SELECT inner_instruction.signature, inner_instruction.parent_instruction_path, inner_instruction.instruction_path
FROM kb_sol_core_inner_instructions inner_instruction
LEFT JOIN kb_sol_core_instructions parent
ON parent.signature = inner_instruction.signature
AND parent.instruction_path = inner_instruction.parent_instruction_path
WHERE parent.id IS NULL;
-- 8. Résumé des statuts du processor.
SELECT processor_version, status, COUNT(*) AS row_count, SUM(attempt_count) AS attempts
FROM kb_sol_ops_processing_ledger
WHERE stage = 'core_extraction'
AND processor_name = 'canonical_to_core'
GROUP BY processor_version, status
ORDER BY processor_version, status;