Core Functionalities
This section details Khedra's primary technical functionalities, explaining how each core feature is implemented and the technical approaches used.
Control Service
Service Management Interface
The Control Service exposes a minimal HTTP interface for pausing and unpausing supported services (scraper
, monitor
) and for reporting their pause status.
Technical Implementation
Implemented functions:
- Pause a pausable service
- Unpause a pausable service
- Report paused / running status
Scope: does not provide start/stop/restart, runtime config mutation, or metrics collection.
// Simplified Control Service interface
type ControlService struct {
serviceManager *ServiceManager
httpServer *http.Server
logger *slog.Logger
}
type ServiceStatus struct {
Name string
State ServiceState
LastStarted time.Time
Uptime time.Duration
Metrics map[string]interface{}
}
type ServiceState int
const (
StateStopped ServiceState = iota
StateStarting
StateRunning
StatePausing
StatePaused
StateStopping
)
Management Endpoints (Implemented)
GET /isPaused
— status for all servicesGET /isPaused?name={service}
— status for one serviceGET /pause?name={service|all}
— pause service(s)GET /unpause?name={service|all}
— unpause service(s)
Mutating operations currently use GET.
Pausable Services
Only services implementing the Pauser
interface can be paused:
- Scraper: Blockchain indexing service (pausable)
- Monitor: Address monitoring service (pausable)
Non‑pausable services: control
, api
, ipfs
(if enabled). The monitor service is disabled by default but is pausable when enabled.
Service Coordination
Coordination is limited to toggling internal paused state.
Blockchain Indexing
The Unchained Index (High-Level Overview)
The Unchained Index implementation resides in upstream TrueBlocks libraries. This repository configures and invokes indexing.
Technical Implementation
The index is implemented as a specialized data structure with these key characteristics:
- Bloom Filter Front-End: A probabilistic data structure that quickly determines if an address might appear in a block
- Address-to-Appearance Mapping: Maps each address to a list of its appearances
- Chunked Storage: Divides the index into manageable chunks (typically 1,000,000 blocks per chunk)
- Versioned Format: Includes version metadata to handle format evolution
Internal storage specifics are handled upstream and not duplicated here.
Indexing Process (Conceptual)
High level only: batches of blocks are processed, appearances extracted, and persisted through the underlying TrueBlocks indexing subsystem; batch size and sleep are configured in config.yaml
.
Performance Optimizations
- Parallel Processing: Multiple blocks processed concurrently
- Bloom Filters: Fast negative lookups to avoid unnecessary disk access
- Binary Encoding: Compact storage format for index data
- Caching: Frequently accessed index portions kept in memory
Address Monitoring (Experimental / Limited)
Monitor Implementation
The monitoring system currently provides service enablement/disablement and pause control; advanced notification features are outside this repository.
Technical Implementation
Monitors are implemented using these components:
- Monitor Registry: Central store of all monitored addresses
- Address Index: Fast lookup structure for monitored addresses
- Activity Tracker: Records and timestamps address activity
- Notification Manager: Handles alert distribution based on configuration
Implementation structs are managed upstream.
Monitoring Process
- Registration: Add addresses to the monitor registry
- Block Processing: As new blocks are processed, check for monitored addresses
- Activity Detection: When a monitored address appears, record the activity
- Notification: Based on configuration, send notifications via configured channels
- State Update: Update the monitor's state with the new activity
Optimization Approaches
Optimizations will be added over time as needed.
API Service (When Enabled)
RESTful Interface
The API service provides HTTP endpoints for querying indexed data and managing Khedra's operations.
Technical Implementation
The API is implemented using these components:
- HTTP Server: Handles incoming requests and routing
- Route Handlers: Process specific endpoint requests
- Authentication Middleware: Optional API key verification
- Response Formatter: Structures data in requested format (JSON, CSV, etc.)
- Documentation: Auto-generated Swagger documentation
Server implementation is provided by upstream services packages.
API Endpoints
The API provides endpoints in several categories:
- Status Endpoints: System and service status information
- Index Endpoints: Query the Unchained Index for address appearances
- Monitor Endpoints: Manage and query address monitors
- Chain Endpoints: Blockchain information and operations
- Admin Endpoints: Configuration and management operations
Performance Considerations
- Connection Pooling: Reuse connections for efficiency
- Response Caching: Cache frequent queries with appropriate invalidation
- Pagination: Limit response sizes for large result sets
- Query Optimization: Efficient translation of API queries to index lookups
- Rate Limiting: Prevent resource exhaustion from excessive requests
IPFS Integration (Optional)
Distributed Index Sharing
The IPFS integration enables sharing and retrieving index chunks through the distributed IPFS network.
Technical Implementation
The IPFS functionality is implemented with these components:
- IPFS Node: Either embedded or external IPFS node connection
- Chunk Manager: Handles breaking the index into shareable chunks
- Publishing Logic: Manages uploading chunks to IPFS
- Discovery Service: Finds and retrieves chunks from the network
- Validation: Verifies the integrity of downloaded chunks
Implementation details are abstracted via the services layer.
Distribution Process
- Chunking: Divide the index into manageable chunks with metadata
- Publishing: Add chunks to IPFS and record their content identifiers (CIDs)
- Announcement: Share availability information through the network
- Discovery: Find chunks needed by querying the IPFS network
- Retrieval: Download needed chunks from peers
- Validation: Verify chunk integrity before integration
Optimization Strategies
- Incremental Updates: Share only changed or new chunks
- Prioritized Retrieval: Download most useful chunks first
- Peer Selection: Connect to reliable peers for better performance
- Background Syncing: Retrieve chunks in the background without blocking
- Compressed Storage: Minimize bandwidth and storage requirements
Configuration Management (YAML)
Flexible Configuration System
Khedra's configuration system provides multiple ways to configure the application, with clear precedence rules.
Technical Implementation
The configuration system is implemented with these components:
- YAML Parser: Reads the configuration file format
- Environment Variable Processor: Overrides from environment variables
- Validation Engine: Ensures configuration values are valid
- Defaults Manager: Provides sensible defaults where needed
- Runtime Updater: Handles configuration changes during operation
Authoritative structure lives in pkg/types/config.go
.
Configuration Sources
The system processes configuration from these sources, in order of precedence:
- Environment Variables: Highest precedence, override all other sources
- Configuration File: User-provided settings in YAML format
- Default Values: Built-in defaults for unspecified settings
Validation Rules
The configuration system enforces these kinds of validation:
- Type Validation: Ensures values have the correct data type
- Range Validation: Numeric values within acceptable ranges
- Format Validation: Strings matching required patterns (e.g., URLs)
- Dependency Validation: Related settings are consistent
- Resource Validation: Settings are compatible with available resources
The descriptions above match the repository's current functionality.