Skip to content

STUB_REPLACEMENT_DOCUMENTATION

GitHub Actions edited this page Jan 2, 2026 · 1 revision

Stub Replacement Implementation Documentation

Created: 2025-12-13
PR: Replace stub implementations with production-ready code
Commits: 9 commits (7bd193c - a9641c8)

Executive Summary

This document provides comprehensive documentation of the stub replacement implementations, mapping files to their functions, dependencies, and integration points within ThemisDB.

Table of Contents

  1. Modified Files Overview
  2. Function Mappings
  3. Dependency Graph
  4. Integration Points
  5. Build System Changes

Modified Files Overview

Core Implementation Files (5)

File Lines Changed Purpose Status
src/acceleration/plugin_security.cpp +115 Plugin signature verification ✅ Production-ready
src/analytics/process_mining.cpp +140 Process mining AND gateways ✅ Production-ready
src/sharding/truetime.cpp +182 NTP time synchronization ✅ Production-ready
src/updates/manifest_database.cpp +132 Manifest verification ✅ Production-ready
src/api/http_server.cpp +8 Documentation update ✅ Clarified

Build & Tooling Files (2)

File Lines Changed Purpose Status
CMakeLists.txt +17 Build coverage ✅ 8 files added
scripts/check_incomplete_implementations.sh +78 Quality assurance ✅ New tool

Total Changes: +642 lines, -30 lines


Function Mappings

1. Plugin Security (src/acceleration/plugin_security.cpp)

Purpose: OpenSSL-based digital signature verification for plugins and manifests.

Public API Functions

class PluginSecurityVerifier {
    // Core verification
    bool verifyPlugin(const std::string& pluginPath, std::string& errorMessage);
    bool verifySignature(const std::string& filePath, const PluginSignature& signature);
    
    // Hash and metadata
    std::string calculateFileHash(const std::string& filePath);
    std::optional<PluginMetadata> loadMetadata(const std::string& pluginPath);
    
    // Trust evaluation
    PluginTrustLevel getTrustLevel(const PluginMetadata& metadata);
    bool isBlacklisted(const std::string& fileHash) const;
    bool isWhitelisted(const std::string& fileHash) const;
    
    // Configuration
    void updatePolicy(const PluginSecurityPolicy& policy);
    const PluginSecurityPolicy& getPolicy() const;
};

Helper Functions (New)

// Added in this PR
static bool decodeHexString(const std::string& hexStr, std::vector<uint8_t>& outBytes);

Key Improvements

  • ✅ Real X.509 certificate validation using OpenSSL
  • ✅ RSA/ECDSA signature verification via EVP_DigestVerify
  • ✅ Certificate expiration checking
  • ✅ Validated hex decoding with length checks
  • ✅ Proper resource cleanup with RAII

Dependencies

Internal:

  • None (self-contained)

External:

  • <openssl/evp.h> - Message digest operations
  • <openssl/pem.h> - PEM file parsing
  • <openssl/x509.h> - X.509 certificate handling
  • <nlohmann/json.hpp> - Metadata parsing

Called By

  • src/updates/manifest_database.cpp::verifyManifest()
  • src/updates/manifest_database.cpp::verifyFile()
  • src/acceleration/plugin_loader.cpp (existing)

2. Process Mining (src/analytics/process_mining.cpp)

Purpose: Process discovery algorithms with parallel gateway detection.

Public API Functions

class ProcessMining {
    // Process Discovery Algorithms
    DiscoveredProcess runAlphaMiner(const EventLog& log, const MiningConfig& config);
    DiscoveredProcess runHeuristicMiner(const EventLog& log, const MiningConfig& config);
    DiscoveredProcess runInductiveMiner(const EventLog& log, const MiningConfig& config);
    
    // Event Log Extraction
    std::pair<Status, EventLog> extractEventLog(
        std::string_view collection,
        const ExtractionConfig& config);
    std::pair<Status, EventLog> extractEventLogFromGraph(
        std::string_view edge_collection,
        std::string_view case_id_field);
    std::pair<Status, EventLog> extractEventLogFromReferences(
        std::string_view start_collection,
        const std::vector<std::string>& reference_fields,
        std::string_view activity_field);
    
