-
Notifications
You must be signed in to change notification settings - Fork 0
Database Design
- Introduction
- Project Structure
- Core Components
- Architecture Overview
- Detailed Component Analysis
- Dependency Analysis
- Performance Considerations
- Troubleshooting Guide
- Conclusion
- Appendices
This document provides comprehensive data model documentation for the AgentID database schema and Redis caching strategy. It focuses on the PostgreSQL tables and Redis integration used to power agent identity management, verification challenges, and community moderation.
The backend is organized around a layered architecture:
- Configuration and environment variables
- Database connectivity and migrations
- Query layer for reusable SQL operations
- Route handlers for API endpoints
- Services for external integrations and badge building
- Middleware for security, rate limiting, and error handling
graph TB
subgraph "Application Layer"
R1["Routes<br/>agents.js, register.js, verify.js"]
S1["Services<br/>badgeBuilder.js, bagsAuthVerifier.js"]
M1["Middleware<br/>rateLimit.js, errorHandler.js"]
end
subgraph "Data Access Layer"
Q1["Queries<br/>models/queries.js"]
D1["PostgreSQL Pool<br/>models/db.js"]
RD["Redis Client<br/>models/redis.js"]
end
subgraph "Configuration"
C1["Config<br/>config/index.js"]
end
R1 --> Q1
R1 --> S1
S1 --> Q1
S1 --> RD
Q1 --> D1
M1 --> R1
C1 --> D1
C1 --> RD
- Purpose: Primary agent registry with reputation metrics and metadata.
- Primary key: pubkey (VARCHAR(88))
- Notable fields:
- name (VARCHAR(255), NOT NULL)
- description (TEXT)
- token_mint (VARCHAR(88))
- bags_api_key_id (VARCHAR(255))
- said_registered (BOOLEAN DEFAULT false)
- said_trust_score (INTEGER DEFAULT 0)
- capability_set (JSONB)
- creator_x (VARCHAR(255))
- creator_wallet (VARCHAR(88))
- registered_at (TIMESTAMPTZ DEFAULT NOW())
- last_verified (TIMESTAMPTZ)
- status (VARCHAR(20) DEFAULT 'verified')
- flag_reason (TEXT)
- bags_score (INTEGER DEFAULT 0)
- total_actions (INTEGER DEFAULT 0)
- successful_actions (INTEGER DEFAULT 0)
- failed_actions (INTEGER DEFAULT 0)
- fee_claims_count (INTEGER DEFAULT 0)
- fee_claims_sol (DECIMAL(18,9) DEFAULT 0)
- swaps_count (INTEGER DEFAULT 0)
- launches_count (INTEGER DEFAULT 0)
Constraints and indexes:
- Primary key on pubkey
- Indexes:
- idx_agent_identities_status on status
- idx_agent_identities_bags_score on bags_score DESC
- Purpose: Challenge-response tracking for PKI verification.
- Primary key: id (SERIAL)
- Foreign key: pubkey references agent_identities(pubkey)
- Notable fields:
- nonce (VARCHAR(64), UNIQUE, NOT NULL)
- challenge (TEXT NOT NULL)
- expires_at (TIMESTAMPTZ NOT NULL)
- completed (BOOLEAN DEFAULT false)
- created_at (TIMESTAMPTZ DEFAULT NOW())
- Purpose: Community moderation system for reporting agents.
- Primary key: id (SERIAL)
- Foreign key: pubkey references agent_identities(pubkey)
- Notable fields:
- reporter_pubkey (VARCHAR(88))
- reason (TEXT NOT NULL)
- evidence (JSONB)
- created_at (TIMESTAMPTZ DEFAULT NOW())
- resolved (BOOLEAN DEFAULT false)
erDiagram
AGENT_IDENTITIES {
varchar pubkey PK
varchar name
text description
varchar token_mint
varchar bags_api_key_id
boolean said_registered
integer said_trust_score
jsonb capability_set
varchar creator_x
varchar creator_wallet
timestamptz registered_at
timestamptz last_verified
varchar status
text flag_reason
integer bags_score
integer total_actions
integer successful_actions
integer failed_actions
integer fee_claims_count
decimal fee_claims_sol
integer swaps_count
integer launches_count
}
AGENT_VERIFICATIONS {
serial id PK
varchar pubkey FK
varchar nonce UK
text challenge
timestamptz expires_at
boolean completed
timestamptz created_at
}
AGENT_FLAGS {
serial id PK
varchar pubkey FK
varchar reporter_pubkey
text reason
jsonb evidence
timestamptz created_at
boolean resolved
}
AGENT_IDENTITIES ||--o{ AGENT_VERIFICATIONS : "has"
AGENT_IDENTITIES ||--o{ AGENT_FLAGS : "reported"
The system integrates PostgreSQL for durable persistence and Redis for high-throughput, short-lived caching. The application exposes REST endpoints that orchestrate database writes and reads, and Redis operations for badge caching.
- Connection pool uses the pg package with a connection string from configuration.
- Production SSL behavior sets rejectUnauthorized to false when NODE_ENV is production.
- Pool error events are logged without crashing the process.
- Query wrapper executes parameterized SQL and logs errors before rethrowing.
- Migration script creates tables and indexes in a transaction.
- Creates agent_identities, agent_verifications, and agent_flags.
- Adds indexes for performance on status, bags_score, pubkey, and resolved.
- Redis client uses ioredis with retryStrategy, maxRetriesPerRequest, and enableOfflineQueue.
- Cache keys for badges follow the pattern: badge:{pubkey}.
- TTL for badges is configured via BADGE_CACHE_TTL (default 60 seconds).
- Validates request body fields
- Verifies Bags signature using Ed25519
- Prevents replay by ensuring nonce appears in message
- Checks for existing agent
- Attempts SAID binding and stores agent record
- Issues a PKI challenge bound to an agent pubkey
- Responds to challenge by verifying signature
- Marks verification as completed upon success
- Enforces expiration and completion checks
- Badge JSON retrieval follows cache-first strategy
- Computes reputation and aggregates action stats
- Stores badge JSON in Redis with TTL
- Provides SVG and HTML widget variants
External dependencies:
- PostgreSQL driver (pg)
- Redis client (ioredis)
- Express ecosystem
- Utility libraries: base58, tweetnacl
- PostgreSQL: Use indexes on frequently filtered columns
- Redis: Short TTL for badges balances freshness and load
- Application: Rate limiting prevents abuse
- Database connectivity: Verify DATABASE_URL and SSL settings
- Migration failures: Ensure transactional migration runs to completion
- Redis connectivity: Monitor connect, error, and reconnect events
- API errors: Check global error handler logs
The AgentID data model centers on three core tables supporting identity, verification, and moderation. PostgreSQL provides durable storage with targeted indexes, while Redis caches frequently accessed badge data.
- DATABASE_URL: PostgreSQL connection string
- REDIS_URL: Redis connection string
- BADGE_CACHE_TTL: Cache TTL for badges (seconds)
- CHALLENGE_EXPIRY_SECONDS: Expiration for verification challenges (seconds)