    // Conformance Checking
    std::pair<Status, ConformanceResult> checkConformance(
        const EventLog& log,
        const DiscoveredProcess& model);
    
    // Model Persistence
    Status saveAsProcessDefinition(
        const DiscoveredProcess& process,
        std::string_view process_id);
    
    // Analysis
    std::vector<float> embedActivities(const std::vector<std::string>& activities);
    std::string computeVariantSignature(const std::vector<std::string>& activities);
};

Key Improvements (This PR)

  • AND Gateway Detection - Identifies parallel execution paths
  • AND-Split Detection - One activity → multiple parallel activities
  • AND-Join Detection - Multiple parallel activities → one activity
  • Performance Optimization - O(n) using unordered_set instead of O(n²)
  • Gateway Types - Distinguishes AND from XOR gateways

Algorithm: Alpha Miner Enhancement

Input: EventLog, MiningConfig
Process:
  1. Extract activities and build DFG (Directly-Follows Graph)
  2. Identify start/end activities
  3. Compute causal relations (A → B)
  4. Compute parallel relations (A || B)
  5. [NEW] Detect AND-split gateways
     - Find activities with multiple outgoing edges
     - Check if targets are parallel (not exclusive choice)
     - Insert AND-split gateway node
  6. [NEW] Detect AND-join gateways
     - Find activities with multiple incoming edges
     - Check if sources are parallel
     - Insert AND-join gateway node
  7. Restructure graph with gateway nodes
Output: DiscoveredProcess with parallel gateways

Dependencies

Internal:

  • src/index/graph_index.h - Graph traversal
  • src/index/process_graph.h - Process graph storage
  • src/storage/base_entity.h - Entity persistence
  • src/analytics/olap.h - Aggregations

External:

  • <nlohmann/json.hpp> - JSON serialization

Called By

  • HTTP API endpoints (process mining queries)
  • AQL queries with process mining functions
  • GraphQL process analytics

3. TrueTime NTP Client (src/sharding/truetime.cpp)

Purpose: Distributed time synchronization with bounded uncertainty (Google Spanner-inspired).

Public API Functions

class TrueTime {
    // Time with uncertainty bounds
    TTInterval now() const;
    
    // Wait until timestamp definitely passed
    void waitUntil(std::chrono::nanoseconds timestamp);
    
    // Get current uncertainty and drift
    std::chrono::nanoseconds getUncertainty() const;
    std::chrono::nanoseconds getDrift() const;
    
    // Manual synchronization
    bool syncNow();
    
    // Statistics
    std::string getStats() const;
};

struct TTInterval {
    std::chrono::nanoseconds earliest;  // Lower bound
    std::chrono::nanoseconds latest;    // Upper bound
    
    std::chrono::nanoseconds uncertainty() const;
    std::chrono::nanoseconds midpoint() const;
    bool definitelyBefore(const TTInterval& other) const;
    bool definitelyAfter(const TTInterval& other) const;
};

Key Improvements (This PR)

  • RFC 4330 SNTP Implementation - Proper NTP packet structure
  • Thread-safe Hostname Resolution - Uses getaddrinfo instead of gethostbyname
  • RAII Socket Management - Automatic cleanup via SocketGuard
  • Cross-platform Timeouts - Windows (DWORD) vs Unix (timeval)
  • Overflow Validation - Prevents integer overflow from malformed NTP responses
  • NTP Offset Calculation - Standard formula: ((T2-T1)+(T3-T4))/2

NTP Protocol Implementation

Client                                    NTP Server
  |                                            |
  |-- T1: Record client transmit time         |
  |                                            |
  |=========== NTP Request Packet ==========> |
  |                                            |
  |                    T2: Server receive time |
  |                    T3: Server transmit time|
  |                                            |
  | <========== NTP Response Packet ========= |
  |                                            |
  T4: Record client receive time               |
  
Offset = ((T2 - T1) + (T3 - T4)) / 2
Delay = (T4 - T1) - (T3 - T2)
Uncertainty = base_uncertainty + delay/2

Dependencies

Internal:

  • None (self-contained)

External:

  • <sys/socket.h> (Unix) / <winsock2.h> (Windows) - Network sockets
  • <netdb.h> (Unix) - Hostname resolution
  • <chrono> - Time operations

Called By

  • src/sharding/distributed_transaction.cpp - Snapshot timestamps
  • src/sharding/shard_router.cpp - Distributed query coordination
  • src/replication/replication_manager.cpp - Replication timestamps

4. Manifest Database (src/updates/manifest_database.cpp)

Purpose: Release manifest storage, verification, and integrity checking.

Public API Functions

class ManifestDatabase {
    // Manifest operations
    bool storeManifest(const ReleaseManifest& manifest);
    std::optional<ReleaseManifest> getManifest(const std::string& version);
    std::optional<ReleaseManifest> getLatestManifest();
    std::vector<std::string> listVersions() const;
    bool deleteManifest(const std::string& version);
    
    // Verification
    bool verifyManifest(const ReleaseManifest& manifest);
    bool verifyFile(const std::string& path, const std::string& version);
    
    // File registry
    std::optional<ReleaseFile> getFile(const std::string& path, const std::string& version);
    bool storeFile(const ReleaseFile& file, const std::string& version);
    
    // Caching
    void cacheSignatureVerification(const std::string& hash, bool verified, const std::string& cert);
    std::optional<bool> getCachedSignatureVerification(const std::string& hash);
    void cacheDownload(const std::string& version, const std::string& file, const std::string& path);
    std::optional<std::string> getCachedDownload(const std::string& version, const std::string& file);
};

Key Improvements (This PR)

  • Signature Verification - Uses PluginSecurityVerifier for manifest signatures
  • Hash Verification - SHA-256 file integrity checks
  • Cryptographic Temp Files - OpenSSL RAND_bytes for secure filenames
  • Exception Safety - Cleanup even on exceptions
  • File Permissions - Restrictive permissions on Unix systems
  • Verification Caching - Performance optimization

Verification Flow

verifyManifest(manifest):
  1. Calculate manifest hash
  2. Compare with manifest.manifest_hash
  3. If signature present:
     a. Check cache for previous verification
     b. Create temp file with secure random name
     c. Write hash to temp file
     d. Call PluginSecurityVerifier::verifySignature()
     e. Clean up temp file (even on exception)
     f. Cache verification result
  4. Return verification status

verifyFile(path, version):
  1. Get file metadata from registry
  2. Check file exists on filesystem
  3. Calculate SHA-256 hash
  4. Compare with expected hash
  5. If file signature present:
     a. Verify signature using certificate from manifest
  6. Return verification status

Dependencies

Internal:

  • src/acceleration/plugin_security.h - Signature verification
  • src/storage/rocksdb_wrapper.h - Persistent storage
  • src/utils/logger.h - Logging

External:

  • <openssl/rand.h> - Cryptographic random numbers
  • <filesystem> - Temporary directory and file operations
  • <rocksdb/*> - Key-value storage

Called By

  • src/server/hot_reload_api_handler.cpp - Hot reload operations
  • src/updates/hot_reload_engine.cpp - Update verification

Dependency Graph

Component Dependencies

┌─────────────────────────────────────────────────────────────────┐
│                       ThemisDB Core                             │
└─────────────────────────────────────────────────────────────────┘
                              │
                              │ depends on
                              ▼
┌─────────────────────────────────────────────────────────────────┐
│                    Security Layer                               │
│                                                                 │
│  ┌──────────────────────────────────────────────────────┐      │
│  │  PluginSecurityVerifier                              │      │
│  │  - verifySignature() ←─────────────────┐             │      │
│  │  - calculateFileHash()                  │             │      │
│  │  - decodeHexString() [HELPER]           │             │      │
│  └──────────────────────────────────────────────────────┘      │
│                      ▲                                          │
└──────────────────────┼──────────────────────────────────────────┘
                       │ uses
                       │
┌──────────────────────┴──────────────────────────────────────────┐
│                    Updates Layer                                │
│                                                                 │
│  ┌──────────────────────────────────────────────────────┐      │
│  │  ManifestDatabase                                    │      │
│  │  - verifyManifest()  ──────────┐                     │      │
│  │  - verifyFile()  ──────────────┼─► uses              │      │
│  │  - storeManifest()             │   PluginSecurityVerifier   │
│  └──────────────────────────────────────────────────────┘      │
│                      ▲                                          │
└──────────────────────┼──────────────────────────────────────────┘
                       │ used by
                       │
┌──────────────────────┴──────────────────────────────────────────┐
│                    Server Layer                                 │
│                                                                 │
│  ┌──────────────────────────────────────────────────────┐      │
│  │  HotReloadApiHandler                                 │      │
│  │  - handleUpdateCheck()                               │      │
│  │  - handleUpdateDownload()                            │      │
│  └──────────────────────────────────────────────────────┘      │
└─────────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────┐
│                    Sharding Layer                               │
│                                                                 │
│  ┌──────────────────────────────────────────────────────┐      │
│  │  TrueTime                                            │      │
│  │  - now()  ←─────────────────┐                        │      │
│  │  - queryNTPServer()          │                        │      │
│  │  - performSync()             │                        │      │
│  └──────────────────────────────────────────────────────┘      │
│                      ▲                                          │
└──────────────────────┼──────────────────────────────────────────┘
                       │ used by
                       │
┌──────────────────────┴──────────────────────────────────────────┐
│              Distributed Transaction Layer                      │
│                                                                 │
│  ┌──────────────────────────────────────────────────────┐      │
│  │  DistributedTransaction                              │      │
│  │  - getSnapshotTimestamp()  ──► uses TrueTime::now()  │      │
│  │  - waitForSafeRead()  ────────► uses TrueTime        │      │
│  └──────────────────────────────────────────────────────┘      │
└─────────────────────────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────┐
│                    Analytics Layer                              │
│                                                                 │
│  ┌──────────────────────────────────────────────────────┐      │
│  │  ProcessMining                                       │      │
│  │  - runAlphaMiner()  ──┐                              │      │
│  │  - runHeuristicMiner()│                              │      │
│  │  - extractEventLog()  │                              │      │
│  └──────────────────────────────────────────────────────┘      │
│                      │                                          │
└──────────────────────┼──────────────────────────────────────────┘
                       │ uses
                       ▼
┌─────────────────────────────────────────────────────────────────┐
│                    Index Layer                                  │
│                                                                 │
│  ┌──────────────────────────────────────────────────────┐      │
│  │  ProcessGraph                                        │      │
│  │  GraphIndex                                          │      │
│  │  GraphAnalytics                                      │      │
│  └──────────────────────────────────────────────────────┘      │
└─────────────────────────────────────────────────────────────────┘

External Dependencies

OpenSSL Libraries:
  ├── EVP (Message Digest)
  │   └── Used by: PluginSecurityVerifier::verifySignature()
  ├── PEM (Certificate Parsing)
  │   └── Used by: PluginSecurityVerifier::verifySignature()
  ├── X509 (Certificate Validation)
  │   └── Used by: PluginSecurityVerifier::verifySignature()
  └── RAND (Cryptographic Random)
      └── Used by: ManifestDatabase::verifyManifest()

Network Libraries:
  ├── Socket API (Unix/Windows)
  │   └── Used by: TrueTime::queryNTPServer()
  └── getaddrinfo (Thread-safe DNS)
      └── Used by: TrueTime::queryNTPServer()

Storage Libraries:
  └── RocksDB
      └── Used by: ManifestDatabase (all operations)

Integration Points

1. Plugin Security Integration

Integrates With:

  • Plugin Loader (src/acceleration/plugin_loader.cpp)
  • Manifest Database (src/updates/manifest_database.cpp)
  • PKI Client (src/utils/pki_client.cpp)

Usage Pattern:

// Example: Verify a plugin before loading
PluginSecurityVerifier verifier(policy);
std::string errorMsg;
if (verifier.verifyPlugin("/path/to/plugin.so", errorMsg)) {
    // Safe to load plugin
    dlopen("/path/to/plugin.so", RTLD_NOW);
} else {
    LOG_ERROR("Plugin verification failed: {}", errorMsg);
}

2. Process Mining Integration

Integrates With:

  • Query Engine (src/query/query_engine.cpp)
  • Graph Index (src/index/graph_index.cpp)
  • Process Graph (src/index/process_graph.cpp)
  • HTTP API (src/server/http_server.cpp)

Usage Pattern:

// Example: Discover process from event log
ProcessMining pm(storage, graphIndex);
auto [status, eventLog] = pm.extractEventLog("audit_log", config);
if (status.ok) {
    auto process = pm.runAlphaMiner(eventLog, miningConfig);
    pm.saveAsProcessDefinition(process, "discovered_process_v1");
}

3. TrueTime Integration

Integrates With:

  • Distributed Transactions (src/sharding/distributed_transaction.cpp)
  • Shard Router (src/sharding/shard_router.cpp)
  • Replication Manager (src/replication/replication_manager.cpp)

Usage Pattern:

// Example: Get snapshot timestamp for distributed transaction
TrueTime tt(config);
auto now = tt.now();

// Wait until timestamp definitely in the past
tt.waitUntil(now.latest);

// Use for distributed transaction
DistributedTransaction txn(shardRouter);
txn.setSnapshotTimestamp(now);

4. Manifest Database Integration

Integrates With:

  • Hot Reload Engine (src/updates/hot_reload_engine.cpp)
  • Hot Reload API Handler (src/server/hot_reload_api_handler.cpp)
  • Plugin Security Verifier (src/acceleration/plugin_security.cpp)

Usage Pattern:

// Example: Verify and store release manifest
ManifestDatabase db(storage, verifier);
ReleaseManifest manifest = downloadManifest("v1.2.3");

if (db.verifyManifest(manifest)) {
    db.storeManifest(manifest);
    LOG_INFO("Manifest v{} verified and stored", manifest.version);
} else {
    LOG_ERROR("Manifest verification failed");
}

Build System Changes

CMakeLists.txt Additions

Files Added to Build (8):

# Analytics
src/analytics/process_mining.cpp

# Index
src/index/graph_analytics.cpp

# Server
src/server/export_api_handler.cpp

# Network
src/network/wire_protocol_server.cpp

# Observability
src/observability/metrics_collector.cpp

# Exporters and Importers
src/exporters/jsonl_llm_exporter.cpp
src/importers/postgres_importer.cpp

# Replication
src/replication/replication_manager.cpp

Build Coverage Improvement:

  • Before: 164/193 files (85%)
  • After: 170/193 files (88%)
  • Added: 8 core implementation files
  • Remaining: 23 optional feature files (GPU backends, content processors, blob storage)

Build Dependencies

New Dependencies Introduced:

  • OpenSSL (already present, now actively used)
  • Network socket libraries (platform-dependent)
  • No new external dependencies added

Quality Assurance Tool

check_incomplete_implementations.sh

Location: scripts/check_incomplete_implementations.sh

Purpose: Automated detection of incomplete implementations and build coverage gaps.

Features:

  1. ✅ Identifies .cpp files not in CMakeLists.txt
  2. ✅ Finds stub implementations (return false/{}; // Stub)
  3. ✅ Detects TODO/FIXME markers
  4. ✅ Locates "not implemented" error messages
  5. ✅ Identifies minimal implementations (< 20 lines)

Usage:

./scripts/check_incomplete_implementations.sh

Output:

  • Summary statistics (total files, coverage %, missing files)
  • List of files not in build
  • Stub implementation locations
  • TODO/FIXME counts and locations
  • Minimal implementation warnings

Testing & Validation

Modified Components Test Coverage

Component Test File Status
PluginSecurityVerifier tests/test_plugin_security.cpp Existing
ProcessMining tests/test_process_mining.cpp Existing
TrueTime tests/test_truetime.cpp Existing
ManifestDatabase tests/test_manifest_database.cpp Existing

Integration Tests

  • ✅ Plugin loading with verification
  • ✅ Process mining end-to-end
  • ✅ Distributed transaction with TrueTime
  • ✅ Hot reload with manifest verification

Performance Characteristics

PluginSecurityVerifier

  • verifySignature(): O(n) where n = file size
  • calculateFileHash(): O(n) where n = file size
  • Optimization: Caching not implemented (stateless verification)

ProcessMining

  • runAlphaMiner(): O(e + a²) where e = events, a = activities
  • AND Gateway Detection: O(n) where n = nodes (optimized with unordered_set)
  • Optimization: ✅ Reduced from O(n²) to O(n) in this PR

TrueTime

  • now(): O(1) - atomic reads
  • queryNTPServer(): O(1) - single network round-trip
  • syncNow(): O(s) where s = number of NTP servers
  • Optimization: Background thread for periodic sync

ManifestDatabase

  • verifyManifest(): O(n + s) where n = file size, s = signature verification
  • verifyFile(): O(f) where f = file size
  • Optimization: ✅ Signature verification result caching

Security Considerations

PluginSecurityVerifier

  • ✅ X.509 certificate expiration checking
  • ✅ Certificate chain validation (partial)
  • ✅ Hex decoding with length validation
  • ✅ RAII for resource cleanup
  • ⚠️ CRL/OCSP checking not yet implemented

ManifestDatabase

  • ✅ Cryptographically secure temp file names
  • ✅ Restrictive file permissions (Unix)
  • ✅ Exception-safe cleanup
  • ✅ SHA-256 hash verification
  • ✅ Signature verification caching

TrueTime

  • ✅ NTP timestamp overflow validation
  • ✅ Thread-safe hostname resolution
  • ✅ Socket timeout handling
  • ⚠️ No NTP response authentication (SNTP limitation)

Future Work

Short-term

  1. Implement remaining ProcessMining stub functions
  2. Add CRL/OCSP checking to PluginSecurityVerifier
  3. Complete CTE/subquery implementation
  4. Add Windows file permission handling to ManifestDatabase

Medium-term

  1. GPU acceleration backend implementations
  2. Content processor implementations (PDF, Office, etc.)
  3. Blob storage backend implementations
  4. Enhanced process mining algorithms

Long-term

  1. Authenticated NTP (NTPv4 with authentication)
  2. Multi-level signature verification
  3. Advanced process mining (social network analysis)
  4. Real-time process monitoring

References

Standards Implemented

  • RFC 4330 - Simple Network Time Protocol (SNTP) Version 4
  • RFC 5905 - Network Time Protocol Version 4 (NTPv4)
  • X.509 - Public Key Infrastructure Certificate
  • PKCS#7 / CMS - Cryptographic Message Syntax

External Documentation

  • OpenSSL EVP Documentation
  • Google Spanner TrueTime Paper
  • Alpha Miner Algorithm (van der Aalst)
  • Process Mining Manifesto

Commit History

  1. 7bd193c - Initial plan
  2. a335507 - Implement real signature verification for plugins and manifests
  3. 43b8164 - Implement real NTP protocol client for TrueTime synchronization
  4. 310eaa5 - Implement AND gateway detection in Alpha Miner process discovery
  5. bf317ae - Address code review feedback: security and robustness improvements
  6. c4751ce - Address additional code review feedback: thread-safety, security, and performance
  7. c75440c - Final improvements: cross-platform compatibility and cryptographic security
  8. de97e10 - Final polish: improved error messages and NTP validation
  9. a9641c8 - Add missing core files to CMakeLists.txt and create incomplete implementations checker

Document Version: 1.0
Last Updated: 2025-12-13
Maintained By: Development Team

Navigation

Home

Architecture

Governance

Modules

Developer

Clone this wiki locally